Skip to content

Commit

Permalink
1. OTA支持断点续传。
Browse files Browse the repository at this point in the history
2. SDK版本号更新为3.0.3。
  • Loading branch information
yougaliu committed Aug 9, 2019
1 parent 50e1926 commit ef3263b
Show file tree
Hide file tree
Showing 15 changed files with 415 additions and 87 deletions.
358 changes: 302 additions & 56 deletions samples/ota/ota_mqtt_sample.c

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion samples/samples.mk
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ ifneq (,$(filter -DOTA_COMM_ENABLED,$(CFLAGS)))
ifneq (,$(filter -DOTA_MQTT_CHANNEL,$(CFLAGS)))
ota_mqtt_sample:
$(eval CFLAGS := $(filter-out $(IOTSDK_INCLUDE_FILES),$(CFLAGS)) \
-I$(TOP_DIR)/src/sdk-impl -I$(TOP_DIR)/src/sdk-impl/exports)
-I$(TOP_DIR)/src/sdk-impl -I$(TOP_DIR)/src/sdk-impl/exports -I$(TOP_DIR)/src/utils/lite)

$(TOP_Q) \
$(PLATFORM_CC) $(CFLAGS) $(SAMPLE_DIR)/ota/[email protected] $(LDFLAGS) -o $@
Expand Down
1 change: 1 addition & 0 deletions src/mqtt/src/mqtt_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ int qcloud_iot_mqtt_init(Qcloud_IoT_Client *pClient, MQTTInitParams *pParams) {
POINTER_SANITY_CHECK(pParams, QCLOUD_ERR_INVAL);

memset(pClient, 0x0, sizeof(Qcloud_IoT_Client));


int size = HAL_Snprintf(s_qcloud_iot_host, HOST_STR_LENGTH, "%s.%s", pParams->product_id, QCLOUD_IOT_MQTT_DIRECT_DOMAIN);
if (size < 0 || size > HOST_STR_LENGTH - 1) {
Expand Down
3 changes: 2 additions & 1 deletion src/ota/include/ota_fetch.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ extern "C" {

#include <stdint.h>

void *ofc_Init(const char *url);

void *ofc_Init(const char *url, uint32_t offset, uint32_t size);

int32_t qcloud_ofc_connect(void *handle);

Expand Down
66 changes: 53 additions & 13 deletions src/ota/src/ota_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,17 +115,6 @@ static void _ota_callback(void *pcontext, const char *msg, uint32_t msg_len) {
goto End;
}

if (NULL == (h_ota->ch_fetch = ofc_Init(h_ota->purl))) {
Log_e("Initialize fetch module failed");
goto End;
}

if (0 != qcloud_ofc_connect(h_ota->ch_fetch)) {
Log_e("Connect fetch module failed");
h_ota->state = IOT_OTAS_DISCONNECTED;
goto End;
}

h_ota->state = IOT_OTAS_FETCHING;
}

Expand Down Expand Up @@ -337,6 +326,38 @@ int IOT_OTA_Destroy(void *handle)
return 0;
}

/*support continuous transmission of breakpoints*/
int IOT_OTA_StartDownload(void *handle, uint32_t offset, uint32_t size)
{
OTA_Struct_t *h_ota = (OTA_Struct_t *) handle;
int Ret;

h_ota->size_fetched += offset;
h_ota->ch_fetch = ofc_Init(h_ota->purl, offset, size);
if (NULL == h_ota->ch_fetch) {
Log_e("Initialize fetch module failed");
return QCLOUD_ERR_FAILURE;
}

Ret = qcloud_ofc_connect(h_ota->ch_fetch);
if (QCLOUD_ERR_SUCCESS != Ret) {
Log_e("Connect fetch module failed");
h_ota->state = IOT_OTAS_DISCONNECTED;
}

return Ret;
}

/*support continuous transmission of breakpoints*/
void IOT_OTA_UpdateClientMd5(void *handle, char * buff, uint32_t size)
{
OTA_Struct_t *h_ota = (OTA_Struct_t *) handle;

qcloud_otalib_md5_update(h_ota->md5, buff, size);
}



int IOT_OTA_ReportVersion(void *handle, const char *version)
{
#define MSG_INFORM_LEN (128)
Expand Down Expand Up @@ -400,12 +421,31 @@ int IOT_OTA_ReportUpgradeBegin(void *handle)

int IOT_OTA_ReportUpgradeSuccess(void *handle, const char *version)
{
return IOT_OTA_ReportUpgradeResult(handle, version, IOT_OTAR_UPGRADE_SUCCESS);
OTA_Struct_t *h_ota = (OTA_Struct_t *) handle;
int ret;

if(NULL == version){
ret = IOT_OTA_ReportUpgradeResult(handle, h_ota->version, IOT_OTAR_UPGRADE_SUCCESS);
}else{
ret = IOT_OTA_ReportUpgradeResult(handle, version, IOT_OTAR_UPGRADE_SUCCESS);
}

return ret;
}


int IOT_OTA_ReportUpgradeFail(void *handle, const char *version)
{
return IOT_OTA_ReportUpgradeResult(handle, version, IOT_OTAR_UPGRADE_FAIL);
OTA_Struct_t *h_ota = (OTA_Struct_t *) handle;
int ret;

if(NULL == version){
ret = IOT_OTA_ReportUpgradeResult(handle, h_ota->version, IOT_OTAR_UPGRADE_FAIL);
}else{
ret = IOT_OTA_ReportUpgradeResult(handle, version, IOT_OTAR_UPGRADE_FAIL);
}

return ret;
}


Expand Down
22 changes: 16 additions & 6 deletions src/ota/src/ota_fetch.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,12 @@ extern "C" {
#include "qcloud_iot_import.h"

#include "ca.h"

#include "utils_httpc.h"



#define OTA_HTTP_HEAD_CONTENT_LEN 256

/* ofc, OTA fetch channel */

typedef struct {
Expand Down Expand Up @@ -59,7 +62,9 @@ static int is_begin_with(const char * str1,char *str2)
return 1;
}

void *ofc_Init(const char *url)

static char sg_head_content[OTA_HTTP_HEAD_CONTENT_LEN];
void *ofc_Init(const char *url, uint32_t offset, uint32_t size)
{
OTAHTTPStruct *h_odc;

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

memset(h_odc, 0, sizeof(OTAHTTPStruct));

memset(sg_head_content, 0, OTA_HTTP_HEAD_CONTENT_LEN);
HAL_Snprintf(sg_head_content, OTA_HTTP_HEAD_CONTENT_LEN,\
"Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"\
"Accept-Encoding: gzip, deflate\r\n"\
"Range: bytes=%d-%d\r\n",
offset, size);

Log_d("head_content:%s", sg_head_content);
/* set http request-header parameter */
h_odc->http.header = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n" \
"Accept-Encoding: gzip, deflate\r\n";

h_odc->http.header = sg_head_content;
h_odc->url = url;

return h_odc;
Expand Down
24 changes: 24 additions & 0 deletions src/sdk-impl/exports/qcloud_iot_export_ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,30 @@ void *IOT_OTA_Init(const char *product_id, const char *device_name, void *ch_sig
int IOT_OTA_Destroy(void *handle);


/**
* @brief 开始ota固件下载
* 根据获取到的http链接,本地固件信息偏移(是否断点续传),建立http连接
*
* @param handle: 指定OTA模块
* @param offset: 固件下载偏移
* @param size: 固件大小
*
* @retval 0 : 成功
* @retval < 0 : 失败,返回具体错误码
*/
int IOT_OTA_StartDownload(void *handle, uint32_t offset, uint32_t size);

/**
* @brief 更新MD5
* 断点续传前,计算本地固件的MD5
*
* @param handle: 指定OTA模块
* @param buff: 待计算的数据
* @param size: 待计算的数据大小
*
*/
void IOT_OTA_UpdateClientMd5(void *handle, char * buff, uint32_t size);

/**
* @brief 向OTA服务器报告固件版本信息。
* NOTE: 进行OTA前请保证先上报一次本地固件的版本信息,以便服务器获取到设备目前的固件信息
Expand Down
2 changes: 1 addition & 1 deletion src/sdk-impl/qcloud_iot_export.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ extern "C" {
#define QCLOUD_IOT_DEVICE_SDK_APPID "21010406"

/* IoT C-SDK version info */
#define QCLOUD_IOT_DEVICE_SDK_VERSION "3.0.2"
#define QCLOUD_IOT_DEVICE_SDK_VERSION "3.0.3"

/* MQTT心跳消息发送周期, 单位:ms */
#define QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL (240 * 1000)
Expand Down
12 changes: 9 additions & 3 deletions src/utils/farra/utils_httpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,9 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len

if (rc != QCLOUD_ERR_SUCCESS) {
IOT_FUNC_EXIT_RC(rc);
}
}else if(0 == left_ms(&timer)){
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
}

if (len == 0) {
/* read no more data */
Expand Down Expand Up @@ -530,6 +532,8 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
rc = _http_client_recv(client, data, 1, max_len, &len, left_ms(&timer), client_data);
if (rc != QCLOUD_ERR_SUCCESS) {
IOT_FUNC_EXIT_RC(rc);
}else if(0 == left_ms(&timer)){
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
}
}
} while (readLen);
Expand All @@ -540,7 +544,7 @@ static int _http_client_retrieve_content(HTTPClient *client, char *data, int len
/* Read missing chars to find end of chunk */
rc = _http_client_recv(client, data + len, 2 - len, HTTP_CLIENT_CHUNK_SIZE - len - 1, &new_trf_len,
left_ms(&timer), client_data);
if (rc != QCLOUD_ERR_SUCCESS) {
if ((rc != QCLOUD_ERR_SUCCESS )|| (0 == left_ms(&timer))) {
IOT_FUNC_EXIT_RC(rc);
}
len += new_trf_len;
Expand Down Expand Up @@ -720,7 +724,9 @@ static int _http_client_recv_response(HTTPClient *client, uint32_t timeout_ms, H

if (rc != QCLOUD_ERR_SUCCESS) {
IOT_FUNC_EXIT_RC(rc);
}
}else if(0 == left_ms(&timer)){
IOT_FUNC_EXIT_RC(QCLOUD_ERR_HTTP_TIMEOUT);
}

buf[reclen] = '\0';

Expand Down
2 changes: 1 addition & 1 deletion src/utils/lite/json_parser.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
* Copyright (c) 2017-2019 Tencent Group. All rights reserved.
* License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lite/json_parser.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
* Copyright (c) 2017-2019 Tencent Group. All rights reserved.
* License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lite/json_token.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
* Copyright (c) 2017-2019 Tencent Group. All rights reserved.
* License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lite/lite-list.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
* Copyright (c) 2017-2019 Tencent Group. All rights reserved.
* License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lite/lite-utils.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
* Copyright (c) 2017-2019 Tencent Group. All rights reserved.
* License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand Down
2 changes: 1 addition & 1 deletion src/utils/lite/string_utils.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2014-2016 Alibaba Group. All rights reserved.
* Copyright (c) 2017-2019 Tencent Group. All rights reserved.
* License-Identifier: Apache-2.0
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
Expand Down

0 comments on commit ef3263b

Please sign in to comment.