1、Verify file size before ota file transfer

2、update MQTT connection parameter configuration
This commit is contained in:
wgzAIIT 2023-09-14 17:13:34 +08:00
parent 474557a5ac
commit 5ef61d14b1
3 changed files with 73 additions and 38 deletions

View File

@ -23,7 +23,7 @@ menu "lib using MQTT"
config CLIENTID config CLIENTID
string "mqtt client id." string "mqtt client id."
default "xidatong0001" default "D001"
config USERNAME config USERNAME
string "mqtt client username." string "mqtt client username."

View File

@ -578,6 +578,11 @@ static void get_start_signal(struct Adapter* adapter)
KPrintf("waiting for start msg...\n"); KPrintf("waiting for start msg...\n");
if(AdapterDeviceRecv(adapter, &start_msg, sizeof(start_msg)) >= 0 && start_msg.header.frame_flag == STARTFLAG) 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) if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,start_msg.header.total_len) != kStatus_Success)
{ {
KPrintf("Failed to erase target fash!\n"); KPrintf("Failed to erase target fash!\n");
@ -896,15 +901,30 @@ reconnect:
ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":"); ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":");
if((ptr1 != NULL) &&(ptr2 != NULL)) if((ptr1 != NULL) &&(ptr2 != NULL))
{ {
// 1.获取新版本固件大小及版本信息
if(sscanf(ptr2,"{\"fileSize\":%d,\"version\":\"%11s\",\"fileId\":%d,\"md5\"",&platform_ota.size,platform_ota.version,&platform_ota.streamId)==3) 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("------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("file size:%d,file id:%d,file version:%s\r\n",platform_ota.size,platform_ota.streamId,platform_ota.version);
KPrintf("---------------------------------------------\r\n"); 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) if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,platform_ota.size) != kStatus_Success)
{ {
KPrintf("Failed to erase target fash!\n"); KPrintf("Failed to erase download partition!\n");
ret = -1; 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; break;
} }
platform_ota.counter = (platform_ota.size%FRAME_LEN != 0)? (platform_ota.size/FRAME_LEN + 1):(platform_ota.size/FRAME_LEN); platform_ota.counter = (platform_ota.size%FRAME_LEN != 0)? (platform_ota.size/FRAME_LEN + 1):(platform_ota.size/FRAME_LEN);
@ -924,6 +944,7 @@ reconnect:
} }
} }
// 2.分片接收新版本固件
if(strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[1])) if(strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[1]))
{ {
memset(FrameBuf,0,sizeof(FrameBuf)); memset(FrameBuf,0,sizeof(FrameBuf));
@ -980,6 +1001,7 @@ reconnect:
} }
} }
// 3.新版本固件接收完毕,写入描述信息
if(0 == ret) if(0 == ret)
{ {
ota_info.down.size = platform_ota.size; ota_info.down.size = platform_ota.size;
@ -1122,14 +1144,25 @@ reconnect:
ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\""); ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\"");
if(ptr != NULL) if(ptr != NULL)
{ {
// 1.获取新版本固件大小及版本信息
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) 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("------Start the firmware file transfer!------\r\n"); 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("file size:%d,file id:%d,file version:%s\r\n",platform_ota.size,platform_ota.streamId,platform_ota.version);
KPrintf("---------------------------------------------\r\n"); 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) if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,platform_ota.size) != kStatus_Success)
{ {
KPrintf("Failed to erase target fash!\n"); KPrintf("Failed to erase download partition!\n");
ret = -1; ret = -1;
break; break;
} }
@ -1150,6 +1183,7 @@ reconnect:
} }
} }
// 2.分片接收新版本固件
if(strstr((char *)Platform_mqtt.cmdbuff,"download_reply")) if(strstr((char *)Platform_mqtt.cmdbuff,"download_reply"))
{ {
memset(FrameBuf,0,sizeof(FrameBuf)); memset(FrameBuf,0,sizeof(FrameBuf));
@ -1206,6 +1240,7 @@ reconnect:
} }
} }
// 3.新版本固件接收完毕,写入描述信息
if(0 == ret) if(0 == ret)
{ {
ota_info.down.size = platform_ota.size; ota_info.down.size = platform_ota.size;

View File

@ -27,55 +27,55 @@
#define JUMP_SUCCESS_FLAG 0XCDCDCDCD #define JUMP_SUCCESS_FLAG 0XCDCDCDCD
typedef enum { typedef enum {
OTA_STATUS_IDLE = 0, // 空闲状态,没有进行OTA升级 OTA_STATUS_IDLE = 0, //空闲状态,没有进行OTA升级
OTA_STATUS_READY, // 准备状态,可以进行OTA升级 OTA_STATUS_READY, //准备状态,可以进行OTA升级
OTA_STATUS_DOWNLOADING, // 正在下载固件 OTA_STATUS_DOWNLOADING, //正在下载固件
OTA_STATUS_DOWNLOADED, // 固件下载完成 OTA_STATUS_DOWNLOADED, //固件下载完成
OTA_STATUS_UPDATING, // 正在进行OTA升级 OTA_STATUS_UPDATING, //正在进行OTA升级
OTA_STATUS_BACKUP, // 正在版本回退 OTA_STATUS_BACKUP, //正在版本回退
OTA_STATUS_ERROR, // 出现错误,升级失败 OTA_STATUS_ERROR, //出现错误,升级失败
} ota_status_t; } ota_status_t;
/* Flash分区中保存固件的属性描述 */ /* Flash分区中保存固件的属性描述 */
typedef struct { typedef struct {
uint32_t size; // 应用程序大小,记录分区固件的大小 uint32_t size; //应用程序大小,记录分区固件的大小
uint32_t crc32; // 应用程序CRC32校验值,记录分区固件的crc32值 uint32_t crc32; //应用程序CRC32校验值,记录分区固件的crc32值
uint8_t version[32]; // 应用程序版本号,记录分区固件的版本 uint8_t version[32]; //应用程序版本号,记录分区固件的版本
uint8_t description[32]; // 固件的描述信息,最多32个字符 uint8_t description[32]; //固件的描述信息,最多32个字符
} firmware_t; } firmware_t;
/* OTA升级过程中的信息结构体 */ /* OTA升级过程中的信息结构体 */
typedef struct { typedef struct {
firmware_t os; // XiUOS System分区属性信息 firmware_t os; //XiUOS System分区属性信息
firmware_t bak; // Bakup分区属性信息 firmware_t bak; //Bakup分区属性信息
firmware_t down; // Download分区属性信息 firmware_t down; //Download分区属性信息
uint32_t status; // 升级状态,取值来自于ota_status_t类型 uint32_t status; //升级状态,取值来自于ota_status_t类型
uint32_t lastjumpflag; // bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0xCDCDCDCD uint32_t lastjumpflag; //bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0xCDCDCDCD
uint8_t error_message[64]; // 错误信息,最多64个字符 uint8_t error_message[64]; //错误信息,最多64个字符
} ota_info_t; } ota_info_t;
#ifdef OTA_BY_TCPSERVER #ifdef OTA_BY_TCPSERVER
#define STARTFLAG 0x1A2B //数据帧开始信号标记 #define STARTFLAG 0x1A2B //数据帧开始信号标记
#define DATAFLAG 0x3C4D //数据帧数据信号标记 #define DATAFLAG 0x3C4D //数据帧数据信号标记
#define ENDTFLAG 0x5E6F //数据帧结束信号标记 #define ENDTFLAG 0x5E6F //数据帧结束信号标记
#define LENGTH 1024 //每帧数据的数据包长度 #define LENGTH 1024 //每帧数据的数据包长度
/*bin包传输过程中的数据帧相关的结构体*/ /*bin包传输过程中的数据帧相关的结构体*/
typedef struct typedef struct
{ {
uint16_t frame_flag; // frame start flag 2 Bytes uint16_t frame_flag; //frame start flag 2 Bytes
uint16_t dev_sid; // device software version uint16_t dev_sid; //device software version
uint32_t total_len; // send data total length caculated from each frame_len uint32_t total_len; //send data total length caculated from each frame_len
} ota_header_t; } ota_header_t;
typedef struct typedef struct
{ {
uint32_t frame_id; // Current frame id uint32_t frame_id; //Current frame id
uint8_t frame_data[LENGTH]; // Current frame data uint8_t frame_data[LENGTH]; //Current frame data
uint16_t frame_len; // Current frame data length uint16_t frame_len; //Current frame data length
uint16_t crc; // Current frame data crc uint16_t crc; //Current frame data crc
} ota_frame_t; } ota_frame_t;
typedef struct typedef struct
@ -87,13 +87,13 @@ typedef struct
#ifdef OTA_BY_PLATFORM #ifdef OTA_BY_PLATFORM
typedef struct{ typedef struct{
uint32_t size; //OTA固件大小 uint32_t size; //OTA固件大小
uint32_t streamId; //OTA固件下载时ID编号 uint32_t streamId; //OTA固件下载时ID编号
uint32_t counter; //OTA总下载次数 uint32_t counter; //OTA总下载次数
uint32_t num; //OTA当前下载次数 uint32_t num; //OTA当前下载次数
uint32_t downlen; //OTA当前下载次数的下载量 uint32_t downlen; //OTA当前下载次数的下载量
uint8_t version[32]; //OTA下载时存储版本号的缓存区 uint8_t version[32]; //OTA下载时存储版本号的缓存区
}OTA_TCB; } OTA_TCB;
#endif #endif
void app_clear_jumpflag(void); void app_clear_jumpflag(void);