1
0

mqtt_al.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*----------------------------------------------------------------------------
  2. * Copyright (c) <2018>, <Huawei Technologies Co., Ltd>
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without modification,
  5. * are permitted provided that the following conditions are met:
  6. * 1. Redistributions of source code must retain the above copyright notice, this list of
  7. * conditions and the following disclaimer.
  8. * 2. Redistributions in binary form must reproduce the above copyright notice, this list
  9. * of conditions and the following disclaimer in the documentation and/or other materials
  10. * provided with the distribution.
  11. * 3. Neither the name of the copyright holder nor the names of its contributors may be used
  12. * to endorse or promote products derived from this software without specific prior written
  13. * permission.
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  15. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  16. * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  17. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
  18. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  19. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  20. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  21. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  22. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  23. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  24. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. *---------------------------------------------------------------------------*/
  26. /*----------------------------------------------------------------------------
  27. * Notice of Export Control Law
  28. * ===============================================
  29. * Huawei LiteOS may be subject to applicable export control laws and regulations, which might
  30. * include those applicable to Huawei LiteOS of U.S. and the country in which you are located.
  31. * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance with such
  32. * applicable export control laws and regulations.
  33. *---------------------------------------------------------------------------*/
  34. #ifndef MQTT_AL_H
  35. #define MQTT_AL_H
  36. #include <stdint.h>
  37. #include <dtls_al.h>
  38. #if defined(__cplusplus)
  39. extern "C" {
  40. #endif
  41. /** brief defines for all the ascii or data used in the mqtt engine */
  42. typedef struct
  43. {
  44. char *data; ///< buffer to storage the data
  45. int len; ///< buffer data length
  46. }mqtt_al_string_t; //used to represent any type string (maybe not ascii)
  47. ///** @brief this enum all the transport encode we support now*/
  48. //typedef enum
  49. //{
  50. // en_mqtt_al_security_none = 0, ///< no encode
  51. // en_mqtt_al_security_psk, ///< use the psk mode in transport layer
  52. // en_mqtt_al_security_cas, ///< use the ca mode in transport layer,only check the server
  53. // en_mqtt_al_security_cacs, ///< use the ca mode in transport layer,both check the server and client
  54. // en_mqtt_al_security_end, ///< the end for the mqtt
  55. //}en_mqtt_al_security_t;
  56. //
  57. ///** @brief this data defines for the psk mode*/
  58. //typedef struct
  59. //{
  60. // mqtt_al_string_t psk_id; ///< the psk id
  61. // mqtt_al_string_t psk_key; ///< the psk key
  62. //}mqtt_al_security_psk_t;
  63. //
  64. ///** @brief this data defines for the cas mode:only check the server */
  65. //typedef struct
  66. //{
  67. // mqtt_al_string_t ca_crt; ///< the ca crt
  68. //}mqtt_al_security_cas_t;
  69. //
  70. //
  71. ///** @brief this data defines for the cacs mode:both check the server and client */
  72. //typedef struct
  73. //{
  74. // mqtt_al_string_t ca_s_crt; ///< the server crt
  75. // mqtt_al_string_t ca_c_crt; ///< the client crt
  76. // mqtt_al_string_t ca_c_key; ///< the client ca key
  77. // mqtt_al_string_t ca_host; ///< for peer certificate, maybe they need this
  78. //}mqtt_al_security_cacs_t;
  79. //
  80. ///** @brief this data defines for the encode parameter for the connect */
  81. //typedef struct
  82. //{
  83. // en_mqtt_al_security_t type; ///< which security type of the data
  84. // union
  85. // {
  86. // mqtt_al_security_psk_t psk; ///< psk data if the type is en_mqtt_security_psk
  87. // mqtt_al_security_cas_t cas; ///< cas data if the type is en_mqtt_security_cas
  88. // mqtt_al_security_cacs_t cacs; ///< cacs data if the type is en_mqtt_security_cacs
  89. // }u;
  90. //}mqtt_al_security_para_t;
  91. /** @brief enum all the qos supported for the application */
  92. typedef enum
  93. {
  94. en_mqtt_al_qos_0 = 0, ///< mqtt QOS 0
  95. en_mqtt_al_qos_1, ///< mqtt QOS 1
  96. en_mqtt_al_qos_2, ///< mqtt QOS 2
  97. en_mqtt_al_qos_err
  98. }en_mqtt_al_qos_t;
  99. /** @brief defines the parameter used for the will message used in connect */
  100. typedef struct
  101. {
  102. mqtt_al_string_t topic; ///< will topic
  103. mqtt_al_string_t msg; ///< will message
  104. en_mqtt_al_qos_t qos; ///< will message qos
  105. int retain; ///< will message retain or not:1 retain while 0 not
  106. }mqtt_al_willmsg_t;
  107. /** @brief enum the mqtt version*/
  108. typedef enum
  109. {
  110. en_mqtt_al_version_3_1_0 = 0,
  111. en_mqtt_al_version_3_1_1,
  112. }en_mqtt_al_verison;
  113. /** @brief defines the paramter for the mqtt connect */
  114. typedef struct
  115. {
  116. mqtt_al_string_t serveraddr; ///< mqtt server:support domain name and dot format
  117. int serverport; ///< mqtt server port
  118. dtls_al_security_t *security; ///< if NULL,will use en_mqtt_security_none
  119. en_mqtt_al_verison version; ///< mqtt version will be used
  120. mqtt_al_string_t clientid; ///< mqtt connect client identifier
  121. mqtt_al_string_t user; ///< mqtt connect user
  122. mqtt_al_string_t passwd; ///< mqtt connect pwd
  123. int cleansession; ///< 1 clean the session while 0 not
  124. mqtt_al_willmsg_t *willmsg; ///< mqtt connect will message
  125. unsigned short keepalivetime;///< keep alive time
  126. char conret; ///< mqtt connect code, return by server
  127. int timeout; ///< how much time will be blocked
  128. }mqtt_al_conpara_t;
  129. /** @brief defines for the mqtt publish */
  130. typedef struct
  131. {
  132. mqtt_al_string_t topic; ///< selected publish topic
  133. mqtt_al_string_t msg; ///< message to be published
  134. en_mqtt_al_qos_t qos; ///< message qos
  135. int retain; ///< message retain :1 retain while 0 not
  136. int timeout; ///< how much time will blocked
  137. }mqtt_al_pubpara_t;
  138. /** @brief defines for the message will be passed to the application dealer*/
  139. typedef struct
  140. {
  141. mqtt_al_string_t topic; ///< message topic
  142. mqtt_al_string_t msg; ///< the topic message payload
  143. en_mqtt_al_qos_t qos; ///< message qos
  144. int dup; ///< message dup times
  145. int retain;///< retain or not
  146. }mqtt_al_msgrcv_t;
  147. /** @brief defines the mqtt received message dealer, called by mqtt engine*/
  148. typedef void (*fn_mqtt_al_msg_dealer)(void *arg,mqtt_al_msgrcv_t *msg);
  149. /** @brief defines the mqtt subscribe parameter*/
  150. typedef struct
  151. {
  152. mqtt_al_string_t topic; ///< topic will be subscribe
  153. en_mqtt_al_qos_t qos; ///< qos requested
  154. fn_mqtt_al_msg_dealer dealer; ///< message dealer:used to deal the received message
  155. void *arg; ///< used for the message dealer
  156. char subret; ///< subscribe result code
  157. int timeout; ///< how much time will be blocked
  158. }mqtt_al_subpara_t;
  159. /** @brief defines the mqtt unsubscribe parameter*/
  160. typedef struct
  161. {
  162. mqtt_al_string_t topic; ///< the unsubscribe topic
  163. uint32_t timeout; ///< how much time will be blocked
  164. }mqtt_al_unsubpara_t;
  165. /** @brief descript the mqtt engine status */
  166. typedef enum
  167. {
  168. en_mqtt_al_connect_ok = 0, ///< which means the mqtt is ok and could supply the service
  169. en_mqtt_al_connect_err, ///< the mqtt no start or connect err
  170. }en_mqtt_al_connect_state;
  171. /** @brief defines for the mqtt connect code returned by the server */
  172. #define cn_mqtt_al_con_code_ok 0 ///< has been accepted by the server
  173. #define cn_mqtt_al_con_code_err_version 1 ///< server not support the version
  174. #define cn_mqtt_al_con_code_err_clientID 2 ///< client identifier is error
  175. #define cn_mqtt_al_con_code_err_netrefuse 3 ///< server service not ready yet
  176. #define cn_mqtt_al_con_code_err_u_p 4 ///< bad user name or pwd
  177. #define cn_mqtt_al_con_code_err_auth 5 ///< the client is not authorized
  178. #define cn_mqtt_al_con_code_err_unkown -1 ///< unknown reason
  179. #define cn_mqtt_al_con_code_err_network 0x80 ///< network reason,you could try once more
  180. /** @brief defines for the mqtt subscribe code returned by the server */
  181. #define cn_mqtt_al_sub_code_qos0 0 ///< success and max qos is QOS 0
  182. #define cn_mqtt_al_sub_code_qos1 1 ///< success and max qos is QOS 1
  183. #define cn_mqtt_al_sub_code_qos2 2 ///< success and max qos is QOS 2
  184. #define cn_mqtt_al_sub_code_err 0x80 ///< subscribe failure
  185. /**
  186. *@brief defines the operation will be registered to the mqtt service
  187. *@brief parameter and return code refere to the corresponding api
  188. *
  189. */
  190. typedef struct
  191. {
  192. // ///< the mqtt init function
  193. // int (* init) (void);
  194. // ///< the mqtt deinit function
  195. // int (* deinit) (void ); ---TODO, have a thought if we really need it
  196. ///< connect to the server
  197. void* (* connect) (mqtt_al_conpara_t *param);
  198. ///< disconnect from the server
  199. int (* disconnect)(void *handle );
  200. ///< publish a message to the server
  201. int (* publish) (void *handle, mqtt_al_pubpara_t *msg);
  202. ///< subscribe a topic to the server
  203. int (* subscribe) (void *handle, mqtt_al_subpara_t *subpara);
  204. ///< unsubscribe a topic to the server
  205. int (* unsubscribe) (void *handle, mqtt_al_unsubpara_t *unsubpara);
  206. ///< check the mqtt engine status
  207. en_mqtt_al_connect_state (* check_status) (void *handle);
  208. }mqtt_al_op_t;
  209. //////////////////////API USED FOR THE MQTT APPLICAITON/////////////////////////
  210. /**
  211. * @brief maybe the mqtt lib need do some initialize
  212. *
  213. * @return 0 success while -1 failed
  214. *
  215. */
  216. int mqtt_al_init(void);
  217. /**
  218. * @brief :when you don't want to use the mqtt service ,please call this function
  219. *
  220. * @return 0 success while -1 failed
  221. */
  222. int mqtt_al_deinit(void);
  223. /**
  224. *@brief: you could use this function to connect to the mqtt server
  225. *
  226. *@param[in] conparam the parameter we will use in connect, refer to the data mqtt_al_conpara_t
  227. *@
  228. *@return: first you should check the return value then the return code in conparam
  229. *
  230. *@retval NULL which means you could not get the connect to the server,maybe network reason
  231. *@retval handle, which means you get the context, please check the conparam for more
  232. */
  233. void * mqtt_al_connect( mqtt_al_conpara_t *conparam);
  234. /**
  235. * @brief : you could use this function to disconnect from the mqtt server
  236. *
  237. * @param[in] handle the handle we get from mqtt_al_connect
  238. * @return 0 success -1 failed
  239. */
  240. int mqtt_al_disconnect(void *handle);
  241. /**
  242. * @brief you could use this function to publish a message to the server
  243. *
  244. * @param[in] handle the handle we get from mqtt_al_connect
  245. *
  246. * @param[in] msg the message we will publish, see the data mqtt_al_pubpara_t
  247. *
  248. * @return 0 success -1 failed
  249. *
  250. */
  251. int mqtt_al_publish(void *handle, mqtt_al_pubpara_t *pubpara);
  252. /**
  253. * @brief you could use this function subscribe a topic from the server
  254. *
  255. * @param[in] handle the handle we get from mqtt_al_connect
  256. *
  257. * @param[in] subpara refer to the data mqtt_al_subpara_t
  258. *
  259. * @return 0 success -1 failed
  260. *
  261. */
  262. int mqtt_al_subscribe(void *handle, mqtt_al_subpara_t *subpara);
  263. /**
  264. * @brief you could use this function unsubscribe a topic from the server
  265. *
  266. * @param[in] handle the handle we get from mqtt_al_connect
  267. *
  268. * @param[in] unsubpara refer to the data mqtt_al_unsubpara_t
  269. *
  270. * @return 0 success -1 failed
  271. *
  272. */
  273. int mqtt_al_unsubscribe(void *handle, mqtt_al_unsubpara_t *unsubpara);
  274. /**
  275. * @brief the mqtt engine state now
  276. *
  277. * @param[in] handle the handle we get from mqtt_al_connect
  278. *
  279. * @return refer to en_mqtt_al_connect_state
  280. */
  281. en_mqtt_al_connect_state mqtt_al_check_status(void *handle);
  282. //////////////////////API USED FOR THE MQTT IMPLEMENT/////////////////////////
  283. /**
  284. *@brief the mqtt lib should call this function to register its implement as a service
  285. *
  286. *@param[in] op refer to the data mqtt_al_op_t
  287. *
  288. *@return 0 success while -1 failed
  289. *
  290. */
  291. int mqtt_al_install(mqtt_al_op_t *op);
  292. /**
  293. *@brief call this function to unregister the mqtt service
  294. *
  295. *@param[in] op refer to the data mqtt_al_op_t
  296. *
  297. *@return 0 success while -1 failed
  298. *
  299. */
  300. int mqtt_al_uninstall(void);
  301. #if defined(__cplusplus)
  302. }
  303. #endif
  304. #endif /* MQTT_AL_H */