Merge pull request #48 from ucloud/feature_unify_download_api

unify download api
This commit is contained in:
ethanDu1 2020-08-07 17:41:20 +08:00 committed by GitHub
commit 09478f954a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 383 additions and 49 deletions

View File

@ -0,0 +1,120 @@
/*
* Copyright (C) 2012-2019 UCloud. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/
#include "uiot_import.h"
uint32_t download_addr = DOWNLOAD_START_ADDR;
void * HAL_Download_Init(_IN_ void * name)
{
int i,ret;
char version_info[VERSION_BYTE_NUM];
uint32_t info_addr = CURRENT_VERSION_START_ADDR;
for(i=0; i < VERSION_BYTE_NUM; i++)
{
version_info[i] = HAL_FLASH_Read_Byte(info_addr);
info_addr++;
}
if(FAILURE_RET == HAL_FLASH_Erase_Sector(FLASH_SECTOR_3, 1))
return NULL;
info_addr = CURRENT_VERSION_START_ADDR;
for(i = 0; i < VERSION_BYTE_NUM; i++)
{
ret = HAL_FLASH_Write_Byte(info_addr++,version_info[i]);
if(SUCCESS_RET != ret)
return NULL;
}
ret = HAL_FLASH_Write_Byte(DOWNLOAD_FINISH_FLG_ADDR,DOWNLOAD_FAILED);
if(SUCCESS_RET != ret)
return NULL;
ret = HAL_FLASH_Erase_Sector(FLASH_SECTOR_8, 4);
if(SUCCESS_RET != ret)
return NULL;
download_addr = DOWNLOAD_START_ADDR;
return &download_addr;
}
int HAL_Download_Write(_IN_ void * handle,_IN_ uint32_t total_length,_IN_ uint8_t *buffer_read,_IN_ uint32_t length)
{
uint8_t *readptr = buffer_read;
int i = 0,ret;
for(i = length; i > 0; i--){
ret = HAL_FLASH_Write_Byte(download_addr++, *readptr++);
if(ret == FAILURE_RET){
printf("HAL_Download_Write failed!\r\n");
return FAILURE_RET;
}
}
return SUCCESS_RET;
}
int HAL_Download_End(_IN_ void * handle)
{
HAL_FLASH_Write_Byte(DOWNLOAD_FINISH_FLG_ADDR,DOWNLOAD_SUCESS);
//reset here
__set_FAULTMASK(1);
HAL_NVIC_SystemReset();
return SUCCESS_RET;
}
int HAL_FLASH_Write_Byte(_IN_ uint32_t sddr,_IN_ uint32_t data)
{
HAL_StatusTypeDef ret;
ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, sddr, data);
if(ret == HAL_ERROR){
printf("write data byte to flash failed!\r\n");
return FAILURE_RET;
}
else
return SUCCESS_RET;
}
int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ int sector_num)
{
uint32_t flash_error;
HAL_StatusTypeDef ret;
FLASH_EraseInitTypeDef flash_erase;
flash_erase.TypeErase = FLASH_TYPEERASE_SECTORS;
flash_erase.Sector = sector;
flash_erase.NbSectors = sector_num;
flash_erase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR | FLASH_FLAG_BSY);
ret = HAL_FLASHEx_Erase(&flash_erase, &flash_error);
FLASH_WaitForLastOperation(50000);
if(ret == HAL_ERROR){
printf("erase sector in flash failed!\r\n");
return FAILURE_RET;
}
else
return SUCCESS_RET;
}
uint8_t HAL_FLASH_Read_Byte(_IN_ uint8_t addr)
{
uint8_t data;
data=*(__IO uint8_t*)( addr);
return data;
}
/*
void version_get(char * verptr)
{
int i;
uint32_t version_addr=CURRENT_VERSION_START_ADDR;
for(i=0;i<VERSION_BYTE_NUM;i++)
verptr[i]=*(__IO uint8_t*)( version_addr++);
}
*/

View File

@ -0,0 +1,43 @@
/*
* Copyright (C) 2012-2019 UCloud. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/
#include "uiot_import.h"
void * HAL_Download_Init(_IN_ void * name)
{
char * file_name =(char *)name;
return (void *)(fopen(file_name, "wb+"));
}
int HAL_Download_Write(_IN_ void * handle,_IN_ uint32_t total_length,_IN_ char *buffer_read,_IN_ uint32_t length)
{
FILE *fp =(FILE *)handle;
if(NULL == fp)
return FAILURE_RET;
if(1 == fwrite(buffer_read, length, 1, fp))
return SUCCESS_RET;
else
return FAILURE_RET;
}
int HAL_Download_End(_IN_ void * handle)
{
FILE *fp =(FILE *)handle;
if(NULL == fp)
return FAILURE_RET;
fclose(fp);
return SUCCESS_RET;
}

View File

@ -0,0 +1,120 @@
/*
* Copyright (C) 2012-2019 UCloud. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* or in the "license" file accompanying this file. This file 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.
*/
#include "uiot_import.h"
uint32_t download_addr = DOWNLOAD_START_ADDR;
void * HAL_Download_Init(_IN_ void * name)
{
int i,ret;
char version_info[VERSION_BYTE_NUM];
uint32_t info_addr = CURRENT_VERSION_START_ADDR;
for(i=0; i < VERSION_BYTE_NUM; i++)
{
version_info[i] = HAL_FLASH_Read_Byte(info_addr);
info_addr++;
}
if(FAILURE_RET == HAL_FLASH_Erase_Sector(FLASH_SECTOR_3))
return NULL;
info_addr = CURRENT_VERSION_START_ADDR;
for(i = 0; i < VERSION_BYTE_NUM; i++)
{
ret = HAL_FLASH_Write_Byte(info_addr++,version_info[i]);
if(SUCCESS_RET != ret)
return NULL;
}
ret = HAL_FLASH_Write_Byte(DOWNLOAD_FINISH_FLG_ADDR,DOWNLOAD_FAILED);
if(SUCCESS_RET != ret)
return NULL;
ret = HAL_FLASH_Erase_Sector(FLASH_SECTOR_8, 4);
if(SUCCESS_RET != ret)
return NULL;
download_addr = DOWNLOAD_START_ADDR;
return &download_addr;
}
int HAL_Download_Write(_IN_ void * handle,_IN_ uint32_t total_length,_IN_ uint8_t *buffer_read,_IN_ uint32_t length)
{
uint8_t *readptr = buffer_read;
int i = 0,ret;
for(i = length; i > 0; i--){
ret = HAL_FLASH_Write_Byte(download_addr++, *readptr++);
if(ret == FAILURE_RET){
printf("HAL_Download_Write failed!\r\n");
return FAILURE_RET;
}
}
return SUCCESS_RET;
}
int HAL_Download_End(_IN_ void * handle)
{
HAL_FLASH_Write_Byte(DOWNLOAD_FINISH_FLG_ADDR,DOWNLOAD_SUCESS);
//reset here
__set_FAULTMASK(1);
HAL_NVIC_SystemReset();
return SUCCESS_RET;
}
int HAL_FLASH_Write_Byte(_IN_ uint32_t sddr,_IN_ uint32_t data)
{
HAL_StatusTypeDef ret;
ret = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, sddr, data);
if(ret == HAL_ERROR){
printf("write data byte to flash failed!\r\n");
return FAILURE_RET;
}
else
return SUCCESS_RET;
}
int HAL_FLASH_Erase_Sector(_IN_ uint8_t sector, _IN_ uint8_t sector_num)
{
uint32_t flash_error;
HAL_StatusTypeDef ret;
FLASH_EraseInitTypeDef flash_erase;
flash_erase.TypeErase = FLASH_TYPEERASE_SECTORS;
flash_erase.Sector = sector;
flash_erase.NbSectors = sector_num;
flash_erase.VoltageRange = FLASH_VOLTAGE_RANGE_3;
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_ERSERR | FLASH_FLAG_BSY);
ret = HAL_FLASHEx_Erase(&flash_erase, &flash_error);
FLASH_WaitForLastOperation(50000);
if(ret == HAL_ERROR){
printf("erase sector in flash failed!\r\n");
return FAILURE_RET;
}
else
return SUCCESS_RET;
}
uint8_t HAL_FLASH_Read_Byte(_IN_ uint8_t addr)
{
uint8_t data;
data=*(__IO uint8_t*)( addr);
return data;
}
/*
void version_get(char * verptr)
{
int i;
uint32_t version_addr=CURRENT_VERSION_START_ADDR;
for(i=0;i<VERSION_BYTE_NUM;i++)
verptr[i]=*(__IO uint8_t*)( version_addr++);
}
*/

View File

@ -97,13 +97,6 @@ static int _setup_connect_init_params(MQTTInitParams* initParams)
return SUCCESS_RET;
}
int IOT_OTA_FetchCallback_func(void *handle, IOT_OTA_UpstreamMsgType state)
{
OTA_Struct_t * h_ota = (OTA_Struct_t *) handle;
printf("file:%s, state:%d\r\n",h_ota->download_file_name, state);
return SUCCESS_RET;
}
int main(int argc, char **argv)
{
int rc;
@ -122,7 +115,7 @@ int main(int argc, char **argv)
return FAILURE_RET;
}
void *h_ota = IOT_OTA_Init(UIOT_MY_PRODUCT_SN, UIOT_MY_DEVICE_SN, client, IOT_OTA_FetchCallback_func);
void *h_ota = IOT_OTA_Init(UIOT_MY_PRODUCT_SN, UIOT_MY_DEVICE_SN, client);
if (NULL == h_ota) {
IOT_MQTT_Destroy(&client);
LOG_ERROR("init OTA failed");

View File

@ -301,7 +301,7 @@ do_exit:
return ret;
}
void *IOT_OTA_Init(const char *product_sn, const char *device_sn, void *ch_signal, IOT_OTA_FetchCallback fetch_callback_func)
void *IOT_OTA_Init(const char *product_sn, const char *device_sn, void *ch_signal)
{
POINTER_VALID_CHECK(product_sn, NULL);
POINTER_VALID_CHECK(device_sn, NULL);
@ -321,8 +321,6 @@ void *IOT_OTA_Init(const char *product_sn, const char *device_sn, void *ch_signa
LOG_ERROR("initialize signal channel failed");
goto do_exit;
}
h_ota->fetch_callback_func = fetch_callback_func;
h_ota->md5 = ota_lib_md5_init();
if (NULL == h_ota->md5) {
@ -702,15 +700,17 @@ int IOT_OTA_fw_download(void *handle)
int file_size = 0, length, firmware_valid, total_length = 0;
char *buffer_read = NULL;
OTA_Struct_t * h_ota = (OTA_Struct_t *) handle;
void * download_handle = NULL;
// 用于存放云端下发的固件版本
char msg_version[33];
void *fp;
IOT_OTA_Ioctl(h_ota, OTA_IOCTL_FILE_SIZE, &file_size, 4);
if (NULL == (fp = HAL_FileOpen(h_ota->download_file_name))) {
LOG_ERROR("open file failed");
return ERR_OTA_INVALID_PARAM;
download_handle = HAL_Download_Init(h_ota->download_file_name);
if(download_handle == NULL)
{
ret = FAILURE_RET;
goto __exit;
}
buffer_read = (char *)HAL_Malloc(HTTP_OTA_BUFF_LEN);
@ -729,11 +729,12 @@ int IOT_OTA_fw_download(void *handle)
if (length > 0)
{
/* Write the data to the corresponding partition address */
if (SUCCESS_RET != HAL_FileWrite(fp, total_length, buffer_read, length)) {
LOG_ERROR("write data to file failed");
if(HAL_Download_Write(download_handle, total_length, buffer_read, length) == FAILURE_RET){
ret = FAILURE_RET;
goto __exit;
}
total_length += length;
total_length += length;
//wait cancel cmd
IOT_OTA_Yield(handle, 100);
}
@ -758,6 +759,9 @@ int IOT_OTA_fw_download(void *handle)
IOT_OTA_Ioctl(h_ota, OTA_IOCTL_VERSION, msg_version, 33);
IOT_OTA_ReportSuccess(h_ota, msg_version);
}
if(HAL_Download_End(download_handle))
ret = FAILURE_RET;
LOG_INFO("Download firmware to flash success.");
}
@ -766,7 +770,6 @@ __exit:
if (buffer_read != NULL)
HAL_Free(buffer_read);
IOT_OTA_Clear(h_ota);
HAL_FileClose(fp);
return ret;
}

View File

@ -107,14 +107,13 @@ typedef struct {
* @brief OTA模块和返回句柄
* MQTT客户端必须在调用此接口之前进行初始化
*
* @param product_sn:
* @param device_sn:
* @param ch_signal: .
* @param fetch_callback_func: ota下载完成后的回调函数指针.
* @param product_sn:
* @param device_sn:
* @param ch_signal: .
*
* @retval : NULL
*/
void *IOT_OTA_Init(const char *product_sn, const char *device_sn, void *ch_signal, IOT_OTA_FetchCallback fetch_callback_func);
void *IOT_OTA_Init(const char *product_sn, const char *device_sn, void *ch_signal);
/**

View File

@ -128,30 +128,6 @@ uint64_t HAL_UptimeMs(void);
*/
void HAL_SleepMs(_IN_ uint32_t ms);
/**
* @brief .
*
* @param file_path .
*/
void *HAL_FileOpen(char *file_path);
/**
* @brief .
*
* @param dest .
* @param offset .
* @param src .
* @param size .
*/
int HAL_FileWrite(void *dest, uint32_t offset, void *src, uint32_t size);
/**
* @brief .
*
* @param fp .
*/
void HAL_FileClose(void *fp);
/**
* @brief FLASH
*
@ -341,19 +317,99 @@ int32_t HAL_TCP_Write(_IN_ uintptr_t fd, _IN_ unsigned char *buf, _IN_ size_t le
* @return <0: TCP读取错误; =0: TCP读超时, ; >0: TCP成功读取的字节数
*/
int32_t HAL_TCP_Read(_IN_ uintptr_t fd, _OU_ unsigned char *buf, _IN_ size_t len, _IN_ uint32_t timeout_ms);
/**
* @brief
*
* @param name
* @return
*/
void * HAL_Download_Init(_IN_ void * name);
/**
* @brief length的buffer_read的数据写入到FLASH中
*
* @param handle
* @param total_length
* @param buffer_read
* @param length
* @return
*/
int HAL_Download_Write(_IN_ void * handle,_IN_ uint32_t total_length,_IN_ char *buffer_read,_IN_ uint32_t length);
/**
* @brief STM32F767 FLASH的information分区的下载标志置位成功
*
* @param handle
* @return -1 0
*/
int HAL_Download_End(_IN_ void * handle);
#ifdef SUPPORT_AT_CMD
/**
* @brief
*
* @param
* @return
*/
void HAL_AT_Init();
/**
* @brief AT命令执行结果
*
* @param pNetwork
* @param buf
* @param len
* @return <0: TCP读取错误; =0: TCP读超时, ; >0: TCP成功读取的字节数
*/
int HAL_AT_Read(_IN_ utils_network_pt pNetwork, _OU_ unsigned char *buffer, _IN_ size_t len);
/**
* @brief AT命令
*
* @param buf
* @param len
* @return <0: TCP写入错误; =0: TCP写超时, ; >0: TCP成功写入的字节数
*/
int HAL_AT_Write(_IN_ unsigned char *buffer, _IN_ size_t len);
/**
* @brief TCP连接,
*
* @param pNetwork
* @return SUCCESSFAILURE
*/
int HAL_AT_TCP_Disconnect(utils_network_pt pNetwork);
/**
* @brief TCP连接HOST地址, TCP连接,
*
* @param pNetwork
* @param host TCP服务器网络地址
* @param port TCP服务器端口
* @return , TCP连接句柄NULL
*/
int HAL_AT_TCP_Connect(_IN_ utils_network_pt pNetwork, _IN_ const char *host, _IN_ uint16_t port);
/**
* @brief TCP连接写入数据
*
* @param pNetwork
* @param buf
* @param len
* @return <0: TCP写入错误; =0: TCP写超时, ; >0: TCP成功写入的字节数
*/
int HAL_AT_Write_Tcp(_IN_ utils_network_pt pNetwork, _IN_ unsigned char *buffer, _IN_ size_t len);
/**
* @brief TCP连接读取数据
*
* @param pNetwork
* @param buf
* @param len
* @return <0: TCP读取错误; =0: TCP读超时, ; >0: TCP成功读取的字节数
*/
int HAL_AT_Read_Tcp(_IN_ utils_network_pt pNetwork, _IN_ unsigned char *buffer, _IN_ size_t len);
#endif