MQTTAsync.h 84 KB

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