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