MQTTClient.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*******************************************************************************
  2. * Copyright (c) 2014, 2017 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. * Allan Stockdill-Mander/Ian Craggs - initial API and implementation and/or initial documentation
  15. * Ian Craggs - documentation and platform specific header
  16. * Ian Craggs - add setMessageHandler function
  17. *******************************************************************************/
  18. #if !defined(MQTT_CLIENT_H)
  19. #define MQTT_CLIENT_H
  20. #if defined(__cplusplus)
  21. extern "C" {
  22. #endif
  23. #if defined(WIN32_DLL) || defined(WIN64_DLL)
  24. #define DLLImport __declspec(dllimport)
  25. #define DLLExport __declspec(dllexport)
  26. #elif defined(LINUX_SO)
  27. #define DLLImport extern
  28. #define DLLExport __attribute__ ((visibility ("default")))
  29. #else
  30. #define DLLImport
  31. #define DLLExport
  32. #endif
  33. #include <MQTTPacket.h>
  34. #include <MQTTInterface.h>
  35. #if defined(MQTTCLIENT_PLATFORM_HEADER)
  36. /* The following sequence of macros converts the MQTTCLIENT_PLATFORM_HEADER value
  37. * into a string constant suitable for use with include.
  38. */
  39. #define xstr(s) str(s)
  40. #define str(s) #s
  41. #include xstr(MQTTCLIENT_PLATFORM_HEADER)
  42. #endif
  43. #define MAX_PACKET_ID 65535 /* according to the MQTT specification - do not change! */
  44. #if !defined(MAX_MESSAGE_HANDLERS)
  45. #define MAX_MESSAGE_HANDLERS 5 /* redefinable - how many subscriptions do you want? */
  46. #endif
  47. enum QoS { QOS0, QOS1, QOS2, SUBFAIL=(int)0x80 };
  48. /* all failure return codes must be negative */
  49. enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, MQTT_SUCCESS = 0 };
  50. /* The Platform specific header must define the Network and Timer structures and functions
  51. * which operate on them.
  52. *
  53. typedef struct Network
  54. {
  55. int (*mqttread)(Network*, unsigned char* read_buffer, int, int);
  56. int (*mqttwrite)(Network*, unsigned char* send_buffer, int, int);
  57. } Network;*/
  58. /* The Timer structure must be defined in the platform specific header,
  59. * and have the following functions to operate on it. */
  60. extern void TimerInit(Timer*);
  61. extern char TimerIsExpired(Timer*);
  62. extern void TimerCountdownMS(Timer*, unsigned int);
  63. extern void TimerCountdown(Timer*, unsigned int);
  64. extern int TimerLeftMS(Timer*);
  65. typedef struct MQTTMessage
  66. {
  67. enum QoS qos;
  68. unsigned char retained;
  69. unsigned char dup;
  70. unsigned short id;
  71. void *payload;
  72. size_t payloadlen;
  73. } MQTTMessage;
  74. typedef struct MessageData
  75. {
  76. MQTTMessage* message;
  77. MQTTString* topicName;
  78. } MessageData;
  79. typedef struct MQTTConnackData
  80. {
  81. unsigned char rc;
  82. unsigned char sessionPresent;
  83. } MQTTConnackData;
  84. typedef struct MQTTSubackData
  85. {
  86. enum QoS grantedQoS;
  87. } MQTTSubackData;
  88. typedef void (*messageHandler)(MessageData*);
  89. typedef struct MQTTClient
  90. {
  91. unsigned int next_packetid,
  92. command_timeout_ms;
  93. size_t buf_size,
  94. readbuf_size;
  95. unsigned char *buf,
  96. *readbuf;
  97. unsigned int keepAliveInterval;
  98. char ping_outstanding;
  99. int isconnected;
  100. int cleansession;
  101. struct MessageHandlers
  102. {
  103. const char* topicFilter;
  104. void (*fp) (MessageData*);
  105. } messageHandlers[MAX_MESSAGE_HANDLERS]; /* Message handlers are indexed by subscription topic */
  106. void (*defaultMessageHandler) (MessageData*);
  107. Network* ipstack;
  108. Timer last_sent, last_received;
  109. osMutexId_t mutex;
  110. #if defined(MQTT_TASK)
  111. Mutex mutex;
  112. Thread thread;
  113. #endif
  114. } MQTTClient;
  115. #define DefaultClient {0, 0, 0, 0, NULL, NULL, 0, 0, 0}
  116. /**
  117. * Create an MQTT client object
  118. * @param client
  119. * @param network
  120. * @param command_timeout_ms
  121. * @param
  122. */
  123. DLLExport void MQTTClientInit(MQTTClient* client, Network* network, unsigned int command_timeout_ms,
  124. unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size);
  125. /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
  126. * The nework object must be connected to the network endpoint before calling this
  127. * @param options - connect options
  128. * @return success code
  129. */
  130. DLLExport int MQTTConnectWithResults(MQTTClient* client, MQTTPacket_connectData* options,
  131. MQTTConnackData* data);
  132. /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
  133. * The nework object must be connected to the network endpoint before calling this
  134. * @param options - connect options
  135. * @return success code
  136. */
  137. DLLExport int MQTTConnect(MQTTClient* client, MQTTPacket_connectData* options);
  138. /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
  139. * @param client - the client object to use
  140. * @param topic - the topic to publish to
  141. * @param message - the message to send
  142. * @return success code
  143. */
  144. DLLExport int MQTTPublish(MQTTClient* client, const char*, MQTTMessage*);
  145. /** MQTT SetMessageHandler - set or remove a per topic message handler
  146. * @param client - the client object to use
  147. * @param topicFilter - the topic filter set the message handler for
  148. * @param messageHandler - pointer to the message handler function or NULL to remove
  149. * @return success code
  150. */
  151. DLLExport int MQTTSetMessageHandler(MQTTClient* c, const char* topicFilter, messageHandler messageHandler);
  152. /** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning.
  153. * @param client - the client object to use
  154. * @param topicFilter - the topic filter to subscribe to
  155. * @param message - the message to send
  156. * @return success code
  157. */
  158. DLLExport int MQTTSubscribe(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler);
  159. /** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning.
  160. * @param client - the client object to use
  161. * @param topicFilter - the topic filter to subscribe to
  162. * @param message - the message to send
  163. * @param data - suback granted QoS returned
  164. * @return success code
  165. */
  166. DLLExport int MQTTSubscribeWithResults(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler, MQTTSubackData* data);
  167. /** MQTT Subscribe - send an MQTT unsubscribe packet and wait for unsuback before returning.
  168. * @param client - the client object to use
  169. * @param topicFilter - the topic filter to unsubscribe from
  170. * @return success code
  171. */
  172. DLLExport int MQTTUnsubscribe(MQTTClient* client, const char* topicFilter);
  173. /** MQTT Disconnect - send an MQTT disconnect packet and close the connection
  174. * @param client - the client object to use
  175. * @return success code
  176. */
  177. DLLExport int MQTTDisconnect(MQTTClient* client);
  178. /** MQTT Yield - MQTT background
  179. * @param client - the client object to use
  180. * @param time - the time, in milliseconds, to yield for
  181. * @return success code
  182. */
  183. DLLExport int MQTTYield(MQTTClient* client, int time);
  184. /** MQTT isConnected
  185. * @param client - the client object to use
  186. * @return truth value indicating whether the client is connected to the server
  187. */
  188. DLLExport int MQTTIsConnected(MQTTClient* client);
  189. #if defined(MQTT_TASK)
  190. /** MQTT start background thread for a client. After this, MQTTYield should not be called.
  191. * @param client - the client object to use
  192. * @return success code
  193. */
  194. DLLExport int MQTTStartTask(MQTTClient* client);
  195. #endif
  196. #if defined(__cplusplus)
  197. }
  198. #endif
  199. #endif