pages.dox 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. @mainpage Overview
  3. @anchor mqtt
  4. @brief MQTT 3.1.1 client library
  5. > MQTT stands for MQ Telemetry Transport. It is a publish/subscribe, extremely simple and lightweight messaging protocol, designed for constrained devices and low-bandwidth, high-latency or unreliable networks. The design principles are to minimise network bandwidth and device resource requirements whilst also attempting to ensure reliability and some degree of assurance of delivery. These principles also turn out to make the protocol ideal of the emerging "machine-to-machine" (M2M) or "Internet of Things" world of connected devices, and for mobile applications where bandwidth and battery power are at a premium.
  6. <span style="float:right;margin-right:4em"> &mdash; <i>Official description of MQTT from [mqtt.org](https://mqtt.org)</i></span><br>
  7. This MQTT library implements the client side of the MQTT 3.1.1 protocol. This library is optimized for resource constrained embedded devices. Features of this library include:
  8. - Fully synchronous API, to allow applications to completely manage their concurrency and multi-threading method.
  9. - Operations on fixed buffers, so that applications may control their memory allocation strategy.
  10. - Scalable performance and footprint. The [configuration settings](@ref core_mqtt_config) allow this library to be tailored to a system's resources.
  11. Please see https://github.com/aws/aws-iot-device-sdk-embedded-C/tree/main/demos/mqtt for example code demonstrating integration with TLS.
  12. @section mqtt_memory_requirements Memory Requirements
  13. @brief Memory requirements of the MQTT library.
  14. @include{doc} size_table.md
  15. */
  16. /**
  17. @page mqtt_design Design
  18. @brief Architecture of the MQTT library.
  19. This MQTT client library provides an implementation of the [MQTT 3.1.1 specification](https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html). It is optimized for resource constrained devices and does not allocate any memory.
  20. @section mqtt_interfaces Interfaces and Callbacks
  21. The MQTT library relies on interfaces to dissociate itself from platform specific functionality,
  22. such as the transport layer or maintaining time. Interfaces used by MQTT are simply function pointers
  23. with expectations of behavior.
  24. The MQTT library expects the application to provide implementations for the following interfaces:
  25. <table>
  26. <tr>
  27. <td><b>Function Pointer</b></td>
  28. <td><b>Use</b></td>
  29. </tr>
  30. <tr>
  31. <td>@ref TransportRecv_t</td>
  32. <td>Receiving data from an established network connection.</td>
  33. </tr>
  34. <tr>
  35. <td>@ref TransportSend_t</td>
  36. <td>Sending data over an established network connection.</td>
  37. </tr>
  38. <tr>
  39. <td>@ref MQTTGetCurrentTimeFunc_t</td>
  40. <td>Obtaining timestamps for complying with user-specified timeouts and the MQTT keep-alive mechanism.</td>
  41. </tr>
  42. <tr>
  43. <td>@ref MQTTEventCallback_t</td>
  44. <td>Returning packets received from the network to the user application after deserialization.</td>
  45. </tr>
  46. </table>
  47. @section mqtt_serializers Serializers and Deserializers
  48. The managed MQTT API in @ref core_mqtt.h uses a set of serialization and deserialization functions
  49. declared in @ref core_mqtt_serializer.h. If a user does not want to use the functionality provided by the managed API,
  50. these low-level functions can be used directly:
  51. - @ref mqtt_getconnectpacketsize_function <br>
  52. - @ref mqtt_serializeconnect_function <br>
  53. - @ref mqtt_getsubscribepacketsize_function <br>
  54. - @ref mqtt_serializesubscribe_function <br>
  55. - @ref mqtt_getunsubscribepacketsize_function <br>
  56. - @ref mqtt_serializeunsubscribe_function <br>
  57. - @ref mqtt_getpublishpacketsize_function <br>
  58. - @ref mqtt_serializepublish_function <br>
  59. - @ref mqtt_serializepublishheader_function <br>
  60. - @ref mqtt_serializeack_function <br>
  61. - @ref mqtt_getdisconnectpacketsize_function <br>
  62. - @ref mqtt_serializedisconnect_function <br>
  63. - @ref mqtt_getpingreqpacketsize_function <br>
  64. - @ref mqtt_serializepingreq_function <br>
  65. - @ref mqtt_deserializepublish_function <br>
  66. - @ref mqtt_deserializeack_function <br>
  67. - @ref mqtt_getincomingpackettypeandlength_function <br>
  68. @section mqtt_sessions Sessions and State
  69. The MQTT 3.1.1 protocol allows for a client and server to maintain persistent sessions, which
  70. can be resumed after a reconnect. The elements of a session stored by this client library consist
  71. of the states of incomplete publishes with Quality of Service levels of 1 (at least once), or 2 (exactly once).
  72. These states are stored in the pointers pointed to by @ref MQTTContext_t.outgoingPublishRecords and @ref MQTTContext_t.incomingPublishRecords;
  73. This library does not store any subscription information, nor any information for QoS 0 publishes.
  74. When resuming a persistent session, the client library will resend PUBRELs for all PUBRECs that had been received
  75. for incomplete outgoing QoS 2 publishes. If the broker does not resume the session, then all state information
  76. in the client will be reset.
  77. @note The library stores only the <i>state</i> of incomplete publishes and not the publish payloads. It is the responsibility of the user application to save publish payloads until the publish is complete.
  78. If a persistent session is resumed, then @ref mqtt_publishtoresend_function should be called to obtain the
  79. packet identifiers of incomplete publishes, followed by a call to @ref mqtt_publish_function to resend the
  80. unacknowledged publish.
  81. @section mqtt_receivepackets Packet Reception
  82. MQTT Packets are received from the network with calls to @ref mqtt_processloop_function or @ref mqtt_receiveloop_function. These functions are mostly identical,
  83. with the exception of keep-alive; the former sends ping requests and processes ping responses to ensure the MQTT session does not remain idle for more than the keep-alive interval, while the latter does not.
  84. Since calls to @ref mqtt_publish_function, @ref mqtt_subscribe_function, and @ref mqtt_unsubscribe_function only send packets and do not
  85. wait for acknowledgments, a call to @ref mqtt_processloop_function or @ref mqtt_receiveloop_function must follow in order to receive any expected acknowledgment.
  86. The exception is @ref mqtt_connect_function; since a MQTT session cannot be considered established until the server acknowledges a CONNECT packet with a CONNACK,
  87. the function waits until the CONNACK is received.
  88. @subsection mqtt_receivetimeout Runtime Timeouts passed to MQTT library
  89. @ref mqtt_connect_function, @ref mqtt_processloop_function, and @ref mqtt_receiveloop_function all accept a timeout parameter for packet reception.<br>
  90. For the @ref mqtt_connect_function, if this value is set to 0, then instead of a time-based loop, it will attempt to call the transport receive function up to a maximum number of retries,
  91. which is defined by @ref MQTT_MAX_CONNACK_RECEIVE_RETRY_COUNT.
  92. For @ref mqtt_processloop_function and @ref mqtt_receiveloop_function, the timeout value represents the <i>minimum</i> duration that will be spent in the function, provided there are no network errors.
  93. Should the timeout be set to 0, then the loop will run for a single iteration. A single iteration of a loop consists of an attempt to receive a single byte from the network, and
  94. if the single byte receive was successful, then attempt(s) to receive the rest of the packet (with retry attempts governed by @ref MQTT_RECV_POLLING_TIMEOUT_MS), followed by sending acknowledgement response, if needed
  95. (with retry attempts governed by @ref MQTT_SEND_TIMEOUT_MS), and then, finally deserialization of the packet received and a call to the application callback.
  96. If the first read did not succeed, then instead the library checks if a ping request needs to be sent (only for the process loop).
  97. See the below diagrams for a representation of the above flows:
  98. | MQTT Connect Diagram | MQTT ProcessLoop Diagram | MQTT ReceiveLoop Diagram |
  99. | :--: | :--: | :--: |
  100. |<img src="mqtt_connect_design.png" alt="MQTT Connect" width=100% /> |<img src="mqtt_processloop_design.png" alt="MQTT Process Loop" width=100% /> |<img src="mqtt_receiveloop_design.png" alt="MQTT Receive Loop" width=100% /> |
  101. @section mqtt_keepalive Keep-Alive
  102. The MQTT standard specifies a keep-alive mechanism to detect half-open or otherwise unusable network connections.
  103. An MQTT client will send periodic ping requests (PINGREQ) to the server if the connection is idle. The MQTT server must respond to ping requests with a ping response (PINGRESP).
  104. In this library, @ref mqtt_processloop_function handles sending of PINGREQs and processing corresponding PINGRESPs to comply with the keep-alive interval set in @ref MQTTContext_t.keepAliveIntervalSec.
  105. The standard does not specify the time duration within which the server has to respond to a ping request, noting only a "reasonable amount of time". If the response to a ping request is not received within @ref MQTT_PINGRESP_TIMEOUT_MS, this library assumes that the connection is dead.
  106. If @ref mqtt_receiveloop_function is used instead of @ref mqtt_processloop_function, then no ping requests are sent. The application must ensure the connection does not remain idle for more than the keep-alive interval by calling @ref mqtt_ping_function to send ping requests.
  107. The timestamp in @ref MQTTContext_t.lastPacketTxTime indicates when a packet was last sent by the library.
  108. Sending any ping request sets the @ref MQTTContext_t.waitingForPingResp flag. This flag is cleared by @ref mqtt_processloop_function when a ping response is received. If @ref mqtt_receiveloop_function is used instead, then this flag must be cleared manually by the application's callback.
  109. */
  110. /**
  111. @page core_mqtt_config Configurations
  112. @brief Configurations of the MQTT Library.
  113. <!-- @par configpagestyle allows the @section titles to be styled according to style.css -->
  114. @par configpagestyle
  115. Some configuration settings are C pre-processor constants, and some are function-like macros for logging. They can be set with a `#define` in the config file (**core_mqtt_config.h**) or by using a compiler option such as -D in gcc.
  116. @section MQTT_DO_NOT_USE_CUSTOM_CONFIG
  117. @copydoc MQTT_DO_NOT_USE_CUSTOM_CONFIG
  118. @section MQTT_PINGRESP_TIMEOUT_MS
  119. @copydoc MQTT_PINGRESP_TIMEOUT_MS
  120. @section MQTT_RECV_POLLING_TIMEOUT_MS
  121. @copydoc MQTT_RECV_POLLING_TIMEOUT_MS
  122. @section MQTT_SEND_TIMEOUT_MS
  123. @copydoc MQTT_SEND_TIMEOUT_MS
  124. @section MQTT_MAX_CONNACK_RECEIVE_RETRY_COUNT
  125. @copydoc MQTT_MAX_CONNACK_RECEIVE_RETRY_COUNT
  126. @section mqtt_logerror LogError
  127. @copydoc LogError
  128. @section mqtt_logwarn LogWarn
  129. @copydoc LogWarn
  130. @section mqtt_loginfo LogInfo
  131. @copydoc LogInfo
  132. @section mqtt_logdebug LogDebug
  133. @copydoc LogDebug
  134. */
  135. /**
  136. @page mqtt_functions Functions
  137. @brief Primary functions of the MQTT library:<br><br>
  138. @subpage mqtt_init_function <br>
  139. @subpage mqtt_connect_function <br>
  140. @subpage mqtt_subscribe_function <br>
  141. @subpage mqtt_publish_function <br>
  142. @subpage mqtt_ping_function <br>
  143. @subpage mqtt_unsubscribe_function <br>
  144. @subpage mqtt_disconnect_function <br>
  145. @subpage mqtt_processloop_function <br>
  146. @subpage mqtt_receiveloop_function <br>
  147. @subpage mqtt_getpacketid_function <br>
  148. @subpage mqtt_getsubackstatuscodes_function <br>
  149. @subpage mqtt_status_strerror_function <br>
  150. @subpage mqtt_publishtoresend_function <br><br>
  151. Serializer functions of the MQTT library:<br><br>
  152. @subpage mqtt_getconnectpacketsize_function <br>
  153. @subpage mqtt_serializeconnect_function <br>
  154. @subpage mqtt_getsubscribepacketsize_function <br>
  155. @subpage mqtt_serializesubscribe_function <br>
  156. @subpage mqtt_getunsubscribepacketsize_function <br>
  157. @subpage mqtt_serializeunsubscribe_function <br>
  158. @subpage mqtt_getpublishpacketsize_function <br>
  159. @subpage mqtt_serializepublish_function <br>
  160. @subpage mqtt_serializepublishheader_function <br>
  161. @subpage mqtt_serializeack_function <br>
  162. @subpage mqtt_getdisconnectpacketsize_function <br>
  163. @subpage mqtt_serializedisconnect_function <br>
  164. @subpage mqtt_getpingreqpacketsize_function <br>
  165. @subpage mqtt_serializepingreq_function <br>
  166. @subpage mqtt_deserializepublish_function <br>
  167. @subpage mqtt_deserializeack_function <br>
  168. @subpage mqtt_getincomingpackettypeandlength_function <br>
  169. @page mqtt_init_function MQTT_Init
  170. @snippet core_mqtt.h declare_mqtt_init
  171. @copydoc MQTT_Init
  172. @page mqtt_connect_function MQTT_Connect
  173. @snippet core_mqtt.h declare_mqtt_connect
  174. @copydoc MQTT_Connect
  175. @page mqtt_subscribe_function MQTT_Subscribe
  176. @snippet core_mqtt.h declare_mqtt_subscribe
  177. @copydoc MQTT_Subscribe
  178. @page mqtt_publish_function MQTT_Publish
  179. @snippet core_mqtt.h declare_mqtt_publish
  180. @copydoc MQTT_Publish
  181. @page mqtt_ping_function MQTT_Ping
  182. @snippet core_mqtt.h declare_mqtt_ping
  183. @copydoc MQTT_Ping
  184. @page mqtt_unsubscribe_function MQTT_Unsubscribe
  185. @snippet core_mqtt.h declare_mqtt_unsubscribe
  186. @copydoc MQTT_Unsubscribe
  187. @page mqtt_disconnect_function MQTT_Disconnect
  188. @snippet core_mqtt.h declare_mqtt_disconnect
  189. @copydoc MQTT_Disconnect
  190. @page mqtt_processloop_function MQTT_ProcessLoop
  191. @snippet core_mqtt.h declare_mqtt_processloop
  192. @copydoc MQTT_ProcessLoop
  193. @page mqtt_receiveloop_function MQTT_ReceiveLoop
  194. @snippet core_mqtt.h declare_mqtt_receiveloop
  195. @copydoc MQTT_ReceiveLoop
  196. @page mqtt_getpacketid_function MQTT_GetPacketId
  197. @snippet core_mqtt.h declare_mqtt_getpacketid
  198. @copydoc MQTT_GetPacketId
  199. @page mqtt_getsubackstatuscodes_function MQTT_GetSubAckStatusCodes
  200. @snippet core_mqtt.h declare_mqtt_getsubackstatuscodes
  201. @copydoc MQTT_GetSubAckStatusCodes
  202. @page mqtt_status_strerror_function MQTT_Status_strerror
  203. @snippet core_mqtt.h declare_mqtt_status_strerror
  204. @copydoc MQTT_Status_strerror
  205. @page mqtt_publishtoresend_function MQTT_PublishToResend
  206. @snippet core_mqtt_state.h declare_mqtt_publishtoresend
  207. @copydoc MQTT_PublishToResend
  208. @page mqtt_getconnectpacketsize_function MQTT_GetConnectPacketSize
  209. @snippet core_mqtt_serializer.h declare_mqtt_getconnectpacketsize
  210. @copydoc MQTT_GetConnectPacketSize
  211. @page mqtt_serializeconnect_function MQTT_SerializeConnect
  212. @snippet core_mqtt_serializer.h declare_mqtt_serializeconnect
  213. @copydoc MQTT_SerializeConnect
  214. @page mqtt_getsubscribepacketsize_function MQTT_GetSubscribePacketSize
  215. @snippet core_mqtt_serializer.h declare_mqtt_getsubscribepacketsize
  216. @copydoc MQTT_GetSubscribePacketSize
  217. @page mqtt_serializesubscribe_function MQTT_SerializeSubscribe
  218. @snippet core_mqtt_serializer.h declare_mqtt_serializesubscribe
  219. @copydoc MQTT_SerializeSubscribe
  220. @page mqtt_getunsubscribepacketsize_function MQTT_GetUnsubscribePacketSize
  221. @snippet core_mqtt_serializer.h declare_mqtt_getunsubscribepacketsize
  222. @copydoc MQTT_GetUnsubscribePacketSize
  223. @page mqtt_serializeunsubscribe_function MQTT_SerializeUnsubscribe
  224. @snippet core_mqtt_serializer.h declare_mqtt_serializeunsubscribe
  225. @copydoc MQTT_SerializeUnsubscribe
  226. @page mqtt_getpublishpacketsize_function MQTT_GetPublishPacketSize
  227. @snippet core_mqtt_serializer.h declare_mqtt_getpublishpacketsize
  228. @copydoc MQTT_GetPublishPacketSize
  229. @page mqtt_serializepublish_function MQTT_SerializePublish
  230. @snippet core_mqtt_serializer.h declare_mqtt_serializepublish
  231. @copydoc MQTT_SerializePublish
  232. @page mqtt_serializepublishheader_function MQTT_SerializePublishHeader
  233. @snippet core_mqtt_serializer.h declare_mqtt_serializepublishheader
  234. @copydoc MQTT_SerializePublishHeader
  235. @page mqtt_serializeack_function MQTT_SerializeAck
  236. @snippet core_mqtt_serializer.h declare_mqtt_serializeack
  237. @copydoc MQTT_SerializeAck
  238. @page mqtt_getdisconnectpacketsize_function MQTT_GetDisconnectPacketSize
  239. @snippet core_mqtt_serializer.h declare_mqtt_getdisconnectpacketsize
  240. @copydoc MQTT_GetDisconnectPacketSize
  241. @page mqtt_serializedisconnect_function MQTT_SerializeDisconnect
  242. @snippet core_mqtt_serializer.h declare_mqtt_serializedisconnect
  243. @copydoc MQTT_SerializeDisconnect
  244. @page mqtt_getpingreqpacketsize_function MQTT_GetPingreqPacketSize
  245. @snippet core_mqtt_serializer.h declare_mqtt_getpingreqpacketsize
  246. @copydoc MQTT_GetPingreqPacketSize
  247. @page mqtt_serializepingreq_function MQTT_SerializePingreq
  248. @snippet core_mqtt_serializer.h declare_mqtt_serializepingreq
  249. @copydoc MQTT_SerializePingreq
  250. @page mqtt_deserializepublish_function MQTT_DeserializePublish
  251. @snippet core_mqtt_serializer.h declare_mqtt_deserializepublish
  252. @copydoc MQTT_DeserializePublish
  253. @page mqtt_deserializeack_function MQTT_DeserializeAck
  254. @snippet core_mqtt_serializer.h declare_mqtt_deserializeack
  255. @copydoc MQTT_DeserializeAck
  256. @page mqtt_getincomingpackettypeandlength_function MQTT_GetIncomingPacketTypeAndLength
  257. @snippet core_mqtt_serializer.h declare_mqtt_getincomingpackettypeandlength
  258. @copydoc MQTT_GetIncomingPacketTypeAndLength
  259. */
  260. /**
  261. @defgroup mqtt_enum_types Enumerated Types
  262. @brief Enumerated types of the MQTT library
  263. */
  264. /**
  265. @defgroup mqtt_callback_types Callback Types
  266. @brief Callback function pointer types of the MQTT library
  267. */
  268. /**
  269. @defgroup mqtt_struct_types Parameter Structures
  270. @brief Structures passed as parameters to [MQTT library functions](@ref mqtt_functions)
  271. These structures are passed as parameters to library functions. Documentation for these structures will state the functions associated with each parameter structure and the purpose of each member.
  272. */
  273. /**
  274. @defgroup mqtt_basic_types Basic Types
  275. @brief Primitive types of the MQTT library.
  276. */
  277. /**
  278. @defgroup mqtt_constants Constants
  279. @brief Constants defined in the MQTT library
  280. */