| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290 |
- #ifndef __mqttClientPublic__
- #define __mqttClientPublic__
- #ifdef __cplusplus
- extern "C" {
- #endif
- #include <stdio.h>
- #include <stdint.h>
- #include <stdbool.h>
- // 允许的mqtt packetId最大值,协议标准为1-65534的非零16位数
- #define RyanMqttMaxPacketId (UINT16_MAX - 1U)
- // 允许的mqtt可变报头和有效载荷最长长度。默认值为协议标准
- #define RyanMqttMaxPayloadLen (268435455UL)
- #define RyanMqttMsgInvalidPacketId (UINT16_MAX)
- /* MQTT packet types. */
- /**
- * @addtogroup mqtt_constants
- * @{
- */
- #define MQTT_PACKET_TYPE_CONNECT ((uint8_t)0x10U) /**< @brief CONNECT (client-to-server). */
- #define MQTT_PACKET_TYPE_CONNACK ((uint8_t)0x20U) /**< @brief CONNACK (server-to-client). */
- #define MQTT_PACKET_TYPE_PUBLISH ((uint8_t)0x30U) /**< @brief PUBLISH (bidirectional). */
- #define MQTT_PACKET_TYPE_PUBACK ((uint8_t)0x40U) /**< @brief PUBACK (bidirectional). */
- #define MQTT_PACKET_TYPE_PUBREC ((uint8_t)0x50U) /**< @brief PUBREC (bidirectional). */
- #define MQTT_PACKET_TYPE_PUBREL ((uint8_t)0x62U) /**< @brief PUBREL (bidirectional). */
- #define MQTT_PACKET_TYPE_PUBCOMP ((uint8_t)0x70U) /**< @brief PUBCOMP (bidirectional). */
- #define MQTT_PACKET_TYPE_SUBSCRIBE ((uint8_t)0x82U) /**< @brief SUBSCRIBE (client-to-server). */
- #define MQTT_PACKET_TYPE_SUBACK ((uint8_t)0x90U) /**< @brief SUBACK (server-to-client). */
- #define MQTT_PACKET_TYPE_UNSUBSCRIBE ((uint8_t)0xA2U) /**< @brief UNSUBSCRIBE (client-to-server). */
- #define MQTT_PACKET_TYPE_UNSUBACK ((uint8_t)0xB0U) /**< @brief UNSUBACK (server-to-client). */
- #define MQTT_PACKET_TYPE_PINGREQ ((uint8_t)0xC0U) /**< @brief PINGREQ (client-to-server). */
- #define MQTT_PACKET_TYPE_PINGRESP ((uint8_t)0xD0U) /**< @brief PINGRESP (server-to-client). */
- #define MQTT_PACKET_TYPE_DISCONNECT ((uint8_t)0xE0U) /**< @brief DISCONNECT (client-to-server). */
- /** @} */
- // 定义枚举类型
- typedef enum
- {
- // RyanMqttBit31 = 0x80000000,
- RyanMqttBit30 = 0x40000000,
- RyanMqttBit29 = 0x20000000,
- RyanMqttBit28 = 0x10000000,
- RyanMqttBit27 = 0x08000000,
- RyanMqttBit26 = 0x04000000,
- RyanMqttBit25 = 0x02000000,
- RyanMqttBit24 = 0x01000000,
- RyanMqttBit23 = 0x00800000,
- RyanMqttBit22 = 0x00400000,
- RyanMqttBit21 = 0x00200000,
- RyanMqttBit20 = 0x00100000,
- RyanMqttBit19 = 0x00080000,
- RyanMqttBit18 = 0x00040000,
- RyanMqttBit17 = 0x00020000,
- RyanMqttBit16 = 0x00010000,
- RyanMqttBit15 = 0x00008000,
- RyanMqttBit14 = 0x00004000,
- RyanMqttBit13 = 0x00002000,
- RyanMqttBit12 = 0x00001000,
- RyanMqttBit11 = 0x00000800,
- RyanMqttBit10 = 0x00000400,
- RyanMqttBit9 = 0x00000200,
- RyanMqttBit8 = 0x00000100,
- RyanMqttBit7 = 0x00000080,
- RyanMqttBit6 = 0x00000040,
- RyanMqttBit5 = 0x00000020,
- RyanMqttBit4 = 0x00000010,
- RyanMqttBit3 = 0x00000008,
- RyanMqttBit2 = 0x00000004,
- RyanMqttBit1 = 0x00000002,
- RyanMqttBit0 = 0x00000001,
- } RyanMqttBit_e;
- // 仅作兼容使用
- #define RyanMqttFalse (false)
- #define RyanMqttTrue (true)
- typedef bool RyanMqttBool_e;
- typedef enum
- {
- RyanMqttQos0 = 0x00,
- RyanMqttQos1 = 0x01,
- RyanMqttQos2 = 0x02,
- RyanMqttSubFail = 0x80
- } RyanMqttQos_e;
- typedef enum
- {
- RyanMqttInvalidState = -1, // 无效状态
- RyanMqttInitState = 0, // 初始化状态
- RyanMqttStartState, // 开始状态
- RyanMqttConnectState, // 连接状态
- RyanMqttDisconnectState, // 断开连接状态
- RyanMqttReconnectState, // 重新连接状态
- } RyanMqttState_e;
- typedef enum
- {
- /**
- * @brief 保留事件
- * @eventData NULL
- */
- RyanMqttEventError = RyanMqttBit0,
- /**
- * @brief 连接成功
- * @eventData 正数为RyanMqttConnectStatus_e*, 负数为RyanMqttError_e*
- */
- RyanMqttEventConnected = RyanMqttBit1,
- /**
- * @brief 可能由用户触发,断开连接
- * @eventData 正数为RyanMqttConnectStatus_e*, 负数为RyanMqttError_e*
- */
- RyanMqttEventDisconnected = RyanMqttBit2,
- /**
- * @brief 订阅成功事件,服务端可以授予比订阅者要求的低的QoS等级
- * @eventData RyanMqttMsgHandler_t*
- */
- RyanMqttEventSubscribed = RyanMqttBit3,
- /**
- * @brief 订阅失败事件,超时 / 服务器返回订阅失败
- * @eventData RyanMqttMsgHandler_t*
- */
- RyanMqttEventSubscribedFailed = RyanMqttBit4,
- // !弃用: 请使用 RyanMqttEventSubscribedFailed
- RyanMqttEventSubscribedFaile = RyanMqttEventSubscribedFailed,
- /**
- * @brief 取消订阅事件
- * @eventData RyanMqttMsgHandler_t*
- */
- RyanMqttEventUnSubscribed = RyanMqttBit5,
- /**
- * @brief 取消订阅失败事件,超时
- * @eventData RyanMqttMsgHandler_t*
- */
- RyanMqttEventUnSubscribedFailed = RyanMqttBit6,
- // !弃用: 请使用 RyanMqttEventUnSubscribedFailed
- RyanMqttEventUnSubscribedFaile = RyanMqttEventUnSubscribedFailed,
- /**
- * @brief qos1 / qos2发送成功事件。发送没有失败,只会重发或者用户手动丢弃
- * @eventData RyanMqttAckHandler_t*
- */
- RyanMqttEventPublished = RyanMqttBit7,
- /**
- * @brief qos1 / qos2数据(或者ack)重发回调函数
- * @eventData RyanMqttAckHandler_t*
- */
- RyanMqttEventRepeatPublishPacket = RyanMqttBit8,
- /**
- * @brief ack重发次数超过警戒值
- * @eventData RyanMqttAckHandler_t*
- */
- RyanMqttEventAckRepeatCountWarning = RyanMqttBit9,
- /**
- * @brief ack记数值超过警戒值
- * @eventData uint16_t* ackHandlerCount; 等待ack的记录个数
- */
- RyanMqttEventAckCountWarning = RyanMqttBit10,
- /**
- * @brief 用户触发,ack句柄丢弃事件,由用户手动调用RyanMqttDestroyAckHandler函数触发
- * 可能是发送qos1 / qos2消息丢弃、ack丢弃,也可能是publish报文的ack丢弃
- *
- * @eventData RyanMqttAckHandler_t*
- */
- RyanMqttEventAckHandlerDiscard = RyanMqttBit11,
- // !弃用: 请使用 RyanMqttEventAckHandlerDiscard
- RyanMqttEventAckHandlerdiscard = RyanMqttEventAckHandlerDiscard,
- /**
- * @brief 重连前事件,用户可以在此时更改connect信息
- * @eventData NULL
- */
- RyanMqttEventReconnectBefore = RyanMqttBit12,
- /**
- * @brief 用户触发,销毁客户端前回调
- * @eventData NULL
- */
- RyanMqttEventDestroyBefore = RyanMqttBit13,
- // !弃用: 请使用 RyanMqttEventDestroyBefore
- RyanMqttEventDestoryBefore = RyanMqttEventDestroyBefore,
- /**
- * @brief 接收到订阅主题数据事件,支持通配符识别,返回的主题信息是报文主题
- * @eventData RyanMqttMsgData_t*
- */
- RyanMqttEventData = RyanMqttBit14,
- /**
- * @brief 接收到未匹配任何订阅主题的报文事件
- *
- * 此事件触发时,报文的主题不在当前客户端订阅的 topic 列表中,
- *
- * @eventData RyanMqttMsgData_t*
- */
- RyanMqttEventUnsubscribedData = RyanMqttBit15,
- RyanMqttEventAnyId = UINT32_MAX,
- } RyanMqttEventId_e;
- // 定义枚举类型
- typedef enum
- {
- RyanMqttParamInvalidError = -0x100, // 参数无效
- RyanMqttRecvPacketTimeOutError, // 读取数据超时
- RyanMqttSendPacketTimeOutError, // 发送数据超时
- RyanSocketFailedError, // 套接字 FD 失败
- RyanMqttSocketConnectFailError, // MQTT socket连接失败
- RyanMqttSendPacketError, // MQTT 发送数据包错误
- RyanMqttSerializePacketError, // 序列化报文失败
- RyanMqttDeserializePacketError, // 解析报文失败
- RyanMqttNoRescourceError, // 没有资源
- RyanMqttHaveRescourceError, // 资源已存在
- RyanMqttNotConnectError, // MQTT 没有连接
- RyanMqttConnectError, // MQTT 已连接
- RyanMqttRecvBufToShortError, // MQTT 缓冲区太短
- RyanMqttSendBufToShortError, // MQTT 缓冲区太短
- RyanMqttNotEnoughMemError, // MQTT 内存不足
- RyanMqttFailedError, // 失败
- RyanMqttInvalidPacketError, // 收到非法的报文
- RyanMqttSuccessError = 0x0000, // 成功
- // RyanMqttErrorForceInt32 = INT32_MAX // 强制编译器使用int32_t类型
- } RyanMqttError_e;
- typedef enum
- {
- // mqtt标准定义
- RyanMqttConnectAccepted = 0, // 连接已被服务端接受
- RyanMqttConnectRefusedProtocolVersion = 1, // 服务端不支持客户端请求的 MQTT 协议级别
- RyanMqttConnectRefusedIdentifier = 2, // 不合格的客户端标识符
- RyanMqttConnectRefusedServer = 3, // 服务端不可用
- RyanMqttConnectRefusedUsernamePass = 4, // 无效的用户名或密码
- RyanMqttConnectRefusedNotAuthorized = 5, // 连接已拒绝,未授权
- // mqtt非标准定义
- RyanMqttConnectClientInvalid = 200, // 客户端处于无效状态
- RyanMqttConnectNetWorkFail, // 网络错误
- RyanMqttConnectDisconnected, // mqtt客户端断开连接
- RyanMqttKeepaliveTimeout, // 心跳超时断开连接
- RyanMqttConnectUserDisconnected, // 用户手动断开连接
- RyanMqttConnectTimeout, // 超时断开
- RyanMqttConnectFirstPackNotConnack, // 发送connect后接受到的第一个报文不是connack
- RyanMqttConnectProtocolError, // 多次收到connack
- RyanMqttConnectInvalidPacketError, // 收到不符合MQTT3.1.1协议的报文,并且要求关闭客户端的
- RyanMqttConnectFailedError, // 杂项原因,比如内存分配失败,序列化失败等
- // RyanMqttConnectStatusForceInt32 = INT32_MAX // 强制编译器使用int32_t类型
- } RyanMqttConnectStatus_e;
- extern const char *RyanMqttStrError(int32_t state);
- #define RyanMqttCheckCodeNoReturn(EX, ErrorCode, Ryanlevel, code) \
- if (!(EX)) \
- { \
- Ryanlevel("ErrorCode: %d, strError: %s", ErrorCode, RyanMqttStrError(ErrorCode)); \
- {code}; \
- }
- #define RyanMqttCheckCode(EX, ErrorCode, level, code) \
- RyanMqttCheckCodeNoReturn(EX, ErrorCode, level, { \
- {code}; \
- return ErrorCode; \
- });
- #define RyanMqttCheckNoReturn(EX, ErrorCode, level) RyanMqttCheckCodeNoReturn(EX, ErrorCode, level, {})
- #define RyanMqttCheck(EX, ErrorCode, level) RyanMqttCheckCode(EX, ErrorCode, level, {})
- #define RyanMqttCheckAssert(EX, ErrorCode, level) \
- RyanMqttCheckCodeNoReturn(EX, ErrorCode, level, { RyanMqttAssert(NULL && "RyanMqttCheckAssert"); })
- // 定义结构体类型
- /* extern variables-----------------------------------------------------------*/
- #ifdef __cplusplus
- }
- #endif
- #endif
|