RyanMqttPublic.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. #ifndef __mqttClientPublic__
  2. #define __mqttClientPublic__
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. // 允许的mqtt packetId最大值,协议标准为1-65534的非零16位数
  10. #define RyanMqttMaxPacketId (UINT16_MAX - 1U)
  11. // 允许的mqtt可变报头和有效载荷最长长度。默认值为协议标准
  12. #define RyanMqttMaxPayloadLen (268435455UL)
  13. #define RyanMqttMsgInvalidPacketId (UINT16_MAX)
  14. /* MQTT packet types. */
  15. /**
  16. * @addtogroup mqtt_constants
  17. * @{
  18. */
  19. #define MQTT_PACKET_TYPE_CONNECT ((uint8_t)0x10U) /**< @brief CONNECT (client-to-server). */
  20. #define MQTT_PACKET_TYPE_CONNACK ((uint8_t)0x20U) /**< @brief CONNACK (server-to-client). */
  21. #define MQTT_PACKET_TYPE_PUBLISH ((uint8_t)0x30U) /**< @brief PUBLISH (bidirectional). */
  22. #define MQTT_PACKET_TYPE_PUBACK ((uint8_t)0x40U) /**< @brief PUBACK (bidirectional). */
  23. #define MQTT_PACKET_TYPE_PUBREC ((uint8_t)0x50U) /**< @brief PUBREC (bidirectional). */
  24. #define MQTT_PACKET_TYPE_PUBREL ((uint8_t)0x62U) /**< @brief PUBREL (bidirectional). */
  25. #define MQTT_PACKET_TYPE_PUBCOMP ((uint8_t)0x70U) /**< @brief PUBCOMP (bidirectional). */
  26. #define MQTT_PACKET_TYPE_SUBSCRIBE ((uint8_t)0x82U) /**< @brief SUBSCRIBE (client-to-server). */
  27. #define MQTT_PACKET_TYPE_SUBACK ((uint8_t)0x90U) /**< @brief SUBACK (server-to-client). */
  28. #define MQTT_PACKET_TYPE_UNSUBSCRIBE ((uint8_t)0xA2U) /**< @brief UNSUBSCRIBE (client-to-server). */
  29. #define MQTT_PACKET_TYPE_UNSUBACK ((uint8_t)0xB0U) /**< @brief UNSUBACK (server-to-client). */
  30. #define MQTT_PACKET_TYPE_PINGREQ ((uint8_t)0xC0U) /**< @brief PINGREQ (client-to-server). */
  31. #define MQTT_PACKET_TYPE_PINGRESP ((uint8_t)0xD0U) /**< @brief PINGRESP (server-to-client). */
  32. #define MQTT_PACKET_TYPE_DISCONNECT ((uint8_t)0xE0U) /**< @brief DISCONNECT (client-to-server). */
  33. /** @} */
  34. // 定义枚举类型
  35. typedef enum
  36. {
  37. // RyanMqttBit31 = 0x80000000,
  38. RyanMqttBit30 = 0x40000000,
  39. RyanMqttBit29 = 0x20000000,
  40. RyanMqttBit28 = 0x10000000,
  41. RyanMqttBit27 = 0x08000000,
  42. RyanMqttBit26 = 0x04000000,
  43. RyanMqttBit25 = 0x02000000,
  44. RyanMqttBit24 = 0x01000000,
  45. RyanMqttBit23 = 0x00800000,
  46. RyanMqttBit22 = 0x00400000,
  47. RyanMqttBit21 = 0x00200000,
  48. RyanMqttBit20 = 0x00100000,
  49. RyanMqttBit19 = 0x00080000,
  50. RyanMqttBit18 = 0x00040000,
  51. RyanMqttBit17 = 0x00020000,
  52. RyanMqttBit16 = 0x00010000,
  53. RyanMqttBit15 = 0x00008000,
  54. RyanMqttBit14 = 0x00004000,
  55. RyanMqttBit13 = 0x00002000,
  56. RyanMqttBit12 = 0x00001000,
  57. RyanMqttBit11 = 0x00000800,
  58. RyanMqttBit10 = 0x00000400,
  59. RyanMqttBit9 = 0x00000200,
  60. RyanMqttBit8 = 0x00000100,
  61. RyanMqttBit7 = 0x00000080,
  62. RyanMqttBit6 = 0x00000040,
  63. RyanMqttBit5 = 0x00000020,
  64. RyanMqttBit4 = 0x00000010,
  65. RyanMqttBit3 = 0x00000008,
  66. RyanMqttBit2 = 0x00000004,
  67. RyanMqttBit1 = 0x00000002,
  68. RyanMqttBit0 = 0x00000001,
  69. } RyanMqttBit_e;
  70. // 仅作兼容使用
  71. #define RyanMqttFalse (false)
  72. #define RyanMqttTrue (true)
  73. typedef bool RyanMqttBool_e;
  74. typedef enum
  75. {
  76. RyanMqttQos0 = 0x00,
  77. RyanMqttQos1 = 0x01,
  78. RyanMqttQos2 = 0x02,
  79. RyanMqttSubFail = 0x80
  80. } RyanMqttQos_e;
  81. typedef enum
  82. {
  83. RyanMqttInvalidState = -1, // 无效状态
  84. RyanMqttInitState = 0, // 初始化状态
  85. RyanMqttStartState, // 开始状态
  86. RyanMqttConnectState, // 连接状态
  87. RyanMqttDisconnectState, // 断开连接状态
  88. RyanMqttReconnectState, // 重新连接状态
  89. } RyanMqttState_e;
  90. typedef enum
  91. {
  92. /**
  93. * @brief 保留事件
  94. * @eventData NULL
  95. */
  96. RyanMqttEventError = RyanMqttBit0,
  97. /**
  98. * @brief 连接成功
  99. * @eventData 正数为RyanMqttConnectStatus_e*, 负数为RyanMqttError_e*
  100. */
  101. RyanMqttEventConnected = RyanMqttBit1,
  102. /**
  103. * @brief 可能由用户触发,断开连接
  104. * @eventData 正数为RyanMqttConnectStatus_e*, 负数为RyanMqttError_e*
  105. */
  106. RyanMqttEventDisconnected = RyanMqttBit2,
  107. /**
  108. * @brief 订阅成功事件,服务端可以授予比订阅者要求的低的QoS等级
  109. * @eventData RyanMqttMsgHandler_t*
  110. */
  111. RyanMqttEventSubscribed = RyanMqttBit3,
  112. /**
  113. * @brief 订阅失败事件,超时 / 服务器返回订阅失败
  114. * @eventData RyanMqttMsgHandler_t*
  115. */
  116. RyanMqttEventSubscribedFailed = RyanMqttBit4,
  117. // !弃用: 请使用 RyanMqttEventSubscribedFailed
  118. RyanMqttEventSubscribedFaile = RyanMqttEventSubscribedFailed,
  119. /**
  120. * @brief 取消订阅事件
  121. * @eventData RyanMqttMsgHandler_t*
  122. */
  123. RyanMqttEventUnSubscribed = RyanMqttBit5,
  124. /**
  125. * @brief 取消订阅失败事件,超时
  126. * @eventData RyanMqttMsgHandler_t*
  127. */
  128. RyanMqttEventUnSubscribedFailed = RyanMqttBit6,
  129. // !弃用: 请使用 RyanMqttEventUnSubscribedFailed
  130. RyanMqttEventUnSubscribedFaile = RyanMqttEventUnSubscribedFailed,
  131. /**
  132. * @brief qos1 / qos2发送成功事件。发送没有失败,只会重发或者用户手动丢弃
  133. * @eventData RyanMqttAckHandler_t*
  134. */
  135. RyanMqttEventPublished = RyanMqttBit7,
  136. /**
  137. * @brief qos1 / qos2数据(或者ack)重发回调函数
  138. * @eventData RyanMqttAckHandler_t*
  139. */
  140. RyanMqttEventRepeatPublishPacket = RyanMqttBit8,
  141. /**
  142. * @brief ack重发次数超过警戒值
  143. * @eventData RyanMqttAckHandler_t*
  144. */
  145. RyanMqttEventAckRepeatCountWarning = RyanMqttBit9,
  146. /**
  147. * @brief ack记数值超过警戒值
  148. * @eventData uint16_t* ackHandlerCount; 等待ack的记录个数
  149. */
  150. RyanMqttEventAckCountWarning = RyanMqttBit10,
  151. /**
  152. * @brief 用户触发,ack句柄丢弃事件,由用户手动调用RyanMqttDestroyAckHandler函数触发
  153. * 可能是发送qos1 / qos2消息丢弃、ack丢弃,也可能是publish报文的ack丢弃
  154. *
  155. * @eventData RyanMqttAckHandler_t*
  156. */
  157. RyanMqttEventAckHandlerDiscard = RyanMqttBit11,
  158. // !弃用: 请使用 RyanMqttEventAckHandlerDiscard
  159. RyanMqttEventAckHandlerdiscard = RyanMqttEventAckHandlerDiscard,
  160. /**
  161. * @brief 重连前事件,用户可以在此时更改connect信息
  162. * @eventData NULL
  163. */
  164. RyanMqttEventReconnectBefore = RyanMqttBit12,
  165. /**
  166. * @brief 用户触发,销毁客户端前回调
  167. * @eventData NULL
  168. */
  169. RyanMqttEventDestroyBefore = RyanMqttBit13,
  170. // !弃用: 请使用 RyanMqttEventDestroyBefore
  171. RyanMqttEventDestoryBefore = RyanMqttEventDestroyBefore,
  172. /**
  173. * @brief 接收到订阅主题数据事件,支持通配符识别,返回的主题信息是报文主题
  174. * @eventData RyanMqttMsgData_t*
  175. */
  176. RyanMqttEventData = RyanMqttBit14,
  177. /**
  178. * @brief 接收到未匹配任何订阅主题的报文事件
  179. *
  180. * 此事件触发时,报文的主题不在当前客户端订阅的 topic 列表中,
  181. *
  182. * @eventData RyanMqttMsgData_t*
  183. */
  184. RyanMqttEventUnsubscribedData = RyanMqttBit15,
  185. RyanMqttEventAnyId = UINT32_MAX,
  186. } RyanMqttEventId_e;
  187. // 定义枚举类型
  188. typedef enum
  189. {
  190. RyanMqttParamInvalidError = -0x100, // 参数无效
  191. RyanMqttRecvPacketTimeOutError, // 读取数据超时
  192. RyanMqttSendPacketTimeOutError, // 发送数据超时
  193. RyanSocketFailedError, // 套接字 FD 失败
  194. RyanMqttSocketConnectFailError, // MQTT socket连接失败
  195. RyanMqttSendPacketError, // MQTT 发送数据包错误
  196. RyanMqttSerializePacketError, // 序列化报文失败
  197. RyanMqttDeserializePacketError, // 解析报文失败
  198. RyanMqttNoRescourceError, // 没有资源
  199. RyanMqttHaveRescourceError, // 资源已存在
  200. RyanMqttNotConnectError, // MQTT 没有连接
  201. RyanMqttConnectError, // MQTT 已连接
  202. RyanMqttRecvBufToShortError, // MQTT 缓冲区太短
  203. RyanMqttSendBufToShortError, // MQTT 缓冲区太短
  204. RyanMqttNotEnoughMemError, // MQTT 内存不足
  205. RyanMqttFailedError, // 失败
  206. RyanMqttInvalidPacketError, // 收到非法的报文
  207. RyanMqttSuccessError = 0x0000, // 成功
  208. // RyanMqttErrorForceInt32 = INT32_MAX // 强制编译器使用int32_t类型
  209. } RyanMqttError_e;
  210. typedef enum
  211. {
  212. // mqtt标准定义
  213. RyanMqttConnectAccepted = 0, // 连接已被服务端接受
  214. RyanMqttConnectRefusedProtocolVersion = 1, // 服务端不支持客户端请求的 MQTT 协议级别
  215. RyanMqttConnectRefusedIdentifier = 2, // 不合格的客户端标识符
  216. RyanMqttConnectRefusedServer = 3, // 服务端不可用
  217. RyanMqttConnectRefusedUsernamePass = 4, // 无效的用户名或密码
  218. RyanMqttConnectRefusedNotAuthorized = 5, // 连接已拒绝,未授权
  219. // mqtt非标准定义
  220. RyanMqttConnectClientInvalid = 200, // 客户端处于无效状态
  221. RyanMqttConnectNetWorkFail, // 网络错误
  222. RyanMqttConnectDisconnected, // mqtt客户端断开连接
  223. RyanMqttKeepaliveTimeout, // 心跳超时断开连接
  224. RyanMqttConnectUserDisconnected, // 用户手动断开连接
  225. RyanMqttConnectTimeout, // 超时断开
  226. RyanMqttConnectFirstPackNotConnack, // 发送connect后接受到的第一个报文不是connack
  227. RyanMqttConnectProtocolError, // 多次收到connack
  228. RyanMqttConnectInvalidPacketError, // 收到不符合MQTT3.1.1协议的报文,并且要求关闭客户端的
  229. RyanMqttConnectFailedError, // 杂项原因,比如内存分配失败,序列化失败等
  230. // RyanMqttConnectStatusForceInt32 = INT32_MAX // 强制编译器使用int32_t类型
  231. } RyanMqttConnectStatus_e;
  232. extern const char *RyanMqttStrError(int32_t state);
  233. #define RyanMqttCheckCodeNoReturn(EX, ErrorCode, Ryanlevel, code) \
  234. if (!(EX)) \
  235. { \
  236. Ryanlevel("ErrorCode: %d, strError: %s", ErrorCode, RyanMqttStrError(ErrorCode)); \
  237. {code}; \
  238. }
  239. #define RyanMqttCheckCode(EX, ErrorCode, level, code) \
  240. RyanMqttCheckCodeNoReturn(EX, ErrorCode, level, { \
  241. {code}; \
  242. return ErrorCode; \
  243. });
  244. #define RyanMqttCheckNoReturn(EX, ErrorCode, level) RyanMqttCheckCodeNoReturn(EX, ErrorCode, level, {})
  245. #define RyanMqttCheck(EX, ErrorCode, level) RyanMqttCheckCode(EX, ErrorCode, level, {})
  246. #define RyanMqttCheckAssert(EX, ErrorCode, level) \
  247. RyanMqttCheckCodeNoReturn(EX, ErrorCode, level, { RyanMqttAssert(NULL && "RyanMqttCheckAssert"); })
  248. // 定义结构体类型
  249. /* extern variables-----------------------------------------------------------*/
  250. #ifdef __cplusplus
  251. }
  252. #endif
  253. #endif