MQTTClient.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*******************************************************************************
  2. * Copyright (c) 2014, 2015 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. *******************************************************************************/
  17. #if !defined(__MQTT_CLIENT_C_)
  18. #define __MQTT_CLIENT_C_
  19. #if defined(__cplusplus)
  20. extern "C" {
  21. #endif
  22. #if defined(WIN32_DLL) || defined(WIN64_DLL)
  23. #define DLLImport __declspec(dllimport)
  24. #define DLLExport __declspec(dllexport)
  25. #elif defined(LINUX_SO)
  26. #define DLLImport extern
  27. #define DLLExport __attribute__ ((visibility ("default")))
  28. #else
  29. #define DLLImport
  30. #define DLLExport
  31. #endif
  32. #include "./MQTTPacket/src/MQTTPacket.h"
  33. #include "stdio.h"
  34. #include "mqtt_interface.h"
  35. #define MAX_PACKET_ID 65535 /* according to the MQTT specification - do not change! */
  36. #if !defined(MAX_MESSAGE_HANDLERS)
  37. #define MAX_MESSAGE_HANDLERS 5 /* redefinable - how many subscriptions do you want? */
  38. #endif
  39. enum QoS { QOS0, QOS1, QOS2 };
  40. /* all failure return codes must be negative */
  41. enum returnCode { BUFFER_OVERFLOW = -2, FAILURE = -1, SUCCESSS = 0 };
  42. /* The Platform specific header must define the Network and Timer structures and functions
  43. * which operate on them.
  44. *
  45. typedef struct Network
  46. {
  47. int (*mqttread)(Network*, unsigned char* read_buffer, int, int);
  48. int (*mqttwrite)(Network*, unsigned char* send_buffer, int, int);
  49. } Network;*/
  50. /* The Timer structure must be defined in the platform specific header,
  51. * and have the following functions to operate on it. */
  52. extern void TimerInit(Timer*);
  53. extern char TimerIsExpired(Timer*);
  54. extern void TimerCountdownMS(Timer*, unsigned int);
  55. extern void TimerCountdown(Timer*, unsigned int);
  56. extern int TimerLeftMS(Timer*);
  57. typedef struct MQTTMessage
  58. {
  59. enum QoS qos;
  60. unsigned char retained;
  61. unsigned char dup;
  62. unsigned short id;
  63. void *payload;
  64. size_t payloadlen;
  65. } MQTTMessage;
  66. typedef struct MessageData
  67. {
  68. MQTTMessage* message;
  69. MQTTString* topicName;
  70. } MessageData;
  71. typedef void (*messageHandler)(MessageData*);
  72. typedef struct MQTTClient
  73. {
  74. unsigned int next_packetid,
  75. command_timeout_ms;
  76. size_t buf_size,
  77. readbuf_size;
  78. unsigned char *buf,
  79. *readbuf;
  80. unsigned int keepAliveInterval;
  81. char ping_outstanding;
  82. int isconnected;
  83. struct MessageHandlers
  84. {
  85. const char* topicFilter;
  86. void (*fp) (MessageData*);
  87. } messageHandlers[MAX_MESSAGE_HANDLERS]; /* Message handlers are indexed by subscription topic */
  88. void (*defaultMessageHandler) (MessageData*);
  89. Network* ipstack;
  90. Timer ping_timer;
  91. #if defined(MQTT_TASK)
  92. Mutex mutex;
  93. Thread thread;
  94. #endif
  95. } MQTTClient;
  96. #define DefaultClient {0, 0, 0, 0, NULL, NULL, 0, 0, 0}
  97. /**
  98. * Create an MQTT client object
  99. * @param client
  100. * @param network
  101. * @param command_timeout_ms
  102. * @param
  103. */
  104. DLLExport void MQTTClientInit(MQTTClient* client, Network* network, unsigned int command_timeout_ms,
  105. unsigned char* sendbuf, size_t sendbuf_size, unsigned char* readbuf, size_t readbuf_size);
  106. /** MQTT Connect - send an MQTT connect packet down the network and wait for a Connack
  107. * The nework object must be connected to the network endpoint before calling this
  108. * @param options - connect options
  109. * @return success code
  110. */
  111. DLLExport int MQTTConnect(MQTTClient* client, MQTTPacket_connectData* options);
  112. /** MQTT Publish - send an MQTT publish packet and wait for all acks to complete for all QoSs
  113. * @param client - the client object to use
  114. * @param topic - the topic to publish to
  115. * @param message - the message to send
  116. * @return success code
  117. */
  118. DLLExport int MQTTPublish(MQTTClient* client, const char*, MQTTMessage*);
  119. /** MQTT Subscribe - send an MQTT subscribe packet and wait for suback before returning.
  120. * @param client - the client object to use
  121. * @param topicFilter - the topic filter to subscribe to
  122. * @param message - the message to send
  123. * @return success code
  124. */
  125. DLLExport int MQTTSubscribe(MQTTClient* client, const char* topicFilter, enum QoS, messageHandler);
  126. /** MQTT Subscribe - send an MQTT unsubscribe packet and wait for unsuback before returning.
  127. * @param client - the client object to use
  128. * @param topicFilter - the topic filter to unsubscribe from
  129. * @return success code
  130. */
  131. DLLExport int MQTTUnsubscribe(MQTTClient* client, const char* topicFilter);
  132. /** MQTT Disconnect - send an MQTT disconnect packet and close the connection
  133. * @param client - the client object to use
  134. * @return success code
  135. */
  136. DLLExport int MQTTDisconnect(MQTTClient* client);
  137. /** MQTT Yield - MQTT background
  138. * @param client - the client object to use
  139. * @param time - the time, in milliseconds, to yield for
  140. * @return success code
  141. */
  142. DLLExport int MQTTYield(MQTTClient* client, int time);
  143. #if defined(MQTT_TASK)
  144. /** MQTT start background thread for a client. After this, MQTTYield should not be called.
  145. * @param client - the client object to use
  146. * @return success code
  147. */
  148. DLLExport int MQTTStartTask(MQTTClient* client);
  149. #endif
  150. #if defined(__cplusplus)
  151. }
  152. #endif
  153. #endif