From bad8dc0fc2a42ee96719ed9dc6678badcdc2f2ba Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Mon, 4 Sep 2023 16:36:45 +0800 Subject: [PATCH 1/8] =?UTF-8?q?1=E3=80=81add=20support=20xiuos=20platform?= =?UTF-8?q?=20for=20ota=202=E3=80=81change=20Kconfig=20for=20mqtt=20and=20?= =?UTF-8?q?Modify=20variable=20name=203=E3=80=81change=20ota=20functions?= =?UTF-8?q?=20for=20support=20xiuos=20platform?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Framework/connection/adapter_agent.c | 2 +- APP_Framework/lib/Makefile | 4 +- APP_Framework/lib/mqtt/Kconfig | 80 +++-- APP_Framework/lib/mqtt/Makefile | 8 +- APP_Framework/lib/mqtt/platform_mqtt.c | 33 +- APP_Framework/lib/mqtt/platform_mqtt.h | 1 - Ubiquitous/XiZi_IIoT/path_kernel.mk | 2 +- Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig | 2 +- .../XiZi_IIoT/tool/bootloader/ota/Kconfig | 0 .../XiZi_IIoT/tool/bootloader/ota/ota.c | 285 +++++++++++++++--- .../XiZi_IIoT/tool/bootloader/ota/ota.h | 8 +- 11 files changed, 348 insertions(+), 77 deletions(-) delete mode 100644 Ubiquitous/XiZi_IIoT/tool/bootloader/ota/Kconfig diff --git a/APP_Framework/Framework/connection/adapter_agent.c b/APP_Framework/Framework/connection/adapter_agent.c index e9c4dcb98..c54caf946 100755 --- a/APP_Framework/Framework/connection/adapter_agent.c +++ b/APP_Framework/Framework/connection/adapter_agent.c @@ -355,7 +355,7 @@ static int GetCompleteATReply(ATAgentType agent) PrivMutexObtain(&agent->lock); if (agent->receive_mode == ENTM_MODE) { if (agent->entm_recv_len < ENTM_RECV_MAX) { -#ifdef TOOL_USING_MQTT +#ifdef LIB_USING_MQTT if((res == 1) && (agent->entm_recv_len < agent->read_len)) { agent->entm_recv_buf[agent->entm_recv_len] = ch; diff --git a/APP_Framework/lib/Makefile b/APP_Framework/lib/Makefile index 5877462e4..401fe189e 100644 --- a/APP_Framework/lib/Makefile +++ b/APP_Framework/lib/Makefile @@ -18,8 +18,8 @@ ifeq ($(CONFIG_LIB_USING_LORAWAN),y) SRC_DIR += lorawan endif -ifeq ($(CONFIG_TOOL_USING_MQTT),y) - SRC_DIR += mqtt +ifeq ($(CONFIG_LIB_USING_MQTT),y) + SRC_DIR += mqtt endif include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/lib/mqtt/Kconfig b/APP_Framework/lib/mqtt/Kconfig index 1084f4f4a..b44bb1b34 100644 --- a/APP_Framework/lib/mqtt/Kconfig +++ b/APP_Framework/lib/mqtt/Kconfig @@ -1,33 +1,71 @@ menu "lib using MQTT" - menuconfig TOOL_USING_MQTT + menuconfig LIB_USING_MQTT bool "Enable support MQTT function" default n select SUPPORT_CONNECTION_FRAMEWORK select CONNECTION_ADAPTER_4G - - if TOOL_USING_MQTT - menu "MQTT connection parameter configuration." - config PLATFORM_PRODUCTKEY - string "Product Key, used to identify a product." - default "iv74vebCdJC" - config CLIENT_DEVICENAME - string "Device name, used to identify a client device." - default "D001" + if LIB_USING_MQTT + choice + prompt "Choose xiuos platform or Alibaba Cloud platform." + default ALIBABA_PLATFORM - config CLIENT_DEVICESECRET - string "Device secret, used for device authentication and data encryption." - default "d2e613c4f714b6b0774bd7b68eeceae3" - - config PLATFORM_SERVERIP - string "mqtt platform server ip." - default "101.133.196.127" + config XIUOS_PLATFORM + bool "xiuos platform." - config PLATFORM_SERVERPORT - string "mqtt platform server port." - default "1883" - endmenu + config ALIBABA_PLATFORM + bool "Alibaba cloud platform." + endchoice + + if XIUOS_PLATFORM + menu "xiuos platform mqtt connection parameter configuration." + + config CLIENTID + string "mqtt client id." + default "xidatong0001" + + config USERNAME + string "mqtt client username." + default "xiuosiot" + + config PASSWORD + string "mqtt client login passwd." + default "xiuosiot" + + config PLATFORM_SERVERIP + string "mqtt platform server ip." + default "115.238.53.59" + + config PLATFORM_SERVERPORT + string "mqtt platform server port." + default "1883" + endmenu + endif + + if ALIBABA_PLATFORM + menu "Alibaba Cloud platform mqtt connection parameter configuration." + config PLATFORM_PRODUCTKEY + string "Product Key, used to identify a product." + default "iv74vebCdJC" + + config CLIENT_DEVICENAME + string "Device name, used to identify a client device." + default "D001" + + config CLIENT_DEVICESECRET + string "Device secret, used for device authentication and data encryption." + default "d2e613c4f714b6b0774bd7b68eeceae3" + + config PLATFORM_SERVERIP + string "mqtt platform server ip." + default "101.133.196.127" + + config PLATFORM_SERVERPORT + string "mqtt platform server port." + default "1883" + endmenu + endif endif endmenu diff --git a/APP_Framework/lib/mqtt/Makefile b/APP_Framework/lib/mqtt/Makefile index b924f47d1..77fa83793 100644 --- a/APP_Framework/lib/mqtt/Makefile +++ b/APP_Framework/lib/mqtt/Makefile @@ -1,3 +1,9 @@ -SRC_FILES := platform_mqtt.c utils_hmacsha1.c +ifeq ($(CONFIG_XIUOS_PLATFORM),y) + SRC_FILES := platform_mqtt.c +endif + +ifeq ($(CONFIG_ALIBABA_PLATFORM),y) + SRC_FILES := platform_mqtt.c utils_hmacsha1.c +endif include $(KERNEL_ROOT)/compiler.mk \ No newline at end of file diff --git a/APP_Framework/lib/mqtt/platform_mqtt.c b/APP_Framework/lib/mqtt/platform_mqtt.c index 91689d280..29870c2e9 100644 --- a/APP_Framework/lib/mqtt/platform_mqtt.c +++ b/APP_Framework/lib/mqtt/platform_mqtt.c @@ -24,6 +24,7 @@ #include #include #include "platform_mqtt.h" +#include "utils_hmacsha1.h" MQTT_TCB Platform_mqtt; //创建一个用于连接云平台mqtt的结构体 static struct Adapter *adapter; @@ -110,14 +111,21 @@ int MQTT_Recv(uint8_t* buf, int buflen) int MQTT_Connect(void) { uint8_t TryConnect_time = 10; //尝试登录次数 - uint8_t passwdtemp[PASSWARD_SIZE]; - - memset(&Platform_mqtt,0,sizeof(Platform_mqtt)); - sprintf(Platform_mqtt.ClientID,"%s|securemode=3,signmethod=hmacsha1|",CLIENT_DEVICENAME); //构建客户端ID并存入缓冲区 - sprintf(Platform_mqtt.Username,"%s&%s",CLIENT_DEVICENAME,PLATFORM_PRODUCTKEY); //构建用户名并存入缓冲区 - memset(passwdtemp,0,sizeof(passwdtemp)); - sprintf(passwdtemp,"clientId%sdeviceName%sproductKey%s",CLIENT_DEVICENAME,CLIENT_DEVICENAME,PLATFORM_PRODUCTKEY); //构建加密时的明文 - utils_hmac_sha1(passwdtemp,strlen(passwdtemp),Platform_mqtt.Passward,(char *)CLIENT_DEVICESECRET,strlen(CLIENT_DEVICESECRET)); //以DeviceSecret为秘钥对temp中的明文进行hmacsha1加密即为密码 + + memset(&Platform_mqtt,0,sizeof(Platform_mqtt)); +#ifdef XIUOS_PLATFORM + sprintf(Platform_mqtt.ClientID,"%s",CLIENTID); //客户端ID存入缓冲区 + sprintf(Platform_mqtt.Username,"%s",USERNAME); //用户名存入缓冲区 + sprintf(Platform_mqtt.Passward,"%s",PASSWORD); //用户名存入缓冲区 +#endif +#ifdef ALIBABA_PLATFORM + uint8_t passwdtemp[PASSWARD_SIZE]; + memset(passwdtemp,0,sizeof(passwdtemp)); + sprintf(Platform_mqtt.ClientID,"%s|securemode=3,signmethod=hmacsha1|",CLIENT_DEVICENAME); //构建客户端ID并存入缓冲区 + sprintf(Platform_mqtt.Username,"%s&%s",CLIENT_DEVICENAME,PLATFORM_PRODUCTKEY); //构建用户名并存入缓冲区 + sprintf(passwdtemp,"clientId%sdeviceName%sproductKey%s",CLIENT_DEVICENAME,CLIENT_DEVICENAME,PLATFORM_PRODUCTKEY); //构建加密时的明文 + utils_hmac_sha1(passwdtemp,strlen(passwdtemp),Platform_mqtt.Passward,(char *)CLIENT_DEVICESECRET,strlen(CLIENT_DEVICESECRET)); //以DeviceSecret为秘钥对temp中的明文进行hmacsha1加密即为密码 +#endif Platform_mqtt.MessageID = 0; //报文标识符清零,CONNECT报文虽然不需要添加报文标识符,但是CONNECT报文是第一个发送的报文,在此清零报文标识符为后续报文做准备 Platform_mqtt.Fixed_len = 1; //CONNECT报文固定报头长度暂定为1 @@ -126,7 +134,7 @@ int MQTT_Connect(void) Platform_mqtt.Remaining_len = Platform_mqtt.Variable_len + Platform_mqtt.Payload_len; //剩余长度=可变报头长度+负载长度 memset(Platform_mqtt.Pack_buff,0,sizeof(Platform_mqtt.Pack_buff)); - Platform_mqtt.Pack_buff[0] = 0x10; //CONNECT报文 固定报头第1个字节0x10 + Platform_mqtt.Pack_buff[0] = 0x10; //CONNECT报文,固定报头第1个字节0x10 do{ if((Platform_mqtt.Remaining_len/128) == 0) { @@ -281,7 +289,7 @@ int MQTT_UnSubscribeTopic(uint8_t *topic_name) Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+0] = Platform_mqtt.MessageID/256; //报文标识符高字节 Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+1] = Platform_mqtt.MessageID%256; //报文标识符低字节 - Platform_mqtt.MessageID++; //每用一次MessageID加1 + Platform_mqtt.MessageID++; //每用一次MessageID加1 Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2] = strlen(topic_name)/256; //主题长度高字节 Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+3] = strlen(topic_name)%256; //主题长度低字节 @@ -377,7 +385,7 @@ void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len) Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+2+strlen(topic_name)] = Platform_mqtt.MessageID/256; //报文标识符高字节 Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+3+strlen(topic_name)] = Platform_mqtt.MessageID%256; //报文标识符低字节 - Platform_mqtt.MessageID++; //每用一次MessageID加1 + Platform_mqtt.MessageID++; //每用一次MessageID加1 memcpy(&Platform_mqtt.Pack_buff[Platform_mqtt.Fixed_len+4+strlen(topic_name)],data,strlen(data)); //复制data数据 @@ -423,11 +431,14 @@ void MQTT_DealPublishData(uint8_t *data, uint16_t data_len) for(i = 1;i < 5;i++) { + //查找可变报头的长度字段,如果最高位为0表示该字节是长度字段的最后一个字节 if((data[i] & 0x80) == 0) break; } + //1代表固定报头占一个字节,i代表可变报头长度字段所占用字节数,2代表主题长度字段占2字节,cmdpos代表报文里主题名称起始位置 cmdpos = 1+i+2; + //data_len减去1+i+2就是报文中有效载荷的长度 cmdlen = data_len-(1+i+2); if(data_len <= CMD_SIZE) diff --git a/APP_Framework/lib/mqtt/platform_mqtt.h b/APP_Framework/lib/mqtt/platform_mqtt.h index 92333e9b1..c08ba3447 100644 --- a/APP_Framework/lib/mqtt/platform_mqtt.h +++ b/APP_Framework/lib/mqtt/platform_mqtt.h @@ -23,7 +23,6 @@ #define _PLATFORM_MQTT_H_ #include -#include "utils_hmacsha1.h" #define KEEPALIVE_TIME 300 //保活时间(单位s),300s #define HEART_TIME 120000 //空闲时发送心跳包的时间间隔(单位ms),120s diff --git a/Ubiquitous/XiZi_IIoT/path_kernel.mk b/Ubiquitous/XiZi_IIoT/path_kernel.mk index 2c3ec451d..f95250128 100755 --- a/Ubiquitous/XiZi_IIoT/path_kernel.mk +++ b/Ubiquitous/XiZi_IIoT/path_kernel.mk @@ -581,7 +581,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 diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig b/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig index c826295d4..9b694513b 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig @@ -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." diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/Kconfig b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/Kconfig deleted file mode 100644 index e69de29bb..000000000 diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index e132346e3..236d8711d 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -128,7 +128,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) { @@ -739,8 +739,224 @@ SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHE #define FRAME_LEN 2048 //每帧数据的数据包长度 static uint8_t MqttRxbuf[3072]; static uint8_t FrameBuf[FRAME_LEN]; -static OTA_TCB AliOTA; +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); + + 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)); +} + +/******************************************************************************* +* 函 数 名: app_ota_by_platform +* 功能描述: 通过云平台MQTT进行升级 +* 形 参: 无 +* 返 回 值: 无 +*******************************************************************************/ +static void app_ota_by_platform(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][64]; + char *ptr; + + 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() == 0) && MQTT_SubscribeTopic(topicdatabuff[0]) == 0 && MQTT_SubscribeTopic(topicdatabuff[1]) == 0) + { + KPrintf("Log in to the cloud platform and subscribe to the topic successfully.\n"); + PropertyVersion(); + } + else + { + KPrintf("Log in to the cloud platform failed, retry!\n"); + goto reconnect; + } + + while(1) + { + memset(MqttRxbuf,0,sizeof(MqttRxbuf)); + datalen = MQTT_Recv(MqttRxbuf, sizeof(MqttRxbuf)); + if(datalen <= 0) + { + freecnt++; + } + else if(MqttRxbuf[0] == 0x30) + { + freecnt = 0; + MQTT_DealPublishData(MqttRxbuf, datalen); + ptr = strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[0]); + if(ptr != NULL) + { + if(sscanf(ptr+strlen(topicdatabuff[0])+1,"{\"fileSize\":%d,\"version\":\"%11s\",\"fileId\":%d,\"md5\":\"%*32s\"}",&platform_ota.size,platform_ota.version,&platform_ota.streamId)==3) + { + KPrintf("ota file size:%d\r\n",platform_ota.size); + KPrintf("ota file id:%d\r\n",platform_ota.streamId); + KPrintf("ota file version:%s\r\n",platform_ota.version); + if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,platform_ota.size) != kStatus_Success) + { + KPrintf("Failed to erase target fash!\n"); + ret = -1; + 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; + break; + } + } + + 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; + 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; + } + } + + } + else + { + 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 = 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); + } + 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); + } + MQTT_UnSubscribeTopic(topicdatabuff[0]); + MQTT_UnSubscribeTopic(topicdatabuff[1]); + MQTT_Disconnect(); + mcuboot.flash_deinit(); + KPrintf("ota file transfer complete,start reboot!\n"); + MdelayKTask(2000); + mcuboot.op_reset(); +} +#endif + +#ifdef ALIBABA_PLATFORM /******************************************************************************* * 函 数 名: PropertyVersion * 功能描述: 向服务器上传当前设备版本信息 @@ -768,11 +984,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,7 +997,7 @@ 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)); } @@ -838,21 +1054,21 @@ reconnect: 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("ota file size:%d\r\n",platform_ota.size); + KPrintf("ota file id:%d\r\n",platform_ota.streamId); + KPrintf("ota file version:%s\r\n",platform_ota.version); + if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,platform_ota.size) != kStatus_Success) { KPrintf("Failed to erase target fash!\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 { @@ -865,37 +1081,37 @@ reconnect: 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; 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); //发送要下载的数据信息给服务器 } } @@ -929,11 +1145,11 @@ reconnect: 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)); @@ -959,6 +1175,7 @@ reconnect: MdelayKTask(2000); mcuboot.op_reset(); } +#endif int OtaTask(void) { diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h index 48880a85c..551669d72 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h @@ -25,10 +25,6 @@ #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升级 @@ -62,6 +58,10 @@ typedef struct { #ifdef OTA_BY_TCPSERVER +#define STARTFLAG 0x1A2B //数据帧开始信号标记 +#define DATAFLAG 0x3C4D //数据帧数据信号标记 +#define ENDTFLAG 0x5E6F //数据帧结束信号标记 +#define LENGTH 1024 //每帧数据的数据包长度 /*bin包传输过程中的数据帧相关的结构体*/ typedef struct { From 117789121e3929c9cf04da0ad8c76c7cbfb45919 Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Thu, 7 Sep 2023 17:04:15 +0800 Subject: [PATCH 2/8] Optimize ota transfer function --- .../Framework/connection/4g/ec200t/ec200t.c | 2 +- .../XiZi_IIoT/tool/bootloader/ota/ota.c | 34 ++++++++++++------- 2 files changed, 23 insertions(+), 13 deletions(-) diff --git a/APP_Framework/Framework/connection/4g/ec200t/ec200t.c b/APP_Framework/Framework/connection/4g/ec200t/ec200t.c index 92f687303..c916f36f1 100644 --- a/APP_Framework/Framework/connection/4g/ec200t/ec200t.c +++ b/APP_Framework/Framework/connection/4g/ec200t/ec200t.c @@ -167,7 +167,7 @@ static int Ec200tIoctl(struct Adapter *adapter, int cmd, void *args) serial_cfg.serial_timeout = OTA_RX_TIMEOUT; #else //serial receive timeout 10s - serial_cfg.serial_timeout = 100000; + serial_cfg.serial_timeout = 10000; #endif serial_cfg.is_ext_uart = 0; #ifdef ADAPTER_EC200T_DRIVER_EXT_PORT diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index 236d8711d..a5105b345 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -852,6 +852,10 @@ reconnect: { 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; } } @@ -864,6 +868,10 @@ reconnect: { 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 @@ -938,19 +946,16 @@ reconnect: strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message)); UpdateOTAFlag(&ota_info); - } + KPrintf("ota file transfer complete,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("ota file transfer failed,start reboot!\n"); } MQTT_UnSubscribeTopic(topicdatabuff[0]); MQTT_UnSubscribeTopic(topicdatabuff[1]); MQTT_Disconnect(); mcuboot.flash_deinit(); - KPrintf("ota file transfer complete,start reboot!\n"); MdelayKTask(2000); mcuboot.op_reset(); } @@ -1074,6 +1079,10 @@ reconnect: { 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; } } @@ -1086,6 +1095,10 @@ reconnect: { 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 @@ -1160,18 +1173,15 @@ reconnect: strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message)); UpdateOTAFlag(&ota_info); - } + KPrintf("ota file transfer complete,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("ota 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(); } From 5da9a9c4aef8afb00d1c0b2b812fb48bfb41db25 Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Fri, 8 Sep 2023 12:08:16 +0800 Subject: [PATCH 3/8] =?UTF-8?q?1=E3=80=81Optimize=20transmission=20string?= =?UTF-8?q?=20matching=20rules=202=E3=80=81change=20Kconfig=20for=20mqtt?= =?UTF-8?q?=20and=20ota?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APP_Framework/lib/mqtt/Kconfig | 10 ++--- Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig | 2 +- .../XiZi_IIoT/tool/bootloader/ota/ota.c | 37 ++++++++++--------- 3 files changed, 26 insertions(+), 23 deletions(-) diff --git a/APP_Framework/lib/mqtt/Kconfig b/APP_Framework/lib/mqtt/Kconfig index b44bb1b34..7c0076fee 100644 --- a/APP_Framework/lib/mqtt/Kconfig +++ b/APP_Framework/lib/mqtt/Kconfig @@ -9,7 +9,7 @@ menu "lib using MQTT" if LIB_USING_MQTT choice prompt "Choose xiuos platform or Alibaba Cloud platform." - default ALIBABA_PLATFORM + default XIUOS_PLATFORM config XIUOS_PLATFORM bool "xiuos platform." @@ -34,11 +34,11 @@ menu "lib using MQTT" default "xiuosiot" config PLATFORM_SERVERIP - string "mqtt platform server ip." + string "xiuos platform server ip." default "115.238.53.59" config PLATFORM_SERVERPORT - string "mqtt platform server port." + string "xiuos platform server port." default "1883" endmenu endif @@ -58,11 +58,11 @@ menu "lib using MQTT" default "d2e613c4f714b6b0774bd7b68eeceae3" config PLATFORM_SERVERIP - string "mqtt platform server ip." + string "Alibaba Cloud platform server ip." default "101.133.196.127" config PLATFORM_SERVERPORT - string "mqtt platform server port." + string "Alibaba Cloud platform server port." default "1883" endmenu endif diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig b/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig index 9b694513b..8494dfd9c 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig @@ -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." diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index a5105b345..5d95c08ed 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -726,7 +726,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(); } @@ -758,6 +758,7 @@ static void PropertyVersion(void) 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报文 } @@ -794,7 +795,7 @@ static void app_ota_by_platform(void* parameter) uint32_t heart_time = 0; uint32_t flashdestination = DOWN_FLAH_ADDRESS; uint8_t topicdatabuff[2][64]; - char *ptr; + char *ptr1, *ptr2; mcuboot.flash_init(); memset(&ota_info, 0, sizeof(ota_info_t)); @@ -829,14 +830,15 @@ reconnect: { freecnt = 0; MQTT_DealPublishData(MqttRxbuf, datalen); - ptr = strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[0]); - if(ptr != NULL) + ptr1 = strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[0]); + ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":"); + if((ptr1 != NULL) &&(ptr2 != NULL)) { - if(sscanf(ptr+strlen(topicdatabuff[0])+1,"{\"fileSize\":%d,\"version\":\"%11s\",\"fileId\":%d,\"md5\":\"%*32s\"}",&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("ota file size:%d\r\n",platform_ota.size); - KPrintf("ota file id:%d\r\n",platform_ota.streamId); - KPrintf("ota file version:%s\r\n",platform_ota.version); + 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(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,platform_ota.size) != kStatus_Success) { KPrintf("Failed to erase target fash!\n"); @@ -877,7 +879,7 @@ reconnect: 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); + KPrintf("current progress is %d/%d.\r\n",platform_ota.num,platform_ota.counter); flashdestination += platform_ota.downlen; platform_ota.num++; } @@ -946,11 +948,11 @@ reconnect: strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message)); UpdateOTAFlag(&ota_info); - KPrintf("ota file transfer complete,start reboot!\n"); + KPrintf("firmware file transfer successful,start reboot!\n"); } else { - KPrintf("ota file transfer failed,start reboot!\n"); + KPrintf("firmware file transfer failed,start reboot!\n"); } MQTT_UnSubscribeTopic(topicdatabuff[0]); MQTT_UnSubscribeTopic(topicdatabuff[1]); @@ -982,6 +984,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报文 } @@ -1061,9 +1064,9 @@ reconnect: { 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",platform_ota.size); - KPrintf("ota file id:%d\r\n",platform_ota.streamId); - KPrintf("ota file version:%s\r\n",platform_ota.version); + 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(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,platform_ota.size) != kStatus_Success) { KPrintf("Failed to erase target fash!\n"); @@ -1104,7 +1107,7 @@ reconnect: 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); + KPrintf("current progress is %d/%d.\r\n",platform_ota.num,platform_ota.counter); flashdestination += platform_ota.downlen; platform_ota.num++; } @@ -1173,11 +1176,11 @@ reconnect: strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message)); UpdateOTAFlag(&ota_info); - KPrintf("ota file transfer complete,start reboot!\n"); + KPrintf("firmware file transfer successful,start reboot!\n"); } else { - KPrintf("ota file transfer failed,start reboot!\n"); + KPrintf("firmware file transfer failed,start reboot!\n"); } MQTT_UnSubscribeTopic(topicdatabuff); MQTT_Disconnect(); From e61ffc45892eb30c4360d33a94afd94d8f8c90ee Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Tue, 12 Sep 2023 09:32:14 +0800 Subject: [PATCH 4/8] Optimize ota transfer function --- APP_Framework/lib/mqtt/platform_mqtt.h | 2 +- .../XiZi_IIoT/tool/bootloader/ota/ota.c | 52 +++++++++---------- 2 files changed, 25 insertions(+), 29 deletions(-) diff --git a/APP_Framework/lib/mqtt/platform_mqtt.h b/APP_Framework/lib/mqtt/platform_mqtt.h index c08ba3447..c8c758f85 100644 --- a/APP_Framework/lib/mqtt/platform_mqtt.h +++ b/APP_Framework/lib/mqtt/platform_mqtt.h @@ -25,7 +25,7 @@ #include #define KEEPALIVE_TIME 300 //保活时间(单位s),300s -#define HEART_TIME 120000 //空闲时发送心跳包的时间间隔(单位ms),120s +#define HEART_TIME 200000 //空闲时发送心跳包的时间间隔(单位ms),200s #define PACK_SIZE 512 //存放报文数据缓冲区大小 #define CMD_SIZE 3072 //保存推送的PUBLISH报文中的数据缓冲区大小 #define CLIENTID_SIZE 64 //存放客户端ID的缓冲区大小 diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index 5d95c08ed..658923efc 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -824,7 +824,18 @@ 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() != 0) //发送心跳包失败可能连接断开,需要重连 + { + KPrintf("The connection has been disconnected, reconnecting!\n"); + goto reconnect; + } + KPrintf("Send heartbeat packet successful!\n"); + } } else if(MqttRxbuf[0] == 0x30) { @@ -916,19 +927,6 @@ 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) @@ -1053,7 +1051,18 @@ 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() != 0) //发送心跳包失败可能连接断开,需要重连 + { + KPrintf("The connection has been disconnected, reconnecting!\n"); + goto reconnect; + } + KPrintf("Send heartbeat packet successful!\n"); + } } else if(MqttRxbuf[0] == 0x30) { @@ -1144,19 +1153,6 @@ 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) From 474557a5acde75788dbeeae3d9c969dd22630b44 Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Wed, 13 Sep 2023 10:15:50 +0800 Subject: [PATCH 5/8] =?UTF-8?q?1=E3=80=81Optimize=20ota=20Bootloader=20fun?= =?UTF-8?q?ction=202=E3=80=81add=20reboot=20after=20flash=20copy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../third_party_driver/common/flash.c | 35 ++- .../XiZi_IIoT/tool/bootloader/ota/ota.c | 205 +++++++++++------- 2 files changed, 144 insertions(+), 96 deletions(-) diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c index a1d77d816..c032160de 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/common/flash.c @@ -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=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; } diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index 658923efc..7e349b7c3 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -19,6 +19,7 @@ * */ #include +#include #include #include "shell.h" #include "xsconfig.h" @@ -42,7 +43,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); @@ -226,30 +227,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 +268,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 +281,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 +416,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 +456,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 +516,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); @@ -863,13 +914,13 @@ reconnect: } 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; + 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; } } @@ -1089,13 +1140,13 @@ reconnect: } 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; + 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; } } @@ -1234,14 +1285,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) From 5ef61d14b12c081748d362522f16b803f770dd06 Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Thu, 14 Sep 2023 17:13:34 +0800 Subject: [PATCH 6/8] =?UTF-8?q?1=E3=80=81Verify=20file=20size=20before=20o?= =?UTF-8?q?ta=20file=20transfer=202=E3=80=81update=20MQTT=20connection=20p?= =?UTF-8?q?arameter=20configuration?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APP_Framework/lib/mqtt/Kconfig | 2 +- .../XiZi_IIoT/tool/bootloader/ota/ota.c | 39 ++++++++++- .../XiZi_IIoT/tool/bootloader/ota/ota.h | 70 +++++++++---------- 3 files changed, 73 insertions(+), 38 deletions(-) diff --git a/APP_Framework/lib/mqtt/Kconfig b/APP_Framework/lib/mqtt/Kconfig index 7c0076fee..a93a57221 100644 --- a/APP_Framework/lib/mqtt/Kconfig +++ b/APP_Framework/lib/mqtt/Kconfig @@ -23,7 +23,7 @@ menu "lib using MQTT" config CLIENTID string "mqtt client id." - default "xidatong0001" + default "D001" config USERNAME string "mqtt client username." diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index 7e349b7c3..005fd5902 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -578,6 +578,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"); @@ -896,15 +901,30 @@ reconnect: ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":"); 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) { 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 target fash!\n"); + 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); @@ -924,6 +944,7 @@ reconnect: } } + // 2.分片接收新版本固件 if(strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[1])) { memset(FrameBuf,0,sizeof(FrameBuf)); @@ -980,6 +1001,7 @@ reconnect: } } + // 3.新版本固件接收完毕,写入描述信息 if(0 == ret) { ota_info.down.size = platform_ota.size; @@ -1122,14 +1144,25 @@ reconnect: ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\""); 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) { 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 target fash!\n"); + KPrintf("Failed to erase download partition!\n"); ret = -1; break; } @@ -1150,6 +1183,7 @@ reconnect: } } + // 2.分片接收新版本固件 if(strstr((char *)Platform_mqtt.cmdbuff,"download_reply")) { memset(FrameBuf,0,sizeof(FrameBuf)); @@ -1206,6 +1240,7 @@ reconnect: } } + // 3.新版本固件接收完毕,写入描述信息 if(0 == ret) { ota_info.down.size = platform_ota.size; diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h index 551669d72..7a3c816db 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h @@ -27,55 +27,55 @@ #define JUMP_SUCCESS_FLAG 0XCDCDCDCD 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 1024 //每帧数据的数据包长度 +#define STARTFLAG 0x1A2B //数据帧开始信号标记 +#define DATAFLAG 0x3C4D //数据帧数据信号标记 +#define ENDTFLAG 0x5E6F //数据帧结束信号标记 +#define LENGTH 1024 //每帧数据的数据包长度 /*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 +87,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); From 60a7835ef8694e3d23dd5328d1965ff834b170c8 Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Tue, 19 Sep 2023 09:22:26 +0800 Subject: [PATCH 7/8] Modify the return value of the mqtt function --- APP_Framework/lib/mqtt/platform_mqtt.c | 32 +++++++++---------- APP_Framework/lib/mqtt/platform_mqtt.h | 9 +++--- .../XiZi_IIoT/tool/bootloader/ota/ota.c | 22 +++++++------ 3 files changed, 33 insertions(+), 30 deletions(-) diff --git a/APP_Framework/lib/mqtt/platform_mqtt.c b/APP_Framework/lib/mqtt/platform_mqtt.c index 29870c2e9..d6906b243 100644 --- a/APP_Framework/lib/mqtt/platform_mqtt.c +++ b/APP_Framework/lib/mqtt/platform_mqtt.c @@ -106,9 +106,9 @@ int MQTT_Recv(uint8_t* buf, int buflen) * 函 数 名: MQTT_Connect * 功能描述: 登录MQTT服务器 * 形 参: 无 -* 返 回 值: 0表示成功,1表示失败 +* 返 回 值: true表示成功,false表示失败 *******************************************************************************/ -int MQTT_Connect(void) +bool MQTT_Connect(void) { uint8_t TryConnect_time = 10; //尝试登录次数 @@ -180,11 +180,11 @@ int MQTT_Connect(void) MQTT_Recv(mqtt_rxbuf, 4); if(mqtt_rxbuf[0] == parket_connetAck[0] && mqtt_rxbuf[1] == parket_connetAck[1]) //连接成功 { - return 0; + return true; } TryConnect_time--; } - return 1; + return false; } @@ -204,9 +204,9 @@ void MQTT_Disconnect(void) * 函 数 名: MQTT_SubscribeTopic * 功能描述: MQTT订阅单个主题 * 形 参: topic_name:要订阅的主题 -* 返 回 值: 0表示订阅成功,1表示订阅失败 +* 返 回 值: true表示订阅成功,false表示订阅失败 *******************************************************************************/ -int MQTT_SubscribeTopic(uint8_t *topic_name) +bool MQTT_SubscribeTopic(uint8_t *topic_name) { uint8_t TrySub_time = 10; //尝试订阅次数 @@ -249,11 +249,11 @@ int MQTT_SubscribeTopic(uint8_t *topic_name) MQTT_Recv(mqtt_rxbuf, 5); if(mqtt_rxbuf[0] == parket_subAck[0] && mqtt_rxbuf[1] == parket_subAck[1]) //订阅成功 { - return 0; + return true; } TrySub_time--; } - return 1; + return false; } @@ -261,9 +261,9 @@ int MQTT_SubscribeTopic(uint8_t *topic_name) * 函 数 名: MQTT_UnSubscribeTopic * 功能描述: MQTT取消订阅单个主题 * 形 参: topic_name:要取消订阅的主题 -* 返 回 值: 0表示订阅成功,1表示订阅失败 +* 返 回 值: true表示取消订阅成功,false表示取消订阅失败 *******************************************************************************/ -int MQTT_UnSubscribeTopic(uint8_t *topic_name) +bool MQTT_UnSubscribeTopic(uint8_t *topic_name) { uint8_t TryUnSub_time = 10; //尝试取消订阅次数 @@ -303,11 +303,11 @@ int MQTT_UnSubscribeTopic(uint8_t *topic_name) MQTT_Recv(mqtt_rxbuf, 4); if(mqtt_rxbuf[0] == parket_unsubAck[0] && mqtt_rxbuf[1] == parket_unsubAck[1]) //取消订阅成功 { - return 0; + return true; } TryUnSub_time--; } - return 1; + return false; } @@ -397,9 +397,9 @@ void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len) * 函 数 名: MQTT_SendHeart * 功能描述: 发送心跳保活包 * 形 参: 无 -* 返 回 值: 0表示发送成功,其他值表示发送失败 +* 返 回 值: true表示发送成功,false表示发送失败 *******************************************************************************/ -int MQTT_SendHeart(void) +bool MQTT_SendHeart(void) { uint8_t TrySentHeart_time = 10; //尝试发送心跳保活次数 while(TrySentHeart_time > 0) @@ -410,11 +410,11 @@ int MQTT_SendHeart(void) MQTT_Recv(mqtt_rxbuf, 2); if(mqtt_rxbuf[0] == 0xD0 && mqtt_rxbuf[1] == 0x00) { - return 0; + return true; } TrySentHeart_time--; } - return 1; + return false; } diff --git a/APP_Framework/lib/mqtt/platform_mqtt.h b/APP_Framework/lib/mqtt/platform_mqtt.h index c8c758f85..8f8877e85 100644 --- a/APP_Framework/lib/mqtt/platform_mqtt.h +++ b/APP_Framework/lib/mqtt/platform_mqtt.h @@ -23,6 +23,7 @@ #define _PLATFORM_MQTT_H_ #include +#include #define KEEPALIVE_TIME 300 //保活时间(单位s),300s #define HEART_TIME 200000 //空闲时发送心跳包的时间间隔(单位ms),200s @@ -50,12 +51,12 @@ extern MQTT_TCB Platform_mqtt; //外部变量声明 int AdapterNetActive(void); int MQTT_Send(const uint8_t* buf, int buflen); int MQTT_Recv(uint8_t* buf, int buflen); -int MQTT_Connect(void); +bool MQTT_Connect(void); void MQTT_Disconnect(void); -int MQTT_SubscribeTopic(uint8_t *topic_name); -int MQTT_UnSubscribeTopic(uint8_t *topic_name); +bool MQTT_SubscribeTopic(uint8_t *topic_name); +bool MQTT_UnSubscribeTopic(uint8_t *topic_name); void MQTT_PublishDataQs0(uint8_t *topic_name,uint8_t *data, uint16_t data_len); void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len); -int MQTT_SendHeart(void); +bool MQTT_SendHeart(void); void MQTT_DealPublishData(uint8_t *data, uint16_t data_len); #endif \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index 005fd5902..5914ec233 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -863,7 +863,7 @@ static void app_ota_by_platform(void* parameter) sprintf(topicdatabuff[1],"ota/%s/files",CLIENTID); reconnect: - if((AdapterNetActive() == 0) && (MQTT_Connect() == 0) && MQTT_SubscribeTopic(topicdatabuff[0]) == 0 && MQTT_SubscribeTopic(topicdatabuff[1]) == 0) + if((AdapterNetActive() == 0) && MQTT_Connect() && MQTT_SubscribeTopic(topicdatabuff[0]) && MQTT_SubscribeTopic(topicdatabuff[1])) { KPrintf("Log in to the cloud platform and subscribe to the topic successfully.\n"); PropertyVersion(); @@ -885,7 +885,7 @@ reconnect: { heart_time = CalculateTimeMsFromTick(CurrentTicksGain()); freecnt = 0; - if(MQTT_SendHeart() != 0) //发送心跳包失败可能连接断开,需要重连 + if(!MQTT_SendHeart()) //发送心跳包失败可能连接断开,需要重连 { KPrintf("The connection has been disconnected, reconnecting!\n"); goto reconnect; @@ -897,11 +897,12 @@ reconnect: { freecnt = 0; MQTT_DealPublishData(MqttRxbuf, datalen); + + // 1.获取新版本固件大小及版本信息 ptr1 = strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[0]); - ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":"); + ptr2 = strstr((char *)Platform_mqtt.cmdbuff,"{\"fileSize\":"); 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) { KPrintf("------Start the firmware file transfer!------\r\n"); @@ -1001,7 +1002,7 @@ reconnect: } } - // 3.新版本固件接收完毕,写入描述信息 + // 新版本固件接收完毕,写入描述信息 if(0 == ret) { ota_info.down.size = platform_ota.size; @@ -1107,7 +1108,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(); @@ -1129,7 +1130,7 @@ reconnect: { heart_time = CalculateTimeMsFromTick(CurrentTicksGain()); freecnt = 0; - if(MQTT_SendHeart() != 0) //发送心跳包失败可能连接断开,需要重连 + if(!MQTT_SendHeart()) //发送心跳包失败可能连接断开,需要重连 { KPrintf("The connection has been disconnected, reconnecting!\n"); goto reconnect; @@ -1141,10 +1142,11 @@ reconnect: { 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) { - // 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) { KPrintf("------Start the firmware file transfer!------\r\n"); @@ -1240,7 +1242,7 @@ reconnect: } } - // 3.新版本固件接收完毕,写入描述信息 + // 新版本固件接收完毕,写入描述信息 if(0 == ret) { ota_info.down.size = platform_ota.size; From 9fd61d3d2ac2bfb8721adb0c8de55a422db134c1 Mon Sep 17 00:00:00 2001 From: wgzAIIT <820906721@qq.com> Date: Tue, 19 Sep 2023 16:07:24 +0800 Subject: [PATCH 8/8] =?UTF-8?q?1=E3=80=81Optimize=20mqtt=20transfer=20spee?= =?UTF-8?q?d=203k/s=202=E3=80=81add=20json=20file=20to=20sd=20card=20by=20?= =?UTF-8?q?mqtt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- APP_Framework/Framework/connection/at_agent.h | 2 +- APP_Framework/lib/mqtt/Kconfig | 7 ++ APP_Framework/lib/mqtt/platform_mqtt.c | 8 +- APP_Framework/lib/mqtt/platform_mqtt.h | 17 ++-- .../third_party_driver/sdio/connect_sdio.c | 8 +- Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig | 6 +- .../XiZi_IIoT/tool/bootloader/ota/ota.c | 83 +++++++++++++++---- .../XiZi_IIoT/tool/bootloader/ota/ota.h | 9 +- 8 files changed, 104 insertions(+), 36 deletions(-) diff --git a/APP_Framework/Framework/connection/at_agent.h b/APP_Framework/Framework/connection/at_agent.h index 81ed948cf..c2cf283db 100755 --- a/APP_Framework/Framework/connection/at_agent.h +++ b/APP_Framework/Framework/connection/at_agent.h @@ -29,7 +29,7 @@ #define REPLY_TIME_OUT 10 #ifdef TOOL_USING_OTA -#define ENTM_RECV_MAX OTA_RX_BUFFERSIZE +#define ENTM_RECV_MAX (OTA_FRAME_SIZE + 512) #else #define ENTM_RECV_MAX 256 #endif diff --git a/APP_Framework/lib/mqtt/Kconfig b/APP_Framework/lib/mqtt/Kconfig index a93a57221..a4aac23c2 100644 --- a/APP_Framework/lib/mqtt/Kconfig +++ b/APP_Framework/lib/mqtt/Kconfig @@ -41,6 +41,13 @@ menu "lib using MQTT" string "xiuos platform server port." default "1883" endmenu + + menuconfig USING_DOWNLOAD_JSON + bool "Enable support download json file function" + default n + select BSP_USING_SDIO + select MOUNT_SDCARD_FS + select LIB_USING_CJSON endif if ALIBABA_PLATFORM diff --git a/APP_Framework/lib/mqtt/platform_mqtt.c b/APP_Framework/lib/mqtt/platform_mqtt.c index d6906b243..bff14ecff 100644 --- a/APP_Framework/lib/mqtt/platform_mqtt.c +++ b/APP_Framework/lib/mqtt/platform_mqtt.c @@ -422,9 +422,9 @@ bool MQTT_SendHeart(void) * 函 数 名: MQTT_DealPublishData * 功能描述: 处理服务器发来的等级0的推送数据,附带topic信息 * 形 参: redata:接收的数据,data_len:要处理的数据长度 -* 返 回 值: 无 +* 返 回 值: 报文中主题部分+实际负载的长度 *******************************************************************************/ -void MQTT_DealPublishData(uint8_t *data, uint16_t data_len) +uint16_t MQTT_DealPublishData(uint8_t *data, uint16_t data_len) { uint8_t i; uint16_t cmdpos,cmdlen; @@ -438,7 +438,7 @@ void MQTT_DealPublishData(uint8_t *data, uint16_t data_len) //1代表固定报头占一个字节,i代表可变报头长度字段所占用字节数,2代表主题长度字段占2字节,cmdpos代表报文里主题名称起始位置 cmdpos = 1+i+2; - //data_len减去1+i+2就是报文中有效载荷的长度 + //data_len减去1+i+2就是报文中主题部分+实际负载的长度 cmdlen = data_len-(1+i+2); if(data_len <= CMD_SIZE) @@ -446,4 +446,6 @@ void MQTT_DealPublishData(uint8_t *data, uint16_t data_len) memset(Platform_mqtt.cmdbuff, 0, CMD_SIZE); memcpy(Platform_mqtt.cmdbuff, &data[cmdpos], cmdlen); } + + return cmdlen; } diff --git a/APP_Framework/lib/mqtt/platform_mqtt.h b/APP_Framework/lib/mqtt/platform_mqtt.h index 8f8877e85..be02d657d 100644 --- a/APP_Framework/lib/mqtt/platform_mqtt.h +++ b/APP_Framework/lib/mqtt/platform_mqtt.h @@ -25,13 +25,14 @@ #include #include -#define KEEPALIVE_TIME 300 //保活时间(单位s),300s -#define HEART_TIME 200000 //空闲时发送心跳包的时间间隔(单位ms),200s -#define PACK_SIZE 512 //存放报文数据缓冲区大小 -#define CMD_SIZE 3072 //保存推送的PUBLISH报文中的数据缓冲区大小 -#define CLIENTID_SIZE 64 //存放客户端ID的缓冲区大小 -#define USERNAME_SIZE 64 //存放用户名的缓冲区大小 -#define PASSWARD_SIZE 64 //存放密码的缓冲区大小 +#define KEEPALIVE_TIME 300 //保活时间(单位s),300s +#define HEART_TIME 200000 //空闲时发送心跳包的时间间隔(单位ms),200s +#define PACK_SIZE 512 //存放报文数据缓冲区大小 +#define MQTT_FRAME_SIZE 3072 //保存推送的PUBLISH报文中的数据负载大小,最大3k +#define CMD_SIZE (MQTT_FRAME_SIZE + 512) //保存推送的PUBLISH报文中的数据缓冲区大小 +#define CLIENTID_SIZE 64 //存放客户端ID的缓冲区大小 +#define USERNAME_SIZE 64 //存放用户名的缓冲区大小 +#define PASSWARD_SIZE 64 //存放密码的缓冲区大小 typedef struct{ uint8_t ClientID[CLIENTID_SIZE]; //存放客户端ID的缓冲区 @@ -58,5 +59,5 @@ bool MQTT_UnSubscribeTopic(uint8_t *topic_name); void MQTT_PublishDataQs0(uint8_t *topic_name,uint8_t *data, uint16_t data_len); void MQTT_PublishDataQs1(uint8_t *topic_name,uint8_t *data, uint16_t data_len); bool MQTT_SendHeart(void); -void MQTT_DealPublishData(uint8_t *data, uint16_t data_len); +uint16_t MQTT_DealPublishData(uint8_t *data, uint16_t data_len); #endif \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/sdio/connect_sdio.c b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/sdio/connect_sdio.c index 2f3260728..ee7daf13a 100644 --- a/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/sdio/connect_sdio.c +++ b/Ubiquitous/XiZi_IIoT/board/xidatong-arm32/third_party_driver/sdio/connect_sdio.c @@ -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() { diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig b/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig index 8494dfd9c..bfcff7819 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/Kconfig @@ -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 diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index 5914ec233..eb3eb4d8e 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -19,10 +19,10 @@ * */ #include +#include #include #include #include "shell.h" -#include "xsconfig.h" #include "mcuboot.h" #include "ymodem.h" #include "ota.h" @@ -35,6 +35,10 @@ #include "platform_mqtt.h" #endif +#ifdef USING_DOWNLOAD_JSON +#include "cJSON.h" +#endif + /**************************************************************************** * Private Function Prototypes ****************************************************************************/ @@ -790,10 +794,14 @@ 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]; + +#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; @@ -837,12 +845,12 @@ static void OTA_Download(int size, int offset) } /******************************************************************************* -* 函 数 名: 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; @@ -850,8 +858,9 @@ static void app_ota_by_platform(void* parameter) ota_info_t ota_info; uint32_t heart_time = 0; uint32_t flashdestination = DOWN_FLAH_ADDRESS; - uint8_t topicdatabuff[2][64]; + uint8_t topicdatabuff[2][32]; char *ptr1, *ptr2; + uint16_t cmdlen; mcuboot.flash_init(); memset(&ota_info, 0, sizeof(ota_info_t)); @@ -865,7 +874,7 @@ static void app_ota_by_platform(void* parameter) 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 topic successfully.\n"); + KPrintf("Log in to the cloud platform and subscribe to the ota topic successfully.\n"); PropertyVersion(); } else @@ -874,6 +883,17 @@ reconnect: 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)); @@ -896,7 +916,7 @@ reconnect: else if(MqttRxbuf[0] == 0x30) { freecnt = 0; - MQTT_DealPublishData(MqttRxbuf, datalen); + cmdlen = MQTT_DealPublishData(MqttRxbuf, datalen); // 1.获取新版本固件大小及版本信息 ptr1 = strstr((char *)Platform_mqtt.cmdbuff,topicdatabuff[0]); @@ -993,7 +1013,40 @@ reconnect: 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 { @@ -1083,12 +1136,12 @@ static void OTA_Download(int size, int offset) } /******************************************************************************* -* 函 数 名: 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; @@ -1277,9 +1330,9 @@ reconnect: 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; } diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h index 7a3c816db..dceb32266 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.h @@ -22,6 +22,7 @@ #define __OTA_DEF_H__ #include "flash_ops.h" +#include "xsconfig.h" #define JUMP_FAILED_FLAG 0XABABABAB #define JUMP_SUCCESS_FLAG 0XCDCDCDCD @@ -58,10 +59,10 @@ typedef struct { #ifdef OTA_BY_TCPSERVER -#define STARTFLAG 0x1A2B //数据帧开始信号标记 -#define DATAFLAG 0x3C4D //数据帧数据信号标记 -#define ENDTFLAG 0x5E6F //数据帧结束信号标记 -#define LENGTH 1024 //每帧数据的数据包长度 +#define STARTFLAG 0x1A2B //数据帧开始信号标记 +#define DATAFLAG 0x3C4D //数据帧数据信号标记 +#define ENDTFLAG 0x5E6F //数据帧结束信号标记 +#define LENGTH OTA_FRAME_SIZE //每帧数据的数据包长度 /*bin包传输过程中的数据帧相关的结构体*/ typedef struct {