Skip to content

Commit f800546

Browse files
author
spikelin
committed
1. fix bugs of OTA reload and HTTP download
2. add one way to check MQTT construct error 3. remove some useless code
1 parent 33118e9 commit f800546

File tree

9 files changed

+65
-46
lines changed

9 files changed

+65
-46
lines changed

include/exports/qcloud_iot_export_mqtt.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ typedef struct {
175175
uint8_t auto_connect_enable; // flag of auto reconnection, 1 is enable and recommended
176176
MQTTEventHandler event_handle; // event callback
177177

178+
int err_code;
179+
178180
} MQTTInitParams;
179181

180182
/**
@@ -183,12 +185,12 @@ typedef struct {
183185
#ifdef AUTH_MODE_CERT
184186
#define DEFAULT_MQTTINIT_PARAMS \
185187
{ \
186-
NULL, NULL, {0}, {0}, 5000, 240 * 1000, 1, 1, { 0 } \
188+
NULL, NULL, {0}, {0}, 5000, 240 * 1000, 1, 1, { 0 }, 0 \
187189
}
188190
#else
189191
#define DEFAULT_MQTTINIT_PARAMS \
190192
{ \
191-
NULL, NULL, NULL, 5000, 240 * 1000, 1, 1, { 0 } \
193+
NULL, NULL, NULL, 5000, 240 * 1000, 1, 1, { 0 }, 0 \
192194
}
193195
#endif
194196

@@ -311,6 +313,7 @@ void IOT_MQTT_StopLoop(void *pClient);
311313
*/
312314
bool IOT_MQTT_GetLoopStatus(void *pClient, int *exit_code);
313315

316+
void IOT_MQTT_SetLoopStatus(void *pClient, bool loop_status);
314317
#endif
315318

316319
#ifdef __cplusplus

include/lite-utils.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ char *LITE_format_string(const char *fmt, ...);
5656
char *LITE_format_nstring(const int len, const char *fmt, ...);
5757
void LITE_hexbuf_convert(unsigned char *digest, char *out, int buflen, int uppercase);
5858
void LITE_hexstr_convert(char *hexstr, uint8_t *out_buf, int len);
59-
void LITE_replace_substr(char orig[], char key[], char swap[]);
6059

6160
char * LITE_json_value_of(char *key, char *src);
6261
list_head_t *LITE_json_keys_of(char *src, char *prefix);

samples/multi_client/multi_client_mqtt_sample.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ static void _mqtt_client_thread_runner(void *ptr)
290290
if (client != NULL) {
291291
Log_i("Cloud Device Construct Success");
292292
} else {
293-
Log_e("MQTT Construct failed!");
293+
Log_e("MQTT Construct failed: %d", init_params.err_code);
294294
goto thread_exit;
295295
}
296296

sdk_src/network/tls/network_tls.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ int network_tls_connect(Network *pNetwork)
3232
{
3333
POINTER_SANITY_CHECK(pNetwork, QCLOUD_ERR_INVAL);
3434

35-
int ret = QCLOUD_ERR_FAILURE;
35+
int ret = QCLOUD_ERR_SSL_CONNECT;
3636

3737
pNetwork->handle = (uintptr_t)HAL_TLS_Connect(&(pNetwork->ssl_connect_params), pNetwork->host, pNetwork->port);
3838
if (pNetwork->handle != 0) {

sdk_src/protocol/http/utils_httpc.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extern "C" {
3333

3434
#define HTTP_CLIENT_AUTHB_SIZE 128
3535

36-
#define HTTP_CLIENT_CHUNK_SIZE 1024
36+
#define HTTP_CLIENT_CHUNK_SIZE 1025
3737
#define HTTP_CLIENT_SEND_BUF_SIZE 1024
3838

3939
#define HTTP_CLIENT_MAX_HOST_LEN 64
@@ -351,8 +351,7 @@ static int _http_client_recv(HTTPClient *client, char *buf, int min_len, int max
351351
IOT_FUNC_EXIT_RC(rc);
352352
}
353353

354-
// IOT_FUNC_EXIT_RC(rc);
355-
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
354+
IOT_FUNC_EXIT_RC(rc);
356355
}
357356

358357
static int _http_client_retrieve_content(HTTPClient *client, char *data, int len, uint32_t timeout_ms,
@@ -392,7 +391,7 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
392391
if (rc != QCLOUD_RET_SUCCESS) {
393392
IOT_FUNC_EXIT_RC(rc);
394393
}
395-
if (0 == left_ms(&timer)) {
394+
if (0 >= left_ms(&timer)) {
396395
Log_e("HTTP read timeout!");
397396
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
398397
}
@@ -431,7 +430,8 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
431430
rc = _http_client_recv(client, data + len, 0, HTTP_CLIENT_CHUNK_SIZE - len - 1, &new_trf_len,
432431
left_ms(&timer), client_data);
433432
len += new_trf_len;
434-
if (rc != QCLOUD_RET_SUCCESS) {
433+
if ((rc != QCLOUD_RET_SUCCESS) || (0 >= left_ms(&timer))) {
434+
Log_w("_http_client_recv ret: %d, left_time: %d", rc, left_ms(&timer));
435435
IOT_FUNC_EXIT_RC(rc);
436436
} else {
437437
continue;
@@ -495,9 +495,10 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
495495
max_len = HTTP_CLIENT_MIN(max_len, readLen);
496496
rc = _http_client_recv(client, data, 1, max_len, &len, left_ms(&timer), client_data);
497497
if (rc != QCLOUD_RET_SUCCESS) {
498+
Log_w("_http_client_recv ret: %d, left_time: %d", rc, left_ms(&timer));
498499
IOT_FUNC_EXIT_RC(rc);
499500
}
500-
if (left_ms(&timer) == 0) {
501+
if (left_ms(&timer) <= 0) {
501502
Log_e("HTTP read timeout!");
502503
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
503504
}
@@ -510,7 +511,8 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
510511
/* Read missing chars to find end of chunk */
511512
rc = _http_client_recv(client, data + len, 2 - len, HTTP_CLIENT_CHUNK_SIZE - len - 1, &new_trf_len,
512513
left_ms(&timer), client_data);
513-
if ((rc != QCLOUD_RET_SUCCESS) || (0 == left_ms(&timer))) {
514+
if ((rc != QCLOUD_RET_SUCCESS) || (0 >= left_ms(&timer))) {
515+
Log_w("_http_client_recv ret: %d, left_time: %d", rc, left_ms(&timer));
514516
IOT_FUNC_EXIT_RC(rc);
515517
}
516518
len += new_trf_len;

sdk_src/protocol/mqtt/mqtt_client.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,15 @@ void *IOT_MQTT_Construct(MQTTInitParams *pParams)
6565
// create and init MQTTClient
6666
if ((mqtt_client = (Qcloud_IoT_Client *)HAL_Malloc(sizeof(Qcloud_IoT_Client))) == NULL) {
6767
Log_e("malloc MQTTClient failed");
68+
pParams->err_code = QCLOUD_ERR_MALLOC;
6869
return NULL;
6970
}
7071

7172
int rc = qcloud_iot_mqtt_init(mqtt_client, pParams);
7273
if (rc != QCLOUD_RET_SUCCESS) {
7374
Log_e("mqtt init failed: %d", rc);
7475
HAL_Free(mqtt_client);
76+
pParams->err_code = rc;
7577
return NULL;
7678
}
7779

@@ -86,6 +88,7 @@ void *IOT_MQTT_Construct(MQTTInitParams *pParams)
8688
Log_e("Device secret is null!");
8789
qcloud_iot_mqtt_fini(mqtt_client);
8890
HAL_Free(mqtt_client);
91+
pParams->err_code = QCLOUD_ERR_INVAL;
8992
return NULL;
9093
}
9194
size_t src_len = strlen(pParams->device_secret);
@@ -99,6 +102,7 @@ void *IOT_MQTT_Construct(MQTTInitParams *pParams)
99102
Log_e("Device secret decode err, secret:%s", pParams->device_secret);
100103
qcloud_iot_mqtt_fini(mqtt_client);
101104
HAL_Free(mqtt_client);
105+
pParams->err_code = QCLOUD_ERR_INVAL;
102106
return NULL;
103107
}
104108
#endif
@@ -108,6 +112,7 @@ void *IOT_MQTT_Construct(MQTTInitParams *pParams)
108112
Log_e("mqtt connect with id: %s failed: %d", mqtt_client->options.conn_id, rc);
109113
qcloud_iot_mqtt_fini(mqtt_client);
110114
HAL_Free(mqtt_client);
115+
pParams->err_code = rc;
111116
return NULL;
112117
} else {
113118
Log_i("mqtt connect with id: %s success", mqtt_client->options.conn_id);
@@ -326,6 +331,13 @@ bool IOT_MQTT_GetLoopStatus(void *pClient, int *exit_code)
326331
return mqtt_client->thread_running;
327332
}
328333

334+
void IOT_MQTT_SetLoopStatus(void *pClient, bool loop_status)
335+
{
336+
POINTER_SANITY_CHECK_RTN(pClient);
337+
Qcloud_IoT_Client *mqtt_client = (Qcloud_IoT_Client *)pClient;
338+
mqtt_client->thread_running = loop_status;
339+
}
340+
329341
#endif
330342

331343
#if 0

sdk_src/services/ota/ota_client.c

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,17 @@ static void _ota_callback(void *pcontext, const char *msg, uint32_t msg_len)
128128
static void IOT_OTA_ResetStatus(void *handle)
129129
{
130130
OTA_Struct_t *h_ota = (OTA_Struct_t *)handle;
131+
Log_i("reset OTA state!");
131132
h_ota->state = IOT_OTAS_INITED;
133+
h_ota->err = 0;
134+
135+
if (NULL != h_ota->purl) {
136+
HAL_Free(h_ota->purl);
137+
}
138+
139+
if (NULL != h_ota->version) {
140+
HAL_Free(h_ota->version);
141+
}
132142
}
133143

134144
static int IOT_OTA_ReportProgress(void *handle, IOT_OTA_Progress_Code progress, IOT_OTAReportType reportType)
@@ -204,6 +214,9 @@ static int IOT_OTA_ReportUpgradeResult(void *handle, const char *version, IOT_OT
204214
return QCLOUD_ERR_FAILURE;
205215
}
206216

217+
if (version == NULL)
218+
version = h_ota->version;
219+
207220
len = strlen(version);
208221
if ((len < OTA_VERSION_STR_LEN_MIN) || (len > OTA_VERSION_STR_LEN_MAX)) {
209222
Log_e("version string is invalid: must be [1, 32] chars");
@@ -233,7 +246,10 @@ static int IOT_OTA_ReportUpgradeResult(void *handle, const char *version, IOT_OT
233246
goto do_exit;
234247
}
235248

249+
if ((IOT_OTAR_UPGRADE_FAIL == reportType) || (IOT_OTAR_UPGRADE_SUCCESS == reportType) ||
250+
(IOT_OTAR_MD5_NOT_MATCH == reportType)) {
236251
IOT_OTA_ResetStatus(h_ota);
252+
}
237253

238254
do_exit:
239255
if (NULL != msg_upgrade) {
@@ -341,6 +357,18 @@ int IOT_OTA_StartDownload(void *handle, uint32_t offset, uint32_t size)
341357

342358
Log_d("to download FW from offset: %u, size: %u", offset, size);
343359
h_ota->size_fetched = offset;
360+
361+
// reset md5 for new download
362+
if (offset == 0) {
363+
Ret = IOT_OTA_ResetClientMD5(h_ota);
364+
if (Ret) {
365+
Log_e("initialize md5 failed");
366+
return QCLOUD_ERR_FAILURE;
367+
}
368+
}
369+
370+
// reinit ofc
371+
qcloud_ofc_deinit(h_ota->ch_fetch);
344372
h_ota->ch_fetch = ofc_Init(h_ota->purl, offset, size);
345373
if (NULL == h_ota->ch_fetch) {
346374
Log_e("Initialize fetch module failed");
@@ -507,7 +535,7 @@ int IOT_OTA_IsFetchFinish(void *handle)
507535
return (IOT_OTAS_FETCHED == h_ota->state);
508536
}
509537

510-
int IOT_OTA_FetchYield(void *handle, char *buf, uint32_t buf_len, uint32_t timeout_ms)
538+
int IOT_OTA_FetchYield(void *handle, char *buf, uint32_t buf_len, uint32_t timeout_s)
511539
{
512540
int ret;
513541
OTA_Struct_t *h_ota = (OTA_Struct_t *)handle;
@@ -521,7 +549,7 @@ int IOT_OTA_FetchYield(void *handle, char *buf, uint32_t buf_len, uint32_t timeo
521549
return IOT_OTA_ERR_INVALID_STATE;
522550
}
523551

524-
ret = qcloud_ofc_fetch(h_ota->ch_fetch, buf, buf_len, timeout_ms);
552+
ret = qcloud_ofc_fetch(h_ota->ch_fetch, buf, buf_len, timeout_s);
525553
if (ret < 0) {
526554
h_ota->state = IOT_OTAS_FETCHED;
527555
h_ota->err = IOT_OTA_ERR_FETCH_FAILED;

sdk_src/services/ota/ota_fetch.c

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,14 @@ int32_t qcloud_ofc_fetch(void *handle, char *buf, uint32_t bufLen, uint32_t time
135135

136136
int qcloud_ofc_deinit(void *handle)
137137
{
138-
IOT_FUNC_ENTRY;
139-
if (NULL != handle) {
140-
HAL_Free(handle);
141-
}
138+
OTAHTTPStruct *h_odc = (OTAHTTPStruct *)handle;
139+
if (NULL == handle)
140+
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
141+
142+
if (h_odc->http.network_stack.is_connected(&h_odc->http.network_stack))
143+
h_odc->http.network_stack.disconnect(&h_odc->http.network_stack);
142144

145+
HAL_Free(handle);
143146
IOT_FUNC_EXIT_RC(QCLOUD_RET_SUCCESS);
144147
}
145148

sdk_src/utils/string_utils.c

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -135,31 +135,3 @@ void LITE_hexstr_convert(char *hexstr, uint8_t *out_buf, int in_len)
135135
}
136136
}
137137

138-
void LITE_replace_substr(char originalString[], char key[], char swap[])
139-
{
140-
int lengthOfOriginalString, lengthOfKey, lengthOfSwap, i, j, flag;
141-
char tmp[512];
142-
143-
lengthOfOriginalString = strlen(originalString);
144-
lengthOfKey = strlen(key);
145-
lengthOfSwap = strlen(swap);
146-
147-
for (i = 0; i <= lengthOfOriginalString - lengthOfKey; i++) {
148-
flag = 1;
149-
for (j = 0; j < lengthOfKey; j++) {
150-
if (originalString[i + j] != key[j]) {
151-
flag = 0;
152-
break;
153-
}
154-
}
155-
156-
if (flag) {
157-
strcpy(tmp, originalString);
158-
strcpy(&tmp[i], swap);
159-
strcpy(&tmp[i + lengthOfSwap], &originalString[i + lengthOfKey]);
160-
strcpy(originalString, tmp);
161-
i += lengthOfSwap - 1;
162-
lengthOfOriginalString = strlen(originalString);
163-
}
164-
}
165-
}

0 commit comments

Comments
 (0)