ql_mqttclient.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. /*============================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =============================================================================*/
  5. /*===========================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ---------- ------------ ----------------------------------------------------
  11. =============================================================================*/
  12. #ifndef QL_MQTTLCLIENT_H
  13. #define QL_MQTTLCLIENT_H
  14. typedef int mqtt_client_t;
  15. #define MQTT_SSL_VERIFY_NONE 0
  16. #define MQTT_SSL_VERIFY_SERVER 1
  17. #define MQTT_SSL_VERIFY_SERVER_CLIENT 2
  18. #define MQTT_OPT_REC_BUF_MIN 16*1024
  19. #define MQTT_OPT_REC_BUF_MAX 64*1024
  20. struct mqtt_ssl_config_t{
  21. int ssl_ctx_id;
  22. int verify_level;
  23. char *cacert_path;
  24. char *client_cert_path;
  25. char *client_key_path;
  26. char *client_key_pwd;
  27. int ssl_version;
  28. int sni_enable;
  29. int ssl_negotiate_timeout;
  30. int ignore_invalid_certsign;
  31. int ignore_multi_certchain_verify;
  32. uint32_t ignore_certitem;
  33. char *cacert_buffer;
  34. bool client_cert_type;
  35. };
  36. struct mqtt_connect_client_info_t {
  37. const char *client_id;
  38. /** User name, set to NULL if not used */
  39. const char* client_user;
  40. /** Password, set to NULL if not used */
  41. const char* client_pass;
  42. /** keep alive time in seconds, 0 to disable keep alive functionality*/
  43. unsigned short keep_alive;
  44. /** timeout of the packet delivery. Range: 1–60. Unit: s.*/
  45. unsigned char pkt_timeout;
  46. /** retry times when packet delivery times out. Range: 0–10.*/
  47. unsigned char retry_times;
  48. /** will topic, set to NULL if will is not to be used,
  49. will_msg, will_qos and will retain are then ignored */
  50. const char* will_topic;
  51. /** will_msg, see will_topic */
  52. const char* will_msg;
  53. /** will_qos, see will_topic */
  54. unsigned char will_qos;
  55. /** will_retain, see will_topic */
  56. unsigned char will_retain;
  57. unsigned char clean_session;
  58. struct mqtt_ssl_config_t *ssl_cfg;
  59. };
  60. typedef enum
  61. {
  62. /** Accepted */
  63. MQTT_CONNECT_ACCEPTED = 0,
  64. /** Refused protocol version */
  65. MQTT_CONNECT_REFUSED_PROTOCOL_VERSION = 1,
  66. /** Refused identifier */
  67. MQTT_CONNECT_REFUSED_IDENTIFIER = 2,
  68. /** Refused server */
  69. MQTT_CONNECT_REFUSED_SERVER = 3,
  70. /** Refused user credentials */
  71. MQTT_CONNECT_REFUSED_USERNAME_PASS = 4,
  72. /** Refused not authorized */
  73. MQTT_CONNECT_REFUSED_NOT_AUTHORIZED_ = 5,
  74. MQTT_CONNECT_TCP_CONNECTED_FAILURE = 254,
  75. MQTT_CONNECT_OUT_OF_MEMORY = 255,
  76. /** Disconnected */
  77. MQTT_CONNECT_DISCONNECTED = 256,
  78. /** Timeout */
  79. MQTT_CONNECT_TIMEOUT = 257
  80. } mqtt_connection_status_e;
  81. typedef enum{
  82. MQTTCLIENT_SUCCESS = 0,
  83. MQTTCLIENT_INVALID_PARAM = -1,
  84. MQTTCLIENT_WOUNDBLOCK = -2,
  85. MQTTCLIENT_OUT_OF_MEM = -3,
  86. MQTTCLIENT_ALLOC_FAIL = -4,
  87. MQTTCLIENT_TCP_CONNECT_FAIL = -5,
  88. MQTTCLIENT_NOT_CONNECT = -6,
  89. MQTTCLIENT_SEND_PKT_FAIL = -7,
  90. MQTTCLIENT_BAD_REQUEST = -8,
  91. MQTTCLIENT_TIMEOUT = -9,
  92. }mqtt_error_code_e;
  93. typedef enum{
  94. MQTT_CLIENT_OPT_REC_BUF_MAX = 1,
  95. MQTT_CLIENT_OPT_VERSION = 2,
  96. }mqtt_option_e;
  97. typedef void (*mqtt_connection_cb_t)(mqtt_client_t *client, void *arg, mqtt_connection_status_e status);
  98. typedef void (*mqtt_request_cb_t)(mqtt_client_t *client, void *arg,int err);
  99. typedef void (*mqtt_incoming_publish_cb_t)(mqtt_client_t *client, void *arg, int pkt_id, const char *topic, const unsigned char *payload, unsigned short payload_len);
  100. typedef void (*mqtt_disconnect_cb_t)(mqtt_client_t *client, void *arg,int err);
  101. typedef void(*mqtt_state_exception_cb_t)(mqtt_client_t *client);
  102. int ql_mqtt_client_init(mqtt_client_t *client, int cid);
  103. /*
  104. host:
  105. mqtt: mqtt://220.180.239.212:8306
  106. mqtts: mqtts://220.180.239.212:8307
  107. */
  108. /*****************************************************************
  109. * Function: ql_mqtt_connect
  110. *
  111. * Description: 完成mqtt client上下文的配置,确定mqtt 连接属性,建立mqtt connect连接
  112. *
  113. * Parameters:
  114. * client [in] mqtt client上下文指针.
  115. * host [in] mqtt 通信对端IP地址或域名地址.
  116. * cb [in] 关联此mqtt client上下文与此上下文的事件回调函数
  117. * arg [in] mqtt 连接控制,自定义传参
  118. * client_info [in] mqtt client上下文连接配置info信息
  119. * exp_cb [in] 关联此mqtt client上下文与此上下文的异常事件处理回调函数
  120. *
  121. * Return:
  122. * 0 成功
  123. * other 错误码
  124. *
  125. *****************************************************************/
  126. int ql_mqtt_connect(mqtt_client_t *client, const char *host, mqtt_connection_cb_t cb, void *arg, const struct mqtt_connect_client_info_t *client_info, mqtt_state_exception_cb_t exp_cb);
  127. /*****************************************************************
  128. * Function: ql_mqtt_publish
  129. *
  130. * Description:mqtt发布消息函数,完成向mqtt服务器的消息推送
  131. *
  132. * Parameters:
  133. * client [in] mqtt client上下文指针.
  134. * topic [in] 所要发布消息的主题名称.
  135. * payload [in] 有效载荷,包含将被发布的应用消息
  136. * payload_length [in] 有效载荷的长度
  137. * qos [in] QoS质量等级
  138. * retain [in] 服务器保留这次推送的信息,有新的订阅者,把这消息推送给它
  139. * cb [in] 请求消息响应的回调处理函数
  140. * arg [in] mqtt 发布控制,自定义传参
  141. *
  142. * Return:
  143. * 0 成功
  144. * other 错误码
  145. *
  146. *****************************************************************/
  147. int ql_mqtt_publish(mqtt_client_t *client, const char *topic, const void *payload, unsigned short payload_length, unsigned char qos, unsigned char retain, mqtt_request_cb_t cb, void *arg);
  148. /*****************************************************************
  149. * Function: ql_mqtt_sub_unsub
  150. *
  151. * Description: mqtt客户端 订阅/取消订阅所选定的topic
  152. *
  153. * Parameters:
  154. * client [in] mqtt client上下文指针.
  155. * topic [in] 订阅或取消订阅的主题名.
  156. * qos [in] QoS质量等级
  157. * cb [in] 请求消息响应的回调处理函数
  158. * arg [in] mqtt 订阅/取消订阅主题,自定义传参
  159. * sub [in] sub为0取消订阅topic,sub非0订阅topic
  160. *
  161. * Return:
  162. * 0 成功
  163. * other 错误码
  164. *
  165. *****************************************************************/
  166. int ql_mqtt_sub_unsub(mqtt_client_t *client, const char *topic, unsigned char qos, mqtt_request_cb_t cb, void *arg,unsigned char sub);
  167. /*****************************************************************
  168. * Function: ql_mqtt_disconnect
  169. *
  170. * Description: mqtt 关闭连接函数,完成mqtt 连接关闭,更改mqtt client上下文相关状态信息,关联关闭事件回调处理函数
  171. *
  172. * Parameters:
  173. * client [in] mqtt client上下文指针.
  174. * cb [in] mqtt client关闭连接事件回调处理函数.
  175. * arg [in] mqtt 关闭连接,自定义传参
  176. *
  177. * Return:
  178. * 0 成功
  179. * other 错误码
  180. *
  181. *****************************************************************/
  182. int ql_mqtt_disconnect(mqtt_client_t *client, mqtt_disconnect_cb_t cb, void *arg);
  183. /*****************************************************************
  184. * Function: ql_mqtt_set_inpub_callback
  185. *
  186. * Description: 关联mqtt client上下文与inpub_cb 主题,载荷信息显示回调函数
  187. *
  188. * Parameters:
  189. * client [in] mqtt client上下文指针.
  190. * inpub_cb [in] 订阅/取消订阅的topic,以及有效载荷,信息显示回调函数.
  191. * arg [in] 自定义传参
  192. *
  193. * Return:
  194. * 0 成功
  195. * other 错误码
  196. *
  197. *****************************************************************/
  198. int ql_mqtt_set_inpub_callback(mqtt_client_t *client, mqtt_incoming_publish_cb_t inpub_cb, void *arg);
  199. /*****************************************************************
  200. * Function: ql_mqtt_client_is_connected
  201. *
  202. * Description: mqtt client上下文合法性检查,以及判断mqtt的连接状态是否成功成功连接对端
  203. *
  204. * Parameters:
  205. * client [in] mqtt client上下文指针.
  206. *
  207. * Return:
  208. * 1 成功
  209. * 0 失败
  210. *
  211. *****************************************************************/
  212. int ql_mqtt_client_is_connected(mqtt_client_t *client);
  213. /*****************************************************************
  214. * Function: ql_mqtt_client_deinit
  215. *
  216. * Description: mqtt client反初始化函数,将此上下文所有信息全部置NULL
  217. *
  218. * Parameters:
  219. * client [in] mqtt client上下文指针.
  220. *
  221. * Return:
  222. * 0 成功
  223. * other 错误码
  224. *
  225. *****************************************************************/
  226. int ql_mqtt_client_deinit(mqtt_client_t *client);
  227. /*****************************************************************
  228. * Function: ql_mqtt_onenet_generate_auth_token
  229. *
  230. * Description: onenet平台token算法,用来生成onenet平台需要的密码。
  231. *
  232. * Parameters:
  233. * expire_time [in] onenet平台的token过期时间(单位为s).
  234. * product_id [in] onenet 平台的产品ID.
  235. * device_name [in] onenet 平台的设备名称.
  236. * version [in] onenet 平台的版本.
  237. * access_key [in] onenet 平台的设备key.
  238. * Return:
  239. * NULL 失败。
  240. * other 成功,使用完后需要free释放空间。
  241. *
  242. *****************************************************************/
  243. char *ql_mqtt_onenet_generate_auth_token(signed long long expire_time,char *product_id,char *device_name,char *version,char *access_key);
  244. /*****************************************************************
  245. * Function: ql_mqtt_client_setopt
  246. *
  247. * Description: 通过opt_tag类型信息,配置mqtt client上下文
  248. *
  249. * Parameters:
  250. * client [in] mqtt client上下文指针.
  251. * opt_tag [in] mqtt client上下文中配置的参数处理类型.
  252. *
  253. * Return:
  254. * 0 成功
  255. * other 错误码
  256. *
  257. *****************************************************************/
  258. int ql_mqtt_client_setopt(mqtt_client_t *client, int opt_tag,...);
  259. #endif