![]() |
|
|||
File indexing completed on 2025-06-01 08:54:12
0001 /* 0002 Copyright (c) 2010-2020 Roger Light <roger@atchoo.org> 0003 0004 All rights reserved. This program and the accompanying materials 0005 are made available under the terms of the Eclipse Public License 2.0 0006 and Eclipse Distribution License v1.0 which accompany this distribution. 0007 0008 The Eclipse Public License is available at 0009 https://www.eclipse.org/legal/epl-2.0/ 0010 and the Eclipse Distribution License is available at 0011 http://www.eclipse.org/org/documents/edl-v10.php. 0012 0013 SPDX-License-Identifier: EPL-2.0 OR BSD-3-Clause 0014 0015 Contributors: 0016 Roger Light - initial implementation and documentation. 0017 */ 0018 0019 #ifndef MOSQUITTO_H 0020 #define MOSQUITTO_H 0021 0022 /* 0023 * File: mosquitto.h 0024 * 0025 * This header contains functions and definitions for use with libmosquitto, the Mosquitto client library. 0026 * 0027 * The definitions are also used in Mosquitto broker plugins, and some functions are available to plugins. 0028 */ 0029 #ifdef __cplusplus 0030 extern "C" { 0031 #endif 0032 0033 0034 #ifdef WIN32 0035 # ifdef mosquitto_EXPORTS 0036 # define libmosq_EXPORT __declspec(dllexport) 0037 # else 0038 # ifndef LIBMOSQUITTO_STATIC 0039 # ifdef libmosquitto_EXPORTS 0040 # define libmosq_EXPORT __declspec(dllexport) 0041 # else 0042 # define libmosq_EXPORT __declspec(dllimport) 0043 # endif 0044 # else 0045 # define libmosq_EXPORT 0046 # endif 0047 # endif 0048 #else 0049 # define libmosq_EXPORT 0050 #endif 0051 0052 #if defined(_MSC_VER) && _MSC_VER < 1900 && !defined(bool) 0053 # ifndef __cplusplus 0054 # define bool char 0055 # define true 1 0056 # define false 0 0057 # endif 0058 #else 0059 # ifndef __cplusplus 0060 # include <stdbool.h> 0061 # endif 0062 #endif 0063 0064 #include <stddef.h> 0065 #include <stdint.h> 0066 0067 #define LIBMOSQUITTO_MAJOR 2 0068 #define LIBMOSQUITTO_MINOR 0 0069 #define LIBMOSQUITTO_REVISION 18 0070 /* LIBMOSQUITTO_VERSION_NUMBER looks like 1002001 for e.g. version 1.2.1. */ 0071 #define LIBMOSQUITTO_VERSION_NUMBER (LIBMOSQUITTO_MAJOR*1000000+LIBMOSQUITTO_MINOR*1000+LIBMOSQUITTO_REVISION) 0072 0073 /* Log types */ 0074 #define MOSQ_LOG_NONE 0 0075 #define MOSQ_LOG_INFO (1<<0) 0076 #define MOSQ_LOG_NOTICE (1<<1) 0077 #define MOSQ_LOG_WARNING (1<<2) 0078 #define MOSQ_LOG_ERR (1<<3) 0079 #define MOSQ_LOG_DEBUG (1<<4) 0080 #define MOSQ_LOG_SUBSCRIBE (1<<5) 0081 #define MOSQ_LOG_UNSUBSCRIBE (1<<6) 0082 #define MOSQ_LOG_WEBSOCKETS (1<<7) 0083 #define MOSQ_LOG_INTERNAL 0x80000000U 0084 #define MOSQ_LOG_ALL 0xFFFFFFFFU 0085 0086 /* Enum: mosq_err_t 0087 * Integer values returned from many libmosquitto functions. */ 0088 enum mosq_err_t { 0089 MOSQ_ERR_AUTH_CONTINUE = -4, 0090 MOSQ_ERR_NO_SUBSCRIBERS = -3, 0091 MOSQ_ERR_SUB_EXISTS = -2, 0092 MOSQ_ERR_CONN_PENDING = -1, 0093 MOSQ_ERR_SUCCESS = 0, 0094 MOSQ_ERR_NOMEM = 1, 0095 MOSQ_ERR_PROTOCOL = 2, 0096 MOSQ_ERR_INVAL = 3, 0097 MOSQ_ERR_NO_CONN = 4, 0098 MOSQ_ERR_CONN_REFUSED = 5, 0099 MOSQ_ERR_NOT_FOUND = 6, 0100 MOSQ_ERR_CONN_LOST = 7, 0101 MOSQ_ERR_TLS = 8, 0102 MOSQ_ERR_PAYLOAD_SIZE = 9, 0103 MOSQ_ERR_NOT_SUPPORTED = 10, 0104 MOSQ_ERR_AUTH = 11, 0105 MOSQ_ERR_ACL_DENIED = 12, 0106 MOSQ_ERR_UNKNOWN = 13, 0107 MOSQ_ERR_ERRNO = 14, 0108 MOSQ_ERR_EAI = 15, 0109 MOSQ_ERR_PROXY = 16, 0110 MOSQ_ERR_PLUGIN_DEFER = 17, 0111 MOSQ_ERR_MALFORMED_UTF8 = 18, 0112 MOSQ_ERR_KEEPALIVE = 19, 0113 MOSQ_ERR_LOOKUP = 20, 0114 MOSQ_ERR_MALFORMED_PACKET = 21, 0115 MOSQ_ERR_DUPLICATE_PROPERTY = 22, 0116 MOSQ_ERR_TLS_HANDSHAKE = 23, 0117 MOSQ_ERR_QOS_NOT_SUPPORTED = 24, 0118 MOSQ_ERR_OVERSIZE_PACKET = 25, 0119 MOSQ_ERR_OCSP = 26, 0120 MOSQ_ERR_TIMEOUT = 27, 0121 MOSQ_ERR_RETAIN_NOT_SUPPORTED = 28, 0122 MOSQ_ERR_TOPIC_ALIAS_INVALID = 29, 0123 MOSQ_ERR_ADMINISTRATIVE_ACTION = 30, 0124 MOSQ_ERR_ALREADY_EXISTS = 31, 0125 }; 0126 0127 /* Enum: mosq_opt_t 0128 * 0129 * Client options. 0130 * 0131 * See <mosquitto_int_option>, <mosquitto_string_option>, and <mosquitto_void_option>. 0132 */ 0133 enum mosq_opt_t { 0134 MOSQ_OPT_PROTOCOL_VERSION = 1, 0135 MOSQ_OPT_SSL_CTX = 2, 0136 MOSQ_OPT_SSL_CTX_WITH_DEFAULTS = 3, 0137 MOSQ_OPT_RECEIVE_MAXIMUM = 4, 0138 MOSQ_OPT_SEND_MAXIMUM = 5, 0139 MOSQ_OPT_TLS_KEYFORM = 6, 0140 MOSQ_OPT_TLS_ENGINE = 7, 0141 MOSQ_OPT_TLS_ENGINE_KPASS_SHA1 = 8, 0142 MOSQ_OPT_TLS_OCSP_REQUIRED = 9, 0143 MOSQ_OPT_TLS_ALPN = 10, 0144 MOSQ_OPT_TCP_NODELAY = 11, 0145 MOSQ_OPT_BIND_ADDRESS = 12, 0146 MOSQ_OPT_TLS_USE_OS_CERTS = 13, 0147 }; 0148 0149 0150 /* MQTT specification restricts client ids to a maximum of 23 characters */ 0151 #define MOSQ_MQTT_ID_MAX_LENGTH 23 0152 0153 #define MQTT_PROTOCOL_V31 3 0154 #define MQTT_PROTOCOL_V311 4 0155 #define MQTT_PROTOCOL_V5 5 0156 0157 /* Struct: mosquitto_message 0158 * 0159 * Contains details of a PUBLISH message. 0160 * 0161 * int mid - the message/packet ID of the PUBLISH message, assuming this is a 0162 * QoS 1 or 2 message. Will be set to 0 for QoS 0 messages. 0163 * 0164 * char *topic - the topic the message was delivered on. 0165 * 0166 * void *payload - the message payload. This will be payloadlen bytes long, and 0167 * may be NULL if a zero length payload was sent. 0168 * 0169 * int payloadlen - the length of the payload, in bytes. 0170 * 0171 * int qos - the quality of service of the message, 0, 1, or 2. 0172 * 0173 * bool retain - set to true for stale retained messages. 0174 */ 0175 struct mosquitto_message{ 0176 int mid; 0177 char *topic; 0178 void *payload; 0179 int payloadlen; 0180 int qos; 0181 bool retain; 0182 }; 0183 0184 struct mosquitto; 0185 typedef struct mqtt5__property mosquitto_property; 0186 0187 /* 0188 * Topic: Threads 0189 * libmosquitto provides thread safe operation, with the exception of 0190 * <mosquitto_lib_init> which is not thread safe. 0191 * 0192 * If the library has been compiled without thread support it is *not* 0193 * guaranteed to be thread safe. 0194 * 0195 * If your application uses threads you must use <mosquitto_threaded_set> to 0196 * tell the library this is the case, otherwise it makes some optimisations 0197 * for the single threaded case that may result in unexpected behaviour for 0198 * the multi threaded case. 0199 */ 0200 /*************************************************** 0201 * Important note 0202 * 0203 * The following functions that deal with network operations will return 0204 * MOSQ_ERR_SUCCESS on success, but this does not mean that the operation has 0205 * taken place. An attempt will be made to write the network data, but if the 0206 * socket is not available for writing at that time then the packet will not be 0207 * sent. To ensure the packet is sent, call mosquitto_loop() (which must also 0208 * be called to process incoming network data). 0209 * This is especially important when disconnecting a client that has a will. If 0210 * the broker does not receive the DISCONNECT command, it will assume that the 0211 * client has disconnected unexpectedly and send the will. 0212 * 0213 * mosquitto_connect() 0214 * mosquitto_disconnect() 0215 * mosquitto_subscribe() 0216 * mosquitto_unsubscribe() 0217 * mosquitto_publish() 0218 ***************************************************/ 0219 0220 0221 /* ====================================================================== 0222 * 0223 * Section: Library version, init, and cleanup 0224 * 0225 * ====================================================================== */ 0226 /* 0227 * Function: mosquitto_lib_version 0228 * 0229 * Can be used to obtain version information for the mosquitto library. 0230 * This allows the application to compare the library version against the 0231 * version it was compiled against by using the LIBMOSQUITTO_MAJOR, 0232 * LIBMOSQUITTO_MINOR and LIBMOSQUITTO_REVISION defines. 0233 * 0234 * Parameters: 0235 * major - an integer pointer. If not NULL, the major version of the 0236 * library will be returned in this variable. 0237 * minor - an integer pointer. If not NULL, the minor version of the 0238 * library will be returned in this variable. 0239 * revision - an integer pointer. If not NULL, the revision of the library will 0240 * be returned in this variable. 0241 * 0242 * Returns: 0243 * LIBMOSQUITTO_VERSION_NUMBER - which is a unique number based on the major, 0244 * minor and revision values. 0245 * See Also: 0246 * <mosquitto_lib_cleanup>, <mosquitto_lib_init> 0247 */ 0248 libmosq_EXPORT int mosquitto_lib_version(int *major, int *minor, int *revision); 0249 0250 /* 0251 * Function: mosquitto_lib_init 0252 * 0253 * Must be called before any other mosquitto functions. 0254 * 0255 * This function is *not* thread safe. 0256 * 0257 * Returns: 0258 * MOSQ_ERR_SUCCESS - on success. 0259 * MOSQ_ERR_UNKNOWN - on Windows, if sockets couldn't be initialized. 0260 * 0261 * See Also: 0262 * <mosquitto_lib_cleanup>, <mosquitto_lib_version> 0263 */ 0264 libmosq_EXPORT int mosquitto_lib_init(void); 0265 0266 /* 0267 * Function: mosquitto_lib_cleanup 0268 * 0269 * Call to free resources associated with the library. 0270 * 0271 * Returns: 0272 * MOSQ_ERR_SUCCESS - always 0273 * 0274 * See Also: 0275 * <mosquitto_lib_init>, <mosquitto_lib_version> 0276 */ 0277 libmosq_EXPORT int mosquitto_lib_cleanup(void); 0278 0279 0280 /* ====================================================================== 0281 * 0282 * Section: Client creation, destruction, and reinitialisation 0283 * 0284 * ====================================================================== */ 0285 /* 0286 * Function: mosquitto_new 0287 * 0288 * Create a new mosquitto client instance. 0289 * 0290 * Parameters: 0291 * id - String to use as the client id. If NULL, a random client id 0292 * will be generated. If id is NULL, clean_session must be true. 0293 * clean_session - set to true to instruct the broker to clean all messages 0294 * and subscriptions on disconnect, false to instruct it to 0295 * keep them. See the man page mqtt(7) for more details. 0296 * Note that a client will never discard its own outgoing 0297 * messages on disconnect. Calling <mosquitto_connect> or 0298 * <mosquitto_reconnect> will cause the messages to be resent. 0299 * Use <mosquitto_reinitialise> to reset a client to its 0300 * original state. 0301 * Must be set to true if the id parameter is NULL. 0302 * obj - A user pointer that will be passed as an argument to any 0303 * callbacks that are specified. 0304 * 0305 * Returns: 0306 * Pointer to a struct mosquitto on success. 0307 * NULL on failure. Interrogate errno to determine the cause for the failure: 0308 * - ENOMEM on out of memory. 0309 * - EINVAL on invalid input parameters. 0310 * 0311 * See Also: 0312 * <mosquitto_reinitialise>, <mosquitto_destroy>, <mosquitto_user_data_set> 0313 */ 0314 libmosq_EXPORT struct mosquitto *mosquitto_new(const char *id, bool clean_session, void *obj); 0315 0316 /* 0317 * Function: mosquitto_destroy 0318 * 0319 * Use to free memory associated with a mosquitto client instance. 0320 * 0321 * Parameters: 0322 * mosq - a struct mosquitto pointer to free. 0323 * 0324 * See Also: 0325 * <mosquitto_new>, <mosquitto_reinitialise> 0326 */ 0327 libmosq_EXPORT void mosquitto_destroy(struct mosquitto *mosq); 0328 0329 /* 0330 * Function: mosquitto_reinitialise 0331 * 0332 * This function allows an existing mosquitto client to be reused. Call on a 0333 * mosquitto instance to close any open network connections, free memory 0334 * and reinitialise the client with the new parameters. The end result is the 0335 * same as the output of <mosquitto_new>. 0336 * 0337 * Parameters: 0338 * mosq - a valid mosquitto instance. 0339 * id - string to use as the client id. If NULL, a random client id 0340 * will be generated. If id is NULL, clean_session must be true. 0341 * clean_session - set to true to instruct the broker to clean all messages 0342 * and subscriptions on disconnect, false to instruct it to 0343 * keep them. See the man page mqtt(7) for more details. 0344 * Must be set to true if the id parameter is NULL. 0345 * obj - A user pointer that will be passed as an argument to any 0346 * callbacks that are specified. 0347 * 0348 * Returns: 0349 * MOSQ_ERR_SUCCESS - on success. 0350 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0351 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0352 * MOSQ_ERR_MALFORMED_UTF8 - if the client id is not valid UTF-8. 0353 * 0354 * See Also: 0355 * <mosquitto_new>, <mosquitto_destroy> 0356 */ 0357 libmosq_EXPORT int mosquitto_reinitialise(struct mosquitto *mosq, const char *id, bool clean_session, void *obj); 0358 0359 0360 /* ====================================================================== 0361 * 0362 * Section: Will 0363 * 0364 * ====================================================================== */ 0365 /* 0366 * Function: mosquitto_will_set 0367 * 0368 * Configure will information for a mosquitto instance. By default, clients do 0369 * not have a will. This must be called before calling <mosquitto_connect>. 0370 * 0371 * It is valid to use this function for clients using all MQTT protocol versions. 0372 * If you need to set MQTT v5 Will properties, use <mosquitto_will_set_v5> instead. 0373 * 0374 * Parameters: 0375 * mosq - a valid mosquitto instance. 0376 * topic - the topic on which to publish the will. 0377 * payloadlen - the size of the payload (bytes). Valid values are between 0 and 0378 * 268,435,455. 0379 * payload - pointer to the data to send. If payloadlen > 0 this must be a 0380 * valid memory location. 0381 * qos - integer value 0, 1 or 2 indicating the Quality of Service to be 0382 * used for the will. 0383 * retain - set to true to make the will a retained message. 0384 * 0385 * Returns: 0386 * MOSQ_ERR_SUCCESS - on success. 0387 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0388 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0389 * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large. 0390 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8. 0391 */ 0392 libmosq_EXPORT int mosquitto_will_set(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain); 0393 0394 /* 0395 * Function: mosquitto_will_set_v5 0396 * 0397 * Configure will information for a mosquitto instance, with attached 0398 * properties. By default, clients do not have a will. This must be called 0399 * before calling <mosquitto_connect>. 0400 * 0401 * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument 0402 * will be applied to the Will. For MQTT v3.1.1 and below, the `properties` 0403 * argument will be ignored. 0404 * 0405 * Set your client to use MQTT v5 immediately after it is created: 0406 * 0407 * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); 0408 * 0409 * Parameters: 0410 * mosq - a valid mosquitto instance. 0411 * topic - the topic on which to publish the will. 0412 * payloadlen - the size of the payload (bytes). Valid values are between 0 and 0413 * 268,435,455. 0414 * payload - pointer to the data to send. If payloadlen > 0 this must be a 0415 * valid memory location. 0416 * qos - integer value 0, 1 or 2 indicating the Quality of Service to be 0417 * used for the will. 0418 * retain - set to true to make the will a retained message. 0419 * properties - list of MQTT 5 properties. Can be NULL. On success only, the 0420 * property list becomes the property of libmosquitto once this 0421 * function is called and will be freed by the library. The 0422 * property list must be freed by the application on error. 0423 * 0424 * Returns: 0425 * MOSQ_ERR_SUCCESS - on success. 0426 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0427 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0428 * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large. 0429 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8. 0430 * MOSQ_ERR_NOT_SUPPORTED - if properties is not NULL and the client is not 0431 * using MQTT v5 0432 * MOSQ_ERR_PROTOCOL - if a property is invalid for use with wills. 0433 * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden. 0434 */ 0435 libmosq_EXPORT int mosquitto_will_set_v5(struct mosquitto *mosq, const char *topic, int payloadlen, const void *payload, int qos, bool retain, mosquitto_property *properties); 0436 0437 /* 0438 * Function: mosquitto_will_clear 0439 * 0440 * Remove a previously configured will. This must be called before calling 0441 * <mosquitto_connect>. 0442 * 0443 * Parameters: 0444 * mosq - a valid mosquitto instance. 0445 * 0446 * Returns: 0447 * MOSQ_ERR_SUCCESS - on success. 0448 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0449 */ 0450 libmosq_EXPORT int mosquitto_will_clear(struct mosquitto *mosq); 0451 0452 0453 /* ====================================================================== 0454 * 0455 * Section: Username and password 0456 * 0457 * ====================================================================== */ 0458 /* 0459 * Function: mosquitto_username_pw_set 0460 * 0461 * Configure username and password for a mosquitto instance. By default, no 0462 * username or password will be sent. For v3.1 and v3.1.1 clients, if username 0463 * is NULL, the password argument is ignored. 0464 * 0465 * This is must be called before calling <mosquitto_connect>. 0466 * 0467 * Parameters: 0468 * mosq - a valid mosquitto instance. 0469 * username - the username to send as a string, or NULL to disable 0470 * authentication. 0471 * password - the password to send as a string. Set to NULL when username is 0472 * valid in order to send just a username. 0473 * 0474 * Returns: 0475 * MOSQ_ERR_SUCCESS - on success. 0476 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0477 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0478 */ 0479 libmosq_EXPORT int mosquitto_username_pw_set(struct mosquitto *mosq, const char *username, const char *password); 0480 0481 0482 /* ====================================================================== 0483 * 0484 * Section: Connecting, reconnecting, disconnecting 0485 * 0486 * ====================================================================== */ 0487 /* 0488 * Function: mosquitto_connect 0489 * 0490 * Connect to an MQTT broker. 0491 * 0492 * It is valid to use this function for clients using all MQTT protocol versions. 0493 * If you need to set MQTT v5 CONNECT properties, use <mosquitto_connect_bind_v5> 0494 * instead. 0495 * 0496 * Parameters: 0497 * mosq - a valid mosquitto instance. 0498 * host - the hostname or ip address of the broker to connect to. 0499 * port - the network port to connect to. Usually 1883. 0500 * keepalive - the number of seconds after which the client should send a PING 0501 * message to the broker if no other messages have been exchanged 0502 * in that time. 0503 * 0504 * Returns: 0505 * MOSQ_ERR_SUCCESS - on success. 0506 * MOSQ_ERR_INVAL - if the input parameters were invalid, which could be any of: 0507 * * mosq == NULL 0508 * * host == NULL 0509 * * port < 0 0510 * * keepalive < 5 0511 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 0512 * contains the error code, even on Windows. 0513 * Use strerror_r() where available or FormatMessage() on 0514 * Windows. 0515 * 0516 * See Also: 0517 * <mosquitto_connect_bind>, <mosquitto_connect_async>, <mosquitto_reconnect>, <mosquitto_disconnect>, <mosquitto_tls_set> 0518 */ 0519 libmosq_EXPORT int mosquitto_connect(struct mosquitto *mosq, const char *host, int port, int keepalive); 0520 0521 /* 0522 * Function: mosquitto_connect_bind 0523 * 0524 * Connect to an MQTT broker. This extends the functionality of 0525 * <mosquitto_connect> by adding the bind_address parameter. Use this function 0526 * if you need to restrict network communication over a particular interface. 0527 * 0528 * Parameters: 0529 * mosq - a valid mosquitto instance. 0530 * host - the hostname or ip address of the broker to connect to. 0531 * port - the network port to connect to. Usually 1883. 0532 * keepalive - the number of seconds after which the client should send a PING 0533 * message to the broker if no other messages have been exchanged 0534 * in that time. 0535 * bind_address - the hostname or ip address of the local network interface to 0536 * bind to. If you do not want to bind to a specific interface, 0537 * set this to NULL. 0538 * 0539 * Returns: 0540 * MOSQ_ERR_SUCCESS - on success. 0541 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0542 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 0543 * contains the error code, even on Windows. 0544 * Use strerror_r() where available or FormatMessage() on 0545 * Windows. 0546 * 0547 * See Also: 0548 * <mosquitto_connect>, <mosquitto_connect_async>, <mosquitto_connect_bind_async> 0549 */ 0550 libmosq_EXPORT int mosquitto_connect_bind(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address); 0551 0552 /* 0553 * Function: mosquitto_connect_bind_v5 0554 * 0555 * Connect to an MQTT broker. This extends the functionality of 0556 * <mosquitto_connect> by adding the bind_address parameter and MQTT v5 0557 * properties. Use this function if you need to restrict network communication 0558 * over a particular interface. 0559 * 0560 * Use e.g. <mosquitto_property_add_string> and similar to create a list of 0561 * properties, then attach them to this publish. Properties need freeing with 0562 * <mosquitto_property_free_all>. 0563 * 0564 * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument 0565 * will be applied to the CONNECT message. For MQTT v3.1.1 and below, the 0566 * `properties` argument will be ignored. 0567 * 0568 * Set your client to use MQTT v5 immediately after it is created: 0569 * 0570 * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); 0571 * 0572 * Parameters: 0573 * mosq - a valid mosquitto instance. 0574 * host - the hostname or ip address of the broker to connect to. 0575 * port - the network port to connect to. Usually 1883. 0576 * keepalive - the number of seconds after which the client should send a PING 0577 * message to the broker if no other messages have been exchanged 0578 * in that time. 0579 * bind_address - the hostname or ip address of the local network interface to 0580 * bind to. If you do not want to bind to a specific interface, 0581 * set this to NULL. 0582 * properties - the MQTT 5 properties for the connect (not for the Will). 0583 * 0584 * Returns: 0585 * MOSQ_ERR_SUCCESS - on success. 0586 * MOSQ_ERR_INVAL - if the input parameters were invalid, which could be any of: 0587 * * mosq == NULL 0588 * * host == NULL 0589 * * port < 0 0590 * * keepalive < 5 0591 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 0592 * contains the error code, even on Windows. 0593 * Use strerror_r() where available or FormatMessage() on 0594 * Windows. 0595 * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden. 0596 * MOSQ_ERR_PROTOCOL - if any property is invalid for use with CONNECT. 0597 * 0598 * See Also: 0599 * <mosquitto_connect>, <mosquitto_connect_async>, <mosquitto_connect_bind_async> 0600 */ 0601 libmosq_EXPORT int mosquitto_connect_bind_v5(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address, const mosquitto_property *properties); 0602 0603 /* 0604 * Function: mosquitto_connect_async 0605 * 0606 * Connect to an MQTT broker. This is a non-blocking call. If you use 0607 * <mosquitto_connect_async> your client must use the threaded interface 0608 * <mosquitto_loop_start>. If you need to use <mosquitto_loop>, you must use 0609 * <mosquitto_connect> to connect the client. 0610 * 0611 * May be called before or after <mosquitto_loop_start>. 0612 * 0613 * Parameters: 0614 * mosq - a valid mosquitto instance. 0615 * host - the hostname or ip address of the broker to connect to. 0616 * port - the network port to connect to. Usually 1883. 0617 * keepalive - the number of seconds after which the client should send a PING 0618 * message to the broker if no other messages have been exchanged 0619 * in that time. 0620 * 0621 * Returns: 0622 * MOSQ_ERR_SUCCESS - on success. 0623 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0624 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 0625 * contains the error code, even on Windows. 0626 * Use strerror_r() where available or FormatMessage() on 0627 * Windows. 0628 * 0629 * See Also: 0630 * <mosquitto_connect_bind_async>, <mosquitto_connect>, <mosquitto_reconnect>, <mosquitto_disconnect>, <mosquitto_tls_set> 0631 */ 0632 libmosq_EXPORT int mosquitto_connect_async(struct mosquitto *mosq, const char *host, int port, int keepalive); 0633 0634 /* 0635 * Function: mosquitto_connect_bind_async 0636 * 0637 * Connect to an MQTT broker. This is a non-blocking call. If you use 0638 * <mosquitto_connect_bind_async> your client must use the threaded interface 0639 * <mosquitto_loop_start>. If you need to use <mosquitto_loop>, you must use 0640 * <mosquitto_connect> to connect the client. 0641 * 0642 * This extends the functionality of <mosquitto_connect_async> by adding the 0643 * bind_address parameter. Use this function if you need to restrict network 0644 * communication over a particular interface. 0645 * 0646 * May be called before or after <mosquitto_loop_start>. 0647 * 0648 * Parameters: 0649 * mosq - a valid mosquitto instance. 0650 * host - the hostname or ip address of the broker to connect to. 0651 * port - the network port to connect to. Usually 1883. 0652 * keepalive - the number of seconds after which the client should send a PING 0653 * message to the broker if no other messages have been exchanged 0654 * in that time. 0655 * bind_address - the hostname or ip address of the local network interface to 0656 * bind to. If you do not want to bind to a specific interface, 0657 * set this to NULL. 0658 * 0659 * Returns: 0660 * MOSQ_ERR_SUCCESS - on success. 0661 * MOSQ_ERR_INVAL - if the input parameters were invalid, which could be any of: 0662 * * mosq == NULL 0663 * * host == NULL 0664 * * port < 0 0665 * * keepalive < 5 0666 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 0667 * contains the error code, even on Windows. 0668 * Use strerror_r() where available or FormatMessage() on 0669 * Windows. 0670 * 0671 * See Also: 0672 * <mosquitto_connect_async>, <mosquitto_connect>, <mosquitto_connect_bind> 0673 */ 0674 libmosq_EXPORT int mosquitto_connect_bind_async(struct mosquitto *mosq, const char *host, int port, int keepalive, const char *bind_address); 0675 0676 /* 0677 * Function: mosquitto_connect_srv 0678 * 0679 * Connect to an MQTT broker. 0680 * 0681 * If you set `host` to `example.com`, then this call will attempt to retrieve 0682 * the DNS SRV record for `_secure-mqtt._tcp.example.com` or 0683 * `_mqtt._tcp.example.com` to discover which actual host to connect to. 0684 * 0685 * DNS SRV support is not usually compiled in to libmosquitto, use of this call 0686 * is not recommended. 0687 * 0688 * Parameters: 0689 * mosq - a valid mosquitto instance. 0690 * host - the hostname to search for an SRV record. 0691 * keepalive - the number of seconds after which the client should send a PING 0692 * message to the broker if no other messages have been exchanged 0693 * in that time. 0694 * bind_address - the hostname or ip address of the local network interface to 0695 * bind to. If you do not want to bind to a specific interface, 0696 * set this to NULL. 0697 * 0698 * Returns: 0699 * MOSQ_ERR_SUCCESS - on success. 0700 * MOSQ_ERR_INVAL - if the input parameters were invalid, which could be any of: 0701 * * mosq == NULL 0702 * * host == NULL 0703 * * port < 0 0704 * * keepalive < 5 0705 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 0706 * contains the error code, even on Windows. 0707 * Use strerror_r() where available or FormatMessage() on 0708 * Windows. 0709 * 0710 * See Also: 0711 * <mosquitto_connect_async>, <mosquitto_connect>, <mosquitto_connect_bind> 0712 */ 0713 libmosq_EXPORT int mosquitto_connect_srv(struct mosquitto *mosq, const char *host, int keepalive, const char *bind_address); 0714 0715 /* 0716 * Function: mosquitto_reconnect 0717 * 0718 * Reconnect to a broker. 0719 * 0720 * This function provides an easy way of reconnecting to a broker after a 0721 * connection has been lost. It uses the values that were provided in the 0722 * <mosquitto_connect> call. It must not be called before 0723 * <mosquitto_connect>. 0724 * 0725 * Parameters: 0726 * mosq - a valid mosquitto instance. 0727 * 0728 * Returns: 0729 * MOSQ_ERR_SUCCESS - on success. 0730 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0731 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0732 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 0733 * contains the error code, even on Windows. 0734 * Use strerror_r() where available or FormatMessage() on 0735 * Windows. 0736 * 0737 * See Also: 0738 * <mosquitto_connect>, <mosquitto_disconnect>, <mosquitto_reconnect_async> 0739 */ 0740 libmosq_EXPORT int mosquitto_reconnect(struct mosquitto *mosq); 0741 0742 /* 0743 * Function: mosquitto_reconnect_async 0744 * 0745 * Reconnect to a broker. Non blocking version of <mosquitto_reconnect>. 0746 * 0747 * This function provides an easy way of reconnecting to a broker after a 0748 * connection has been lost. It uses the values that were provided in the 0749 * <mosquitto_connect> or <mosquitto_connect_async> calls. It must not be 0750 * called before <mosquitto_connect>. 0751 * 0752 * Parameters: 0753 * mosq - a valid mosquitto instance. 0754 * 0755 * Returns: 0756 * MOSQ_ERR_SUCCESS - on success. 0757 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0758 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0759 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 0760 * contains the error code, even on Windows. 0761 * Use strerror_r() where available or FormatMessage() on 0762 * Windows. 0763 * 0764 * See Also: 0765 * <mosquitto_connect>, <mosquitto_disconnect> 0766 */ 0767 libmosq_EXPORT int mosquitto_reconnect_async(struct mosquitto *mosq); 0768 0769 /* 0770 * Function: mosquitto_disconnect 0771 * 0772 * Disconnect from the broker. 0773 * 0774 * It is valid to use this function for clients using all MQTT protocol versions. 0775 * If you need to set MQTT v5 DISCONNECT properties, use 0776 * <mosquitto_disconnect_v5> instead. 0777 * 0778 * Parameters: 0779 * mosq - a valid mosquitto instance. 0780 * 0781 * Returns: 0782 * MOSQ_ERR_SUCCESS - on success. 0783 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0784 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 0785 */ 0786 libmosq_EXPORT int mosquitto_disconnect(struct mosquitto *mosq); 0787 0788 /* 0789 * Function: mosquitto_disconnect_v5 0790 * 0791 * Disconnect from the broker, with attached MQTT properties. 0792 * 0793 * Use e.g. <mosquitto_property_add_string> and similar to create a list of 0794 * properties, then attach them to this publish. Properties need freeing with 0795 * <mosquitto_property_free_all>. 0796 * 0797 * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument 0798 * will be applied to the DISCONNECT message. For MQTT v3.1.1 and below, the 0799 * `properties` argument will be ignored. 0800 * 0801 * Set your client to use MQTT v5 immediately after it is created: 0802 * 0803 * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); 0804 * 0805 * Parameters: 0806 * mosq - a valid mosquitto instance. 0807 * reason_code - the disconnect reason code. 0808 * properties - a valid mosquitto_property list, or NULL. 0809 * 0810 * Returns: 0811 * MOSQ_ERR_SUCCESS - on success. 0812 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0813 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 0814 * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden. 0815 * MOSQ_ERR_PROTOCOL - if any property is invalid for use with DISCONNECT. 0816 */ 0817 libmosq_EXPORT int mosquitto_disconnect_v5(struct mosquitto *mosq, int reason_code, const mosquitto_property *properties); 0818 0819 0820 /* ====================================================================== 0821 * 0822 * Section: Publishing, subscribing, unsubscribing 0823 * 0824 * ====================================================================== */ 0825 /* 0826 * Function: mosquitto_publish 0827 * 0828 * Publish a message on a given topic. 0829 * 0830 * It is valid to use this function for clients using all MQTT protocol versions. 0831 * If you need to set MQTT v5 PUBLISH properties, use <mosquitto_publish_v5> 0832 * instead. 0833 * 0834 * Parameters: 0835 * mosq - a valid mosquitto instance. 0836 * mid - pointer to an int. If not NULL, the function will set this 0837 * to the message id of this particular message. This can be then 0838 * used with the publish callback to determine when the message 0839 * has been sent. 0840 * Note that although the MQTT protocol doesn't use message ids 0841 * for messages with QoS=0, libmosquitto assigns them message ids 0842 * so they can be tracked with this parameter. 0843 * topic - null terminated string of the topic to publish to. 0844 * payloadlen - the size of the payload (bytes). Valid values are between 0 and 0845 * 268,435,455. 0846 * payload - pointer to the data to send. If payloadlen > 0 this must be a 0847 * valid memory location. 0848 * qos - integer value 0, 1 or 2 indicating the Quality of Service to be 0849 * used for the message. 0850 * retain - set to true to make the message retained. 0851 * 0852 * Returns: 0853 * MOSQ_ERR_SUCCESS - on success. 0854 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0855 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0856 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 0857 * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 0858 * broker. 0859 * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large. 0860 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 0861 * MOSQ_ERR_QOS_NOT_SUPPORTED - if the QoS is greater than that supported by 0862 * the broker. 0863 * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than 0864 * supported by the broker. 0865 * 0866 * See Also: 0867 * <mosquitto_max_inflight_messages_set> 0868 */ 0869 libmosq_EXPORT int mosquitto_publish(struct mosquitto *mosq, int *mid, const char *topic, int payloadlen, const void *payload, int qos, bool retain); 0870 0871 0872 /* 0873 * Function: mosquitto_publish_v5 0874 * 0875 * Publish a message on a given topic, with attached MQTT properties. 0876 * 0877 * Use e.g. <mosquitto_property_add_string> and similar to create a list of 0878 * properties, then attach them to this publish. Properties need freeing with 0879 * <mosquitto_property_free_all>. 0880 * 0881 * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument 0882 * will be applied to the PUBLISH message. For MQTT v3.1.1 and below, the 0883 * `properties` argument will be ignored. 0884 * 0885 * Set your client to use MQTT v5 immediately after it is created: 0886 * 0887 * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); 0888 * 0889 * Parameters: 0890 * mosq - a valid mosquitto instance. 0891 * mid - pointer to an int. If not NULL, the function will set this 0892 * to the message id of this particular message. This can be then 0893 * used with the publish callback to determine when the message 0894 * has been sent. 0895 * Note that although the MQTT protocol doesn't use message ids 0896 * for messages with QoS=0, libmosquitto assigns them message ids 0897 * so they can be tracked with this parameter. 0898 * topic - null terminated string of the topic to publish to. 0899 * payloadlen - the size of the payload (bytes). Valid values are between 0 and 0900 * 268,435,455. 0901 * payload - pointer to the data to send. If payloadlen > 0 this must be a 0902 * valid memory location. 0903 * qos - integer value 0, 1 or 2 indicating the Quality of Service to be 0904 * used for the message. 0905 * retain - set to true to make the message retained. 0906 * properties - a valid mosquitto_property list, or NULL. 0907 * 0908 * Returns: 0909 * MOSQ_ERR_SUCCESS - on success. 0910 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0911 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0912 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 0913 * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 0914 * broker. 0915 * MOSQ_ERR_PAYLOAD_SIZE - if payloadlen is too large. 0916 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 0917 * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden. 0918 * MOSQ_ERR_PROTOCOL - if any property is invalid for use with PUBLISH. 0919 * MOSQ_ERR_QOS_NOT_SUPPORTED - if the QoS is greater than that supported by 0920 * the broker. 0921 * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than 0922 * supported by the broker. 0923 */ 0924 libmosq_EXPORT int mosquitto_publish_v5( 0925 struct mosquitto *mosq, 0926 int *mid, 0927 const char *topic, 0928 int payloadlen, 0929 const void *payload, 0930 int qos, 0931 bool retain, 0932 const mosquitto_property *properties); 0933 0934 0935 /* 0936 * Function: mosquitto_subscribe 0937 * 0938 * Subscribe to a topic. 0939 * 0940 * It is valid to use this function for clients using all MQTT protocol versions. 0941 * If you need to set MQTT v5 SUBSCRIBE properties, use <mosquitto_subscribe_v5> 0942 * instead. 0943 * 0944 * Parameters: 0945 * mosq - a valid mosquitto instance. 0946 * mid - a pointer to an int. If not NULL, the function will set this to 0947 * the message id of this particular message. This can be then used 0948 * with the subscribe callback to determine when the message has been 0949 * sent. 0950 * sub - the subscription pattern. 0951 * qos - the requested Quality of Service for this subscription. 0952 * 0953 * Returns: 0954 * MOSQ_ERR_SUCCESS - on success. 0955 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0956 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0957 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 0958 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 0959 * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than 0960 * supported by the broker. 0961 */ 0962 libmosq_EXPORT int mosquitto_subscribe(struct mosquitto *mosq, int *mid, const char *sub, int qos); 0963 0964 /* 0965 * Function: mosquitto_subscribe_v5 0966 * 0967 * Subscribe to a topic, with attached MQTT properties. 0968 * 0969 * Use e.g. <mosquitto_property_add_string> and similar to create a list of 0970 * properties, then attach them to this publish. Properties need freeing with 0971 * <mosquitto_property_free_all>. 0972 * 0973 * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument 0974 * will be applied to the PUBLISH message. For MQTT v3.1.1 and below, the 0975 * `properties` argument will be ignored. 0976 * 0977 * Set your client to use MQTT v5 immediately after it is created: 0978 * 0979 * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); 0980 * 0981 * Parameters: 0982 * mosq - a valid mosquitto instance. 0983 * mid - a pointer to an int. If not NULL, the function will set this to 0984 * the message id of this particular message. This can be then used 0985 * with the subscribe callback to determine when the message has been 0986 * sent. 0987 * sub - the subscription pattern. 0988 * qos - the requested Quality of Service for this subscription. 0989 * options - options to apply to this subscription, OR'd together. Set to 0 to 0990 * use the default options, otherwise choose from list of <mqtt5_sub_options> 0991 * properties - a valid mosquitto_property list, or NULL. 0992 * 0993 * Returns: 0994 * MOSQ_ERR_SUCCESS - on success. 0995 * MOSQ_ERR_INVAL - if the input parameters were invalid. 0996 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 0997 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 0998 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 0999 * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden. 1000 * MOSQ_ERR_PROTOCOL - if any property is invalid for use with SUBSCRIBE. 1001 * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than 1002 * supported by the broker. 1003 */ 1004 libmosq_EXPORT int mosquitto_subscribe_v5(struct mosquitto *mosq, int *mid, const char *sub, int qos, int options, const mosquitto_property *properties); 1005 1006 /* 1007 * Function: mosquitto_subscribe_multiple 1008 * 1009 * Subscribe to multiple topics. 1010 * 1011 * Parameters: 1012 * mosq - a valid mosquitto instance. 1013 * mid - a pointer to an int. If not NULL, the function will set this to 1014 * the message id of this particular message. This can be then used 1015 * with the subscribe callback to determine when the message has been 1016 * sent. 1017 * sub_count - the count of subscriptions to be made 1018 * sub - array of sub_count pointers, each pointing to a subscription string. 1019 * The "char *const *const" datatype ensures that neither the array of 1020 * pointers nor the strings that they point to are mutable. If you aren't 1021 * familiar with this, just think of it as a safer "char **", 1022 * equivalent to "const char *" for a simple string pointer. 1023 * qos - the requested Quality of Service for each subscription. 1024 * options - options to apply to this subscription, OR'd together. This 1025 * argument is not used for MQTT v3 susbcriptions. Set to 0 to use 1026 * the default options, otherwise choose from list of <mqtt5_sub_options> 1027 * properties - a valid mosquitto_property list, or NULL. Only used with MQTT 1028 * v5 clients. 1029 * 1030 * Returns: 1031 * MOSQ_ERR_SUCCESS - on success. 1032 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1033 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1034 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1035 * MOSQ_ERR_MALFORMED_UTF8 - if a topic is not valid UTF-8 1036 * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than 1037 * supported by the broker. 1038 */ 1039 libmosq_EXPORT int mosquitto_subscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count, char *const *const sub, int qos, int options, const mosquitto_property *properties); 1040 1041 /* 1042 * Function: mosquitto_unsubscribe 1043 * 1044 * Unsubscribe from a topic. 1045 * 1046 * Parameters: 1047 * mosq - a valid mosquitto instance. 1048 * mid - a pointer to an int. If not NULL, the function will set this to 1049 * the message id of this particular message. This can be then used 1050 * with the unsubscribe callback to determine when the message has been 1051 * sent. 1052 * sub - the unsubscription pattern. 1053 * 1054 * Returns: 1055 * MOSQ_ERR_SUCCESS - on success. 1056 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1057 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1058 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1059 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 1060 * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than 1061 * supported by the broker. 1062 */ 1063 libmosq_EXPORT int mosquitto_unsubscribe(struct mosquitto *mosq, int *mid, const char *sub); 1064 1065 /* 1066 * Function: mosquitto_unsubscribe_v5 1067 * 1068 * Unsubscribe from a topic, with attached MQTT properties. 1069 * 1070 * It is valid to use this function for clients using all MQTT protocol versions. 1071 * If you need to set MQTT v5 UNSUBSCRIBE properties, use 1072 * <mosquitto_unsubscribe_v5> instead. 1073 * 1074 * Use e.g. <mosquitto_property_add_string> and similar to create a list of 1075 * properties, then attach them to this publish. Properties need freeing with 1076 * <mosquitto_property_free_all>. 1077 * 1078 * If the mosquitto instance `mosq` is using MQTT v5, the `properties` argument 1079 * will be applied to the PUBLISH message. For MQTT v3.1.1 and below, the 1080 * `properties` argument will be ignored. 1081 * 1082 * Set your client to use MQTT v5 immediately after it is created: 1083 * 1084 * mosquitto_int_option(mosq, MOSQ_OPT_PROTOCOL_VERSION, MQTT_PROTOCOL_V5); 1085 * 1086 * Parameters: 1087 * mosq - a valid mosquitto instance. 1088 * mid - a pointer to an int. If not NULL, the function will set this to 1089 * the message id of this particular message. This can be then used 1090 * with the unsubscribe callback to determine when the message has been 1091 * sent. 1092 * sub - the unsubscription pattern. 1093 * properties - a valid mosquitto_property list, or NULL. Only used with MQTT 1094 * v5 clients. 1095 * 1096 * Returns: 1097 * MOSQ_ERR_SUCCESS - on success. 1098 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1099 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1100 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1101 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 1102 * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden. 1103 * MOSQ_ERR_PROTOCOL - if any property is invalid for use with UNSUBSCRIBE. 1104 * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than 1105 * supported by the broker. 1106 */ 1107 libmosq_EXPORT int mosquitto_unsubscribe_v5(struct mosquitto *mosq, int *mid, const char *sub, const mosquitto_property *properties); 1108 1109 /* 1110 * Function: mosquitto_unsubscribe_multiple 1111 * 1112 * Unsubscribe from multiple topics. 1113 * 1114 * Parameters: 1115 * mosq - a valid mosquitto instance. 1116 * mid - a pointer to an int. If not NULL, the function will set this to 1117 * the message id of this particular message. This can be then used 1118 * with the subscribe callback to determine when the message has been 1119 * sent. 1120 * sub_count - the count of unsubscriptions to be made 1121 * sub - array of sub_count pointers, each pointing to an unsubscription string. 1122 * The "char *const *const" datatype ensures that neither the array of 1123 * pointers nor the strings that they point to are mutable. If you aren't 1124 * familiar with this, just think of it as a safer "char **", 1125 * equivalent to "const char *" for a simple string pointer. 1126 * properties - a valid mosquitto_property list, or NULL. Only used with MQTT 1127 * v5 clients. 1128 * 1129 * Returns: 1130 * MOSQ_ERR_SUCCESS - on success. 1131 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1132 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1133 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1134 * MOSQ_ERR_MALFORMED_UTF8 - if a topic is not valid UTF-8 1135 * MOSQ_ERR_OVERSIZE_PACKET - if the resulting packet would be larger than 1136 * supported by the broker. 1137 */ 1138 libmosq_EXPORT int mosquitto_unsubscribe_multiple(struct mosquitto *mosq, int *mid, int sub_count, char *const *const sub, const mosquitto_property *properties); 1139 1140 1141 /* ====================================================================== 1142 * 1143 * Section: Struct mosquitto_message helper functions 1144 * 1145 * ====================================================================== */ 1146 /* 1147 * Function: mosquitto_message_copy 1148 * 1149 * Copy the contents of a mosquitto message to another message. 1150 * Useful for preserving a message received in the on_message() callback. 1151 * 1152 * Parameters: 1153 * dst - a pointer to a valid mosquitto_message struct to copy to. 1154 * src - a pointer to a valid mosquitto_message struct to copy from. 1155 * 1156 * Returns: 1157 * MOSQ_ERR_SUCCESS - on success. 1158 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1159 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1160 * 1161 * See Also: 1162 * <mosquitto_message_free> 1163 */ 1164 libmosq_EXPORT int mosquitto_message_copy(struct mosquitto_message *dst, const struct mosquitto_message *src); 1165 1166 /* 1167 * Function: mosquitto_message_free 1168 * 1169 * Completely free a mosquitto_message struct. 1170 * 1171 * Parameters: 1172 * message - pointer to a mosquitto_message pointer to free. 1173 * 1174 * See Also: 1175 * <mosquitto_message_copy>, <mosquitto_message_free_contents> 1176 */ 1177 libmosq_EXPORT void mosquitto_message_free(struct mosquitto_message **message); 1178 1179 /* 1180 * Function: mosquitto_message_free_contents 1181 * 1182 * Free a mosquitto_message struct contents, leaving the struct unaffected. 1183 * 1184 * Parameters: 1185 * message - pointer to a mosquitto_message struct to free its contents. 1186 * 1187 * See Also: 1188 * <mosquitto_message_copy>, <mosquitto_message_free> 1189 */ 1190 libmosq_EXPORT void mosquitto_message_free_contents(struct mosquitto_message *message); 1191 1192 1193 /* ====================================================================== 1194 * 1195 * Section: Network loop (managed by libmosquitto) 1196 * 1197 * The internal network loop must be called at a regular interval. The two 1198 * recommended approaches are to use either <mosquitto_loop_forever> or 1199 * <mosquitto_loop_start>. <mosquitto_loop_forever> is a blocking call and is 1200 * suitable for the situation where you only want to handle incoming messages 1201 * in callbacks. <mosquitto_loop_start> is a non-blocking call, it creates a 1202 * separate thread to run the loop for you. Use this function when you have 1203 * other tasks you need to run at the same time as the MQTT client, e.g. 1204 * reading data from a sensor. 1205 * 1206 * ====================================================================== */ 1207 1208 /* 1209 * Function: mosquitto_loop_forever 1210 * 1211 * This function call loop() for you in an infinite blocking loop. It is useful 1212 * for the case where you only want to run the MQTT client loop in your 1213 * program. 1214 * 1215 * It handles reconnecting in case server connection is lost. If you call 1216 * mosquitto_disconnect() in a callback it will return. 1217 * 1218 * Parameters: 1219 * mosq - a valid mosquitto instance. 1220 * timeout - Maximum number of milliseconds to wait for network activity 1221 * in the select() call before timing out. Set to 0 for instant 1222 * return. Set negative to use the default of 1000ms. 1223 * max_packets - this parameter is currently unused and should be set to 1 for 1224 * future compatibility. 1225 * 1226 * Returns: 1227 * MOSQ_ERR_SUCCESS - on success. 1228 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1229 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1230 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1231 * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost. 1232 * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 1233 * broker. 1234 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 1235 * contains the error code, even on Windows. 1236 * Use strerror_r() where available or FormatMessage() on 1237 * Windows. 1238 * 1239 * See Also: 1240 * <mosquitto_loop>, <mosquitto_loop_start> 1241 */ 1242 libmosq_EXPORT int mosquitto_loop_forever(struct mosquitto *mosq, int timeout, int max_packets); 1243 1244 /* 1245 * Function: mosquitto_loop_start 1246 * 1247 * This is part of the threaded client interface. Call this once to start a new 1248 * thread to process network traffic. This provides an alternative to 1249 * repeatedly calling <mosquitto_loop> yourself. 1250 * 1251 * Parameters: 1252 * mosq - a valid mosquitto instance. 1253 * 1254 * Returns: 1255 * MOSQ_ERR_SUCCESS - on success. 1256 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1257 * MOSQ_ERR_NOT_SUPPORTED - if thread support is not available. 1258 * 1259 * See Also: 1260 * <mosquitto_connect_async>, <mosquitto_loop>, <mosquitto_loop_forever>, <mosquitto_loop_stop> 1261 */ 1262 libmosq_EXPORT int mosquitto_loop_start(struct mosquitto *mosq); 1263 1264 /* 1265 * Function: mosquitto_loop_stop 1266 * 1267 * This is part of the threaded client interface. Call this once to stop the 1268 * network thread previously created with <mosquitto_loop_start>. This call 1269 * will block until the network thread finishes. For the network thread to end, 1270 * you must have previously called <mosquitto_disconnect> or have set the force 1271 * parameter to true. 1272 * 1273 * Parameters: 1274 * mosq - a valid mosquitto instance. 1275 * force - set to true to force thread cancellation. If false, 1276 * <mosquitto_disconnect> must have already been called. 1277 * 1278 * Returns: 1279 * MOSQ_ERR_SUCCESS - on success. 1280 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1281 * MOSQ_ERR_NOT_SUPPORTED - if thread support is not available. 1282 * 1283 * See Also: 1284 * <mosquitto_loop>, <mosquitto_loop_start> 1285 */ 1286 libmosq_EXPORT int mosquitto_loop_stop(struct mosquitto *mosq, bool force); 1287 1288 /* 1289 * Function: mosquitto_loop 1290 * 1291 * The main network loop for the client. This must be called frequently 1292 * to keep communications between the client and broker working. This is 1293 * carried out by <mosquitto_loop_forever> and <mosquitto_loop_start>, which 1294 * are the recommended ways of handling the network loop. You may also use this 1295 * function if you wish. It must not be called inside a callback. 1296 * 1297 * If incoming data is present it will then be processed. Outgoing commands, 1298 * from e.g. <mosquitto_publish>, are normally sent immediately that their 1299 * function is called, but this is not always possible. <mosquitto_loop> will 1300 * also attempt to send any remaining outgoing messages, which also includes 1301 * commands that are part of the flow for messages with QoS>0. 1302 * 1303 * This calls select() to monitor the client network socket. If you want to 1304 * integrate mosquitto client operation with your own select() call, use 1305 * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write> and 1306 * <mosquitto_loop_misc>. 1307 * 1308 * Threads: 1309 * 1310 * Parameters: 1311 * mosq - a valid mosquitto instance. 1312 * timeout - Maximum number of milliseconds to wait for network activity 1313 * in the select() call before timing out. Set to 0 for instant 1314 * return. Set negative to use the default of 1000ms. 1315 * max_packets - this parameter is currently unused and should be set to 1 for 1316 * future compatibility. 1317 * 1318 * Returns: 1319 * MOSQ_ERR_SUCCESS - on success. 1320 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1321 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1322 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1323 * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost. 1324 * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 1325 * broker. 1326 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 1327 * contains the error code, even on Windows. 1328 * Use strerror_r() where available or FormatMessage() on 1329 * Windows. 1330 * See Also: 1331 * <mosquitto_loop_forever>, <mosquitto_loop_start>, <mosquitto_loop_stop> 1332 */ 1333 libmosq_EXPORT int mosquitto_loop(struct mosquitto *mosq, int timeout, int max_packets); 1334 1335 /* ====================================================================== 1336 * 1337 * Section: Network loop (for use in other event loops) 1338 * 1339 * ====================================================================== */ 1340 /* 1341 * Function: mosquitto_loop_read 1342 * 1343 * Carry out network read operations. 1344 * This should only be used if you are not using mosquitto_loop() and are 1345 * monitoring the client network socket for activity yourself. 1346 * 1347 * Parameters: 1348 * mosq - a valid mosquitto instance. 1349 * max_packets - this parameter is currently unused and should be set to 1 for 1350 * future compatibility. 1351 * 1352 * Returns: 1353 * MOSQ_ERR_SUCCESS - on success. 1354 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1355 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1356 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1357 * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost. 1358 * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 1359 * broker. 1360 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 1361 * contains the error code, even on Windows. 1362 * Use strerror_r() where available or FormatMessage() on 1363 * Windows. 1364 * 1365 * See Also: 1366 * <mosquitto_socket>, <mosquitto_loop_write>, <mosquitto_loop_misc> 1367 */ 1368 libmosq_EXPORT int mosquitto_loop_read(struct mosquitto *mosq, int max_packets); 1369 1370 /* 1371 * Function: mosquitto_loop_write 1372 * 1373 * Carry out network write operations. 1374 * This should only be used if you are not using mosquitto_loop() and are 1375 * monitoring the client network socket for activity yourself. 1376 * 1377 * Parameters: 1378 * mosq - a valid mosquitto instance. 1379 * max_packets - this parameter is currently unused and should be set to 1 for 1380 * future compatibility. 1381 * 1382 * Returns: 1383 * MOSQ_ERR_SUCCESS - on success. 1384 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1385 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1386 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1387 * MOSQ_ERR_CONN_LOST - if the connection to the broker was lost. 1388 * MOSQ_ERR_PROTOCOL - if there is a protocol error communicating with the 1389 * broker. 1390 * MOSQ_ERR_ERRNO - if a system call returned an error. The variable errno 1391 * contains the error code, even on Windows. 1392 * Use strerror_r() where available or FormatMessage() on 1393 * Windows. 1394 * 1395 * See Also: 1396 * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_misc>, <mosquitto_want_write> 1397 */ 1398 libmosq_EXPORT int mosquitto_loop_write(struct mosquitto *mosq, int max_packets); 1399 1400 /* 1401 * Function: mosquitto_loop_misc 1402 * 1403 * Carry out miscellaneous operations required as part of the network loop. 1404 * This should only be used if you are not using mosquitto_loop() and are 1405 * monitoring the client network socket for activity yourself. 1406 * 1407 * This function deals with handling PINGs and checking whether messages need 1408 * to be retried, so should be called fairly frequently, around once per second 1409 * is sufficient. 1410 * 1411 * Parameters: 1412 * mosq - a valid mosquitto instance. 1413 * 1414 * Returns: 1415 * MOSQ_ERR_SUCCESS - on success. 1416 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1417 * MOSQ_ERR_NO_CONN - if the client isn't connected to a broker. 1418 * 1419 * See Also: 1420 * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write> 1421 */ 1422 libmosq_EXPORT int mosquitto_loop_misc(struct mosquitto *mosq); 1423 1424 1425 /* ====================================================================== 1426 * 1427 * Section: Network loop (helper functions) 1428 * 1429 * ====================================================================== */ 1430 /* 1431 * Function: mosquitto_socket 1432 * 1433 * Return the socket handle for a mosquitto instance. Useful if you want to 1434 * include a mosquitto client in your own select() calls. 1435 * 1436 * Parameters: 1437 * mosq - a valid mosquitto instance. 1438 * 1439 * Returns: 1440 * The socket for the mosquitto client or -1 on failure. 1441 */ 1442 libmosq_EXPORT int mosquitto_socket(struct mosquitto *mosq); 1443 1444 /* 1445 * Function: mosquitto_want_write 1446 * 1447 * Returns true if there is data ready to be written on the socket. 1448 * 1449 * Parameters: 1450 * mosq - a valid mosquitto instance. 1451 * 1452 * See Also: 1453 * <mosquitto_socket>, <mosquitto_loop_read>, <mosquitto_loop_write> 1454 */ 1455 libmosq_EXPORT bool mosquitto_want_write(struct mosquitto *mosq); 1456 1457 /* 1458 * Function: mosquitto_threaded_set 1459 * 1460 * Used to tell the library that your application is using threads, but not 1461 * using <mosquitto_loop_start>. The library operates slightly differently when 1462 * not in threaded mode in order to simplify its operation. If you are managing 1463 * your own threads and do not use this function you will experience crashes 1464 * due to race conditions. 1465 * 1466 * When using <mosquitto_loop_start>, this is set automatically. 1467 * 1468 * Parameters: 1469 * mosq - a valid mosquitto instance. 1470 * threaded - true if your application is using threads, false otherwise. 1471 */ 1472 libmosq_EXPORT int mosquitto_threaded_set(struct mosquitto *mosq, bool threaded); 1473 1474 1475 /* ====================================================================== 1476 * 1477 * Section: Client options 1478 * 1479 * ====================================================================== */ 1480 /* 1481 * Function: mosquitto_opts_set 1482 * 1483 * Used to set options for the client. 1484 * 1485 * This function is deprecated, the replacement <mosquitto_int_option>, 1486 * <mosquitto_string_option> and <mosquitto_void_option> functions should 1487 * be used instead. 1488 * 1489 * Parameters: 1490 * mosq - a valid mosquitto instance. 1491 * option - the option to set. 1492 * value - the option specific value. 1493 * 1494 * Options: 1495 * MOSQ_OPT_PROTOCOL_VERSION - Value must be an int, set to either 1496 * MQTT_PROTOCOL_V31 or MQTT_PROTOCOL_V311. Must be set 1497 * before the client connects. 1498 * Defaults to MQTT_PROTOCOL_V31. 1499 * 1500 * MOSQ_OPT_SSL_CTX - Pass an openssl SSL_CTX to be used when creating 1501 * TLS connections rather than libmosquitto creating its own. 1502 * This must be called before connecting to have any effect. 1503 * If you use this option, the onus is on you to ensure that 1504 * you are using secure settings. 1505 * Setting to NULL means that libmosquitto will use its own SSL_CTX 1506 * if TLS is to be used. 1507 * This option is only available for openssl 1.1.0 and higher. 1508 * 1509 * MOSQ_OPT_SSL_CTX_WITH_DEFAULTS - Value must be an int set to 1 or 0. 1510 * If set to 1, then the user specified SSL_CTX passed in using 1511 * MOSQ_OPT_SSL_CTX will have the default options applied to it. 1512 * This means that you only need to change the values that are 1513 * relevant to you. If you use this option then you must configure 1514 * the TLS options as normal, i.e. you should use 1515 * <mosquitto_tls_set> to configure the cafile/capath as a minimum. 1516 * This option is only available for openssl 1.1.0 and higher. 1517 */ 1518 libmosq_EXPORT int mosquitto_opts_set(struct mosquitto *mosq, enum mosq_opt_t option, void *value); 1519 1520 /* 1521 * Function: mosquitto_int_option 1522 * 1523 * Used to set integer options for the client. 1524 * 1525 * Parameters: 1526 * mosq - a valid mosquitto instance. 1527 * option - the option to set. 1528 * value - the option specific value. 1529 * 1530 * Options: 1531 * MOSQ_OPT_TCP_NODELAY - Set to 1 to disable Nagle's algorithm on client 1532 * sockets. This has the effect of reducing latency of individual 1533 * messages at the potential cost of increasing the number of 1534 * packets being sent. 1535 * Defaults to 0, which means Nagle remains enabled. 1536 * 1537 * MOSQ_OPT_PROTOCOL_VERSION - Value must be set to either MQTT_PROTOCOL_V31, 1538 * MQTT_PROTOCOL_V311, or MQTT_PROTOCOL_V5. Must be set before the 1539 * client connects. Defaults to MQTT_PROTOCOL_V311. 1540 * 1541 * MOSQ_OPT_RECEIVE_MAXIMUM - Value can be set between 1 and 65535 inclusive, 1542 * and represents the maximum number of incoming QoS 1 and QoS 2 1543 * messages that this client wants to process at once. Defaults to 1544 * 20. This option is not valid for MQTT v3.1 or v3.1.1 clients. 1545 * Note that if the MQTT_PROP_RECEIVE_MAXIMUM property is in the 1546 * proplist passed to mosquitto_connect_v5(), then that property 1547 * will override this option. Using this option is the recommended 1548 * method however. 1549 * 1550 * MOSQ_OPT_SEND_MAXIMUM - Value can be set between 1 and 65535 inclusive, 1551 * and represents the maximum number of outgoing QoS 1 and QoS 2 1552 * messages that this client will attempt to have "in flight" at 1553 * once. Defaults to 20. 1554 * This option is not valid for MQTT v3.1 or v3.1.1 clients. 1555 * Note that if the broker being connected to sends a 1556 * MQTT_PROP_RECEIVE_MAXIMUM property that has a lower value than 1557 * this option, then the broker provided value will be used. 1558 * 1559 * MOSQ_OPT_SSL_CTX_WITH_DEFAULTS - If value is set to a non zero value, 1560 * then the user specified SSL_CTX passed in using MOSQ_OPT_SSL_CTX 1561 * will have the default options applied to it. This means that 1562 * you only need to change the values that are relevant to you. 1563 * If you use this option then you must configure the TLS options 1564 * as normal, i.e. you should use <mosquitto_tls_set> to 1565 * configure the cafile/capath as a minimum. 1566 * This option is only available for openssl 1.1.0 and higher. 1567 * 1568 * MOSQ_OPT_TLS_OCSP_REQUIRED - Set whether OCSP checking on TLS 1569 * connections is required. Set to 1 to enable checking, 1570 * or 0 (the default) for no checking. 1571 * 1572 * MOSQ_OPT_TLS_USE_OS_CERTS - Set to 1 to instruct the client to load and 1573 * trust OS provided CA certificates for use with TLS connections. 1574 * Set to 0 (the default) to only use manually specified CA certs. 1575 */ 1576 libmosq_EXPORT int mosquitto_int_option(struct mosquitto *mosq, enum mosq_opt_t option, int value); 1577 1578 1579 /* 1580 * Function: mosquitto_string_option 1581 * 1582 * Used to set const char* options for the client. 1583 * 1584 * Parameters: 1585 * mosq - a valid mosquitto instance. 1586 * option - the option to set. 1587 * value - the option specific value. 1588 * 1589 * Options: 1590 * MOSQ_OPT_TLS_ENGINE - Configure the client for TLS Engine support. 1591 * Pass a TLS Engine ID to be used when creating TLS 1592 * connections. Must be set before <mosquitto_connect>. 1593 * Must be a valid engine, and note that the string will not be used 1594 * until a connection attempt is made so this function will return 1595 * success even if an invalid engine string is passed. 1596 * 1597 * MOSQ_OPT_TLS_KEYFORM - Configure the client to treat the keyfile 1598 * differently depending on its type. Must be set 1599 * before <mosquitto_connect>. 1600 * Set as either "pem" or "engine", to determine from where the 1601 * private key for a TLS connection will be obtained. Defaults to 1602 * "pem", a normal private key file. 1603 * 1604 * MOSQ_OPT_TLS_KPASS_SHA1 - Where the TLS Engine requires the use of 1605 * a password to be accessed, this option allows a hex encoded 1606 * SHA1 hash of the private key password to be passed to the 1607 * engine directly. Must be set before <mosquitto_connect>. 1608 * 1609 * MOSQ_OPT_TLS_ALPN - If the broker being connected to has multiple 1610 * services available on a single TLS port, such as both MQTT 1611 * and WebSockets, use this option to configure the ALPN 1612 * option for the connection. 1613 * 1614 * MOSQ_OPT_BIND_ADDRESS - Set the hostname or ip address of the local network 1615 * interface to bind to when connecting. 1616 */ 1617 libmosq_EXPORT int mosquitto_string_option(struct mosquitto *mosq, enum mosq_opt_t option, const char *value); 1618 1619 1620 /* 1621 * Function: mosquitto_void_option 1622 * 1623 * Used to set void* options for the client. 1624 * 1625 * Parameters: 1626 * mosq - a valid mosquitto instance. 1627 * option - the option to set. 1628 * value - the option specific value. 1629 * 1630 * Options: 1631 * MOSQ_OPT_SSL_CTX - Pass an openssl SSL_CTX to be used when creating TLS 1632 * connections rather than libmosquitto creating its own. This must 1633 * be called before connecting to have any effect. If you use this 1634 * option, the onus is on you to ensure that you are using secure 1635 * settings. 1636 * Setting to NULL means that libmosquitto will use its own SSL_CTX 1637 * if TLS is to be used. 1638 * This option is only available for openssl 1.1.0 and higher. 1639 */ 1640 libmosq_EXPORT int mosquitto_void_option(struct mosquitto *mosq, enum mosq_opt_t option, void *value); 1641 1642 /* 1643 * Function: mosquitto_reconnect_delay_set 1644 * 1645 * Control the behaviour of the client when it has unexpectedly disconnected in 1646 * <mosquitto_loop_forever> or after <mosquitto_loop_start>. The default 1647 * behaviour if this function is not used is to repeatedly attempt to reconnect 1648 * with a delay of 1 second until the connection succeeds. 1649 * 1650 * Use reconnect_delay parameter to change the delay between successive 1651 * reconnection attempts. You may also enable exponential backoff of the time 1652 * between reconnections by setting reconnect_exponential_backoff to true and 1653 * set an upper bound on the delay with reconnect_delay_max. 1654 * 1655 * Example 1: 1656 * delay=2, delay_max=10, exponential_backoff=False 1657 * Delays would be: 2, 4, 6, 8, 10, 10, ... 1658 * 1659 * Example 2: 1660 * delay=3, delay_max=30, exponential_backoff=True 1661 * Delays would be: 3, 6, 12, 24, 30, 30, ... 1662 * 1663 * Parameters: 1664 * mosq - a valid mosquitto instance. 1665 * reconnect_delay - the number of seconds to wait between 1666 * reconnects. 1667 * reconnect_delay_max - the maximum number of seconds to wait 1668 * between reconnects. 1669 * reconnect_exponential_backoff - use exponential backoff between 1670 * reconnect attempts. Set to true to enable 1671 * exponential backoff. 1672 * 1673 * Returns: 1674 * MOSQ_ERR_SUCCESS - on success. 1675 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1676 */ 1677 libmosq_EXPORT int mosquitto_reconnect_delay_set(struct mosquitto *mosq, unsigned int reconnect_delay, unsigned int reconnect_delay_max, bool reconnect_exponential_backoff); 1678 1679 /* 1680 * Function: mosquitto_max_inflight_messages_set 1681 * 1682 * This function is deprected. Use the <mosquitto_int_option> function with the 1683 * MOSQ_OPT_SEND_MAXIMUM option instead. 1684 * 1685 * Set the number of QoS 1 and 2 messages that can be "in flight" at one time. 1686 * An in flight message is part way through its delivery flow. Attempts to send 1687 * further messages with <mosquitto_publish> will result in the messages being 1688 * queued until the number of in flight messages reduces. 1689 * 1690 * A higher number here results in greater message throughput, but if set 1691 * higher than the maximum in flight messages on the broker may lead to 1692 * delays in the messages being acknowledged. 1693 * 1694 * Set to 0 for no maximum. 1695 * 1696 * Parameters: 1697 * mosq - a valid mosquitto instance. 1698 * max_inflight_messages - the maximum number of inflight messages. Defaults 1699 * to 20. 1700 * 1701 * Returns: 1702 * MOSQ_ERR_SUCCESS - on success. 1703 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1704 */ 1705 libmosq_EXPORT int mosquitto_max_inflight_messages_set(struct mosquitto *mosq, unsigned int max_inflight_messages); 1706 1707 /* 1708 * Function: mosquitto_message_retry_set 1709 * 1710 * This function now has no effect. 1711 */ 1712 libmosq_EXPORT void mosquitto_message_retry_set(struct mosquitto *mosq, unsigned int message_retry); 1713 1714 /* 1715 * Function: mosquitto_user_data_set 1716 * 1717 * When <mosquitto_new> is called, the pointer given as the "obj" parameter 1718 * will be passed to the callbacks as user data. The <mosquitto_user_data_set> 1719 * function allows this obj parameter to be updated at any time. This function 1720 * will not modify the memory pointed to by the current user data pointer. If 1721 * it is dynamically allocated memory you must free it yourself. 1722 * 1723 * Parameters: 1724 * mosq - a valid mosquitto instance. 1725 * obj - A user pointer that will be passed as an argument to any callbacks 1726 * that are specified. 1727 */ 1728 libmosq_EXPORT void mosquitto_user_data_set(struct mosquitto *mosq, void *obj); 1729 1730 /* Function: mosquitto_userdata 1731 * 1732 * Retrieve the "userdata" variable for a mosquitto client. 1733 * 1734 * Parameters: 1735 * mosq - a valid mosquitto instance. 1736 * 1737 * Returns: 1738 * A pointer to the userdata member variable. 1739 */ 1740 libmosq_EXPORT void *mosquitto_userdata(struct mosquitto *mosq); 1741 1742 1743 /* ====================================================================== 1744 * 1745 * Section: TLS support 1746 * 1747 * ====================================================================== */ 1748 /* 1749 * Function: mosquitto_tls_set 1750 * 1751 * Configure the client for certificate based SSL/TLS support. Must be called 1752 * before <mosquitto_connect>. 1753 * 1754 * Cannot be used in conjunction with <mosquitto_tls_psk_set>. 1755 * 1756 * Define the Certificate Authority certificates to be trusted (ie. the server 1757 * certificate must be signed with one of these certificates) using cafile. 1758 * 1759 * If the server you are connecting to requires clients to provide a 1760 * certificate, define certfile and keyfile with your client certificate and 1761 * private key. If your private key is encrypted, provide a password callback 1762 * function or you will have to enter the password at the command line. 1763 * 1764 * Parameters: 1765 * mosq - a valid mosquitto instance. 1766 * cafile - path to a file containing the PEM encoded trusted CA 1767 * certificate files. Either cafile or capath must not be NULL. 1768 * capath - path to a directory containing the PEM encoded trusted CA 1769 * certificate files. See mosquitto.conf for more details on 1770 * configuring this directory. Either cafile or capath must not 1771 * be NULL. 1772 * certfile - path to a file containing the PEM encoded certificate file 1773 * for this client. If NULL, keyfile must also be NULL and no 1774 * client certificate will be used. 1775 * keyfile - path to a file containing the PEM encoded private key for 1776 * this client. If NULL, certfile must also be NULL and no 1777 * client certificate will be used. 1778 * pw_callback - if keyfile is encrypted, set pw_callback to allow your client 1779 * to pass the correct password for decryption. If set to NULL, 1780 * the password must be entered on the command line. 1781 * Your callback must write the password into "buf", which is 1782 * "size" bytes long. The return value must be the length of the 1783 * password. "userdata" will be set to the calling mosquitto 1784 * instance. The mosquitto userdata member variable can be 1785 * retrieved using <mosquitto_userdata>. 1786 * 1787 * Returns: 1788 * MOSQ_ERR_SUCCESS - on success. 1789 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1790 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1791 * 1792 * See Also: 1793 * <mosquitto_tls_opts_set>, <mosquitto_tls_psk_set>, 1794 * <mosquitto_tls_insecure_set>, <mosquitto_userdata> 1795 */ 1796 libmosq_EXPORT int mosquitto_tls_set(struct mosquitto *mosq, 1797 const char *cafile, const char *capath, 1798 const char *certfile, const char *keyfile, 1799 int (*pw_callback)(char *buf, int size, int rwflag, void *userdata)); 1800 1801 /* 1802 * Function: mosquitto_tls_insecure_set 1803 * 1804 * Configure verification of the server hostname in the server certificate. If 1805 * value is set to true, it is impossible to guarantee that the host you are 1806 * connecting to is not impersonating your server. This can be useful in 1807 * initial server testing, but makes it possible for a malicious third party to 1808 * impersonate your server through DNS spoofing, for example. 1809 * Do not use this function in a real system. Setting value to true makes the 1810 * connection encryption pointless. 1811 * Must be called before <mosquitto_connect>. 1812 * 1813 * Parameters: 1814 * mosq - a valid mosquitto instance. 1815 * value - if set to false, the default, certificate hostname checking is 1816 * performed. If set to true, no hostname checking is performed and 1817 * the connection is insecure. 1818 * 1819 * Returns: 1820 * MOSQ_ERR_SUCCESS - on success. 1821 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1822 * 1823 * See Also: 1824 * <mosquitto_tls_set> 1825 */ 1826 libmosq_EXPORT int mosquitto_tls_insecure_set(struct mosquitto *mosq, bool value); 1827 1828 /* 1829 * Function: mosquitto_tls_opts_set 1830 * 1831 * Set advanced SSL/TLS options. Must be called before <mosquitto_connect>. 1832 * 1833 * Parameters: 1834 * mosq - a valid mosquitto instance. 1835 * cert_reqs - an integer defining the verification requirements the client 1836 * will impose on the server. This can be one of: 1837 * * SSL_VERIFY_NONE (0): the server will not be verified in any way. 1838 * * SSL_VERIFY_PEER (1): the server certificate will be verified 1839 * and the connection aborted if the verification fails. 1840 * The default and recommended value is SSL_VERIFY_PEER. Using 1841 * SSL_VERIFY_NONE provides no security. 1842 * tls_version - the version of the SSL/TLS protocol to use as a string. If NULL, 1843 * the default value is used. The default value and the 1844 * available values depend on the version of openssl that the 1845 * library was compiled against. For openssl >= 1.0.1, the 1846 * available options are tlsv1.2, tlsv1.1 and tlsv1, with tlv1.2 1847 * as the default. For openssl < 1.0.1, only tlsv1 is available. 1848 * ciphers - a string describing the ciphers available for use. See the 1849 * "openssl ciphers" tool for more information. If NULL, the 1850 * default ciphers will be used. 1851 * 1852 * Returns: 1853 * MOSQ_ERR_SUCCESS - on success. 1854 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1855 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1856 * 1857 * See Also: 1858 * <mosquitto_tls_set> 1859 */ 1860 libmosq_EXPORT int mosquitto_tls_opts_set(struct mosquitto *mosq, int cert_reqs, const char *tls_version, const char *ciphers); 1861 1862 /* 1863 * Function: mosquitto_tls_psk_set 1864 * 1865 * Configure the client for pre-shared-key based TLS support. Must be called 1866 * before <mosquitto_connect>. 1867 * 1868 * Cannot be used in conjunction with <mosquitto_tls_set>. 1869 * 1870 * Parameters: 1871 * mosq - a valid mosquitto instance. 1872 * psk - the pre-shared-key in hex format with no leading "0x". 1873 * identity - the identity of this client. May be used as the username 1874 * depending on the server settings. 1875 * ciphers - a string describing the PSK ciphers available for use. See the 1876 * "openssl ciphers" tool for more information. If NULL, the 1877 * default ciphers will be used. 1878 * 1879 * Returns: 1880 * MOSQ_ERR_SUCCESS - on success. 1881 * MOSQ_ERR_INVAL - if the input parameters were invalid. 1882 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 1883 * 1884 * See Also: 1885 * <mosquitto_tls_set> 1886 */ 1887 libmosq_EXPORT int mosquitto_tls_psk_set(struct mosquitto *mosq, const char *psk, const char *identity, const char *ciphers); 1888 1889 1890 /* 1891 * Function: mosquitto_ssl_get 1892 * 1893 * Retrieve a pointer to the SSL structure used for TLS connections in this 1894 * client. This can be used in e.g. the connect callback to carry out 1895 * additional verification steps. 1896 * 1897 * Parameters: 1898 * mosq - a valid mosquitto instance 1899 * 1900 * Returns: 1901 * A valid pointer to an openssl SSL structure - if the client is using TLS. 1902 * NULL - if the client is not using TLS, or TLS support is not compiled in. 1903 */ 1904 libmosq_EXPORT void *mosquitto_ssl_get(struct mosquitto *mosq); 1905 1906 1907 /* ====================================================================== 1908 * 1909 * Section: Callbacks 1910 * 1911 * ====================================================================== */ 1912 /* 1913 * Function: mosquitto_connect_callback_set 1914 * 1915 * Set the connect callback. This is called when the library receives a CONNACK 1916 * message in response to a connection. 1917 * 1918 * Parameters: 1919 * mosq - a valid mosquitto instance. 1920 * on_connect - a callback function in the following form: 1921 * void callback(struct mosquitto *mosq, void *obj, int rc) 1922 * 1923 * Callback Parameters: 1924 * mosq - the mosquitto instance making the callback. 1925 * obj - the user data provided in <mosquitto_new> 1926 * rc - the return code of the connection response. The values are defined by 1927 * the MQTT protocol version in use. 1928 * For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html 1929 * For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html 1930 */ 1931 libmosq_EXPORT void mosquitto_connect_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int)); 1932 1933 /* 1934 * Function: mosquitto_connect_with_flags_callback_set 1935 * 1936 * Set the connect callback. This is called when the library receives a CONNACK 1937 * message in response to a connection. 1938 * 1939 * Parameters: 1940 * mosq - a valid mosquitto instance. 1941 * on_connect - a callback function in the following form: 1942 * void callback(struct mosquitto *mosq, void *obj, int rc) 1943 * 1944 * Callback Parameters: 1945 * mosq - the mosquitto instance making the callback. 1946 * obj - the user data provided in <mosquitto_new> 1947 * rc - the return code of the connection response. The values are defined by 1948 * the MQTT protocol version in use. 1949 * For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html 1950 * For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html 1951 * flags - the connect flags. 1952 */ 1953 libmosq_EXPORT void mosquitto_connect_with_flags_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int)); 1954 1955 /* 1956 * Function: mosquitto_connect_v5_callback_set 1957 * 1958 * Set the connect callback. This is called when the library receives a CONNACK 1959 * message in response to a connection. 1960 * 1961 * It is valid to set this callback for all MQTT protocol versions. If it is 1962 * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props` 1963 * argument will always be NULL. 1964 * 1965 * Parameters: 1966 * mosq - a valid mosquitto instance. 1967 * on_connect - a callback function in the following form: 1968 * void callback(struct mosquitto *mosq, void *obj, int rc) 1969 * 1970 * Callback Parameters: 1971 * mosq - the mosquitto instance making the callback. 1972 * obj - the user data provided in <mosquitto_new> 1973 * rc - the return code of the connection response. The values are defined by 1974 * the MQTT protocol version in use. 1975 * For MQTT v5.0, look at section 3.2.2.2 Connect Reason code: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html 1976 * For MQTT v3.1.1, look at section 3.2.2.3 Connect Return code: http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html 1977 * flags - the connect flags. 1978 * props - list of MQTT 5 properties, or NULL 1979 * 1980 */ 1981 libmosq_EXPORT void mosquitto_connect_v5_callback_set(struct mosquitto *mosq, void (*on_connect)(struct mosquitto *, void *, int, int, const mosquitto_property *props)); 1982 1983 /* 1984 * Function: mosquitto_disconnect_callback_set 1985 * 1986 * Set the disconnect callback. This is called when the broker has received the 1987 * DISCONNECT command and has disconnected the client. 1988 * 1989 * Parameters: 1990 * mosq - a valid mosquitto instance. 1991 * on_disconnect - a callback function in the following form: 1992 * void callback(struct mosquitto *mosq, void *obj) 1993 * 1994 * Callback Parameters: 1995 * mosq - the mosquitto instance making the callback. 1996 * obj - the user data provided in <mosquitto_new> 1997 * rc - integer value indicating the reason for the disconnect. A value of 0 1998 * means the client has called <mosquitto_disconnect>. Any other value 1999 * indicates that the disconnect is unexpected. 2000 */ 2001 libmosq_EXPORT void mosquitto_disconnect_callback_set(struct mosquitto *mosq, void (*on_disconnect)(struct mosquitto *, void *, int)); 2002 2003 /* 2004 * Function: mosquitto_disconnect_v5_callback_set 2005 * 2006 * Set the disconnect callback. This is called when the broker has received the 2007 * DISCONNECT command and has disconnected the client. 2008 * 2009 * It is valid to set this callback for all MQTT protocol versions. If it is 2010 * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props` 2011 * argument will always be NULL. 2012 * 2013 * Parameters: 2014 * mosq - a valid mosquitto instance. 2015 * on_disconnect - a callback function in the following form: 2016 * void callback(struct mosquitto *mosq, void *obj) 2017 * 2018 * Callback Parameters: 2019 * mosq - the mosquitto instance making the callback. 2020 * obj - the user data provided in <mosquitto_new> 2021 * rc - integer value indicating the reason for the disconnect. A value of 0 2022 * means the client has called <mosquitto_disconnect>. Any other value 2023 * indicates that the disconnect is unexpected. 2024 * props - list of MQTT 5 properties, or NULL 2025 */ 2026 libmosq_EXPORT void mosquitto_disconnect_v5_callback_set(struct mosquitto *mosq, void (*on_disconnect)(struct mosquitto *, void *, int, const mosquitto_property *props)); 2027 2028 /* 2029 * Function: mosquitto_publish_callback_set 2030 * 2031 * Set the publish callback. This is called when a message initiated with 2032 * <mosquitto_publish> has been sent to the broker. "Sent" means different 2033 * things depending on the QoS of the message: 2034 * 2035 * QoS 0: The PUBLISH was passed to the local operating system for delivery, 2036 * there is no guarantee that it was delivered to the remote broker. 2037 * QoS 1: The PUBLISH was sent to the remote broker and the corresponding 2038 * PUBACK was received by the library. 2039 * QoS 2: The PUBLISH was sent to the remote broker and the corresponding 2040 * PUBCOMP was received by the library. 2041 * 2042 * Parameters: 2043 * mosq - a valid mosquitto instance. 2044 * on_publish - a callback function in the following form: 2045 * void callback(struct mosquitto *mosq, void *obj, int mid) 2046 * 2047 * Callback Parameters: 2048 * mosq - the mosquitto instance making the callback. 2049 * obj - the user data provided in <mosquitto_new> 2050 * mid - the message id of the sent message. 2051 */ 2052 libmosq_EXPORT void mosquitto_publish_callback_set(struct mosquitto *mosq, void (*on_publish)(struct mosquitto *, void *, int)); 2053 2054 /* 2055 * Function: mosquitto_publish_v5_callback_set 2056 * 2057 * Set the publish callback. This is called when a message initiated with 2058 * <mosquitto_publish> has been sent to the broker. This callback will be 2059 * called both if the message is sent successfully, or if the broker responded 2060 * with an error, which will be reflected in the reason_code parameter. 2061 * "Sent" means different things depending on the QoS of the message: 2062 * 2063 * QoS 0: The PUBLISH was passed to the local operating system for delivery, 2064 * there is no guarantee that it was delivered to the remote broker. 2065 * QoS 1: The PUBLISH was sent to the remote broker and the corresponding 2066 * PUBACK was received by the library. 2067 * QoS 2: The PUBLISH was sent to the remote broker and the corresponding 2068 * PUBCOMP was received by the library. 2069 * 2070 * 2071 * It is valid to set this callback for all MQTT protocol versions. If it is 2072 * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props` 2073 * argument will always be NULL. 2074 * 2075 * Parameters: 2076 * mosq - a valid mosquitto instance. 2077 * on_publish - a callback function in the following form: 2078 * void callback(struct mosquitto *mosq, void *obj, int mid) 2079 * 2080 * Callback Parameters: 2081 * mosq - the mosquitto instance making the callback. 2082 * obj - the user data provided in <mosquitto_new> 2083 * mid - the message id of the sent message. 2084 * reason_code - the MQTT 5 reason code 2085 * props - list of MQTT 5 properties, or NULL 2086 */ 2087 libmosq_EXPORT void mosquitto_publish_v5_callback_set(struct mosquitto *mosq, void (*on_publish)(struct mosquitto *, void *, int, int, const mosquitto_property *props)); 2088 2089 /* 2090 * Function: mosquitto_message_callback_set 2091 * 2092 * Set the message callback. This is called when a message is received from the 2093 * broker and the required QoS flow has completed. 2094 * 2095 * Parameters: 2096 * mosq - a valid mosquitto instance. 2097 * on_message - a callback function in the following form: 2098 * void callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) 2099 * 2100 * Callback Parameters: 2101 * mosq - the mosquitto instance making the callback. 2102 * obj - the user data provided in <mosquitto_new> 2103 * message - the message data. This variable and associated memory will be 2104 * freed by the library after the callback completes. The client 2105 * should make copies of any of the data it requires. 2106 * 2107 * See Also: 2108 * <mosquitto_message_copy> 2109 */ 2110 libmosq_EXPORT void mosquitto_message_callback_set(struct mosquitto *mosq, void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *)); 2111 2112 /* 2113 * Function: mosquitto_message_v5_callback_set 2114 * 2115 * Set the message callback. This is called when a message is received from the 2116 * broker and the required QoS flow has completed. 2117 * 2118 * It is valid to set this callback for all MQTT protocol versions. If it is 2119 * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props` 2120 * argument will always be NULL. 2121 * 2122 * Parameters: 2123 * mosq - a valid mosquitto instance. 2124 * on_message - a callback function in the following form: 2125 * void callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) 2126 * 2127 * Callback Parameters: 2128 * mosq - the mosquitto instance making the callback. 2129 * obj - the user data provided in <mosquitto_new> 2130 * message - the message data. This variable and associated memory will be 2131 * freed by the library after the callback completes. The client 2132 * should make copies of any of the data it requires. 2133 * props - list of MQTT 5 properties, or NULL 2134 * 2135 * See Also: 2136 * <mosquitto_message_copy> 2137 */ 2138 libmosq_EXPORT void mosquitto_message_v5_callback_set(struct mosquitto *mosq, void (*on_message)(struct mosquitto *, void *, const struct mosquitto_message *, const mosquitto_property *props)); 2139 2140 /* 2141 * Function: mosquitto_subscribe_callback_set 2142 * 2143 * Set the subscribe callback. This is called when the library receives a 2144 * SUBACK message in response to a SUBSCRIBE. 2145 * 2146 * Parameters: 2147 * mosq - a valid mosquitto instance. 2148 * on_subscribe - a callback function in the following form: 2149 * void callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) 2150 * 2151 * Callback Parameters: 2152 * mosq - the mosquitto instance making the callback. 2153 * obj - the user data provided in <mosquitto_new> 2154 * mid - the message id of the subscribe message. 2155 * qos_count - the number of granted subscriptions (size of granted_qos). 2156 * granted_qos - an array of integers indicating the granted QoS for each of 2157 * the subscriptions. 2158 */ 2159 libmosq_EXPORT void mosquitto_subscribe_callback_set(struct mosquitto *mosq, void (*on_subscribe)(struct mosquitto *, void *, int, int, const int *)); 2160 2161 /* 2162 * Function: mosquitto_subscribe_v5_callback_set 2163 * 2164 * Set the subscribe callback. This is called when the library receives a 2165 * SUBACK message in response to a SUBSCRIBE. 2166 * 2167 * It is valid to set this callback for all MQTT protocol versions. If it is 2168 * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props` 2169 * argument will always be NULL. 2170 * 2171 * Parameters: 2172 * mosq - a valid mosquitto instance. 2173 * on_subscribe - a callback function in the following form: 2174 * void callback(struct mosquitto *mosq, void *obj, int mid, int qos_count, const int *granted_qos) 2175 * 2176 * Callback Parameters: 2177 * mosq - the mosquitto instance making the callback. 2178 * obj - the user data provided in <mosquitto_new> 2179 * mid - the message id of the subscribe message. 2180 * qos_count - the number of granted subscriptions (size of granted_qos). 2181 * granted_qos - an array of integers indicating the granted QoS for each of 2182 * the subscriptions. 2183 * props - list of MQTT 5 properties, or NULL 2184 */ 2185 libmosq_EXPORT void mosquitto_subscribe_v5_callback_set(struct mosquitto *mosq, void (*on_subscribe)(struct mosquitto *, void *, int, int, const int *, const mosquitto_property *props)); 2186 2187 /* 2188 * Function: mosquitto_unsubscribe_callback_set 2189 * 2190 * Set the unsubscribe callback. This is called when the library receives a 2191 * UNSUBACK message in response to an UNSUBSCRIBE. 2192 * 2193 * Parameters: 2194 * mosq - a valid mosquitto instance. 2195 * on_unsubscribe - a callback function in the following form: 2196 * void callback(struct mosquitto *mosq, void *obj, int mid) 2197 * 2198 * Callback Parameters: 2199 * mosq - the mosquitto instance making the callback. 2200 * obj - the user data provided in <mosquitto_new> 2201 * mid - the message id of the unsubscribe message. 2202 */ 2203 libmosq_EXPORT void mosquitto_unsubscribe_callback_set(struct mosquitto *mosq, void (*on_unsubscribe)(struct mosquitto *, void *, int)); 2204 2205 /* 2206 * Function: mosquitto_unsubscribe_v5_callback_set 2207 * 2208 * Set the unsubscribe callback. This is called when the library receives a 2209 * UNSUBACK message in response to an UNSUBSCRIBE. 2210 * 2211 * It is valid to set this callback for all MQTT protocol versions. If it is 2212 * used with MQTT clients that use MQTT v3.1.1 or earlier, then the `props` 2213 * argument will always be NULL. 2214 * 2215 * Parameters: 2216 * mosq - a valid mosquitto instance. 2217 * on_unsubscribe - a callback function in the following form: 2218 * void callback(struct mosquitto *mosq, void *obj, int mid) 2219 * 2220 * Callback Parameters: 2221 * mosq - the mosquitto instance making the callback. 2222 * obj - the user data provided in <mosquitto_new> 2223 * mid - the message id of the unsubscribe message. 2224 * props - list of MQTT 5 properties, or NULL 2225 */ 2226 libmosq_EXPORT void mosquitto_unsubscribe_v5_callback_set(struct mosquitto *mosq, void (*on_unsubscribe)(struct mosquitto *, void *, int, const mosquitto_property *props)); 2227 2228 /* 2229 * Function: mosquitto_log_callback_set 2230 * 2231 * Set the logging callback. This should be used if you want event logging 2232 * information from the client library. 2233 * 2234 * mosq - a valid mosquitto instance. 2235 * on_log - a callback function in the following form: 2236 * void callback(struct mosquitto *mosq, void *obj, int level, const char *str) 2237 * 2238 * Callback Parameters: 2239 * mosq - the mosquitto instance making the callback. 2240 * obj - the user data provided in <mosquitto_new> 2241 * level - the log message level from the values: 2242 * MOSQ_LOG_INFO 2243 * MOSQ_LOG_NOTICE 2244 * MOSQ_LOG_WARNING 2245 * MOSQ_LOG_ERR 2246 * MOSQ_LOG_DEBUG 2247 * str - the message string. 2248 */ 2249 libmosq_EXPORT void mosquitto_log_callback_set(struct mosquitto *mosq, void (*on_log)(struct mosquitto *, void *, int, const char *)); 2250 2251 2252 /* ============================================================================= 2253 * 2254 * Section: SOCKS5 proxy functions 2255 * 2256 * ============================================================================= 2257 */ 2258 2259 /* 2260 * Function: mosquitto_socks5_set 2261 * 2262 * Configure the client to use a SOCKS5 proxy when connecting. Must be called 2263 * before connecting. "None" and "username/password" authentication is 2264 * supported. 2265 * 2266 * Parameters: 2267 * mosq - a valid mosquitto instance. 2268 * host - the SOCKS5 proxy host to connect to. 2269 * port - the SOCKS5 proxy port to use. 2270 * username - if not NULL, use this username when authenticating with the proxy. 2271 * password - if not NULL and username is not NULL, use this password when 2272 * authenticating with the proxy. 2273 */ 2274 libmosq_EXPORT int mosquitto_socks5_set(struct mosquitto *mosq, const char *host, int port, const char *username, const char *password); 2275 2276 2277 /* ============================================================================= 2278 * 2279 * Section: Utility functions 2280 * 2281 * ============================================================================= 2282 */ 2283 2284 /* 2285 * Function: mosquitto_strerror 2286 * 2287 * Call to obtain a const string description of a mosquitto error number. 2288 * 2289 * Parameters: 2290 * mosq_errno - a mosquitto error number. 2291 * 2292 * Returns: 2293 * A constant string describing the error. 2294 */ 2295 libmosq_EXPORT const char *mosquitto_strerror(int mosq_errno); 2296 2297 /* 2298 * Function: mosquitto_connack_string 2299 * 2300 * Call to obtain a const string description of an MQTT connection result. 2301 * 2302 * Parameters: 2303 * connack_code - an MQTT connection result. 2304 * 2305 * Returns: 2306 * A constant string describing the result. 2307 */ 2308 libmosq_EXPORT const char *mosquitto_connack_string(int connack_code); 2309 2310 /* 2311 * Function: mosquitto_reason_string 2312 * 2313 * Call to obtain a const string description of an MQTT reason code. 2314 * 2315 * Parameters: 2316 * reason_code - an MQTT reason code. 2317 * 2318 * Returns: 2319 * A constant string describing the reason. 2320 */ 2321 libmosq_EXPORT const char *mosquitto_reason_string(int reason_code); 2322 2323 /* Function: mosquitto_string_to_command 2324 * 2325 * Take a string input representing an MQTT command and convert it to the 2326 * libmosquitto integer representation. 2327 * 2328 * Parameters: 2329 * str - the string to parse. 2330 * cmd - pointer to an int, for the result. 2331 * 2332 * Returns: 2333 * MOSQ_ERR_SUCCESS - on success 2334 * MOSQ_ERR_INVAL - on an invalid input. 2335 * 2336 * Example: 2337 * (start code) 2338 * mosquitto_string_to_command("CONNECT", &cmd); 2339 * // cmd == CMD_CONNECT 2340 * (end) 2341 */ 2342 libmosq_EXPORT int mosquitto_string_to_command(const char *str, int *cmd); 2343 2344 /* 2345 * Function: mosquitto_sub_topic_tokenise 2346 * 2347 * Tokenise a topic or subscription string into an array of strings 2348 * representing the topic hierarchy. 2349 * 2350 * For example: 2351 * 2352 * subtopic: "a/deep/topic/hierarchy" 2353 * 2354 * Would result in: 2355 * 2356 * topics[0] = "a" 2357 * topics[1] = "deep" 2358 * topics[2] = "topic" 2359 * topics[3] = "hierarchy" 2360 * 2361 * and: 2362 * 2363 * subtopic: "/a/deep/topic/hierarchy/" 2364 * 2365 * Would result in: 2366 * 2367 * topics[0] = NULL 2368 * topics[1] = "a" 2369 * topics[2] = "deep" 2370 * topics[3] = "topic" 2371 * topics[4] = "hierarchy" 2372 * 2373 * Parameters: 2374 * subtopic - the subscription/topic to tokenise 2375 * topics - a pointer to store the array of strings 2376 * count - an int pointer to store the number of items in the topics array. 2377 * 2378 * Returns: 2379 * MOSQ_ERR_SUCCESS - on success 2380 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 2381 * MOSQ_ERR_MALFORMED_UTF8 - if the topic is not valid UTF-8 2382 * 2383 * Example: 2384 * 2385 * > char **topics; 2386 * > int topic_count; 2387 * > int i; 2388 * > 2389 * > mosquitto_sub_topic_tokenise("$SYS/broker/uptime", &topics, &topic_count); 2390 * > 2391 * > for(i=0; i<token_count; i++){ 2392 * > printf("%d: %s\n", i, topics[i]); 2393 * > } 2394 * 2395 * See Also: 2396 * <mosquitto_sub_topic_tokens_free> 2397 */ 2398 libmosq_EXPORT int mosquitto_sub_topic_tokenise(const char *subtopic, char ***topics, int *count); 2399 2400 /* 2401 * Function: mosquitto_sub_topic_tokens_free 2402 * 2403 * Free memory that was allocated in <mosquitto_sub_topic_tokenise>. 2404 * 2405 * Parameters: 2406 * topics - pointer to string array. 2407 * count - count of items in string array. 2408 * 2409 * Returns: 2410 * MOSQ_ERR_SUCCESS - on success 2411 * MOSQ_ERR_INVAL - if the input parameters were invalid. 2412 * 2413 * See Also: 2414 * <mosquitto_sub_topic_tokenise> 2415 */ 2416 libmosq_EXPORT int mosquitto_sub_topic_tokens_free(char ***topics, int count); 2417 2418 /* 2419 * Function: mosquitto_topic_matches_sub 2420 * 2421 * Check whether a topic matches a subscription. 2422 * 2423 * For example: 2424 * 2425 * foo/bar would match the subscription foo/# or +/bar 2426 * non/matching would not match the subscription non/+/+ 2427 * 2428 * Parameters: 2429 * sub - subscription string to check topic against. 2430 * topic - topic to check. 2431 * result - bool pointer to hold result. Will be set to true if the topic 2432 * matches the subscription. 2433 * 2434 * Returns: 2435 * MOSQ_ERR_SUCCESS - on success 2436 * MOSQ_ERR_INVAL - if the input parameters were invalid. 2437 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 2438 */ 2439 libmosq_EXPORT int mosquitto_topic_matches_sub(const char *sub, const char *topic, bool *result); 2440 2441 2442 /* 2443 * Function: mosquitto_topic_matches_sub2 2444 * 2445 * Check whether a topic matches a subscription. 2446 * 2447 * For example: 2448 * 2449 * foo/bar would match the subscription foo/# or +/bar 2450 * non/matching would not match the subscription non/+/+ 2451 * 2452 * Parameters: 2453 * sub - subscription string to check topic against. 2454 * sublen - length in bytes of sub string 2455 * topic - topic to check. 2456 * topiclen - length in bytes of topic string 2457 * result - bool pointer to hold result. Will be set to true if the topic 2458 * matches the subscription. 2459 * 2460 * Returns: 2461 * MOSQ_ERR_SUCCESS - on success 2462 * MOSQ_ERR_INVAL - if the input parameters were invalid. 2463 * MOSQ_ERR_NOMEM - if an out of memory condition occurred. 2464 */ 2465 libmosq_EXPORT int mosquitto_topic_matches_sub2(const char *sub, size_t sublen, const char *topic, size_t topiclen, bool *result); 2466 2467 /* 2468 * Function: mosquitto_pub_topic_check 2469 * 2470 * Check whether a topic to be used for publishing is valid. 2471 * 2472 * This searches for + or # in a topic and checks its length. 2473 * 2474 * This check is already carried out in <mosquitto_publish> and 2475 * <mosquitto_will_set>, there is no need to call it directly before them. It 2476 * may be useful if you wish to check the validity of a topic in advance of 2477 * making a connection for example. 2478 * 2479 * Parameters: 2480 * topic - the topic to check 2481 * 2482 * Returns: 2483 * MOSQ_ERR_SUCCESS - for a valid topic 2484 * MOSQ_ERR_INVAL - if the topic contains a + or a #, or if it is too long. 2485 * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8 2486 * 2487 * See Also: 2488 * <mosquitto_sub_topic_check> 2489 */ 2490 libmosq_EXPORT int mosquitto_pub_topic_check(const char *topic); 2491 2492 /* 2493 * Function: mosquitto_pub_topic_check2 2494 * 2495 * Check whether a topic to be used for publishing is valid. 2496 * 2497 * This searches for + or # in a topic and checks its length. 2498 * 2499 * This check is already carried out in <mosquitto_publish> and 2500 * <mosquitto_will_set>, there is no need to call it directly before them. It 2501 * may be useful if you wish to check the validity of a topic in advance of 2502 * making a connection for example. 2503 * 2504 * Parameters: 2505 * topic - the topic to check 2506 * topiclen - length of the topic in bytes 2507 * 2508 * Returns: 2509 * MOSQ_ERR_SUCCESS - for a valid topic 2510 * MOSQ_ERR_INVAL - if the topic contains a + or a #, or if it is too long. 2511 * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8 2512 * 2513 * See Also: 2514 * <mosquitto_sub_topic_check> 2515 */ 2516 libmosq_EXPORT int mosquitto_pub_topic_check2(const char *topic, size_t topiclen); 2517 2518 /* 2519 * Function: mosquitto_sub_topic_check 2520 * 2521 * Check whether a topic to be used for subscribing is valid. 2522 * 2523 * This searches for + or # in a topic and checks that they aren't in invalid 2524 * positions, such as with foo/#/bar, foo/+bar or foo/bar#, and checks its 2525 * length. 2526 * 2527 * This check is already carried out in <mosquitto_subscribe> and 2528 * <mosquitto_unsubscribe>, there is no need to call it directly before them. 2529 * It may be useful if you wish to check the validity of a topic in advance of 2530 * making a connection for example. 2531 * 2532 * Parameters: 2533 * topic - the topic to check 2534 * 2535 * Returns: 2536 * MOSQ_ERR_SUCCESS - for a valid topic 2537 * MOSQ_ERR_INVAL - if the topic contains a + or a # that is in an 2538 * invalid position, or if it is too long. 2539 * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8 2540 * 2541 * See Also: 2542 * <mosquitto_sub_topic_check> 2543 */ 2544 libmosq_EXPORT int mosquitto_sub_topic_check(const char *topic); 2545 2546 /* 2547 * Function: mosquitto_sub_topic_check2 2548 * 2549 * Check whether a topic to be used for subscribing is valid. 2550 * 2551 * This searches for + or # in a topic and checks that they aren't in invalid 2552 * positions, such as with foo/#/bar, foo/+bar or foo/bar#, and checks its 2553 * length. 2554 * 2555 * This check is already carried out in <mosquitto_subscribe> and 2556 * <mosquitto_unsubscribe>, there is no need to call it directly before them. 2557 * It may be useful if you wish to check the validity of a topic in advance of 2558 * making a connection for example. 2559 * 2560 * Parameters: 2561 * topic - the topic to check 2562 * topiclen - the length in bytes of the topic 2563 * 2564 * Returns: 2565 * MOSQ_ERR_SUCCESS - for a valid topic 2566 * MOSQ_ERR_INVAL - if the topic contains a + or a # that is in an 2567 * invalid position, or if it is too long. 2568 * MOSQ_ERR_MALFORMED_UTF8 - if topic is not valid UTF-8 2569 * 2570 * See Also: 2571 * <mosquitto_sub_topic_check> 2572 */ 2573 libmosq_EXPORT int mosquitto_sub_topic_check2(const char *topic, size_t topiclen); 2574 2575 2576 /* 2577 * Function: mosquitto_validate_utf8 2578 * 2579 * Helper function to validate whether a UTF-8 string is valid, according to 2580 * the UTF-8 spec and the MQTT additions. 2581 * 2582 * Parameters: 2583 * str - a string to check 2584 * len - the length of the string in bytes 2585 * 2586 * Returns: 2587 * MOSQ_ERR_SUCCESS - on success 2588 * MOSQ_ERR_INVAL - if str is NULL or len<0 or len>65536 2589 * MOSQ_ERR_MALFORMED_UTF8 - if str is not valid UTF-8 2590 */ 2591 libmosq_EXPORT int mosquitto_validate_utf8(const char *str, int len); 2592 2593 2594 /* ============================================================================= 2595 * 2596 * Section: One line client helper functions 2597 * 2598 * ============================================================================= 2599 */ 2600 2601 struct libmosquitto_will { 2602 char *topic; 2603 void *payload; 2604 int payloadlen; 2605 int qos; 2606 bool retain; 2607 }; 2608 2609 struct libmosquitto_auth { 2610 char *username; 2611 char *password; 2612 }; 2613 2614 struct libmosquitto_tls { 2615 char *cafile; 2616 char *capath; 2617 char *certfile; 2618 char *keyfile; 2619 char *ciphers; 2620 char *tls_version; 2621 int (*pw_callback)(char *buf, int size, int rwflag, void *userdata); 2622 int cert_reqs; 2623 }; 2624 2625 /* 2626 * Function: mosquitto_subscribe_simple 2627 * 2628 * Helper function to make subscribing to a topic and retrieving some messages 2629 * very straightforward. 2630 * 2631 * This connects to a broker, subscribes to a topic, waits for msg_count 2632 * messages to be received, then returns after disconnecting cleanly. 2633 * 2634 * Parameters: 2635 * messages - pointer to a "struct mosquitto_message *". The received 2636 * messages will be returned here. On error, this will be set to 2637 * NULL. 2638 * msg_count - the number of messages to retrieve. 2639 * want_retained - if set to true, stale retained messages will be treated as 2640 * normal messages with regards to msg_count. If set to 2641 * false, they will be ignored. 2642 * topic - the subscription topic to use (wildcards are allowed). 2643 * qos - the qos to use for the subscription. 2644 * host - the broker to connect to. 2645 * port - the network port the broker is listening on. 2646 * client_id - the client id to use, or NULL if a random client id should be 2647 * generated. 2648 * keepalive - the MQTT keepalive value. 2649 * clean_session - the MQTT clean session flag. 2650 * username - the username string, or NULL for no username authentication. 2651 * password - the password string, or NULL for an empty password. 2652 * will - a libmosquitto_will struct containing will information, or NULL for 2653 * no will. 2654 * tls - a libmosquitto_tls struct containing TLS related parameters, or NULL 2655 * for no use of TLS. 2656 * 2657 * 2658 * Returns: 2659 * MOSQ_ERR_SUCCESS - on success 2660 * Greater than 0 - on error. 2661 */ 2662 libmosq_EXPORT int mosquitto_subscribe_simple( 2663 struct mosquitto_message **messages, 2664 int msg_count, 2665 bool want_retained, 2666 const char *topic, 2667 int qos, 2668 const char *host, 2669 int port, 2670 const char *client_id, 2671 int keepalive, 2672 bool clean_session, 2673 const char *username, 2674 const char *password, 2675 const struct libmosquitto_will *will, 2676 const struct libmosquitto_tls *tls); 2677 2678 2679 /* 2680 * Function: mosquitto_subscribe_callback 2681 * 2682 * Helper function to make subscribing to a topic and processing some messages 2683 * very straightforward. 2684 * 2685 * This connects to a broker, subscribes to a topic, then passes received 2686 * messages to a user provided callback. If the callback returns a 1, it then 2687 * disconnects cleanly and returns. 2688 * 2689 * Parameters: 2690 * callback - a callback function in the following form: 2691 * int callback(struct mosquitto *mosq, void *obj, const struct mosquitto_message *message) 2692 * Note that this is the same as the normal on_message callback, 2693 * except that it returns an int. 2694 * userdata - user provided pointer that will be passed to the callback. 2695 * topic - the subscription topic to use (wildcards are allowed). 2696 * qos - the qos to use for the subscription. 2697 * host - the broker to connect to. 2698 * port - the network port the broker is listening on. 2699 * client_id - the client id to use, or NULL if a random client id should be 2700 * generated. 2701 * keepalive - the MQTT keepalive value. 2702 * clean_session - the MQTT clean session flag. 2703 * username - the username string, or NULL for no username authentication. 2704 * password - the password string, or NULL for an empty password. 2705 * will - a libmosquitto_will struct containing will information, or NULL for 2706 * no will. 2707 * tls - a libmosquitto_tls struct containing TLS related parameters, or NULL 2708 * for no use of TLS. 2709 * 2710 * 2711 * Returns: 2712 * MOSQ_ERR_SUCCESS - on success 2713 * Greater than 0 - on error. 2714 */ 2715 libmosq_EXPORT int mosquitto_subscribe_callback( 2716 int (*callback)(struct mosquitto *, void *, const struct mosquitto_message *), 2717 void *userdata, 2718 const char *topic, 2719 int qos, 2720 const char *host, 2721 int port, 2722 const char *client_id, 2723 int keepalive, 2724 bool clean_session, 2725 const char *username, 2726 const char *password, 2727 const struct libmosquitto_will *will, 2728 const struct libmosquitto_tls *tls); 2729 2730 2731 /* ============================================================================= 2732 * 2733 * Section: Properties 2734 * 2735 * ============================================================================= 2736 */ 2737 2738 2739 /* 2740 * Function: mosquitto_property_add_byte 2741 * 2742 * Add a new byte property to a property list. 2743 * 2744 * If *proplist == NULL, a new list will be created, otherwise the new property 2745 * will be appended to the list. 2746 * 2747 * Parameters: 2748 * proplist - pointer to mosquitto_property pointer, the list of properties 2749 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 2750 * value - integer value for the new property 2751 * 2752 * Returns: 2753 * MOSQ_ERR_SUCCESS - on success 2754 * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL 2755 * MOSQ_ERR_NOMEM - on out of memory 2756 * 2757 * Example: 2758 * > mosquitto_property *proplist = NULL; 2759 * > mosquitto_property_add_byte(&proplist, MQTT_PROP_PAYLOAD_FORMAT_IDENTIFIER, 1); 2760 */ 2761 libmosq_EXPORT int mosquitto_property_add_byte(mosquitto_property **proplist, int identifier, uint8_t value); 2762 2763 /* 2764 * Function: mosquitto_property_add_int16 2765 * 2766 * Add a new int16 property to a property list. 2767 * 2768 * If *proplist == NULL, a new list will be created, otherwise the new property 2769 * will be appended to the list. 2770 * 2771 * Parameters: 2772 * proplist - pointer to mosquitto_property pointer, the list of properties 2773 * identifier - property identifier (e.g. MQTT_PROP_RECEIVE_MAXIMUM) 2774 * value - integer value for the new property 2775 * 2776 * Returns: 2777 * MOSQ_ERR_SUCCESS - on success 2778 * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL 2779 * MOSQ_ERR_NOMEM - on out of memory 2780 * 2781 * Example: 2782 * > mosquitto_property *proplist = NULL; 2783 * > mosquitto_property_add_int16(&proplist, MQTT_PROP_RECEIVE_MAXIMUM, 1000); 2784 */ 2785 libmosq_EXPORT int mosquitto_property_add_int16(mosquitto_property **proplist, int identifier, uint16_t value); 2786 2787 /* 2788 * Function: mosquitto_property_add_int32 2789 * 2790 * Add a new int32 property to a property list. 2791 * 2792 * If *proplist == NULL, a new list will be created, otherwise the new property 2793 * will be appended to the list. 2794 * 2795 * Parameters: 2796 * proplist - pointer to mosquitto_property pointer, the list of properties 2797 * identifier - property identifier (e.g. MQTT_PROP_MESSAGE_EXPIRY_INTERVAL) 2798 * value - integer value for the new property 2799 * 2800 * Returns: 2801 * MOSQ_ERR_SUCCESS - on success 2802 * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL 2803 * MOSQ_ERR_NOMEM - on out of memory 2804 * 2805 * Example: 2806 * > mosquitto_property *proplist = NULL; 2807 * > mosquitto_property_add_int32(&proplist, MQTT_PROP_MESSAGE_EXPIRY_INTERVAL, 86400); 2808 */ 2809 libmosq_EXPORT int mosquitto_property_add_int32(mosquitto_property **proplist, int identifier, uint32_t value); 2810 2811 /* 2812 * Function: mosquitto_property_add_varint 2813 * 2814 * Add a new varint property to a property list. 2815 * 2816 * If *proplist == NULL, a new list will be created, otherwise the new property 2817 * will be appended to the list. 2818 * 2819 * Parameters: 2820 * proplist - pointer to mosquitto_property pointer, the list of properties 2821 * identifier - property identifier (e.g. MQTT_PROP_SUBSCRIPTION_IDENTIFIER) 2822 * value - integer value for the new property 2823 * 2824 * Returns: 2825 * MOSQ_ERR_SUCCESS - on success 2826 * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL 2827 * MOSQ_ERR_NOMEM - on out of memory 2828 * 2829 * Example: 2830 * > mosquitto_property *proplist = NULL; 2831 * > mosquitto_property_add_varint(&proplist, MQTT_PROP_SUBSCRIPTION_IDENTIFIER, 1); 2832 */ 2833 libmosq_EXPORT int mosquitto_property_add_varint(mosquitto_property **proplist, int identifier, uint32_t value); 2834 2835 /* 2836 * Function: mosquitto_property_add_binary 2837 * 2838 * Add a new binary property to a property list. 2839 * 2840 * If *proplist == NULL, a new list will be created, otherwise the new property 2841 * will be appended to the list. 2842 * 2843 * Parameters: 2844 * proplist - pointer to mosquitto_property pointer, the list of properties 2845 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 2846 * value - pointer to the property data 2847 * len - length of property data in bytes 2848 * 2849 * Returns: 2850 * MOSQ_ERR_SUCCESS - on success 2851 * MOSQ_ERR_INVAL - if identifier is invalid, or if proplist is NULL 2852 * MOSQ_ERR_NOMEM - on out of memory 2853 * 2854 * Example: 2855 * > mosquitto_property *proplist = NULL; 2856 * > mosquitto_property_add_binary(&proplist, MQTT_PROP_AUTHENTICATION_DATA, auth_data, auth_data_len); 2857 */ 2858 libmosq_EXPORT int mosquitto_property_add_binary(mosquitto_property **proplist, int identifier, const void *value, uint16_t len); 2859 2860 /* 2861 * Function: mosquitto_property_add_string 2862 * 2863 * Add a new string property to a property list. 2864 * 2865 * If *proplist == NULL, a new list will be created, otherwise the new property 2866 * will be appended to the list. 2867 * 2868 * Parameters: 2869 * proplist - pointer to mosquitto_property pointer, the list of properties 2870 * identifier - property identifier (e.g. MQTT_PROP_CONTENT_TYPE) 2871 * value - string value for the new property, must be UTF-8 and zero terminated 2872 * 2873 * Returns: 2874 * MOSQ_ERR_SUCCESS - on success 2875 * MOSQ_ERR_INVAL - if identifier is invalid, if value is NULL, or if proplist is NULL 2876 * MOSQ_ERR_NOMEM - on out of memory 2877 * MOSQ_ERR_MALFORMED_UTF8 - value is not valid UTF-8. 2878 * 2879 * Example: 2880 * > mosquitto_property *proplist = NULL; 2881 * > mosquitto_property_add_string(&proplist, MQTT_PROP_CONTENT_TYPE, "application/json"); 2882 */ 2883 libmosq_EXPORT int mosquitto_property_add_string(mosquitto_property **proplist, int identifier, const char *value); 2884 2885 /* 2886 * Function: mosquitto_property_add_string_pair 2887 * 2888 * Add a new string pair property to a property list. 2889 * 2890 * If *proplist == NULL, a new list will be created, otherwise the new property 2891 * will be appended to the list. 2892 * 2893 * Parameters: 2894 * proplist - pointer to mosquitto_property pointer, the list of properties 2895 * identifier - property identifier (e.g. MQTT_PROP_USER_PROPERTY) 2896 * name - string name for the new property, must be UTF-8 and zero terminated 2897 * value - string value for the new property, must be UTF-8 and zero terminated 2898 * 2899 * Returns: 2900 * MOSQ_ERR_SUCCESS - on success 2901 * MOSQ_ERR_INVAL - if identifier is invalid, if name or value is NULL, or if proplist is NULL 2902 * MOSQ_ERR_NOMEM - on out of memory 2903 * MOSQ_ERR_MALFORMED_UTF8 - if name or value are not valid UTF-8. 2904 * 2905 * Example: 2906 * > mosquitto_property *proplist = NULL; 2907 * > mosquitto_property_add_string_pair(&proplist, MQTT_PROP_USER_PROPERTY, "client", "mosquitto_pub"); 2908 */ 2909 libmosq_EXPORT int mosquitto_property_add_string_pair(mosquitto_property **proplist, int identifier, const char *name, const char *value); 2910 2911 2912 /* 2913 * Function: mosquitto_property_identifier 2914 * 2915 * Return the property identifier for a single property. 2916 * 2917 * Parameters: 2918 * property - pointer to a valid mosquitto_property pointer. 2919 * 2920 * Returns: 2921 * A valid property identifier on success 2922 * 0 - on error 2923 */ 2924 libmosq_EXPORT int mosquitto_property_identifier(const mosquitto_property *property); 2925 2926 2927 /* 2928 * Function: mosquitto_property_next 2929 * 2930 * Return the next property in a property list. Use to iterate over a property 2931 * list, e.g.: 2932 * 2933 * (start code) 2934 * for(prop = proplist; prop != NULL; prop = mosquitto_property_next(prop)){ 2935 * if(mosquitto_property_identifier(prop) == MQTT_PROP_CONTENT_TYPE){ 2936 * ... 2937 * } 2938 * } 2939 * (end) 2940 * 2941 * Parameters: 2942 * proplist - pointer to mosquitto_property pointer, the list of properties 2943 * 2944 * Returns: 2945 * Pointer to the next item in the list 2946 * NULL, if proplist is NULL, or if there are no more items in the list. 2947 */ 2948 libmosq_EXPORT const mosquitto_property *mosquitto_property_next(const mosquitto_property *proplist); 2949 2950 2951 /* 2952 * Function: mosquitto_property_read_byte 2953 * 2954 * Attempt to read a byte property matching an identifier, from a property list 2955 * or single property. This function can search for multiple entries of the 2956 * same identifier by using the returned value and skip_first. Note however 2957 * that it is forbidden for most properties to be duplicated. 2958 * 2959 * If the property is not found, *value will not be modified, so it is safe to 2960 * pass a variable with a default value to be potentially overwritten: 2961 * 2962 * (start code) 2963 * uint16_t keepalive = 60; // default value 2964 * // Get value from property list, or keep default if not found. 2965 * mosquitto_property_read_int16(proplist, MQTT_PROP_SERVER_KEEP_ALIVE, &keepalive, false); 2966 * (end) 2967 * 2968 * Parameters: 2969 * proplist - mosquitto_property pointer, the list of properties or single property 2970 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 2971 * value - pointer to store the value, or NULL if the value is not required. 2972 * skip_first - boolean that indicates whether the first item in the list 2973 * should be ignored or not. Should usually be set to false. 2974 * 2975 * Returns: 2976 * A valid property pointer if the property is found 2977 * NULL, if the property is not found, or proplist is NULL. 2978 * 2979 * Example: 2980 * (start code) 2981 * // proplist is obtained from a callback 2982 * mosquitto_property *prop; 2983 * prop = mosquitto_property_read_byte(proplist, identifier, &value, false); 2984 * while(prop){ 2985 * printf("value: %s\n", value); 2986 * prop = mosquitto_property_read_byte(prop, identifier, &value); 2987 * } 2988 * (end) 2989 */ 2990 libmosq_EXPORT const mosquitto_property *mosquitto_property_read_byte( 2991 const mosquitto_property *proplist, 2992 int identifier, 2993 uint8_t *value, 2994 bool skip_first); 2995 2996 /* 2997 * Function: mosquitto_property_read_int16 2998 * 2999 * Read an int16 property value from a property. 3000 * 3001 * Parameters: 3002 * property - property to read 3003 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 3004 * value - pointer to store the value, or NULL if the value is not required. 3005 * skip_first - boolean that indicates whether the first item in the list 3006 * should be ignored or not. Should usually be set to false. 3007 * 3008 * Returns: 3009 * A valid property pointer if the property is found 3010 * NULL, if the property is not found, or proplist is NULL. 3011 * 3012 * Example: 3013 * See <mosquitto_property_read_byte> 3014 */ 3015 libmosq_EXPORT const mosquitto_property *mosquitto_property_read_int16( 3016 const mosquitto_property *proplist, 3017 int identifier, 3018 uint16_t *value, 3019 bool skip_first); 3020 3021 /* 3022 * Function: mosquitto_property_read_int32 3023 * 3024 * Read an int32 property value from a property. 3025 * 3026 * Parameters: 3027 * property - pointer to mosquitto_property pointer, the list of properties 3028 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 3029 * value - pointer to store the value, or NULL if the value is not required. 3030 * skip_first - boolean that indicates whether the first item in the list 3031 * should be ignored or not. Should usually be set to false. 3032 * 3033 * Returns: 3034 * A valid property pointer if the property is found 3035 * NULL, if the property is not found, or proplist is NULL. 3036 * 3037 * Example: 3038 * See <mosquitto_property_read_byte> 3039 */ 3040 libmosq_EXPORT const mosquitto_property *mosquitto_property_read_int32( 3041 const mosquitto_property *proplist, 3042 int identifier, 3043 uint32_t *value, 3044 bool skip_first); 3045 3046 /* 3047 * Function: mosquitto_property_read_varint 3048 * 3049 * Read a varint property value from a property. 3050 * 3051 * Parameters: 3052 * property - property to read 3053 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 3054 * value - pointer to store the value, or NULL if the value is not required. 3055 * skip_first - boolean that indicates whether the first item in the list 3056 * should be ignored or not. Should usually be set to false. 3057 * 3058 * Returns: 3059 * A valid property pointer if the property is found 3060 * NULL, if the property is not found, or proplist is NULL. 3061 * 3062 * Example: 3063 * See <mosquitto_property_read_byte> 3064 */ 3065 libmosq_EXPORT const mosquitto_property *mosquitto_property_read_varint( 3066 const mosquitto_property *proplist, 3067 int identifier, 3068 uint32_t *value, 3069 bool skip_first); 3070 3071 /* 3072 * Function: mosquitto_property_read_binary 3073 * 3074 * Read a binary property value from a property. 3075 * 3076 * On success, value must be free()'d by the application. 3077 * 3078 * Parameters: 3079 * property - property to read 3080 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 3081 * value - pointer to store the value, or NULL if the value is not required. 3082 * skip_first - boolean that indicates whether the first item in the list 3083 * should be ignored or not. Should usually be set to false. 3084 * 3085 * Returns: 3086 * A valid property pointer if the property is found 3087 * NULL, if the property is not found, or proplist is NULL, or if an out of memory condition occurred. 3088 * 3089 * Example: 3090 * See <mosquitto_property_read_byte> 3091 */ 3092 libmosq_EXPORT const mosquitto_property *mosquitto_property_read_binary( 3093 const mosquitto_property *proplist, 3094 int identifier, 3095 void **value, 3096 uint16_t *len, 3097 bool skip_first); 3098 3099 /* 3100 * Function: mosquitto_property_read_string 3101 * 3102 * Read a string property value from a property. 3103 * 3104 * On success, value must be free()'d by the application. 3105 * 3106 * Parameters: 3107 * property - property to read 3108 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 3109 * value - pointer to char*, for the property data to be stored in, or NULL if 3110 * the value is not required. 3111 * skip_first - boolean that indicates whether the first item in the list 3112 * should be ignored or not. Should usually be set to false. 3113 * 3114 * Returns: 3115 * A valid property pointer if the property is found 3116 * NULL, if the property is not found, or proplist is NULL, or if an out of memory condition occurred. 3117 * 3118 * Example: 3119 * See <mosquitto_property_read_byte> 3120 */ 3121 libmosq_EXPORT const mosquitto_property *mosquitto_property_read_string( 3122 const mosquitto_property *proplist, 3123 int identifier, 3124 char **value, 3125 bool skip_first); 3126 3127 /* 3128 * Function: mosquitto_property_read_string_pair 3129 * 3130 * Read a string pair property value pair from a property. 3131 * 3132 * On success, name and value must be free()'d by the application. 3133 * 3134 * Parameters: 3135 * property - property to read 3136 * identifier - property identifier (e.g. MQTT_PROP_PAYLOAD_FORMAT_INDICATOR) 3137 * name - pointer to char* for the name property data to be stored in, or NULL 3138 * if the name is not required. 3139 * value - pointer to char*, for the property data to be stored in, or NULL if 3140 * the value is not required. 3141 * skip_first - boolean that indicates whether the first item in the list 3142 * should be ignored or not. Should usually be set to false. 3143 * 3144 * Returns: 3145 * A valid property pointer if the property is found 3146 * NULL, if the property is not found, or proplist is NULL, or if an out of memory condition occurred. 3147 * 3148 * Example: 3149 * See <mosquitto_property_read_byte> 3150 */ 3151 libmosq_EXPORT const mosquitto_property *mosquitto_property_read_string_pair( 3152 const mosquitto_property *proplist, 3153 int identifier, 3154 char **name, 3155 char **value, 3156 bool skip_first); 3157 3158 /* 3159 * Function: mosquitto_property_free_all 3160 * 3161 * Free all properties from a list of properties. Frees the list and sets *properties to NULL. 3162 * 3163 * Parameters: 3164 * properties - list of properties to free 3165 * 3166 * Example: 3167 * > mosquitto_properties *properties = NULL; 3168 * > // Add properties 3169 * > mosquitto_property_free_all(&properties); 3170 */ 3171 libmosq_EXPORT void mosquitto_property_free_all(mosquitto_property **properties); 3172 3173 /* 3174 * Function: mosquitto_property_copy_all 3175 * 3176 * Parameters: 3177 * dest - pointer for new property list 3178 * src - property list 3179 * 3180 * Returns: 3181 * MOSQ_ERR_SUCCESS - on successful copy 3182 * MOSQ_ERR_INVAL - if dest is NULL 3183 * MOSQ_ERR_NOMEM - on out of memory (dest will be set to NULL) 3184 */ 3185 libmosq_EXPORT int mosquitto_property_copy_all(mosquitto_property **dest, const mosquitto_property *src); 3186 3187 /* 3188 * Function: mosquitto_property_check_command 3189 * 3190 * Check whether a property identifier is valid for the given command. 3191 * 3192 * Parameters: 3193 * command - MQTT command (e.g. CMD_CONNECT) 3194 * identifier - MQTT property (e.g. MQTT_PROP_USER_PROPERTY) 3195 * 3196 * Returns: 3197 * MOSQ_ERR_SUCCESS - if the identifier is valid for command 3198 * MOSQ_ERR_PROTOCOL - if the identifier is not valid for use with command. 3199 */ 3200 libmosq_EXPORT int mosquitto_property_check_command(int command, int identifier); 3201 3202 3203 /* 3204 * Function: mosquitto_property_check_all 3205 * 3206 * Check whether a list of properties are valid for a particular command, 3207 * whether there are duplicates, and whether the values are valid where 3208 * possible. 3209 * 3210 * Note that this function is used internally in the library whenever 3211 * properties are passed to it, so in basic use this is not needed, but should 3212 * be helpful to check property lists *before* the point of using them. 3213 * 3214 * Parameters: 3215 * command - MQTT command (e.g. CMD_CONNECT) 3216 * properties - list of MQTT properties to check. 3217 * 3218 * Returns: 3219 * MOSQ_ERR_SUCCESS - if all properties are valid 3220 * MOSQ_ERR_DUPLICATE_PROPERTY - if a property is duplicated where it is forbidden. 3221 * MOSQ_ERR_PROTOCOL - if any property is invalid 3222 */ 3223 libmosq_EXPORT int mosquitto_property_check_all(int command, const mosquitto_property *properties); 3224 3225 /* 3226 * Function: mosquitto_property_identifier_to_string 3227 * 3228 * Return the property name as a string for a property identifier. 3229 * The property name is as defined in the MQTT specification, with - as a 3230 * separator, for example: payload-format-indicator. 3231 * 3232 * Parameters: 3233 * identifier - valid MQTT property identifier integer 3234 * 3235 * Returns: 3236 * A const string to the property name on success 3237 * NULL on failure 3238 */ 3239 libmosq_EXPORT const char *mosquitto_property_identifier_to_string(int identifier); 3240 3241 3242 /* Function: mosquitto_string_to_property_info 3243 * 3244 * Parse a property name string and convert to a property identifier and data type. 3245 * The property name is as defined in the MQTT specification, with - as a 3246 * separator, for example: payload-format-indicator. 3247 * 3248 * Parameters: 3249 * propname - the string to parse 3250 * identifier - pointer to an int to receive the property identifier 3251 * type - pointer to an int to receive the property type 3252 * 3253 * Returns: 3254 * MOSQ_ERR_SUCCESS - on success 3255 * MOSQ_ERR_INVAL - if the string does not match a property 3256 * 3257 * Example: 3258 * (start code) 3259 * mosquitto_string_to_property_info("response-topic", &id, &type); 3260 * // id == MQTT_PROP_RESPONSE_TOPIC 3261 * // type == MQTT_PROP_TYPE_STRING 3262 * (end) 3263 */ 3264 libmosq_EXPORT int mosquitto_string_to_property_info(const char *propname, int *identifier, int *type); 3265 3266 3267 #ifdef __cplusplus 3268 } 3269 #endif 3270 3271 #endif
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |