Imported Upstream version 0.9.1
[platform/upstream/iotivity.git] / service / protocol-plugin / plugins / mqtt-light / lib / mosquitto.h
1 /*
2 Copyright (c) 2010-2014 Roger Light <roger@atchoo.org>
3 All rights reserved.
4
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7
8 1. Redistributions of source code must retain the above copyright notice,
9    this list of conditions and the following disclaimer.
10 2. Redistributions in binary form must reproduce the above copyright
11    notice, this list of conditions and the following disclaimer in the
12    documentation and/or other materials provided with the distribution.
13 3. Neither the name of mosquitto nor the names of its
14    contributors may be used to endorse or promote products derived from
15    this software without specific prior written permission.
16
17 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 POSSIBILITY OF SUCH DAMAGE.
28
29
30 This product includes software developed by the OpenSSL Project for use in the
31 OpenSSL Toolkit. (http://www.openssl.org/)
32 This product includes cryptographic software written by Eric Young
33 (eay@cryptsoft.com)
34 This product includes software written by Tim Hudson (tjh@cryptsoft.com)
35 */
36
37 #ifndef _MOSQUITTO_H_
38 #define _MOSQUITTO_H_
39
40 #ifdef __cplusplus
41 extern "C" {
42 #endif
43
44 #if defined(WIN32) && !defined(WITH_BROKER)
45 #   ifdef libmosquitto_EXPORTS
46 #       define libmosq_EXPORT  __declspec(dllexport)
47 #   else
48 #       define libmosq_EXPORT  __declspec(dllimport)
49 #   endif
50 #else
51 #   define libmosq_EXPORT
52 #endif
53
54 #ifdef WIN32
55 #   ifndef __cplusplus
56 #       define bool char
57 #       define true 1
58 #       define false 0
59 #   endif
60 #else
61 #   ifndef __cplusplus
62 #       include <stdbool.h>
63 #   endif
64 #endif
65
66 #define LIBMOSQUITTO_MAJOR 1
67 #define LIBMOSQUITTO_MINOR 3
68 #define LIBMOSQUITTO_REVISION 1
69 /* LIBMOSQUITTO_VERSION_NUMBER looks like 1002001 for e.g. version 1.2.1. */
70 #define LIBMOSQUITTO_VERSION_NUMBER (LIBMOSQUITTO_MAJOR*1000000+LIBMOSQUITTO_MINOR*1000+LIBMOSQUITTO_REVISION)
71
72 /* Log types */
73 #define MOSQ_LOG_NONE 0x00
74 #define MOSQ_LOG_INFO 0x01
75 #define MOSQ_LOG_NOTICE 0x02
76 #define MOSQ_LOG_WARNING 0x04
77 #define MOSQ_LOG_ERR 0x08
78 #define MOSQ_LOG_DEBUG 0x10
79 #define MOSQ_LOG_SUBSCRIBE 0x20
80 #define MOSQ_LOG_UNSUBSCRIBE 0x40
81 #define MOSQ_LOG_ALL 0xFFFF
82
83 /* Error values */
84 enum mosq_err_t
85 {
86     MOSQ_ERR_CONN_PENDING = -1,
87     MOSQ_ERR_SUCCESS = 0,
88     MOSQ_ERR_NOMEM = 1,
89     MOSQ_ERR_PROTOCOL = 2,
90     MOSQ_ERR_INVAL = 3,
91     MOSQ_ERR_NO_CONN = 4,
92     MOSQ_ERR_CONN_REFUSED = 5,
93     MOSQ_ERR_NOT_FOUND = 6,
94     MOSQ_ERR_CONN_LOST = 7,
95     MOSQ_ERR_TLS = 8,
96     MOSQ_ERR_PAYLOAD_SIZE = 9,
97     MOSQ_ERR_NOT_SUPPORTED = 10,
98     MOSQ_ERR_AUTH = 11,
99     MOSQ_ERR_ACL_DENIED = 12,
100     MOSQ_ERR_UNKNOWN = 13,
101     MOSQ_ERR_ERRNO = 14,
102     MOSQ_ERR_EAI = 15
103 };
104
105 /* MQTT specification restricts client ids to a maximum of 23 characters */
106 #define MOSQ_MQTT_ID_MAX_LENGTH 23
107
108 struct mosquitto_message
109 {
110     int mid;
111     char *topic;
112     void *payload;
113     int payloadlen;
114     int qos;
115     bool retain;
116 };
117
118 struct mosquitto;
119
120 /*
121  * Topic: Threads
122  *  libmosquitto provides thread safe operation, with the exception of
123  *  <mosquitto_lib_init> which is not thread safe.
124  */
125 /***************************************************
126  * Important note
127  *
128  * The following functions that deal with network operations will return
129  * MOSQ_ERR_SUCCESS on success, but this does not mean that the operation has
130  * taken place. An attempt will be made to write the network data, but if the
131  * socket is not available for writing at that time then the packet will not be
132  * sent. To ensure the packet is sent, call mosquitto_loop() (which must also
133  * be called to process incoming network data).
134  * This is especially important when disconnecting a client that has a will. If
135  * the broker does not receive the DISCONNECT command, it will assume that the
136  * client has disconnected unexpectedly and send the will.
137  *
138  * mosquitto_connect()
139  * mosquitto_disconnect()
140  * mosquitto_subscribe()
141  * mosquitto_unsubscribe()
142  * mosquitto_publish()
143  ***************************************************/
144
145 /*
146  * Function: mosquitto_lib_version
147  *
148  * Can be used to obtain version information for the mosquitto library.
149  * This allows the application to compare the library version against the
150  * version it was compiled against by using the LIBMOSQUITTO_MAJOR,
151  * LIBMOSQUITTO_MINOR and LIBMOSQUITTO_REVISION defines.
152  *
153  * Parameters:
154  *  major -    an integer pointer. If not NULL, the major version of the
155  *             library will be returned in this variable.
156  *  minor -    an integer pointer. If not NULL, the minor version of the
157  *             library will be returned in this variable.
158  *  revision - an integer pointer. If not NULL, the revision of the library will
159  *             be returned in this variable.
160  *
161  * Returns:
162  *  LIBMOSQUITTO_VERSION_NUMBER, which is a unique number based on the major,
163  *      minor and revision values.
164  * See Also:
165  *  <mosquitto_lib_cleanup>, <mosquitto_lib_init>
166  */
167 libmosq_EXPORT int mosquitto_lib_version(int *major, int *minor, int *revision);
168
169 /*
170  * Function: mosquitto_lib_init
171  *
172  * Must be called before any other mosquitto functions.
173  *
174  * This function is *not* thread safe.
175  *
176  * Returns:
177  *  MOSQ_ERR_SUCCESS - always
178  *
179  * See Also:
180  *  <mosquitto_lib_cleanup>, <mosquitto_lib_version>
181  */
182 libmosq_EXPORT int mosquitto_lib_init(void);
183
184 /*
185  * Function: mosquitto_lib_cleanup
186  *
187  * Call to free resources associated with the library.
188  *
189  * Returns:
190  *  MOSQ_ERR_SUCCESS - always
191  *
192  * See Also:
193  *  <mosquitto_lib_init>, <mosquitto_lib_version>
194  */
195 libmosq_EXPORT int mosquitto_lib_cleanup(void);
196
197 /*
198  * Function: mosquitto_new
199  *
200  * Create a new mosquitto client instance.
201  *
202  * Parameters:
203  *  id -            String to use as the client id. If NULL, a random client id
204  *                  will be generated. If id is NULL, clean_session must be true.
205  *  clean_session - set to true to instruct the broker to clean all messages
206  *                  and subscriptions on disconnect, false to instruct it to
207  *                  keep them. See the man page mqtt(7) for more details.
208  *                  Note that a client will never discard its own outgoing
209  *                  messages on disconnect. Calling <mosquitto_connect> or
210  *                  <mosquitto_reconnect> will cause the messages to be resent.
211  *                  Use <mosquitto_reinitialise> to reset a client to its
212  *                  original state.
213  *                  Must be set to true if the id parameter is NULL.
214  *  obj -           A user pointer that will be passed as an argument to any
215  *                  callbacks that are specified.
216  *
217  * Returns:
218  *  Pointer to a struct mosquitto on success.
219  *  NULL on failure. Interrogate errno to determine the cause for the failure:
220  *      - ENOMEM on out of memory.
221  *      - EINVAL on invalid input parameters.
222  *
223  * See Also:
224  *  <mosquitto_reinitialise>, <mosquitto_destroy>, <mosquitto_user_data_set>
225  */
226 libmosq_EXPORT struct mosquitto *mosquitto_new(const char *id, bool clean_session, void *obj);
227
228 /*
229  * Function: mosquitto_destroy
230  *
231  * Use to free memory associated with a mosquitto client instance.
232  *
233  * Parameters:
234  *  mosq - a struct mosquitto pointer to free.
235  *
236  * See Also:
237  *  <mosquitto_new>, <mosquitto_reinitialise>
238  */
239 libmosq_EXPORT void mosquitto_destroy(struct mosquitto *mosq);
240
241 /*
242  * Function: mosquitto_reinitialise
243  *
244  * This function allows an existing mosquitto client to be reused. Call on a
245  * mosquitto instance to close any open network connections, free memory
246  * and reinitialise the client with the new parameters. The end result is the
247  * same as the output of <mosquitto_new>.
248  *
249  * Parameters:
250  *  mosq -          a valid mosquitto instance.
251  *  id -            string to use as the client id. If NULL, a random client id
252  *                  will be generated. If id is NULL, clean_session must be true.
253  *  clean_session - set to true to instruct the broker to clean all messages
254  *                  and subscriptions on disconnect, false to instruct it to
255  *                  keep them. See the man page mqtt(7) for more details.
256  *                  Must be set to true if the id parameter is NULL.
257  *  obj -           A user pointer that will be passed as an argument to any
258  *                  callbacks that are specified.
259  *
260  * Returns:
261  *  MOSQ_ERR_SUCCESS - on success.
262  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
263  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
264  *
265  * See Also:
266  *  <mosquitto_new>, <mosquitto_destroy>
267  */
268 libmosq_EXPORT int mosquitto_reinitialise(struct mosquitto *mosq, const char *id,
269         bool clean_session, void *obj);
270
271 /*
272  * Function: mosquitto_will_set
273  *
274  * Configure will information for a mosquitto instance. By default, clients do
275  * not have a will.  This must be called before calling <mosquitto_connect>.
276  *
277  * Parameters:
278  *  mosq -       a valid mosquitto instance.
279  *  topic -      the topic on which to publish the will.
280  *  payloadlen - the size of the payload (bytes). Valid values are between 0 and
281  *               268,435,455.
282  *  payload -    pointer to the data to send. If payloadlen > 0 this must be a
283  *               valid memory location.
284  *  qos -        integer value 0, 1 or 2 indicating the Quality of Service to be
285  *               used for the will.
286  *  retain -     set to true to make the will a retained message.
287  *
288  * Returns:
289  *  MOSQ_ERR_SUCCESS -      on success.
290  *  MOSQ_ERR_INVAL -        if the input parameters were invalid.
291  *  MOSQ_ERR_NOMEM -        if an out of memory condition occurred.
292  *  MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large.
293  */
294 libmosq_EXPORT int mosquitto_will_set(struct mosquitto *mosq, const char *topic, int payloadlen,
295                                       const void *payload, int qos, bool retain);
296
297 /*
298  * Function: mosquitto_will_clear
299  *
300  * Remove a previously configured will. This must be called before calling
301  * <mosquitto_connect>.
302  *
303  * Parameters:
304  *  mosq - a valid mosquitto instance.
305  *
306  * Returns:
307  *  MOSQ_ERR_SUCCESS - on success.
308  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
309  */
310 libmosq_EXPORT int mosquitto_will_clear(struct mosquitto *mosq);
311
312 /*
313  * Function: mosquitto_username_pw_set
314  *
315  * Configure username and password for a mosquitton instance. This is only
316  * supported by brokers that implement the MQTT spec v3.1. By default, no
317  * username or password will be sent.
318  * If username is NULL, the password argument is ignored.
319  * This must be called before calling mosquitto_connect().
320  *
321  * This is must be called before calling <mosquitto_connect>.
322  *
323  * Parameters:
324  *  mosq -     a valid mosquitto instance.
325  *  username - the username to send as a string, or NULL to disable
326  *             authentication.
327  *  password - the password to send as a string. Set to NULL when username is
328  *             valid in order to send just a username.
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  */
335 libmosq_EXPORT int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username,
336         const char *password);
337
338 /*
339  * Function: mosquitto_connect
340  *
341  * Connect to an MQTT broker.
342  *
343  * Parameters:
344  *  mosq -      a valid mosquitto instance.
345  *  host -      the hostname or ip address of the broker to connect to.
346  *  port -      the network port to connect to. Usually 1883.
347  *  keepalive - the number of seconds after which the broker should send a PING
348  *              message to the client if no other messages have been exchanged
349  *              in that time.
350  *
351  * Returns:
352  *  MOSQ_ERR_SUCCESS - on success.
353  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
354  *  MOSQ_ERR_ERRNO -   if a system call returned an error. The variable errno
355  *                     contains the error code, even on Windows.
356  *                     Use strerror_r() where available or FormatMessage() on
357  *                     Windows.
358  *
359  * See Also:
360  *  <mosquitto_connect_bind>, <mosquitto_connect_async>, <mosquitto_reconnect>, <mosquitto_disconnect>, <mosquitto_tls_set>
361  */
362 libmosq_EXPORT int mosquitto_connect(struct mosquitto *mosq, const char *host, int port,
363                                      int keepalive);
364
365 /*
366  * Function: mosquitto_connect_bind
367  *
368  * Connect to an MQTT broker. This extends the functionality of
369  * <mosquitto_connect> by adding the bind_address parameter. Use this function
370  * if you need to restrict network communication over a particular interface.
371  *
372  * Parameters:
373  *  mosq -         a valid mosquitto instance.
374  *  host -         the hostname or ip address of the broker to connect to.
375  *  port -         the network port to connect to. Usually 1883.
376  *  keepalive -    the number of seconds after which the broker should send a PING
377  *                 message to the client if no other messages have been exchanged
378  *                 in that time.
379  *  bind_address - the hostname or ip address of the local network interface to
380  *                 bind to.
381  *
382  * Returns:
383  *  MOSQ_ERR_SUCCESS - on success.
384  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
385  *  MOSQ_ERR_ERRNO -   if a system call returned an error. The variable errno
386  *                     contains the error code, even on Windows.
387  *                     Use strerror_r() where available or FormatMessage() on
388  *                     Windows.
389  *
390  * See Also:
391  *  <mosquitto_connect>, <mosquitto_connect_async>, <mosquitto_connect_bind_async>
392  */
393 libmosq_EXPORT int mosquitto_connect_bind(struct mosquitto *mosq, const char *host, int port,
394         int keepalive, const char *bind_address);
395
396 /*
397  * Function: mosquitto_connect_async
398  *
399  * Connect to an MQTT broker. This is a non-blocking call. If you use
400  * <mosquitto_connect_async> your client must use the threaded interface
401  * <mosquitto_loop_start>. If you need to use <mosquitto_loop>, you must use
402  * <mosquitto_connect> to connect the client.
403  *
404  * May be called before or after <mosquitto_loop_start>.
405  *
406  * Parameters:
407  *  mosq -      a valid mosquitto instance.
408  *  host -      the hostname or ip address of the broker to connect to.
409  *  port -      the network port to connect to. Usually 1883.
410  *  keepalive - the number of seconds after which the broker should send a PING
411  *              message to the client if no other messages have been exchanged
412  *              in that time.
413  *
414  * Returns:
415  *  MOSQ_ERR_SUCCESS - on success.
416  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
417  *  MOSQ_ERR_ERRNO -   if a system call returned an error. The variable errno
418  *                     contains the error code, even on Windows.
419  *                     Use strerror_r() where available or FormatMessage() on
420  *                     Windows.
421  *
422  * See Also:
423  *  <mosquitto_connect_bind_async>, <mosquitto_connect>, <mosquitto_reconnect>, <mosquitto_disconnect>, <mosquitto_tls_set>
424  */
425 libmosq_EXPORT int mosquitto_connect_async(struct mosquitto *mosq, const char *host, int port,
426         int keepalive);
427
428 /*
429  * Function: mosquitto_connect_async
430  *
431  * Connect to an MQTT broker. This is a non-blocking call. If you use
432  * <mosquitto_connect_async> your client must use the threaded interface
433  * <mosquitto_loop_start>. If you need to use <mosquitto_loop>, you must use
434  * <mosquitto_connect> to connect the client.
435  *
436  * This extends the functionality of <mosquitto_connect_async> by adding the
437  * bind_address parameter. Use this function if you need to restrict network
438  * communication over a particular interface.
439  *
440  * May be called before or after <mosquitto_loop_start>.
441  *
442  * Parameters:
443  *  mosq -         a valid mosquitto instance.
444  *  host -         the hostname or ip address of the broker to connect to.
445  *  port -         the network port to connect to. Usually 1883.
446  *  keepalive -    the number of seconds after which the broker should send a PING
447  *                 message to the client if no other messages have been exchanged
448  *                 in that time.
449  *  bind_address - the hostname or ip address of the local network interface to
450  *                 bind to.
451  *
452  * Returns:
453  *  MOSQ_ERR_SUCCESS - on success.
454  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
455  *  MOSQ_ERR_ERRNO -   if a system call returned an error. The variable errno
456  *                     contains the error code, even on Windows.
457  *                     Use strerror_r() where available or FormatMessage() on
458  *                     Windows.
459  *
460  * See Also:
461  *  <mosquitto_connect_async>, <mosquitto_connect>, <mosquitto_connect_bind>
462  */
463 libmosq_EXPORT int mosquitto_connect_bind_async(struct mosquitto *mosq, const char *host, int port,
464         int keepalive, const char *bind_address);
465
466 /*
467  * Function: mosquitto_connect_srv
468  *
469  * Connect to an MQTT broker. This is a non-blocking call. If you use
470  * <mosquitto_connect_async> your client must use the threaded interface
471  * <mosquitto_loop_start>. If you need to use <mosquitto_loop>, you must use
472  * <mosquitto_connect> to connect the client.
473  *
474  * This extends the functionality of <mosquitto_connect_async> by adding the
475  * bind_address parameter. Use this function if you need to restrict network
476  * communication over a particular interface.
477  *
478  * May be called before or after <mosquitto_loop_start>.
479  *
480  * Parameters:
481  *  mosq -         a valid mosquitto instance.
482  *  host -         the hostname or ip address of the broker to connect to.
483  *  keepalive -    the number of seconds after which the broker should send a PING
484  *                 message to the client if no other messages have been exchanged
485  *                 in that time.
486  *  bind_address - the hostname or ip address of the local network interface to
487  *                 bind to.
488  *
489  * Returns:
490  *  MOSQ_ERR_SUCCESS - on success.
491  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
492  *  MOSQ_ERR_ERRNO -   if a system call returned an error. The variable errno
493  *                     contains the error code, even on Windows.
494  *                     Use strerror_r() where available or FormatMessage() on
495  *                     Windows.
496  *
497  * See Also:
498  *  <mosquitto_connect_async>, <mosquitto_connect>, <mosquitto_connect_bind>
499  */
500 libmosq_EXPORT int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepalive,
501         const char *bind_address);
502
503 /*
504  * Function: mosquitto_reconnect
505  *
506  * Reconnect to a broker.
507  *
508  * This function provides an easy way of reconnecting to a broker after a
509  * connection has been lost. It uses the values that were provided in the
510  * <mosquitto_connect> call. It must not be called before
511  * <mosquitto_connect>.
512  *
513  * Parameters:
514  *  mosq - a valid mosquitto instance.
515  *
516  * Returns:
517  *  MOSQ_ERR_SUCCESS - on success.
518  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
519  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
520  *
521  * Returns:
522  *  MOSQ_ERR_SUCCESS - on success.
523  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
524  *  MOSQ_ERR_ERRNO -   if a system call returned an error. The variable errno
525  *                     contains the error code, even on Windows.
526  *                     Use strerror_r() where available or FormatMessage() on
527  *                     Windows.
528  *
529  * See Also:
530  *  <mosquitto_connect>, <mosquitto_disconnect>, <mosquitto_reconnect_async>
531  */
532 libmosq_EXPORT int mosquitto_reconnect(struct mosquitto *mosq);
533
534 /*
535  * Function: mosquitto_reconnect_async
536  *
537  * Reconnect to a broker. Non blocking version of <mosquitto_reconnect>.
538  *
539  * This function provides an easy way of reconnecting to a broker after a
540  * connection has been lost. It uses the values that were provided in the
541  * <mosquitto_connect> or <mosquitto_connect_async> calls. It must not be
542  * called before <mosquitto_connect>.
543  *
544  * Parameters:
545  *  mosq - a valid mosquitto instance.
546  *
547  * Returns:
548  *  MOSQ_ERR_SUCCESS - on success.
549  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
550  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
551  *
552  * Returns:
553  *  MOSQ_ERR_SUCCESS - on success.
554  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
555  *  MOSQ_ERR_ERRNO -   if a system call returned an error. The variable errno
556  *                     contains the error code, even on Windows.
557  *                     Use strerror_r() where available or FormatMessage() on
558  *                     Windows.
559  *
560  * See Also:
561  *  <mosquitto_connect>, <mosquitto_disconnect>
562  */
563 libmosq_EXPORT int mosquitto_reconnect_async(struct mosquitto *mosq);
564
565 /*
566  * Function: mosquitto_disconnect
567  *
568  * Disconnect from the broker.
569  *
570  * Parameters:
571  *  mosq - a valid mosquitto instance.
572  *
573  * Returns:
574  *  MOSQ_ERR_SUCCESS - on success.
575  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
576  *  MOSQ_ERR_NO_CONN -  if the client isn't connected to a broker.
577  */
578 libmosq_EXPORT int mosquitto_disconnect(struct mosquitto *mosq);
579
580 /*
581  * Function: mosquitto_publish
582  *
583  * Publish a message on a given topic.
584  *
585  * Parameters:
586  *  mosq -       a valid mosquitto instance.
587  *  mid -        pointer to an int. If not NULL, the function will set this
588  *               to the message id of this particular message. This can be then
589  *               used with the publish callback to determine when the message
590  *               has been sent.
591  *               Note that although the MQTT protocol doesn't use message ids
592  *               for messages with QoS=0, libmosquitto assigns them message ids
593  *               so they can be tracked with this parameter.
594  *  payloadlen - the size of the payload (bytes). Valid values are between 0 and
595  *               268,435,455.
596  *  payload -    pointer to the data to send. If payloadlen > 0 this must be a
597  *               valid memory location.
598  *  qos -        integer value 0, 1 or 2 indicating the Quality of Service to be
599  *               used for the message.
600  *  retain -     set to true to make the message retained.
601  *
602  * Returns:
603  *  MOSQ_ERR_SUCCESS -      on success.
604  *  MOSQ_ERR_INVAL -        if the input parameters were invalid.
605  *  MOSQ_ERR_NOMEM -        if an out of memory condition occurred.
606  *  MOSQ_ERR_NO_CONN -      if the client isn't connected to a broker.
607  *  MOSQ_ERR_PROTOCOL -     if there is a protocol error communicating with the
608  *                          broker.
609  *  MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large.
610  *
611  * See Also:
612  *  <mosquitto_max_inflight_messages_set>
613  */
614 libmosq_EXPORT int mosquitto_publish(struct mosquitto *mosq, int *mid, const char *topic,
615                                      int payloadlen, const void *payload, int qos, bool retain);
616
617 /*
618  * Function: mosquitto_subscribe
619  *
620  * Subscribe to a topic.
621  *
622  * Parameters:
623  *  mosq - a valid mosquitto instance.
624  *  mid -  a pointer to an int. If not NULL, the function will set this to
625  *         the message id of this particular message. This can be then used
626  *         with the subscribe callback to determine when the message has been
627  *         sent.
628  *  sub -  the subscription pattern.
629  *  qos -  the requested Quality of Service for this subscription.
630  *
631  * Returns:
632  *  MOSQ_ERR_SUCCESS - on success.
633  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
634  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
635  *  MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
636  */
637 libmosq_EXPORT int mosquitto_subscribe(struct mosquitto *mosq, int *mid, const char *sub, int qos);
638
639 /*
640  * Function: mosquitto_unsubscribe
641  *
642  * Unsubscribe from a topic.
643  *
644  * Parameters:
645  *  mosq - a valid mosquitto instance.
646  *  mid -  a pointer to an int. If not NULL, the function will set this to
647  *         the message id of this particular message. This can be then used
648  *         with the unsubscribe callback to determine when the message has been
649  *         sent.
650  *  sub -  the unsubscription pattern.
651  *
652  * Returns:
653  *  MOSQ_ERR_SUCCESS - on success.
654  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
655  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
656  *  MOSQ_ERR_NO_CONN - if the client isn't connected to a broker.
657  */
658 libmosq_EXPORT int mosquitto_unsubscribe(struct mosquitto *mosq, int *mid, const char *sub);
659
660 /*
661  * Function: mosquitto_message_copy
662  *
663  * Copy the contents of a mosquitto message to another message.
664  * Useful for preserving a message received in the on_message() callback.
665  *
666  * Parameters:
667  *  dst - a pointer to a valid mosquitto_message struct to copy to.
668  *  src - a pointer to a valid mosquitto_message struct to copy from.
669  *
670  * Returns:
671  *  MOSQ_ERR_SUCCESS - on success.
672  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
673  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
674  *
675  * See Also:
676  *  <mosquitto_message_free>
677  */
678 libmosq_EXPORT int mosquitto_message_copy(struct mosquitto_message *dst,
679         const struct mosquitto_message *src);
680
681 /*
682  * Function: mosquitto_message_free
683  *
684  * Completely free a mosquitto_message struct.
685  *
686  * Parameters:
687  *  message - pointer to a mosquitto_message pointer to free.
688  *
689  * See Also:
690  *  <mosquitto_message_copy>
691  */
692 libmosq_EXPORT void mosquitto_message_free(struct mosquitto_message **message);
693
694 /*
695  * Function: mosquitto_loop
696  *
697  * The main network loop for the client. You must call this frequently in order
698  * to keep communications between the client and broker working. If incoming
699  * data is present it will then be processed. Outgoing commands, from e.g.
700  * <mosquitto_publish>, are normally sent immediately that their function is
701  * called, but this is not always possible. <mosquitto_loop> will also attempt
702  * to send any remaining outgoing messages, which also includes commands that
703  * are part of the flow for messages with QoS>0.
704  *
705  * An alternative approach is to use <mosquitto_loop_start> to run the client
706  * loop in its own thread.
707  *
708  * This calls select() to monitor the client network socket. If you want to
709  * integrate mosquitto client operation with your own select() call, use
710  * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write> and
711  * <mosquitto_loop_misc>.
712  *
713  * Threads:
714  *
715  * Parameters:
716  *  mosq -        a valid mosquitto instance.
717  *  timeout -     Maximum number of milliseconds to wait for network activity
718  *                in the select() call before timing out. Set to 0 for instant
719  *                return.  Set negative to use the default of 1000ms.
720  *  max_packets - this parameter is currently unused and should be set to 1 for
721  *                future compatibility.
722  *
723  * Returns:
724  *  MOSQ_ERR_SUCCESS -   on success.
725  *  MOSQ_ERR_INVAL -     if the input parameters were invalid.
726  *  MOSQ_ERR_NOMEM -     if an out of memory condition occurred.
727  *  MOSQ_ERR_NO_CONN -   if the client isn't connected to a broker.
728  *  MOSQ_ERR_CONN_LOST - if the connection to the broker was lost.
729  *  MOSQ_ERR_PROTOCOL -  if there is a protocol error communicating with the
730  *                       broker.
731  *  MOSQ_ERR_ERRNO -     if a system call returned an error. The variable errno
732  *                       contains the error code, even on Windows.
733  *                       Use strerror_r() where available or FormatMessage() on
734  *                       Windows.
735  * See Also:
736  *  <mosquitto_loop_forever>, <mosquitto_loop_start>, <mosquitto_loop_stop>
737  */
738 libmosq_EXPORT int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets);
739
740 /*
741  * Function: mosquitto_loop_forever
742  *
743  * This function call loop() for you in an infinite blocking loop. It is useful
744  * for the case where you only want to run the MQTT client loop in your
745  * program.
746  *
747  * It handles reconnecting in case server connection is lost. If you call
748  * mosquitto_disconnect() in a callback it will return.
749  *
750  * Parameters:
751  *  mosq - a valid mosquitto instance.
752  *  timeout -     Maximum number of milliseconds to wait for network activity
753  *                in the select() call before timing out. Set to 0 for instant
754  *                return.  Set negative to use the default of 1000ms.
755  *  max_packets - this parameter is currently unused and should be set to 1 for
756  *                future compatibility.
757  *
758  * Returns:
759  *  MOSQ_ERR_SUCCESS -   on success.
760  *  MOSQ_ERR_INVAL -     if the input parameters were invalid.
761  *  MOSQ_ERR_NOMEM -     if an out of memory condition occurred.
762  *  MOSQ_ERR_NO_CONN -   if the client isn't connected to a broker.
763  *  MOSQ_ERR_CONN_LOST - if the connection to the broker was lost.
764  *  MOSQ_ERR_PROTOCOL -  if there is a protocol error communicating with the
765  *                       broker.
766  *  MOSQ_ERR_ERRNO -     if a system call returned an error. The variable errno
767  *                       contains the error code, even on Windows.
768  *                       Use strerror_r() where available or FormatMessage() on
769  *                       Windows.
770  *
771  * See Also:
772  *  <mosquitto_loop>, <mosquitto_loop_start>
773  */
774 libmosq_EXPORT int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets);
775
776 /*
777  * Function: mosquitto_loop_start
778  *
779  * This is part of the threaded client interface. Call this once to start a new
780  * thread to process network traffic. This provides an alternative to
781  * repeatedly calling <mosquitto_loop> yourself.
782  *
783  * Parameters:
784  *  mosq - a valid mosquitto instance.
785  *
786  * Returns:
787  *  MOSQ_ERR_SUCCESS -       on success.
788  *  MOSQ_ERR_INVAL -         if the input parameters were invalid.
789  *  MOSQ_ERR_NOT_SUPPORTED - if thread support is not available.
790  *
791  * See Also:
792  *  <mosquitto_connect_async>, <mosquitto_loop>, <mosquitto_loop_forever>, <mosquitto_loop_stop>
793  */
794 libmosq_EXPORT int mosquitto_loop_start(struct mosquitto *mosq);
795
796 /*
797  * Function: mosquitto_loop_stop
798  *
799  * This is part of the threaded client interface. Call this once to stop the
800  * network thread previously created with <mosquitto_loop_start>. This call
801  * will block until the network thread finishes. For the network thread to end,
802  * you must have previously called <mosquitto_disconnect> or have set the force
803  * parameter to true.
804  *
805  * Parameters:
806  *  mosq - a valid mosquitto instance.
807  *  force - set to true to force thread cancellation. If false,
808  *          <mosquitto_disconnect> must have already been called.
809  *
810  * Returns:
811  *  MOSQ_ERR_SUCCESS -       on success.
812  *  MOSQ_ERR_INVAL -         if the input parameters were invalid.
813  *  MOSQ_ERR_NOT_SUPPORTED - if thread support is not available.
814  *
815  * See Also:
816  *  <mosquitto_loop>, <mosquitto_loop_start>
817  */
818 libmosq_EXPORT int mosquitto_loop_stop(struct mosquitto *mosq, bool force);
819
820 /*
821  * Function: mosquitto_socket
822  *
823  * Return the socket handle for a mosquitto instance. Useful if you want to
824  * include a mosquitto client in your own select() calls.
825  *
826  * Parameters:
827  *  mosq - a valid mosquitto instance.
828  *
829  * Returns:
830  *  The socket for the mosquitto client or -1 on failure.
831  */
832 libmosq_EXPORT int mosquitto_socket(struct mosquitto *mosq);
833
834 /*
835  * Function: mosquitto_loop_read
836  *
837  * Carry out network read operations.
838  * This should only be used if you are not using mosquitto_loop() and are
839  * monitoring the client network socket for activity yourself.
840  *
841  * Parameters:
842  *  mosq -        a valid mosquitto instance.
843  *  max_packets - this parameter is currently unused and should be set to 1 for
844  *                future compatibility.
845  *
846  * Returns:
847  *  MOSQ_ERR_SUCCESS -   on success.
848  *  MOSQ_ERR_INVAL -     if the input parameters were invalid.
849  *  MOSQ_ERR_NOMEM -     if an out of memory condition occurred.
850  *  MOSQ_ERR_NO_CONN -   if the client isn't connected to a broker.
851  *  MOSQ_ERR_CONN_LOST - if the connection to the broker was lost.
852  *  MOSQ_ERR_PROTOCOL -  if there is a protocol error communicating with the
853  *                       broker.
854  *  MOSQ_ERR_ERRNO -     if a system call returned an error. The variable errno
855  *                       contains the error code, even on Windows.
856  *                       Use strerror_r() where available or FormatMessage() on
857  *                       Windows.
858  *
859  * See Also:
860  *  <mosquitto_socket>, <mosquitto_loop_write>, <mosquitto_loop_misc>
861  */
862 libmosq_EXPORT int mosquitto_loop_read(struct mosquitto *mosq, int max_packets);
863
864 /*
865  * Function: mosquitto_loop_write
866  *
867  * Carry out network write operations.
868  * This should only be used if you are not using mosquitto_loop() and are
869  * monitoring the client network socket for activity yourself.
870  *
871  * Parameters:
872  *  mosq -        a valid mosquitto instance.
873  *  max_packets - this parameter is currently unused and should be set to 1 for
874  *                future compatibility.
875  *
876  * Returns:
877  *  MOSQ_ERR_SUCCESS -   on success.
878  *  MOSQ_ERR_INVAL -     if the input parameters were invalid.
879  *  MOSQ_ERR_NOMEM -     if an out of memory condition occurred.
880  *  MOSQ_ERR_NO_CONN -   if the client isn't connected to a broker.
881  *  MOSQ_ERR_CONN_LOST - if the connection to the broker was lost.
882  *  MOSQ_ERR_PROTOCOL -  if there is a protocol error communicating with the
883  *                       broker.
884  *  MOSQ_ERR_ERRNO -     if a system call returned an error. The variable errno
885  *                       contains the error code, even on Windows.
886  *                       Use strerror_r() where available or FormatMessage() on
887  *                       Windows.
888  *
889  * See Also:
890  *  <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_misc>, <mosquitto_want_write>
891  */
892 libmosq_EXPORT int mosquitto_loop_write(struct mosquitto *mosq, int max_packets);
893
894 /*
895  * Function: mosquitto_loop_misc
896  *
897  * Carry out miscellaneous operations required as part of the network loop.
898  * This should only be used if you are not using mosquitto_loop() and are
899  * monitoring the client network socket for activity yourself.
900  *
901  * This function deals with handling PINGs and checking whether messages need
902  * to be retried, so should be called fairly frequently.
903  *
904  * Parameters:
905  *  mosq - a valid mosquitto instance.
906  *
907  * Returns:
908  *  MOSQ_ERR_SUCCESS -   on success.
909  *  MOSQ_ERR_INVAL -     if the input parameters were invalid.
910  *  MOSQ_ERR_NO_CONN -   if the client isn't connected to a broker.
911  *
912  * See Also:
913  *  <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write>
914  */
915 libmosq_EXPORT int mosquitto_loop_misc(struct mosquitto *mosq);
916
917 /*
918  * Function: mosquitto_want_write
919  *
920  * Returns true if there is data ready to be written on the socket.
921  *
922  * Parameters:
923  *  mosq - a valid mosquitto instance.
924  *
925  * See Also:
926  *  <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write>
927  */
928 libmosq_EXPORT bool mosquitto_want_write(struct mosquitto *mosq);
929
930 /*
931  * Function: mosquitto_tls_set
932  *
933  * Configure the client for certificate based SSL/TLS support. Must be called
934  * before <mosquitto_connect>.
935  *
936  * Cannot be used in conjunction with <mosquitto_tls_psk_set>.
937  *
938  * Define the Certificate Authority certificates to be trusted (ie. the server
939  * certificate must be signed with one of these certificates) using cafile.
940  *
941  * If the server you are connecting to requires clients to provide a
942  * certificate, define certfile and keyfile with your client certificate and
943  * private key. If your private key is encrypted, provide a password callback
944  * function or you will have to enter the password at the command line.
945  *
946  * Parameters:
947  *  mosq -        a valid mosquitto instance.
948  *  cafile -      path to a file containing the PEM encoded trusted CA
949  *                certificate files. Either cafile or capath must not be NULL.
950  *  capath -      path to a directory containing the PEM encoded trusted CA
951  *                certificate files. See mosquitto.conf for more details on
952  *                configuring this directory. Either cafile or capath must not
953  *                be NULL.
954  *  certfile -    path to a file containing the PEM encoded certificate file
955  *                for this client. If NULL, keyfile must also be NULL and no
956  *                client certificate will be used.
957  *  keyfile -     path to a file containing the PEM encoded private key for
958  *                this client. If NULL, certfile must also be NULL and no
959  *                client certificate will be used.
960  *  pw_callback - if keyfile is encrypted, set pw_callback to allow your client
961  *                to pass the correct password for decryption. If set to NULL,
962  *                the password must be entered on the command line.
963  *                Your callback must write the password into "buf", which is
964  *                "size" bytes long. The return value must be the length of the
965  *                password. "userdata" will be set to the calling mosquitto
966  *                instance.
967  *
968  * Returns:
969  *  MOSQ_ERR_SUCCESS - on success.
970  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
971  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
972  *
973  * See Also:
974  *  <mosquitto_tls_opts_set>, <mosquitto_tls_psk_set>, <mosquitto_tls_insecure_set>
975  */
976 libmosq_EXPORT int mosquitto_tls_set(struct mosquitto *mosq,
977                                      const char *cafile, const char *capath,
978                                      const char *certfile, const char *keyfile,
979                                      int (*pw_callback)(char *buf, int size, int rwflag, void *userdata));
980
981 /*
982  * Function: mosquitto_tls_insecure_set
983  *
984  * Configure verification of the server hostname in the server certificate. If
985  * value is set to true, it is impossible to guarantee that the host you are
986  * connecting to is not impersonating your server. This can be useful in
987  * initial server testing, but makes it possible for a malicious third party to
988  * impersonate your server through DNS spoofing, for example.
989  * Do not use this function in a real system. Setting value to true makes the
990  * connection encryption pointless.
991  * Must be called before <mosquitto_connect>.
992  *
993  * Parameters:
994  *  mosq -  a valid mosquitto instance.
995  *  value - if set to false, the default, certificate hostname checking is
996  *          performed. If set to true, no hostname checking is performed and
997  *          the connection is insecure.
998  *
999  * Returns:
1000  *  MOSQ_ERR_SUCCESS - on success.
1001  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
1002  *
1003  * See Also:
1004  *  <mosquitto_tls_set>
1005  */
1006 libmosq_EXPORT int mosquitto_tls_insecure_set(struct mosquitto *mosq, bool value);
1007
1008 /*
1009  * Function: mosquitto_tls_opts_set
1010  *
1011  * Set advanced SSL/TLS options. Must be called before <mosquitto_connect>.
1012  *
1013  * Parameters:
1014  *  mosq -        a valid mosquitto instance.
1015  *  cert_reqs -   an integer defining the verification requirements the client
1016  *                will impose on the server. This can be one of:
1017  *                * SSL_VERIFY_NONE (0): the server will not be verified in any way.
1018  *                * SSL_VERIFY_PEER (1): the server certificate will be verified
1019  *                  and the connection aborted if the verification fails.
1020  *                The default and recommended value is SSL_VERIFY_PEER. Using
1021  *                SSL_VERIFY_NONE provides no security.
1022  *  tls_version - the version of the SSL/TLS protocol to use as a string. If NULL,
1023  *                the default value is used. The default value and the
1024  *                available values depend on the version of openssl that the
1025  *                library was compiled against. For openssl >= 1.0.1, the
1026  *                available options are tlsv1.2, tlsv1.1 and tlsv1, with tlv1.2
1027  *                as the default. For openssl < 1.0.1, only tlsv1 is available.
1028  *  ciphers -     a string describing the ciphers available for use. See the
1029  *                "openssl ciphers" tool for more information. If NULL, the
1030  *                default ciphers will be used.
1031  *
1032  * Returns:
1033  *  MOSQ_ERR_SUCCESS - on success.
1034  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
1035  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
1036  *
1037  * See Also:
1038  *  <mosquitto_tls_set>
1039  */
1040 libmosq_EXPORT int mosquitto_tls_opts_set(struct mosquitto *mosq, int cert_reqs,
1041         const char *tls_version, const char *ciphers);
1042
1043 /*
1044  * Function: mosquitto_tls_psk_set
1045  *
1046  * Configure the client for pre-shared-key based TLS support. Must be called
1047  * before <mosquitto_connect>.
1048  *
1049  * Cannot be used in conjunction with <mosquitto_tls_set>.
1050  *
1051  * Parameters:
1052  *  mosq -     a valid mosquitto instance.
1053  *  psk -      the pre-shared-key in hex format with no leading "0x".
1054  *  identity - the identity of this client. May be used as the username
1055  *             depending on the server settings.
1056  *  ciphers -  a string describing the PSK ciphers available for use. See the
1057  *             "openssl ciphers" tool for more information. If NULL, the
1058  *             default ciphers will be used.
1059  *
1060  * Returns:
1061  *  MOSQ_ERR_SUCCESS - on success.
1062  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
1063  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
1064  *
1065  * See Also:
1066  *  <mosquitto_tls_set>
1067  */
1068 libmosq_EXPORT int mosquitto_tls_psk_set(struct mosquitto *mosq, const char *psk,
1069         const char *identity, const char *ciphers);
1070
1071 /*
1072  * Function: mosquitto_connect_callback_set
1073  *
1074  * Set the connect callback. This is called when the broker sends a CONNACK
1075  * message in response to a connection.
1076  *
1077  * Parameters:
1078  *  mosq -       a valid mosquitto instance.
1079  *  on_connect - a callback function in the following form:
1080  *               void callback(struct mosquitto *mosq, void *obj, int rc)
1081  *
1082  * Callback Parameters:
1083  *  mosq - the mosquitto instance making the callback.
1084  *  obj - the user data provided in <mosquitto_new>
1085  *  rc -  the return code of the connection response, one of:
1086  *
1087  * * 0 - success
1088  * * 1 - connection refused (unacceptable protocol version)
1089  * * 2 - connection refused (identifier rejected)
1090  * * 3 - connection refused (broker unavailable)
1091  * * 4-255 - reserved for future use
1092  */
1093 libmosq_EXPORT void mosquitto_connect_callback_set(struct mosquitto *mosq,
1094         void (*on_connect)(struct mosquitto *, void *, int));
1095
1096 /*
1097  * Function: mosquitto_disconnect_callback_set
1098  *
1099  * Set the disconnect callback. This is called when the broker has received the
1100  * DISCONNECT command and has disconnected the client.
1101  *
1102  * Parameters:
1103  *  mosq -          a valid mosquitto instance.
1104  *  on_disconnect - a callback function in the following form:
1105  *                  void callback(struct mosquitto *mosq, void *obj)
1106  *
1107  * Callback Parameters:
1108  *  mosq - the mosquitto instance making the callback.
1109  *  obj -  the user data provided in <mosquitto_new>
1110  *  rc -   integer value indicating the reason for the disconnect. A value of 0
1111  *         means the client has called <mosquitto_disconnect>. Any other value
1112  *         indicates that the disconnect is unexpected.
1113  */
1114 libmosq_EXPORT void mosquitto_disconnect_callback_set(struct mosquitto *mosq,
1115         void (*on_disconnect)(struct mosquitto *, void *, int));
1116
1117 /*
1118  * Function: mosquitto_publish_callback_set
1119  *
1120  * Set the publish callback. This is called when a message initiated with
1121  * <mosquitto_publish> has been sent to the broker successfully.
1122  *
1123  * Parameters:
1124  *  mosq -       a valid mosquitto instance.
1125  *  on_publish - a callback function in the following form:
1126  *               void callback(struct mosquitto *mosq, void *obj, int mid)
1127  *
1128  * Callback Parameters:
1129  *  mosq - the mosquitto instance making the callback.
1130  *  obj -  the user data provided in <mosquitto_new>
1131  *  mid -  the message id of the sent message.
1132  */
1133 libmosq_EXPORT void mosquitto_publish_callback_set(struct mosquitto *mosq,
1134         void (*on_publish)(struct mosquitto *, void *, int));
1135
1136 /*
1137  * Function: mosquitto_message_callback_set
1138  *
1139  * Set the message callback. This is called when a message is received from the
1140  * broker.
1141  *
1142  * Parameters:
1143  *  mosq -       a valid mosquitto instance.
1144  *  on_message - a callback function in the following form:
1145  *               void callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message)
1146  *
1147  * Callback Parameters:
1148  *  mosq -    the mosquitto instance making the callback.
1149  *  obj -     the user data provided in <mosquitto_new>
1150  *  message - the message data. This variable and associated memory will be
1151  *            freed by the library after the callback completes. The client
1152  *            should make copies of any of the data it requires.
1153  *
1154  * See Also:
1155  *  <mosquitto_message_copy>
1156  */
1157 libmosq_EXPORT void mosquitto_message_callback_set(struct mosquitto *mosq,
1158         void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *));
1159
1160 /*
1161  * Function: mosquitto_subscribe_callback_set
1162  *
1163  * Set the subscribe callback. This is called when the broker responds to a
1164  * subscription request.
1165  *
1166  * Parameters:
1167  *  mosq -         a valid mosquitto instance.
1168  *  on_subscribe - a callback function in the following form:
1169  *                 void callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos)
1170  *
1171  * Callback Parameters:
1172  *  mosq -        the mosquitto instance making the callback.
1173  *  obj -         the user data provided in <mosquitto_new>
1174  *  mid -         the message id of the subscribe message.
1175  *  qos_count -   the number of granted subscriptions (size of granted_qos).
1176  *  granted_qos - an array of integers indicating the granted QoS for each of
1177  *                the subscriptions.
1178  */
1179 libmosq_EXPORT void mosquitto_subscribe_callback_set(struct mosquitto *mosq,
1180         void (*on_subscribe)(struct mosquitto *, void *, int, int, const int *));
1181
1182 /*
1183  * Function: mosquitto_unsubscribe_callback_set
1184  *
1185  * Set the unsubscribe callback. This is called when the broker responds to a
1186  * unsubscription request.
1187  *
1188  * Parameters:
1189  *  mosq -           a valid mosquitto instance.
1190  *  on_unsubscribe - a callback function in the following form:
1191  *                   void callback(struct mosquitto *mosq, void *obj, int mid)
1192  *
1193  * Callback Parameters:
1194  *  mosq - the mosquitto instance making the callback.
1195  *  obj -  the user data provided in <mosquitto_new>
1196  *  mid -  the message id of the unsubscribe message.
1197  */
1198 libmosq_EXPORT void mosquitto_unsubscribe_callback_set(struct mosquitto *mosq,
1199         void (*on_unsubscribe)(struct mosquitto *, void *, int));
1200
1201 /*
1202  * Function: mosquitto_log_callback_set
1203  *
1204  * Set the logging callback. This should be used if you want event logging
1205  * information from the client library.
1206  *
1207  *  mosq -   a valid mosquitto instance.
1208  *  on_log - a callback function in the following form:
1209  *           void callback(struct mosquitto *mosq, void *obj, int level, const char *str)
1210  *
1211  * Callback Parameters:
1212  *  mosq -  the mosquitto instance making the callback.
1213  *  obj -   the user data provided in <mosquitto_new>
1214  *  level - the log message level from the values:
1215  *          MOSQ_LOG_INFO
1216  *          MOSQ_LOG_NOTICE
1217  *          MOSQ_LOG_WARNING
1218  *          MOSQ_LOG_ERR
1219  *          MOSQ_LOG_DEBUG
1220  *  str -   the message string.
1221  */
1222 libmosq_EXPORT void mosquitto_log_callback_set(struct mosquitto *mosq,
1223         void (*on_log)(struct mosquitto *, void *, int, const char *));
1224
1225 /*
1226  * Function: mosquitto_reconnect_delay_set
1227  *
1228  * Control the behaviour of the client when it has unexpectedly disconnected in
1229  * <mosquitto_loop_forever> or after <mosquitto_loop_start>. The default
1230  * behaviour if this function is not used is to repeatedly attempt to reconnect
1231  * with a delay of 1 second until the connection succeeds.
1232  *
1233  * Use reconnect_delay parameter to change the delay between successive
1234  * reconnection attempts. You may also enable exponential backoff of the time
1235  * between reconnections by setting reconnect_exponential_backoff to true and
1236  * set an upper bound on the delay with reconnect_delay_max.
1237  *
1238  * Example 1:
1239  *  delay=2, delay_max=10, exponential_backoff=False
1240  *  Delays would be: 2, 4, 6, 8, 10, 10, ...
1241  *
1242  * Example 2:
1243  *  delay=3, delay_max=30, exponential_backoff=True
1244  *  Delays would be: 3, 6, 12, 24, 30, 30, ...
1245  *
1246  * Parameters:
1247  *  mosq -                          a valid mosquitto instance.
1248  *  reconnect_delay -               the number of seconds to wait between
1249  *                                  reconnects.
1250  *  reconnect_delay_max -           the maximum number of seconds to wait
1251  *                                  between reconnects.
1252  *  reconnect_exponential_backoff - use exponential backoff between
1253  *                                  reconnect attempts. Set to true to enable
1254  *                                  exponential backoff.
1255  *
1256  * Returns:
1257  *  MOSQ_ERR_SUCCESS - on success.
1258  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
1259  */
1260 libmosq_EXPORT int mosquitto_reconnect_delay_set(struct mosquitto *mosq,
1261         unsigned int reconnect_delay, unsigned int reconnect_delay_max, bool reconnect_exponential_backoff);
1262
1263 /*
1264  * Function: mosquitto_max_inflight_messages_set
1265  *
1266  * Set the number of QoS 1 and 2 messages that can be "in flight" at one time.
1267  * An in flight message is part way through its delivery flow. Attempts to send
1268  * further messages with <mosquitto_publish> will result in the messages being
1269  * queued until the number of in flight messages reduces.
1270  *
1271  * A higher number here results in greater message throughput, but if set
1272  * higher than the maximum in flight messages on the broker may lead to
1273  * delays in the messages being acknowledged.
1274  *
1275  * Set to 0 for no maximum.
1276  *
1277  * Parameters:
1278  *  mosq -                  a valid mosquitto instance.
1279  *  max_inflight_messages - the maximum number of inflight messages. Defaults
1280  *                          to 20.
1281  *
1282  * Returns:
1283  *  MOSQ_ERR_SUCCESS - on success.
1284  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
1285  */
1286 libmosq_EXPORT int mosquitto_max_inflight_messages_set(struct mosquitto *mosq,
1287         unsigned int max_inflight_messages);
1288
1289 /*
1290  * Function: mosquitto_message_retry_set
1291  *
1292  * Set the number of seconds to wait before retrying messages. This applies to
1293  * publish messages with QoS>0. May be called at any time.
1294  *
1295  * Parameters:
1296  *  mosq -          a valid mosquitto instance.
1297  *  message_retry - the number of seconds to wait for a response before
1298  *                  retrying. Defaults to 20.
1299  */
1300 libmosq_EXPORT void mosquitto_message_retry_set(struct mosquitto *mosq, unsigned int message_retry);
1301
1302 /*
1303  * Function: mosquitto_user_data_set
1304  *
1305  * When <mosquitto_new> is called, the pointer given as the "obj" parameter
1306  * will be passed to the callbacks as user data. The <mosquitto_user_data_set>
1307  * function allows this obj parameter to be updated at any time. This function
1308  * will not modify the memory pointed to by the current user data pointer. If
1309  * it is dynamically allocated memory you must free it yourself.
1310  *
1311  * Parameters:
1312  *  mosq - a valid mosquitto instance.
1313  *  obj -  A user pointer that will be passed as an argument to any callbacks
1314  *         that are specified.
1315  */
1316 libmosq_EXPORT void mosquitto_user_data_set(struct mosquitto *mosq, void *obj);
1317
1318
1319 /* =============================================================================
1320  *
1321  * Section: Utility functions
1322  *
1323  * =============================================================================
1324  */
1325
1326 /*
1327  * Function: mosquitto_strerror
1328  *
1329  * Call to obtain a const string description of a mosquitto error number.
1330  *
1331  * Parameters:
1332  *  mosq_errno - a mosquitto error number.
1333  *
1334  * Returns:
1335  *  A constant string describing the error.
1336  */
1337 libmosq_EXPORT const char *mosquitto_strerror(int mosq_errno);
1338
1339 /*
1340  * Function: mosquitto_connack_string
1341  *
1342  * Call to obtain a const string description of an MQTT connection result.
1343  *
1344  * Parameters:
1345  *  connack_code - an MQTT connection result.
1346  *
1347  * Returns:
1348  *  A constant string describing the result.
1349  */
1350 libmosq_EXPORT const char *mosquitto_connack_string(int connack_code);
1351
1352 /*
1353  * Function: mosquitto_sub_topic_tokenise
1354  *
1355  * Tokenise a topic or subscription string into an array of strings
1356  * representing the topic hierarchy.
1357  *
1358  * For example:
1359  *
1360  * subtopic: "a/deep/topic/hierarchy"
1361  *
1362  * Would result in:
1363  *
1364  * topics[0] = "a"
1365  * topics[1] = "deep"
1366  * topics[2] = "topic"
1367  * topics[3] = "hierarchy"
1368  *
1369  * and:
1370  *
1371  * subtopic: "/a/deep/topic/hierarchy/"
1372  *
1373  * Would result in:
1374  *
1375  * topics[0] = NULL
1376  * topics[1] = "a"
1377  * topics[2] = "deep"
1378  * topics[3] = "topic"
1379  * topics[4] = "hierarchy"
1380  *
1381  * Parameters:
1382  *  subtopic - the subscription/topic to tokenise
1383  *  topics -   a pointer to store the array of strings
1384  *  count -    an int pointer to store the number of items in the topics array.
1385  *
1386  * Returns:
1387  *  MOSQ_ERR_SUCCESS - on success
1388  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
1389  *
1390  * Example:
1391  *
1392  * > char **topics;
1393  * > int topic_count;
1394  * > int i;
1395  * >
1396  * > mosquitto_sub_topic_tokenise("$SYS/broker/uptime", &topics, &topic_count);
1397  * >
1398  * > for(i=0; i<token_count; i++){
1399  * >     printf("%d: %s\n", i, topics[i]);
1400  * > }
1401  *
1402  * See Also:
1403  *  <mosquitto_sub_topic_tokens_free>
1404  */
1405 libmosq_EXPORT int mosquitto_sub_topic_tokenise(const char *subtopic, char ***topics, int *count);
1406
1407 /*
1408  * Function: mosquitto_sub_topic_tokens_free
1409  *
1410  * Free memory that was allocated in <mosquitto_sub_topic_tokenise>.
1411  *
1412  * Parameters:
1413  *  topics - pointer to string array.
1414  *  count - count of items in string array.
1415  *
1416  * Returns:
1417  *  MOSQ_ERR_SUCCESS - on success
1418  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
1419  *
1420  * See Also:
1421  *  <mosquitto_sub_topic_tokenise>
1422  */
1423 libmosq_EXPORT int mosquitto_sub_topic_tokens_free(char ***topics, int count);
1424
1425 /*
1426  * Function: mosquitto_topic_matches_sub
1427  *
1428  * Check whether a topic matches a subscription.
1429  *
1430  * For example:
1431  *
1432  * foo/bar would match the subscription foo/# or +/bar
1433  * non/matching would not match the subscription non/+/+
1434  *
1435  * Parameters:
1436  *  sub - subscription string to check topic against.
1437  *  topic - topic to check.
1438  *  result - bool pointer to hold result. Will be set to true if the topic
1439  *           matches the subscription.
1440  *
1441  * Returns:
1442  *  MOSQ_ERR_SUCCESS - on success
1443  *  MOSQ_ERR_INVAL -   if the input parameters were invalid.
1444  *  MOSQ_ERR_NOMEM -   if an out of memory condition occurred.
1445  */
1446 libmosq_EXPORT int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result);
1447
1448 #ifdef __cplusplus
1449 }
1450 #endif
1451
1452 #endif