1 /* 2 * Copyright (C) 2015-2018 Alibaba Group Holding Limited 3 */ 4 5 #ifndef __IOTX_MQTT_H__ 6 #define __IOTX_MQTT_H__ 7 8 #include "linkkit/infra/infra_types.h" 9 #include "linkkit/infra/infra_list.h" 10 #include "linkkit/infra/infra_timer.h" 11 #include "iotx_mqtt_config.h" 12 #include "linkkit/mqtt_api.h" 13 14 #include "MQTTPacket.h" 15 #ifdef INFRA_MEM_STATS 16 #include "linkkit/infra/infra_mem_stats.h" 17 #define mqtt_malloc(size) LITE_malloc(size, MEM_MAGIC, "mqtt") 18 #define mqtt_free(ptr) LITE_free(ptr) 19 #else 20 #define mqtt_malloc(size) HAL_Malloc(size) 21 #define mqtt_free(ptr) \ 22 { \ 23 HAL_Free((void *)ptr); \ 24 ptr = NULL; \ 25 } 26 #endif 27 28 #define MQTT_DYNBUF_SEND_MARGIN (64) 29 30 #define MQTT_DYNBUF_RECV_MARGIN (8) 31 32 typedef enum { 33 IOTX_MC_CONNECTION_ACCEPTED = 0, 34 IOTX_MC_CONNECTION_REFUSED_UNACCEPTABLE_PROTOCOL_VERSION = 1, 35 IOTX_MC_CONNECTION_REFUSED_IDENTIFIER_REJECTED = 2, 36 IOTX_MC_CONNECTION_REFUSED_SERVER_UNAVAILABLE = 3, 37 IOTX_MC_CONNECTION_REFUSED_BAD_USERDATA = 4, 38 IOTX_MC_CONNECTION_REFUSED_NOT_AUTHORIZED = 5 39 } iotx_mc_connect_ack_code_t; 40 41 /* State of MQTT client */ 42 typedef enum { 43 IOTX_MC_STATE_INVALID = 0, /* MQTT in invalid state */ 44 IOTX_MC_STATE_INITIALIZED = 1, /* MQTT in initializing state */ 45 IOTX_MC_STATE_CONNECTED = 2, /* MQTT in connected state */ 46 IOTX_MC_STATE_DISCONNECTED = 3, /* MQTT in disconnected state */ 47 IOTX_MC_STATE_DISCONNECTED_RECONNECTING = 48 4, /* MQTT in reconnecting state */ 49 IOTX_MC_STATE_CONNECT_BLOCK = 50 5 /* MQTT in connecting state when using async protocol stack */ 51 } iotx_mc_state_t; 52 53 typedef enum MQTT_NODE_STATE { 54 IOTX_MC_NODE_STATE_NORMANL = 0, 55 IOTX_MC_NODE_STATE_INVALID, 56 } iotx_mc_node_t; 57 58 typedef enum { 59 TOPIC_NAME_TYPE = 0, 60 TOPIC_FILTER_TYPE 61 } iotx_mc_topic_type_t; 62 63 /* Handle structure of subscribed topic */ 64 typedef struct iotx_mc_topic_handle_s { 65 iotx_mc_topic_type_t topic_type; 66 iotx_mqtt_event_handle_t handle; 67 #ifdef PLATFORM_HAS_DYNMEM 68 const char *topic_filter; 69 struct list_head linked_list; 70 #else 71 const char topic_filter[CONFIG_MQTT_TOPIC_MAXLEN]; 72 int used; 73 #endif 74 } iotx_mc_topic_handle_t; 75 76 #if !WITH_MQTT_ONLY_QOS0 77 /* Information structure of published topic */ 78 typedef struct REPUBLISH_INFO { 79 iotx_time_t pub_start_time; /* start time of publish request */ 80 iotx_mc_node_t node_state; /* state of this node */ 81 uint16_t msg_id; /* packet id of publish */ 82 uint32_t len; /* length of publish message */ 83 #ifdef PLATFORM_HAS_DYNMEM 84 unsigned char *buf; /* publish message */ 85 struct list_head linked_list; 86 #else 87 unsigned char buf[IOTX_MC_TX_MAX_LEN]; /* publish message */ 88 int used; 89 #endif 90 } iotx_mc_pub_info_t, *iotx_mc_pub_info_pt; 91 #endif 92 /* Reconnected parameter of MQTT client */ 93 typedef struct { 94 iotx_time_t reconnect_next_time; /* the next time point of reconnect */ 95 uint32_t reconnect_time_interval_ms; /* time interval of this reconnect */ 96 } iotx_mc_reconnect_param_t; 97 98 typedef struct { 99 uintptr_t packet_id; 100 uint8_t ack_type; 101 iotx_mqtt_event_handle_func_fpt sub_state_cb; 102 #ifdef PLATFORM_HAS_DYNMEM 103 struct list_head linked_list; 104 #else 105 int used; 106 #endif 107 } mqtt_sub_sync_node_t; 108 109 /* structure of MQTT client */ 110 typedef struct Client { 111 void *lock_generic; /* generic lock */ 112 uint32_t packet_id; /* packet id */ 113 uint32_t request_timeout_ms; /* request timeout in millisecond */ 114 uint32_t cycle_timeout_ms; 115 uint32_t buf_size_send; /* send buffer size in byte */ 116 #ifdef PLATFORM_HAS_DYNMEM 117 #if WITH_MQTT_DYN_BUF 118 uint32_t buf_size_send_max; /* send buffer size max limit in byte */ 119 uint32_t buf_size_read_max; /* recv buffer size max limit in byte */ 120 #endif 121 #endif 122 uint32_t buf_size_read; /* read buffer size in byte */ 123 uint8_t keepalive_probes; /* keepalive probes */ 124 #ifdef PLATFORM_HAS_DYNMEM 125 char *buf_send; /* pointer of send buffer */ 126 char *buf_read; /* pointer of read buffer */ 127 #else 128 char buf_send[IOTX_MC_TX_MAX_LEN]; 129 char buf_read[IOTX_MC_RX_MAX_LEN]; 130 #endif 131 #ifdef PLATFORM_HAS_DYNMEM 132 struct list_head list_sub_handle; /* list of subscribe handle */ 133 #else 134 iotx_mc_topic_handle_t list_sub_handle[IOTX_MC_SUBHANDLE_LIST_MAX_LEN]; 135 #endif 136 utils_network_t ipstack; /* network parameter */ 137 iotx_time_t next_ping_time; /* next ping time */ 138 iotx_mc_state_t client_state; /* state of MQTT client */ 139 iotx_mc_reconnect_param_t reconnect_param; /* reconnect parameter */ 140 MQTTPacket_connectData connect_data; /* connection parameter */ 141 #if !WITH_MQTT_ONLY_QOS0 142 #ifdef PLATFORM_HAS_DYNMEM 143 struct list_head list_pub_wait_ack; /* list of wait publish ack */ 144 #else 145 iotx_mc_pub_info_t list_pub_wait_ack[IOTX_MC_REPUB_NUM_MAX]; 146 #endif 147 #endif 148 #ifdef PLATFORM_HAS_DYNMEM 149 struct list_head list_sub_sync_ack; 150 #else 151 mqtt_sub_sync_node_t list_sub_sync_ack[IOTX_MC_SUBSYNC_LIST_MAX_LEN]; 152 #endif 153 void *lock_list_pub; /* lock for list of QoS1 pub */ 154 void *lock_write_buf; /* lock of write */ 155 void *lock_read_buf; /* lock of write */ 156 void *lock_yield; 157 iotx_mqtt_event_handle_t handle_event; /* event handle */ 158 #ifndef PLATFORM_HAS_DYNMEM 159 int used; 160 #endif 161 } iotx_mc_client_t, *iotx_mc_client_pt; 162 163 /* Information structure of mutli-subscribe */ 164 typedef struct { 165 const char *topicFilter; 166 iotx_mqtt_qos_t qos; 167 iotx_mqtt_event_handle_func_fpt messageHandler; 168 } iotx_mutli_sub_info_t, *iotx_mutli_sub_info_pt; 169 170 #endif /* __IOTX_MQTT_H__ */ 171