Paho MQTT C Client Library
MQTTClient.h
Go to the documentation of this file.
1 /*******************************************************************************
2  * Copyright (c) 2009, 2018 IBM Corp.
3  *
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * and Eclipse Distribution License v1.0 which accompany this distribution.
7  *
8  * The Eclipse Public License is available at
9  * http://www.eclipse.org/legal/epl-v10.html
10  * and the Eclipse Distribution License is available at
11  * http://www.eclipse.org/org/documents/edl-v10.php.
12  *
13  * Contributors:
14  * Ian Craggs - initial API and implementation and/or initial documentation
15  * Ian Craggs, Allan Stockdill-Mander - SSL updates
16  * Ian Craggs - multiple server connection support
17  * Ian Craggs - MQTT 3.1.1 support
18  * Ian Craggs - remove const from eyecatchers #168
19  *******************************************************************************/
20 
103 /*
105 */
106 #if !defined(MQTTCLIENT_H)
107 #define MQTTCLIENT_H
108 
109 #if defined(__cplusplus)
110  extern "C" {
111 #endif
112 
113 #if defined(WIN32) || defined(WIN64)
114  #define DLLImport __declspec(dllimport)
115  #define DLLExport __declspec(dllexport)
116 #else
117  #define DLLImport extern
118  #define DLLExport __attribute__ ((visibility ("default")))
119 #endif
120 
121 #include <stdio.h>
122 /*
124 */
125 
126 #include "MQTTProperties.h"
127 #include "MQTTReasonCodes.h"
128 #include "MQTTSubscribeOpts.h"
129 #if !defined(NO_PERSISTENCE)
130 #include "MQTTClientPersistence.h"
131 #endif
132 
137 #define MQTTCLIENT_SUCCESS 0
138 
142 #define MQTTCLIENT_FAILURE -1
143 
144 /* error code -2 is MQTTCLIENT_PERSISTENCE_ERROR */
145 
149 #define MQTTCLIENT_DISCONNECTED -3
150 
154 #define MQTTCLIENT_MAX_MESSAGES_INFLIGHT -4
155 
158 #define MQTTCLIENT_BAD_UTF8_STRING -5
159 
162 #define MQTTCLIENT_NULL_PARAMETER -6
163 
168 #define MQTTCLIENT_TOPICNAME_TRUNCATED -7
169 
173 #define MQTTCLIENT_BAD_STRUCTURE -8
174 
177 #define MQTTCLIENT_BAD_QOS -9
178 
181 #define MQTTCLIENT_SSL_NOT_SUPPORTED -10
182 
185  #define MQTTCLIENT_BAD_MQTT_VERSION -11
186 
189 #define MQTTCLIENT_BAD_PROTOCOL -14
190 
193  #define MQTTCLIENT_BAD_MQTT_OPTION -15
194 
197  #define MQTTCLIENT_WRONG_MQTT_VERSION -16
198 
199 
203 #define MQTTVERSION_DEFAULT 0
204 
207 #define MQTTVERSION_3_1 3
208 
211 #define MQTTVERSION_3_1_1 4
212 
215  #define MQTTVERSION_5 5
216 
219 #define MQTT_BAD_SUBSCRIBE 0x80
220 
224 typedef struct
225 {
227  char struct_id[4];
233 
234 #define MQTTClient_init_options_initializer { {'M', 'Q', 'T', 'G'}, 0, 0 }
235 
241 
246 typedef void* MQTTClient;
258 typedef int MQTTClient_token;
259 
266 typedef struct
267 {
269  char struct_id[4];
276  void* payload;
290  int qos;
309  int retained;
316  int dup;
320  int msgid;
326 
327 #define MQTTClient_message_initializer { {'M', 'Q', 'T', 'M'}, 1, 0, NULL, 0, 0, 0, 0, MQTTProperties_initializer }
328 
355 typedef int MQTTClient_messageArrived(void* context, char* topicName, int topicLen, MQTTClient_message* message);
356 
378 
394 typedef void MQTTClient_connectionLost(void* context, char* cause);
395 
425 
426 
436 typedef void MQTTClient_disconnected(void* context, MQTTProperties* properties,
437  enum MQTTReasonCodes reasonCode);
438 
453 
477 typedef void MQTTClient_published(void* context, int dt, int packet_type, MQTTProperties* properties,
478  enum MQTTReasonCodes reasonCode);
479 
480 DLLExport int MQTTClient_setPublished(MQTTClient handle, void* context, MQTTClient_published* co);
481 
524 DLLExport int MQTTClient_create(MQTTClient* handle, const char* serverURI, const char* clientId,
525  int persistence_type, void* persistence_context);
526 
527 typedef struct
528 {
530  char struct_id[4];
540 
541 #define MQTTClient_createOptions_initializer { {'M', 'Q', 'C', 'O'}, MQTTVERSION_DEFAULT }
542 
587 DLLExport int MQTTClient_createWithOptions(MQTTClient* handle, const char* serverURI, const char* clientId,
588  int persistence_type, void* persistence_context, MQTTClient_createOptions* options);
589 
602 typedef struct
603 {
605  char struct_id[4];
611  const char* topicName;
613  const char* message;
617  int retained;
622  int qos;
624  struct
625  {
626  int len;
627  const void* data;
628  } payload;
630 
631 #define MQTTClient_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 1, NULL, NULL, 0, 0, {0, NULL} }
632 
633 #define MQTT_SSL_VERSION_DEFAULT 0
634 #define MQTT_SSL_VERSION_TLS_1_0 1
635 #define MQTT_SSL_VERSION_TLS_1_1 2
636 #define MQTT_SSL_VERSION_TLS_1_2 3
637 
650 typedef struct
651 {
653  char struct_id[4];
656 
658  const char* trustStore;
659 
663  const char* keyStore;
664 
668  const char* privateKey;
670  const char* privateKeyPassword;
671 
680  const char* enabledCipherSuites;
681 
684 
690 
696  int verify;
697 
703  const char* CApath;
704 
709  int (*ssl_error_cb) (const char *str, size_t len, void *u);
710 
716 
718 
719 #define MQTTClient_SSLOptions_initializer { {'M', 'Q', 'T', 'S'}, 3, NULL, NULL, NULL, NULL, NULL, 1, MQTT_SSL_VERSION_DEFAULT, 0, NULL, NULL, NULL }
720 
735 typedef struct
736 {
738  char struct_id[4];
789  int reliable;
801  const char* username;
807  const char* password;
839  char* const* serverURIs;
851  struct
852  {
853  const char* serverURI;
854  int MQTTVersion;
856  } returned;
860  struct
861  {
862  int len;
863  const void* data;
864  } binarypwd;
869  /*
870  * MQTT V5 clean start flag. Only clears state at the beginning of the session.
871  */
874 
875 #define MQTTClient_connectOptions_initializer { {'M', 'Q', 'T', 'C'}, 6, 60, 1, 1, NULL, NULL, NULL, 30, 0, NULL, 0, NULL, MQTTVERSION_DEFAULT, {NULL, 0, 0}, {0, NULL}, -1, 0}
876 
877 #define MQTTClient_connectOptions_initializer5 { {'M', 'Q', 'T', 'C'}, 6, 60, 0, 1, NULL, NULL, NULL, 30, 0, NULL, 0, NULL, MQTTVERSION_5, {NULL, 0, 0}, {0, NULL}, -1, 1}
878 
886 typedef struct
887 {
888  const char* name;
889  const char* value;
891 
898 
920 
921 
922 typedef struct MQTTResponse
923 {
924  int version;
926  int reasonCodeCount; /* used for subscribeMany5 and unsubscribeMany5 */
927  enum MQTTReasonCodes* reasonCodes; /* used for subscribeMany5 and unsubscribeMany5 */
928  MQTTProperties* properties; /* optional */
929 } MQTTResponse;
930 
931 #define MQTTResponse_initializer {1, MQTTREASONCODE_SUCCESS, 0, NULL, NULL}
932 
934 
936  MQTTProperties* connectProperties, MQTTProperties* willProperties);
937 
956 DLLExport int MQTTClient_disconnect(MQTTClient handle, int timeout);
957 
958 DLLExport int MQTTClient_disconnect5(MQTTClient handle, int timeout, enum MQTTReasonCodes reason, MQTTProperties* props);
959 
968 
969 
970 /* Subscribe is synchronous. QoS list parameter is changed on return to granted QoSs.
971  Returns return code, MQTTCLIENT_SUCCESS == success, non-zero some sort of error (TBD) */
972 
986 DLLExport int MQTTClient_subscribe(MQTTClient handle, const char* topic, int qos);
987 
988 
989 DLLExport MQTTResponse MQTTClient_subscribe5(MQTTClient handle, const char* topic, int qos,
990  MQTTSubscribe_options* opts, MQTTProperties* props);
991 
1008 DLLExport int MQTTClient_subscribeMany(MQTTClient handle, int count, char* const* topic, int* qos);
1009 
1010 DLLExport MQTTResponse MQTTClient_subscribeMany5(MQTTClient handle, int count, char* const* topic,
1011  int* qos, MQTTSubscribe_options* opts, MQTTProperties* props);
1012 
1024 DLLExport int MQTTClient_unsubscribe(MQTTClient handle, const char* topic);
1025 
1026 DLLExport MQTTResponse MQTTClient_unsubscribe5(MQTTClient handle, const char* topic, MQTTProperties* props);
1027 
1039 DLLExport int MQTTClient_unsubscribeMany(MQTTClient handle, int count, char* const* topic);
1040 
1041 DLLExport MQTTResponse MQTTClient_unsubscribeMany5(MQTTClient handle, int count, char* const* topic, MQTTProperties* props);
1042 
1064 DLLExport int MQTTClient_publish(MQTTClient handle, const char* topicName, int payloadlen, const void* payload, int qos, int retained,
1066 
1067 DLLExport MQTTResponse MQTTClient_publish5(MQTTClient handle, const char* topicName, int payloadlen, const void* payload,
1068  int qos, int retained, MQTTProperties* properties, MQTTClient_deliveryToken* dt);
1088 DLLExport int MQTTClient_publishMessage(MQTTClient handle, const char* topicName, MQTTClient_message* msg, MQTTClient_deliveryToken* dt);
1089 
1090 
1093 
1109 DLLExport int MQTTClient_waitForCompletion(MQTTClient handle, MQTTClient_deliveryToken dt, unsigned long timeout);
1110 
1111 
1131 
1138 DLLExport void MQTTClient_yield(void);
1139 
1171 DLLExport int MQTTClient_receive(MQTTClient handle, char** topicName, int* topicLen, MQTTClient_message** message,
1172  unsigned long timeout);
1173 
1185 
1194 DLLExport void MQTTClient_free(void* ptr);
1195 
1204 
1205 
1207 {
1215 };
1216 
1217 
1224 
1225 
1234 typedef void MQTTClient_traceCallback(enum MQTTCLIENT_TRACE_LEVELS level, char* message);
1235 
1243 
1249 DLLExport const char* MQTTClient_strerror(int code);
1250 
1251 #ifdef __cplusplus
1252  }
1253 #endif
1254 
1255 #endif
1256 
int MQTTVersion
Definition: MQTTClient.h:538
Definition: MQTTClient.h:1210
void MQTTClient_free(void *ptr)
int MQTTVersion
Definition: MQTTClient.h:847
int MQTTClient_getPendingDeliveryTokens(MQTTClient handle, MQTTClient_deliveryToken **tokens)
void * MQTTClient
Definition: MQTTClient.h:246
int MQTTClient_publish(MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTClient_deliveryToken *dt)
int struct_version
Definition: MQTTClient.h:532
const char * MQTTClient_strerror(int code)
void MQTTClient_yield(void)
const char * message
Definition: MQTTClient.h:613
int struct_version
Definition: MQTTClient.h:747
const char * username
Definition: MQTTClient.h:801
void MQTTClient_connectionLost(void *context, char *cause)
Definition: MQTTClient.h:394
int dup
Definition: MQTTClient.h:316
int MQTTClient_unsubscribe(MQTTClient handle, const char *topic)
const char * enabledCipherSuites
Definition: MQTTClient.h:680
int MQTTClient_subscribeMany(MQTTClient handle, int count, char *const *topic, int *qos)
int struct_version
Definition: MQTTClient.h:272
int qos
Definition: MQTTClient.h:290
const char * password
Definition: MQTTClient.h:807
const char * serverURI
Definition: MQTTClient.h:853
Definition: MQTTClient.h:1212
int msgid
Definition: MQTTClient.h:320
MQTTProperties * properties
Definition: MQTTClient.h:928
int retained
Definition: MQTTClient.h:617
const void * data
Definition: MQTTClient.h:627
int retryInterval
Definition: MQTTClient.h:819
int maxInflightMessages
Definition: MQTTClient.h:868
int reliable
Definition: MQTTClient.h:789
Definition: MQTTClient.h:266
int MQTTClient_receive(MQTTClient handle, char **topicName, int *topicLen, MQTTClient_message **message, unsigned long timeout)
MQTTProperties properties
Definition: MQTTClient.h:324
int cleanstart
Definition: MQTTClient.h:872
MQTTResponse MQTTClient_unsubscribeMany5(MQTTClient handle, int count, char *const *topic, MQTTProperties *props)
void MQTTClient_global_init(MQTTClient_init_options *inits)
int keepAliveInterval
Definition: MQTTClient.h:757
int payloadlen
Definition: MQTTClient.h:274
int verify
Definition: MQTTClient.h:696
const char * topicName
Definition: MQTTClient.h:611
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions *options)
int connectTimeout
Definition: MQTTClient.h:811
Definition: MQTTSubscribeOpts.h:21
Definition: MQTTClient.h:224
int struct_version
Definition: MQTTClient.h:655
int MQTTClient_disconnect(MQTTClient handle, int timeout)
void MQTTClient_traceCallback(enum MQTTCLIENT_TRACE_LEVELS level, char *message)
Definition: MQTTClient.h:1234
int MQTTClient_disconnect5(MQTTClient handle, int timeout, enum MQTTReasonCodes reason, MQTTProperties *props)
Definition: MQTTClient.h:1209
void MQTTClient_setTraceCallback(MQTTClient_traceCallback *callback)
int MQTTClient_publishMessage(MQTTClient handle, const char *topicName, MQTTClient_message *msg, MQTTClient_deliveryToken *dt)
#define DLLExport
Definition: MQTTProperties.h:58
MQTTResponse MQTTClient_subscribe5(MQTTClient handle, const char *topic, int qos, MQTTSubscribe_options *opts, MQTTProperties *props)
const void * data
Definition: MQTTClient.h:863
MQTTResponse MQTTClient_connect5(MQTTClient handle, MQTTClient_connectOptions *options, MQTTProperties *connectProperties, MQTTProperties *willProperties)
int MQTTClient_setPublished(MQTTClient handle, void *context, MQTTClient_published *co)
Definition: MQTTClient.h:650
enum MQTTReasonCodes * reasonCodes
Definition: MQTTClient.h:927
Definition: MQTTClient.h:922
int struct_version
Definition: MQTTClient.h:229
int qos
Definition: MQTTClient.h:622
Definition: MQTTClient.h:1214
int sslVersion
Definition: MQTTClient.h:689
int len
Definition: MQTTClient.h:862
Definition: MQTTClient.h:602
void * ssl_error_context
Definition: MQTTClient.h:715
int version
Definition: MQTTClient.h:924
int reasonCodeCount
Definition: MQTTClient.h:926
const char * trustStore
Definition: MQTTClient.h:658
Definition: MQTTClient.h:1211
int MQTTClient_unsubscribeMany(MQTTClient handle, int count, char *const *topic)
int sessionPresent
Definition: MQTTClient.h:855
enum MQTTReasonCodes reasonCode
Definition: MQTTClient.h:925
int MQTTClient_setCallbacks(MQTTClient handle, void *context, MQTTClient_connectionLost *cl, MQTTClient_messageArrived *ma, MQTTClient_deliveryComplete *dc)
Definition: MQTTClient.h:735
void MQTTClient_destroy(MQTTClient *handle)
int MQTTClient_createWithOptions(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context, MQTTClient_createOptions *options)
int MQTTClient_subscribe(MQTTClient handle, const char *topic, int qos)
MQTTResponse MQTTClient_unsubscribe5(MQTTClient handle, const char *topic, MQTTProperties *props)
MQTTResponse MQTTClient_subscribeMany5(MQTTClient handle, int count, char *const *topic, int *qos, MQTTSubscribe_options *opts, MQTTProperties *props)
const char * name
Definition: MQTTClient.h:888
MQTTClient_willOptions * will
Definition: MQTTClient.h:795
void MQTTClient_deliveryComplete(void *context, MQTTClient_deliveryToken dt)
Definition: MQTTClient.h:377
const char * privateKeyPassword
Definition: MQTTClient.h:670
int MQTTClient_deliveryToken
Definition: MQTTClient.h:257
char *const * serverURIs
Definition: MQTTClient.h:839
int MQTTClient_waitForCompletion(MQTTClient handle, MQTTClient_deliveryToken dt, unsigned long timeout)
const char * privateKey
Definition: MQTTClient.h:668
void MQTTClient_setTraceLevel(enum MQTTCLIENT_TRACE_LEVELS level)
MQTTResponse MQTTClient_publish5(MQTTClient handle, const char *topicName, int payloadlen, const void *payload, int qos, int retained, MQTTProperties *properties, MQTTClient_deliveryToken *dt)
MQTTClient_SSLOptions * ssl
Definition: MQTTClient.h:824
Definition: MQTTProperties.h:118
Definition: MQTTClient.h:1208
MQTTReasonCodes
Definition: MQTTReasonCodes.h:21
int MQTTClient_isConnected(MQTTClient handle)
int MQTTClient_token
Definition: MQTTClient.h:258
int MQTTClient_setDisconnected(MQTTClient handle, void *context, MQTTClient_disconnected *co)
int serverURIcount
Definition: MQTTClient.h:828
Definition: MQTTClient.h:1213
int MQTTClient_create(MQTTClient *handle, const char *serverURI, const char *clientId, int persistence_type, void *persistence_context)
const char * keyStore
Definition: MQTTClient.h:663
void MQTTResponse_free(MQTTResponse response)
MQTTResponse MQTTClient_publishMessage5(MQTTClient handle, const char *topicName, MQTTClient_message *msg, MQTTClient_deliveryToken *dt)
int retained
Definition: MQTTClient.h:309
int enableServerCertAuth
Definition: MQTTClient.h:683
int len
Definition: MQTTClient.h:626
void MQTTClient_disconnected(void *context, MQTTProperties *properties, enum MQTTReasonCodes reasonCode)
Definition: MQTTClient.h:436
struct MQTTResponse MQTTResponse
MQTTClient_nameValue * MQTTClient_getVersionInfo(void)
const char * value
Definition: MQTTClient.h:889
void MQTTClient_published(void *context, int dt, int packet_type, MQTTProperties *properties, enum MQTTReasonCodes reasonCode)
Definition: MQTTClient.h:477
int cleansession
Definition: MQTTClient.h:779
int do_openssl_init
Definition: MQTTClient.h:231
This structure represents a persistent data store, used to store outbound and inbound messages...
Definition: MQTTClient.h:527
int struct_version
Definition: MQTTClient.h:609
void * payload
Definition: MQTTClient.h:276
int MQTTClient_messageArrived(void *context, char *topicName, int topicLen, MQTTClient_message *message)
Definition: MQTTClient.h:355
void MQTTClient_freeMessage(MQTTClient_message **msg)
Definition: MQTTClient.h:886
const char * CApath
Definition: MQTTClient.h:703
MQTTCLIENT_TRACE_LEVELS
Definition: MQTTClient.h:1206