mqttclient.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /*
  2. * @Author: jiejie
  3. * @Github: https://github.com/jiejieTop
  4. * @Date: 2019-12-09 21:31:25
  5. * @LastEditTime : 2022-06-11 22:45:02
  6. * @Description: the code belongs to jiejie, please keep the author information and source code according to the license.
  7. */
  8. #ifndef _MQTTCLIENT_H_
  9. #define _MQTTCLIENT_H_
  10. #include <stdio.h>
  11. #include <stdint.h>
  12. #include <string.h>
  13. #include "MQTTPacket.h"
  14. #include "mqtt_list.h"
  15. #include "platform_timer.h"
  16. #include "platform_memory.h"
  17. #include "platform_mutex.h"
  18. #include "platform_thread.h"
  19. #include "mqtt_defconfig.h"
  20. #include "network.h"
  21. #include "random.h"
  22. #include "mqtt_error.h"
  23. #include "mqtt_log.h"
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. typedef enum mqtt_qos {
  28. QOS0 = 0,
  29. QOS1 = 1,
  30. QOS2 = 2,
  31. SUBFAIL = 0x80
  32. } mqtt_qos_t;
  33. typedef enum client_state {
  34. CLIENT_STATE_INVALID = -1,
  35. CLIENT_STATE_INITIALIZED = 0,
  36. CLIENT_STATE_CONNECTED = 1,
  37. CLIENT_STATE_DISCONNECTED = 2,
  38. CLIENT_STATE_CLEAN_SESSION = 3
  39. }client_state_t;
  40. typedef struct mqtt_connack_data {
  41. uint8_t rc;
  42. uint8_t session_present;
  43. } mqtt_connack_data_t;
  44. typedef struct mqtt_message {
  45. mqtt_qos_t qos;
  46. uint8_t retained;
  47. uint8_t dup;
  48. uint16_t id;
  49. size_t payloadlen;
  50. void *payload;
  51. } mqtt_message_t;
  52. typedef struct message_data {
  53. char topic_name[MQTT_TOPIC_LEN_MAX];
  54. mqtt_message_t *message;
  55. } message_data_t;
  56. typedef void (*interceptor_handler_t)(void* client, message_data_t* msg);
  57. typedef void (*message_handler_t)(void* client, message_data_t* msg);
  58. typedef void (*reconnect_handler_t)(void* client, void* reconnect_date);
  59. typedef struct message_handlers {
  60. mqtt_list_t list;
  61. mqtt_qos_t qos;
  62. const char* topic_filter;
  63. message_handler_t handler;
  64. } message_handlers_t;
  65. typedef struct ack_handlers {
  66. mqtt_list_t list;
  67. platform_timer_t timer;
  68. uint32_t type;
  69. uint16_t packet_id;
  70. message_handlers_t *handler;
  71. uint16_t payload_len;
  72. uint8_t *payload;
  73. } ack_handlers_t;
  74. typedef struct mqtt_will_options {
  75. mqtt_qos_t will_qos;
  76. uint8_t will_retained;
  77. char *will_topic;
  78. char *will_message;
  79. } mqtt_will_options_t;
  80. typedef struct mqtt_client {
  81. char *mqtt_client_id;
  82. char *mqtt_user_name;
  83. char *mqtt_password;
  84. char *mqtt_host;
  85. char *mqtt_port;
  86. char *mqtt_ca;
  87. void *mqtt_reconnect_data;
  88. uint8_t *mqtt_read_buf;
  89. uint8_t *mqtt_write_buf;
  90. uint16_t mqtt_keep_alive_interval;
  91. uint16_t mqtt_packet_id;
  92. uint32_t mqtt_will_flag : 1;
  93. uint32_t mqtt_clean_session : 1;
  94. uint32_t mqtt_ping_outstanding : 2;
  95. uint32_t mqtt_version : 4;
  96. uint32_t mqtt_ack_handler_number : 24;
  97. uint32_t mqtt_cmd_timeout;
  98. uint32_t mqtt_read_buf_size;
  99. uint32_t mqtt_write_buf_size;
  100. uint32_t mqtt_reconnect_try_duration;
  101. size_t mqtt_client_id_len;
  102. size_t mqtt_user_name_len;
  103. size_t mqtt_password_len;
  104. mqtt_will_options_t *mqtt_will_options;
  105. client_state_t mqtt_client_state;
  106. platform_mutex_t mqtt_write_lock;
  107. platform_mutex_t mqtt_global_lock;
  108. mqtt_list_t mqtt_msg_handler_list;
  109. mqtt_list_t mqtt_ack_handler_list;
  110. network_t *mqtt_network;
  111. platform_thread_t *mqtt_thread;
  112. platform_timer_t mqtt_last_sent;
  113. platform_timer_t mqtt_last_received;
  114. reconnect_handler_t mqtt_reconnect_handler;
  115. interceptor_handler_t mqtt_interceptor_handler;
  116. } mqtt_client_t;
  117. #define MQTT_ROBUSTNESS_CHECK(item, err) if (!(item)) { \
  118. MQTT_LOG_E("%s:%d %s()... check for error.", __FILE__, __LINE__, __FUNCTION__); \
  119. return err; }
  120. #define MQTT_CLIENT_SET_DEFINE(name, type, res) \
  121. type mqtt_set_##name(mqtt_client_t *c, type t) { \
  122. MQTT_ROBUSTNESS_CHECK((c), res); \
  123. c->mqtt_##name = t; \
  124. return c->mqtt_##name; \
  125. }
  126. #define MQTT_CLIENT_SET_STATEMENT(name, type) \
  127. type mqtt_set_##name(mqtt_client_t *, type);
  128. MQTT_CLIENT_SET_STATEMENT(client_id, char*)
  129. MQTT_CLIENT_SET_STATEMENT(user_name, char*)
  130. MQTT_CLIENT_SET_STATEMENT(password, char*)
  131. MQTT_CLIENT_SET_STATEMENT(host, char*)
  132. MQTT_CLIENT_SET_STATEMENT(port, char*)
  133. MQTT_CLIENT_SET_STATEMENT(ca, char*)
  134. MQTT_CLIENT_SET_STATEMENT(reconnect_data, void*)
  135. MQTT_CLIENT_SET_STATEMENT(keep_alive_interval, uint16_t)
  136. MQTT_CLIENT_SET_STATEMENT(will_flag, uint32_t)
  137. MQTT_CLIENT_SET_STATEMENT(clean_session, uint32_t)
  138. MQTT_CLIENT_SET_STATEMENT(version, uint32_t)
  139. MQTT_CLIENT_SET_STATEMENT(cmd_timeout, uint32_t)
  140. MQTT_CLIENT_SET_STATEMENT(read_buf_size, uint32_t)
  141. MQTT_CLIENT_SET_STATEMENT(write_buf_size, uint32_t)
  142. MQTT_CLIENT_SET_STATEMENT(reconnect_try_duration, uint32_t)
  143. MQTT_CLIENT_SET_STATEMENT(reconnect_handler, reconnect_handler_t)
  144. MQTT_CLIENT_SET_STATEMENT(interceptor_handler, interceptor_handler_t)
  145. void mqtt_sleep_ms(int ms);
  146. mqtt_client_t *mqtt_lease(void);
  147. int mqtt_release(mqtt_client_t* c);
  148. int mqtt_connect(mqtt_client_t* c);
  149. int mqtt_disconnect(mqtt_client_t* c);
  150. int mqtt_keep_alive(mqtt_client_t* c);
  151. int mqtt_subscribe(mqtt_client_t* c, const char* topic_filter, mqtt_qos_t qos, message_handler_t msg_handler);
  152. int mqtt_unsubscribe(mqtt_client_t* c, const char* topic_filter);
  153. int mqtt_publish(mqtt_client_t* c, const char* topic_filter, mqtt_message_t* msg);
  154. int mqtt_list_subscribe_topic(mqtt_client_t* c);
  155. int mqtt_set_will_options(mqtt_client_t* c, char *topic, mqtt_qos_t qos, uint8_t retained, char *message);
  156. #ifdef __cplusplus
  157. }
  158. #endif
  159. #endif /* _MQTTCLIENT_H_ */