Skip to content

Commit fdad955

Browse files
committed
v2.3.3发布
1. 优化MQTT PING request发包策略。 2. 优化错误码类型。 3. 修复MQTT获取sys及log消息的问题。
1 parent a8fe9cc commit fdad955

10 files changed

+46
-42
lines changed

src/mqtt/include/mqtt_client.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ uint16_t get_next_packet_id(Qcloud_IoT_Client *pClient);
374374
* @param options
375375
* @return
376376
*/
377-
void get_next_conn_id(MQTTConnectParams *options);
377+
void get_next_conn_id(char *conn_id);
378378

379379
/**
380380
*

src/mqtt/src/mqtt_client.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ static int s_qcloud_iot_port = 8883;
4848
static unsigned char sg_psk_str[DECODE_PSK_LENGTH];
4949
#endif
5050

51-
#define min(a,b) (a) < (b) ? (a) : (b)
5251

5352

5453
static uint16_t _get_random_start_packet_id(void)
@@ -88,7 +87,7 @@ void* IOT_MQTT_Construct(MQTTInitParams *pParams)
8887
MQTTConnectParams connect_params = DEFAULT_MQTTCONNECT_PARAMS;
8988
connect_params.client_id = iot_device_info_get()->client_id;
9089
// 超过11.5分钟的心跳间隔自动转为11.5(11.5 * 60)的时长
91-
connect_params.keep_alive_interval = min(pParams->keep_alive_interval_ms / 1000, 690);
90+
connect_params.keep_alive_interval = Min(pParams->keep_alive_interval_ms / 1000, 690);
9291
connect_params.clean_session = pParams->clean_session;
9392
connect_params.auto_connect_enable = pParams->auto_connect_enable;
9493
#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)

src/mqtt/src/mqtt_client_common.c

+7-6
Original file line numberDiff line numberDiff line change
@@ -65,25 +65,25 @@ uint16_t get_next_packet_id(Qcloud_IoT_Client *pClient) {
6565
IOT_FUNC_EXIT_RC(pClient->next_packet_id);
6666
}
6767

68-
void get_next_conn_id(MQTTConnectParams *options) {
68+
void get_next_conn_id(char *conn_id) {
6969
int i;
7070
srand((unsigned)time(0));
7171
for (i = 0; i < MAX_CONN_ID_LEN - 1; i++) {
7272
int flag = rand() % 3;
7373
switch(flag) {
7474
case 0:
75-
options->conn_id[i] = (rand() % 26) + 'a';
75+
conn_id[i] = (rand() % 26) + 'a';
7676
break;
7777
case 1:
78-
options->conn_id[i] = (rand() % 26) + 'A';
78+
conn_id[i] = (rand() % 26) + 'A';
7979
break;
8080
case 2:
81-
options->conn_id[i] = (rand() % 10) + '0';
81+
conn_id[i] = (rand() % 10) + '0';
8282
break;
8383
}
8484
}
8585

86-
options->conn_id[MAX_CONN_ID_LEN - 1] = '\0';
86+
conn_id[MAX_CONN_ID_LEN - 1] = '\0';
8787
}
8888

8989
/**
@@ -1334,15 +1334,16 @@ int cycle_for_read(Qcloud_IoT_Client *pClient, Timer *timer, uint8_t *packet_typ
13341334
}
13351335
}
13361336

1337-
/* Receiving below msgs are all considered as PING OK */
13381337
switch (*packet_type) {
1338+
/* Recv below msgs are all considered as PING OK */
13391339
case PUBACK:
13401340
case SUBACK:
13411341
case UNSUBACK:
13421342
case PINGRESP: {
13431343
_handle_pingresp_packet(pClient);
13441344
break;
13451345
}
1346+
/* Recv downlink pub means link is OK but we still need to send PING request */
13461347
case PUBLISH: {
13471348
HAL_MutexLock(pClient->lock_generic);
13481349
pClient->is_ping_outstanding = 0;

src/mqtt/src/mqtt_client_connect.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ static int _serialize_connect_packet(unsigned char *buf, size_t buf_len, MQTTCon
128128

129129
int username_len = strlen(options->client_id) + strlen(QCLOUD_IOT_DEVICE_SDK_APPID) + MAX_CONN_ID_LEN + cur_timesec_len + 4;
130130
options->username = (char*)HAL_Malloc(username_len);
131-
get_next_conn_id(options);
131+
get_next_conn_id(options->conn_id);
132132
HAL_Snprintf(options->username, username_len, "%s;%s;%s;%ld", options->client_id, QCLOUD_IOT_DEVICE_SDK_APPID, options->conn_id, cur_timesec);
133133

134134
#if defined(AUTH_WITH_NOTLS) && defined(AUTH_MODE_KEY)

src/mqtt/src/mqtt_client_yield.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ static int _handle_reconnect(Qcloud_IoT_Client *pClient) {
132132
*/
133133
static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient)
134134
{
135-
#define MQTT_PING_TIMES_IN_KEEPALIVE_INTERVAL 2
135+
#define MQTT_PING_RETRY_TIMES 2
136136

137137
IOT_FUNC_ENTRY;
138138

@@ -148,7 +148,7 @@ static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient)
148148
IOT_FUNC_EXIT_RC(QCLOUD_ERR_SUCCESS);
149149
}
150150

151-
if (pClient->is_ping_outstanding >= MQTT_PING_TIMES_IN_KEEPALIVE_INTERVAL) {
151+
if (pClient->is_ping_outstanding >= MQTT_PING_RETRY_TIMES) {
152152
//Reaching here means we haven't received any MQTT packet for a long time (keep_alive_interval)
153153
Log_e("Fail to recv MQTT msg. Something wrong with the connection.");
154154
rc = _handle_disconnect(pClient);
@@ -183,7 +183,7 @@ static int _mqtt_keep_alive(Qcloud_IoT_Client *pClient)
183183
HAL_MutexLock(pClient->lock_generic);
184184
pClient->is_ping_outstanding++;
185185
/* start a timer to wait for PINGRESP from server */
186-
countdown(&pClient->ping_timer, pClient->options.keep_alive_interval/MQTT_PING_TIMES_IN_KEEPALIVE_INTERVAL);
186+
countdown(&pClient->ping_timer, Min(5, pClient->options.keep_alive_interval/2));
187187
HAL_MutexUnlock(pClient->lock_generic);
188188
Log_d("PING request %u has been sent...", pClient->is_ping_outstanding);
189189

src/nbiot/src/nbiot_client.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ typedef enum tlvType {
3434

3535
int toBigEnd32(unsigned char* buf, uint32_t x)
3636
{
37-
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_NULL);
37+
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
3838

3939
buf[0] = (x >> 24) & 0xFF;
4040
buf[1] = (x >> 16) & 0xFF;
@@ -46,7 +46,7 @@ int toBigEnd32(unsigned char* buf, uint32_t x)
4646

4747
int toBigEnd16(unsigned char* buf, uint16_t x)
4848
{
49-
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_NULL);
49+
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
5050

5151
*buf = ((x) & 0xFF00) >> 8 ;
5252
*(buf+1) = (x) & 0xFF ;
@@ -57,8 +57,8 @@ int toBigEnd16(unsigned char* buf, uint16_t x)
5757

5858
int ntohBig32(uint32_t* x, unsigned char* buf)
5959
{
60-
POINTER_SANITY_CHECK(x, QCLOUD_ERR_NULL);
61-
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_NULL);
60+
POINTER_SANITY_CHECK(x, QCLOUD_ERR_INVAL);
61+
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
6262

6363
*x = (buf[0] << 24) + (buf[1] << 16) + (buf[2] << 8) + buf[3];
6464

@@ -67,8 +67,8 @@ int ntohBig32(uint32_t* x, unsigned char* buf)
6767

6868
int ntohBig16(uint16_t* x, unsigned char* buf)
6969
{
70-
POINTER_SANITY_CHECK(x, QCLOUD_ERR_NULL);
71-
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_NULL);
70+
POINTER_SANITY_CHECK(x, QCLOUD_ERR_INVAL);
71+
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
7272

7373
*x = (buf[0] << 8) + buf[1];
7474

@@ -97,10 +97,10 @@ void StrToHex(char *pbDest, char *pbSrc, int nLen)
9797

9898
int calToken(char* token, int* tokenSize, const unsigned char* keyBase64, char* topic, uint32_t expiry, uint8_t qos, char* payload)
9999
{
100-
POINTER_SANITY_CHECK(token, QCLOUD_ERR_NULL);
101-
POINTER_SANITY_CHECK(keyBase64, QCLOUD_ERR_NULL);
102-
POINTER_SANITY_CHECK(topic, QCLOUD_ERR_NULL);
103-
POINTER_SANITY_CHECK(payload, QCLOUD_ERR_NULL);
100+
POINTER_SANITY_CHECK(token, QCLOUD_ERR_INVAL);
101+
POINTER_SANITY_CHECK(keyBase64, QCLOUD_ERR_INVAL);
102+
POINTER_SANITY_CHECK(topic, QCLOUD_ERR_INVAL);
103+
POINTER_SANITY_CHECK(payload, QCLOUD_ERR_INVAL);
104104

105105
size_t len = 0;
106106

@@ -148,8 +148,8 @@ int calToken(char* token, int* tokenSize, const unsigned char* keyBase64, char*
148148

149149
int addTLV(unsigned char* buf, uint8_t type, char* value)
150150
{
151-
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_NULL);
152-
POINTER_SANITY_CHECK(value, QCLOUD_ERR_NULL);
151+
POINTER_SANITY_CHECK(buf, QCLOUD_ERR_INVAL);
152+
POINTER_SANITY_CHECK(value, QCLOUD_ERR_INVAL);
153153
unsigned char* ptr = buf;
154154
uint16_t length = strlen(value);
155155

@@ -171,9 +171,9 @@ int addTLV(unsigned char* buf, uint8_t type, char* value)
171171

172172
int IOT_NB_setMessage(unsigned char* msg, unsigned int* length, NBIoTSetMessage* nbiotMsg)
173173
{
174-
POINTER_SANITY_CHECK(msg, QCLOUD_ERR_NULL);
175-
POINTER_SANITY_CHECK(length, QCLOUD_ERR_NULL);
176-
POINTER_SANITY_CHECK(nbiotMsg, QCLOUD_ERR_NULL);
174+
POINTER_SANITY_CHECK(msg, QCLOUD_ERR_INVAL);
175+
POINTER_SANITY_CHECK(length, QCLOUD_ERR_INVAL);
176+
POINTER_SANITY_CHECK(nbiotMsg, QCLOUD_ERR_INVAL);
177177

178178
uint16_t v1Size = strlen(nbiotMsg->topic);
179179
if( v1Size > MAX_SIZE_TOPIC )
@@ -267,8 +267,8 @@ int IOT_NB_setMessage(unsigned char* msg, unsigned int* length, NBIoTSetMessage*
267267

268268
int IOT_NB_getMessage(NBIoTGetMessage* nbiotMsg, unsigned char* msg)
269269
{
270-
POINTER_SANITY_CHECK(nbiotMsg, QCLOUD_ERR_NULL);
271-
POINTER_SANITY_CHECK(msg, QCLOUD_ERR_NULL);
270+
POINTER_SANITY_CHECK(nbiotMsg, QCLOUD_ERR_INVAL);
271+
POINTER_SANITY_CHECK(msg, QCLOUD_ERR_INVAL);
272272

273273
size_t len = 0;
274274
//uint16_t u16Temp = 0;
@@ -392,4 +392,4 @@ int IOT_NB_getMessage(NBIoTGetMessage* nbiotMsg, unsigned char* msg)
392392

393393

394394
return QCLOUD_ERR_SUCCESS;
395-
}
395+
}

src/sdk-impl/exports/qcloud_iot_export_error.h

-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ typedef enum {
4242
QCLOUD_ERR_SUCCESS = 0, // 表示成功返回
4343
QCLOUD_ERR_FAILURE = -1001, // 表示失败返回
4444
QCLOUD_ERR_INVAL = -1002, // 表示参数无效错误
45-
QCLOUD_ERR_NULL = -1003, // 表示空指针
4645

4746
QCLOUD_ERR_MQTT_PUSH_TO_LIST_FAILED = -102, // 表示往等待 ACK 列表中添加元素失败
4847
QCLOUD_ERR_MQTT_NO_CONN = -103, // 表示未与MQTT服务器建立连接或已经断开连接

src/sdk-impl/qcloud_iot_import.h

+4
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ extern "C" {
3333
#define IOT_TRUE (1) /* indicate boolean value true */
3434
#define IOT_FALSE (0) /* indicate boolean value false */
3535

36+
#define Max(a,b) ((a) > (b) ? (a) : (b))
37+
#define Min(a,b) ((a) < (b) ? (a) : (b))
38+
39+
3640
/**
3741
* @brief 创建互斥锁
3842
*

src/system/src/log_mqtt.c

100644100755
+5-5
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "device.h"
2323
#include "log_upload.h"
2424

25-
#define min(a,b) (a) < (b) ? (a) : (b)
2625

2726
static bool sg_log_recv_ok = false;
2827
static bool sg_log_sub_ok = false;
@@ -54,7 +53,7 @@ static void _log_level_sub_cb(void *pClient, MQTTMessage *message, void *userDat
5453
return;
5554
}
5655

57-
json_buf_len = min(LOG_JSON_LENGTH - 1, message->payload_len);
56+
json_buf_len = Min(LOG_JSON_LENGTH - 1, message->payload_len);
5857
memcpy(json_buf, message->payload, json_buf_len);
5958
json_buf[json_buf_len] = '\0'; // json_parse relies on a string
6059

@@ -125,11 +124,11 @@ static void _log_mqtt_event_handler(void *pclient, void *handle_context, MQTTEve
125124

126125
static int _iot_log_level_get_publish(void *pClient)
127126
{
128-
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_NULL);
127+
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
129128

130129
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
131130
DeviceInfo *dev_info = iot_device_info_get();
132-
POINTER_SANITY_CHECK(dev_info, QCLOUD_ERR_NULL);
131+
POINTER_SANITY_CHECK(dev_info, QCLOUD_ERR_INVAL);
133132

134133
char topic_name[128] = {0};
135134
char payload_content[128] = {0};
@@ -173,7 +172,7 @@ int qcloud_get_log_level(void* pClient, int *log_level)
173172
int cntSub = 0;
174173
int cntRev = 0;
175174

176-
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_NULL);
175+
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
177176
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
178177

179178
//如果第一次订阅$log/operation/get/${productid}/${devicename}, 则执行订阅操作
@@ -204,6 +203,7 @@ int qcloud_get_log_level(void* pClient, int *log_level)
204203
return QCLOUD_ERR_FAILURE;
205204
}
206205

206+
sg_log_recv_ok = false;
207207
// 发布获取时间
208208
ret = _iot_log_level_get_publish(mqtt_client);
209209
if (ret < 0) {

src/system/src/system_mqtt.c

100644100755
+6-5
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ static void system_mqtt_event_handler(void *pclient, void *handle_context, MQTTE
9292

9393
static int _iot_system_info_get_publish(void *pClient)
9494
{
95-
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_NULL);
95+
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
9696

9797
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
9898
DeviceInfo *dev_info = iot_device_info_get();
99-
POINTER_SANITY_CHECK(dev_info, QCLOUD_ERR_NULL);
99+
POINTER_SANITY_CHECK(dev_info, QCLOUD_ERR_INVAL);
100100

101101
char topic_name[128] = {0};
102102
char payload_content[128] = {0};
@@ -114,10 +114,10 @@ static int _iot_system_info_get_publish(void *pClient)
114114

115115
static int _iot_system_info_result_subscribe(void *pClient, OnMessageHandler pCallback)
116116
{
117-
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_NULL);
117+
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
118118

119119
DeviceInfo *dev_info = iot_device_info_get();
120-
POINTER_SANITY_CHECK(dev_info, QCLOUD_ERR_NULL);
120+
POINTER_SANITY_CHECK(dev_info, QCLOUD_ERR_INVAL);
121121

122122
char topic_name[128] = {0};
123123
int size = HAL_Snprintf(topic_name, sizeof(topic_name), "$sys/operation/result/%s/%s", dev_info->product_id, dev_info->device_name);
@@ -138,7 +138,7 @@ int IOT_SYSTEM_GET_TIME(void* pClient, long *time)
138138
int cntSub = 0;
139139
int cntRev = 0;
140140

141-
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_NULL);
141+
POINTER_SANITY_CHECK(pClient, QCLOUD_ERR_INVAL);
142142
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
143143

144144
//如果第一次订阅$sys/operation/get/${productid}/${devicename}, 则执行订阅操作
@@ -169,6 +169,7 @@ int IOT_SYSTEM_GET_TIME(void* pClient, long *time)
169169
return QCLOUD_ERR_FAILURE;
170170
}
171171

172+
sg_sys_recv_ok = false;
172173
// 发布获取时间
173174
ret = _iot_system_info_get_publish(mqtt_client);
174175
if (ret < 0) {

0 commit comments

Comments
 (0)