Skip to content

Commit ef3263b

Browse files
author
yougaliu
committed
1. OTA支持断点续传。
2. SDK版本号更新为3.0.3。
1 parent 50e1926 commit ef3263b

File tree

15 files changed

+415
-87
lines changed

15 files changed

+415
-87
lines changed

samples/ota/ota_mqtt_sample.c

Lines changed: 302 additions & 56 deletions
Large diffs are not rendered by default.

samples/samples.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ ifneq (,$(filter -DOTA_COMM_ENABLED,$(CFLAGS)))
4949
ifneq (,$(filter -DOTA_MQTT_CHANNEL,$(CFLAGS)))
5050
ota_mqtt_sample:
5151
$(eval CFLAGS := $(filter-out $(IOTSDK_INCLUDE_FILES),$(CFLAGS)) \
52-
-I$(TOP_DIR)/src/sdk-impl -I$(TOP_DIR)/src/sdk-impl/exports)
52+
-I$(TOP_DIR)/src/sdk-impl -I$(TOP_DIR)/src/sdk-impl/exports -I$(TOP_DIR)/src/utils/lite)
5353

5454
$(TOP_Q) \
5555
$(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/ota/$@.c $(LDFLAGS) -o $@

src/mqtt/src/mqtt_client.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams) {
228228
POINTER_SANITY_CHECK(pParams, QCLOUD_ERR_INVAL);
229229

230230
memset(pClient, 0x0, sizeof(Qcloud_IoT_Client));
231+
231232

232233
int size = HAL_Snprintf(s_qcloud_iot_host, HOST_STR_LENGTH, "%s.%s", pParams->product_id, QCLOUD_IOT_MQTT_DIRECT_DOMAIN);
233234
if (size < 0 || size > HOST_STR_LENGTH - 1) {

src/ota/include/ota_fetch.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ extern "C" {
2222

2323
#include <stdint.h>
2424

25-
void *ofc_Init(const char *url);
25+
26+
void *ofc_Init(const char *url, uint32_t offset, uint32_t size);
2627

2728
int32_t qcloud_ofc_connect(void *handle);
2829

src/ota/src/ota_client.c

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,6 @@ static void _ota_callback(void *pcontext, const char *msg, uint32_t msg_len) {
115115
goto End;
116116
}
117117

118-
if (NULL == (h_ota->ch_fetch = ofc_Init(h_ota->purl))) {
119-
Log_e("Initialize fetch module failed");
120-
goto End;
121-
}
122-
123-
if (0 != qcloud_ofc_connect(h_ota->ch_fetch)) {
124-
Log_e("Connect fetch module failed");
125-
h_ota->state = IOT_OTAS_DISCONNECTED;
126-
goto End;
127-
}
128-
129118
h_ota->state = IOT_OTAS_FETCHING;
130119
}
131120

@@ -337,6 +326,38 @@ int IOT_OTA_Destroy(void *handle)
337326
return 0;
338327
}
339328

329+
/*support continuous transmission of breakpoints*/
330+
int IOT_OTA_StartDownload(void *handle, uint32_t offset, uint32_t size)
331+
{
332+
OTA_Struct_t *h_ota = (OTA_Struct_t *) handle;
333+
int Ret;
334+
335+
h_ota->size_fetched += offset;
336+
h_ota->ch_fetch = ofc_Init(h_ota->purl, offset, size);
337+
if (NULL == h_ota->ch_fetch) {
338+
Log_e("Initialize fetch module failed");
339+
return QCLOUD_ERR_FAILURE;
340+
}
341+
342+
Ret = qcloud_ofc_connect(h_ota->ch_fetch);
343+
if (QCLOUD_ERR_SUCCESS != Ret) {
344+
Log_e("Connect fetch module failed");
345+
h_ota->state = IOT_OTAS_DISCONNECTED;
346+
}
347+
348+
return Ret;
349+
}
350+
351+
/*support continuous transmission of breakpoints*/
352+
void IOT_OTA_UpdateClientMd5(void *handle, char * buff, uint32_t size)
353+
{
354+
OTA_Struct_t *h_ota = (OTA_Struct_t *) handle;
355+
356+
qcloud_otalib_md5_update(h_ota->md5, buff, size);
357+
}
358+
359+
360+
340361
int IOT_OTA_ReportVersion(void *handle, const char *version)
341362
{
342363
#define MSG_INFORM_LEN (128)
@@ -400,12 +421,31 @@ int IOT_OTA_ReportUpgradeBegin(void *handle)
400421

401422
int IOT_OTA_ReportUpgradeSuccess(void *handle, const char *version)
402423
{
403-
return IOT_OTA_ReportUpgradeResult(handle, version, IOT_OTAR_UPGRADE_SUCCESS);
424+
OTA_Struct_t *h_ota = (OTA_Struct_t *) handle;
425+
int ret;
426+
427+
if(NULL == version){
428+
ret = IOT_OTA_ReportUpgradeResult(handle, h_ota->version, IOT_OTAR_UPGRADE_SUCCESS);
429+
}else{
430+
ret = IOT_OTA_ReportUpgradeResult(handle, version, IOT_OTAR_UPGRADE_SUCCESS);
431+
}
432+
433+
return ret;
404434
}
405435

436+
406437
int IOT_OTA_ReportUpgradeFail(void *handle, const char *version)
407438
{
408-
return IOT_OTA_ReportUpgradeResult(handle, version, IOT_OTAR_UPGRADE_FAIL);
439+
OTA_Struct_t *h_ota = (OTA_Struct_t *) handle;
440+
int ret;
441+
442+
if(NULL == version){
443+
ret = IOT_OTA_ReportUpgradeResult(handle, h_ota->version, IOT_OTAR_UPGRADE_FAIL);
444+
}else{
445+
ret = IOT_OTA_ReportUpgradeResult(handle, version, IOT_OTAR_UPGRADE_FAIL);
446+
}
447+
448+
return ret;
409449
}
410450

411451

src/ota/src/ota_fetch.c

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@ extern "C" {
2626
#include "qcloud_iot_import.h"
2727

2828
#include "ca.h"
29-
3029
#include "utils_httpc.h"
3130

31+
32+
33+
#define OTA_HTTP_HEAD_CONTENT_LEN 256
34+
3235
/* ofc, OTA fetch channel */
3336

3437
typedef struct {
@@ -59,7 +62,9 @@ static int is_begin_with(const char * str1,char *str2)
5962
return 1;
6063
}
6164

62-
void *ofc_Init(const char *url)
65+
66+
static char sg_head_content[OTA_HTTP_HEAD_CONTENT_LEN];
67+
void *ofc_Init(const char *url, uint32_t offset, uint32_t size)
6368
{
6469
OTAHTTPStruct *h_odc;
6570

@@ -69,11 +74,16 @@ void *ofc_Init(const char *url)
6974
}
7075

7176
memset(h_odc, 0, sizeof(OTAHTTPStruct));
72-
77+
memset(sg_head_content, 0, OTA_HTTP_HEAD_CONTENT_LEN);
78+
HAL_Snprintf(sg_head_content, OTA_HTTP_HEAD_CONTENT_LEN,\
79+
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"\
80+
"Accept-Encoding: gzip, deflate\r\n"\
81+
"Range: bytes=%d-%d\r\n",
82+
offset, size);
83+
84+
Log_d("head_content:%s", sg_head_content);
7385
/* set http request-header parameter */
74-
h_odc->http.header = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" \
75-
"Accept-Encoding: gzip, deflate\r\n";
76-
86+
h_odc->http.header = sg_head_content;
7787
h_odc->url = url;
7888

7989
return h_odc;

src/sdk-impl/exports/qcloud_iot_export_ota.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,30 @@ void *IOT_OTA_Init(const char *product_id, const char *device_name, void *ch_sig
132132
int IOT_OTA_Destroy(void *handle);
133133

134134

135+
/**
136+
* @brief 开始ota固件下载
137+
* 根据获取到的http链接,本地固件信息偏移(是否断点续传),建立http连接
138+
*
139+
* @param handle: 指定OTA模块
140+
* @param offset: 固件下载偏移
141+
* @param size: 固件大小
142+
*
143+
* @retval 0 : 成功
144+
* @retval < 0 : 失败,返回具体错误码
145+
*/
146+
int IOT_OTA_StartDownload(void *handle, uint32_t offset, uint32_t size);
147+
148+
/**
149+
* @brief 更新MD5
150+
* 断点续传前,计算本地固件的MD5
151+
*
152+
* @param handle: 指定OTA模块
153+
* @param buff: 待计算的数据
154+
* @param size: 待计算的数据大小
155+
*
156+
*/
157+
void IOT_OTA_UpdateClientMd5(void *handle, char * buff, uint32_t size);
158+
135159
/**
136160
* @brief 向OTA服务器报告固件版本信息。
137161
* NOTE: 进行OTA前请保证先上报一次本地固件的版本信息,以便服务器获取到设备目前的固件信息

src/sdk-impl/qcloud_iot_export.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ extern "C" {
2424
#define QCLOUD_IOT_DEVICE_SDK_APPID "21010406"
2525

2626
/* IoT C-SDK version info */
27-
#define QCLOUD_IOT_DEVICE_SDK_VERSION "3.0.2"
27+
#define QCLOUD_IOT_DEVICE_SDK_VERSION "3.0.3"
2828

2929
/* MQTT心跳消息发送周期, 单位:ms */
3030
#define QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL (240 * 1000)

src/utils/farra/utils_httpc.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -424,7 +424,9 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
424424

425425
if (rc != QCLOUD_ERR_SUCCESS) {
426426
IOT_FUNC_EXIT_RC(rc);
427-
}
427+
}else if(0 == left_ms(&timer)){
428+
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
429+
}
428430

429431
if (len == 0) {
430432
/* read no more data */
@@ -530,6 +532,8 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
530532
rc = _http_client_recv(client, data, 1, max_len, &len, left_ms(&timer), client_data);
531533
if (rc != QCLOUD_ERR_SUCCESS) {
532534
IOT_FUNC_EXIT_RC(rc);
535+
}else if(0 == left_ms(&timer)){
536+
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
533537
}
534538
}
535539
} while (readLen);
@@ -540,7 +544,7 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
540544
/* Read missing chars to find end of chunk */
541545
rc = _http_client_recv(client, data + len, 2 - len, HTTP_CLIENT_CHUNK_SIZE - len - 1, &new_trf_len,
542546
left_ms(&timer), client_data);
543-
if (rc != QCLOUD_ERR_SUCCESS) {
547+
if ((rc != QCLOUD_ERR_SUCCESS )|| (0 == left_ms(&timer))) {
544548
IOT_FUNC_EXIT_RC(rc);
545549
}
546550
len += new_trf_len;
@@ -720,7 +724,9 @@ static int _http_client_recv_response(HTTPClient *client, uint32_t timeout_ms, H
720724

721725
if (rc != QCLOUD_ERR_SUCCESS) {
722726
IOT_FUNC_EXIT_RC(rc);
723-
}
727+
}else if(0 == left_ms(&timer)){
728+
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
729+
}
724730

725731
buf[reclen] = '\0';
726732

src/utils/lite/json_parser.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
2+
* Copyright (c) 2017-2019 Tencent Group. All rights reserved.
33
* License-Identifier: Apache-2.0
44
*
55
* Licensed under the Apache License, Version 2.0 (the "License"); you may

0 commit comments

Comments
 (0)