手把手教你STM32的远程升级 ota升级详细流程( 三 )

3.检查升级任务;dev_id是设备ID,authorization是鉴权参数,cur_version是当前的版本号,timeout发送超时时间

手把手教你STM32的远程升级 ota升级详细流程

文章插图

手把手教你STM32的远程升级 ota升级详细流程

文章插图

手把手教你STM32的远程升级 ota升级详细流程

文章插图
复制
//检查升级任务uint8_tDetect_Task(char*dev_id,char*cur_version,char*authorization,uint16_ttimeout) { uint16_ttime=0; charsend_buf[280]; USART1_Clear(); //清除串口数据 snprintf(send_buf, sizeof(send_buf), \"GET /ota/south/check?\"\"dev_id=%s&manuf=100&model=10001&type=2&version=%s&cdn=false HTTP/1.1\\r\\n\"\"Authorization:%s\\r\\n\"\"Host:ota.heclouds.com\\r\\n\\r\\n\", dev_id, cur_version,authorization); Uart2_Send(send_buf); while(time<timeout) { if(strstr( (constchar*)usart_info.buf , (constchar*)\"\\\"errno\\\":0\")) break; Delay_Ms(100); time++; } if(time>=timeout) return1; elsereturn0; } 3.下载资源(我省略了”检查token有效”步骤);ctoken是上一步“检查升级任务”返回的Token,这个每次请求都不一样,所以注意要记录;size:平台返回的固件大小(字节);bytes_range:分片大小(字节)

手把手教你STM32的远程升级 ota升级详细流程

文章插图
复制
/* ************************************************************ * 函数名称: OTA_Download_Range * * 函数功能: 分片下载固件 * * 入口参数: token:平台返回的Token * size:平台返回的固件大小(字节) * bytes_range:分片大小(字节) * * 返回参数: 0-成功 其他-失败 * * 说明: ************************************************************ */uint8_tDownload_Task(char*ctoken,unsignedintsize, constunsignedshort bytes_range,uint16_ttimeout) { MD5_CTX md5_ctx; //MD5相关变量unsignedcharmd5_t[16]; charmd5_t1[16]; charmd5_result[40]; uint16_ttime=0; char*data_ptr = NULL; charsend_buf[256]; unsignedcharflash_buf[OTA_BUFFER_SIZE]; //flash读写缓存unsignedintbytes = 0; MD5_Init(&md5_ctx); Flash_cashu(); while(bytes < size) { time = 0; memset(send_buf, 0, sizeof(send_buf)); USART1_Clear(); //清除串口数据 snprintf(send_buf, sizeof(send_buf), \"GET /ota/south/download/\"\"%s HTTP/1.1\\r\\n\"\"Range:bytes=%d-%d\\r\\n\"\"Host:ota.heclouds.com\\r\\n\\r\\n\", ctoken, bytes, bytes + bytes_range - 1); Uart2_Send(send_buf); //----------------------------------------------------等待数据---------------------------------------------------------------------while(time < 30) { if(usart_info.buf[0] != 0) break; Delay_Ms(100); time++; } if(time <= 29) { Delay_Ms(500); //----------------------------------------------------跳过HTTP报文头、找到固件数据--------------------------------------------------data_ptr = strstr( (constchar*)usart_info.buf, \"Range\"); data_ptr = strstr(data_ptr, \"\\r\\n\"); data_ptr += 4; //----------------------------------------------------将固件数据写入缓存和闪存-----------------------------------------------------if(data_ptr != NULL) { if((size - bytes) >= OTA_BUFFER_SIZE) { memcpy(flash_buf + (bytes % OTA_BUFFER_SIZE), data_ptr, bytes_range); STMFLASH_Write_NoCheck(FLASH_APP1_ADDR + bytes,(uint16_t*)flash_buf,OTA_BUFFER_SIZE / 2); bytes = bytes + OTA_BUFFER_SIZE; MD5_Update(&md5_ctx, (unsignedchar*)data_ptr, bytes_range); } else{ memcpy(flash_buf + (bytes % OTA_BUFFER_SIZE), data_ptr, size - bytes); STMFLASH_Write_NoCheck(FLASH_APP1_ADDR + bytes , (uint16_t*)flash_buf , (size % OTA_BUFFER_SIZE) / 2); MD5_Update(&md5_ctx, (unsignedchar*)data_ptr, size - bytes); bytes = size; } } } } //----------------------------------------------------MD校验比对------------------------------------------------------------------memset(md5_result, 0, sizeof(md5_result)); MD5_Final(&md5_ctx, md5_t); for(inti = 0; i < 16; i++) { if(md5_t[i] <= 0x0f) sprintf(md5_t1, \"0%x\", md5_t[i]); elsesprintf(md5_t1, \"%x\", md5_t[i]); strcat(md5_result, md5_t1); } if(strcmp(md5_result, ota_info.md5) == 0) return0; elsereturn1; }