forked from xuos/xiuos
				
			1、add support xiuos platform for ota
2、change Kconfig for mqtt and Modify variable name 3、change ota functions for support xiuos platform
This commit is contained in:
		
							parent
							
								
									e33bd9dd78
								
							
						
					
					
						commit
						bad8dc0fc2
					
				|  | @ -355,7 +355,7 @@ static int GetCompleteATReply(ATAgentType agent) | ||||||
|         PrivMutexObtain(&agent->lock); |         PrivMutexObtain(&agent->lock); | ||||||
|         if (agent->receive_mode == ENTM_MODE) { |         if (agent->receive_mode == ENTM_MODE) { | ||||||
|             if (agent->entm_recv_len < ENTM_RECV_MAX) { |             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))  |                 if((res == 1) && (agent->entm_recv_len < agent->read_len))  | ||||||
|                 { |                 { | ||||||
|                     agent->entm_recv_buf[agent->entm_recv_len] = ch; |                     agent->entm_recv_buf[agent->entm_recv_len] = ch; | ||||||
|  |  | ||||||
|  | @ -18,7 +18,7 @@ ifeq ($(CONFIG_LIB_USING_LORAWAN),y) | ||||||
| 	SRC_DIR += lorawan | 	SRC_DIR += lorawan | ||||||
| endif | endif | ||||||
| 
 | 
 | ||||||
| ifeq ($(CONFIG_TOOL_USING_MQTT),y) | ifeq ($(CONFIG_LIB_USING_MQTT),y) | ||||||
|     SRC_DIR += mqtt |     SRC_DIR += mqtt | ||||||
| endif | endif | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -1,13 +1,50 @@ | ||||||
| menu "lib using MQTT" | menu "lib using MQTT" | ||||||
| 
 | 
 | ||||||
|     menuconfig TOOL_USING_MQTT |     menuconfig LIB_USING_MQTT | ||||||
|         bool "Enable support MQTT function" |         bool "Enable support MQTT function" | ||||||
|         default n |         default n | ||||||
|         select SUPPORT_CONNECTION_FRAMEWORK |         select SUPPORT_CONNECTION_FRAMEWORK | ||||||
|         select CONNECTION_ADAPTER_4G |         select CONNECTION_ADAPTER_4G | ||||||
| 
 | 
 | ||||||
|     if TOOL_USING_MQTT |     if LIB_USING_MQTT | ||||||
|         menu "MQTT connection parameter configuration." |         choice  | ||||||
|  |             prompt "Choose xiuos platform or Alibaba Cloud platform." | ||||||
|  |             default ALIBABA_PLATFORM | ||||||
|  | 
 | ||||||
|  |             config XIUOS_PLATFORM | ||||||
|  |                 bool "xiuos platform." | ||||||
|  | 
 | ||||||
|  |             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 |                 config PLATFORM_PRODUCTKEY | ||||||
|                     string "Product Key, used to identify a product." |                     string "Product Key, used to identify a product." | ||||||
|                     default "iv74vebCdJC" |                     default "iv74vebCdJC" | ||||||
|  | @ -29,5 +66,6 @@ menu "lib using MQTT" | ||||||
|                     default "1883" |                     default "1883" | ||||||
|             endmenu |             endmenu | ||||||
|         endif |         endif | ||||||
|  |     endif | ||||||
| 
 | 
 | ||||||
| endmenu | endmenu | ||||||
|  |  | ||||||
|  | @ -1,3 +1,9 @@ | ||||||
|  | ifeq ($(CONFIG_XIUOS_PLATFORM),y) | ||||||
|  |   SRC_FILES := platform_mqtt.c | ||||||
|  | endif | ||||||
|  | 
 | ||||||
|  | ifeq ($(CONFIG_ALIBABA_PLATFORM),y) | ||||||
|   SRC_FILES := platform_mqtt.c  utils_hmacsha1.c |   SRC_FILES := platform_mqtt.c  utils_hmacsha1.c | ||||||
|  | endif | ||||||
| 
 | 
 | ||||||
| include $(KERNEL_ROOT)/compiler.mk | include $(KERNEL_ROOT)/compiler.mk | ||||||
|  | @ -24,6 +24,7 @@ | ||||||
| #include <adapter.h> | #include <adapter.h> | ||||||
| #include <transform.h> | #include <transform.h> | ||||||
| #include "platform_mqtt.h" | #include "platform_mqtt.h" | ||||||
|  | #include "utils_hmacsha1.h" | ||||||
| 
 | 
 | ||||||
| MQTT_TCB Platform_mqtt;  //创建一个用于连接云平台mqtt的结构体
 | MQTT_TCB Platform_mqtt;  //创建一个用于连接云平台mqtt的结构体
 | ||||||
| static struct Adapter *adapter; | static struct Adapter *adapter; | ||||||
|  | @ -110,14 +111,21 @@ int MQTT_Recv(uint8_t* buf, int buflen) | ||||||
| int MQTT_Connect(void) | int MQTT_Connect(void) | ||||||
| { | { | ||||||
|     uint8_t TryConnect_time = 10;  //尝试登录次数
 |     uint8_t TryConnect_time = 10;  //尝试登录次数
 | ||||||
| 	uint8_t passwdtemp[PASSWARD_SIZE]; |  | ||||||
| 
 | 
 | ||||||
|     memset(&Platform_mqtt,0,sizeof(Platform_mqtt)); |     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.ClientID,"%s|securemode=3,signmethod=hmacsha1|",CLIENT_DEVICENAME);   //构建客户端ID并存入缓冲区
 | ||||||
|     sprintf(Platform_mqtt.Username,"%s&%s",CLIENT_DEVICENAME,PLATFORM_PRODUCTKEY);              //构建用户名并存入缓冲区
 |     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);  //构建加密时的明文   
 |     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加密即为密码
 |     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.MessageID = 0;      //报文标识符清零,CONNECT报文虽然不需要添加报文标识符,但是CONNECT报文是第一个发送的报文,在此清零报文标识符为后续报文做准备
 | ||||||
|     Platform_mqtt.Fixed_len = 1;      //CONNECT报文固定报头长度暂定为1
 |     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; //剩余长度=可变报头长度+负载长度
 |     Platform_mqtt.Remaining_len = Platform_mqtt.Variable_len + Platform_mqtt.Payload_len; //剩余长度=可变报头长度+负载长度
 | ||||||
|     memset(Platform_mqtt.Pack_buff,0,sizeof(Platform_mqtt.Pack_buff)); |     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{ |     do{ | ||||||
|         if((Platform_mqtt.Remaining_len/128) == 0) |         if((Platform_mqtt.Remaining_len/128) == 0) | ||||||
|         { |         { | ||||||
|  | @ -423,11 +431,14 @@ void MQTT_DealPublishData(uint8_t *data, uint16_t data_len) | ||||||
| 
 | 
 | ||||||
|     for(i = 1;i < 5;i++) |     for(i = 1;i < 5;i++) | ||||||
|     { |     { | ||||||
|  |         //查找可变报头的长度字段,如果最高位为0表示该字节是长度字段的最后一个字节
 | ||||||
|         if((data[i] & 0x80) == 0) |         if((data[i] & 0x80) == 0) | ||||||
|             break; |             break; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     //1代表固定报头占一个字节,i代表可变报头长度字段所占用字节数,2代表主题长度字段占2字节,cmdpos代表报文里主题名称起始位置
 | ||||||
|     cmdpos = 1+i+2; |     cmdpos = 1+i+2; | ||||||
|  |     //data_len减去1+i+2就是报文中有效载荷的长度
 | ||||||
|     cmdlen = data_len-(1+i+2); |     cmdlen = data_len-(1+i+2); | ||||||
| 
 | 
 | ||||||
|     if(data_len <= CMD_SIZE) |     if(data_len <= CMD_SIZE) | ||||||
|  |  | ||||||
|  | @ -23,7 +23,6 @@ | ||||||
| #define	_PLATFORM_MQTT_H_ | #define	_PLATFORM_MQTT_H_ | ||||||
| 
 | 
 | ||||||
| #include <stdint.h> | #include <stdint.h> | ||||||
| #include "utils_hmacsha1.h" |  | ||||||
| 
 | 
 | ||||||
| #define  KEEPALIVE_TIME 300    //保活时间(单位s),300s
 | #define  KEEPALIVE_TIME 300    //保活时间(单位s),300s
 | ||||||
| #define  HEART_TIME  120000    //空闲时发送心跳包的时间间隔(单位ms),120s
 | #define  HEART_TIME  120000    //空闲时发送心跳包的时间间隔(单位ms),120s
 | ||||||
|  |  | ||||||
|  | @ -581,7 +581,7 @@ KERNELPATHS +=-I$(KERNEL_ROOT)/tool/bootloader/flash \ | ||||||
| 	-I$(KERNEL_ROOT)/tool/bootloader/ota # | 	-I$(KERNEL_ROOT)/tool/bootloader/ota # | ||||||
| endif | endif | ||||||
| 
 | 
 | ||||||
| ifeq ($(CONFIG_TOOL_USING_MQTT), y) | ifeq ($(CONFIG_LIB_USING_MQTT), y) | ||||||
| KERNELPATHS +=-I$(KERNEL_ROOT)/../../APP_Framework/lib/mqtt | KERNELPATHS +=-I$(KERNEL_ROOT)/../../APP_Framework/lib/mqtt | ||||||
| endif | endif | ||||||
| 
 | 
 | ||||||
|  |  | ||||||
|  | @ -23,7 +23,7 @@ menu "OTA function" | ||||||
| 
 | 
 | ||||||
|                 config OTA_BY_PLATFORM |                 config OTA_BY_PLATFORM | ||||||
|                     bool "Through IoT management platform." |                     bool "Through IoT management platform." | ||||||
|                     select TOOL_USING_MQTT |                     select LIB_USING_MQTT | ||||||
| 
 | 
 | ||||||
|                 config OTA_BY_TCPSERVER |                 config OTA_BY_TCPSERVER | ||||||
|                     bool "Through the public network TCP server." |                     bool "Through the public network TCP server." | ||||||
|  |  | ||||||
|  | @ -128,7 +128,7 @@ static uint32_t calculate_crc32(uint32_t addr, uint32_t len) | ||||||
| * 形    参: cur_version:当前版本号,new_version:生成的新版本号 | * 形    参: cur_version:当前版本号,new_version:生成的新版本号 | ||||||
| * 返 回 值: 0:生成成功,-1:生成失败 | * 返 回 值: 0:生成成功,-1:生成失败 | ||||||
| * 说    明: 为保持一致,平台通过OTA传输而来的版本号也要保持这样三段式的形式 | * 说    明: 为保持一致,平台通过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)  | 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   //每帧数据的数据包长度
 | #define FRAME_LEN   2048   //每帧数据的数据包长度
 | ||||||
| static uint8_t MqttRxbuf[3072]; | static uint8_t MqttRxbuf[3072]; | ||||||
| static uint8_t FrameBuf[FRAME_LEN]; | 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 | * 函 数 名: PropertyVersion | ||||||
| * 功能描述: 向服务器上传当前设备版本信息 | * 功能描述: 向服务器上传当前设备版本信息 | ||||||
|  | @ -768,11 +984,11 @@ static void PropertyVersion(void) | ||||||
| 
 | 
 | ||||||
| /*-------------------------------------------------*/ | /*-------------------------------------------------*/ | ||||||
| /*函数名:OTA下载数据                              */ | /*函数名:OTA下载数据                              */ | ||||||
| /*参  数:size:本次下载量                         */ | /*参  数:size:本次下载量                         */ | ||||||
| /*参  数:offset:本次下载偏移量                   */ | /*参  数:offset:本次下载偏移量                   */ | ||||||
| /*返回值:无                                       */ | /*返回值:无                                       */ | ||||||
| /*-------------------------------------------------*/ | /*-------------------------------------------------*/ | ||||||
| void OTA_Download(int size, int offset) | static void OTA_Download(int size, int offset) | ||||||
| { | { | ||||||
|     uint8_t topicdatabuff[64]; |     uint8_t topicdatabuff[64]; | ||||||
|     uint8_t tempdatabuff[128]; |     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); |     sprintf(topicdatabuff,"/sys/%s/%s/thing/file/download",PLATFORM_PRODUCTKEY,CLIENT_DEVICENAME); | ||||||
| 
 | 
 | ||||||
|     memset(tempdatabuff,0,128); |     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)); |     MQTT_PublishDataQs0(topicdatabuff, tempdatabuff, strlen(tempdatabuff)); | ||||||
| } | } | ||||||
|  | @ -838,21 +1054,21 @@ reconnect: | ||||||
|             ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\"");  |             ptr = strstr((char *)Platform_mqtt.cmdbuff,"{\"code\":\"1000\"");  | ||||||
|             if(ptr != NULL) |             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 size:%d\r\n",platform_ota.size); | ||||||
|                     KPrintf("ota file id:%d\r\n",AliOTA.streamId); |                     KPrintf("ota file id:%d\r\n",platform_ota.streamId); | ||||||
|                     KPrintf("ota file version:%s\r\n",AliOTA.version); |                     KPrintf("ota file version:%s\r\n",platform_ota.version); | ||||||
|                     if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,AliOTA.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 target fash!\n"); | ||||||
|                         ret = -1; |                         ret = -1; | ||||||
|                         break; |                         break; | ||||||
|                     } |                     } | ||||||
|                     AliOTA.counter = (AliOTA.size%FRAME_LEN != 0)? (AliOTA.size/FRAME_LEN + 1):(AliOTA.size/FRAME_LEN); |                     platform_ota.counter = (platform_ota.size%FRAME_LEN != 0)? (platform_ota.size/FRAME_LEN + 1):(platform_ota.size/FRAME_LEN); | ||||||
|                     AliOTA.num = 1;                                          //下载次数,初始值为1
 |                     platform_ota.num = 1;                                          //下载次数,初始值为1
 | ||||||
|                     AliOTA.downlen = FRAME_LEN;                              //记录本次下载量
 |                     platform_ota.downlen = FRAME_LEN;                              //记录本次下载量
 | ||||||
|                     OTA_Download(AliOTA.downlen,(AliOTA.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
 |                     OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
 | ||||||
|                 } |                 } | ||||||
|                 else |                 else | ||||||
|                 { |                 { | ||||||
|  | @ -865,37 +1081,37 @@ reconnect: | ||||||
|             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)); | ||||||
|                 memcpy(FrameBuf, &MqttRxbuf[datalen-AliOTA.downlen-2], AliOTA.downlen); |                 memcpy(FrameBuf, &MqttRxbuf[datalen-platform_ota.downlen-2], platform_ota.downlen); | ||||||
|                 if(mcuboot.op_flash_write(flashdestination,FrameBuf,AliOTA.downlen) != kStatus_Success) |                 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; |                     ret = -1; | ||||||
|                     break; |                     break; | ||||||
|                 } |                 } | ||||||
|                 else |                 else | ||||||
|                 { |                 { | ||||||
|                     KPrintf("current frame[%d] is written to flash 0x%x address successful.\n", AliOTA.num -1, flashdestination); |                     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",AliOTA.num,AliOTA.counter);  |                     KPrintf("Current progress is %d/%d\r\n",platform_ota.num,platform_ota.counter);  | ||||||
|                     flashdestination += AliOTA.downlen; |                     flashdestination += platform_ota.downlen; | ||||||
|                     AliOTA.num++;  |                     platform_ota.num++;  | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|                 if(AliOTA.num < AliOTA.counter) //如果小于总下载次数
 |                 if(platform_ota.num < platform_ota.counter) //如果小于总下载次数
 | ||||||
|                 { |                 { | ||||||
|                     AliOTA.downlen = FRAME_LEN;                                //记录本次下载量
 |                     platform_ota.downlen = FRAME_LEN;                                      //记录本次下载量
 | ||||||
|                     OTA_Download(AliOTA.downlen,(AliOTA.num - 1)*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;                              //记录本次下载量
 |                         platform_ota.downlen = FRAME_LEN;                                    //记录本次下载量
 | ||||||
|                         OTA_Download(AliOTA.downlen,(AliOTA.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
 |                         OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN); //发送要下载的数据信息给服务器
 | ||||||
|                     } |                     } | ||||||
|                     else |                     else | ||||||
|                     { |                     { | ||||||
|                         AliOTA.downlen = AliOTA.size%FRAME_LEN;                   //记录本次下载量
 |                         platform_ota.downlen = platform_ota.size%FRAME_LEN;                   //记录本次下载量
 | ||||||
|                         OTA_Download(AliOTA.downlen,(AliOTA.num - 1)*FRAME_LEN);  //发送要下载的数据信息给服务器	
 |                         OTA_Download(platform_ota.downlen,(platform_ota.num - 1)*FRAME_LEN);  //发送要下载的数据信息给服务器
 | ||||||
|                     } |                     } | ||||||
|                 } |                 } | ||||||
| 
 | 
 | ||||||
|  | @ -929,11 +1145,11 @@ reconnect: | ||||||
| 
 | 
 | ||||||
|     if(0 == ret) |     if(0 == ret) | ||||||
|     { |     { | ||||||
|         ota_info.down.size = AliOTA.size; |         ota_info.down.size = platform_ota.size; | ||||||
|         ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, AliOTA.size); |         ota_info.down.crc32 = calculate_crc32(DOWN_FLAH_ADDRESS, platform_ota.size); | ||||||
| 
 | 
 | ||||||
|         memset(ota_info.down.version,0,sizeof(ota_info.down.version));  |         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));  |         memset(ota_info.down.description,0,sizeof(ota_info.down.description));  | ||||||
|         strncpy(ota_info.down.description, "MQTT OTA bin.",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); |     MdelayKTask(2000); | ||||||
|     mcuboot.op_reset(); |     mcuboot.op_reset(); | ||||||
| } | } | ||||||
|  | #endif | ||||||
| 
 | 
 | ||||||
| int OtaTask(void) | int OtaTask(void) | ||||||
| { | { | ||||||
|  |  | ||||||
|  | @ -25,10 +25,6 @@ | ||||||
| 
 | 
 | ||||||
| #define JUMP_FAILED_FLAG  0XABABABAB | #define JUMP_FAILED_FLAG  0XABABABAB | ||||||
| #define JUMP_SUCCESS_FLAG 0XCDCDCDCD | #define JUMP_SUCCESS_FLAG 0XCDCDCDCD | ||||||
| #define STARTFLAG             0x1A2B   //数据帧开始信号标记
 |  | ||||||
| #define DATAFLAG              0x3C4D   //数据帧数据信号标记
 |  | ||||||
| #define ENDTFLAG              0x5E6F   //数据帧结束信号标记
 |  | ||||||
| #define LENGTH                  1024   //每帧数据的数据包长度
 |  | ||||||
| 
 | 
 | ||||||
| typedef enum { | typedef enum { | ||||||
|     OTA_STATUS_IDLE = 0,     // 空闲状态,没有进行OTA升级
 |     OTA_STATUS_IDLE = 0,     // 空闲状态,没有进行OTA升级
 | ||||||
|  | @ -62,6 +58,10 @@ typedef struct { | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| #ifdef OTA_BY_TCPSERVER | #ifdef OTA_BY_TCPSERVER | ||||||
|  | #define STARTFLAG             0x1A2B   //数据帧开始信号标记
 | ||||||
|  | #define DATAFLAG              0x3C4D   //数据帧数据信号标记
 | ||||||
|  | #define ENDTFLAG              0x5E6F   //数据帧结束信号标记
 | ||||||
|  | #define LENGTH                  1024   //每帧数据的数据包长度
 | ||||||
| /*bin包传输过程中的数据帧相关的结构体*/ | /*bin包传输过程中的数据帧相关的结构体*/ | ||||||
| typedef struct | typedef struct | ||||||
| { | { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue