Merge pull request 'merge code' (#56) from xidatong-arm32 into jerryscript

This commit is contained in:
wgzAIIT
2023-09-20 16:11:36 +08:00
15 changed files with 699 additions and 265 deletions
@@ -626,7 +626,6 @@ uint8_t FLASH_WritePage(uint32_t addr, const uint32_t *buf, uint32_t len)
flexspi_xfer_t flashXfer;
addr &= 0x0FFFFFFF;
flashXfer.operation = kFLEXSPIOperation_Write;
flashXfer.seqNum = 1;
flashXfer.seqId = NOR_CMD_LUT_SEQ_IDX_PAGEPROGRAM;
@@ -692,8 +691,8 @@ status_t NorFlash_Write_PageProgram(uint8_t* pBuffer,uint32_t WriteAddr,uint16_t
uint8_t temp_data[256] = {0xff};
memcpy(temp_data,pBuffer,NumByteToWrite);
status_t status = FLASH_WritePage(WriteAddr,(void *)temp_data,FLASH_PAGE_SIZE);
status_t status = FLASH_WritePage(WriteAddr,(void *)temp_data,FLASH_PAGE_SIZE);
if(status != kStatus_Success)
{
KPrintf("Write_PageProgram 0x%x faild!\r\n",WriteAddr);
@@ -773,9 +772,9 @@ status_t Flash_Erase(uint32_t start_addr, uint32_t imageSize)
for(i=0;i<sectorNum;i++)
{
status = FLASH_EraseSector(start_addr+i*SECTOR_SIZE);
if (status != kStatus_Success)
status = FLASH_EraseSector(start_addr+i*SECTOR_SIZE);
if(status != kStatus_Success)
{
KPrintf("Erase_Sector 0x%x faild!\r\n",i*SECTOR_SIZE);
return status;
@@ -817,7 +816,7 @@ status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWri
while(1)
{
status = FLASH_ReadBuf(CHIP_FLAH_BASE + secPos*SECTOR_SIZE, (void *)NorFlash_BUF, SECTOR_SIZE);//读出整个扇区的内容
if (status != kStatus_Success)
if(status != kStatus_Success)
{
return status;
}
@@ -831,7 +830,7 @@ status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWri
if(i < secRemain)//需要擦除
{
status = FLASH_EraseSector(CHIP_FLAH_BASE + secPos*SECTOR_SIZE);
if (status != kStatus_Success)
if(status != kStatus_Success)
{
return status;
}
@@ -840,7 +839,7 @@ status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWri
NorFlash_BUF[i+secOff] = pBuffer[i];
}
status = NorFlash_Write_NoCheck(NorFlash_BUF,CHIP_FLAH_BASE + secPos*SECTOR_SIZE,SECTOR_SIZE);//写入整个扇区
if (status != kStatus_Success)
if(status != kStatus_Success)
{
return status;
}
@@ -848,7 +847,7 @@ status_t Flash_Write(uint32_t WriteAddr, uint8_t *pBuffer, uint32_t NumByteToWri
else
{
status = NorFlash_Write_NoCheck(pBuffer,CHIP_FLAH_BASE + WriteAddr,secRemain);//写已经擦除了的,直接写入扇区剩余区间.
if (status != kStatus_Success)
if(status != kStatus_Success)
{
return status;
}
@@ -896,7 +895,6 @@ status_t Flash_Read(uint32_t addr, uint8_t *buf, uint32_t len)
{
return FLASH_ReadBuf(addr, (void *)buf, len);
}
else
{
void* result = memcpy(buf, (void*)addr, len);
@@ -907,8 +905,7 @@ status_t Flash_Read(uint32_t addr, uint8_t *buf, uint32_t len)
else
{
return (status_t)kStatus_Success;
}
}
}
}
@@ -993,10 +990,10 @@ status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
uint32_t WriteAddr;
WriteAddr = *FlashAddress;
status = Flash_Write(WriteAddr,Data,DataLength);
if (status != kStatus_Success)
if(status != kStatus_Success)
{
return status;
}
}
*FlashAddress += DataLength;
return (status_t)kStatus_Success;
}
@@ -1015,13 +1012,13 @@ status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
packetNum ++;
if(1 == packetNum)
{
WriteAddr = *FlashAddress;
WriteAddr = *FlashAddress;
}
if(dataLen>=SECTOR_SIZE)
{
status = Flash_Write(WriteAddr,dataBuff,dataLen);
if (status != kStatus_Success)
if(status != kStatus_Success)
{
return status;
}
@@ -1033,10 +1030,10 @@ status_t NOR_FLASH_Write(uint32_t* FlashAddress, uint8_t* Data ,uint16_t DataLen
else
{
status = Flash_Write(WriteAddr,dataBuff,dataLen);
if (status != kStatus_Success)
if(status != kStatus_Success)
{
return status;
}
}
packetNum = 0;
dataLen = 0;
}
@@ -350,10 +350,9 @@ static uint8 SdCardReadCd(void)
return BusDevReadData(pin->owner_haldev, &read_param);
}
static int sd_card_status = 0;
static void SdCardTask(void* parameter)
{
static int sd_card_status = 0;
while (1) {
if (!SdCardReadCd()) {
if (!sd_card_status) {
@@ -369,6 +368,11 @@ static void SdCardTask(void* parameter)
}
}
int GetSdCardStatus(void)
{
return sd_card_status;
}
#ifdef MOUNT_SDCARD
int MountSDCard()
{
+1 -1
View File
@@ -596,7 +596,7 @@ KERNELPATHS +=-I$(KERNEL_ROOT)/tool/bootloader/flash \
-I$(KERNEL_ROOT)/tool/bootloader/ota #
endif
ifeq ($(CONFIG_TOOL_USING_MQTT), y)
ifeq ($(CONFIG_LIB_USING_MQTT), y)
KERNELPATHS +=-I$(KERNEL_ROOT)/../../APP_Framework/lib/mqtt
endif
+5 -5
View File
@@ -7,7 +7,7 @@ menu "OTA function"
if TOOL_USING_OTA
choice
prompt "Compile bootloader bin or application bin."
default MCUBOOT_BOOTLOADER
default MCUBOOT_APPLICATION
config MCUBOOT_BOOTLOADER
bool "Config as bootloader."
@@ -23,7 +23,7 @@ menu "OTA function"
config OTA_BY_PLATFORM
bool "Through IoT management platform."
select TOOL_USING_MQTT
select LIB_USING_MQTT
config OTA_BY_TCPSERVER
bool "Through the public network TCP server."
@@ -65,10 +65,10 @@ menu "OTA function"
default 10000 if OTA_BY_TCPSERVER
default 10000 if MCUBOOT_BOOTLOADER
config OTA_RX_BUFFERSIZE
int "OTA receive data buffer size."
config OTA_FRAME_SIZE
int "OTA receive data frame size."
default 3072 if OTA_BY_PLATFORM
default 2048 if OTA_BY_TCPSERVER
default 1024 if OTA_BY_TCPSERVER
default 256 if MCUBOOT_BOOTLOADER
endif
+501 -134
View File
@@ -19,9 +19,10 @@
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <transform.h>
#include "shell.h"
#include "xsconfig.h"
#include "mcuboot.h"
#include "ymodem.h"
#include "ota.h"
@@ -34,6 +35,10 @@
#include "platform_mqtt.h"
#endif
#ifdef USING_DOWNLOAD_JSON
#include "cJSON.h"
#endif
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
@@ -42,7 +47,7 @@ static int create_version(uint8_t* cur_version, uint8_t* new_version);
static status_t UpdateOTAFlag(ota_info_t *ptr);
static void InitialVersion(void);
static void BackupVersion(void);
static void UpdateNewApplication(void);
static bool UpdateNewApplication(void);
static void Update(void);
static void BootLoaderJumpApp(void);
@@ -128,7 +133,7 @@ static uint32_t calculate_crc32(uint32_t addr, uint32_t len)
* : cur_version:,new_version:
* : 0:,-1:
* : ,OTA传输而来的版本号也要保持这样三段式的形式
major.minor.patch,01.02.03
major.minor.patch,001.002.003
*******************************************************************************/
static int create_version(uint8_t* cur_version, uint8_t* new_version)
{
@@ -226,30 +231,39 @@ static void BackupVersion(void)
ota_info.status = OTA_STATUS_BACKUP;
UpdateOTAFlag(&ota_info);
status = mcuboot.op_flash_copy(BAKUP_FLAH_ADDRESS, XIUOS_FLAH_ADDRESS, ota_info.bak.size);
if((status == kStatus_Success) &&(calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.bak.size) == ota_info.bak.crc32))
// 1.先清空XiUOS System分区
status = mcuboot.op_flash_erase(XIUOS_FLAH_ADDRESS, ota_info.os.size);
if(status == kStatus_Success)
{
mcuboot.print_string("\r\n------Backup app version success!------\r\n");
ota_info.os.size = ota_info.bak.size;
ota_info.os.crc32 = ota_info.bak.crc32;
memset(ota_info.os.version,0,sizeof(ota_info.os.version));
strncpy(ota_info.os.version, ota_info.bak.version, sizeof(ota_info.bak.version));
memset(ota_info.os.description,0,sizeof(ota_info.os.description));
strncpy(ota_info.os.description, ota_info.bak.description, sizeof(ota_info.bak.description));
memset(&ota_info.os,0,sizeof(ota_info.os));
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Clear app partition success!------\r\n");
}
else
{
mcuboot.print_string("\r\n------Backup app version failed!------\r\n");
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "Backup app version failed!",sizeof(ota_info.error_message));
mcuboot.print_string("\r\n------Clear app partition failed!------\r\n");
return;
}
// 2.拷贝backup分区到XiUOS System分区
status = mcuboot.op_flash_copy(BAKUP_FLAH_ADDRESS, XIUOS_FLAH_ADDRESS, ota_info.bak.size);
if(status == kStatus_Success)
{
ota_info.os.size = ota_info.bak.size;
ota_info.os.crc32 = ota_info.bak.crc32;
memset(ota_info.os.version,0,sizeof(ota_info.os.version));
strncpy(ota_info.os.version, ota_info.bak.version, sizeof(ota_info.bak.version));
memset(ota_info.os.description,0,sizeof(ota_info.os.description));
strncpy(ota_info.os.description, ota_info.bak.description, sizeof(ota_info.bak.description));
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Version rollback successful!------\r\n");
}
else
{
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "Version rollback failed!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Version rollback failed!------\r\n");
}
}
@@ -258,10 +272,11 @@ static void BackupVersion(void)
* : UpdateNewApplication
* : bootloader里进行调用,Flash中Flag分区中的信息决定是否进行版本更新
* :
* :
* : true:,flash搬移的操作,
false:,flash搬移
* : APP分区保持不变,APP分区的版本为新版本
*******************************************************************************/
static void UpdateNewApplication(void)
static bool UpdateNewApplication(void)
{
status_t status;
ota_info_t ota_info; // 定义OTA信息结构体
@@ -270,97 +285,126 @@ static void UpdateNewApplication(void)
// 从Flash中读取OTA信息
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
// 如果OTA升级状态为准备状态且APP分区与download分区版本不同,才可以进行升级
// 如果OTA升级状态为准备状态,且APP分区与download分区版本不同,才需要进行升级
if((ota_info.status == OTA_STATUS_READY) && (ota_info.os.crc32 != ota_info.down.crc32))
{
mcuboot.print_string("\r\n------Start to update the app!------\r\n");
mcuboot.print_string("\r\n------Start upgrading to new version!------\r\n");
// 校验downlad分区固件CRC
if(calculate_crc32(DOWN_FLAH_ADDRESS, ota_info.down.size) == ota_info.down.crc32)
{
ota_info.status = OTA_STATUS_UPDATING;
UpdateOTAFlag(&ota_info);
// 1.如果CRC校验通过,开始升级,逐字节拷贝Flash,先备份当前XiUOS System分区内容
// 1.如果CRC校验通过,开始升级,先清空Backup分区
status = mcuboot.op_flash_erase(BAKUP_FLAH_ADDRESS, ota_info.bak.size);
if(status == kStatus_Success)
{
memset(&ota_info.bak,0,sizeof(ota_info.bak));
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Clear backup partition success!------\r\n");
}
else
{
mcuboot.print_string("\r\n------Clear backup partition failed!------\r\n");
goto finish;
}
// 2.逐字节拷贝Flash,备份当前XiUOS System分区内容到Backup分区
status = mcuboot.op_flash_copy(XIUOS_FLAH_ADDRESS, BAKUP_FLAH_ADDRESS, ota_info.os.size);
if((status == kStatus_Success) &&(calculate_crc32(BAKUP_FLAH_ADDRESS, ota_info.os.size) == ota_info.os.crc32))
{
mcuboot.print_string("\r\n------Backup app success!------\r\n");
ota_info.bak.size = ota_info.os.size;
ota_info.bak.crc32 = ota_info.os.crc32;
memset(ota_info.bak.version,0,sizeof(ota_info.bak.version));
strncpy(ota_info.bak.version, ota_info.os.version, sizeof(ota_info.os.version));
memset(ota_info.bak.description,0,sizeof(ota_info.bak.description));
strncpy(ota_info.bak.description, ota_info.os.description, sizeof(ota_info.os.description));
UpdateOTAFlag(&ota_info);;
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Backup app success!------\r\n");
}
else
{
mcuboot.print_string("\r\n------Backup app failed!------\r\n");
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "Backup app failed!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);;
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Backup app failed!------\r\n");
goto finish;
}
// 2.拷贝download分区到XiUOS System分区
// 3.清空XiUOS System分区
status = mcuboot.op_flash_erase(XIUOS_FLAH_ADDRESS, ota_info.os.size);
if(status == kStatus_Success)
{
memset(&ota_info.os,0,sizeof(ota_info.os));
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Clear app partition success!------\r\n");
}
else
{
mcuboot.print_string("\r\n------Clear app partition failed!------\r\n");
goto finish;
}
// 4.拷贝download分区到XiUOS System分区
status = mcuboot.op_flash_copy(DOWN_FLAH_ADDRESS, XIUOS_FLAH_ADDRESS, ota_info.down.size);
if((status == kStatus_Success) &&(calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.down.size) == ota_info.down.crc32))
{
mcuboot.print_string("\r\n------The download partition is copied successfully!------\r\n");
ota_info.os.size = ota_info.down.size;
ota_info.os.crc32 = ota_info.down.crc32;
memset(ota_info.os.version,0,sizeof(ota_info.os.version));
strncpy(ota_info.os.version, ota_info.down.version, sizeof(ota_info.down.version));
memset(ota_info.os.description,0,sizeof(ota_info.os.description));
strncpy(ota_info.os.description, ota_info.down.description, sizeof(ota_info.down.description));
ota_info.status == OTA_STATUS_IDLE; // 拷贝download分区到XiUOS System分区成功,将OTA升级状态设置为IDLE
UpdateOTAFlag(&ota_info);;
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------The download partition is copied successfully!------\r\n");
}
else
{
mcuboot.print_string("\r\n------The download partition copy failed!------\r\n");
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "The download partition copy failed!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);;
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------The download partition copy failed!------\r\n");
goto finish;
}
mcuboot.print_string("\r\n------Update completed!------\r\n");
// 5.清空download分区
status = mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS, ota_info.down.size);
if(status == kStatus_Success)
{
memset(&ota_info.down,0,sizeof(ota_info.down));
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Clear download partition success!------\r\n");
}
else
{
mcuboot.print_string("\r\n------Clear download partition failed!------\r\n");
goto finish;
}
ota_info.status == OTA_STATUS_IDLE; //将OTA升级状态设置为IDLE
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------New version upgrade completed,reboot again!------\r\n");
goto finish;
}
else
{
// 如果download分区CRC校验失败升级失败
mcuboot.print_string("\r\n------Download Firmware CRC check failed!------\r\n");
// 如果download分区CRC校验失败,升级失败
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "Download Firmware CRC check failed!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);;
UpdateOTAFlag(&ota_info);
mcuboot.print_string("\r\n------Download Firmware CRC check failed!------\r\n");
goto finish;
}
}
else
{
mcuboot.print_string("\r\n------No need to update the app!------\r\n");
goto finish;
return false;
}
finish:
return;
return true;
}
@@ -376,23 +420,33 @@ static void Update(void)
mcuboot.flash_init();
memset(&ota_info, 0, sizeof(ota_info_t));
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
/* 此时APP分区还没有有效固件,需在bootloader下通过iap烧写出厂固件 */
if((ota_info.os.size > APP_FLASH_SIZE) || (calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.os.size) != ota_info.os.crc32))
/* APP分区有效固件,需在bootloader下烧写初始固件, os.size大于分区大小视为无效固件*/
if(ota_info.os.size > APP_FLASH_SIZE)
{
mcuboot.print_string("\r\nNeed to flash initial firmware!\r\n");
InitialVersion();
mcuboot.flash_deinit();
}
else
/*进行新版本的升级*/
else
{
UpdateNewApplication();
if(UpdateNewApplication() == true) /*如果实际发生了flash搬移,操作完成后再重启一次*/
{
mcuboot.flash_deinit();
mcuboot.op_delay(2000);
mcuboot.op_reset();
}
else /*如果实际未发生flash搬移,说明未发生实际的升级,操作完成后不必再重启*/
{
mcuboot.flash_deinit();
}
}
mcuboot.flash_deinit();
}
/*******************************************************************************
* : BootLoaderJumpApp
* : ,BAKUP分区进行恢复
* : ,BAKUP分区进行恢复,
* :
* :
*******************************************************************************/
@@ -406,7 +460,7 @@ static void BootLoaderJumpApp(void)
if(ota_info.lastjumpflag == JUMP_FAILED_FLAG)
{
mcuboot.print_string("\r\n------Bootloader false, begin backup!------\r\n");
mcuboot.print_string("\r\n------Jump to app partition failed,start version rollback!------\r\n");
BackupVersion();
}
else
@@ -466,6 +520,7 @@ static void app_ota_by_iap(void)
UpdateOTAFlag(&ota_info);
}
mcuboot.flash_deinit();
MdelayKTask(2000);
mcuboot.op_reset();
}
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),iap, app_ota_by_iap, ota by iap function);
@@ -527,6 +582,11 @@ static void get_start_signal(struct Adapter* adapter)
KPrintf("waiting for start msg...\n");
if(AdapterDeviceRecv(adapter, &start_msg, sizeof(start_msg)) >= 0 && start_msg.header.frame_flag == STARTFLAG)
{
if(start_msg.header.total_len > APP_FLASH_SIZE)
{
KPrintf("File size is larger than partition size,the partition size is %dk.\n",APP_FLASH_SIZE/1024);
break;
}
if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,start_msg.header.total_len) != kStatus_Success)
{
KPrintf("Failed to erase target fash!\n");
@@ -726,7 +786,7 @@ static void app_ota_by_4g(void)
}
}
mcuboot.flash_deinit();
KPrintf("ota file transfer complete,start reboot!\n");
KPrintf("firmware file transfer successful,start reboot!\n");
MdelayKTask(2000);
mcuboot.op_reset();
}
@@ -734,13 +794,301 @@ static void app_ota_by_4g(void)
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),ota, app_ota_by_4g, ota by 4g function);
#endif
#ifdef OTA_BY_PLATFORM
#define FRAME_LEN 2048 //每帧数据的数据包长度
static uint8_t MqttRxbuf[3072];
static uint8_t FrameBuf[FRAME_LEN];
static OTA_TCB AliOTA;
#if (OTA_FRAME_SIZE <= MQTT_FRAME_SIZE)
#define FRAME_LEN OTA_FRAME_SIZE
#else
#error "The value of FRAME_LEN should not be greater than MQTT_FRAME_SIZE!!"
#endif
static uint8_t MqttRxbuf[FRAME_LEN + 512];
static uint8_t FrameBuf[FRAME_LEN];
static OTA_TCB platform_ota;
#ifdef XIUOS_PLATFORM
/*******************************************************************************
* : PropertyVersion
* :
* :
* :
*******************************************************************************/
static void PropertyVersion(void)
{
uint8_t tempdatabuff[128];
ota_info_t ota_info;
memset(&ota_info, 0, sizeof(ota_info_t));
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
memset(tempdatabuff,0,128);
sprintf(tempdatabuff,"{\"clientId\":\"%s\",\"version\":\"%s\"}",CLIENTID,ota_info.os.version);
KPrintf("------current version is:%s------\r\n",ota_info.os.version);
MQTT_PublishDataQs1("xiuosiot/ota/version",tempdatabuff,strlen(tempdatabuff)); //发送等级QS=1的PUBLISH报文
}
/*-------------------------------------------------*/
/*函数名:OTA下载数据 */
/*参 数:size:本次下载量 */
/*参 数:offset:本次下载偏移量 */
/*返回值:无 */
/*-------------------------------------------------*/
static void OTA_Download(int size, int offset)
{
uint8_t tempdatabuff[128];
memset(tempdatabuff,0,128);
sprintf(tempdatabuff,"{\"clientId\":\"%s\",\"fileId\":%d,\"fileOffset\":%d,\"size\":%d}",Platform_mqtt.ClientID,platform_ota.streamId,offset,size);
MQTT_PublishDataQs0("xiuosiot/ota/files", tempdatabuff, strlen(tempdatabuff));
}
/*******************************************************************************
* : mqttCloudInteraction
* : MQTT协议与云平台交互
* :
* :
*******************************************************************************/
static void mqttCloudInteraction(void* parameter)
{
int datalen;
int ret = 0;
int freecnt = 0;
ota_info_t ota_info;
uint32_t heart_time = 0;
uint32_t flashdestination = DOWN_FLAH_ADDRESS;
uint8_t topicdatabuff[2][32];
char *ptr1, *ptr2;
uint16_t cmdlen;
mcuboot.flash_init();
memset(&ota_info, 0, sizeof(ota_info_t));
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
ota_info.status = OTA_STATUS_DOWNLOADING;
UpdateOTAFlag(&ota_info);
memset(topicdatabuff,0,sizeof(topicdatabuff));
sprintf(topicdatabuff[0],"ota/%s/update",CLIENTID);
sprintf(topicdatabuff[1],"ota/%s/files",CLIENTID);
reconnect:
if((AdapterNetActive() == 0) && MQTT_Connect() && MQTT_SubscribeTopic(topicdatabuff[0]) && MQTT_SubscribeTopic(topicdatabuff[1]))
{
KPrintf("Log in to the cloud platform and subscribe to the ota topic successfully.\n");
PropertyVersion();
}
else
{
KPrintf("Log in to the cloud platform failed, retry!\n");
goto reconnect;
}
#ifdef USING_DOWNLOAD_JSON
uint8_t jsontopicdatabuff[32];
uint8_t jsonfilename[32];
memset(jsontopicdatabuff,0,sizeof(jsontopicdatabuff));
sprintf(jsontopicdatabuff,"protocol/%s/files",CLIENTID);
if(MQTT_SubscribeTopic(jsontopicdatabuff))
{
KPrintf("subscribe to the json download topic successfully.\n");
}
#endif
while(1)
{
memset(MqttRxbuf,0,sizeof(MqttRxbuf));
datalen = MQTT_Recv(MqttRxbuf, sizeof(MqttRxbuf));
if(datalen <= 0)
{
freecnt++;
if((freecnt >= 20) && (CalculateTimeMsFromTick(CurrentTicksGain()) - heart_time >= HEART_TIME)) //连续20次未收到数据默认为为空闲状态,需每隔一段时间发送需要发送心跳包保活
{
heart_time = CalculateTimeMsFromTick(CurrentTicksGain());
freecnt = 0;
if(!MQTT_SendHeart()) //发送心跳包失败可能连接断开,需要重连
{
KPrintf("The connection has been disconnected, reconnecting!\n");
goto reconnect;
}
KPrintf("Send heartbeat packet successful!\n");
}
}
else if(MqttRxbuf[0] == 0x30)
{
freecnt = 0;
cmdlen = MQTT_DealPublishData(MqttRxbuf, datalen);
// 1.获取新版本固件大小及版本信息
ptr1 = strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[0]);
ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":");
if((ptr1 != NULL) &&(ptr2 != NULL))
{
if(sscanf(ptr2,"{\"fileSize\":%d,\"version\":\"%11s\",\"fileId\":%d,\"md5\"",&platform_ota.size,platform_ota.version,&platform_ota.streamId)==3)
{
KPrintf("------Start the firmware file transfer!------\r\n");
KPrintf("file size:%d,file id:%d,file version:%s\r\n",platform_ota.size,platform_ota.streamId,platform_ota.version);
KPrintf("---------------------------------------------\r\n");
if(platform_ota.size > APP_FLASH_SIZE)
{
KPrintf("File size is larger than partition size,the partition size is %dk.\n",APP_FLASH_SIZE/1024);
ret = -1;
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "File size is larger than partition size!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
break;
}
if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,platform_ota.size) != kStatus_Success)
{
KPrintf("Failed to erase download partition!\n");
ret = -1;
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "Failed to erase download partition!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
break;
}
platform_ota.counter = (platform_ota.size%FRAME_LEN != 0)? (platform_ota.size/FRAME_LEN + 1):(platform_ota.size/FRAME_LEN);
platform_ota.num = 1; //下载次数,初始值为1
platform_ota.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
}
else
{
KPrintf("Failed to get ota information!\n");
ret = -1;
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "Failed to get ota information!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
break;
}
}
// 2.分片接收新版本固件
if(strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[1]))
{
memset(FrameBuf,0,sizeof(FrameBuf));
memcpy(FrameBuf, &MqttRxbuf[datalen-platform_ota.downlen], platform_ota.downlen);
if(mcuboot.op_flash_write(flashdestination,FrameBuf,platform_ota.downlen) != kStatus_Success)
{
KPrintf("current frame[%d] flash failed.\n",platform_ota.num-1);
ret = -1;
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
sprintf(ota_info.error_message,"current frame[%d] flash failed.",platform_ota.num-1);
UpdateOTAFlag(&ota_info);
break;
}
else
{
KPrintf("current frame[%d] is written to flash 0x%x address successful.\n", platform_ota.num -1, flashdestination);
KPrintf("current progress is %d/%d.\r\n",platform_ota.num,platform_ota.counter);
flashdestination += platform_ota.downlen;
platform_ota.num++;
}
if(platform_ota.num < platform_ota.counter) //如果小于总下载次数
{
platform_ota.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
}
else if(platform_ota.num == platform_ota.counter) //如果等于总下载次数,说明是最后一次下载
{
if(platform_ota.size%FRAME_LEN == 0) //判断固件大小是否是FRAME_LEN的整数倍
{
platform_ota.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
}
else
{
platform_ota.downlen = platform_ota.size%FRAME_LEN; //记录本次下载量
OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
}
}
else //下载完毕
{
ret = 0;
break;
}
}
#ifdef USING_DOWNLOAD_JSON
// 3.下载json文件,SD卡要确保已经插入
extern int GetSdCardStatus(void);
if(strstr((char *)Platform_mqtt.cmdbuff,jsontopicdatabuff) && GetSdCardStatus())
{
KPrintf("------Start download joson file !------\r\n");
memset(jsonfilename,0,sizeof(jsonfilename));
memset(FrameBuf,0,sizeof(FrameBuf));
memcpy(FrameBuf, &Platform_mqtt.cmdbuff[strlen(jsontopicdatabuff)],cmdlen-strlen(jsontopicdatabuff));
cJSON *json_obj = cJSON_Parse(FrameBuf);
char* product_name = cJSON_GetObjectItem(json_obj, "productName")->valuestring;
sprintf(jsonfilename,"%s",product_name);
strcat(jsonfilename,".json");
FILE *fp = fopen(jsonfilename, "w");
if(fp == NULL)
{
KPrintf("%s file create failed,please check!\r\n",jsonfilename);
cJSON_Delete(json_obj);
continue;
}
else
{
KPrintf("%s file create success!\r\n",jsonfilename);
char *json_print_str = cJSON_Print(json_obj);
fprintf(fp, "%s", json_print_str);
fclose(fp);
cJSON_free(json_print_str);
cJSON_Delete(json_obj);
}
KPrintf("download %s file done!------\r\n",jsonfilename);
}
#endif
}
else
{
freecnt = 0;
continue;
}
}
// 新版本固件接收完毕,写入描述信息
if(0 == ret)
{
ota_info.down.size = platform_ota.size;
ota_info.down.crc32 = calculate_crc32(DOWN_FLAH_ADDRESS, platform_ota.size);
memset(ota_info.down.version,0,sizeof(ota_info.down.version));
strncpy(ota_info.down.version, platform_ota.version, sizeof(ota_info.down.version));
memset(ota_info.down.description,0,sizeof(ota_info.down.description));
strncpy(ota_info.down.description, "MQTT OTA bin.",sizeof(ota_info.down.description));
ota_info.status = OTA_STATUS_READY;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
KPrintf("firmware file transfer successful,start reboot!\n");
}
else
{
KPrintf("firmware file transfer failed,start reboot!\n");
}
MQTT_UnSubscribeTopic(topicdatabuff[0]);
MQTT_UnSubscribeTopic(topicdatabuff[1]);
MQTT_Disconnect();
mcuboot.flash_deinit();
MdelayKTask(2000);
mcuboot.op_reset();
}
#endif
#ifdef ALIBABA_PLATFORM
/*******************************************************************************
* : PropertyVersion
* :
@@ -761,6 +1109,7 @@ static void PropertyVersion(void)
memset(tempdatabuff,0,128);
sprintf(tempdatabuff,"{\"id\": \"1\",\"params\": {\"version\": \"%s\"}}",ota_info.os.version);
KPrintf("------current version is:%s------\r\n",ota_info.os.version);
MQTT_PublishDataQs1(topicdatabuff,tempdatabuff,strlen(tempdatabuff)); //发送等级QS=1的PUBLISH报文
}
@@ -768,11 +1117,11 @@ static void PropertyVersion(void)
/*-------------------------------------------------*/
/*函数名:OTA下载数据 */
/*参 数:size本次下载量 */
/*参 数:offset本次下载偏移量 */
/*参 数:size:本次下载量 */
/*参 数:offset:本次下载偏移量 */
/*返回值:无 */
/*-------------------------------------------------*/
void OTA_Download(int size, int offset)
static void OTA_Download(int size, int offset)
{
uint8_t topicdatabuff[64];
uint8_t tempdatabuff[128];
@@ -781,18 +1130,18 @@ void OTA_Download(int size, int offset)
sprintf(topicdatabuff,"/sys/%s/%s/thing/file/download",PLATFORM_PRODUCTKEY,CLIENT_DEVICENAME);
memset(tempdatabuff,0,128);
sprintf(tempdatabuff,"{\"id\": \"1\",\"params\": {\"fileInfo\":{\"streamId\":%d,\"fileId\":1},\"fileBlock\":{\"size\":%d,\"offset\":%d}}}",AliOTA.streamId,size,offset);
sprintf(tempdatabuff,"{\"id\": \"1\",\"params\": {\"fileInfo\":{\"streamId\":%d,\"fileId\":1},\"fileBlock\":{\"size\":%d,\"offset\":%d}}}",platform_ota.streamId,size,offset);
MQTT_PublishDataQs0(topicdatabuff, tempdatabuff, strlen(tempdatabuff));
}
/*******************************************************************************
* : app_ota_by_platform
* : MQTT进行升级
* : mqttCloudInteraction
* : MQTT协议与云平台交互
* :
* :
*******************************************************************************/
static void app_ota_by_platform(void* parameter)
static void mqttCloudInteraction(void* parameter)
{
int datalen;
int ret = 0;
@@ -812,7 +1161,7 @@ static void app_ota_by_platform(void* parameter)
sprintf(topicdatabuff,"/sys/%s/%s/thing/file/download_reply",PLATFORM_PRODUCTKEY,CLIENT_DEVICENAME);
reconnect:
if((AdapterNetActive() == 0) && (MQTT_Connect() == 0) && MQTT_SubscribeTopic(topicdatabuff) == 0)
if((AdapterNetActive() == 0) && MQTT_Connect() && MQTT_SubscribeTopic(topicdatabuff))
{
KPrintf("Log in to the cloud platform and subscribe to the topic successfully.\n");
PropertyVersion();
@@ -829,73 +1178,105 @@ reconnect:
datalen = MQTT_Recv(MqttRxbuf, sizeof(MqttRxbuf));
if(datalen <= 0)
{
freecnt++;
freecnt++;
if((freecnt >= 20) && (CalculateTimeMsFromTick(CurrentTicksGain()) - heart_time >= HEART_TIME)) //连续20次未收到数据默认为为空闲状态,需每隔一段时间发送需要发送心跳包保活
{
heart_time = CalculateTimeMsFromTick(CurrentTicksGain());
freecnt = 0;
if(!MQTT_SendHeart()) //发送心跳包失败可能连接断开,需要重连
{
KPrintf("The connection has been disconnected, reconnecting!\n");
goto reconnect;
}
KPrintf("Send heartbeat packet successful!\n");
}
}
else if(MqttRxbuf[0] == 0x30)
{
freecnt = 0;
MQTT_DealPublishData(MqttRxbuf, datalen);
ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\"");
// 1.获取新版本固件大小及版本信息
ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\"");
if(ptr != NULL)
{
if(sscanf(ptr,"{\"code\":\"1000\",\"data\":{\"size\":%d,\"streamId\":%d,\"sign\":\"%*32s\",\"dProtocol\":\"mqtt\",\"version\":\"%11s\"",&AliOTA.size,&AliOTA.streamId,AliOTA.version)==3)
if(sscanf(ptr,"{\"code\":\"1000\",\"data\":{\"size\":%d,\"streamId\":%d,\"sign\":\"%*32s\",\"dProtocol\":\"mqtt\",\"version\":\"%11s\"",&platform_ota.size,&platform_ota.streamId,platform_ota.version)==3)
{
KPrintf("ota file size:%d\r\n",AliOTA.size);
KPrintf("ota file id:%d\r\n",AliOTA.streamId);
KPrintf("ota file version:%s\r\n",AliOTA.version);
if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,AliOTA.size != kStatus_Success))
KPrintf("------Start the firmware file transfer!------\r\n");
KPrintf("file size:%d,file id:%d,file version:%s\r\n",platform_ota.size,platform_ota.streamId,platform_ota.version);
KPrintf("---------------------------------------------\r\n");
if(platform_ota.size > APP_FLASH_SIZE)
{
KPrintf("Failed to erase target fash!\n");
KPrintf("File size is larger than partition size,the partition size is %dk.\n",APP_FLASH_SIZE/1024);
ret = -1;
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "File size is larger than partition size!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
break;
}
if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,platform_ota.size) != kStatus_Success)
{
KPrintf("Failed to erase download partition!\n");
ret = -1;
break;
}
AliOTA.counter = (AliOTA.size%FRAME_LEN != 0)? (AliOTA.size/FRAME_LEN + 1):(AliOTA.size/FRAME_LEN);
AliOTA.num = 1; //下载次数,初始值为1
AliOTA.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(AliOTA.downlen,(AliOTA.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
platform_ota.counter = (platform_ota.size%FRAME_LEN != 0)? (platform_ota.size/FRAME_LEN + 1):(platform_ota.size/FRAME_LEN);
platform_ota.num = 1; //下载次数,初始值为1
platform_ota.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
}
else
{
KPrintf("Failed to get ota information!\n");
ret = -1;
break;
KPrintf("Failed to get ota information!\n");
ret = -1;
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "Failed to get ota information!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
break;
}
}
// 2.分片接收新版本固件
if(strstr((char *)Platform_mqtt.cmdbuff,"download_reply"))
{
memset(FrameBuf,0,sizeof(FrameBuf));
memcpy(FrameBuf, &MqttRxbuf[datalen-AliOTA.downlen-2], AliOTA.downlen);
if(mcuboot.op_flash_write(flashdestination,FrameBuf,AliOTA.downlen) != kStatus_Success)
memcpy(FrameBuf, &MqttRxbuf[datalen-platform_ota.downlen-2], platform_ota.downlen);
if(mcuboot.op_flash_write(flashdestination,FrameBuf,platform_ota.downlen) != kStatus_Success)
{
KPrintf("current frame[%d] flash failed.\n",AliOTA.num-1);
KPrintf("current frame[%d] flash failed.\n",platform_ota.num-1);
ret = -1;
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
sprintf(ota_info.error_message,"current frame[%d] flash failed.",platform_ota.num-1);
UpdateOTAFlag(&ota_info);
break;
}
else
{
KPrintf("current frame[%d] is written to flash 0x%x address successful.\n", AliOTA.num -1, flashdestination);
KPrintf("Current progress is %d/%d\r\n",AliOTA.num,AliOTA.counter);
flashdestination += AliOTA.downlen;
AliOTA.num++;
KPrintf("current frame[%d] is written to flash 0x%x address successful.\n", platform_ota.num -1, flashdestination);
KPrintf("current progress is %d/%d.\r\n",platform_ota.num,platform_ota.counter);
flashdestination += platform_ota.downlen;
platform_ota.num++;
}
if(AliOTA.num < AliOTA.counter) //如果小于总下载次数
if(platform_ota.num < platform_ota.counter) //如果小于总下载次数
{
AliOTA.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(AliOTA.downlen,(AliOTA.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
platform_ota.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
}
else if(AliOTA.num == AliOTA.counter) //如果等于总下载次数,说明是最后一次下载
else if(platform_ota.num == platform_ota.counter) //如果等于总下载次数,说明是最后一次下载
{
if(AliOTA.size%FRAME_LEN == 0) //判断固件大小是否是FRAME_LEN的整数倍
if(platform_ota.size%FRAME_LEN == 0) //判断固件大小是否是FRAME_LEN的整数倍
{
AliOTA.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(AliOTA.downlen,(AliOTA.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
platform_ota.downlen = FRAME_LEN; //记录本次下载量
OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
}
else
{
AliOTA.downlen = AliOTA.size%FRAME_LEN; //记录本次下载量
OTA_Download(AliOTA.downlen,(AliOTA.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
platform_ota.downlen = platform_ota.size%FRAME_LEN; //记录本次下载量
OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
}
}
@@ -912,28 +1293,16 @@ reconnect:
freecnt = 0;
continue;
}
if((freecnt >= 10) && (CalculateTimeMsFromTick(CurrentTicksGain()) - heart_time >= HEART_TIME)) //连续10次未收到数据默认为为空闲状态,需每隔一段时间发送需要发送心跳包保活
{
heart_time = CalculateTimeMsFromTick(CurrentTicksGain());
if(MQTT_SendHeart() != 0) //发送心跳包失败可能连接断开,需要重连
{
KPrintf("The connection has been disconnected, reconnecting!\n");
freecnt = 0;
heart_time = 0;
goto reconnect;
}
KPrintf("Send heartbeat packet successful!\n");
}
}
// 新版本固件接收完毕,写入描述信息
if(0 == ret)
{
ota_info.down.size = AliOTA.size;
ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, AliOTA.size);
ota_info.down.size = platform_ota.size;
ota_info.down.crc32 = calculate_crc32(DOWN_FLAH_ADDRESS, platform_ota.size);
memset(ota_info.down.version,0,sizeof(ota_info.down.version));
strncpy(ota_info.down.version, AliOTA.version, sizeof(ota_info.down.version));
strncpy(ota_info.down.version, platform_ota.version, sizeof(ota_info.down.version));
memset(ota_info.down.description,0,sizeof(ota_info.down.description));
strncpy(ota_info.down.description, "MQTT OTA bin.",sizeof(ota_info.down.description));
@@ -944,28 +1313,26 @@ reconnect:
strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
}
KPrintf("firmware file transfer successful,start reboot!\n");
}
else
{
ota_info.status = OTA_STATUS_ERROR;
memset(ota_info.error_message,0,sizeof(ota_info.error_message));
strncpy(ota_info.error_message, "Failed to download firmware to download partition!",sizeof(ota_info.error_message));
UpdateOTAFlag(&ota_info);
KPrintf("firmware file transfer failed,start reboot!\n");
}
MQTT_UnSubscribeTopic(topicdatabuff);
MQTT_Disconnect();
mcuboot.flash_deinit();
KPrintf("ota file transfer complete,start reboot!\n");
MdelayKTask(2000);
mcuboot.op_reset();
}
#endif
int OtaTask(void)
{
int32 ota_task = 0;
ota_task = KTaskCreate("ota_platform", app_ota_by_platform, NULL,8192, 10);
ota_task = KTaskCreate("mqtt_platform", mqttCloudInteraction, NULL,10240, 10);
if(ota_task < 0) {
KPrintf("ota_task create failed ...%s %d.\n", __FUNCTION__,__LINE__);
KPrintf("matt platform task create failed ...%s %d.\n", __FUNCTION__,__LINE__);
return ERROR;
}
@@ -1008,14 +1375,14 @@ void ota_entry(void)
mcuboot.board_init();
mcuboot.print_string("Please press 'space' key into menu in 10s !!!\r\n");
mcuboot.print_string("Please press 'space' key into menu in 5s !!!\r\n");
while(timeout)
{
ret = (SerialKeyPressed((uint8_t*)&ch1));
if(ret) break;
timeout--;
mcuboot.op_delay(10);
mcuboot.op_delay(5);
}
while(1)
+36 -35
View File
@@ -22,60 +22,61 @@
#define __OTA_DEF_H__
#include "flash_ops.h"
#include "xsconfig.h"
#define JUMP_FAILED_FLAG 0XABABABAB
#define JUMP_SUCCESS_FLAG 0XCDCDCDCD
#define STARTFLAG 0x1A2B //数据帧开始信号标记
#define DATAFLAG 0x3C4D //数据帧数据信号标记
#define ENDTFLAG 0x5E6F //数据帧结束信号标记
#define LENGTH 1024 //每帧数据的数据包长度
typedef enum {
OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级
OTA_STATUS_READY, // 准备状态,可以进行OTA升级
OTA_STATUS_DOWNLOADING, // 正在下载固件
OTA_STATUS_DOWNLOADED, // 固件下载完成
OTA_STATUS_UPDATING, // 正在进行OTA升级
OTA_STATUS_BACKUP, // 正在版本回退
OTA_STATUS_ERROR, // 出现错误,升级失败
OTA_STATUS_IDLE = 0, //空闲状态,没有进行OTA升级
OTA_STATUS_READY, //准备状态,可以进行OTA升级
OTA_STATUS_DOWNLOADING, //正在下载固件
OTA_STATUS_DOWNLOADED, //固件下载完成
OTA_STATUS_UPDATING, //正在进行OTA升级
OTA_STATUS_BACKUP, //正在版本回退
OTA_STATUS_ERROR, //出现错误,升级失败
} ota_status_t;
/* Flash分区中保存固件的属性描述 */
typedef struct {
uint32_t size; // 应用程序大小,记录分区固件的大小
uint32_t crc32; // 应用程序CRC32校验值,记录分区固件的crc32值
uint8_t version[32]; // 应用程序版本号,记录分区固件的版本
uint8_t description[32]; // 固件的描述信息,最多32个字符
uint32_t size; //应用程序大小,记录分区固件的大小
uint32_t crc32; //应用程序CRC32校验值,记录分区固件的crc32值
uint8_t version[32]; //应用程序版本号,记录分区固件的版本
uint8_t description[32]; //固件的描述信息,最多32个字符
} firmware_t;
/* OTA升级过程中的信息结构体 */
typedef struct {
firmware_t os; // XiUOS System分区属性信息
firmware_t bak; // Bakup分区属性信息
firmware_t down; // Download分区属性信息
uint32_t status; // 升级状态,取值来自于ota_status_t类型
uint32_t lastjumpflag; // bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0xCDCDCDCD
uint8_t error_message[64]; // 错误信息,最多64个字符
firmware_t os; //XiUOS System分区属性信息
firmware_t bak; //Bakup分区属性信息
firmware_t down; //Download分区属性信息
uint32_t status; //升级状态,取值来自于ota_status_t类型
uint32_t lastjumpflag; //bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0xCDCDCDCD
uint8_t error_message[64]; //错误信息,最多64个字符
} ota_info_t;
#ifdef OTA_BY_TCPSERVER
#define STARTFLAG 0x1A2B //数据帧开始信号标记
#define DATAFLAG 0x3C4D //数据帧数据信号标记
#define ENDTFLAG 0x5E6F //数据帧结束信号标记
#define LENGTH OTA_FRAME_SIZE //每帧数据的数据包长度
/*bin包传输过程中的数据帧相关的结构体*/
typedef struct
{
uint16_t frame_flag; // frame start flag 2 Bytes
uint16_t dev_sid; // device software version
uint32_t total_len; // send data total length caculated from each frame_len
uint16_t frame_flag; //frame start flag 2 Bytes
uint16_t dev_sid; //device software version
uint32_t total_len; //send data total length caculated from each frame_len
} ota_header_t;
typedef struct
{
uint32_t frame_id; // Current frame id
uint8_t frame_data[LENGTH]; // Current frame data
uint16_t frame_len; // Current frame data length
uint16_t crc; // Current frame data crc
uint32_t frame_id; //Current frame id
uint8_t frame_data[LENGTH]; //Current frame data
uint16_t frame_len; //Current frame data length
uint16_t crc; //Current frame data crc
} ota_frame_t;
typedef struct
@@ -87,13 +88,13 @@ typedef struct
#ifdef OTA_BY_PLATFORM
typedef struct{
uint32_t size; //OTA固件大小
uint32_t streamId; //OTA固件下载时ID编号
uint32_t counter; //OTA总下载次数
uint32_t num; //OTA当前下载次数
uint32_t downlen; //OTA当前下载次数的下载量
uint8_t version[32]; //OTA下载时存储版本号的缓存区
}OTA_TCB;
uint32_t size; //OTA固件大小
uint32_t streamId; //OTA固件下载时ID编号
uint32_t counter; //OTA总下载次数
uint32_t num; //OTA当前下载次数
uint32_t downlen; //OTA当前下载次数的下载量
uint8_t version[32]; //OTA下载时存储版本号的缓存区
} OTA_TCB;
#endif
void app_clear_jumpflag(void);