mosquitto.h 122 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243
  1. /*
  2. Copyright (c) 2010-2020 Roger Light <roger@atchoo.org>
  3. All rights reserved. This program and the accompanying materials
  4. are made available under the terms of the Eclipse Public License 2.0
  5. and Eclipse Distribution License v1.0 which accompany this distribution.
  6. The Eclipse Public License is available at
  7. https://www.eclipse.org/legal/epl-2.0/
  8. and the Eclipse Distribution License is available at
  9. http://www.eclipse.org/org/documents/edl-v10.php.
  10. SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause
  11. Contributors:
  12. Roger Light - initial implementation and documentation.
  13. */
  14. #ifndef MOSQUITTO_H
  15. #define MOSQUITTO_H
  16. /*
  17. * File: mosquitto.h
  18. *
  19. * This header contains functions and definitions for use with libmosquitto, the Mosquitto client library.
  20. *
  21. * The definitions are also used in Mosquitto broker plugins, and some functions are available to plugins.
  22. */
  23. #ifdef __cplusplus
  24. extern "C" {
  25. #endif
  26. #ifdef WIN32
  27. # ifdef mosquitto_EXPORTS
  28. # define libmosq_EXPORT __declspec(dllexport)
  29. # else
  30. # ifndef LIBMOSQUITTO_STATIC
  31. # ifdef libmosquitto_EXPORTS
  32. # define libmosq_EXPORT __declspec(dllexport)
  33. # else
  34. # define libmosq_EXPORT __declspec(dllimport)
  35. # endif
  36. # else
  37. # define libmosq_EXPORT
  38. # endif
  39. # endif
  40. #else
  41. # define libmosq_EXPORT
  42. #endif
  43. #if defined(_MSC_VER) && _MSC_VER < 1900 && !defined(bool)
  44. # ifndef __cplusplus
  45. # define bool char
  46. # define true 1
  47. # define false 0
  48. # endif
  49. #else
  50. # ifndef __cplusplus
  51. # include <stdbool.h>
  52. # endif
  53. #endif
  54. #include <stddef.h>
  55. #include <stdint.h>
  56. #define LIBMOSQUITTO_MAJOR 2
  57. #define LIBMOSQUITTO_MINOR 0
  58. #define LIBMOSQUITTO_REVISION 13
  59. /* LIBMOSQUITTO_VERSION_NUMBER looks like 1002001 for e.g. version 1.2.1. */
  60. #define LIBMOSQUITTO_VERSION_NUMBER (LIBMOSQUITTO_MAJOR*1000000+LIBMOSQUITTO_MINOR*1000+LIBMOSQUITTO_REVISION)
  61. /* Log types */
  62. #define MOSQ_LOG_NONE 0
  63. #define MOSQ_LOG_INFO (1<<0)
  64. #define MOSQ_LOG_NOTICE (1<<1)
  65. #define MOSQ_LOG_WARNING (1<<2)
  66. #define MOSQ_LOG_ERR (1<<3)
  67. #define MOSQ_LOG_DEBUG (1<<4)
  68. #define MOSQ_LOG_SUBSCRIBE (1<<5)
  69. #define MOSQ_LOG_UNSUBSCRIBE (1<<6)
  70. #define MOSQ_LOG_WEBSOCKETS (1<<7)
  71. #define MOSQ_LOG_INTERNAL 0x80000000U
  72. #define MOSQ_LOG_ALL 0xFFFFFFFFU
  73. /* Error values */
  74. enum mosq_err_t {
  75. MOSQ_ERR_AUTH_CONTINUE = -4,
  76. MOSQ_ERR_NO_SUBSCRIBERS = -3,
  77. MOSQ_ERR_SUB_EXISTS = -2,
  78. MOSQ_ERR_CONN_PENDING = -1,
  79. MOSQ_ERR_SUCCESS = 0,
  80. MOSQ_ERR_NOMEM = 1,
  81. MOSQ_ERR_PROTOCOL = 2,
  82. MOSQ_ERR_INVAL = 3,
  83. MOSQ_ERR_NO_CONN = 4,
  84. MOSQ_ERR_CONN_REFUSED = 5,
  85. MOSQ_ERR_NOT_FOUND = 6,
  86. MOSQ_ERR_CONN_LOST = 7,
  87. MOSQ_ERR_TLS = 8,
  88. MOSQ_ERR_PAYLOAD_SIZE = 9,
  89. MOSQ_ERR_NOT_SUPPORTED = 10,
  90. MOSQ_ERR_AUTH = 11,
  91. MOSQ_ERR_ACL_DENIED = 12,
  92. MOSQ_ERR_UNKNOWN = 13,
  93. MOSQ_ERR_ERRNO = 14,
  94. MOSQ_ERR_EAI = 15,
  95. MOSQ_ERR_PROXY = 16,
  96. MOSQ_ERR_PLUGIN_DEFER = 17,
  97. MOSQ_ERR_MALFORMED_UTF8 = 18,
  98. MOSQ_ERR_KEEPALIVE = 19,
  99. MOSQ_ERR_LOOKUP = 20,
  100. MOSQ_ERR_MALFORMED_PACKET = 21,
  101. MOSQ_ERR_DUPLICATE_PROPERTY = 22,
  102. MOSQ_ERR_TLS_HANDSHAKE = 23,
  103. MOSQ_ERR_QOS_NOT_SUPPORTED = 24,
  104. MOSQ_ERR_OVERSIZE_PACKET = 25,
  105. MOSQ_ERR_OCSP = 26,
  106. MOSQ_ERR_TIMEOUT = 27,
  107. MOSQ_ERR_RETAIN_NOT_SUPPORTED = 28,
  108. MOSQ_ERR_TOPIC_ALIAS_INVALID = 29,
  109. MOSQ_ERR_ADMINISTRATIVE_ACTION = 30,
  110. MOSQ_ERR_ALREADY_EXISTS = 31,
  111. };
  112. /* Option values */
  113. enum mosq_opt_t {
  114. MOSQ_OPT_PROTOCOL_VERSION = 1,
  115. MOSQ_OPT_SSL_CTX = 2,
  116. MOSQ_OPT_SSL_CTX_WITH_DEFAULTS = 3,
  117. MOSQ_OPT_RECEIVE_MAXIMUM = 4,
  118. MOSQ_OPT_SEND_MAXIMUM = 5,
  119. MOSQ_OPT_TLS_KEYFORM = 6,
  120. MOSQ_OPT_TLS_ENGINE = 7,
  121. MOSQ_OPT_TLS_ENGINE_KPASS_SHA1 = 8,
  122. MOSQ_OPT_TLS_OCSP_REQUIRED = 9,
  123. MOSQ_OPT_TLS_ALPN = 10,
  124. MOSQ_OPT_TCP_NODELAY = 11,
  125. MOSQ_OPT_BIND_ADDRESS = 12,
  126. MOSQ_OPT_TLS_USE_OS_CERTS = 13,
  127. };
  128. /* MQTT specification restricts client ids to a maximum of 23 characters */
  129. #define MOSQ_MQTT_ID_MAX_LENGTH 23
  130. #define MQTT_PROTOCOL_V31 3
  131. #define MQTT_PROTOCOL_V311 4
  132. #define MQTT_PROTOCOL_V5 5
  133. struct mosquitto_message{
  134. int mid;
  135. char *topic;
  136. void *payload;
  137. int payloadlen;
  138. int qos;
  139. bool retain;
  140. };
  141. struct mosquitto;
  142. typedef struct mqtt5__property mosquitto_property;
  143. /*
  144. * Topic: Threads
  145. * libmosquitto provides thread safe operation, with the exception of
  146. * <mosquitto_lib_init> which is not thread safe.
  147. *
  148. * If the library has been compiled without thread support it is *not*
  149. * guaranteed to be thread safe.
  150. *
  151. * If your application uses threads you must use <mosquitto_threaded_set> to
  152. * tell the library this is the case, otherwise it makes some optimisations
  153. * for the single threaded case that may result in unexpected behaviour for
  154. * the multi threaded case.
  155. */
  156. /***************************************************
  157. * Important note
  158. *
  159. * The following functions that deal with network operations will return
  160. * MOSQ_ERR_SUCCESS on success, but this does not mean that the operation has
  161. * taken place. An attempt will be made to write the network data, but if the
  162. * socket is not available for writing at that time then the packet will not be
  163. * sent. To ensure the packet is sent, call mosquitto_loop() (which must also
  164. * be called to process incoming network data).
  165. * This is especially important when disconnecting a client that has a will. If
  166. * the broker does not receive the DISCONNECT command, it will assume that the
  167. * client has disconnected unexpectedly and send the will.
  168. *
  169. * mosquitto_connect()
  170. * mosquitto_disconnect()
  171. * mosquitto_subscribe()
  172. * mosquitto_unsubscribe()
  173. * mosquitto_publish()
  174. ***************************************************/
  175. /* ======================================================================
  176. *
  177. * Section: Library version, init, and cleanup
  178. *
  179. * ====================================================================== */
  180. /*
  181. * Function: mosquitto_lib_version
  182. *
  183. * Can be used to obtain version information for the mosquitto library.
  184. * This allows the application to compare the library version against the
  185. * version it was compiled against by using the LIBMOSQUITTO_MAJOR,
  186. * LIBMOSQUITTO_MINOR and LIBMOSQUITTO_REVISION defines.
  187. *
  188. * Parameters:
  189. * major - an integer pointer. If not NULL, the major version of the
  190. * library will be returned in this variable.
  191. * minor - an integer pointer. If not NULL, the minor version of the
  192. * library will be returned in this variable.
  193. * revision - an integer pointer. If not NULL, the revision of the library will
  194. * be returned in this variable.
  195. *
  196. * Returns:
  197. * LIBMOSQUITTO_VERSION_NUMBER - which is a unique number based on the major,
  198. * minor and revision values.
  199. * See Also:
  200. * <mosquitto_lib_cleanup>, <mosquitto_lib_init>
  201. */
  202. libmosq_EXPORT int mosquitto_lib_version(int *major, int *minor, int *revision);
  203. /*
  204. * Function: mosquitto_lib_init
  205. *
  206. * Must be called before any other mosquitto functions.
  207. *
  208. * This function is *not* thread safe.
  209. *
  210. * Returns:
  211. * MOSQ_ERR_SUCCESS - on success.
  212. * MOSQ_ERR_UNKNOWN - on Windows, if sockets couldn't be initialized.
  213. *
  214. * See Also:
  215. * <mosquitto_lib_cleanup>, <mosquitto_lib_version>
  216. */
  217. libmosq_EXPORT int mosquitto_lib_init(void);
  218. /*
  219. * Function: mosquitto_lib_cleanup
  220. *
  221. * Call to free resources associated with the library.
  222. *
  223. * Returns:
  224. * MOSQ_ERR_SUCCESS - always
  225. *
  226. * See Also:
  227. * <mosquitto_lib_init>, <mosquitto_lib_version>
  228. */
  229. libmosq_EXPORT int mosquitto_lib_cleanup(void);
  230. /* ======================================================================
  231. *
  232. * Section: Client creation, destruction, and reinitialisation
  233. *
  234. * ====================================================================== */
  235. /*
  236. * Function: mosquitto_new
  237. *
  238. * Create a new mosquitto client instance.
  239. *
  240. * Parameters:
  241. * id - String to use as the client id. If NULL, a random client id
  242. * will be generated. If id is NULL, clean_session must be true.
  243. * clean_session - set to true to instruct the broker to clean all messages
  244. * and subscriptions on disconnect, false to instruct it to
  245. * keep them. See the man page mqtt(7) for more details.
  246. * Note that a client will never discard its own outgoing
  247. * messages on disconnect. Calling <mosquitto_connect> or
  248. * <mosquitto_reconnect> will cause the messages to be resent.
  249. * Use <mosquitto_reinitialise> to reset a client to its
  250. * original state.
  251. * Must be set to true if the id parameter is NULL.
  252. * obj - A user pointer that will be passed as an argument to any
  253. * callbacks that are specified.
  254. *
  255. * Returns:
  256. * Pointer to a struct mosquitto on success.
  257. * NULL on failure. Interrogate errno to determine the cause for the failure:
  258. * - ENOMEM on out of memory.
  259. * - EINVAL on invalid input parameters.
  260. *
  261. * See Also:
  262. * <mosquitto_reinitialise>, <mosquitto_destroy>, <mosquitto_user_data_set>
  263. */
  264. libmosq_EXPORT struct mosquitto *mosquitto_new(const char *id, bool clean_session, void *obj);
  265. /*
  266. * Function: mosquitto_destroy
  267. *
  268. * Use to free memory associated with a mosquitto client instance.
  269. *
  270. * Parameters:
  271. * mosq - a struct mosquitto pointer to free.
  272. *
  273. * See Also:
  274. * <mosquitto_new>, <mosquitto_reinitialise>
  275. */
  276. libmosq_EXPORT void mosquitto_destroy(struct mosquitto *mosq);
  277. /*
  278. * Function: mosquitto_reinitialise
  279. *
  280. * This function allows an existing mosquitto client to be reused. Call on a
  281. * mosquitto instance to close any open network connections, free memory
  282. * and reinitialise the client with the new parameters. The end result is the
  283. * same as the output of <mosquitto_new>.
  284. *
  285. * Parameters:
  286. * mosq - a valid mosquitto instance.
  287. * id - string to use as the client id. If NULL, a random client id
  288. * will be generated. If id is NULL, clean_session must be true.
  289. * clean_session - set to true to instruct the broker to clean all messages
  290. * and subscriptions on disconnect, false to instruct it to
  291. * keep them. See the man page mqtt(7) for more details.
  292. * Must be set to true if the id parameter is NULL.
  293. * obj - A user pointer that will be passed as an argument to any
  294. * callbacks that are specified.
  295. *
  296. * Returns:
  297. * MOSQ_ERR_SUCCESS - on success.
  298. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  299. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  300. *
  301. * See Also:
  302. * <mosquitto_new>, <mosquitto_destroy>
  303. */
  304. libmosq_EXPORT int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_session, void *obj);
  305. /* ======================================================================
  306. *
  307. * Section: Will
  308. *
  309. * ====================================================================== */
  310. /*
  311. * Function: mosquitto_will_set
  312. *
  313. * Configure will information for a mosquitto instance. By default, clients do
  314. * not have a will. This must be called before calling <mosquitto_connect>.
  315. *
  316. * It is valid to use this function for clients using all MQTT protocol versions.
  317. * If you need to set MQTT v5 Will properties, use <mosquitto_will_set_v5> instead.
  318. *
  319. * Parameters:
  320. * mosq - a valid mosquitto instance.
  321. * topic - the topic on which to publish the will.
  322. * payloadlen - the size of the payload (bytes). Valid values are between 0 and
  323. * 268,435,455.
  324. * payload - pointer to the data to send. If payloadlen > 0 this must be a
  325. * valid memory location.
  326. * qos - integer value 0, 1 or 2 indicating the Quality of Service to be
  327. * used for the will.
  328. * retain - set to true to make the will a retained message.
  329. *
  330. * Returns:
  331. * MOSQ_ERR_SUCCESS - on success.
  332. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  333. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  334. * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large.
  335. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8.
  336. */
  337. libmosq_EXPORT int mosquitto_will_set(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain);
  338. /*
  339. * Function: mosquitto_will_set_v5
  340. *
  341. * Configure will information for a mosquitto instance, with attached
  342. * properties. By default, clients do not have a will. This must be called
  343. * before calling <mosquitto_connect>.
  344. *
  345. * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument
  346. * will be applied to the Will. For MQTT v3.1.1 and below, the `properties`
  347. * argument will be ignored.
  348. *
  349. * Set your client to use MQTT v5 immediately after it is created:
  350. *
  351. * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  352. *
  353. * Parameters:
  354. * mosq - a valid mosquitto instance.
  355. * topic - the topic on which to publish the will.
  356. * payloadlen - the size of the payload (bytes). Valid values are between 0 and
  357. * 268,435,455.
  358. * payload - pointer to the data to send. If payloadlen > 0 this must be a
  359. * valid memory location.
  360. * qos - integer value 0, 1 or 2 indicating the Quality of Service to be
  361. * used for the will.
  362. * retain - set to true to make the will a retained message.
  363. * properties - list of MQTT 5 properties. Can be NULL. On success only, the
  364. * property list becomes the property of libmosquitto once this
  365. * function is called and will be freed by the library. The
  366. * property list must be freed by the application on error.
  367. *
  368. * Returns:
  369. * MOSQ_ERR_SUCCESS - on success.
  370. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  371. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  372. * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large.
  373. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8.
  374. * MOSQ_ERR_NOT_SUPPORTED - if properties is not NULL and the client is not
  375. * using MQTT v5
  376. * MOSQ_ERR_PROTOCOL - if a property is invalid for use with wills.
  377. * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden.
  378. */
  379. libmosq_EXPORT int mosquitto_will_set_v5(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain, mosquitto_property *properties);
  380. /*
  381. * Function: mosquitto_will_clear
  382. *
  383. * Remove a previously configured will. This must be called before calling
  384. * <mosquitto_connect>.
  385. *
  386. * Parameters:
  387. * mosq - a valid mosquitto instance.
  388. *
  389. * Returns:
  390. * MOSQ_ERR_SUCCESS - on success.
  391. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  392. */
  393. libmosq_EXPORT int mosquitto_will_clear(struct mosquitto *mosq);
  394. /* ======================================================================
  395. *
  396. * Section: Username and password
  397. *
  398. * ====================================================================== */
  399. /*
  400. * Function: mosquitto_username_pw_set
  401. *
  402. * Configure username and password for a mosquitto instance. By default, no
  403. * username or password will be sent. For v3.1 and v3.1.1 clients, if username
  404. * is NULL, the password argument is ignored.
  405. *
  406. * This is must be called before calling <mosquitto_connect>.
  407. *
  408. * Parameters:
  409. * mosq - a valid mosquitto instance.
  410. * username - the username to send as a string, or NULL to disable
  411. * authentication.
  412. * password - the password to send as a string. Set to NULL when username is
  413. * valid in order to send just a username.
  414. *
  415. * Returns:
  416. * MOSQ_ERR_SUCCESS - on success.
  417. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  418. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  419. */
  420. libmosq_EXPORT int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, const char *password);
  421. /* ======================================================================
  422. *
  423. * Section: Connecting, reconnecting, disconnecting
  424. *
  425. * ====================================================================== */
  426. /*
  427. * Function: mosquitto_connect
  428. *
  429. * Connect to an MQTT broker.
  430. *
  431. * It is valid to use this function for clients using all MQTT protocol versions.
  432. * If you need to set MQTT v5 CONNECT properties, use <mosquitto_connect_bind_v5>
  433. * instead.
  434. *
  435. * Parameters:
  436. * mosq - a valid mosquitto instance.
  437. * host - the hostname or ip address of the broker to connect to.
  438. * port - the network port to connect to. Usually 1883.
  439. * keepalive - the number of seconds after which the broker should send a PING
  440. * message to the client if no other messages have been exchanged
  441. * in that time.
  442. *
  443. * Returns:
  444. * MOSQ_ERR_SUCCESS - on success.
  445. * MOSQ_ERR_INVAL - if the input parameters were invalid, which could be any of:
  446. * * mosq == NULL
  447. * * host == NULL
  448. * * port < 0
  449. * * keepalive < 5
  450. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  451. * contains the error code, even on Windows.
  452. * Use strerror_r() where available or FormatMessage() on
  453. * Windows.
  454. *
  455. * See Also:
  456. * <mosquitto_connect_bind>, <mosquitto_connect_async>, <mosquitto_reconnect>, <mosquitto_disconnect>, <mosquitto_tls_set>
  457. */
  458. libmosq_EXPORT int mosquitto_connect(struct mosquitto *mosq, const char *host, int port, int keepalive);
  459. /*
  460. * Function: mosquitto_connect_bind
  461. *
  462. * Connect to an MQTT broker. This extends the functionality of
  463. * <mosquitto_connect> by adding the bind_address parameter. Use this function
  464. * if you need to restrict network communication over a particular interface.
  465. *
  466. * Parameters:
  467. * mosq - a valid mosquitto instance.
  468. * host - the hostname or ip address of the broker to connect to.
  469. * port - the network port to connect to. Usually 1883.
  470. * keepalive - the number of seconds after which the broker should send a PING
  471. * message to the client if no other messages have been exchanged
  472. * in that time.
  473. * bind_address - the hostname or ip address of the local network interface to
  474. * bind to. If you do not want to bind to a specific interface,
  475. * set this to NULL.
  476. *
  477. * Returns:
  478. * MOSQ_ERR_SUCCESS - on success.
  479. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  480. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  481. * contains the error code, even on Windows.
  482. * Use strerror_r() where available or FormatMessage() on
  483. * Windows.
  484. *
  485. * See Also:
  486. * <mosquitto_connect>, <mosquitto_connect_async>, <mosquitto_connect_bind_async>
  487. */
  488. libmosq_EXPORT int mosquitto_connect_bind(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address);
  489. /*
  490. * Function: mosquitto_connect_bind_v5
  491. *
  492. * Connect to an MQTT broker. This extends the functionality of
  493. * <mosquitto_connect> by adding the bind_address parameter and MQTT v5
  494. * properties. Use this function if you need to restrict network communication
  495. * over a particular interface.
  496. *
  497. * Use e.g. <mosquitto_property_add_string> and similar to create a list of
  498. * properties, then attach them to this publish. Properties need freeing with
  499. * <mosquitto_property_free_all>.
  500. *
  501. * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument
  502. * will be applied to the CONNECT message. For MQTT v3.1.1 and below, the
  503. * `properties` argument will be ignored.
  504. *
  505. * Set your client to use MQTT v5 immediately after it is created:
  506. *
  507. * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  508. *
  509. * Parameters:
  510. * mosq - a valid mosquitto instance.
  511. * host - the hostname or ip address of the broker to connect to.
  512. * port - the network port to connect to. Usually 1883.
  513. * keepalive - the number of seconds after which the broker should send a PING
  514. * message to the client if no other messages have been exchanged
  515. * in that time.
  516. * bind_address - the hostname or ip address of the local network interface to
  517. * bind to. If you do not want to bind to a specific interface,
  518. * set this to NULL.
  519. * properties - the MQTT 5 properties for the connect (not for the Will).
  520. *
  521. * Returns:
  522. * MOSQ_ERR_SUCCESS - on success.
  523. * MOSQ_ERR_INVAL - if the input parameters were invalid, which could be any of:
  524. * * mosq == NULL
  525. * * host == NULL
  526. * * port < 0
  527. * * keepalive < 5
  528. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  529. * contains the error code, even on Windows.
  530. * Use strerror_r() where available or FormatMessage() on
  531. * Windows.
  532. * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden.
  533. * MOSQ_ERR_PROTOCOL - if any property is invalid for use with CONNECT.
  534. *
  535. * See Also:
  536. * <mosquitto_connect>, <mosquitto_connect_async>, <mosquitto_connect_bind_async>
  537. */
  538. libmosq_EXPORT int mosquitto_connect_bind_v5(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address, const mosquitto_property *properties);
  539. /*
  540. * Function: mosquitto_connect_async
  541. *
  542. * Connect to an MQTT broker. This is a non-blocking call. If you use
  543. * <mosquitto_connect_async> your client must use the threaded interface
  544. * <mosquitto_loop_start>. If you need to use <mosquitto_loop>, you must use
  545. * <mosquitto_connect> to connect the client.
  546. *
  547. * May be called before or after <mosquitto_loop_start>.
  548. *
  549. * Parameters:
  550. * mosq - a valid mosquitto instance.
  551. * host - the hostname or ip address of the broker to connect to.
  552. * port - the network port to connect to. Usually 1883.
  553. * keepalive - the number of seconds after which the broker should send a PING
  554. * message to the client if no other messages have been exchanged
  555. * in that time.
  556. *
  557. * Returns:
  558. * MOSQ_ERR_SUCCESS - on success.
  559. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  560. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  561. * contains the error code, even on Windows.
  562. * Use strerror_r() where available or FormatMessage() on
  563. * Windows.
  564. *
  565. * See Also:
  566. * <mosquitto_connect_bind_async>, <mosquitto_connect>, <mosquitto_reconnect>, <mosquitto_disconnect>, <mosquitto_tls_set>
  567. */
  568. libmosq_EXPORT int mosquitto_connect_async(struct mosquitto *mosq, const char *host, int port, int keepalive);
  569. /*
  570. * Function: mosquitto_connect_bind_async
  571. *
  572. * Connect to an MQTT broker. This is a non-blocking call. If you use
  573. * <mosquitto_connect_bind_async> your client must use the threaded interface
  574. * <mosquitto_loop_start>. If you need to use <mosquitto_loop>, you must use
  575. * <mosquitto_connect> to connect the client.
  576. *
  577. * This extends the functionality of <mosquitto_connect_async> by adding the
  578. * bind_address parameter. Use this function if you need to restrict network
  579. * communication over a particular interface.
  580. *
  581. * May be called before or after <mosquitto_loop_start>.
  582. *
  583. * Parameters:
  584. * mosq - a valid mosquitto instance.
  585. * host - the hostname or ip address of the broker to connect to.
  586. * port - the network port to connect to. Usually 1883.
  587. * keepalive - the number of seconds after which the broker should send a PING
  588. * message to the client if no other messages have been exchanged
  589. * in that time.
  590. * bind_address - the hostname or ip address of the local network interface to
  591. * bind to. If you do not want to bind to a specific interface,
  592. * set this to NULL.
  593. *
  594. * Returns:
  595. * MOSQ_ERR_SUCCESS - on success.
  596. * MOSQ_ERR_INVAL - if the input parameters were invalid, which could be any of:
  597. * * mosq == NULL
  598. * * host == NULL
  599. * * port < 0
  600. * * keepalive < 5
  601. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  602. * contains the error code, even on Windows.
  603. * Use strerror_r() where available or FormatMessage() on
  604. * Windows.
  605. *
  606. * See Also:
  607. * <mosquitto_connect_async>, <mosquitto_connect>, <mosquitto_connect_bind>
  608. */
  609. libmosq_EXPORT int mosquitto_connect_bind_async(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address);
  610. /*
  611. * Function: mosquitto_connect_srv
  612. *
  613. * Connect to an MQTT broker.
  614. *
  615. * If you set `host` to `example.com`, then this call will attempt to retrieve
  616. * the DNS SRV record for `_secure-mqtt._tcp.example.com` or
  617. * `_mqtt._tcp.example.com` to discover which actual host to connect to.
  618. *
  619. * DNS SRV support is not usually compiled in to libmosquitto, use of this call
  620. * is not recommended.
  621. *
  622. * Parameters:
  623. * mosq - a valid mosquitto instance.
  624. * host - the hostname to search for an SRV record.
  625. * keepalive - the number of seconds after which the broker should send a PING
  626. * message to the client if no other messages have been exchanged
  627. * in that time.
  628. * bind_address - the hostname or ip address of the local network interface to
  629. * bind to. If you do not want to bind to a specific interface,
  630. * set this to NULL.
  631. *
  632. * Returns:
  633. * MOSQ_ERR_SUCCESS - on success.
  634. * MOSQ_ERR_INVAL - if the input parameters were invalid, which could be any of:
  635. * * mosq == NULL
  636. * * host == NULL
  637. * * port < 0
  638. * * keepalive < 5
  639. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  640. * contains the error code, even on Windows.
  641. * Use strerror_r() where available or FormatMessage() on
  642. * Windows.
  643. *
  644. * See Also:
  645. * <mosquitto_connect_async>, <mosquitto_connect>, <mosquitto_connect_bind>
  646. */
  647. libmosq_EXPORT int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepalive, const char *bind_address);
  648. /*
  649. * Function: mosquitto_reconnect
  650. *
  651. * Reconnect to a broker.
  652. *
  653. * This function provides an easy way of reconnecting to a broker after a
  654. * connection has been lost. It uses the values that were provided in the
  655. * <mosquitto_connect> call. It must not be called before
  656. * <mosquitto_connect>.
  657. *
  658. * Parameters:
  659. * mosq - a valid mosquitto instance.
  660. *
  661. * Returns:
  662. * MOSQ_ERR_SUCCESS - on success.
  663. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  664. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  665. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  666. * contains the error code, even on Windows.
  667. * Use strerror_r() where available or FormatMessage() on
  668. * Windows.
  669. *
  670. * See Also:
  671. * <mosquitto_connect>, <mosquitto_disconnect>, <mosquitto_reconnect_async>
  672. */
  673. libmosq_EXPORT int mosquitto_reconnect(struct mosquitto *mosq);
  674. /*
  675. * Function: mosquitto_reconnect_async
  676. *
  677. * Reconnect to a broker. Non blocking version of <mosquitto_reconnect>.
  678. *
  679. * This function provides an easy way of reconnecting to a broker after a
  680. * connection has been lost. It uses the values that were provided in the
  681. * <mosquitto_connect> or <mosquitto_connect_async> calls. It must not be
  682. * called before <mosquitto_connect>.
  683. *
  684. * Parameters:
  685. * mosq - a valid mosquitto instance.
  686. *
  687. * Returns:
  688. * MOSQ_ERR_SUCCESS - on success.
  689. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  690. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  691. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  692. * contains the error code, even on Windows.
  693. * Use strerror_r() where available or FormatMessage() on
  694. * Windows.
  695. *
  696. * See Also:
  697. * <mosquitto_connect>, <mosquitto_disconnect>
  698. */
  699. libmosq_EXPORT int mosquitto_reconnect_async(struct mosquitto *mosq);
  700. /*
  701. * Function: mosquitto_disconnect
  702. *
  703. * Disconnect from the broker.
  704. *
  705. * It is valid to use this function for clients using all MQTT protocol versions.
  706. * If you need to set MQTT v5 DISCONNECT properties, use
  707. * <mosquitto_disconnect_v5> instead.
  708. *
  709. * Parameters:
  710. * mosq - a valid mosquitto instance.
  711. *
  712. * Returns:
  713. * MOSQ_ERR_SUCCESS - on success.
  714. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  715. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  716. */
  717. libmosq_EXPORT int mosquitto_disconnect(struct mosquitto *mosq);
  718. /*
  719. * Function: mosquitto_disconnect_v5
  720. *
  721. * Disconnect from the broker, with attached MQTT properties.
  722. *
  723. * Use e.g. <mosquitto_property_add_string> and similar to create a list of
  724. * properties, then attach them to this publish. Properties need freeing with
  725. * <mosquitto_property_free_all>.
  726. *
  727. * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument
  728. * will be applied to the DISCONNECT message. For MQTT v3.1.1 and below, the
  729. * `properties` argument will be ignored.
  730. *
  731. * Set your client to use MQTT v5 immediately after it is created:
  732. *
  733. * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  734. *
  735. * Parameters:
  736. * mosq - a valid mosquitto instance.
  737. * reason_code - the disconnect reason code.
  738. * properties - a valid mosquitto_property list, or NULL.
  739. *
  740. * Returns:
  741. * MOSQ_ERR_SUCCESS - on success.
  742. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  743. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  744. * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden.
  745. * MOSQ_ERR_PROTOCOL - if any property is invalid for use with DISCONNECT.
  746. */
  747. libmosq_EXPORT int mosquitto_disconnect_v5(struct mosquitto *mosq, int reason_code, const mosquitto_property *properties);
  748. /* ======================================================================
  749. *
  750. * Section: Publishing, subscribing, unsubscribing
  751. *
  752. * ====================================================================== */
  753. /*
  754. * Function: mosquitto_publish
  755. *
  756. * Publish a message on a given topic.
  757. *
  758. * It is valid to use this function for clients using all MQTT protocol versions.
  759. * If you need to set MQTT v5 PUBLISH properties, use <mosquitto_publish_v5>
  760. * instead.
  761. *
  762. * Parameters:
  763. * mosq - a valid mosquitto instance.
  764. * mid - pointer to an int. If not NULL, the function will set this
  765. * to the message id of this particular message. This can be then
  766. * used with the publish callback to determine when the message
  767. * has been sent.
  768. * Note that although the MQTT protocol doesn't use message ids
  769. * for messages with QoS=0, libmosquitto assigns them message ids
  770. * so they can be tracked with this parameter.
  771. * topic - null terminated string of the topic to publish to.
  772. * payloadlen - the size of the payload (bytes). Valid values are between 0 and
  773. * 268,435,455.
  774. * payload - pointer to the data to send. If payloadlen > 0 this must be a
  775. * valid memory location.
  776. * qos - integer value 0, 1 or 2 indicating the Quality of Service to be
  777. * used for the message.
  778. * retain - set to true to make the message retained.
  779. *
  780. * Returns:
  781. * MOSQ_ERR_SUCCESS - on success.
  782. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  783. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  784. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  785. * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the
  786. * broker.
  787. * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large.
  788. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8
  789. * MOSQ_ERR_QOS_NOT_SUPPORTED - if the QoS is greater than that supported by
  790. * the broker.
  791. * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than
  792. * supported by the broker.
  793. *
  794. * See Also:
  795. * <mosquitto_max_inflight_messages_set>
  796. */
  797. libmosq_EXPORT int mosquitto_publish(struct mosquitto *mosq, int *mid, const char *topic, int payloadlen, const void *payload, int qos, bool retain);
  798. /*
  799. * Function: mosquitto_publish_v5
  800. *
  801. * Publish a message on a given topic, with attached MQTT properties.
  802. *
  803. * Use e.g. <mosquitto_property_add_string> and similar to create a list of
  804. * properties, then attach them to this publish. Properties need freeing with
  805. * <mosquitto_property_free_all>.
  806. *
  807. * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument
  808. * will be applied to the PUBLISH message. For MQTT v3.1.1 and below, the
  809. * `properties` argument will be ignored.
  810. *
  811. * Set your client to use MQTT v5 immediately after it is created:
  812. *
  813. * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  814. *
  815. * Parameters:
  816. * mosq - a valid mosquitto instance.
  817. * mid - pointer to an int. If not NULL, the function will set this
  818. * to the message id of this particular message. This can be then
  819. * used with the publish callback to determine when the message
  820. * has been sent.
  821. * Note that although the MQTT protocol doesn't use message ids
  822. * for messages with QoS=0, libmosquitto assigns them message ids
  823. * so they can be tracked with this parameter.
  824. * topic - null terminated string of the topic to publish to.
  825. * payloadlen - the size of the payload (bytes). Valid values are between 0 and
  826. * 268,435,455.
  827. * payload - pointer to the data to send. If payloadlen > 0 this must be a
  828. * valid memory location.
  829. * qos - integer value 0, 1 or 2 indicating the Quality of Service to be
  830. * used for the message.
  831. * retain - set to true to make the message retained.
  832. * properties - a valid mosquitto_property list, or NULL.
  833. *
  834. * Returns:
  835. * MOSQ_ERR_SUCCESS - on success.
  836. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  837. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  838. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  839. * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the
  840. * broker.
  841. * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large.
  842. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8
  843. * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden.
  844. * MOSQ_ERR_PROTOCOL - if any property is invalid for use with PUBLISH.
  845. * MOSQ_ERR_QOS_NOT_SUPPORTED - if the QoS is greater than that supported by
  846. * the broker.
  847. * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than
  848. * supported by the broker.
  849. */
  850. libmosq_EXPORT int mosquitto_publish_v5(
  851. struct mosquitto *mosq,
  852. int *mid,
  853. const char *topic,
  854. int payloadlen,
  855. const void *payload,
  856. int qos,
  857. bool retain,
  858. const mosquitto_property *properties);
  859. /*
  860. * Function: mosquitto_subscribe
  861. *
  862. * Subscribe to a topic.
  863. *
  864. * It is valid to use this function for clients using all MQTT protocol versions.
  865. * If you need to set MQTT v5 SUBSCRIBE properties, use <mosquitto_subscribe_v5>
  866. * instead.
  867. *
  868. * Parameters:
  869. * mosq - a valid mosquitto instance.
  870. * mid - a pointer to an int. If not NULL, the function will set this to
  871. * the message id of this particular message. This can be then used
  872. * with the subscribe callback to determine when the message has been
  873. * sent.
  874. * sub - the subscription pattern.
  875. * qos - the requested Quality of Service for this subscription.
  876. *
  877. * Returns:
  878. * MOSQ_ERR_SUCCESS - on success.
  879. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  880. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  881. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  882. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8
  883. * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than
  884. * supported by the broker.
  885. */
  886. libmosq_EXPORT int mosquitto_subscribe(struct mosquitto *mosq, int *mid, const char *sub, int qos);
  887. /*
  888. * Function: mosquitto_subscribe_v5
  889. *
  890. * Subscribe to a topic, with attached MQTT properties.
  891. *
  892. * Use e.g. <mosquitto_property_add_string> and similar to create a list of
  893. * properties, then attach them to this publish. Properties need freeing with
  894. * <mosquitto_property_free_all>.
  895. *
  896. * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument
  897. * will be applied to the PUBLISH message. For MQTT v3.1.1 and below, the
  898. * `properties` argument will be ignored.
  899. *
  900. * Set your client to use MQTT v5 immediately after it is created:
  901. *
  902. * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  903. *
  904. * Parameters:
  905. * mosq - a valid mosquitto instance.
  906. * mid - a pointer to an int. If not NULL, the function will set this to
  907. * the message id of this particular message. This can be then used
  908. * with the subscribe callback to determine when the message has been
  909. * sent.
  910. * sub - the subscription pattern.
  911. * qos - the requested Quality of Service for this subscription.
  912. * options - options to apply to this subscription, OR'd together. Set to 0 to
  913. * use the default options, otherwise choose from list of <mqtt5_sub_options>
  914. * properties - a valid mosquitto_property list, or NULL.
  915. *
  916. * Returns:
  917. * MOSQ_ERR_SUCCESS - on success.
  918. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  919. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  920. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  921. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8
  922. * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden.
  923. * MOSQ_ERR_PROTOCOL - if any property is invalid for use with SUBSCRIBE.
  924. * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than
  925. * supported by the broker.
  926. */
  927. libmosq_EXPORT int mosquitto_subscribe_v5(struct mosquitto *mosq, int *mid, const char *sub, int qos, int options, const mosquitto_property *properties);
  928. /*
  929. * Function: mosquitto_subscribe_multiple
  930. *
  931. * Subscribe to multiple topics.
  932. *
  933. * Parameters:
  934. * mosq - a valid mosquitto instance.
  935. * mid - a pointer to an int. If not NULL, the function will set this to
  936. * the message id of this particular message. This can be then used
  937. * with the subscribe callback to determine when the message has been
  938. * sent.
  939. * sub_count - the count of subscriptions to be made
  940. * sub - array of sub_count pointers, each pointing to a subscription string.
  941. * The "char *const *const" datatype ensures that neither the array of
  942. * pointers nor the strings that they point to are mutable. If you aren't
  943. * familiar with this, just think of it as a safer "char **",
  944. * equivalent to "const char *" for a simple string pointer.
  945. * qos - the requested Quality of Service for each subscription.
  946. * options - options to apply to this subscription, OR'd together. This
  947. * argument is not used for MQTT v3 susbcriptions. Set to 0 to use
  948. * the default options, otherwise choose from list of <mqtt5_sub_options>
  949. * properties - a valid mosquitto_property list, or NULL. Only used with MQTT
  950. * v5 clients.
  951. *
  952. * Returns:
  953. * MOSQ_ERR_SUCCESS - on success.
  954. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  955. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  956. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  957. * MOSQ_ERR_MALFORMED_UTF8 - if a topic is not valid UTF-8
  958. * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than
  959. * supported by the broker.
  960. */
  961. libmosq_EXPORT int mosquitto_subscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count, char *const *const sub, int qos, int options, const mosquitto_property *properties);
  962. /*
  963. * Function: mosquitto_unsubscribe
  964. *
  965. * Unsubscribe from a topic.
  966. *
  967. * Parameters:
  968. * mosq - a valid mosquitto instance.
  969. * mid - a pointer to an int. If not NULL, the function will set this to
  970. * the message id of this particular message. This can be then used
  971. * with the unsubscribe callback to determine when the message has been
  972. * sent.
  973. * sub - the unsubscription pattern.
  974. *
  975. * Returns:
  976. * MOSQ_ERR_SUCCESS - on success.
  977. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  978. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  979. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  980. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8
  981. * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than
  982. * supported by the broker.
  983. */
  984. libmosq_EXPORT int mosquitto_unsubscribe(struct mosquitto *mosq, int *mid, const char *sub);
  985. /*
  986. * Function: mosquitto_unsubscribe_v5
  987. *
  988. * Unsubscribe from a topic, with attached MQTT properties.
  989. *
  990. * It is valid to use this function for clients using all MQTT protocol versions.
  991. * If you need to set MQTT v5 UNSUBSCRIBE properties, use
  992. * <mosquitto_unsubscribe_v5> instead.
  993. *
  994. * Use e.g. <mosquitto_property_add_string> and similar to create a list of
  995. * properties, then attach them to this publish. Properties need freeing with
  996. * <mosquitto_property_free_all>.
  997. *
  998. * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument
  999. * will be applied to the PUBLISH message. For MQTT v3.1.1 and below, the
  1000. * `properties` argument will be ignored.
  1001. *
  1002. * Set your client to use MQTT v5 immediately after it is created:
  1003. *
  1004. * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5);
  1005. *
  1006. * Parameters:
  1007. * mosq - a valid mosquitto instance.
  1008. * mid - a pointer to an int. If not NULL, the function will set this to
  1009. * the message id of this particular message. This can be then used
  1010. * with the unsubscribe callback to determine when the message has been
  1011. * sent.
  1012. * sub - the unsubscription pattern.
  1013. * properties - a valid mosquitto_property list, or NULL. Only used with MQTT
  1014. * v5 clients.
  1015. *
  1016. * Returns:
  1017. * MOSQ_ERR_SUCCESS - on success.
  1018. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1019. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1020. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  1021. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8
  1022. * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden.
  1023. * MOSQ_ERR_PROTOCOL - if any property is invalid for use with UNSUBSCRIBE.
  1024. * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than
  1025. * supported by the broker.
  1026. */
  1027. libmosq_EXPORT int mosquitto_unsubscribe_v5(struct mosquitto *mosq, int *mid, const char *sub, const mosquitto_property *properties);
  1028. /*
  1029. * Function: mosquitto_unsubscribe_multiple
  1030. *
  1031. * Unsubscribe from multiple topics.
  1032. *
  1033. * Parameters:
  1034. * mosq - a valid mosquitto instance.
  1035. * mid - a pointer to an int. If not NULL, the function will set this to
  1036. * the message id of this particular message. This can be then used
  1037. * with the subscribe callback to determine when the message has been
  1038. * sent.
  1039. * sub_count - the count of unsubscriptions to be made
  1040. * sub - array of sub_count pointers, each pointing to an unsubscription string.
  1041. * The "char *const *const" datatype ensures that neither the array of
  1042. * pointers nor the strings that they point to are mutable. If you aren't
  1043. * familiar with this, just think of it as a safer "char **",
  1044. * equivalent to "const char *" for a simple string pointer.
  1045. * properties - a valid mosquitto_property list, or NULL. Only used with MQTT
  1046. * v5 clients.
  1047. *
  1048. * Returns:
  1049. * MOSQ_ERR_SUCCESS - on success.
  1050. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1051. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1052. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  1053. * MOSQ_ERR_MALFORMED_UTF8 - if a topic is not valid UTF-8
  1054. * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than
  1055. * supported by the broker.
  1056. */
  1057. libmosq_EXPORT int mosquitto_unsubscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count, char *const *const sub, const mosquitto_property *properties);
  1058. /* ======================================================================
  1059. *
  1060. * Section: Struct mosquitto_message helper functions
  1061. *
  1062. * ====================================================================== */
  1063. /*
  1064. * Function: mosquitto_message_copy
  1065. *
  1066. * Copy the contents of a mosquitto message to another message.
  1067. * Useful for preserving a message received in the on_message() callback.
  1068. *
  1069. * Parameters:
  1070. * dst - a pointer to a valid mosquitto_message struct to copy to.
  1071. * src - a pointer to a valid mosquitto_message struct to copy from.
  1072. *
  1073. * Returns:
  1074. * MOSQ_ERR_SUCCESS - on success.
  1075. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1076. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1077. *
  1078. * See Also:
  1079. * <mosquitto_message_free>
  1080. */
  1081. libmosq_EXPORT int mosquitto_message_copy(struct mosquitto_message *dst, const struct mosquitto_message *src);
  1082. /*
  1083. * Function: mosquitto_message_free
  1084. *
  1085. * Completely free a mosquitto_message struct.
  1086. *
  1087. * Parameters:
  1088. * message - pointer to a mosquitto_message pointer to free.
  1089. *
  1090. * See Also:
  1091. * <mosquitto_message_copy>, <mosquitto_message_free_contents>
  1092. */
  1093. libmosq_EXPORT void mosquitto_message_free(struct mosquitto_message **message);
  1094. /*
  1095. * Function: mosquitto_message_free_contents
  1096. *
  1097. * Free a mosquitto_message struct contents, leaving the struct unaffected.
  1098. *
  1099. * Parameters:
  1100. * message - pointer to a mosquitto_message struct to free its contents.
  1101. *
  1102. * See Also:
  1103. * <mosquitto_message_copy>, <mosquitto_message_free>
  1104. */
  1105. libmosq_EXPORT void mosquitto_message_free_contents(struct mosquitto_message *message);
  1106. /* ======================================================================
  1107. *
  1108. * Section: Network loop (managed by libmosquitto)
  1109. *
  1110. * The internal network loop must be called at a regular interval. The two
  1111. * recommended approaches are to use either <mosquitto_loop_forever> or
  1112. * <mosquitto_loop_start>. <mosquitto_loop_forever> is a blocking call and is
  1113. * suitable for the situation where you only want to handle incoming messages
  1114. * in callbacks. <mosquitto_loop_start> is a non-blocking call, it creates a
  1115. * separate thread to run the loop for you. Use this function when you have
  1116. * other tasks you need to run at the same time as the MQTT client, e.g.
  1117. * reading data from a sensor.
  1118. *
  1119. * ====================================================================== */
  1120. /*
  1121. * Function: mosquitto_loop_forever
  1122. *
  1123. * This function call loop() for you in an infinite blocking loop. It is useful
  1124. * for the case where you only want to run the MQTT client loop in your
  1125. * program.
  1126. *
  1127. * It handles reconnecting in case server connection is lost. If you call
  1128. * mosquitto_disconnect() in a callback it will return.
  1129. *
  1130. * Parameters:
  1131. * mosq - a valid mosquitto instance.
  1132. * timeout - Maximum number of milliseconds to wait for network activity
  1133. * in the select() call before timing out. Set to 0 for instant
  1134. * return. Set negative to use the default of 1000ms.
  1135. * max_packets - this parameter is currently unused and should be set to 1 for
  1136. * future compatibility.
  1137. *
  1138. * Returns:
  1139. * MOSQ_ERR_SUCCESS - on success.
  1140. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1141. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1142. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  1143. * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost.
  1144. * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the
  1145. * broker.
  1146. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  1147. * contains the error code, even on Windows.
  1148. * Use strerror_r() where available or FormatMessage() on
  1149. * Windows.
  1150. *
  1151. * See Also:
  1152. * <mosquitto_loop>, <mosquitto_loop_start>
  1153. */
  1154. libmosq_EXPORT int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets);
  1155. /*
  1156. * Function: mosquitto_loop_start
  1157. *
  1158. * This is part of the threaded client interface. Call this once to start a new
  1159. * thread to process network traffic. This provides an alternative to
  1160. * repeatedly calling <mosquitto_loop> yourself.
  1161. *
  1162. * Parameters:
  1163. * mosq - a valid mosquitto instance.
  1164. *
  1165. * Returns:
  1166. * MOSQ_ERR_SUCCESS - on success.
  1167. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1168. * MOSQ_ERR_NOT_SUPPORTED - if thread support is not available.
  1169. *
  1170. * See Also:
  1171. * <mosquitto_connect_async>, <mosquitto_loop>, <mosquitto_loop_forever>, <mosquitto_loop_stop>
  1172. */
  1173. libmosq_EXPORT int mosquitto_loop_start(struct mosquitto *mosq);
  1174. /*
  1175. * Function: mosquitto_loop_stop
  1176. *
  1177. * This is part of the threaded client interface. Call this once to stop the
  1178. * network thread previously created with <mosquitto_loop_start>. This call
  1179. * will block until the network thread finishes. For the network thread to end,
  1180. * you must have previously called <mosquitto_disconnect> or have set the force
  1181. * parameter to true.
  1182. *
  1183. * Parameters:
  1184. * mosq - a valid mosquitto instance.
  1185. * force - set to true to force thread cancellation. If false,
  1186. * <mosquitto_disconnect> must have already been called.
  1187. *
  1188. * Returns:
  1189. * MOSQ_ERR_SUCCESS - on success.
  1190. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1191. * MOSQ_ERR_NOT_SUPPORTED - if thread support is not available.
  1192. *
  1193. * See Also:
  1194. * <mosquitto_loop>, <mosquitto_loop_start>
  1195. */
  1196. libmosq_EXPORT int mosquitto_loop_stop(struct mosquitto *mosq, bool force);
  1197. /*
  1198. * Function: mosquitto_loop
  1199. *
  1200. * The main network loop for the client. This must be called frequently
  1201. * to keep communications between the client and broker working. This is
  1202. * carried out by <mosquitto_loop_forever> and <mosquitto_loop_start>, which
  1203. * are the recommended ways of handling the network loop. You may also use this
  1204. * function if you wish. It must not be called inside a callback.
  1205. *
  1206. * If incoming data is present it will then be processed. Outgoing commands,
  1207. * from e.g. <mosquitto_publish>, are normally sent immediately that their
  1208. * function is called, but this is not always possible. <mosquitto_loop> will
  1209. * also attempt to send any remaining outgoing messages, which also includes
  1210. * commands that are part of the flow for messages with QoS>0.
  1211. *
  1212. * This calls select() to monitor the client network socket. If you want to
  1213. * integrate mosquitto client operation with your own select() call, use
  1214. * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write> and
  1215. * <mosquitto_loop_misc>.
  1216. *
  1217. * Threads:
  1218. *
  1219. * Parameters:
  1220. * mosq - a valid mosquitto instance.
  1221. * timeout - Maximum number of milliseconds to wait for network activity
  1222. * in the select() call before timing out. Set to 0 for instant
  1223. * return. Set negative to use the default of 1000ms.
  1224. * max_packets - this parameter is currently unused and should be set to 1 for
  1225. * future compatibility.
  1226. *
  1227. * Returns:
  1228. * MOSQ_ERR_SUCCESS - on success.
  1229. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1230. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1231. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  1232. * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost.
  1233. * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the
  1234. * broker.
  1235. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  1236. * contains the error code, even on Windows.
  1237. * Use strerror_r() where available or FormatMessage() on
  1238. * Windows.
  1239. * See Also:
  1240. * <mosquitto_loop_forever>, <mosquitto_loop_start>, <mosquitto_loop_stop>
  1241. */
  1242. libmosq_EXPORT int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets);
  1243. /* ======================================================================
  1244. *
  1245. * Section: Network loop (for use in other event loops)
  1246. *
  1247. * ====================================================================== */
  1248. /*
  1249. * Function: mosquitto_loop_read
  1250. *
  1251. * Carry out network read operations.
  1252. * This should only be used if you are not using mosquitto_loop() and are
  1253. * monitoring the client network socket for activity yourself.
  1254. *
  1255. * Parameters:
  1256. * mosq - a valid mosquitto instance.
  1257. * max_packets - this parameter is currently unused and should be set to 1 for
  1258. * future compatibility.
  1259. *
  1260. * Returns:
  1261. * MOSQ_ERR_SUCCESS - on success.
  1262. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1263. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1264. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  1265. * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost.
  1266. * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the
  1267. * broker.
  1268. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  1269. * contains the error code, even on Windows.
  1270. * Use strerror_r() where available or FormatMessage() on
  1271. * Windows.
  1272. *
  1273. * See Also:
  1274. * <mosquitto_socket>, <mosquitto_loop_write>, <mosquitto_loop_misc>
  1275. */
  1276. libmosq_EXPORT int mosquitto_loop_read(struct mosquitto *mosq, int max_packets);
  1277. /*
  1278. * Function: mosquitto_loop_write
  1279. *
  1280. * Carry out network write operations.
  1281. * This should only be used if you are not using mosquitto_loop() and are
  1282. * monitoring the client network socket for activity yourself.
  1283. *
  1284. * Parameters:
  1285. * mosq - a valid mosquitto instance.
  1286. * max_packets - this parameter is currently unused and should be set to 1 for
  1287. * future compatibility.
  1288. *
  1289. * Returns:
  1290. * MOSQ_ERR_SUCCESS - on success.
  1291. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1292. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1293. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  1294. * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost.
  1295. * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the
  1296. * broker.
  1297. * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno
  1298. * contains the error code, even on Windows.
  1299. * Use strerror_r() where available or FormatMessage() on
  1300. * Windows.
  1301. *
  1302. * See Also:
  1303. * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_misc>, <mosquitto_want_write>
  1304. */
  1305. libmosq_EXPORT int mosquitto_loop_write(struct mosquitto *mosq, int max_packets);
  1306. /*
  1307. * Function: mosquitto_loop_misc
  1308. *
  1309. * Carry out miscellaneous operations required as part of the network loop.
  1310. * This should only be used if you are not using mosquitto_loop() and are
  1311. * monitoring the client network socket for activity yourself.
  1312. *
  1313. * This function deals with handling PINGs and checking whether messages need
  1314. * to be retried, so should be called fairly frequently, around once per second
  1315. * is sufficient.
  1316. *
  1317. * Parameters:
  1318. * mosq - a valid mosquitto instance.
  1319. *
  1320. * Returns:
  1321. * MOSQ_ERR_SUCCESS - on success.
  1322. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1323. * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
  1324. *
  1325. * See Also:
  1326. * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write>
  1327. */
  1328. libmosq_EXPORT int mosquitto_loop_misc(struct mosquitto *mosq);
  1329. /* ======================================================================
  1330. *
  1331. * Section: Network loop (helper functions)
  1332. *
  1333. * ====================================================================== */
  1334. /*
  1335. * Function: mosquitto_socket
  1336. *
  1337. * Return the socket handle for a mosquitto instance. Useful if you want to
  1338. * include a mosquitto client in your own select() calls.
  1339. *
  1340. * Parameters:
  1341. * mosq - a valid mosquitto instance.
  1342. *
  1343. * Returns:
  1344. * The socket for the mosquitto client or -1 on failure.
  1345. */
  1346. libmosq_EXPORT int mosquitto_socket(struct mosquitto *mosq);
  1347. /*
  1348. * Function: mosquitto_want_write
  1349. *
  1350. * Returns true if there is data ready to be written on the socket.
  1351. *
  1352. * Parameters:
  1353. * mosq - a valid mosquitto instance.
  1354. *
  1355. * See Also:
  1356. * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write>
  1357. */
  1358. libmosq_EXPORT bool mosquitto_want_write(struct mosquitto *mosq);
  1359. /*
  1360. * Function: mosquitto_threaded_set
  1361. *
  1362. * Used to tell the library that your application is using threads, but not
  1363. * using <mosquitto_loop_start>. The library operates slightly differently when
  1364. * not in threaded mode in order to simplify its operation. If you are managing
  1365. * your own threads and do not use this function you will experience crashes
  1366. * due to race conditions.
  1367. *
  1368. * When using <mosquitto_loop_start>, this is set automatically.
  1369. *
  1370. * Parameters:
  1371. * mosq - a valid mosquitto instance.
  1372. * threaded - true if your application is using threads, false otherwise.
  1373. */
  1374. libmosq_EXPORT int mosquitto_threaded_set(struct mosquitto *mosq, bool threaded);
  1375. /* ======================================================================
  1376. *
  1377. * Section: Client options
  1378. *
  1379. * ====================================================================== */
  1380. /*
  1381. * Function: mosquitto_opts_set
  1382. *
  1383. * Used to set options for the client.
  1384. *
  1385. * This function is deprecated, the replacement <mosquitto_int_option>,
  1386. * <mosquitto_string_option> and <mosquitto_void_option> functions should
  1387. * be used instead.
  1388. *
  1389. * Parameters:
  1390. * mosq - a valid mosquitto instance.
  1391. * option - the option to set.
  1392. * value - the option specific value.
  1393. *
  1394. * Options:
  1395. * MOSQ_OPT_PROTOCOL_VERSION - Value must be an int, set to either
  1396. * MQTT_PROTOCOL_V31 or MQTT_PROTOCOL_V311. Must be set
  1397. * before the client connects.
  1398. * Defaults to MQTT_PROTOCOL_V31.
  1399. *
  1400. * MOSQ_OPT_SSL_CTX - Pass an openssl SSL_CTX to be used when creating
  1401. * TLS connections rather than libmosquitto creating its own.
  1402. * This must be called before connecting to have any effect.
  1403. * If you use this option, the onus is on you to ensure that
  1404. * you are using secure settings.
  1405. * Setting to NULL means that libmosquitto will use its own SSL_CTX
  1406. * if TLS is to be used.
  1407. * This option is only available for openssl 1.1.0 and higher.
  1408. *
  1409. * MOSQ_OPT_SSL_CTX_WITH_DEFAULTS - Value must be an int set to 1 or 0.
  1410. * If set to 1, then the user specified SSL_CTX passed in using
  1411. * MOSQ_OPT_SSL_CTX will have the default options applied to it.
  1412. * This means that you only need to change the values that are
  1413. * relevant to you. If you use this option then you must configure
  1414. * the TLS options as normal, i.e. you should use
  1415. * <mosquitto_tls_set> to configure the cafile/capath as a minimum.
  1416. * This option is only available for openssl 1.1.0 and higher.
  1417. */
  1418. libmosq_EXPORT int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t option, void *value);
  1419. /*
  1420. * Function: mosquitto_int_option
  1421. *
  1422. * Used to set integer options for the client.
  1423. *
  1424. * Parameters:
  1425. * mosq - a valid mosquitto instance.
  1426. * option - the option to set.
  1427. * value - the option specific value.
  1428. *
  1429. * Options:
  1430. * MOSQ_OPT_TCP_NODELAY - Set to 1 to disable Nagle's algorithm on client
  1431. * sockets. This has the effect of reducing latency of individual
  1432. * messages at the potential cost of increasing the number of
  1433. * packets being sent.
  1434. * Defaults to 0, which means Nagle remains enabled.
  1435. *
  1436. * MOSQ_OPT_PROTOCOL_VERSION - Value must be set to either MQTT_PROTOCOL_V31,
  1437. * MQTT_PROTOCOL_V311, or MQTT_PROTOCOL_V5. Must be set before the
  1438. * client connects. Defaults to MQTT_PROTOCOL_V311.
  1439. *
  1440. * MOSQ_OPT_RECEIVE_MAXIMUM - Value can be set between 1 and 65535 inclusive,
  1441. * and represents the maximum number of incoming QoS 1 and QoS 2
  1442. * messages that this client wants to process at once. Defaults to
  1443. * 20. This option is not valid for MQTT v3.1 or v3.1.1 clients.
  1444. * Note that if the MQTT_PROP_RECEIVE_MAXIMUM property is in the
  1445. * proplist passed to mosquitto_connect_v5(), then that property
  1446. * will override this option. Using this option is the recommended
  1447. * method however.
  1448. *
  1449. * MOSQ_OPT_SEND_MAXIMUM - Value can be set between 1 and 65535 inclusive,
  1450. * and represents the maximum number of outgoing QoS 1 and QoS 2
  1451. * messages that this client will attempt to have "in flight" at
  1452. * once. Defaults to 20.
  1453. * This option is not valid for MQTT v3.1 or v3.1.1 clients.
  1454. * Note that if the broker being connected to sends a
  1455. * MQTT_PROP_RECEIVE_MAXIMUM property that has a lower value than
  1456. * this option, then the broker provided value will be used.
  1457. *
  1458. * MOSQ_OPT_SSL_CTX_WITH_DEFAULTS - If value is set to a non zero value,
  1459. * then the user specified SSL_CTX passed in using MOSQ_OPT_SSL_CTX
  1460. * will have the default options applied to it. This means that
  1461. * you only need to change the values that are relevant to you.
  1462. * If you use this option then you must configure the TLS options
  1463. * as normal, i.e. you should use <mosquitto_tls_set> to
  1464. * configure the cafile/capath as a minimum.
  1465. * This option is only available for openssl 1.1.0 and higher.
  1466. *
  1467. * MOSQ_OPT_TLS_OCSP_REQUIRED - Set whether OCSP checking on TLS
  1468. * connections is required. Set to 1 to enable checking,
  1469. * or 0 (the default) for no checking.
  1470. *
  1471. * MOSQ_OPT_TLS_USE_OS_CERTS - Set to 1 to instruct the client to load and
  1472. * trust OS provided CA certificates for use with TLS connections.
  1473. * Set to 0 (the default) to only use manually specified CA certs.
  1474. */
  1475. libmosq_EXPORT int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int value);
  1476. /*
  1477. * Function: mosquitto_string_option
  1478. *
  1479. * Used to set const char* options for the client.
  1480. *
  1481. * Parameters:
  1482. * mosq - a valid mosquitto instance.
  1483. * option - the option to set.
  1484. * value - the option specific value.
  1485. *
  1486. * Options:
  1487. * MOSQ_OPT_TLS_ENGINE - Configure the client for TLS Engine support.
  1488. * Pass a TLS Engine ID to be used when creating TLS
  1489. * connections. Must be set before <mosquitto_connect>.
  1490. *
  1491. * MOSQ_OPT_TLS_KEYFORM - Configure the client to treat the keyfile
  1492. * differently depending on its type. Must be set
  1493. * before <mosquitto_connect>.
  1494. * Set as either "pem" or "engine", to determine from where the
  1495. * private key for a TLS connection will be obtained. Defaults to
  1496. * "pem", a normal private key file.
  1497. *
  1498. * MOSQ_OPT_TLS_KPASS_SHA1 - Where the TLS Engine requires the use of
  1499. * a password to be accessed, this option allows a hex encoded
  1500. * SHA1 hash of the private key password to be passed to the
  1501. * engine directly. Must be set before <mosquitto_connect>.
  1502. *
  1503. * MOSQ_OPT_TLS_ALPN - If the broker being connected to has multiple
  1504. * services available on a single TLS port, such as both MQTT
  1505. * and WebSockets, use this option to configure the ALPN
  1506. * option for the connection.
  1507. *
  1508. * MOSQ_OPT_BIND_ADDRESS - Set the hostname or ip address of the local network
  1509. * interface to bind to when connecting.
  1510. */
  1511. libmosq_EXPORT int mosquitto_string_option(struct mosquitto *mosq, enum mosq_opt_t option, const char *value);
  1512. /*
  1513. * Function: mosquitto_void_option
  1514. *
  1515. * Used to set void* options for the client.
  1516. *
  1517. * Parameters:
  1518. * mosq - a valid mosquitto instance.
  1519. * option - the option to set.
  1520. * value - the option specific value.
  1521. *
  1522. * Options:
  1523. * MOSQ_OPT_SSL_CTX - Pass an openssl SSL_CTX to be used when creating TLS
  1524. * connections rather than libmosquitto creating its own. This must
  1525. * be called before connecting to have any effect. If you use this
  1526. * option, the onus is on you to ensure that you are using secure
  1527. * settings.
  1528. * Setting to NULL means that libmosquitto will use its own SSL_CTX
  1529. * if TLS is to be used.
  1530. * This option is only available for openssl 1.1.0 and higher.
  1531. */
  1532. libmosq_EXPORT int mosquitto_void_option(struct mosquitto *mosq, enum mosq_opt_t option, void *value);
  1533. /*
  1534. * Function: mosquitto_reconnect_delay_set
  1535. *
  1536. * Control the behaviour of the client when it has unexpectedly disconnected in
  1537. * <mosquitto_loop_forever> or after <mosquitto_loop_start>. The default
  1538. * behaviour if this function is not used is to repeatedly attempt to reconnect
  1539. * with a delay of 1 second until the connection succeeds.
  1540. *
  1541. * Use reconnect_delay parameter to change the delay between successive
  1542. * reconnection attempts. You may also enable exponential backoff of the time
  1543. * between reconnections by setting reconnect_exponential_backoff to true and
  1544. * set an upper bound on the delay with reconnect_delay_max.
  1545. *
  1546. * Example 1:
  1547. * delay=2, delay_max=10, exponential_backoff=False
  1548. * Delays would be: 2, 4, 6, 8, 10, 10, ...
  1549. *
  1550. * Example 2:
  1551. * delay=3, delay_max=30, exponential_backoff=True
  1552. * Delays would be: 3, 6, 12, 24, 30, 30, ...
  1553. *
  1554. * Parameters:
  1555. * mosq - a valid mosquitto instance.
  1556. * reconnect_delay - the number of seconds to wait between
  1557. * reconnects.
  1558. * reconnect_delay_max - the maximum number of seconds to wait
  1559. * between reconnects.
  1560. * reconnect_exponential_backoff - use exponential backoff between
  1561. * reconnect attempts. Set to true to enable
  1562. * exponential backoff.
  1563. *
  1564. * Returns:
  1565. * MOSQ_ERR_SUCCESS - on success.
  1566. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1567. */
  1568. libmosq_EXPORT int mosquitto_reconnect_delay_set(struct mosquitto *mosq, unsigned int reconnect_delay, unsigned int reconnect_delay_max, bool reconnect_exponential_backoff);
  1569. /*
  1570. * Function: mosquitto_max_inflight_messages_set
  1571. *
  1572. * This function is deprected. Use the <mosquitto_int_option> function with the
  1573. * MOSQ_OPT_SEND_MAXIMUM option instead.
  1574. *
  1575. * Set the number of QoS 1 and 2 messages that can be "in flight" at one time.
  1576. * An in flight message is part way through its delivery flow. Attempts to send
  1577. * further messages with <mosquitto_publish> will result in the messages being
  1578. * queued until the number of in flight messages reduces.
  1579. *
  1580. * A higher number here results in greater message throughput, but if set
  1581. * higher than the maximum in flight messages on the broker may lead to
  1582. * delays in the messages being acknowledged.
  1583. *
  1584. * Set to 0 for no maximum.
  1585. *
  1586. * Parameters:
  1587. * mosq - a valid mosquitto instance.
  1588. * max_inflight_messages - the maximum number of inflight messages. Defaults
  1589. * to 20.
  1590. *
  1591. * Returns:
  1592. * MOSQ_ERR_SUCCESS - on success.
  1593. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1594. */
  1595. libmosq_EXPORT int mosquitto_max_inflight_messages_set(struct mosquitto *mosq, unsigned int max_inflight_messages);
  1596. /*
  1597. * Function: mosquitto_message_retry_set
  1598. *
  1599. * This function now has no effect.
  1600. */
  1601. libmosq_EXPORT void mosquitto_message_retry_set(struct mosquitto *mosq, unsigned int message_retry);
  1602. /*
  1603. * Function: mosquitto_user_data_set
  1604. *
  1605. * When <mosquitto_new> is called, the pointer given as the "obj" parameter
  1606. * will be passed to the callbacks as user data. The <mosquitto_user_data_set>
  1607. * function allows this obj parameter to be updated at any time. This function
  1608. * will not modify the memory pointed to by the current user data pointer. If
  1609. * it is dynamically allocated memory you must free it yourself.
  1610. *
  1611. * Parameters:
  1612. * mosq - a valid mosquitto instance.
  1613. * obj - A user pointer that will be passed as an argument to any callbacks
  1614. * that are specified.
  1615. */
  1616. libmosq_EXPORT void mosquitto_user_data_set(struct mosquitto *mosq, void *obj);
  1617. /* Function: mosquitto_userdata
  1618. *
  1619. * Retrieve the "userdata" variable for a mosquitto client.
  1620. *
  1621. * Parameters:
  1622. * mosq - a valid mosquitto instance.
  1623. *
  1624. * Returns:
  1625. * A pointer to the userdata member variable.
  1626. */
  1627. libmosq_EXPORT void *mosquitto_userdata(struct mosquitto *mosq);
  1628. /* ======================================================================
  1629. *
  1630. * Section: TLS support
  1631. *
  1632. * ====================================================================== */
  1633. /*
  1634. * Function: mosquitto_tls_set
  1635. *
  1636. * Configure the client for certificate based SSL/TLS support. Must be called
  1637. * before <mosquitto_connect>.
  1638. *
  1639. * Cannot be used in conjunction with <mosquitto_tls_psk_set>.
  1640. *
  1641. * Define the Certificate Authority certificates to be trusted (ie. the server
  1642. * certificate must be signed with one of these certificates) using cafile.
  1643. *
  1644. * If the server you are connecting to requires clients to provide a
  1645. * certificate, define certfile and keyfile with your client certificate and
  1646. * private key. If your private key is encrypted, provide a password callback
  1647. * function or you will have to enter the password at the command line.
  1648. *
  1649. * Parameters:
  1650. * mosq - a valid mosquitto instance.
  1651. * cafile - path to a file containing the PEM encoded trusted CA
  1652. * certificate files. Either cafile or capath must not be NULL.
  1653. * capath - path to a directory containing the PEM encoded trusted CA
  1654. * certificate files. See mosquitto.conf for more details on
  1655. * configuring this directory. Either cafile or capath must not
  1656. * be NULL.
  1657. * certfile - path to a file containing the PEM encoded certificate file
  1658. * for this client. If NULL, keyfile must also be NULL and no
  1659. * client certificate will be used.
  1660. * keyfile - path to a file containing the PEM encoded private key for
  1661. * this client. If NULL, certfile must also be NULL and no
  1662. * client certificate will be used.
  1663. * pw_callback - if keyfile is encrypted, set pw_callback to allow your client
  1664. * to pass the correct password for decryption. If set to NULL,
  1665. * the password must be entered on the command line.
  1666. * Your callback must write the password into "buf", which is
  1667. * "size" bytes long. The return value must be the length of the
  1668. * password. "userdata" will be set to the calling mosquitto
  1669. * instance. The mosquitto userdata member variable can be
  1670. * retrieved using <mosquitto_userdata>.
  1671. *
  1672. * Returns:
  1673. * MOSQ_ERR_SUCCESS - on success.
  1674. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1675. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1676. *
  1677. * See Also:
  1678. * <mosquitto_tls_opts_set>, <mosquitto_tls_psk_set>,
  1679. * <mosquitto_tls_insecure_set>, <mosquitto_userdata>
  1680. */
  1681. libmosq_EXPORT int mosquitto_tls_set(struct mosquitto *mosq,
  1682. const char *cafile, const char *capath,
  1683. const char *certfile, const char *keyfile,
  1684. int (*pw_callback)(char *buf, int size, int rwflag, void *userdata));
  1685. /*
  1686. * Function: mosquitto_tls_insecure_set
  1687. *
  1688. * Configure verification of the server hostname in the server certificate. If
  1689. * value is set to true, it is impossible to guarantee that the host you are
  1690. * connecting to is not impersonating your server. This can be useful in
  1691. * initial server testing, but makes it possible for a malicious third party to
  1692. * impersonate your server through DNS spoofing, for example.
  1693. * Do not use this function in a real system. Setting value to true makes the
  1694. * connection encryption pointless.
  1695. * Must be called before <mosquitto_connect>.
  1696. *
  1697. * Parameters:
  1698. * mosq - a valid mosquitto instance.
  1699. * value - if set to false, the default, certificate hostname checking is
  1700. * performed. If set to true, no hostname checking is performed and
  1701. * the connection is insecure.
  1702. *
  1703. * Returns:
  1704. * MOSQ_ERR_SUCCESS - on success.
  1705. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1706. *
  1707. * See Also:
  1708. * <mosquitto_tls_set>
  1709. */
  1710. libmosq_EXPORT int mosquitto_tls_insecure_set(struct mosquitto *mosq, bool value);
  1711. /*
  1712. * Function: mosquitto_tls_opts_set
  1713. *
  1714. * Set advanced SSL/TLS options. Must be called before <mosquitto_connect>.
  1715. *
  1716. * Parameters:
  1717. * mosq - a valid mosquitto instance.
  1718. * cert_reqs - an integer defining the verification requirements the client
  1719. * will impose on the server. This can be one of:
  1720. * * SSL_VERIFY_NONE (0): the server will not be verified in any way.
  1721. * * SSL_VERIFY_PEER (1): the server certificate will be verified
  1722. * and the connection aborted if the verification fails.
  1723. * The default and recommended value is SSL_VERIFY_PEER. Using
  1724. * SSL_VERIFY_NONE provides no security.
  1725. * tls_version - the version of the SSL/TLS protocol to use as a string. If NULL,
  1726. * the default value is used. The default value and the
  1727. * available values depend on the version of openssl that the
  1728. * library was compiled against. For openssl >= 1.0.1, the
  1729. * available options are tlsv1.2, tlsv1.1 and tlsv1, with tlv1.2
  1730. * as the default. For openssl < 1.0.1, only tlsv1 is available.
  1731. * ciphers - a string describing the ciphers available for use. See the
  1732. * "openssl ciphers" tool for more information. If NULL, the
  1733. * default ciphers will be used.
  1734. *
  1735. * Returns:
  1736. * MOSQ_ERR_SUCCESS - on success.
  1737. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1738. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1739. *
  1740. * See Also:
  1741. * <mosquitto_tls_set>
  1742. */
  1743. libmosq_EXPORT int mosquitto_tls_opts_set(struct mosquitto *mosq, int cert_reqs, const char *tls_version, const char *ciphers);
  1744. /*
  1745. * Function: mosquitto_tls_psk_set
  1746. *
  1747. * Configure the client for pre-shared-key based TLS support. Must be called
  1748. * before <mosquitto_connect>.
  1749. *
  1750. * Cannot be used in conjunction with <mosquitto_tls_set>.
  1751. *
  1752. * Parameters:
  1753. * mosq - a valid mosquitto instance.
  1754. * psk - the pre-shared-key in hex format with no leading "0x".
  1755. * identity - the identity of this client. May be used as the username
  1756. * depending on the server settings.
  1757. * ciphers - a string describing the PSK ciphers available for use. See the
  1758. * "openssl ciphers" tool for more information. If NULL, the
  1759. * default ciphers will be used.
  1760. *
  1761. * Returns:
  1762. * MOSQ_ERR_SUCCESS - on success.
  1763. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  1764. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  1765. *
  1766. * See Also:
  1767. * <mosquitto_tls_set>
  1768. */
  1769. libmosq_EXPORT int mosquitto_tls_psk_set(struct mosquitto *mosq, const char *psk, const char *identity, const char *ciphers);
  1770. /*
  1771. * Function: mosquitto_ssl_get
  1772. *
  1773. * Retrieve a pointer to the SSL structure used for TLS connections in this
  1774. * client. This can be used in e.g. the connect callback to carry out
  1775. * additional verification steps.
  1776. *
  1777. * Parameters:
  1778. * mosq - a valid mosquitto instance
  1779. *
  1780. * Returns:
  1781. * A valid pointer to an openssl SSL structure - if the client is using TLS.
  1782. * NULL - if the client is not using TLS, or TLS support is not compiled in.
  1783. */
  1784. libmosq_EXPORT void *mosquitto_ssl_get(struct mosquitto *mosq);
  1785. /* ======================================================================
  1786. *
  1787. * Section: Callbacks
  1788. *
  1789. * ====================================================================== */
  1790. /*
  1791. * Function: mosquitto_connect_callback_set
  1792. *
  1793. * Set the connect callback. This is called when the library receives a CONNACK
  1794. * message in response to a connection.
  1795. *
  1796. * Parameters:
  1797. * mosq - a valid mosquitto instance.
  1798. * on_connect - a callback function in the following form:
  1799. * void callback(struct mosquitto *mosq, void *obj, int rc)
  1800. *
  1801. * Callback Parameters:
  1802. * mosq - the mosquitto instance making the callback.
  1803. * obj - the user data provided in <mosquitto_new>
  1804. * rc - the return code of the connection response. The values are defined by
  1805. * the MQTT protocol version in use.
  1806. * For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
  1807. * For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
  1808. */
  1809. libmosq_EXPORT void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int));
  1810. /*
  1811. * Function: mosquitto_connect_with_flags_callback_set
  1812. *
  1813. * Set the connect callback. This is called when the library receives a CONNACK
  1814. * message in response to a connection.
  1815. *
  1816. * Parameters:
  1817. * mosq - a valid mosquitto instance.
  1818. * on_connect - a callback function in the following form:
  1819. * void callback(struct mosquitto *mosq, void *obj, int rc)
  1820. *
  1821. * Callback Parameters:
  1822. * mosq - the mosquitto instance making the callback.
  1823. * obj - the user data provided in <mosquitto_new>
  1824. * rc - the return code of the connection response. The values are defined by
  1825. * the MQTT protocol version in use.
  1826. * For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
  1827. * For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
  1828. * flags - the connect flags.
  1829. */
  1830. libmosq_EXPORT void mosquitto_connect_with_flags_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int));
  1831. /*
  1832. * Function: mosquitto_connect_v5_callback_set
  1833. *
  1834. * Set the connect callback. This is called when the library receives a CONNACK
  1835. * message in response to a connection.
  1836. *
  1837. * It is valid to set this callback for all MQTT protocol versions. If it is
  1838. * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props`
  1839. * argument will always be NULL.
  1840. *
  1841. * Parameters:
  1842. * mosq - a valid mosquitto instance.
  1843. * on_connect - a callback function in the following form:
  1844. * void callback(struct mosquitto *mosq, void *obj, int rc)
  1845. *
  1846. * Callback Parameters:
  1847. * mosq - the mosquitto instance making the callback.
  1848. * obj - the user data provided in <mosquitto_new>
  1849. * rc - the return code of the connection response. The values are defined by
  1850. * the MQTT protocol version in use.
  1851. * For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html
  1852. * For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html
  1853. * flags - the connect flags.
  1854. * props - list of MQTT 5 properties, or NULL
  1855. *
  1856. */
  1857. libmosq_EXPORT void mosquitto_connect_v5_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int, const mosquitto_property *props));
  1858. /*
  1859. * Function: mosquitto_disconnect_callback_set
  1860. *
  1861. * Set the disconnect callback. This is called when the broker has received the
  1862. * DISCONNECT command and has disconnected the client.
  1863. *
  1864. * Parameters:
  1865. * mosq - a valid mosquitto instance.
  1866. * on_disconnect - a callback function in the following form:
  1867. * void callback(struct mosquitto *mosq, void *obj)
  1868. *
  1869. * Callback Parameters:
  1870. * mosq - the mosquitto instance making the callback.
  1871. * obj - the user data provided in <mosquitto_new>
  1872. * rc - integer value indicating the reason for the disconnect. A value of 0
  1873. * means the client has called <mosquitto_disconnect>. Any other value
  1874. * indicates that the disconnect is unexpected.
  1875. */
  1876. libmosq_EXPORT void mosquitto_disconnect_callback_set(struct mosquitto *mosq, void (*on_disconnect)(struct mosquitto *, void *, int));
  1877. /*
  1878. * Function: mosquitto_disconnect_v5_callback_set
  1879. *
  1880. * Set the disconnect callback. This is called when the broker has received the
  1881. * DISCONNECT command and has disconnected the client.
  1882. *
  1883. * It is valid to set this callback for all MQTT protocol versions. If it is
  1884. * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props`
  1885. * argument will always be NULL.
  1886. *
  1887. * Parameters:
  1888. * mosq - a valid mosquitto instance.
  1889. * on_disconnect - a callback function in the following form:
  1890. * void callback(struct mosquitto *mosq, void *obj)
  1891. *
  1892. * Callback Parameters:
  1893. * mosq - the mosquitto instance making the callback.
  1894. * obj - the user data provided in <mosquitto_new>
  1895. * rc - integer value indicating the reason for the disconnect. A value of 0
  1896. * means the client has called <mosquitto_disconnect>. Any other value
  1897. * indicates that the disconnect is unexpected.
  1898. * props - list of MQTT 5 properties, or NULL
  1899. */
  1900. libmosq_EXPORT void mosquitto_disconnect_v5_callback_set(struct mosquitto *mosq, void (*on_disconnect)(struct mosquitto *, void *, int, const mosquitto_property *props));
  1901. /*
  1902. * Function: mosquitto_publish_callback_set
  1903. *
  1904. * Set the publish callback. This is called when a message initiated with
  1905. * <mosquitto_publish> has been sent to the broker. "Sent" means different
  1906. * things depending on the QoS of the message:
  1907. *
  1908. * QoS 0: The PUBLISH was passed to the local operating system for delivery,
  1909. * there is no guarantee that it was delivered to the remote broker.
  1910. * QoS 1: The PUBLISH was sent to the remote broker and the corresponding
  1911. * PUBACK was received by the library.
  1912. * QoS 2: The PUBLISH was sent to the remote broker and the corresponding
  1913. * PUBCOMP was received by the library.
  1914. *
  1915. * Parameters:
  1916. * mosq - a valid mosquitto instance.
  1917. * on_publish - a callback function in the following form:
  1918. * void callback(struct mosquitto *mosq, void *obj, int mid)
  1919. *
  1920. * Callback Parameters:
  1921. * mosq - the mosquitto instance making the callback.
  1922. * obj - the user data provided in <mosquitto_new>
  1923. * mid - the message id of the sent message.
  1924. */
  1925. libmosq_EXPORT void mosquitto_publish_callback_set(struct mosquitto *mosq, void (*on_publish)(struct mosquitto *, void *, int));
  1926. /*
  1927. * Function: mosquitto_publish_v5_callback_set
  1928. *
  1929. * Set the publish callback. This is called when a message initiated with
  1930. * <mosquitto_publish> has been sent to the broker. This callback will be
  1931. * called both if the message is sent successfully, or if the broker responded
  1932. * with an error, which will be reflected in the reason_code parameter.
  1933. * "Sent" means different things depending on the QoS of the message:
  1934. *
  1935. * QoS 0: The PUBLISH was passed to the local operating system for delivery,
  1936. * there is no guarantee that it was delivered to the remote broker.
  1937. * QoS 1: The PUBLISH was sent to the remote broker and the corresponding
  1938. * PUBACK was received by the library.
  1939. * QoS 2: The PUBLISH was sent to the remote broker and the corresponding
  1940. * PUBCOMP was received by the library.
  1941. *
  1942. *
  1943. * It is valid to set this callback for all MQTT protocol versions. If it is
  1944. * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props`
  1945. * argument will always be NULL.
  1946. *
  1947. * Parameters:
  1948. * mosq - a valid mosquitto instance.
  1949. * on_publish - a callback function in the following form:
  1950. * void callback(struct mosquitto *mosq, void *obj, int mid)
  1951. *
  1952. * Callback Parameters:
  1953. * mosq - the mosquitto instance making the callback.
  1954. * obj - the user data provided in <mosquitto_new>
  1955. * mid - the message id of the sent message.
  1956. * reason_code - the MQTT 5 reason code
  1957. * props - list of MQTT 5 properties, or NULL
  1958. */
  1959. libmosq_EXPORT void mosquitto_publish_v5_callback_set(struct mosquitto *mosq, void (*on_publish)(struct mosquitto *, void *, int, int, const mosquitto_property *props));
  1960. /*
  1961. * Function: mosquitto_message_callback_set
  1962. *
  1963. * Set the message callback. This is called when a message is received from the
  1964. * broker and the required QoS flow has completed.
  1965. *
  1966. * Parameters:
  1967. * mosq - a valid mosquitto instance.
  1968. * on_message - a callback function in the following form:
  1969. * void callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
  1970. *
  1971. * Callback Parameters:
  1972. * mosq - the mosquitto instance making the callback.
  1973. * obj - the user data provided in <mosquitto_new>
  1974. * message - the message data. This variable and associated memory will be
  1975. * freed by the library after the callback completes. The client
  1976. * should make copies of any of the data it requires.
  1977. *
  1978. * See Also:
  1979. * <mosquitto_message_copy>
  1980. */
  1981. libmosq_EXPORT void mosquitto_message_callback_set(struct mosquitto *mosq, void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *));
  1982. /*
  1983. * Function: mosquitto_message_v5_callback_set
  1984. *
  1985. * Set the message callback. This is called when a message is received from the
  1986. * broker and the required QoS flow has completed.
  1987. *
  1988. * It is valid to set this callback for all MQTT protocol versions. If it is
  1989. * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props`
  1990. * argument will always be NULL.
  1991. *
  1992. * Parameters:
  1993. * mosq - a valid mosquitto instance.
  1994. * on_message - a callback function in the following form:
  1995. * void callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
  1996. *
  1997. * Callback Parameters:
  1998. * mosq - the mosquitto instance making the callback.
  1999. * obj - the user data provided in <mosquitto_new>
  2000. * message - the message data. This variable and associated memory will be
  2001. * freed by the library after the callback completes. The client
  2002. * should make copies of any of the data it requires.
  2003. * props - list of MQTT 5 properties, or NULL
  2004. *
  2005. * See Also:
  2006. * <mosquitto_message_copy>
  2007. */
  2008. libmosq_EXPORT void mosquitto_message_v5_callback_set(struct mosquitto *mosq, void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *, const mosquitto_property *props));
  2009. /*
  2010. * Function: mosquitto_subscribe_callback_set
  2011. *
  2012. * Set the subscribe callback. This is called when the library receives a
  2013. * SUBACK message in response to a SUBSCRIBE.
  2014. *
  2015. * Parameters:
  2016. * mosq - a valid mosquitto instance.
  2017. * on_subscribe - a callback function in the following form:
  2018. * void callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
  2019. *
  2020. * Callback Parameters:
  2021. * mosq - the mosquitto instance making the callback.
  2022. * obj - the user data provided in <mosquitto_new>
  2023. * mid - the message id of the subscribe message.
  2024. * qos_count - the number of granted subscriptions (size of granted_qos).
  2025. * granted_qos - an array of integers indicating the granted QoS for each of
  2026. * the subscriptions.
  2027. */
  2028. libmosq_EXPORT void mosquitto_subscribe_callback_set(struct mosquitto *mosq, void (*on_subscribe)(struct mosquitto *, void *, int, int, const int *));
  2029. /*
  2030. * Function: mosquitto_subscribe_v5_callback_set
  2031. *
  2032. * Set the subscribe callback. This is called when the library receives a
  2033. * SUBACK message in response to a SUBSCRIBE.
  2034. *
  2035. * It is valid to set this callback for all MQTT protocol versions. If it is
  2036. * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props`
  2037. * argument will always be NULL.
  2038. *
  2039. * Parameters:
  2040. * mosq - a valid mosquitto instance.
  2041. * on_subscribe - a callback function in the following form:
  2042. * void callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
  2043. *
  2044. * Callback Parameters:
  2045. * mosq - the mosquitto instance making the callback.
  2046. * obj - the user data provided in <mosquitto_new>
  2047. * mid - the message id of the subscribe message.
  2048. * qos_count - the number of granted subscriptions (size of granted_qos).
  2049. * granted_qos - an array of integers indicating the granted QoS for each of
  2050. * the subscriptions.
  2051. * props - list of MQTT 5 properties, or NULL
  2052. */
  2053. libmosq_EXPORT void mosquitto_subscribe_v5_callback_set(struct mosquitto *mosq, void (*on_subscribe)(struct mosquitto *, void *, int, int, const int *, const mosquitto_property *props));
  2054. /*
  2055. * Function: mosquitto_unsubscribe_callback_set
  2056. *
  2057. * Set the unsubscribe callback. This is called when the library receives a
  2058. * UNSUBACK message in response to an UNSUBSCRIBE.
  2059. *
  2060. * Parameters:
  2061. * mosq - a valid mosquitto instance.
  2062. * on_unsubscribe - a callback function in the following form:
  2063. * void callback(struct mosquitto *mosq, void *obj, int mid)
  2064. *
  2065. * Callback Parameters:
  2066. * mosq - the mosquitto instance making the callback.
  2067. * obj - the user data provided in <mosquitto_new>
  2068. * mid - the message id of the unsubscribe message.
  2069. */
  2070. libmosq_EXPORT void mosquitto_unsubscribe_callback_set(struct mosquitto *mosq, void (*on_unsubscribe)(struct mosquitto *, void *, int));
  2071. /*
  2072. * Function: mosquitto_unsubscribe_v5_callback_set
  2073. *
  2074. * Set the unsubscribe callback. This is called when the library receives a
  2075. * UNSUBACK message in response to an UNSUBSCRIBE.
  2076. *
  2077. * It is valid to set this callback for all MQTT protocol versions. If it is
  2078. * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props`
  2079. * argument will always be NULL.
  2080. *
  2081. * Parameters:
  2082. * mosq - a valid mosquitto instance.
  2083. * on_unsubscribe - a callback function in the following form:
  2084. * void callback(struct mosquitto *mosq, void *obj, int mid)
  2085. *
  2086. * Callback Parameters:
  2087. * mosq - the mosquitto instance making the callback.
  2088. * obj - the user data provided in <mosquitto_new>
  2089. * mid - the message id of the unsubscribe message.
  2090. * props - list of MQTT 5 properties, or NULL
  2091. */
  2092. libmosq_EXPORT void mosquitto_unsubscribe_v5_callback_set(struct mosquitto *mosq, void (*on_unsubscribe)(struct mosquitto *, void *, int, const mosquitto_property *props));
  2093. /*
  2094. * Function: mosquitto_log_callback_set
  2095. *
  2096. * Set the logging callback. This should be used if you want event logging
  2097. * information from the client library.
  2098. *
  2099. * mosq - a valid mosquitto instance.
  2100. * on_log - a callback function in the following form:
  2101. * void callback(struct mosquitto *mosq, void *obj, int level, const char *str)
  2102. *
  2103. * Callback Parameters:
  2104. * mosq - the mosquitto instance making the callback.
  2105. * obj - the user data provided in <mosquitto_new>
  2106. * level - the log message level from the values:
  2107. * MOSQ_LOG_INFO
  2108. * MOSQ_LOG_NOTICE
  2109. * MOSQ_LOG_WARNING
  2110. * MOSQ_LOG_ERR
  2111. * MOSQ_LOG_DEBUG
  2112. * str - the message string.
  2113. */
  2114. libmosq_EXPORT void mosquitto_log_callback_set(struct mosquitto *mosq, void (*on_log)(struct mosquitto *, void *, int, const char *));
  2115. /* =============================================================================
  2116. *
  2117. * Section: SOCKS5 proxy functions
  2118. *
  2119. * =============================================================================
  2120. */
  2121. /*
  2122. * Function: mosquitto_socks5_set
  2123. *
  2124. * Configure the client to use a SOCKS5 proxy when connecting. Must be called
  2125. * before connecting. "None" and "username/password" authentication is
  2126. * supported.
  2127. *
  2128. * Parameters:
  2129. * mosq - a valid mosquitto instance.
  2130. * host - the SOCKS5 proxy host to connect to.
  2131. * port - the SOCKS5 proxy port to use.
  2132. * username - if not NULL, use this username when authenticating with the proxy.
  2133. * password - if not NULL and username is not NULL, use this password when
  2134. * authenticating with the proxy.
  2135. */
  2136. libmosq_EXPORT int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, const char *username, const char *password);
  2137. /* =============================================================================
  2138. *
  2139. * Section: Utility functions
  2140. *
  2141. * =============================================================================
  2142. */
  2143. /*
  2144. * Function: mosquitto_strerror
  2145. *
  2146. * Call to obtain a const string description of a mosquitto error number.
  2147. *
  2148. * Parameters:
  2149. * mosq_errno - a mosquitto error number.
  2150. *
  2151. * Returns:
  2152. * A constant string describing the error.
  2153. */
  2154. libmosq_EXPORT const char *mosquitto_strerror(int mosq_errno);
  2155. /*
  2156. * Function: mosquitto_connack_string
  2157. *
  2158. * Call to obtain a const string description of an MQTT connection result.
  2159. *
  2160. * Parameters:
  2161. * connack_code - an MQTT connection result.
  2162. *
  2163. * Returns:
  2164. * A constant string describing the result.
  2165. */
  2166. libmosq_EXPORT const char *mosquitto_connack_string(int connack_code);
  2167. /*
  2168. * Function: mosquitto_reason_string
  2169. *
  2170. * Call to obtain a const string description of an MQTT reason code.
  2171. *
  2172. * Parameters:
  2173. * reason_code - an MQTT reason code.
  2174. *
  2175. * Returns:
  2176. * A constant string describing the reason.
  2177. */
  2178. libmosq_EXPORT const char *mosquitto_reason_string(int reason_code);
  2179. /* Function: mosquitto_string_to_command
  2180. *
  2181. * Take a string input representing an MQTT command and convert it to the
  2182. * libmosquitto integer representation.
  2183. *
  2184. * Parameters:
  2185. * str - the string to parse.
  2186. * cmd - pointer to an int, for the result.
  2187. *
  2188. * Returns:
  2189. * MOSQ_ERR_SUCCESS - on success
  2190. * MOSQ_ERR_INVAL - on an invalid input.
  2191. *
  2192. * Example:
  2193. * (start code)
  2194. * mosquitto_string_to_command("CONNECT", &cmd);
  2195. * // cmd == CMD_CONNECT
  2196. * (end)
  2197. */
  2198. libmosq_EXPORT int mosquitto_string_to_command(const char *str, int *cmd);
  2199. /*
  2200. * Function: mosquitto_sub_topic_tokenise
  2201. *
  2202. * Tokenise a topic or subscription string into an array of strings
  2203. * representing the topic hierarchy.
  2204. *
  2205. * For example:
  2206. *
  2207. * subtopic: "a/deep/topic/hierarchy"
  2208. *
  2209. * Would result in:
  2210. *
  2211. * topics[0] = "a"
  2212. * topics[1] = "deep"
  2213. * topics[2] = "topic"
  2214. * topics[3] = "hierarchy"
  2215. *
  2216. * and:
  2217. *
  2218. * subtopic: "/a/deep/topic/hierarchy/"
  2219. *
  2220. * Would result in:
  2221. *
  2222. * topics[0] = NULL
  2223. * topics[1] = "a"
  2224. * topics[2] = "deep"
  2225. * topics[3] = "topic"
  2226. * topics[4] = "hierarchy"
  2227. *
  2228. * Parameters:
  2229. * subtopic - the subscription/topic to tokenise
  2230. * topics - a pointer to store the array of strings
  2231. * count - an int pointer to store the number of items in the topics array.
  2232. *
  2233. * Returns:
  2234. * MOSQ_ERR_SUCCESS - on success
  2235. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  2236. * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8
  2237. *
  2238. * Example:
  2239. *
  2240. * > char **topics;
  2241. * > int topic_count;
  2242. * > int i;
  2243. * >
  2244. * > mosquitto_sub_topic_tokenise("$SYS/broker/uptime", &topics, &topic_count);
  2245. * >
  2246. * > for(i=0; i<token_count; i++){
  2247. * > printf("%d: %s\n", i, topics[i]);
  2248. * > }
  2249. *
  2250. * See Also:
  2251. * <mosquitto_sub_topic_tokens_free>
  2252. */
  2253. libmosq_EXPORT int mosquitto_sub_topic_tokenise(const char *subtopic, char ***topics, int *count);
  2254. /*
  2255. * Function: mosquitto_sub_topic_tokens_free
  2256. *
  2257. * Free memory that was allocated in <mosquitto_sub_topic_tokenise>.
  2258. *
  2259. * Parameters:
  2260. * topics - pointer to string array.
  2261. * count - count of items in string array.
  2262. *
  2263. * Returns:
  2264. * MOSQ_ERR_SUCCESS - on success
  2265. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  2266. *
  2267. * See Also:
  2268. * <mosquitto_sub_topic_tokenise>
  2269. */
  2270. libmosq_EXPORT int mosquitto_sub_topic_tokens_free(char ***topics, int count);
  2271. /*
  2272. * Function: mosquitto_topic_matches_sub
  2273. *
  2274. * Check whether a topic matches a subscription.
  2275. *
  2276. * For example:
  2277. *
  2278. * foo/bar would match the subscription foo/# or +/bar
  2279. * non/matching would not match the subscription non/+/+
  2280. *
  2281. * Parameters:
  2282. * sub - subscription string to check topic against.
  2283. * topic - topic to check.
  2284. * result - bool pointer to hold result. Will be set to true if the topic
  2285. * matches the subscription.
  2286. *
  2287. * Returns:
  2288. * MOSQ_ERR_SUCCESS - on success
  2289. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  2290. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  2291. */
  2292. libmosq_EXPORT int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result);
  2293. /*
  2294. * Function: mosquitto_topic_matches_sub2
  2295. *
  2296. * Check whether a topic matches a subscription.
  2297. *
  2298. * For example:
  2299. *
  2300. * foo/bar would match the subscription foo/# or +/bar
  2301. * non/matching would not match the subscription non/+/+
  2302. *
  2303. * Parameters:
  2304. * sub - subscription string to check topic against.
  2305. * sublen - length in bytes of sub string
  2306. * topic - topic to check.
  2307. * topiclen - length in bytes of topic string
  2308. * result - bool pointer to hold result. Will be set to true if the topic
  2309. * matches the subscription.
  2310. *
  2311. * Returns:
  2312. * MOSQ_ERR_SUCCESS - on success
  2313. * MOSQ_ERR_INVAL - if the input parameters were invalid.
  2314. * MOSQ_ERR_NOMEM - if an out of memory condition occurred.
  2315. */
  2316. libmosq_EXPORT int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *topic, size_t topiclen, bool *result);
  2317. /*
  2318. * Function: mosquitto_pub_topic_check
  2319. *
  2320. * Check whether a topic to be used for publishing is valid.
  2321. *
  2322. * This searches for + or # in a topic and checks its length.
  2323. *
  2324. * This check is already carried out in <mosquitto_publish> and
  2325. * <mosquitto_will_set>, there is no need to call it directly before them. It
  2326. * may be useful if you wish to check the validity of a topic in advance of
  2327. * making a connection for example.
  2328. *
  2329. * Parameters:
  2330. * topic - the topic to check
  2331. *
  2332. * Returns:
  2333. * MOSQ_ERR_SUCCESS - for a valid topic
  2334. * MOSQ_ERR_INVAL - if the topic contains a + or a #, or if it is too long.
  2335. * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8
  2336. *
  2337. * See Also:
  2338. * <mosquitto_sub_topic_check>
  2339. */
  2340. libmosq_EXPORT int mosquitto_pub_topic_check(const char *topic);
  2341. /*
  2342. * Function: mosquitto_pub_topic_check2
  2343. *
  2344. * Check whether a topic to be used for publishing is valid.
  2345. *
  2346. * This searches for + or # in a topic and checks its length.
  2347. *
  2348. * This check is already carried out in <mosquitto_publish> and
  2349. * <mosquitto_will_set>, there is no need to call it directly before them. It
  2350. * may be useful if you wish to check the validity of a topic in advance of
  2351. * making a connection for example.
  2352. *
  2353. * Parameters:
  2354. * topic - the topic to check
  2355. * topiclen - length of the topic in bytes
  2356. *
  2357. * Returns:
  2358. * MOSQ_ERR_SUCCESS - for a valid topic
  2359. * MOSQ_ERR_INVAL - if the topic contains a + or a #, or if it is too long.
  2360. * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8
  2361. *
  2362. * See Also:
  2363. * <mosquitto_sub_topic_check>
  2364. */
  2365. libmosq_EXPORT int mosquitto_pub_topic_check2(const char *topic, size_t topiclen);
  2366. /*
  2367. * Function: mosquitto_sub_topic_check
  2368. *
  2369. * Check whether a topic to be used for subscribing is valid.
  2370. *
  2371. * This searches for + or # in a topic and checks that they aren't in invalid
  2372. * positions, such as with foo/#/bar, foo/+bar or foo/bar#, and checks its
  2373. * length.
  2374. *
  2375. * This check is already carried out in <mosquitto_subscribe> and
  2376. * <mosquitto_unsubscribe>, there is no need to call it directly before them.
  2377. * It may be useful if you wish to check the validity of a topic in advance of
  2378. * making a connection for example.
  2379. *
  2380. * Parameters:
  2381. * topic - the topic to check
  2382. *
  2383. * Returns:
  2384. * MOSQ_ERR_SUCCESS - for a valid topic
  2385. * MOSQ_ERR_INVAL - if the topic contains a + or a # that is in an
  2386. * invalid position, or if it is too long.
  2387. * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8
  2388. *
  2389. * See Also:
  2390. * <mosquitto_sub_topic_check>
  2391. */
  2392. libmosq_EXPORT int mosquitto_sub_topic_check(const char *topic);
  2393. /*
  2394. * Function: mosquitto_sub_topic_check2
  2395. *
  2396. * Check whether a topic to be used for subscribing is valid.
  2397. *
  2398. * This searches for + or # in a topic and checks that they aren't in invalid
  2399. * positions, such as with foo/#/bar, foo/+bar or foo/bar#, and checks its
  2400. * length.
  2401. *
  2402. * This check is already carried out in <mosquitto_subscribe> and
  2403. * <mosquitto_unsubscribe>, there is no need to call it directly before them.
  2404. * It may be useful if you wish to check the validity of a topic in advance of
  2405. * making a connection for example.
  2406. *
  2407. * Parameters:
  2408. * topic - the topic to check
  2409. * topiclen - the length in bytes of the topic
  2410. *
  2411. * Returns:
  2412. * MOSQ_ERR_SUCCESS - for a valid topic
  2413. * MOSQ_ERR_INVAL - if the topic contains a + or a # that is in an
  2414. * invalid position, or if it is too long.
  2415. * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8
  2416. *
  2417. * See Also:
  2418. * <mosquitto_sub_topic_check>
  2419. */
  2420. libmosq_EXPORT int mosquitto_sub_topic_check2(const char *topic, size_t topiclen);
  2421. /*
  2422. * Function: mosquitto_validate_utf8
  2423. *
  2424. * Helper function to validate whether a UTF-8 string is valid, according to
  2425. * the UTF-8 spec and the MQTT additions.
  2426. *
  2427. * Parameters:
  2428. * str - a string to check
  2429. * len - the length of the string in bytes
  2430. *
  2431. * Returns:
  2432. * MOSQ_ERR_SUCCESS - on success
  2433. * MOSQ_ERR_INVAL - if str is NULL or len<0 or len>65536
  2434. * MOSQ_ERR_MALFORMED_UTF8 - if str is not valid UTF-8
  2435. */
  2436. libmosq_EXPORT int mosquitto_validate_utf8(const char *str, int len);
  2437. /* =============================================================================
  2438. *
  2439. * Section: One line client helper functions
  2440. *
  2441. * =============================================================================
  2442. */
  2443. struct libmosquitto_will {
  2444. char *topic;
  2445. void *payload;
  2446. int payloadlen;
  2447. int qos;
  2448. bool retain;
  2449. };
  2450. struct libmosquitto_auth {
  2451. char *username;
  2452. char *password;
  2453. };
  2454. struct libmosquitto_tls {
  2455. char *cafile;
  2456. char *capath;
  2457. char *certfile;
  2458. char *keyfile;
  2459. char *ciphers;
  2460. char *tls_version;
  2461. int (*pw_callback)(char *buf, int size, int rwflag, void *userdata);
  2462. int cert_reqs;
  2463. };
  2464. /*
  2465. * Function: mosquitto_subscribe_simple
  2466. *
  2467. * Helper function to make subscribing to a topic and retrieving some messages
  2468. * very straightforward.
  2469. *
  2470. * This connects to a broker, subscribes to a topic, waits for msg_count
  2471. * messages to be received, then returns after disconnecting cleanly.
  2472. *
  2473. * Parameters:
  2474. * messages - pointer to a "struct mosquitto_message *". The received
  2475. * messages will be returned here. On error, this will be set to
  2476. * NULL.
  2477. * msg_count - the number of messages to retrieve.
  2478. * want_retained - if set to true, stale retained messages will be treated as
  2479. * normal messages with regards to msg_count. If set to
  2480. * false, they will be ignored.
  2481. * topic - the subscription topic to use (wildcards are allowed).
  2482. * qos - the qos to use for the subscription.
  2483. * host - the broker to connect to.
  2484. * port - the network port the broker is listening on.
  2485. * client_id - the client id to use, or NULL if a random client id should be
  2486. * generated.
  2487. * keepalive - the MQTT keepalive value.
  2488. * clean_session - the MQTT clean session flag.
  2489. * username - the username string, or NULL for no username authentication.
  2490. * password - the password string, or NULL for an empty password.
  2491. * will - a libmosquitto_will struct containing will information, or NULL for
  2492. * no will.
  2493. * tls - a libmosquitto_tls struct containing TLS related parameters, or NULL
  2494. * for no use of TLS.
  2495. *
  2496. *
  2497. * Returns:
  2498. * MOSQ_ERR_SUCCESS - on success
  2499. * Greater than 0 - on error.
  2500. */
  2501. libmosq_EXPORT int mosquitto_subscribe_simple(
  2502. struct mosquitto_message **messages,
  2503. int msg_count,
  2504. bool want_retained,
  2505. const char *topic,
  2506. int qos,
  2507. const char *host,
  2508. int port,
  2509. const char *client_id,
  2510. int keepalive,
  2511. bool clean_session,
  2512. const char *username,
  2513. const char *password,
  2514. const struct libmosquitto_will *will,
  2515. const struct libmosquitto_tls *tls);
  2516. /*
  2517. * Function: mosquitto_subscribe_callback
  2518. *
  2519. * Helper function to make subscribing to a topic and processing some messages
  2520. * very straightforward.
  2521. *
  2522. * This connects to a broker, subscribes to a topic, then passes received
  2523. * messages to a user provided callback. If the callback returns a 1, it then
  2524. * disconnects cleanly and returns.
  2525. *
  2526. * Parameters:
  2527. * callback - a callback function in the following form:
  2528. * int callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
  2529. * Note that this is the same as the normal on_message callback,
  2530. * except that it returns an int.
  2531. * userdata - user provided pointer that will be passed to the callback.
  2532. * topic - the subscription topic to use (wildcards are allowed).
  2533. * qos - the qos to use for the subscription.
  2534. * host - the broker to connect to.
  2535. * port - the network port the broker is listening on.
  2536. * client_id - the client id to use, or NULL if a random client id should be
  2537. * generated.
  2538. * keepalive - the MQTT keepalive value.
  2539. * clean_session - the MQTT clean session flag.
  2540. * username - the username string, or NULL for no username authentication.
  2541. * password - the password string, or NULL for an empty password.
  2542. * will - a libmosquitto_will struct containing will information, or NULL for
  2543. * no will.
  2544. * tls - a libmosquitto_tls struct containing TLS related parameters, or NULL
  2545. * for no use of TLS.
  2546. *
  2547. *
  2548. * Returns:
  2549. * MOSQ_ERR_SUCCESS - on success
  2550. * Greater than 0 - on error.
  2551. */
  2552. libmosq_EXPORT int mosquitto_subscribe_callback(
  2553. int (*callback)(struct mosquitto *, void *, const struct mosquitto_message *),
  2554. void *userdata,
  2555. const char *topic,
  2556. int qos,
  2557. const char *host,
  2558. int port,
  2559. const char *client_id,
  2560. int keepalive,
  2561. bool clean_session,
  2562. const char *username,
  2563. const char *password,
  2564. const struct libmosquitto_will *will,
  2565. const struct libmosquitto_tls *tls);
  2566. /* =============================================================================
  2567. *
  2568. * Section: Properties
  2569. *
  2570. * =============================================================================
  2571. */
  2572. /*
  2573. * Function: mosquitto_property_add_byte
  2574. *
  2575. * Add a new byte property to a property list.
  2576. *
  2577. * If *proplist == NULL, a new list will be created, otherwise the new property
  2578. * will be appended to the list.
  2579. *
  2580. * Parameters:
  2581. * proplist - pointer to mosquitto_property pointer, the list of properties
  2582. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2583. * value - integer value for the new property
  2584. *
  2585. * Returns:
  2586. * MOSQ_ERR_SUCCESS - on success
  2587. * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL
  2588. * MOSQ_ERR_NOMEM - on out of memory
  2589. *
  2590. * Example:
  2591. * > mosquitto_property *proplist = NULL;
  2592. * > mosquitto_property_add_byte(&proplist, MQTT_PROP_PAYLOAD_FORMAT_IDENTIFIER, 1);
  2593. */
  2594. libmosq_EXPORT int mosquitto_property_add_byte(mosquitto_property **proplist, int identifier, uint8_t value);
  2595. /*
  2596. * Function: mosquitto_property_add_int16
  2597. *
  2598. * Add a new int16 property to a property list.
  2599. *
  2600. * If *proplist == NULL, a new list will be created, otherwise the new property
  2601. * will be appended to the list.
  2602. *
  2603. * Parameters:
  2604. * proplist - pointer to mosquitto_property pointer, the list of properties
  2605. * identifier - property identifier (e.g. MQTT_PROP_RECEIVE_MAXIMUM)
  2606. * value - integer value for the new property
  2607. *
  2608. * Returns:
  2609. * MOSQ_ERR_SUCCESS - on success
  2610. * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL
  2611. * MOSQ_ERR_NOMEM - on out of memory
  2612. *
  2613. * Example:
  2614. * > mosquitto_property *proplist = NULL;
  2615. * > mosquitto_property_add_int16(&proplist, MQTT_PROP_RECEIVE_MAXIMUM, 1000);
  2616. */
  2617. libmosq_EXPORT int mosquitto_property_add_int16(mosquitto_property **proplist, int identifier, uint16_t value);
  2618. /*
  2619. * Function: mosquitto_property_add_int32
  2620. *
  2621. * Add a new int32 property to a property list.
  2622. *
  2623. * If *proplist == NULL, a new list will be created, otherwise the new property
  2624. * will be appended to the list.
  2625. *
  2626. * Parameters:
  2627. * proplist - pointer to mosquitto_property pointer, the list of properties
  2628. * identifier - property identifier (e.g. MQTT_PROP_MESSAGE_EXPIRY_INTERVAL)
  2629. * value - integer value for the new property
  2630. *
  2631. * Returns:
  2632. * MOSQ_ERR_SUCCESS - on success
  2633. * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL
  2634. * MOSQ_ERR_NOMEM - on out of memory
  2635. *
  2636. * Example:
  2637. * > mosquitto_property *proplist = NULL;
  2638. * > mosquitto_property_add_int32(&proplist, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, 86400);
  2639. */
  2640. libmosq_EXPORT int mosquitto_property_add_int32(mosquitto_property **proplist, int identifier, uint32_t value);
  2641. /*
  2642. * Function: mosquitto_property_add_varint
  2643. *
  2644. * Add a new varint property to a property list.
  2645. *
  2646. * If *proplist == NULL, a new list will be created, otherwise the new property
  2647. * will be appended to the list.
  2648. *
  2649. * Parameters:
  2650. * proplist - pointer to mosquitto_property pointer, the list of properties
  2651. * identifier - property identifier (e.g. MQTT_PROP_SUBSCRIPTION_IDENTIFIER)
  2652. * value - integer value for the new property
  2653. *
  2654. * Returns:
  2655. * MOSQ_ERR_SUCCESS - on success
  2656. * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL
  2657. * MOSQ_ERR_NOMEM - on out of memory
  2658. *
  2659. * Example:
  2660. * > mosquitto_property *proplist = NULL;
  2661. * > mosquitto_property_add_varint(&proplist, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, 1);
  2662. */
  2663. libmosq_EXPORT int mosquitto_property_add_varint(mosquitto_property **proplist, int identifier, uint32_t value);
  2664. /*
  2665. * Function: mosquitto_property_add_binary
  2666. *
  2667. * Add a new binary property to a property list.
  2668. *
  2669. * If *proplist == NULL, a new list will be created, otherwise the new property
  2670. * will be appended to the list.
  2671. *
  2672. * Parameters:
  2673. * proplist - pointer to mosquitto_property pointer, the list of properties
  2674. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2675. * value - pointer to the property data
  2676. * len - length of property data in bytes
  2677. *
  2678. * Returns:
  2679. * MOSQ_ERR_SUCCESS - on success
  2680. * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL
  2681. * MOSQ_ERR_NOMEM - on out of memory
  2682. *
  2683. * Example:
  2684. * > mosquitto_property *proplist = NULL;
  2685. * > mosquitto_property_add_binary(&proplist, MQTT_PROP_AUTHENTICATION_DATA, auth_data, auth_data_len);
  2686. */
  2687. libmosq_EXPORT int mosquitto_property_add_binary(mosquitto_property **proplist, int identifier, const void *value, uint16_t len);
  2688. /*
  2689. * Function: mosquitto_property_add_string
  2690. *
  2691. * Add a new string property to a property list.
  2692. *
  2693. * If *proplist == NULL, a new list will be created, otherwise the new property
  2694. * will be appended to the list.
  2695. *
  2696. * Parameters:
  2697. * proplist - pointer to mosquitto_property pointer, the list of properties
  2698. * identifier - property identifier (e.g. MQTT_PROP_CONTENT_TYPE)
  2699. * value - string value for the new property, must be UTF-8 and zero terminated
  2700. *
  2701. * Returns:
  2702. * MOSQ_ERR_SUCCESS - on success
  2703. * MOSQ_ERR_INVAL - if identifier is invalid, if value is NULL, or if proplist is NULL
  2704. * MOSQ_ERR_NOMEM - on out of memory
  2705. * MOSQ_ERR_MALFORMED_UTF8 - value is not valid UTF-8.
  2706. *
  2707. * Example:
  2708. * > mosquitto_property *proplist = NULL;
  2709. * > mosquitto_property_add_string(&proplist, MQTT_PROP_CONTENT_TYPE, "application/json");
  2710. */
  2711. libmosq_EXPORT int mosquitto_property_add_string(mosquitto_property **proplist, int identifier, const char *value);
  2712. /*
  2713. * Function: mosquitto_property_add_string_pair
  2714. *
  2715. * Add a new string pair property to a property list.
  2716. *
  2717. * If *proplist == NULL, a new list will be created, otherwise the new property
  2718. * will be appended to the list.
  2719. *
  2720. * Parameters:
  2721. * proplist - pointer to mosquitto_property pointer, the list of properties
  2722. * identifier - property identifier (e.g. MQTT_PROP_USER_PROPERTY)
  2723. * name - string name for the new property, must be UTF-8 and zero terminated
  2724. * value - string value for the new property, must be UTF-8 and zero terminated
  2725. *
  2726. * Returns:
  2727. * MOSQ_ERR_SUCCESS - on success
  2728. * MOSQ_ERR_INVAL - if identifier is invalid, if name or value is NULL, or if proplist is NULL
  2729. * MOSQ_ERR_NOMEM - on out of memory
  2730. * MOSQ_ERR_MALFORMED_UTF8 - if name or value are not valid UTF-8.
  2731. *
  2732. * Example:
  2733. * > mosquitto_property *proplist = NULL;
  2734. * > mosquitto_property_add_string_pair(&proplist, MQTT_PROP_USER_PROPERTY, "client", "mosquitto_pub");
  2735. */
  2736. libmosq_EXPORT int mosquitto_property_add_string_pair(mosquitto_property **proplist, int identifier, const char *name, const char *value);
  2737. /*
  2738. * Function: mosquitto_property_identifier
  2739. *
  2740. * Return the property identifier for a single property.
  2741. *
  2742. * Parameters:
  2743. * property - pointer to a valid mosquitto_property pointer.
  2744. *
  2745. * Returns:
  2746. * A valid property identifier on success
  2747. * 0 - on error
  2748. */
  2749. libmosq_EXPORT int mosquitto_property_identifier(const mosquitto_property *property);
  2750. /*
  2751. * Function: mosquitto_property_next
  2752. *
  2753. * Return the next property in a property list. Use to iterate over a property
  2754. * list, e.g.:
  2755. *
  2756. * (start code)
  2757. * for(prop = proplist; prop != NULL; prop = mosquitto_property_next(prop)){
  2758. * if(mosquitto_property_identifier(prop) == MQTT_PROP_CONTENT_TYPE){
  2759. * ...
  2760. * }
  2761. * }
  2762. * (end)
  2763. *
  2764. * Parameters:
  2765. * proplist - pointer to mosquitto_property pointer, the list of properties
  2766. *
  2767. * Returns:
  2768. * Pointer to the next item in the list
  2769. * NULL, if proplist is NULL, or if there are no more items in the list.
  2770. */
  2771. libmosq_EXPORT const mosquitto_property *mosquitto_property_next(const mosquitto_property *proplist);
  2772. /*
  2773. * Function: mosquitto_property_read_byte
  2774. *
  2775. * Attempt to read a byte property matching an identifier, from a property list
  2776. * or single property. This function can search for multiple entries of the
  2777. * same identifier by using the returned value and skip_first. Note however
  2778. * that it is forbidden for most properties to be duplicated.
  2779. *
  2780. * If the property is not found, *value will not be modified, so it is safe to
  2781. * pass a variable with a default value to be potentially overwritten:
  2782. *
  2783. * (start code)
  2784. * uint16_t keepalive = 60; // default value
  2785. * // Get value from property list, or keep default if not found.
  2786. * mosquitto_property_read_int16(proplist, MQTT_PROP_SERVER_KEEP_ALIVE, &keepalive, false);
  2787. * (end)
  2788. *
  2789. * Parameters:
  2790. * proplist - mosquitto_property pointer, the list of properties or single property
  2791. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2792. * value - pointer to store the value, or NULL if the value is not required.
  2793. * skip_first - boolean that indicates whether the first item in the list
  2794. * should be ignored or not. Should usually be set to false.
  2795. *
  2796. * Returns:
  2797. * A valid property pointer if the property is found
  2798. * NULL, if the property is not found, or proplist is NULL.
  2799. *
  2800. * Example:
  2801. * (start code)
  2802. * // proplist is obtained from a callback
  2803. * mosquitto_property *prop;
  2804. * prop = mosquitto_property_read_byte(proplist, identifier, &value, false);
  2805. * while(prop){
  2806. * printf("value: %s\n", value);
  2807. * prop = mosquitto_property_read_byte(prop, identifier, &value);
  2808. * }
  2809. * (end)
  2810. */
  2811. libmosq_EXPORT const mosquitto_property *mosquitto_property_read_byte(
  2812. const mosquitto_property *proplist,
  2813. int identifier,
  2814. uint8_t *value,
  2815. bool skip_first);
  2816. /*
  2817. * Function: mosquitto_property_read_int16
  2818. *
  2819. * Read an int16 property value from a property.
  2820. *
  2821. * Parameters:
  2822. * property - property to read
  2823. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2824. * value - pointer to store the value, or NULL if the value is not required.
  2825. * skip_first - boolean that indicates whether the first item in the list
  2826. * should be ignored or not. Should usually be set to false.
  2827. *
  2828. * Returns:
  2829. * A valid property pointer if the property is found
  2830. * NULL, if the property is not found, or proplist is NULL.
  2831. *
  2832. * Example:
  2833. * See <mosquitto_property_read_byte>
  2834. */
  2835. libmosq_EXPORT const mosquitto_property *mosquitto_property_read_int16(
  2836. const mosquitto_property *proplist,
  2837. int identifier,
  2838. uint16_t *value,
  2839. bool skip_first);
  2840. /*
  2841. * Function: mosquitto_property_read_int32
  2842. *
  2843. * Read an int32 property value from a property.
  2844. *
  2845. * Parameters:
  2846. * property - pointer to mosquitto_property pointer, the list of properties
  2847. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2848. * value - pointer to store the value, or NULL if the value is not required.
  2849. * skip_first - boolean that indicates whether the first item in the list
  2850. * should be ignored or not. Should usually be set to false.
  2851. *
  2852. * Returns:
  2853. * A valid property pointer if the property is found
  2854. * NULL, if the property is not found, or proplist is NULL.
  2855. *
  2856. * Example:
  2857. * See <mosquitto_property_read_byte>
  2858. */
  2859. libmosq_EXPORT const mosquitto_property *mosquitto_property_read_int32(
  2860. const mosquitto_property *proplist,
  2861. int identifier,
  2862. uint32_t *value,
  2863. bool skip_first);
  2864. /*
  2865. * Function: mosquitto_property_read_varint
  2866. *
  2867. * Read a varint property value from a property.
  2868. *
  2869. * Parameters:
  2870. * property - property to read
  2871. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2872. * value - pointer to store the value, or NULL if the value is not required.
  2873. * skip_first - boolean that indicates whether the first item in the list
  2874. * should be ignored or not. Should usually be set to false.
  2875. *
  2876. * Returns:
  2877. * A valid property pointer if the property is found
  2878. * NULL, if the property is not found, or proplist is NULL.
  2879. *
  2880. * Example:
  2881. * See <mosquitto_property_read_byte>
  2882. */
  2883. libmosq_EXPORT const mosquitto_property *mosquitto_property_read_varint(
  2884. const mosquitto_property *proplist,
  2885. int identifier,
  2886. uint32_t *value,
  2887. bool skip_first);
  2888. /*
  2889. * Function: mosquitto_property_read_binary
  2890. *
  2891. * Read a binary property value from a property.
  2892. *
  2893. * On success, value must be free()'d by the application.
  2894. *
  2895. * Parameters:
  2896. * property - property to read
  2897. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2898. * value - pointer to store the value, or NULL if the value is not required.
  2899. * skip_first - boolean that indicates whether the first item in the list
  2900. * should be ignored or not. Should usually be set to false.
  2901. *
  2902. * Returns:
  2903. * A valid property pointer if the property is found
  2904. * NULL, if the property is not found, or proplist is NULL, or if an out of memory condition occurred.
  2905. *
  2906. * Example:
  2907. * See <mosquitto_property_read_byte>
  2908. */
  2909. libmosq_EXPORT const mosquitto_property *mosquitto_property_read_binary(
  2910. const mosquitto_property *proplist,
  2911. int identifier,
  2912. void **value,
  2913. uint16_t *len,
  2914. bool skip_first);
  2915. /*
  2916. * Function: mosquitto_property_read_string
  2917. *
  2918. * Read a string property value from a property.
  2919. *
  2920. * On success, value must be free()'d by the application.
  2921. *
  2922. * Parameters:
  2923. * property - property to read
  2924. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2925. * value - pointer to char*, for the property data to be stored in, or NULL if
  2926. * the value is not required.
  2927. * skip_first - boolean that indicates whether the first item in the list
  2928. * should be ignored or not. Should usually be set to false.
  2929. *
  2930. * Returns:
  2931. * A valid property pointer if the property is found
  2932. * NULL, if the property is not found, or proplist is NULL, or if an out of memory condition occurred.
  2933. *
  2934. * Example:
  2935. * See <mosquitto_property_read_byte>
  2936. */
  2937. libmosq_EXPORT const mosquitto_property *mosquitto_property_read_string(
  2938. const mosquitto_property *proplist,
  2939. int identifier,
  2940. char **value,
  2941. bool skip_first);
  2942. /*
  2943. * Function: mosquitto_property_read_string_pair
  2944. *
  2945. * Read a string pair property value pair from a property.
  2946. *
  2947. * On success, name and value must be free()'d by the application.
  2948. *
  2949. * Parameters:
  2950. * property - property to read
  2951. * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR)
  2952. * name - pointer to char* for the name property data to be stored in, or NULL
  2953. * if the name is not required.
  2954. * value - pointer to char*, for the property data to be stored in, or NULL if
  2955. * the value is not required.
  2956. * skip_first - boolean that indicates whether the first item in the list
  2957. * should be ignored or not. Should usually be set to false.
  2958. *
  2959. * Returns:
  2960. * A valid property pointer if the property is found
  2961. * NULL, if the property is not found, or proplist is NULL, or if an out of memory condition occurred.
  2962. *
  2963. * Example:
  2964. * See <mosquitto_property_read_byte>
  2965. */
  2966. libmosq_EXPORT const mosquitto_property *mosquitto_property_read_string_pair(
  2967. const mosquitto_property *proplist,
  2968. int identifier,
  2969. char **name,
  2970. char **value,
  2971. bool skip_first);
  2972. /*
  2973. * Function: mosquitto_property_free_all
  2974. *
  2975. * Free all properties from a list of properties. Frees the list and sets *properties to NULL.
  2976. *
  2977. * Parameters:
  2978. * properties - list of properties to free
  2979. *
  2980. * Example:
  2981. * > mosquitto_properties *properties = NULL;
  2982. * > // Add properties
  2983. * > mosquitto_property_free_all(&properties);
  2984. */
  2985. libmosq_EXPORT void mosquitto_property_free_all(mosquitto_property **properties);
  2986. /*
  2987. * Function: mosquitto_property_copy_all
  2988. *
  2989. * Parameters:
  2990. * dest - pointer for new property list
  2991. * src - property list
  2992. *
  2993. * Returns:
  2994. * MOSQ_ERR_SUCCESS - on successful copy
  2995. * MOSQ_ERR_INVAL - if dest is NULL
  2996. * MOSQ_ERR_NOMEM - on out of memory (dest will be set to NULL)
  2997. */
  2998. libmosq_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const mosquitto_property *src);
  2999. /*
  3000. * Function: mosquitto_property_check_command
  3001. *
  3002. * Check whether a property identifier is valid for the given command.
  3003. *
  3004. * Parameters:
  3005. * command - MQTT command (e.g. CMD_CONNECT)
  3006. * identifier - MQTT property (e.g. MQTT_PROP_USER_PROPERTY)
  3007. *
  3008. * Returns:
  3009. * MOSQ_ERR_SUCCESS - if the identifier is valid for command
  3010. * MOSQ_ERR_PROTOCOL - if the identifier is not valid for use with command.
  3011. */
  3012. libmosq_EXPORT int mosquitto_property_check_command(int command, int identifier);
  3013. /*
  3014. * Function: mosquitto_property_check_all
  3015. *
  3016. * Check whether a list of properties are valid for a particular command,
  3017. * whether there are duplicates, and whether the values are valid where
  3018. * possible.
  3019. *
  3020. * Note that this function is used internally in the library whenever
  3021. * properties are passed to it, so in basic use this is not needed, but should
  3022. * be helpful to check property lists *before* the point of using them.
  3023. *
  3024. * Parameters:
  3025. * command - MQTT command (e.g. CMD_CONNECT)
  3026. * properties - list of MQTT properties to check.
  3027. *
  3028. * Returns:
  3029. * MOSQ_ERR_SUCCESS - if all properties are valid
  3030. * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden.
  3031. * MOSQ_ERR_PROTOCOL - if any property is invalid
  3032. */
  3033. libmosq_EXPORT int mosquitto_property_check_all(int command, const mosquitto_property *properties);
  3034. /*
  3035. * Function: mosquitto_property_identifier_to_string
  3036. *
  3037. * Return the property name as a string for a property identifier.
  3038. * The property name is as defined in the MQTT specification, with - as a
  3039. * separator, for example: payload-format-indicator.
  3040. *
  3041. * Parameters:
  3042. * identifier - valid MQTT property identifier integer
  3043. *
  3044. * Returns:
  3045. * A const string to the property name on success
  3046. * NULL on failure
  3047. */
  3048. libmosq_EXPORT const char *mosquitto_property_identifier_to_string(int identifier);
  3049. /* Function: mosquitto_string_to_property_info
  3050. *
  3051. * Parse a property name string and convert to a property identifier and data type.
  3052. * The property name is as defined in the MQTT specification, with - as a
  3053. * separator, for example: payload-format-indicator.
  3054. *
  3055. * Parameters:
  3056. * propname - the string to parse
  3057. * identifier - pointer to an int to receive the property identifier
  3058. * type - pointer to an int to receive the property type
  3059. *
  3060. * Returns:
  3061. * MOSQ_ERR_SUCCESS - on success
  3062. * MOSQ_ERR_INVAL - if the string does not match a property
  3063. *
  3064. * Example:
  3065. * (start code)
  3066. * mosquitto_string_to_property_info("response-topic", &id, &type);
  3067. * // id == MQTT_PROP_RESPONSE_TOPIC
  3068. * // type == MQTT_PROP_TYPE_STRING
  3069. * (end)
  3070. */
  3071. libmosq_EXPORT int mosquitto_string_to_property_info(const char *propname, int *identifier, int *type);
  3072. #ifdef __cplusplus
  3073. }
  3074. #endif
  3075. #endif