libemqtt.h 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. /*
  2. * This file is part of libemqtt.
  3. *
  4. * libemqtt is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * libemqtt is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with libemqtt. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*
  18. *
  19. * Created by Filipe Varela on 09/10/16.
  20. * Copyright 2009 Caixa Mágica Software. All rights reserved.
  21. *
  22. * Fork developed by Vicente Ruiz Rodríguez
  23. * Copyright 2012 Vicente Ruiz Rodríguez <vruiz2.0@gmail.com>. All rights reserved.
  24. *
  25. */
  26. #ifndef __LIBEMQTT_H__
  27. #define __LIBEMQTT_H__
  28. #include <stdint.h>
  29. /**
  30. * @defgroup luatos_MQTT MQTT相关接口
  31. * @{
  32. */
  33. #ifndef MQTT_CONF_CLIENT_ID_LENGTH
  34. #define MQTT_CONF_CLIENT_ID_LENGTH 192
  35. #endif
  36. #ifndef MQTT_CONF_USERNAME_LENGTH
  37. #define MQTT_CONF_USERNAME_LENGTH 192 // Recommended by MQTT Specification (12 + '\0')
  38. #endif
  39. #ifndef MQTT_CONF_PASSWORD_LENGTH
  40. #define MQTT_CONF_PASSWORD_LENGTH 512 // Recommended by MQTT Specification (12 + '\0')
  41. #endif
  42. #define MQTT_MSG_CONNECT 1<<4
  43. #define MQTT_MSG_CONNACK 2<<4
  44. #define MQTT_MSG_PUBLISH 3<<4
  45. #define MQTT_MSG_PUBACK 4<<4
  46. #define MQTT_MSG_PUBREC 5<<4
  47. #define MQTT_MSG_PUBREL 6<<4
  48. #define MQTT_MSG_PUBCOMP 7<<4
  49. #define MQTT_MSG_SUBSCRIBE 8<<4
  50. #define MQTT_MSG_SUBACK 9<<4
  51. #define MQTT_MSG_UNSUBSCRIBE 10<<4
  52. #define MQTT_MSG_UNSUBACK 11<<4
  53. #define MQTT_MSG_PINGREQ 12<<4
  54. #define MQTT_MSG_PINGRESP 13<<4
  55. #define MQTT_MSG_DISCONNECT 14<<4 /**< mqtt 关闭断开消息(只要有断开就会上报,无论是否重连) */
  56. /** Extract the message type from buffer.
  57. * @param buffer Pointer to the packet.
  58. *
  59. * @return Message Type byte.
  60. */
  61. #define MQTTParseMessageType(buffer) ( *buffer & 0xF0 )
  62. /** Indicate if it is a duplicate packet.
  63. * @param buffer Pointer to the packet.
  64. *
  65. * @retval 0 Not duplicate.
  66. * @retval !=0 Duplicate.
  67. */
  68. #define MQTTParseMessageDuplicate(buffer) ( *buffer & 0x08 )
  69. /** Extract the message QoS level.
  70. * @param buffer Pointer to the packet.
  71. *
  72. * @return QoS Level (0, 1 or 2).
  73. */
  74. #define MQTTParseMessageQos(buffer) ( (*buffer & 0x06) >> 1 )
  75. /** Indicate if this packet has a retain flag.
  76. * @param buffer Pointer to the packet.
  77. *
  78. * @retval 0 Not duplicate.
  79. * @retval !=0 Duplicate.
  80. */
  81. #define MQTTParseMessageRetain(buffer) ( *buffer & 0x01 )
  82. /** Parse packet buffer for number of bytes in remaining length field.
  83. *
  84. * Given a packet, return number of bytes in remaining length
  85. * field in MQTT fixed header. Can be from 1 - 4 bytes depending
  86. * on length of message.
  87. *
  88. * @param buf Pointer to the packet.
  89. *
  90. * @retval number of bytes
  91. */
  92. uint8_t mqtt_num_rem_len_bytes(const uint8_t* buf);
  93. /** Parse packet buffer for remaning length value.
  94. *
  95. * Given a packet, return remaining length value (in fixed header).
  96. *
  97. * @param buf Pointer to the packet.
  98. *
  99. * @retval remaining length
  100. */
  101. uint32_t mqtt_parse_rem_len(const uint8_t* buf);
  102. /** Parse packet buffer for message id.
  103. *
  104. * @param buf Pointer to the packet.
  105. *
  106. * @retval message id
  107. */
  108. uint16_t mqtt_parse_msg_id(const uint8_t* buf);
  109. /** Parse a packet buffer for the publish topic.
  110. *
  111. * Given a packet containing an MQTT publish message,
  112. * return the message topic.
  113. *
  114. * @param buf Pointer to the packet.
  115. * @param topic Pointer destination buffer for topic
  116. *
  117. * @retval size in bytes of topic (0 = no publish message in buffer)
  118. */
  119. uint16_t mqtt_parse_pub_topic(const uint8_t* buf, uint8_t* topic);
  120. /** Parse a packet buffer for a pointer to the publish topic.
  121. *
  122. * Not called directly - called by mqtt_parse_pub_topic
  123. */
  124. uint16_t mqtt_parse_pub_topic_ptr(const uint8_t* buf, const uint8_t** topic_ptr);
  125. /** Parse a packet buffer for the publish message.
  126. *
  127. * Given a packet containing an MQTT publish message,
  128. * return the message.
  129. *
  130. * @param buf Pointer to the packet.
  131. * @param msg Pointer destination buffer for message
  132. *
  133. * @retval size in bytes of topic (0 = no publish message in buffer)
  134. */
  135. uint32_t mqtt_parse_publish_msg(const uint8_t* buf, uint8_t* msg);
  136. /** Parse a packet buffer for a pointer to the publish message.
  137. *
  138. * Not called directly - called by mqtt_parse_pub_msg
  139. */
  140. uint32_t mqtt_parse_pub_msg_ptr(const uint8_t* buf, const uint8_t** msg_ptr);
  141. typedef struct {
  142. void* socket_info;
  143. int (*send)(void* socket_info, const void* buf, unsigned int count);
  144. // Connection info
  145. char clientid[MQTT_CONF_CLIENT_ID_LENGTH];
  146. // Auth fields
  147. char username[MQTT_CONF_USERNAME_LENGTH];
  148. char password[MQTT_CONF_PASSWORD_LENGTH];
  149. // Management fields
  150. uint16_t seq;
  151. uint16_t alive;
  152. // Will topic
  153. uint8_t will_retain;
  154. uint8_t will_qos;
  155. uint8_t clean_session;
  156. char *will_data; // 包含topic和payload
  157. uint32_t will_len;
  158. } mqtt_broker_handle_t;
  159. /** Initialize the information to connect to the broker.
  160. * @param broker Data structure that contains the connection information with the broker.
  161. * @param clientid A string that identifies the client id.
  162. *
  163. * @note Only has effect before to call mqtt_connect
  164. */
  165. void mqtt_init(mqtt_broker_handle_t* broker, const char* clientid);
  166. /** Enable the authentication to connect to the broker.
  167. * @param broker Data structure that contains the connection information with the broker.
  168. * @param username A string that contains the username.
  169. * @param password A string that contains the password.
  170. *
  171. * @note Only has effect before to call mqtt_connect
  172. */
  173. void mqtt_init_auth(mqtt_broker_handle_t* broker, const char* username, const char* password);
  174. /** Set the keep alive timer.
  175. * @param broker Data structure that contains the connection information with the broker.
  176. * @param alive Keep aliver timer value (in seconds).
  177. *
  178. * @note Only has effect before to call mqtt_connect
  179. */
  180. void mqtt_set_alive(mqtt_broker_handle_t* broker, uint16_t alive);
  181. int mqtt_set_will(mqtt_broker_handle_t* broker, const char* topic,
  182. const char* payload, size_t payload_len,
  183. uint8_t qos, size_t retain);
  184. /** Connect to the broker.
  185. * @param broker Data structure that contains the connection information with the broker.
  186. *
  187. * @retval 1 On success.
  188. * @retval 0 On connection error.
  189. * @retval -1 On IO error.
  190. */
  191. int mqtt_connect(mqtt_broker_handle_t* broker);
  192. /** Disconnect to the broker.
  193. * @param broker Data structure that contains the connection information with the broker.
  194. *
  195. * @note The socket must also be closed.
  196. *
  197. * @retval 1 On success.
  198. * @retval 0 On connection error.
  199. * @retval -1 On IO error.
  200. */
  201. int mqtt_disconnect(mqtt_broker_handle_t* broker);
  202. /** Publish a message on a topic. This message will be published with 0 Qos level.
  203. * @param broker Data structure that contains the connection information with the broker.
  204. * @param topic The topic name.
  205. * @param msg The message.
  206. * @param retain Enable or disable the Retain flag (values: 0 or 1).
  207. *
  208. * @retval 1 On success.
  209. * @retval 0 On connection error.
  210. * @retval -1 On IO error.
  211. */
  212. int mqtt_publish(mqtt_broker_handle_t* broker, const char* topic, const char* msg, uint32_t msg_len, uint8_t retain);
  213. /** Publish a message on a topic.
  214. * @param broker Data structure that contains the connection information with the broker.
  215. * @param topic The topic name.
  216. * @param msg The message.
  217. * @param retain Enable or disable the Retain flag (values: 0 or 1).
  218. * @param qos Quality of Service (values: 0, 1 or 2)
  219. * @param message_id Variable that will store the Message ID, if the pointer is not NULL.
  220. *
  221. * @retval 1 On success.
  222. * @retval 0 On connection error.
  223. * @retval -1 On IO error.
  224. */
  225. int mqtt_publish_with_qos(mqtt_broker_handle_t* broker, const char* topic, const char* msg, uint32_t msg_len, uint8_t retain, uint8_t qos, uint16_t* message_id);
  226. /** Send a PUBREL message. It's used for PUBLISH message with 2 QoS level.
  227. * @param broker Data structure that contains the connection information with the broker.
  228. * @param message_id Message ID
  229. *
  230. * @retval 1 On success.
  231. * @retval 0 On connection error.
  232. * @retval -1 On IO error.
  233. */
  234. int mqtt_pubrel(mqtt_broker_handle_t* broker, uint16_t message_id);
  235. /** Subscribe to a topic.
  236. * @param broker Data structure that contains the connection information with the broker.
  237. * @param topic The topic name.
  238. * @param message_id Variable that will store the Message ID, if the pointer is not NULL.
  239. *
  240. * @retval 1 On success.
  241. * @retval 0 On connection error.
  242. * @retval -1 On IO error.
  243. */
  244. int mqtt_subscribe(mqtt_broker_handle_t* broker, const char* topic, uint16_t* message_id, uint8_t qos);
  245. /** Unsubscribe from a topic.
  246. * @param broker Data structure that contains the connection information with the broker.
  247. * @param topic The topic name.
  248. * @param message_id Variable that will store the Message ID, if the pointer is not NULL.
  249. *
  250. * @retval 1 On success.
  251. * @retval 0 On connection error.
  252. * @retval -1 On IO error.
  253. */
  254. int mqtt_unsubscribe(mqtt_broker_handle_t* broker, const char* topic, uint16_t* message_id);
  255. /** Make a ping.
  256. * @param broker Data structure that contains the connection information with the broker.
  257. *
  258. * @retval 1 On success.
  259. * @retval 0 On connection error.
  260. * @retval -1 On IO error.
  261. */
  262. int mqtt_ping(mqtt_broker_handle_t* broker);
  263. int mqtt_puback(mqtt_broker_handle_t* broker, uint16_t message_id);
  264. int mqtt_pubrec(mqtt_broker_handle_t* broker, uint16_t message_id);
  265. int mqtt_pubcomp(mqtt_broker_handle_t* broker, uint16_t message_id);
  266. /** @}*/
  267. #endif // __LIBEMQTT_H__