MQTTClient.h 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972
  1. /*******************************************************************************
  2. * Copyright (c) 2009, 2022 IBM Corp., Ian Craggs and others
  3. *
  4. * All rights reserved. This program and the accompanying materials
  5. * are made available under the terms of the Eclipse Public License v2.0
  6. * and Eclipse Distribution License v1.0 which accompany this distribution.
  7. *
  8. * The Eclipse Public License is available at
  9. * https://www.eclipse.org/legal/epl-2.0/
  10. * and the Eclipse Distribution License is available at
  11. * http://www.eclipse.org/org/documents/edl-v10.php.
  12. *
  13. * Contributors:
  14. * Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs, Allan Stockdill-Mander - SSL updates
  16. * Ian Craggs - multiple server connection support
  17. * Ian Craggs - MQTT 3.1.1 support
  18. * Ian Craggs - remove const from eyecatchers #168
  19. *******************************************************************************/
  20. /**
  21. * @cond MQTTClient_internal
  22. * @mainpage MQTT Client Library Internals
  23. * In the beginning there was one MQTT C client library, MQTTClient, as implemented in MQTTClient.c
  24. * This library was designed to be easy to use for applications which didn't mind if some of the calls
  25. * blocked for a while. For instance, the MQTTClient_connect call will block until a successful
  26. * connection has completed, or a connection has failed, which could be as long as the "connection
  27. * timeout" interval, whose default is 30 seconds.
  28. *
  29. * However in mobile devices and other windowing environments, blocking on the GUI thread is a bad
  30. * thing as it causes the user interface to freeze. Hence a new API, MQTTAsync, implemented
  31. * in MQTTAsync.c, was devised. There are no blocking calls in this library, so it is well suited
  32. * to GUI and mobile environments, at the expense of some extra complexity.
  33. *
  34. * Both libraries are designed to be sparing in the use of threads. So multiple client objects are
  35. * handled by one or two threads, with a select call in Socket_getReadySocket(), used to determine
  36. * when a socket has incoming data. This API is thread safe: functions may be called by multiple application
  37. * threads, with the exception of ::MQTTClient_yield and ::MQTTClient_receive, which are intended
  38. * for single threaded environments only.
  39. *
  40. * @endcond
  41. * @cond MQTTClient_main
  42. * @mainpage MQTT Client library for C (MQTTClient)
  43. * © Copyright 2009, 2022 IBM Corp., Ian Craggs and others
  44. *
  45. * @brief An MQTT client library in C.
  46. *
  47. * These pages describe the original more synchronous API which might be
  48. * considered easier to use. Some of the calls will block. For the new
  49. * totally asynchronous API where no calls block, which is especially suitable
  50. * for use in windowed environments, see the
  51. * <a href="../../MQTTAsync/html/index.html">MQTT C Client Asynchronous API Documentation</a>.
  52. * The MQTTClient API is not thread safe, whereas the MQTTAsync API is.
  53. *
  54. * An MQTT client application connects to MQTT-capable servers.
  55. * A typical client is responsible for collecting information from a telemetry
  56. * device and publishing the information to the server. It can also subscribe
  57. * to topics, receive messages, and use this information to control the
  58. * telemetry device.
  59. *
  60. * MQTT clients implement the published MQTT v3 protocol. You can write your own
  61. * API to the MQTT protocol using the programming language and platform of your
  62. * choice. This can be time-consuming and error-prone.
  63. *
  64. * To simplify writing MQTT client applications, this library encapsulates
  65. * the MQTT v3 protocol for you. Using this library enables a fully functional
  66. * MQTT client application to be written in a few lines of code.
  67. * The information presented here documents the API provided
  68. * by the MQTT Client library for C.
  69. *
  70. * <b>Using the client</b><br>
  71. * Applications that use the client library typically use a similar structure:
  72. * <ul>
  73. * <li>Create a client object</li>
  74. * <li>Set the options to connect to an MQTT server</li>
  75. * <li>Set up callback functions if multi-threaded (asynchronous mode)
  76. * operation is being used (see @ref async).</li>
  77. * <li>Subscribe to any topics the client needs to receive</li>
  78. * <li>Repeat until finished:</li>
  79. * <ul>
  80. * <li>Publish any messages the client needs to</li>
  81. * <li>Handle any incoming messages</li>
  82. * </ul>
  83. * <li>Disconnect the client</li>
  84. * <li>Free any memory being used by the client</li>
  85. * </ul>
  86. * Some simple examples are shown here:
  87. * <ul>
  88. * <li>@ref pubsync</li>
  89. * <li>@ref pubasync</li>
  90. * <li>@ref subasync</li>
  91. * </ul>
  92. * Additional information about important concepts is provided here:
  93. * <ul>
  94. * <li>@ref async</li>
  95. * <li>@ref callbacks</li>
  96. * <li>@ref wildcard</li>
  97. * <li>@ref qos</li>
  98. * <li>@ref tracing</li>
  99. * </ul>
  100. * @endcond
  101. */
  102. /*
  103. /// @cond EXCLUDE
  104. */
  105. #if !defined(MQTTCLIENT_H)
  106. #define MQTTCLIENT_H
  107. #if defined(__cplusplus)
  108. extern "C" {
  109. #endif
  110. #include <stdio.h>
  111. /*
  112. /// @endcond
  113. */
  114. #include "MQTTExportDeclarations.h"
  115. #include "MQTTProperties.h"
  116. #include "MQTTReasonCodes.h"
  117. #include "MQTTSubscribeOpts.h"
  118. #if !defined(NO_PERSISTENCE)
  119. #include "MQTTClientPersistence.h"
  120. #endif
  121. /**
  122. * Return code: No error. Indicates successful completion of an MQTT client
  123. * operation.
  124. */
  125. #define MQTTCLIENT_SUCCESS 0
  126. /**
  127. * Return code: A generic error code indicating the failure of an MQTT client
  128. * operation.
  129. */
  130. #define MQTTCLIENT_FAILURE -1
  131. /* error code -2 is MQTTCLIENT_PERSISTENCE_ERROR */
  132. /**
  133. * Return code: The client is disconnected.
  134. */
  135. #define MQTTCLIENT_DISCONNECTED -3
  136. /**
  137. * Return code: The maximum number of messages allowed to be simultaneously
  138. * in-flight has been reached.
  139. */
  140. #define MQTTCLIENT_MAX_MESSAGES_INFLIGHT -4
  141. /**
  142. * Return code: An invalid UTF-8 string has been detected.
  143. */
  144. #define MQTTCLIENT_BAD_UTF8_STRING -5
  145. /**
  146. * Return code: A NULL parameter has been supplied when this is invalid.
  147. */
  148. #define MQTTCLIENT_NULL_PARAMETER -6
  149. /**
  150. * Return code: The topic has been truncated (the topic string includes
  151. * embedded NULL characters). String functions will not access the full topic.
  152. * Use the topic length value to access the full topic.
  153. */
  154. #define MQTTCLIENT_TOPICNAME_TRUNCATED -7
  155. /**
  156. * Return code: A structure parameter does not have the correct eyecatcher
  157. * and version number.
  158. */
  159. #define MQTTCLIENT_BAD_STRUCTURE -8
  160. /**
  161. * Return code: A QoS value that falls outside of the acceptable range (0,1,2)
  162. */
  163. #define MQTTCLIENT_BAD_QOS -9
  164. /**
  165. * Return code: Attempting SSL connection using non-SSL version of library
  166. */
  167. #define MQTTCLIENT_SSL_NOT_SUPPORTED -10
  168. /**
  169. * Return code: unrecognized MQTT version
  170. */
  171. #define MQTTCLIENT_BAD_MQTT_VERSION -11
  172. /**
  173. * Return code: protocol prefix in serverURI should be:
  174. * @li @em tcp:// or @em mqtt:// - Insecure TCP
  175. * @li @em ssl:// or @em mqtts:// - Encrypted SSL/TLS
  176. * @li @em ws:// - Insecure websockets
  177. * @li @em wss:// - Secure web sockets
  178. * The TLS enabled prefixes (ssl, mqtts, wss) are only valid if a TLS
  179. * version of the library is linked with.
  180. */
  181. #define MQTTCLIENT_BAD_PROTOCOL -14
  182. /**
  183. * Return code: option not applicable to the requested version of MQTT
  184. */
  185. #define MQTTCLIENT_BAD_MQTT_OPTION -15
  186. /**
  187. * Return code: call not applicable to the requested version of MQTT
  188. */
  189. #define MQTTCLIENT_WRONG_MQTT_VERSION -16
  190. /**
  191. * Return code: 0 length will topic on connect
  192. */
  193. #define MQTTCLIENT_0_LEN_WILL_TOPIC -17
  194. /**
  195. * Default MQTT version to connect with. Use 3.1.1 then fall back to 3.1
  196. */
  197. #define MQTTVERSION_DEFAULT 0
  198. /**
  199. * MQTT version to connect with: 3.1
  200. */
  201. #define MQTTVERSION_3_1 3
  202. /**
  203. * MQTT version to connect with: 3.1.1
  204. */
  205. #define MQTTVERSION_3_1_1 4
  206. /**
  207. * MQTT version to connect with: 5
  208. */
  209. #define MQTTVERSION_5 5
  210. /**
  211. * Bad return code from subscribe, as defined in the 3.1.1 specification
  212. */
  213. #define MQTT_BAD_SUBSCRIBE 0x80
  214. /**
  215. * Initialization options
  216. */
  217. typedef struct
  218. {
  219. /** The eyecatcher for this structure. Must be MQTG. */
  220. char struct_id[4];
  221. /** The version number of this structure. Must be 0 */
  222. int struct_version;
  223. /** 1 = we do openssl init, 0 = leave it to the application */
  224. int do_openssl_init;
  225. } MQTTClient_init_options;
  226. #define MQTTClient_init_options_initializer { {'M', 'Q', 'T', 'G'}, 0, 0 }
  227. /**
  228. * Global init of mqtt library. Call once on program start to set global behaviour.
  229. * do_openssl_init - if mqtt library should initialize OpenSSL (1) or rely on the caller to do it before using the library (0)
  230. */
  231. LIBMQTT_API void MQTTClient_global_init(MQTTClient_init_options* inits);
  232. /**
  233. * A handle representing an MQTT client. A valid client handle is available
  234. * following a successful call to MQTTClient_create().
  235. */
  236. typedef void* MQTTClient;
  237. /**
  238. * A value representing an MQTT message. A delivery token is returned to the
  239. * client application when a message is published. The token can then be used to
  240. * check that the message was successfully delivered to its destination (see
  241. * MQTTClient_publish(),
  242. * MQTTClient_publishMessage(),
  243. * MQTTClient_deliveryComplete(),
  244. * MQTTClient_waitForCompletion() and
  245. * MQTTClient_getPendingDeliveryTokens()).
  246. */
  247. typedef int MQTTClient_deliveryToken;
  248. typedef int MQTTClient_token;
  249. /**
  250. * A structure representing the payload and attributes of an MQTT message. The
  251. * message topic is not part of this structure (see MQTTClient_publishMessage(),
  252. * MQTTClient_publish(), MQTTClient_receive(), MQTTClient_freeMessage()
  253. * and MQTTClient_messageArrived()).
  254. */
  255. typedef struct
  256. {
  257. /** The eyecatcher for this structure. must be MQTM. */
  258. char struct_id[4];
  259. /** The version number of this structure. Must be 0 or 1
  260. * 0 indicates no message properties */
  261. int struct_version;
  262. /** The length of the MQTT message payload in bytes. */
  263. int payloadlen;
  264. /** A pointer to the payload of the MQTT message. */
  265. void* payload;
  266. /**
  267. * The quality of service (QoS) assigned to the message.
  268. * There are three levels of QoS:
  269. * <DL>
  270. * <DT><B>QoS0</B></DT>
  271. * <DD>Fire and forget - the message may not be delivered</DD>
  272. * <DT><B>QoS1</B></DT>
  273. * <DD>At least once - the message will be delivered, but may be
  274. * delivered more than once in some circumstances.</DD>
  275. * <DT><B>QoS2</B></DT>
  276. * <DD>Once and one only - the message will be delivered exactly once.</DD>
  277. * </DL>
  278. */
  279. int qos;
  280. /**
  281. * The retained flag serves two purposes depending on whether the message
  282. * it is associated with is being published or received.
  283. *
  284. * <b>retained = true</b><br>
  285. * For messages being published, a true setting indicates that the MQTT
  286. * server should retain a copy of the message. The message will then be
  287. * transmitted to new subscribers to a topic that matches the message topic.
  288. * For subscribers registering a new subscription, the flag being true
  289. * indicates that the received message is not a new one, but one that has
  290. * been retained by the MQTT server.
  291. *
  292. * <b>retained = false</b> <br>
  293. * For publishers, this indicates that this message should not be retained
  294. * by the MQTT server. For subscribers, a false setting indicates this is
  295. * a normal message, received as a result of it being published to the
  296. * server.
  297. */
  298. int retained;
  299. /**
  300. * The dup flag indicates whether or not this message is a duplicate.
  301. * It is only meaningful when receiving QoS1 messages. When true, the
  302. * client application should take appropriate action to deal with the
  303. * duplicate message.
  304. */
  305. int dup;
  306. /** The message identifier is normally reserved for internal use by the
  307. * MQTT client and server.
  308. */
  309. int msgid;
  310. /**
  311. * The MQTT V5 properties associated with the message.
  312. */
  313. MQTTProperties properties;
  314. } MQTTClient_message;
  315. #define MQTTClient_message_initializer { {'M', 'Q', 'T', 'M'}, 1, 0, NULL, 0, 0, 0, 0, MQTTProperties_initializer }
  316. /**
  317. * This is a callback function. The client application
  318. * must provide an implementation of this function to enable asynchronous
  319. * receipt of messages. The function is registered with the client library by
  320. * passing it as an argument to MQTTClient_setCallbacks(). It is
  321. * called by the client library when a new message that matches a client
  322. * subscription has been received from the server. This function is executed on
  323. * a separate thread to the one on which the client application is running.
  324. * @param context A pointer to the <i>context</i> value originally passed to
  325. * MQTTClient_setCallbacks(), which contains any application-specific context.
  326. * @param topicName The topic associated with the received message.
  327. * @param topicLen The length of the topic if there are one
  328. * more NULL characters embedded in <i>topicName</i>, otherwise <i>topicLen</i>
  329. * is 0. If <i>topicLen</i> is 0, the value returned by <i>strlen(topicName)</i>
  330. * can be trusted. If <i>topicLen</i> is greater than 0, the full topic name
  331. * can be retrieved by accessing <i>topicName</i> as a byte array of length
  332. * <i>topicLen</i>.
  333. * @param message The MQTTClient_message structure for the received message.
  334. * This structure contains the message payload and attributes.
  335. * @return This function must return 0 or 1 indicating whether or not
  336. * the message has been safely received by the client application. <br>
  337. * Returning 1 indicates that the message has been successfully handled.
  338. * To free the message storage, ::MQTTClient_freeMessage must be called.
  339. * To free the topic name storage, ::MQTTClient_free must be called.<br>
  340. * Returning 0 indicates that there was a problem. In this
  341. * case, the client library will reinvoke MQTTClient_messageArrived() to
  342. * attempt to deliver the message to the application again.
  343. * Do not free the message and topic storage when returning 0, otherwise
  344. * the redelivery will fail.
  345. */
  346. typedef int MQTTClient_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* message);
  347. /**
  348. * This is a callback function. The client application
  349. * must provide an implementation of this function to enable asynchronous
  350. * notification of delivery of messages. The function is registered with the
  351. * client library by passing it as an argument to MQTTClient_setCallbacks().
  352. * It is called by the client library after the client application has
  353. * published a message to the server. It indicates that the necessary
  354. * handshaking and acknowledgements for the requested quality of service (see
  355. * MQTTClient_message.qos) have been completed. This function is executed on a
  356. * separate thread to the one on which the client application is running.
  357. * <b>Note:</b>MQTTClient_deliveryComplete() is not called when messages are
  358. * published at QoS0.
  359. * @param context A pointer to the <i>context</i> value originally passed to
  360. * MQTTClient_setCallbacks(), which contains any application-specific context.
  361. * @param dt The ::MQTTClient_deliveryToken associated with
  362. * the published message. Applications can check that all messages have been
  363. * correctly published by matching the delivery tokens returned from calls to
  364. * MQTTClient_publish() and MQTTClient_publishMessage() with the tokens passed
  365. * to this callback.
  366. */
  367. typedef void MQTTClient_deliveryComplete(void* context, MQTTClient_deliveryToken dt);
  368. /**
  369. * This is a callback function. The client application
  370. * must provide an implementation of this function to enable asynchronous
  371. * notification of the loss of connection to the server. The function is
  372. * registered with the client library by passing it as an argument to
  373. * MQTTClient_setCallbacks(). It is called by the client library if the client
  374. * loses its connection to the server. The client application must take
  375. * appropriate action, such as trying to reconnect or reporting the problem.
  376. * This function is executed on a separate thread to the one on which the
  377. * client application is running.
  378. * @param context A pointer to the <i>context</i> value originally passed to
  379. * MQTTClient_setCallbacks(), which contains any application-specific context.
  380. * @param cause The reason for the disconnection.
  381. * Currently, <i>cause</i> is always set to NULL.
  382. */
  383. typedef void MQTTClient_connectionLost(void* context, char* cause);
  384. /**
  385. * This function sets the callback functions for a specific client.
  386. * If your client application doesn't use a particular callback, set the
  387. * relevant parameter to NULL. Calling MQTTClient_setCallbacks() puts the
  388. * client into multi-threaded mode. Any necessary message acknowledgements and
  389. * status communications are handled in the background without any intervention
  390. * from the client application. See @ref async for more information.
  391. *
  392. * <b>Note:</b> The MQTT client must be disconnected when this function is
  393. * called.
  394. * @param handle A valid client handle from a successful call to
  395. * MQTTClient_create().
  396. * @param context A pointer to any application-specific context. The
  397. * the <i>context</i> pointer is passed to each of the callback functions to
  398. * provide access to the context information in the callback.
  399. * @param cl A pointer to an MQTTClient_connectionLost() callback
  400. * function. You can set this to NULL if your application doesn't handle
  401. * disconnections.
  402. * @param ma A pointer to an MQTTClient_messageArrived() callback
  403. * function. This callback function must be set when you call
  404. * MQTTClient_setCallbacks(), as otherwise there would be nowhere to deliver
  405. * any incoming messages.
  406. * @param dc A pointer to an MQTTClient_deliveryComplete() callback
  407. * function. You can set this to NULL if your application publishes
  408. * synchronously or if you do not want to check for successful delivery.
  409. * @return ::MQTTCLIENT_SUCCESS if the callbacks were correctly set,
  410. * ::MQTTCLIENT_FAILURE if an error occurred.
  411. */
  412. LIBMQTT_API int MQTTClient_setCallbacks(MQTTClient handle, void* context, MQTTClient_connectionLost* cl,
  413. MQTTClient_messageArrived* ma, MQTTClient_deliveryComplete* dc);
  414. /**
  415. * This is a callback function, which will be called when the a disconnect
  416. * packet is received from the server. This applies to MQTT V5 and above only.
  417. * @param context A pointer to the <i>context</i> value originally passed to
  418. * ::MQTTClient_setDisconnected(), which contains any application-specific context.
  419. * @param properties The MQTT V5 properties received with the disconnect, if any.
  420. * @param reasonCode The MQTT V5 reason code received with the disconnect.
  421. * Currently, <i>cause</i> is always set to NULL.
  422. */
  423. typedef void MQTTClient_disconnected(void* context, MQTTProperties* properties,
  424. enum MQTTReasonCodes reasonCode);
  425. /**
  426. * Sets the MQTTClient_disconnected() callback function for a client. This will be called
  427. * if a disconnect packet is received from the server. Only valid for MQTT V5 and above.
  428. * @param handle A valid client handle from a successful call to
  429. * MQTTClient_create().
  430. * @param context A pointer to any application-specific context. The
  431. * the <i>context</i> pointer is passed to each of the callback functions to
  432. * provide access to the context information in the callback.
  433. * @param co A pointer to an MQTTClient_disconnected() callback
  434. * function. NULL removes the callback setting.
  435. * @return ::MQTTCLIENT_SUCCESS if the callbacks were correctly set,
  436. * ::MQTTCLIENT_FAILURE if an error occurred.
  437. */
  438. LIBMQTT_API int MQTTClient_setDisconnected(MQTTClient handle, void* context, MQTTClient_disconnected* co);
  439. /**
  440. * This is a callback function, the MQTT V5 version of MQTTClient_deliveryComplete().
  441. * The client application
  442. * must provide an implementation of this function to enable asynchronous
  443. * notification of the completed delivery of messages.
  444. * It is called by the client library after the client application has
  445. * published a message to the server. It indicates that the necessary
  446. * handshaking and acknowledgements for the requested quality of service (see
  447. * MQTTClient_message.qos) have been completed. This function is executed on a
  448. * separate thread to the one on which the client application is running.
  449. * <b>Note:</b> It is not called when messages are published at QoS0.
  450. * @param context A pointer to the <i>context</i> value originally passed to
  451. * MQTTClient_setCallbacks(), which contains any application-specific context.
  452. * @param dt The ::MQTTClient_deliveryToken associated with
  453. * the published message. Applications can check that all messages have been
  454. * correctly published by matching the delivery tokens returned from calls to
  455. * MQTTClient_publish() and MQTTClient_publishMessage() with the tokens passed
  456. * to this callback.
  457. * @param packet_type the last received packet type for this completion. For QoS 1
  458. * always PUBACK. For QoS 2 could be PUBREC or PUBCOMP.
  459. * @param properties the MQTT V5 properties returned with the last packet from the server
  460. * @param reasonCode the reason code returned from the server
  461. */
  462. typedef void MQTTClient_published(void* context, int dt, int packet_type, MQTTProperties* properties,
  463. enum MQTTReasonCodes reasonCode);
  464. LIBMQTT_API int MQTTClient_setPublished(MQTTClient handle, void* context, MQTTClient_published* co);
  465. /**
  466. * This function creates an MQTT client ready for connection to the
  467. * specified server and using the specified persistent storage (see
  468. * MQTTClient_persistence). See also MQTTClient_destroy().
  469. * @param handle A pointer to an ::MQTTClient handle. The handle is
  470. * populated with a valid client reference following a successful return from
  471. * this function.
  472. * @param serverURI A null-terminated string specifying the server to
  473. * which the client will connect. It takes the form <i>protocol://host:port</i>.
  474. * Currently, <i>protocol</i> must be:
  475. * <br>
  476. * @em tcp:// or @em mqtt:// - Insecure TCP
  477. * <br>
  478. * @em ssl:// or @em mqtts:// - Encrypted SSL/TLS
  479. * <br>
  480. * @em ws:// - Insecure websockets
  481. * <br>
  482. * @em wss:// - Secure web sockets
  483. * <br>
  484. * The TLS enabled prefixes (ssl, mqtts, wss) are only valid if a TLS
  485. * version of the library is linked with.
  486. * For <i>host</i>, you can specify either an IP address or a host name. For
  487. * instance, to connect to a server running on the local machines with the
  488. * default MQTT port, specify <i>tcp://localhost:1883</i>.
  489. * @param clientId The client identifier passed to the server when the
  490. * client connects to it. It is a null-terminated UTF-8 encoded string.
  491. * @param persistence_type The type of persistence to be used by the client:
  492. * <br>
  493. * ::MQTTCLIENT_PERSISTENCE_NONE: Use in-memory persistence. If the device or
  494. * system on which the client is running fails or is switched off, the current
  495. * state of any in-flight messages is lost and some messages may not be
  496. * delivered even at QoS1 and QoS2.
  497. * <br>
  498. * ::MQTTCLIENT_PERSISTENCE_DEFAULT: Use the default (file system-based)
  499. * persistence mechanism. Status about in-flight messages is held in persistent
  500. * storage and provides some protection against message loss in the case of
  501. * unexpected failure.
  502. * <br>
  503. * ::MQTTCLIENT_PERSISTENCE_USER: Use an application-specific persistence
  504. * implementation. Using this type of persistence gives control of the
  505. * persistence mechanism to the application. The application has to implement
  506. * the MQTTClient_persistence interface.
  507. * @param persistence_context If the application uses
  508. * ::MQTTCLIENT_PERSISTENCE_NONE persistence, this argument is unused and should
  509. * be set to NULL. For ::MQTTCLIENT_PERSISTENCE_DEFAULT persistence, it
  510. * should be set to the location of the persistence directory (if set
  511. * to NULL, the persistence directory used is the working directory).
  512. * Applications that use ::MQTTCLIENT_PERSISTENCE_USER persistence set this
  513. * argument to point to a valid MQTTClient_persistence structure.
  514. * @return ::MQTTCLIENT_SUCCESS if the client is successfully created, otherwise
  515. * an error code is returned.
  516. */
  517. LIBMQTT_API int MQTTClient_create(MQTTClient* handle, const char* serverURI, const char* clientId,
  518. int persistence_type, void* persistence_context);
  519. /** Options for the ::MQTTClient_createWithOptions call */
  520. typedef struct
  521. {
  522. /** The eyecatcher for this structure. must be MQCO. */
  523. char struct_id[4];
  524. /** The version number of this structure. Must be 0 */
  525. int struct_version;
  526. /** Whether the MQTT version is 3.1, 3.1.1, or 5. To use V5, this must be set.
  527. * MQTT V5 has to be chosen here, because during the create call the message persistence
  528. * is initialized, and we want to know whether the format of any persisted messages
  529. * is appropriate for the MQTT version we are going to connect with. Selecting 3.1 or
  530. * 3.1.1 and attempting to read 5.0 persisted messages will result in an error on create. */
  531. int MQTTVersion;
  532. } MQTTClient_createOptions;
  533. #define MQTTClient_createOptions_initializer { {'M', 'Q', 'C', 'O'}, 0, MQTTVERSION_DEFAULT }
  534. /**
  535. * A version of :MQTTClient_create() with additional options.
  536. * This function creates an MQTT client ready for connection to the
  537. * specified server and using the specified persistent storage (see
  538. * MQTTClient_persistence). See also MQTTClient_destroy().
  539. * @param handle A pointer to an ::MQTTClient handle. The handle is
  540. * populated with a valid client reference following a successful return from
  541. * this function.
  542. * @param serverURI A null-terminated string specifying the server to
  543. * which the client will connect. It takes the form <i>protocol://host:port</i>.
  544. * Currently, <i>protocol</i> must be <i>tcp</i> or <i>ssl</i>.
  545. * For <i>host</i>, you can
  546. * specify either an IP address or a host name. For instance, to connect to
  547. * a server running on the local machines with the default MQTT port, specify
  548. * <i>tcp://localhost:1883</i>.
  549. * @param clientId The client identifier passed to the server when the
  550. * client connects to it. It is a null-terminated UTF-8 encoded string.
  551. * @param persistence_type The type of persistence to be used by the client:
  552. * <br>
  553. * ::MQTTCLIENT_PERSISTENCE_NONE: Use in-memory persistence. If the device or
  554. * system on which the client is running fails or is switched off, the current
  555. * state of any in-flight messages is lost and some messages may not be
  556. * delivered even at QoS1 and QoS2.
  557. * <br>
  558. * ::MQTTCLIENT_PERSISTENCE_DEFAULT: Use the default (file system-based)
  559. * persistence mechanism. Status about in-flight messages is held in persistent
  560. * storage and provides some protection against message loss in the case of
  561. * unexpected failure.
  562. * <br>
  563. * ::MQTTCLIENT_PERSISTENCE_USER: Use an application-specific persistence
  564. * implementation. Using this type of persistence gives control of the
  565. * persistence mechanism to the application. The application has to implement
  566. * the MQTTClient_persistence interface.
  567. * @param persistence_context If the application uses
  568. * ::MQTTCLIENT_PERSISTENCE_NONE persistence, this argument is unused and should
  569. * be set to NULL. For ::MQTTCLIENT_PERSISTENCE_DEFAULT persistence, it
  570. * should be set to the location of the persistence directory (if set
  571. * to NULL, the persistence directory used is the working directory).
  572. * Applications that use ::MQTTCLIENT_PERSISTENCE_USER persistence set this
  573. * argument to point to a valid MQTTClient_persistence structure.
  574. * @param options additional options for the create.
  575. * @return ::MQTTCLIENT_SUCCESS if the client is successfully created, otherwise
  576. * an error code is returned.
  577. */
  578. LIBMQTT_API int MQTTClient_createWithOptions(MQTTClient* handle, const char* serverURI, const char* clientId,
  579. int persistence_type, void* persistence_context, MQTTClient_createOptions* options);
  580. /**
  581. * MQTTClient_willOptions defines the MQTT "Last Will and Testament" (LWT) settings for
  582. * the client. In the event that a client unexpectedly loses its connection to
  583. * the server, the server publishes the LWT message to the LWT topic on
  584. * behalf of the client. This allows other clients (subscribed to the LWT topic)
  585. * to be made aware that the client has disconnected. To enable the LWT
  586. * function for a specific client, a valid pointer to an MQTTClient_willOptions
  587. * structure is passed in the MQTTClient_connectOptions structure used in the
  588. * MQTTClient_connect() call that connects the client to the server. The pointer
  589. * to MQTTClient_willOptions can be set to NULL if the LWT function is not
  590. * required.
  591. */
  592. typedef struct
  593. {
  594. /** The eyecatcher for this structure. must be MQTW. */
  595. char struct_id[4];
  596. /** The version number of this structure. Must be 0 or 1
  597. 0 means there is no binary payload option
  598. */
  599. int struct_version;
  600. /** The LWT topic to which the LWT message will be published. */
  601. const char* topicName;
  602. /** The LWT payload in string form. */
  603. const char* message;
  604. /**
  605. * The retained flag for the LWT message (see MQTTClient_message.retained).
  606. */
  607. int retained;
  608. /**
  609. * The quality of service setting for the LWT message (see
  610. * MQTTClient_message.qos and @ref qos).
  611. */
  612. int qos;
  613. /** The LWT payload in binary form. This is only checked and used if the message option is NULL */
  614. struct
  615. {
  616. int len; /**< binary payload length */
  617. const void* data; /**< binary payload data */
  618. } payload;
  619. } MQTTClient_willOptions;
  620. #define MQTTClient_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 1, NULL, NULL, 0, 0, {0, NULL} }
  621. #define MQTT_SSL_VERSION_DEFAULT 0
  622. #define MQTT_SSL_VERSION_TLS_1_0 1
  623. #define MQTT_SSL_VERSION_TLS_1_1 2
  624. #define MQTT_SSL_VERSION_TLS_1_2 3
  625. /**
  626. * MQTTClient_sslProperties defines the settings to establish an SSL/TLS connection using the
  627. * OpenSSL library. It covers the following scenarios:
  628. * - Server authentication: The client needs the digital certificate of the server. It is included
  629. * in a store containting trusted material (also known as "trust store").
  630. * - Mutual authentication: Both client and server are authenticated during the SSL handshake. In
  631. * addition to the digital certificate of the server in a trust store, the client will need its own
  632. * digital certificate and the private key used to sign its digital certificate stored in a "key store".
  633. * - Anonymous connection: Both client and server do not get authenticated and no credentials are needed
  634. * to establish an SSL connection. Note that this scenario is not fully secure since it is subject to
  635. * man-in-the-middle attacks.
  636. */
  637. typedef struct
  638. {
  639. /** The eyecatcher for this structure. Must be MQTS */
  640. char struct_id[4];
  641. /** The version number of this structure. Must be 0, 1, 2, 3, 4 or 5.
  642. * 0 means no sslVersion
  643. * 1 means no verify, CApath
  644. * 2 means no ssl_error_context, ssl_error_cb
  645. * 3 means no ssl_psk_cb, ssl_psk_context, disableDefaultTrustStore
  646. * 4 means no protos, protos_len
  647. */
  648. int struct_version;
  649. /** The file in PEM format containing the public digital certificates trusted by the client. */
  650. const char* trustStore;
  651. /** The file in PEM format containing the public certificate chain of the client. It may also include
  652. * the client's private key.
  653. */
  654. const char* keyStore;
  655. /** If not included in the sslKeyStore, this setting points to the file in PEM format containing
  656. * the client's private key.
  657. */
  658. const char* privateKey;
  659. /** The password to load the client's privateKey if encrypted. */
  660. const char* privateKeyPassword;
  661. /**
  662. * The list of cipher suites that the client will present to the server during the SSL handshake. For a
  663. * full explanation of the cipher list format, please see the OpenSSL on-line documentation:
  664. * http://www.openssl.org/docs/apps/ciphers.html#CIPHER_LIST_FORMAT
  665. * If this setting is ommitted, its default value will be "ALL", that is, all the cipher suites -excluding
  666. * those offering no encryption- will be considered.
  667. * This setting can be used to set an SSL anonymous connection ("aNULL" string value, for instance).
  668. */
  669. const char* enabledCipherSuites;
  670. /** True/False option to enable verification of the server certificate **/
  671. int enableServerCertAuth;
  672. /** The SSL/TLS version to use. Specify one of MQTT_SSL_VERSION_DEFAULT (0),
  673. * MQTT_SSL_VERSION_TLS_1_0 (1), MQTT_SSL_VERSION_TLS_1_1 (2) or MQTT_SSL_VERSION_TLS_1_2 (3).
  674. * Only used if struct_version is >= 1.
  675. */
  676. int sslVersion;
  677. /**
  678. * Whether to carry out post-connect checks, including that a certificate
  679. * matches the given host name.
  680. * Exists only if struct_version >= 2
  681. */
  682. int verify;
  683. /**
  684. * From the OpenSSL documentation:
  685. * If CApath is not NULL, it points to a directory containing CA certificates in PEM format.
  686. * Exists only if struct_version >= 2
  687. */
  688. const char* CApath;
  689. /**
  690. * Callback function for OpenSSL error handler ERR_print_errors_cb
  691. * Exists only if struct_version >= 3
  692. */
  693. int (*ssl_error_cb) (const char *str, size_t len, void *u);
  694. /**
  695. * Application-specific contex for OpenSSL error handler ERR_print_errors_cb
  696. * Exists only if struct_version >= 3
  697. */
  698. void* ssl_error_context;
  699. /**
  700. * Callback function for setting TLS-PSK options. Parameters correspond to that of
  701. * SSL_CTX_set_psk_client_callback, except for u which is the pointer ssl_psk_context.
  702. * Exists only if struct_version >= 4
  703. */
  704. unsigned int (*ssl_psk_cb) (const char *hint, char *identity, unsigned int max_identity_len, unsigned char *psk, unsigned int max_psk_len, void *u);
  705. /**
  706. * Application-specific contex for ssl_psk_cb
  707. * Exists only if struct_version >= 4
  708. */
  709. void* ssl_psk_context;
  710. /**
  711. * Don't load default SSL CA. Should be used together with PSK to make sure
  712. * regular servers with certificate in place is not accepted.
  713. * Exists only if struct_version >= 4
  714. */
  715. int disableDefaultTrustStore;
  716. /**
  717. * The protocol-lists must be in wire-format, which is defined as a vector of non-empty, 8-bit length-prefixed, byte strings.
  718. * The length-prefix byte is not included in the length. Each string is limited to 255 bytes. A byte-string length of 0 is invalid.
  719. * A truncated byte-string is invalid.
  720. * Check documentation for SSL_CTX_set_alpn_protos
  721. * Exists only if struct_version >= 5
  722. */
  723. const unsigned char *protos;
  724. /**
  725. * The length of the vector protos vector
  726. * Exists only if struct_version >= 5
  727. */
  728. unsigned int protos_len;
  729. } MQTTClient_SSLOptions;
  730. #define MQTTClient_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 5, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL, NULL, NULL, 0, NULL, 0 }
  731. /**
  732. * MQTTClient_libraryInfo is used to store details relating to the currently used
  733. * library such as the version in use, the time it was built and relevant openSSL
  734. * options.
  735. * There is one static instance of this struct in MQTTClient.c
  736. */
  737. typedef struct
  738. {
  739. const char* name;
  740. const char* value;
  741. } MQTTClient_nameValue;
  742. /**
  743. * This function returns version information about the library.
  744. * no trace information will be returned.
  745. * @return an array of strings describing the library. The last entry is a NULL pointer.
  746. */
  747. LIBMQTT_API MQTTClient_nameValue* MQTTClient_getVersionInfo(void);
  748. /**
  749. * MQTTClient_connectOptions defines several settings that control the way the
  750. * client connects to an MQTT server.
  751. *
  752. * <b>Note:</b> Default values are not defined for members of
  753. * MQTTClient_connectOptions so it is good practice to specify all settings.
  754. * If the MQTTClient_connectOptions structure is defined as an automatic
  755. * variable, all members are set to random values and thus must be set by the
  756. * client application. If the MQTTClient_connectOptions structure is defined
  757. * as a static variable, initialization (in compliant compilers) sets all
  758. * values to 0 (NULL for pointers). A #keepAliveInterval setting of 0 prevents
  759. * correct operation of the client and so you <b>must</b> at least set a value
  760. * for #keepAliveInterval.
  761. *
  762. * Suitable default values are set in the following initializers:
  763. * - MQTTClient_connectOptions_initializer: for MQTT 3.1.1 non-WebSockets
  764. * - MQTTClient_connectOptions_initializer5: for MQTT 5.0 non-WebSockets
  765. * - MQTTClient_connectOptions_initializer_ws: for MQTT 3.1.1 WebSockets
  766. * - MQTTClient_connectOptions_initializer5_ws: for MQTT 5.0 WebSockets
  767. */
  768. typedef struct
  769. {
  770. /** The eyecatcher for this structure. must be MQTC. */
  771. char struct_id[4];
  772. /** The version number of this structure. Must be 0, 1, 2, 3, 4, 5, 6, 7 or 8.
  773. * 0 signifies no SSL options and no serverURIs
  774. * 1 signifies no serverURIs
  775. * 2 signifies no MQTTVersion
  776. * 3 signifies no returned values
  777. * 4 signifies no binary password option
  778. * 5 signifies no maxInflightMessages and cleanstart
  779. * 6 signifies no HTTP headers option
  780. * 7 signifies no HTTP proxy and HTTPS proxy options
  781. */
  782. int struct_version;
  783. /** The "keep alive" interval, measured in seconds, defines the maximum time
  784. * that should pass without communication between the client and the server
  785. * The client will ensure that at least one message travels across the
  786. * network within each keep alive period. In the absence of a data-related
  787. * message during the time period, the client sends a very small MQTT
  788. * "ping" message, which the server will acknowledge. The keep alive
  789. * interval enables the client to detect when the server is no longer
  790. * available without having to wait for the long TCP/IP timeout.
  791. */
  792. int keepAliveInterval;
  793. /**
  794. * This is a boolean value. The cleansession setting controls the behaviour
  795. * of both the client and the server at connection and disconnection time.
  796. * The client and server both maintain session state information. This
  797. * information is used to ensure "at least once" and "exactly once"
  798. * delivery, and "exactly once" receipt of messages. Session state also
  799. * includes subscriptions created by an MQTT client. You can choose to
  800. * maintain or discard state information between sessions.
  801. *
  802. * When cleansession is true, the state information is discarded at
  803. * connect and disconnect. Setting cleansession to false keeps the state
  804. * information. When you connect an MQTT client application with
  805. * MQTTClient_connect(), the client identifies the connection using the
  806. * client identifier and the address of the server. The server checks
  807. * whether session information for this client
  808. * has been saved from a previous connection to the server. If a previous
  809. * session still exists, and cleansession=true, then the previous session
  810. * information at the client and server is cleared. If cleansession=false,
  811. * the previous session is resumed. If no previous session exists, a new
  812. * session is started.
  813. */
  814. int cleansession;
  815. /**
  816. * This is a boolean value that controls how many messages can be in-flight
  817. * simultaneously. Setting <i>reliable</i> to true means that a published
  818. * message must be completed (acknowledgements received) before another
  819. * can be sent. Attempts to publish additional messages receive an
  820. * ::MQTTCLIENT_MAX_MESSAGES_INFLIGHT return code. Setting this flag to
  821. * false allows up to 10 messages to be in-flight. This can increase
  822. * overall throughput in some circumstances.
  823. */
  824. int reliable;
  825. /**
  826. * This is a pointer to an MQTTClient_willOptions structure. If your
  827. * application does not make use of the Last Will and Testament feature,
  828. * set this pointer to NULL.
  829. */
  830. MQTTClient_willOptions* will;
  831. /**
  832. * MQTT servers that support the MQTT v3.1.1 protocol provide authentication
  833. * and authorisation by user name and password. This is the user name
  834. * parameter.
  835. */
  836. const char* username;
  837. /**
  838. * MQTT servers that support the MQTT v3.1.1 protocol provide authentication
  839. * and authorisation by user name and password. This is the password
  840. * parameter.
  841. */
  842. const char* password;
  843. /**
  844. * The time interval in seconds to allow a connect to complete.
  845. */
  846. int connectTimeout;
  847. /**
  848. * The time interval in seconds after which unacknowledged publish requests are
  849. * retried during a TCP session. With MQTT 3.1.1 and later, retries are
  850. * not required except on reconnect. 0 turns off in-session retries, and is the
  851. * recommended setting. Adding retries to an already overloaded network only
  852. * exacerbates the problem.
  853. */
  854. int retryInterval;
  855. /**
  856. * This is a pointer to an MQTTClient_SSLOptions structure. If your
  857. * application does not make use of SSL, set this pointer to NULL.
  858. */
  859. MQTTClient_SSLOptions* ssl;
  860. /**
  861. * The number of entries in the optional serverURIs array. Defaults to 0.
  862. */
  863. int serverURIcount;
  864. /**
  865. * An optional array of null-terminated strings specifying the servers to
  866. * which the client will connect. Each string takes the form <i>protocol://host:port</i>.
  867. * <i>protocol</i> must be <i>tcp</i>, <i>ssl</i>, <i>ws</i> or <i>wss</i>.
  868. * The TLS enabled prefixes (ssl, wss) are only valid if a TLS version of the library
  869. * is linked with.
  870. * For <i>host</i>, you can
  871. * specify either an IP address or a host name. For instance, to connect to
  872. * a server running on the local machines with the default MQTT port, specify
  873. * <i>tcp://localhost:1883</i>.
  874. * If this list is empty (the default), the server URI specified on MQTTClient_create()
  875. * is used.
  876. */
  877. char* const* serverURIs;
  878. /**
  879. * Sets the version of MQTT to be used on the connect.
  880. * MQTTVERSION_DEFAULT (0) = default: start with 3.1.1, and if that fails, fall back to 3.1
  881. * MQTTVERSION_3_1 (3) = only try version 3.1
  882. * MQTTVERSION_3_1_1 (4) = only try version 3.1.1
  883. * MQTTVERSION_5 (5) = only try version 5.0
  884. */
  885. int MQTTVersion;
  886. /**
  887. * Returned from the connect when the MQTT version used to connect is 3.1.1
  888. */
  889. struct
  890. {
  891. const char* serverURI; /**< the serverURI connected to */
  892. int MQTTVersion; /**< the MQTT version used to connect with */
  893. int sessionPresent; /**< if the MQTT version is 3.1.1, the value of sessionPresent returned in the connack */
  894. } returned;
  895. /**
  896. * Optional binary password. Only checked and used if the password option is NULL
  897. */
  898. struct
  899. {
  900. int len; /**< binary password length */
  901. const void* data; /**< binary password data */
  902. } binarypwd;
  903. /**
  904. * The maximum number of messages in flight
  905. */
  906. int maxInflightMessages;
  907. /*
  908. * MQTT V5 clean start flag. Only clears state at the beginning of the session.
  909. */
  910. int cleanstart;
  911. /**
  912. * HTTP headers for websockets
  913. */
  914. const MQTTClient_nameValue* httpHeaders;
  915. /**
  916. * HTTP proxy
  917. */
  918. const char* httpProxy;
  919. /**
  920. * HTTPS proxy
  921. */
  922. const char* httpsProxy;
  923. } MQTTClient_connectOptions;
  924. /** Initializer for connect options for MQTT 3.1.1 non-WebSocket connections */
  925. #define MQTTClient_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 8, 60, 1, 1, NULL, NULL, NULL, 30, 0, NULL,\
  926. 0, NULL, MQTTVERSION_DEFAULT, {NULL, 0, 0}, {0, NULL}, -1, 0, NULL, NULL, NULL}
  927. /** Initializer for connect options for MQTT 5.0 non-WebSocket connections */
  928. #define MQTTClient_connectOptions_initializer5 { {'M', 'Q', 'T', 'C'}, 8, 60, 0, 1, NULL, NULL, NULL, 30, 0, NULL,\
  929. 0, NULL, MQTTVERSION_5, {NULL, 0, 0}, {0, NULL}, -1, 1, NULL, NULL, NULL}
  930. /** Initializer for connect options for MQTT 3.1.1 WebSockets connections.
  931. * The keepalive interval is set to 45 seconds to avoid webserver 60 second inactivity timeouts.
  932. */
  933. #define MQTTClient_connectOptions_initializer_ws { {'M', 'Q', 'T', 'C'}, 8, 45, 1, 1, NULL, NULL, NULL, 30, 0, NULL,\
  934. 0, NULL, MQTTVERSION_DEFAULT, {NULL, 0, 0}, {0, NULL}, -1, 0, NULL, NULL, NULL}
  935. /** Initializer for connect options for MQTT 5.0 WebSockets connections.
  936. * The keepalive interval is set to 45 seconds to avoid webserver 60 second inactivity timeouts.
  937. */
  938. #define MQTTClient_connectOptions_initializer5_ws { {'M', 'Q', 'T', 'C'}, 8, 45, 0, 1, NULL, NULL, NULL, 30, 0, NULL,\
  939. 0, NULL, MQTTVERSION_5, {NULL, 0, 0}, {0, NULL}, -1, 1, NULL, NULL, NULL}
  940. /**
  941. * This function attempts to connect a previously-created client (see
  942. * MQTTClient_create()) to an MQTT server using the specified options. If you
  943. * want to enable asynchronous message and status notifications, you must call
  944. * MQTTClient_setCallbacks() prior to MQTTClient_connect().
  945. * @param handle A valid client handle from a successful call to
  946. * MQTTClient_create().
  947. * @param options A pointer to a valid MQTTClient_connectOptions
  948. * structure.
  949. * @return ::MQTTCLIENT_SUCCESS if the client successfully connects to the
  950. * server. An error code is returned if the client was unable to connect to
  951. * the server.
  952. * Error codes greater than 0 are returned by the MQTT protocol:<br><br>
  953. * <b>1</b>: Connection refused: Unacceptable protocol version<br>
  954. * <b>2</b>: Connection refused: Identifier rejected<br>
  955. * <b>3</b>: Connection refused: Server unavailable<br>
  956. * <b>4</b>: Connection refused: Bad user name or password<br>
  957. * <b>5</b>: Connection refused: Not authorized<br>
  958. * <b>6-255</b>: Reserved for future use<br>
  959. */
  960. LIBMQTT_API int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options);
  961. /** MQTT version 5.0 response information */
  962. typedef struct MQTTResponse
  963. {
  964. int version; /* the version number of this structure */
  965. enum MQTTReasonCodes reasonCode; /* the MQTT 5.0 reason code returned */
  966. int reasonCodeCount; /* the number of reason codes. Used for subscribeMany5 and unsubscribeMany5 */
  967. enum MQTTReasonCodes* reasonCodes; /* a list of reason codes. Used for subscribeMany5 and unsubscribeMany5 */
  968. MQTTProperties* properties; /* optionally, the MQTT 5.0 properties returned */
  969. } MQTTResponse;
  970. #define MQTTResponse_initializer {1, MQTTREASONCODE_SUCCESS, 0, NULL, NULL}
  971. /**
  972. * Frees the storage associated with the MQTT response.
  973. * @param response the response structure to be freed
  974. */
  975. LIBMQTT_API void MQTTResponse_free(MQTTResponse response);
  976. /**
  977. * Attempts to connect a previously-created client (see
  978. * MQTTClient_create()) to an MQTT server using MQTT version 5.0 and the specified options. If you
  979. * want to enable asynchronous message and status notifications, you must call
  980. * MQTTClient_setCallbacks() prior to MQTTClient_connect().
  981. * @param handle A valid client handle from a successful call to
  982. * MQTTClient_create().
  983. * @param options A pointer to a valid MQTTClient_connectOptions
  984. * structure.
  985. * @param connectProperties the MQTT 5.0 connect properties to use
  986. * @param willProperties the MQTT 5.0 properties to set on the will message
  987. * @return the MQTT 5.0 response information: error codes and properties.
  988. */
  989. LIBMQTT_API MQTTResponse MQTTClient_connect5(MQTTClient handle, MQTTClient_connectOptions* options,
  990. MQTTProperties* connectProperties, MQTTProperties* willProperties);
  991. /**
  992. * This function attempts to disconnect the client from the MQTT
  993. * server. In order to allow the client time to complete handling of messages
  994. * that are in-flight when this function is called, a timeout period is
  995. * specified. When the timeout period has expired, the client disconnects even
  996. * if there are still outstanding message acknowledgements.
  997. * The next time the client connects to the same server, any QoS 1 or 2
  998. * messages which have not completed will be retried depending on the
  999. * cleansession settings for both the previous and the new connection (see
  1000. * MQTTClient_connectOptions.cleansession and MQTTClient_connect()).
  1001. * @param handle A valid client handle from a successful call to
  1002. * MQTTClient_create().
  1003. * @param timeout The client delays disconnection for up to this time (in
  1004. * milliseconds) in order to allow in-flight message transfers to complete.
  1005. * @return ::MQTTCLIENT_SUCCESS if the client successfully disconnects from
  1006. * the server. An error code is returned if the client was unable to disconnect
  1007. * from the server
  1008. */
  1009. LIBMQTT_API int MQTTClient_disconnect(MQTTClient handle, int timeout);
  1010. LIBMQTT_API int MQTTClient_disconnect5(MQTTClient handle, int timeout, enum MQTTReasonCodes reason, MQTTProperties* props);
  1011. /**
  1012. * This function allows the client application to test whether or not a
  1013. * client is currently connected to the MQTT server.
  1014. * @param handle A valid client handle from a successful call to
  1015. * MQTTClient_create().
  1016. * @return Boolean true if the client is connected, otherwise false.
  1017. */
  1018. LIBMQTT_API int MQTTClient_isConnected(MQTTClient handle);
  1019. /* Subscribe is synchronous. QoS list parameter is changed on return to granted QoSs.
  1020. Returns return code, MQTTCLIENT_SUCCESS == success, non-zero some sort of error (TBD) */
  1021. /**
  1022. * This function attempts to subscribe a client to a single topic, which may
  1023. * contain wildcards (see @ref wildcard). This call also specifies the
  1024. * @ref qos requested for the subscription
  1025. * (see also MQTTClient_subscribeMany()).
  1026. * @param handle A valid client handle from a successful call to
  1027. * MQTTClient_create().
  1028. * @param topic The subscription topic, which may include wildcards.
  1029. * @param qos The requested quality of service for the subscription.
  1030. * @return ::MQTTCLIENT_SUCCESS if the subscription request is successful.
  1031. * An error code is returned if there was a problem registering the
  1032. * subscription.
  1033. */
  1034. LIBMQTT_API int MQTTClient_subscribe(MQTTClient handle, const char* topic, int qos);
  1035. /**
  1036. * This function attempts to subscribe an MQTT version 5.0 client to a single topic, which may
  1037. * contain wildcards (see @ref wildcard). This call also specifies the
  1038. * @ref qos requested for the subscription
  1039. * (see also MQTTClient_subscribeMany()).
  1040. * @param handle A valid client handle from a successful call to
  1041. * MQTTClient_create().
  1042. * @param topic The subscription topic, which may include wildcards.
  1043. * @param qos The requested quality of service for the subscription.
  1044. * @param opts the MQTT 5.0 subscribe options to be used
  1045. * @param props the MQTT 5.0 properties to be used
  1046. * @return the MQTT 5.0 response information: error codes and properties.
  1047. */
  1048. LIBMQTT_API MQTTResponse MQTTClient_subscribe5(MQTTClient handle, const char* topic, int qos,
  1049. MQTTSubscribe_options* opts, MQTTProperties* props);
  1050. /**
  1051. * This function attempts to subscribe a client to a list of topics, which may
  1052. * contain wildcards (see @ref wildcard). This call also specifies the
  1053. * @ref qos requested for each topic (see also MQTTClient_subscribe()).
  1054. * @param handle A valid client handle from a successful call to
  1055. * MQTTClient_create().
  1056. * @param count The number of topics for which the client is requesting
  1057. * subscriptions.
  1058. * @param topic An array (of length <i>count</i>) of pointers to
  1059. * topics, each of which may include wildcards.
  1060. * @param qos An array (of length <i>count</i>) of @ref qos
  1061. * values. qos[n] is the requested QoS for topic[n].
  1062. * @return ::MQTTCLIENT_SUCCESS if the subscription request is successful.
  1063. * An error code is returned if there was a problem registering the
  1064. * subscriptions.
  1065. */
  1066. LIBMQTT_API int MQTTClient_subscribeMany(MQTTClient handle, int count, char* const* topic, int* qos);
  1067. /**
  1068. * This function attempts to subscribe an MQTT version 5.0 client to a list of topics, which may
  1069. * contain wildcards (see @ref wildcard). This call also specifies the
  1070. * @ref qos requested for each topic (see also MQTTClient_subscribe()).
  1071. * @param handle A valid client handle from a successful call to
  1072. * MQTTClient_create().
  1073. * @param count The number of topics for which the client is requesting
  1074. * subscriptions.
  1075. * @param topic An array (of length <i>count</i>) of pointers to
  1076. * topics, each of which may include wildcards.
  1077. * @param qos An array (of length <i>count</i>) of @ref qos
  1078. * values. qos[n] is the requested QoS for topic[n].
  1079. * @param opts the MQTT 5.0 subscribe options to be used
  1080. * @param props the MQTT 5.0 properties to be used
  1081. * @return the MQTT 5.0 response information: error codes and properties.
  1082. */
  1083. LIBMQTT_API MQTTResponse MQTTClient_subscribeMany5(MQTTClient handle, int count, char* const* topic,
  1084. int* qos, MQTTSubscribe_options* opts, MQTTProperties* props);
  1085. /**
  1086. * This function attempts to remove an existing subscription made by the
  1087. * specified client.
  1088. * @param handle A valid client handle from a successful call to
  1089. * MQTTClient_create().
  1090. * @param topic The topic for the subscription to be removed, which may
  1091. * include wildcards (see @ref wildcard).
  1092. * @return ::MQTTCLIENT_SUCCESS if the subscription is removed.
  1093. * An error code is returned if there was a problem removing the
  1094. * subscription.
  1095. */
  1096. LIBMQTT_API int MQTTClient_unsubscribe(MQTTClient handle, const char* topic);
  1097. /**
  1098. * This function attempts to remove an existing subscription made by the
  1099. * specified client using MQTT 5.0.
  1100. * @param handle A valid client handle from a successful call to
  1101. * MQTTClient_create().
  1102. * @param topic The topic for the subscription to be removed, which may
  1103. * include wildcards (see @ref wildcard).
  1104. * @param props the MQTT 5.0 properties to be used
  1105. * @return the MQTT 5.0 response information: error codes and properties.
  1106. */
  1107. LIBMQTT_API MQTTResponse MQTTClient_unsubscribe5(MQTTClient handle, const char* topic, MQTTProperties* props);
  1108. /**
  1109. * This function attempts to remove existing subscriptions to a list of topics
  1110. * made by the specified client.
  1111. * @param handle A valid client handle from a successful call to
  1112. * MQTTClient_create().
  1113. * @param count The number subscriptions to be removed.
  1114. * @param topic An array (of length <i>count</i>) of pointers to the topics of
  1115. * the subscriptions to be removed, each of which may include wildcards.
  1116. * @return ::MQTTCLIENT_SUCCESS if the subscriptions are removed.
  1117. * An error code is returned if there was a problem removing the subscriptions.
  1118. */
  1119. LIBMQTT_API int MQTTClient_unsubscribeMany(MQTTClient handle, int count, char* const* topic);
  1120. /**
  1121. * This function attempts to remove existing subscriptions to a list of topics
  1122. * made by the specified client using MQTT version 5.0.
  1123. * @param handle A valid client handle from a successful call to
  1124. * MQTTClient_create().
  1125. * @param count The number subscriptions to be removed.
  1126. * @param topic An array (of length <i>count</i>) of pointers to the topics of
  1127. * the subscriptions to be removed, each of which may include wildcards.
  1128. * @param props the MQTT 5.0 properties to be used
  1129. * @return the MQTT 5.0 response information: error codes and properties.
  1130. */
  1131. LIBMQTT_API MQTTResponse MQTTClient_unsubscribeMany5(MQTTClient handle, int count, char* const* topic, MQTTProperties* props);
  1132. /**
  1133. * This function attempts to publish a message to a given topic (see also
  1134. * MQTTClient_publishMessage()). An ::MQTTClient_deliveryToken is issued when
  1135. * this function returns successfully. If the client application needs to
  1136. * test for succesful delivery of QoS1 and QoS2 messages, this can be done
  1137. * either asynchronously or synchronously (see @ref async,
  1138. * ::MQTTClient_waitForCompletion and MQTTClient_deliveryComplete()).
  1139. * @param handle A valid client handle from a successful call to
  1140. * MQTTClient_create().
  1141. * @param topicName The topic associated with this message.
  1142. * @param payloadlen The length of the payload in bytes.
  1143. * @param payload A pointer to the byte array payload of the message.
  1144. * @param qos The @ref qos of the message.
  1145. * @param retained The retained flag for the message.
  1146. * @param dt A pointer to an ::MQTTClient_deliveryToken. This is populated
  1147. * with a token representing the message when the function returns
  1148. * successfully. If your application does not use delivery tokens, set this
  1149. * argument to NULL.
  1150. * @return ::MQTTCLIENT_SUCCESS if the message is accepted for publication.
  1151. * An error code is returned if there was a problem accepting the message.
  1152. */
  1153. LIBMQTT_API int MQTTClient_publish(MQTTClient handle, const char* topicName, int payloadlen, const void* payload, int qos, int retained,
  1154. MQTTClient_deliveryToken* dt);
  1155. /**
  1156. * Attempts to publish a message to a given topic using MQTT version 5.0 (see also
  1157. * MQTTClient_publishMessage5()). An ::MQTTClient_deliveryToken is issued when
  1158. * this function returns successfully. If the client application needs to
  1159. * test for succesful delivery of QoS1 and QoS2 messages, this can be done
  1160. * either asynchronously or synchronously (see @ref async,
  1161. * ::MQTTClient_waitForCompletion and MQTTClient_deliveryComplete()).
  1162. * @param handle A valid client handle from a successful call to
  1163. * MQTTClient_create().
  1164. * @param topicName The topic associated with this message.
  1165. * @param payloadlen The length of the payload in bytes.
  1166. * @param payload A pointer to the byte array payload of the message.
  1167. * @param qos The @ref qos of the message.
  1168. * @param retained The retained flag for the message.
  1169. * @param properties the MQTT 5.0 properties to be used
  1170. * @param dt A pointer to an ::MQTTClient_deliveryToken. This is populated
  1171. * with a token representing the message when the function returns
  1172. * successfully. If your application does not use delivery tokens, set this
  1173. * argument to NULL.
  1174. * @return the MQTT 5.0 response information: error codes and properties.
  1175. */
  1176. LIBMQTT_API MQTTResponse MQTTClient_publish5(MQTTClient handle, const char* topicName, int payloadlen, const void* payload,
  1177. int qos, int retained, MQTTProperties* properties, MQTTClient_deliveryToken* dt);
  1178. /**
  1179. * This function attempts to publish a message to a given topic (see also
  1180. * MQTTClient_publish()). An ::MQTTClient_deliveryToken is issued when
  1181. * this function returns successfully. If the client application needs to
  1182. * test for succesful delivery of QoS1 and QoS2 messages, this can be done
  1183. * either asynchronously or synchronously (see @ref async,
  1184. * ::MQTTClient_waitForCompletion and MQTTClient_deliveryComplete()).
  1185. * @param handle A valid client handle from a successful call to
  1186. * MQTTClient_create().
  1187. * @param topicName The topic associated with this message.
  1188. * @param msg A pointer to a valid MQTTClient_message structure containing
  1189. * the payload and attributes of the message to be published.
  1190. * @param dt A pointer to an ::MQTTClient_deliveryToken. This is populated
  1191. * with a token representing the message when the function returns
  1192. * successfully. If your application does not use delivery tokens, set this
  1193. * argument to NULL.
  1194. * @return ::MQTTCLIENT_SUCCESS if the message is accepted for publication.
  1195. * An error code is returned if there was a problem accepting the message.
  1196. */
  1197. LIBMQTT_API int MQTTClient_publishMessage(MQTTClient handle, const char* topicName, MQTTClient_message* msg, MQTTClient_deliveryToken* dt);
  1198. /**
  1199. * Attempts to publish a message to the given topic using MQTT version 5.0
  1200. * (see also
  1201. * MQTTClient_publish5()). An ::MQTTClient_deliveryToken is issued when
  1202. * this function returns successfully. If the client application needs to
  1203. * test for succesful delivery of QoS1 and QoS2 messages, this can be done
  1204. * either asynchronously or synchronously (see @ref async,
  1205. * ::MQTTClient_waitForCompletion and MQTTClient_deliveryComplete()).
  1206. * @param handle A valid client handle from a successful call to
  1207. * MQTTClient_create().
  1208. * @param topicName The topic associated with this message.
  1209. * @param msg A pointer to a valid MQTTClient_message structure containing
  1210. * the payload and attributes of the message to be published.
  1211. * @param dt A pointer to an ::MQTTClient_deliveryToken. This is populated
  1212. * with a token representing the message when the function returns
  1213. * successfully. If your application does not use delivery tokens, set this
  1214. * argument to NULL.
  1215. * @return the MQTT 5.0 response information: error codes and properties.
  1216. */
  1217. LIBMQTT_API MQTTResponse MQTTClient_publishMessage5(MQTTClient handle, const char* topicName, MQTTClient_message* msg,
  1218. MQTTClient_deliveryToken* dt);
  1219. /**
  1220. * This function is called by the client application to synchronize execution
  1221. * of the main thread with completed publication of a message. When called,
  1222. * MQTTClient_waitForCompletion() blocks execution until the message has been
  1223. * successful delivered or the specified timeout has expired. See @ref async.
  1224. * @param handle A valid client handle from a successful call to
  1225. * MQTTClient_create().
  1226. * @param dt The ::MQTTClient_deliveryToken that represents the message being
  1227. * tested for successful delivery. Delivery tokens are issued by the
  1228. * publishing functions MQTTClient_publish() and MQTTClient_publishMessage().
  1229. * @param timeout The maximum time to wait in milliseconds.
  1230. * @return ::MQTTCLIENT_SUCCESS if the message was successfully delivered.
  1231. * An error code is returned if the timeout expires or there was a problem
  1232. * checking the token.
  1233. */
  1234. LIBMQTT_API int MQTTClient_waitForCompletion(MQTTClient handle, MQTTClient_deliveryToken dt, unsigned long timeout);
  1235. /**
  1236. * This function sets a pointer to an array of delivery tokens for
  1237. * messages that are currently in-flight (pending completion).
  1238. *
  1239. * <b>Important note:</b> The memory used to hold the array of tokens is
  1240. * malloc()'d in this function. The client application is responsible for
  1241. * freeing this memory when it is no longer required.
  1242. * @param handle A valid client handle from a successful call to
  1243. * MQTTClient_create().
  1244. * @param tokens The address of a pointer to an ::MQTTClient_deliveryToken.
  1245. * When the function returns successfully, the pointer is set to point to an
  1246. * array of tokens representing messages pending completion. The last member of
  1247. * the array is set to -1 to indicate there are no more tokens. If no tokens
  1248. * are pending, the pointer is set to NULL.
  1249. * @return ::MQTTCLIENT_SUCCESS if the function returns successfully.
  1250. * An error code is returned if there was a problem obtaining the list of
  1251. * pending tokens.
  1252. */
  1253. LIBMQTT_API int MQTTClient_getPendingDeliveryTokens(MQTTClient handle, MQTTClient_deliveryToken **tokens);
  1254. /**
  1255. * When implementing a single-threaded client, call this function periodically
  1256. * to allow processing of message retries and to send MQTT keepalive pings.
  1257. * If the application is calling MQTTClient_receive() regularly, then it is
  1258. * not necessary to call this function.
  1259. */
  1260. LIBMQTT_API void MQTTClient_yield(void);
  1261. /**
  1262. * This function performs a synchronous receive of incoming messages. It should
  1263. * be used only when the client application has not set callback methods to
  1264. * support asynchronous receipt of messages (see @ref async and
  1265. * MQTTClient_setCallbacks()). Using this function allows a single-threaded
  1266. * client subscriber application to be written. When called, this function
  1267. * blocks until the next message arrives or the specified timeout expires
  1268. *(see also MQTTClient_yield()).
  1269. *
  1270. * <b>Important note:</b> The application must free() the memory allocated
  1271. * to the topic and the message when processing is complete (see
  1272. * MQTTClient_freeMessage()).
  1273. * @param handle A valid client handle from a successful call to
  1274. * MQTTClient_create().
  1275. * @param topicName The address of a pointer to a topic. This function
  1276. * allocates the memory for the topic and returns it to the application
  1277. * by setting <i>topicName</i> to point to the topic.
  1278. * @param topicLen The length of the topic. If the return code from this
  1279. * function is ::MQTTCLIENT_TOPICNAME_TRUNCATED, the topic contains embedded
  1280. * NULL characters and the full topic should be retrieved by using
  1281. * <i>topicLen</i>.
  1282. * @param message The address of a pointer to the received message. This
  1283. * function allocates the memory for the message and returns it to the
  1284. * application by setting <i>message</i> to point to the received message.
  1285. * The pointer is set to NULL if the timeout expires.
  1286. * @param timeout The length of time to wait for a message in milliseconds.
  1287. * @return ::MQTTCLIENT_SUCCESS or ::MQTTCLIENT_TOPICNAME_TRUNCATED if a
  1288. * message is received. ::MQTTCLIENT_SUCCESS can also indicate that the
  1289. * timeout expired, in which case <i>message</i> is NULL. An error code is
  1290. * returned if there was a problem trying to receive a message.
  1291. */
  1292. LIBMQTT_API int MQTTClient_receive(MQTTClient handle, char** topicName, int* topicLen, MQTTClient_message** message,
  1293. unsigned long timeout);
  1294. /**
  1295. * This function frees memory allocated to an MQTT message, including the
  1296. * additional memory allocated to the message payload. The client application
  1297. * calls this function when the message has been fully processed. <b>Important
  1298. * note:</b> This function does not free the memory allocated to a message
  1299. * topic string. It is the responsibility of the client application to free
  1300. * this memory using the MQTTClient_free() library function.
  1301. * @param msg The address of a pointer to the ::MQTTClient_message structure
  1302. * to be freed.
  1303. */
  1304. LIBMQTT_API void MQTTClient_freeMessage(MQTTClient_message** msg);
  1305. /**
  1306. * This function frees memory allocated by the MQTT C client library, especially the
  1307. * topic name. This is needed on Windows when the client libary and application
  1308. * program have been compiled with different versions of the C compiler. It is
  1309. * thus good policy to always use this function when freeing any MQTT C client-
  1310. * allocated memory.
  1311. * @param ptr The pointer to the client library storage to be freed.
  1312. */
  1313. LIBMQTT_API void MQTTClient_free(void* ptr);
  1314. /**
  1315. * This function frees the memory allocated to an MQTT client (see
  1316. * MQTTClient_create()). It should be called when the client is no longer
  1317. * required.
  1318. * @param handle A pointer to the handle referring to the ::MQTTClient
  1319. * structure to be freed.
  1320. */
  1321. LIBMQTT_API void MQTTClient_destroy(MQTTClient* handle);
  1322. enum MQTTCLIENT_TRACE_LEVELS
  1323. {
  1324. MQTTCLIENT_TRACE_MAXIMUM = 1,
  1325. MQTTCLIENT_TRACE_MEDIUM,
  1326. MQTTCLIENT_TRACE_MINIMUM,
  1327. MQTTCLIENT_TRACE_PROTOCOL,
  1328. MQTTCLIENT_TRACE_ERROR,
  1329. MQTTCLIENT_TRACE_SEVERE,
  1330. MQTTCLIENT_TRACE_FATAL,
  1331. };
  1332. /**
  1333. * This function sets the level of trace information which will be
  1334. * returned in the trace callback.
  1335. * @param level the trace level required
  1336. */
  1337. LIBMQTT_API void MQTTClient_setTraceLevel(enum MQTTCLIENT_TRACE_LEVELS level);
  1338. /**
  1339. * This is a callback function prototype which must be implemented if you want
  1340. * to receive trace information. Do not invoke any other Paho API calls in this
  1341. * callback function - unpredictable behavior may result.
  1342. * @param level the trace level of the message returned
  1343. * @param message the trace message. This is a pointer to a static buffer which
  1344. * will be overwritten on each call. You must copy the data if you want to keep
  1345. * it for later.
  1346. */
  1347. typedef void MQTTClient_traceCallback(enum MQTTCLIENT_TRACE_LEVELS level, char* message);
  1348. /**
  1349. * This function sets the trace callback if needed. If set to NULL,
  1350. * no trace information will be returned. The default trace level is
  1351. * MQTTASYNC_TRACE_MINIMUM.
  1352. * @param callback a pointer to the function which will handle the trace information
  1353. */
  1354. LIBMQTT_API void MQTTClient_setTraceCallback(MQTTClient_traceCallback* callback);
  1355. /**
  1356. * Sets the timeout value for un/subscribe commands when waiting for the un/suback response from
  1357. * the server. Values less than 5000 are not allowed.
  1358. * @param handle A valid client handle from a successful call to MQTTClient_create().
  1359. * @param milliSeconds the maximum number of milliseconds to wait
  1360. * @return MQTTCLIENT_SUCCESS or MQTTCLIENT_FAILURE
  1361. */
  1362. LIBMQTT_API int MQTTClient_setCommandTimeout(MQTTClient handle, unsigned long milliSeconds);
  1363. /**
  1364. * Returns a pointer to the string representation of the error or NULL.
  1365. *
  1366. * Do not free after use. Returns NULL if the error code is unknown.
  1367. */
  1368. LIBMQTT_API const char* MQTTClient_strerror(int code);
  1369. #if defined(__cplusplus)
  1370. }
  1371. #endif
  1372. #endif
  1373. /*!
  1374. * @cond MQTTClient_main
  1375. * @page async Asynchronous vs synchronous client applications
  1376. * This client library supports two modes of operation. These are referred to
  1377. * as <b>synchronous</b> and <b>asynchronous</b> modes. If your application
  1378. * calls MQTTClient_setCallbacks(), this puts the client into asynchronous
  1379. * mode, otherwise it operates in synchronous mode.
  1380. *
  1381. * In synchronous mode, the client application runs on a single thread.
  1382. * Messages are published using the MQTTClient_publish() and
  1383. * MQTTClient_publishMessage() functions. To determine that a QoS1 or QoS2
  1384. * (see @ref qos) message has been successfully delivered, the application
  1385. * must call the MQTTClient_waitForCompletion() function. An example showing
  1386. * synchronous publication is shown in @ref pubsync. Receiving messages in
  1387. * synchronous mode uses the MQTTClient_receive() function. Client applications
  1388. * must call either MQTTClient_receive() or MQTTClient_yield() relatively
  1389. * frequently in order to allow processing of acknowledgements and the MQTT
  1390. * "pings" that keep the network connection to the server alive.
  1391. *
  1392. * In asynchronous mode, the client application runs on several threads. The
  1393. * main program calls functions in the client library to publish and subscribe,
  1394. * just as for the synchronous mode. Processing of handshaking and maintaining
  1395. * the network connection is performed in the background, however.
  1396. * Notifications of status and message reception are provided to the client
  1397. * application using callbacks registered with the library by the call to
  1398. * MQTTClient_setCallbacks() (see MQTTClient_messageArrived(),
  1399. * MQTTClient_connectionLost() and MQTTClient_deliveryComplete()).
  1400. * This API is not thread safe however - it is not possible to call it from multiple
  1401. * threads without synchronization. You can use the MQTTAsync API for that.
  1402. *
  1403. * @page callbacks Callbacks
  1404. * You must not call a function from this API from within a callback otherwise
  1405. * a deadlock might result. The only exception to this is the ability to call
  1406. * connect within the connection lost callback, to allow a reconnect.
  1407. *
  1408. * When using MQTT 5.0, you can also call connect from within the disconnected
  1409. * callback, which is invoked when the MQTT server sends a disconnect packet.
  1410. * This server behaviour is allowed in MQTT 5.0, but not in MQTT 3.1.1, so the
  1411. * disconnected callback will never be invoked if you use MQTT 3.1.1.
  1412. *
  1413. * In particular, you must make a publish call within the message arrived callback.
  1414. * These restrictions are all lifted in the
  1415. * <a href="../../MQTTAsync/html/index.html">MQTTAsync API</a>.
  1416. *
  1417. * If no callbacks are assigned, this will include the message arrived callback.
  1418. * This could be done if the application is a pure publisher, and does
  1419. * not subscribe to any topics. If however messages are received, and no message
  1420. * arrived callback is set, or receive not called, then those messages will accumulate
  1421. * and take up memory, as there is no place for them to be delivered.
  1422. * It is up to the application to protect against this situation.
  1423. *
  1424. * @page wildcard Subscription wildcards
  1425. * Every MQTT message includes a topic that classifies it. MQTT servers use
  1426. * topics to determine which subscribers should receive messages published to
  1427. * the server.
  1428. *
  1429. * Consider the server receiving messages from several environmental sensors.
  1430. * Each sensor publishes its measurement data as a message with an associated
  1431. * topic. Subscribing applications need to know which sensor originally
  1432. * published each received message. A unique topic is thus used to identify
  1433. * each sensor and measurement type. Topics such as SENSOR1TEMP,
  1434. * SENSOR1HUMIDITY, SENSOR2TEMP and so on achieve this but are not very
  1435. * flexible. If additional sensors are added to the system at a later date,
  1436. * subscribing applications must be modified to receive them.
  1437. *
  1438. * To provide more flexibility, MQTT supports a hierarchical topic namespace.
  1439. * This allows application designers to organize topics to simplify their
  1440. * management. Levels in the hierarchy are delimited by the '/' character,
  1441. * such as SENSOR/1/HUMIDITY. Publishers and subscribers use these
  1442. * hierarchical topics as already described.
  1443. *
  1444. * For subscriptions, two wildcard characters are supported:
  1445. * <ul>
  1446. * <li>A '#' character represents a complete sub-tree of the hierarchy and
  1447. * thus must be the last character in a subscription topic string, such as
  1448. * SENSOR/#. This will match any topic starting with SENSOR/, such as
  1449. * SENSOR/1/TEMP and SENSOR/2/HUMIDITY.</li>
  1450. * <li> A '+' character represents a single level of the hierarchy and is
  1451. * used between delimiters. For example, SENSOR/+/TEMP will match
  1452. * SENSOR/1/TEMP and SENSOR/2/TEMP.</li>
  1453. * </ul>
  1454. * Publishers are not allowed to use the wildcard characters in their topic
  1455. * names.
  1456. *
  1457. * Deciding on your topic hierarchy is an important step in your system design.
  1458. *
  1459. * @page qos Quality of service
  1460. * The MQTT protocol provides three qualities of service for delivering
  1461. * messages between clients and servers: "at most once", "at least once" and
  1462. * "exactly once".
  1463. *
  1464. * Quality of service (QoS) is an attribute of an individual message being
  1465. * published. An application sets the QoS for a specific message by setting the
  1466. * MQTTClient_message.qos field to the required value.
  1467. *
  1468. * A subscribing client can set the maximum quality of service a server uses
  1469. * to send messages that match the client subscriptions. The
  1470. * MQTTClient_subscribe() and MQTTClient_subscribeMany() functions set this
  1471. * maximum. The QoS of a message forwarded to a subscriber thus might be
  1472. * different to the QoS given to the message by the original publisher.
  1473. * The lower of the two values is used to forward a message.
  1474. *
  1475. * The three levels are:
  1476. *
  1477. * <b>QoS0, At most once:</b> The message is delivered at most once, or it
  1478. * may not be delivered at all. Its delivery across the network is not
  1479. * acknowledged. The message is not stored. The message could be lost if the
  1480. * client is disconnected, or if the server fails. QoS0 is the fastest mode of
  1481. * transfer. It is sometimes called "fire and forget".
  1482. *
  1483. * The MQTT protocol does not require servers to forward publications at QoS0
  1484. * to a client. If the client is disconnected at the time the server receives
  1485. * the publication, the publication might be discarded, depending on the
  1486. * server implementation.
  1487. *
  1488. * <b>QoS1, At least once:</b> The message is always delivered at least once.
  1489. * It might be delivered multiple times if there is a failure before an
  1490. * acknowledgment is received by the sender. The message must be stored
  1491. * locally at the sender, until the sender receives confirmation that the
  1492. * message has been published by the receiver. The message is stored in case
  1493. * the message must be sent again.
  1494. *
  1495. * <b>QoS2, Exactly once:</b> The message is always delivered exactly once.
  1496. * The message must be stored locally at the sender, until the sender receives
  1497. * confirmation that the message has been published by the receiver. The
  1498. * message is stored in case the message must be sent again. QoS2 is the
  1499. * safest, but slowest mode of transfer. A more sophisticated handshaking
  1500. * and acknowledgement sequence is used than for QoS1 to ensure no duplication
  1501. * of messages occurs.
  1502. * @page pubsync Synchronous publication example
  1503. @code
  1504. #include <stdio.h>
  1505. #include <stdlib.h>
  1506. #include <string.h>
  1507. #include "MQTTClient.h"
  1508. #define ADDRESS "tcp://mqtt.eclipseprojects.io:1883"
  1509. #define CLIENTID "ExampleClientPub"
  1510. #define TOPIC "MQTT Examples"
  1511. #define PAYLOAD "Hello World!"
  1512. #define QOS 1
  1513. #define TIMEOUT 10000L
  1514. int main(int argc, char* argv[])
  1515. {
  1516. MQTTClient client;
  1517. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  1518. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  1519. MQTTClient_deliveryToken token;
  1520. int rc;
  1521. if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
  1522. MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
  1523. {
  1524. printf("Failed to create client, return code %d\n", rc);
  1525. exit(EXIT_FAILURE);
  1526. }
  1527. conn_opts.keepAliveInterval = 20;
  1528. conn_opts.cleansession = 1;
  1529. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  1530. {
  1531. printf("Failed to connect, return code %d\n", rc);
  1532. exit(EXIT_FAILURE);
  1533. }
  1534. pubmsg.payload = PAYLOAD;
  1535. pubmsg.payloadlen = (int)strlen(PAYLOAD);
  1536. pubmsg.qos = QOS;
  1537. pubmsg.retained = 0;
  1538. if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
  1539. {
  1540. printf("Failed to publish message, return code %d\n", rc);
  1541. exit(EXIT_FAILURE);
  1542. }
  1543. printf("Waiting for up to %d seconds for publication of %s\n"
  1544. "on topic %s for client with ClientID: %s\n",
  1545. (int)(TIMEOUT/1000), PAYLOAD, TOPIC, CLIENTID);
  1546. rc = MQTTClient_waitForCompletion(client, token, TIMEOUT);
  1547. printf("Message with delivery token %d delivered\n", token);
  1548. if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
  1549. printf("Failed to disconnect, return code %d\n", rc);
  1550. MQTTClient_destroy(&client);
  1551. return rc;
  1552. }
  1553. * @endcode
  1554. *
  1555. * @page pubasync Asynchronous publication example
  1556. @code{.c}
  1557. #include <stdio.h>
  1558. #include <stdlib.h>
  1559. #include <string.h>
  1560. #include "MQTTClient.h"
  1561. #if !defined(_WIN32)
  1562. #include <unistd.h>
  1563. #else
  1564. #include <windows.h>
  1565. #endif
  1566. #define ADDRESS "tcp://mqtt.eclipseprojects.io:1883"
  1567. #define CLIENTID "ExampleClientPub"
  1568. #define TOPIC "MQTT Examples"
  1569. #define PAYLOAD "Hello World!"
  1570. #define QOS 1
  1571. #define TIMEOUT 10000L
  1572. MQTTClient_deliveryToken deliveredtoken;
  1573. void delivered(void *context, MQTTClient_deliveryToken dt)
  1574. {
  1575. printf("Message with token value %d delivery confirmed\n", dt);
  1576. deliveredtoken = dt;
  1577. }
  1578. int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
  1579. {
  1580. printf("Message arrived\n");
  1581. printf(" topic: %s\n", topicName);
  1582. printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
  1583. MQTTClient_freeMessage(&message);
  1584. MQTTClient_free(topicName);
  1585. return 1;
  1586. }
  1587. void connlost(void *context, char *cause)
  1588. {
  1589. printf("\nConnection lost\n");
  1590. printf(" cause: %s\n", cause);
  1591. }
  1592. int main(int argc, char* argv[])
  1593. {
  1594. MQTTClient client;
  1595. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  1596. MQTTClient_message pubmsg = MQTTClient_message_initializer;
  1597. MQTTClient_deliveryToken token;
  1598. int rc;
  1599. if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
  1600. MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
  1601. {
  1602. printf("Failed to create client, return code %d\n", rc);
  1603. rc = EXIT_FAILURE;
  1604. goto exit;
  1605. }
  1606. if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
  1607. {
  1608. printf("Failed to set callbacks, return code %d\n", rc);
  1609. rc = EXIT_FAILURE;
  1610. goto destroy_exit;
  1611. }
  1612. conn_opts.keepAliveInterval = 20;
  1613. conn_opts.cleansession = 1;
  1614. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  1615. {
  1616. printf("Failed to connect, return code %d\n", rc);
  1617. rc = EXIT_FAILURE;
  1618. goto destroy_exit;
  1619. }
  1620. pubmsg.payload = PAYLOAD;
  1621. pubmsg.payloadlen = (int)strlen(PAYLOAD);
  1622. pubmsg.qos = QOS;
  1623. pubmsg.retained = 0;
  1624. deliveredtoken = 0;
  1625. if ((rc = MQTTClient_publishMessage(client, TOPIC, &pubmsg, &token)) != MQTTCLIENT_SUCCESS)
  1626. {
  1627. printf("Failed to publish message, return code %d\n", rc);
  1628. rc = EXIT_FAILURE;
  1629. }
  1630. else
  1631. {
  1632. printf("Waiting for publication of %s\n"
  1633. "on topic %s for client with ClientID: %s\n",
  1634. PAYLOAD, TOPIC, CLIENTID);
  1635. while (deliveredtoken != token)
  1636. {
  1637. #if defined(_WIN32)
  1638. Sleep(100);
  1639. #else
  1640. usleep(10000L);
  1641. #endif
  1642. }
  1643. }
  1644. if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
  1645. {
  1646. printf("Failed to disconnect, return code %d\n", rc);
  1647. rc = EXIT_FAILURE;
  1648. }
  1649. destroy_exit:
  1650. MQTTClient_destroy(&client);
  1651. exit:
  1652. return rc;
  1653. }
  1654. * @endcode
  1655. * @page subasync Asynchronous subscription example
  1656. @code
  1657. #include <stdio.h>
  1658. #include <stdlib.h>
  1659. #include <string.h>
  1660. #include "MQTTClient.h"
  1661. #define ADDRESS "tcp://mqtt.eclipseprojects.io:1883"
  1662. #define CLIENTID "ExampleClientSub"
  1663. #define TOPIC "MQTT Examples"
  1664. #define PAYLOAD "Hello World!"
  1665. #define QOS 1
  1666. #define TIMEOUT 10000L
  1667. volatile MQTTClient_deliveryToken deliveredtoken;
  1668. void delivered(void *context, MQTTClient_deliveryToken dt)
  1669. {
  1670. printf("Message with token value %d delivery confirmed\n", dt);
  1671. deliveredtoken = dt;
  1672. }
  1673. int msgarrvd(void *context, char *topicName, int topicLen, MQTTClient_message *message)
  1674. {
  1675. printf("Message arrived\n");
  1676. printf(" topic: %s\n", topicName);
  1677. printf(" message: %.*s\n", message->payloadlen, (char*)message->payload);
  1678. MQTTClient_freeMessage(&message);
  1679. MQTTClient_free(topicName);
  1680. return 1;
  1681. }
  1682. void connlost(void *context, char *cause)
  1683. {
  1684. printf("\nConnection lost\n");
  1685. printf(" cause: %s\n", cause);
  1686. }
  1687. int main(int argc, char* argv[])
  1688. {
  1689. MQTTClient client;
  1690. MQTTClient_connectOptions conn_opts = MQTTClient_connectOptions_initializer;
  1691. int rc;
  1692. if ((rc = MQTTClient_create(&client, ADDRESS, CLIENTID,
  1693. MQTTCLIENT_PERSISTENCE_NONE, NULL)) != MQTTCLIENT_SUCCESS)
  1694. {
  1695. printf("Failed to create client, return code %d\n", rc);
  1696. rc = EXIT_FAILURE;
  1697. goto exit;
  1698. }
  1699. if ((rc = MQTTClient_setCallbacks(client, NULL, connlost, msgarrvd, delivered)) != MQTTCLIENT_SUCCESS)
  1700. {
  1701. printf("Failed to set callbacks, return code %d\n", rc);
  1702. rc = EXIT_FAILURE;
  1703. goto destroy_exit;
  1704. }
  1705. conn_opts.keepAliveInterval = 20;
  1706. conn_opts.cleansession = 1;
  1707. if ((rc = MQTTClient_connect(client, &conn_opts)) != MQTTCLIENT_SUCCESS)
  1708. {
  1709. printf("Failed to connect, return code %d\n", rc);
  1710. rc = EXIT_FAILURE;
  1711. goto destroy_exit;
  1712. }
  1713. printf("Subscribing to topic %s\nfor client %s using QoS%d\n\n"
  1714. "Press Q<Enter> to quit\n\n", TOPIC, CLIENTID, QOS);
  1715. if ((rc = MQTTClient_subscribe(client, TOPIC, QOS)) != MQTTCLIENT_SUCCESS)
  1716. {
  1717. printf("Failed to subscribe, return code %d\n", rc);
  1718. rc = EXIT_FAILURE;
  1719. }
  1720. else
  1721. {
  1722. int ch;
  1723. do
  1724. {
  1725. ch = getchar();
  1726. } while (ch!='Q' && ch != 'q');
  1727. if ((rc = MQTTClient_unsubscribe(client, TOPIC)) != MQTTCLIENT_SUCCESS)
  1728. {
  1729. printf("Failed to unsubscribe, return code %d\n", rc);
  1730. rc = EXIT_FAILURE;
  1731. }
  1732. }
  1733. if ((rc = MQTTClient_disconnect(client, 10000)) != MQTTCLIENT_SUCCESS)
  1734. {
  1735. printf("Failed to disconnect, return code %d\n", rc);
  1736. rc = EXIT_FAILURE;
  1737. }
  1738. destroy_exit:
  1739. MQTTClient_destroy(&client);
  1740. exit:
  1741. return rc;
  1742. }
  1743. * @endcode
  1744. * @page tracing Tracing
  1745. *
  1746. * Runtime tracing is controlled by environment variables.
  1747. *
  1748. * Tracing is switched on by setting MQTT_C_CLIENT_TRACE. A value of ON, or stdout, prints to
  1749. * stdout, any other value is interpreted as a file name to use.
  1750. *
  1751. * The amount of trace detail is controlled with the MQTT_C_CLIENT_TRACE_LEVEL environment
  1752. * variable - valid values are ERROR, PROTOCOL, MINIMUM, MEDIUM and MAXIMUM
  1753. * (from least to most verbose).
  1754. *
  1755. * The variable MQTT_C_CLIENT_TRACE_MAX_LINES limits the number of lines of trace that are output
  1756. * to a file. Two files are used at most, when they are full, the last one is overwritten with the
  1757. * new trace entries. The default size is 1000 lines.
  1758. *
  1759. * ### MQTT Packet Tracing
  1760. *
  1761. * A feature that can be very useful is printing the MQTT packets that are sent and received. To
  1762. * achieve this, use the following environment variable settings:
  1763. * @code
  1764. MQTT_C_CLIENT_TRACE=ON
  1765. MQTT_C_CLIENT_TRACE_LEVEL=PROTOCOL
  1766. * @endcode
  1767. * The output you should see looks like this:
  1768. * @code
  1769. 20130528 155936.813 3 stdout-subscriber -> CONNECT cleansession: 1 (0)
  1770. 20130528 155936.813 3 stdout-subscriber <- CONNACK rc: 0
  1771. 20130528 155936.813 3 stdout-subscriber -> SUBSCRIBE msgid: 1 (0)
  1772. 20130528 155936.813 3 stdout-subscriber <- SUBACK msgid: 1
  1773. 20130528 155941.818 3 stdout-subscriber -> DISCONNECT (0)
  1774. * @endcode
  1775. * where the fields are:
  1776. * 1. date
  1777. * 2. time
  1778. * 3. socket number
  1779. * 4. client id
  1780. * 5. direction (-> from client to server, <- from server to client)
  1781. * 6. packet details
  1782. *
  1783. * ### Default Level Tracing
  1784. *
  1785. * This is an extract of a default level trace of a call to connect:
  1786. * @code
  1787. 19700101 010000.000 (1152206656) (0)> MQTTClient_connect:893
  1788. 19700101 010000.000 (1152206656) (1)> MQTTClient_connectURI:716
  1789. 20130528 160447.479 Connecting to serverURI localhost:1883
  1790. 20130528 160447.479 (1152206656) (2)> MQTTProtocol_connect:98
  1791. 20130528 160447.479 (1152206656) (3)> MQTTProtocol_addressPort:48
  1792. 20130528 160447.479 (1152206656) (3)< MQTTProtocol_addressPort:73
  1793. 20130528 160447.479 (1152206656) (3)> Socket_new:599
  1794. 20130528 160447.479 New socket 4 for localhost, port 1883
  1795. 20130528 160447.479 (1152206656) (4)> Socket_addSocket:163
  1796. 20130528 160447.479 (1152206656) (5)> Socket_setnonblocking:73
  1797. 20130528 160447.479 (1152206656) (5)< Socket_setnonblocking:78 (0)
  1798. 20130528 160447.479 (1152206656) (4)< Socket_addSocket:176 (0)
  1799. 20130528 160447.479 (1152206656) (4)> Socket_error:95
  1800. 20130528 160447.479 (1152206656) (4)< Socket_error:104 (115)
  1801. 20130528 160447.479 Connect pending
  1802. 20130528 160447.479 (1152206656) (3)< Socket_new:683 (115)
  1803. 20130528 160447.479 (1152206656) (2)< MQTTProtocol_connect:131 (115)
  1804. * @endcode
  1805. * where the fields are:
  1806. * 1. date
  1807. * 2. time
  1808. * 3. thread id
  1809. * 4. function nesting level
  1810. * 5. function entry (>) or exit (<)
  1811. * 6. function name : line of source code file
  1812. * 7. return value (if there is one)
  1813. *
  1814. * ### Memory Allocation Tracing
  1815. *
  1816. * Setting the trace level to maximum causes memory allocations and frees to be traced along with
  1817. * the default trace entries, with messages like the following:
  1818. * @code
  1819. 20130528 161819.657 Allocating 16 bytes in heap at file /home/icraggs/workspaces/mqrtc/mqttv3c/src/MQTTPacket.c line 177 ptr 0x179f930
  1820. 20130528 161819.657 Freeing 16 bytes in heap at file /home/icraggs/workspaces/mqrtc/mqttv3c/src/MQTTPacket.c line 201, heap use now 896 bytes
  1821. * @endcode
  1822. * When the last MQTT client object is destroyed, if the trace is being recorded
  1823. * and all memory allocated by the client library has not been freed, an error message will be
  1824. * written to the trace. This can help with fixing memory leaks. The message will look like this:
  1825. * @code
  1826. 20130528 163909.208 Some memory not freed at shutdown, possible memory leak
  1827. 20130528 163909.208 Heap scan start, total 880 bytes
  1828. 20130528 163909.208 Heap element size 32, line 354, file /home/icraggs/workspaces/mqrtc/mqttv3c/src/MQTTPacket.c, ptr 0x260cb00
  1829. 20130528 163909.208 Content
  1830. 20130528 163909.209 Heap scan end
  1831. * @endcode
  1832. * @endcond
  1833. */