MQTTClient.h 86 KB

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