add new qloud-c-sdk component

This commit is contained in:
mculover666
2022-03-25 10:06:56 +08:00
parent 565cd29e94
commit a3ac2e56d8
166 changed files with 35027 additions and 0 deletions

View File

@@ -0,0 +1,3 @@
file(GLOB src_app ${CMAKE_CURRENT_SOURCE_DIR}/*.c)
add_executable(app_ota ${src_app})
target_link_libraries(app_ota ${libsdk})

View File

@@ -0,0 +1,247 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file broadcast_sample.c
* @brief
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-07-18
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-07-18 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "qcloud_iot_common.h"
#include "ota_downloader.h"
#include "utils_log.h"
/**
* @brief MQTT event callback, @see MQTTEventHandleFun
*
* @param[in] client pointer to mqtt client
* @param[in] handle_context context
* @param[in] msg msg
*/
static void _mqtt_event_handler(void *client, void *handle_context, MQTTEventMsg *msg)
{
MQTTMessage *mqtt_message = (MQTTMessage *)msg->msg;
uintptr_t packet_id = (uintptr_t)msg->msg;
switch (msg->event_type) {
case MQTT_EVENT_UNDEF:
Log_i("undefined event occur.");
break;
case MQTT_EVENT_DISCONNECT:
Log_i("MQTT disconnect.");
break;
case MQTT_EVENT_RECONNECT:
Log_i("MQTT reconnect.");
break;
case MQTT_EVENT_PUBLISH_RECEIVED:
Log_i("topic message arrived but without any related handle: topic=%.*s, topic_msg=%.*s",
mqtt_message->topic_len, STRING_PTR_PRINT_SANITY_CHECK(mqtt_message->topic_name),
mqtt_message->payload_len, STRING_PTR_PRINT_SANITY_CHECK(mqtt_message->payload_str));
break;
case MQTT_EVENT_SUBSCRIBE_SUCCESS:
Log_i("subscribe success, packet-id=%u", (unsigned int)packet_id);
break;
case MQTT_EVENT_SUBSCRIBE_TIMEOUT:
Log_i("subscribe wait ack timeout, packet-id=%u", (unsigned int)packet_id);
break;
case MQTT_EVENT_SUBSCRIBE_NACK:
Log_i("subscribe nack, packet-id=%u", (unsigned int)packet_id);
break;
case MQTT_EVENT_UNSUBSCRIBE_SUCCESS:
Log_i("unsubscribe success, packet-id=%u", (unsigned int)packet_id);
break;
case MQTT_EVENT_UNSUBSCRIBE_TIMEOUT:
Log_i("unsubscribe timeout, packet-id=%u", (unsigned int)packet_id);
break;
case MQTT_EVENT_UNSUBSCRIBE_NACK:
Log_i("unsubscribe nack, packet-id=%u", (unsigned int)packet_id);
break;
case MQTT_EVENT_PUBLISH_SUCCESS:
Log_i("publish success, packet-id=%u", (unsigned int)packet_id);
break;
case MQTT_EVENT_PUBLISH_TIMEOUT:
Log_i("publish timeout, packet-id=%u", (unsigned int)packet_id);
break;
case MQTT_EVENT_PUBLISH_NACK:
Log_i("publish nack, packet-id=%u", (unsigned int)packet_id);
break;
default:
Log_i("Should NOT arrive here.");
break;
}
}
/**
* @brief Setup MQTT construct parameters.
*
* @param[in,out] initParams @see MQTTInitParams
* @param[in] device_info @see DeviceInfo
*/
static void _setup_connect_init_params(MQTTInitParams *init_params, DeviceInfo *device_info)
{
init_params->device_info = device_info;
init_params->command_timeout = QCLOUD_IOT_MQTT_COMMAND_TIMEOUT;
init_params->keep_alive_interval_ms = QCLOUD_IOT_MQTT_KEEP_ALIVE_INTERNAL;
init_params->auto_connect_enable = 1;
init_params->event_handle.h_fp = _mqtt_event_handler;
init_params->event_handle.context = NULL;
}
// ----------------------------------------------------------------------------
// OTA callback
// ----------------------------------------------------------------------------
void _update_firmware_callback(UtilsJsonValue version, UtilsJsonValue url, UtilsJsonValue md5sum, uint32_t file_size,
void *usr_data)
{
Log_i("recv firmware: version=%.*s|url=%.*s|md5sum=%.*s|file_size=%u", version.value_len, version.value,
url.value_len, url.value, md5sum.value_len, md5sum.value, file_size);
// only one firmware one time is supportted now
OTAFirmwareInfo firmware_info;
memset(&firmware_info, 0, sizeof(OTAFirmwareInfo));
strncpy(firmware_info.version, version.value, version.value_len);
strncpy(firmware_info.md5sum, md5sum.value, md5sum.value_len);
firmware_info.file_size = file_size;
ota_downloader_info_set(&firmware_info, url.value, url.value_len);
}
// ----------------------------------------------------------------------------
// Main
// ----------------------------------------------------------------------------
static int sg_main_exit = 0;
#ifdef __linux__
#include <signal.h>
#include <pthread.h>
#include <unistd.h>
static void _main_exit(int sig)
{
Log_e("demo exit by signal:%d\n", sig);
sg_main_exit = 1;
}
#endif
int main(int argc, char **argv)
{
#ifdef __linux__
signal(SIGINT, _main_exit);
#endif
int rc;
char buf[1024];
// init log level
LogHandleFunc func = {0};
func.log_malloc = HAL_Malloc;
func.log_free = HAL_Free;
func.log_get_current_time_str = HAL_Timer_Current;
func.log_printf = HAL_Printf;
utils_log_init(func, LOG_LEVEL_DEBUG, 2048);
DeviceInfo device_info;
rc = HAL_GetDevInfo((void *)&device_info);
if (rc) {
Log_e("get device info failed: %d", rc);
return rc;
}
// init connection
MQTTInitParams init_params = DEFAULT_MQTT_INIT_PARAMS;
_setup_connect_init_params(&init_params, &device_info);
// create MQTT client and connect with server
void *client = IOT_MQTT_Construct(&init_params);
if (client) {
Log_i("Cloud Device Construct Success");
} else {
Log_e("MQTT Construct failed!");
return QCLOUD_ERR_FAILURE;
}
// OTA init
IotOTAUpdateCallback ota_callback = {
.update_firmware_callback = _update_firmware_callback,
.report_version_reply_callback = NULL,
};
rc = IOT_OTA_Init(client, ota_callback, NULL);
if (rc) {
Log_e("OTA init failed!, rc=%d", rc);
return rc;
}
rc = IOT_OTA_ReportVersion(client, buf, sizeof(buf), QCLOUD_IOT_DEVICE_SDK_VERSION);
if (rc) {
Log_e("OTA report version failed!, rc=%d", rc);
return rc;
}
rc = ota_downloader_init(client);
if (rc) {
Log_e("OTA downloader init failed!, rc=%d", rc);
return rc;
}
do {
ota_downloader_process();
rc = IOT_MQTT_Yield(client, QCLOUD_IOT_MQTT_YIELD_TIMEOUT);
switch (rc) {
case QCLOUD_RET_SUCCESS:
break;
case QCLOUD_ERR_MQTT_ATTEMPTING_RECONNECT:
continue;
case QCLOUD_RET_MQTT_RECONNECTED:
break;
default:
Log_e("Exit loop caused of errCode:%d", rc);
goto exit;
}
} while (!sg_main_exit);
exit:
ota_downloader_deinit();
IOT_OTA_Deinit(client);
rc = IOT_MQTT_Destroy(&client);
utils_log_deinit();
return rc;
}

View File

@@ -0,0 +1,400 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file ota_downloader.c
* @brief
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-10-20
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-10-20 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#include "ota_downloader.h"
#include "ota_firmware_save.h"
#include "utils_log.h"
#include "utils_md5.h"
#define OTA_HTTP_TIMEOUT_MS 5000
#define OTA_HTTP_BUF_SIZE 1024
#define MAX_SIZE_OF_DOWNLOAD_URL 512
/**
* @brief Break point info.
*
*/
typedef struct {
OTAFirmwareInfo file_id;
uint32_t downloaded_size;
} OTADownloadInfo;
/**
* @brief OTA downloader handle.
*
*/
typedef struct {
void* cos_download;
void* mqtt_client;
void* downloader;
OTAFirmwareInfo download_now;
OTADownloadInfo break_point;
char download_url[MAX_SIZE_OF_DOWNLOAD_URL];
uint8_t download_buff[OTA_HTTP_BUF_SIZE];
uint32_t download_size;
IotMd5Context download_md5_ctx;
OTADownloaderStatus status;
} OTADownloaderHandle;
/**
* @brief Handle.
*
*/
static OTADownloaderHandle sg_ota_downloader_handle = {0};
// ----------------------------------------------------------------------------
// Downloader function
// ----------------------------------------------------------------------------
// break point function
/**
* @brief Read break point from file.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_break_point_init(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
sg_ota_downloader_handle.cos_download = NULL;
utils_md5_reset(&handle->download_md5_ctx);
return ota_break_point_read((uint8_t*)&handle->break_point, sizeof(handle->break_point)) < 0;
}
/**
* @brief Memset break point.
*
* @param[in,out] usr_data @see OTADownloaderHandle
*/
static void _ota_break_point_deinit(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
memset(&handle->break_point, 0, sizeof(handle->break_point));
}
/**
* @brief Set break point using download now info.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_break_point_set(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
// read break point info from flash
memset(&handle->break_point, 0, sizeof(handle->break_point));
memcpy(&handle->break_point.file_id, &handle->download_now, sizeof(handle->break_point.file_id));
return 0;
}
/**
* @brief Update break point and save.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_break_point_save(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
// update md5 sum & download_size
handle->break_point.downloaded_size += handle->download_size;
utils_md5_update(&handle->download_md5_ctx, handle->download_buff, handle->download_size);
// report progress
char buf[256];
int buf_len = sizeof(buf);
IOT_OTA_ReportProgress(handle->mqtt_client, buf, buf_len, IOT_OTA_REPORT_TYPE_DOWNLOADING,
handle->break_point.downloaded_size * 100 / handle->download_now.file_size,
handle->break_point.file_id.version);
// write to local only write downloaded size is ok
return ota_break_point_write((uint8_t*)&handle->break_point, sizeof(OTADownloadInfo));
}
/**
* @brief Check if break point matches download now info.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_break_point_check(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
// version should be the same, download size should not bigger than file size
Log_d("download now:%s,%d,%s", handle->download_now.version, handle->download_now.file_size,
handle->download_now.md5sum);
Log_d("break point:%s,%d,%d,%s", handle->break_point.file_id.version, handle->break_point.file_id.file_size,
handle->break_point.downloaded_size, handle->break_point.file_id.md5sum);
return strncmp(handle->break_point.file_id.version, handle->download_now.version, MAX_SIZE_OF_FW_VERSION) ||
handle->break_point.file_id.file_size != handle->download_now.file_size ||
handle->break_point.downloaded_size > handle->download_now.file_size ||
strncmp(handle->break_point.file_id.md5sum, handle->download_now.md5sum, 32);
}
/**
* @brief Calculate md5 sum according break point.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_break_point_restore(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
// update md5 according downloaded data
size_t rlen, total_read = 0, size = 0;
size = handle->break_point.downloaded_size;
while (size > 0) {
rlen = (size > OTA_HTTP_BUF_SIZE) ? OTA_HTTP_BUF_SIZE : size;
if (ota_firmware_read(handle->download_buff, rlen, total_read) < 0) {
Log_e("read data failed");
handle->break_point.downloaded_size = 0;
break;
}
utils_md5_update(&handle->download_md5_ctx, handle->download_buff, rlen);
size -= rlen;
total_read += rlen;
}
return 0;
}
// data download function
/**
* @brief Init cos downloader.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_data_download_init(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
IotCosDownloadParams params = {
.url = handle->download_url,
.offset = handle->break_point.downloaded_size,
.file_size = handle->break_point.file_id.file_size,
.is_fragmentation = false,
.is_https_enabled = false,
};
handle->cos_download = IOT_COS_DownloadInit(&params);
return handle->cos_download ? 0 : -1;
}
/**
* @brief Deinit cos downloader.
*
* @param[in,out] usr_data @see OTADownloaderHandle
*/
static void _ota_data_download_deinit(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
IOT_COS_DownloadDeinit(handle->cos_download);
}
/**
* @brief Check if download finished.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_data_download_is_over(void* usr_data)
{
// check download is over
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
return handle->break_point.downloaded_size == handle->download_now.file_size;
}
/**
* @brief Download from cos server.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_data_download_recv(void* usr_data)
{
// download data using http
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
// TODO: https download
handle->download_size =
IOT_COS_DownloadFetch(handle->cos_download, handle->download_buff, OTA_HTTP_BUF_SIZE, OTA_HTTP_TIMEOUT_MS);
return handle->download_size;
}
/**
* @brief Sava firmware to file.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @return 0 for success
*/
static int _ota_data_download_save(void* usr_data)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
return handle->download_size > 0
? ota_firmware_write(handle->download_buff, handle->download_size, handle->break_point.downloaded_size)
: 0;
}
/**
* @brief Process download result.
*
* @param[in,out] usr_data @see OTADownloaderHandle
* @param[in] status status when finish download, @see UtilsDownloaderStatus
* @return 0 for success
*/
static int _ota_data_download_finish(void* usr_data, UtilsDownloaderStatus status)
{
OTADownloaderHandle* handle = (OTADownloaderHandle*)usr_data;
int rc = 0;
char buf[256];
int buf_len = sizeof(buf);
switch (status) {
case UTILS_DOWNLOADER_STATUS_SUCCESS:
utils_md5_finish(&handle->download_md5_ctx);
ota_firmware_finish(handle->download_now.file_size);
int valid = !utils_md5_compare(&handle->download_md5_ctx, handle->download_now.md5sum);
if (!valid) {
memset(&handle->break_point, 0, sizeof(handle->break_point));
ota_break_point_write((uint8_t*)&handle->break_point, sizeof(handle->break_point));
}
rc = valid ? IOT_OTA_ReportProgress(handle->mqtt_client, buf, buf_len, IOT_OTA_REPORT_TYPE_UPGRADE_SUCCESS,
0, handle->download_now.version)
: IOT_OTA_ReportProgress(handle->mqtt_client, buf, buf_len, IOT_OTA_REPORT_TYPE_MD5_NOT_MATCH, 0,
handle->download_now.version);
break;
case UTILS_DOWNLOADER_STATUS_NETWORK_FAILED:
rc = IOT_OTA_ReportProgress(handle->mqtt_client, buf, buf_len, IOT_OTA_REPORT_TYPE_DOWNLOAD_TIMEOUT, 0,
handle->download_now.version);
break;
case UTILS_DOWNLOADER_STATUS_BREAK_POINT_FAILED:
case UTILS_DOWNLOADER_STATUS_DATA_DOWNLOAD_FAILED:
rc = IOT_OTA_ReportProgress(handle->mqtt_client, buf, buf_len, IOT_OTA_REPORT_TYPE_UPGRADE_FAIL, 0,
handle->download_now.version);
break;
default:
break;
}
handle->status = OTA_DOWNLOADER_STATUS_FINISHED;
return rc;
}
// ----------------------------------------------------------------------------
// API
// ----------------------------------------------------------------------------
/**
* @brief Init ota downloader.
*
* @param[in,out] client pointer to mqtt client
* @return 0 for success.
*/
int ota_downloader_init(void* client)
{
// downloader init
UtilsDownloaderFunction ota_callback = {
.downloader_malloc = HAL_Malloc,
.downloader_free = HAL_Free,
// break point
.break_point_init = _ota_break_point_init,
.break_point_deinit = _ota_break_point_deinit,
.break_point_set = _ota_break_point_set,
.break_point_save = _ota_break_point_save,
.break_point_check = _ota_break_point_check,
.break_point_restore = _ota_break_point_restore,
// data download
.data_download_init = _ota_data_download_init,
.data_download_deinit = _ota_data_download_deinit,
.data_download_is_over = _ota_data_download_is_over,
.data_download_recv = _ota_data_download_recv,
.data_download_save = _ota_data_download_save,
.data_download_finish = _ota_data_download_finish,
};
sg_ota_downloader_handle.downloader = utils_downloader_init(ota_callback, &sg_ota_downloader_handle);
if (!sg_ota_downloader_handle.downloader) {
Log_e("initialize downloaded failed");
return -1;
}
sg_ota_downloader_handle.mqtt_client = client;
sg_ota_downloader_handle.status = OTA_DOWNLOADER_STATUS_INITTED;
return 0;
}
/**
* @brief Set download info of ota firmware.
*
* @param[in] firmware_info pointer to firmware info
* @param[in] url url of cos download
* @param[in] url_len download length
*/
void ota_downloader_info_set(OTAFirmwareInfo* firmware_info, const char* url, int url_len)
{
if (OTA_DOWNLOADER_STATUS_DOWNLOADING != sg_ota_downloader_handle.status) {
memcpy(&sg_ota_downloader_handle.download_now, firmware_info, sizeof(OTAFirmwareInfo));
strncpy(sg_ota_downloader_handle.download_url, url, url_len);
sg_ota_downloader_handle.download_url[url_len] = '\0';
sg_ota_downloader_handle.status = OTA_DOWNLOADER_STATUS_DOWNLOADING;
}
}
/**
* @brief Process ota download.
*
* @return @see OTADownloaderStatus
*/
OTADownloaderStatus ota_downloader_process(void)
{
if (OTA_DOWNLOADER_STATUS_DOWNLOADING == sg_ota_downloader_handle.status) {
utils_downloader_process(sg_ota_downloader_handle.downloader);
}
return sg_ota_downloader_handle.status;
}
/**
* @brief Deinit ota downloader.
*
*/
void ota_downloader_deinit(void)
{
utils_downloader_deinit(sg_ota_downloader_handle.downloader);
memset(&sg_ota_downloader_handle, 0, sizeof(sg_ota_downloader_handle));
}

View File

@@ -0,0 +1,102 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file ota_downloader.h
* @brief
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-10-20
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-10-20 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#ifndef IOT_HUB_DEVICE_C_SDK_APP_OTA_OTA_DOWNLOADER_H_
#define IOT_HUB_DEVICE_C_SDK_APP_OTA_OTA_DOWNLOADER_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "qcloud_iot_common.h"
#include "utils_downloader.h"
/**
* @brief Firmware version size.
*
*/
#define MAX_SIZE_OF_FW_VERSION 32
/**
* @brief OTA firmware info.
*
*/
typedef struct {
char version[MAX_SIZE_OF_FW_VERSION + 4];
uint32_t file_size;
char md5sum[33];
} OTAFirmwareInfo;
/**
* @brief Downloader status.
*
*/
typedef enum {
OTA_DOWNLOADER_STATUS_NO_INITTED = 0,
OTA_DOWNLOADER_STATUS_INITTED,
OTA_DOWNLOADER_STATUS_DOWNLOADING,
OTA_DOWNLOADER_STATUS_FINISHED,
} OTADownloaderStatus;
/**
* @brief Init ota downloader.
*
* @param[in,out] client pointer to mqtt client
* @return 0 for success.
*/
int ota_downloader_init(void* client);
/**
* @brief Set download info of ota firmware.
*
* @param[in] firmware_info pointer to firmware info
* @param[in] url url of cos download
* @param[in] url_len download length
*/
void ota_downloader_info_set(OTAFirmwareInfo* firmware_info, const char* url, int url_len);
/**
* @brief Process ota download.
*
* @return @see OTADownloaderStatus
*/
OTADownloaderStatus ota_downloader_process(void);
/**
* @brief Deinit ota downloader.
*
*/
void ota_downloader_deinit(void);
#ifdef __cplusplus
}
#endif
#endif // IOT_HUB_DEVICE_C_SDK_APP_OTA_OTA_DOWNLOADER_H_

View File

@@ -0,0 +1,141 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file ota_firmware_save.c
* @brief
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-10-20
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-10-20 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#include "ota_firmware_save.h"
#include "utils_log.h"
#define OTA_FILE_PATH "./app_ota_fw.bin"
#define OTA_BREAK_POINT_FILE_PATH "./break_point.dat"
/**
* @brief Read ota break point from file.
*
* @param[out] buf data to read
* @param[in] buf_len data buffer len
* @return > 0 for read len, -1 for fail
*/
int ota_break_point_read(uint8_t *buf, uint32_t buf_len)
{
FILE *fp = fopen(OTA_BREAK_POINT_FILE_PATH, "rb");
if (!fp) {
fp = fopen(OTA_BREAK_POINT_FILE_PATH, "wb+");
if (!fp) {
Log_e("create file failed");
return -1;
}
}
int ret = fread(buf, 1, buf_len, fp);
fclose(fp);
return ret;
}
/**
* @brief Write ota break point to file.
*
* @param[in] data break point data to write
* @param[in] data_len data length
* @return 0 for success
*/
int ota_break_point_write(const uint8_t *data, uint32_t data_len)
{
FILE *fp = fopen(OTA_BREAK_POINT_FILE_PATH, "wb+");
if (!fp) {
Log_e("open file failed");
return -1;
}
fwrite(data, 1, data_len, fp);
fclose(fp);
return 0;
}
/**
* @brief Read firmware from file.
*
* @param[out] buf data to read
* @param[in] buf_len data buffer len
* @param[in] offset offset of file
* @return > 0 for read len, -1 for fail
*/
int ota_firmware_read(uint8_t *buf, uint32_t buf_len, uint32_t offset)
{
int ret = -1;
FILE *fp = fopen(OTA_FILE_PATH, "rb");
if (!fp) {
fp = fopen(OTA_FILE_PATH, "wb+");
if (!fp) {
Log_e("create file failed");
return -1;
}
}
ret = fseek(fp, offset, SEEK_SET);
if (!ret) {
ret = fread(buf, 1, buf_len, fp);
}
fclose(fp);
return ret;
}
/**
* @brief Write firmware to file.
*
* @param[in] data firmware data to write
* @param[in] data_len data length
* @param[in] offset offset of file
* @return 0 for success
*/
int ota_firmware_write(uint8_t *data, uint32_t data_len, uint32_t offset)
{
int ret = -1;
FILE *fp = fopen(OTA_FILE_PATH, "rb+");
if (!fp) {
fp = fopen(OTA_FILE_PATH, "wb+");
if (!fp) {
Log_e("create file failed");
return -1;
}
}
ret = fseek(fp, offset, SEEK_SET);
if (!ret) {
fwrite(data, 1, data_len, fp);
}
fclose(fp);
return 0;
}
/**
* @brief Finish write firmware.
*
* @param[in] total_len total length of firmware
* @return 0 for success
*/
int ota_firmware_finish(uint32_t total_len)
{
return 0;
}

View File

@@ -0,0 +1,89 @@
/**
* @copyright
*
* Tencent is pleased to support the open source community by making IoT Hub available.
* Copyright(C) 2018 - 2021 THL A29 Limited, a Tencent company.All rights reserved.
*
* Licensed under the MIT License(the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is
* distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific language governing permissions and
* limitations under the License.
*
* @file ota_firmware_save.h
* @brief
* @author fancyxu (fancyxu@tencent.com)
* @version 1.0
* @date 2021-10-20
*
* @par Change Log:
* <table>
* <tr><th>Date <th>Version <th>Author <th>Description
* <tr><td>2021-10-20 <td>1.0 <td>fancyxu <td>first commit
* </table>
*/
#ifndef IOT_HUB_DEVICE_C_SDK_APP_OTA_OTA_FIRMWARE_SAVE_H_
#define IOT_HUB_DEVICE_C_SDK_APP_OTA_OTA_FIRMWARE_SAVE_H_
#ifdef __cplusplus
extern "C" {
#endif
#include <stdio.h>
#include <stdint.h>
/**
* @brief Read ota break point from file.
*
* @param[out] buf data to read
* @param[in] buf_len data buffer len
* @return > 0 for read len, -1 for fail
*/
int ota_break_point_read(uint8_t *buf, uint32_t buf_len);
/**
* @brief Write ota break point to file.
*
* @param[in] data break point data to write
* @param[in] data_len data length
* @return 0 for success
*/
int ota_break_point_write(const uint8_t *data, uint32_t data_len);
/**
* @brief Read firmware from file.
*
* @param[out] buf data to read
* @param[in] buf_len data buffer len
* @param[in] offset offset of file
* @return > 0 for read len, -1 for fail
*/
int ota_firmware_read(uint8_t *buf, uint32_t buf_len, uint32_t offset);
/**
* @brief Write firmware to file.
*
* @param[in] data firmware data to write
* @param[in] data_len data length
* @param[in] offset offset of file
* @return 0 for success
*/
int ota_firmware_write(uint8_t *data, uint32_t data_len, uint32_t offset);
/**
* @brief Finish write firmware.
*
* @param[in] total_len total length of firmware
* @return 0 for success
*/
int ota_firmware_finish(uint32_t total_len);
#ifdef __cplusplus
}
#endif
#endif // IOT_HUB_DEVICE_C_SDK_APP_OTA_OTA_FIRMWARE_SAVE_H_