diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c index 28f412d93..9a234d0a1 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/ota.c @@ -375,7 +375,7 @@ static void app_ota_by_iap(void) mcuboot.flash_deinit(); mcuboot.op_reset(); } -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),ota, app_ota_by_iap, ota by iap function); +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); /******************************************************************************* @@ -482,11 +482,16 @@ void ota_entry(void) static int ota_data_recv(struct Adapter* adapter) { struct ota_data recv_msg; + ota_info_t ota_info; char reply[16] = {0}; - int ret = 0; - int try_times = 10; - int frame_cnt = 0; + int ret = 0, try_times = 10, frame_cnt = 0; + uint32_t file_size = 0; + uint32_t flashdestination = DOWN_FLAH_ADDRESS; + mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t)); + ota_info.status = OTA_STATUS_DOWNLOADING; + UpdateOTAFlag(&ota_info); + while(1) { memset(&recv_msg, 0, sizeof(struct ota_data)); @@ -501,23 +506,37 @@ static int ota_data_recv(struct Adapter* adapter) if(0 == strncmp("aiit_ota_end",recv_msg.frame.frame_data, strlen("aiit_ota_end"))) //说明当前是结束帧 { - printf("total [%d]frames [%d]Bytes crc[%x],receive successful,\n",frame_cnt,recv_msg.header.total_len,recv_msg.frame.crc); + printf("total %d frames %d bytes crc[0x%x],receive successful,\n",frame_cnt,recv_msg.header.total_len,recv_msg.frame.crc); memset(reply, 0, 16); memcpy(reply, "ok", strlen("ok")); AdapterDeviceSend(adapter, reply, strlen(reply)); + + ota_info.status = OTA_STATUS_DOWNLOADED; + UpdateOTAFlag(&ota_info); + + file_size = recv_msg.header.total_len; ret = 0; break; } frame_cnt = recv_msg.frame.frame_id; - if(recv_msg.frame.crc == calculate_crc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len)) { - printf("current [%d] frame,length[%d] Bytes.\n",frame_cnt,recv_msg.frame.frame_len); - /*写入flash待实现*/ + printf("current[%d] frame,length %d bytes.\n",frame_cnt,recv_msg.frame.frame_len); + if(mcuboot.op_flash_write(flashdestination, recv_msg.frame.frame_data, recv_msg.frame.frame_len) != kStatus_Success) + { + printf("current[%d] frame flash failed.\n",frame_cnt); + ret = -1; + break; + } + else + { + printf("current[%d] frame is written to flash 0x%x address successful.\n", frame_cnt, flashdestination); + flashdestination += recv_msg.frame.frame_len; + } } else { - printf("current [%d] frame crc check failed,try again!\n",frame_cnt); + printf("current[%d] frame crc check failed,try again!\n",frame_cnt); goto try_again; } @@ -543,7 +562,7 @@ send_ok_again: try_again: if(try_times == 0) { - printf("current [%d] frame try 10 times failed,break out!\n",frame_cnt); + printf("current[%d] frame try 10 times failed,break out!\n",frame_cnt); ret = -1; break; } @@ -556,10 +575,22 @@ try_again: } } - if(0 == ret) - { - printf("ota file done,start application.\n"); - //传输完成需要干什么; + //download分区固件下载成功,更新Flag分区 + if(0 == ret) + { + ota_info.down.size = file_size; + ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, file_size); + ota_info.down.version = ota_info.os.version + 1; + strncpy(ota_info.down.description, "4G OTA bin package !",sizeof(ota_info.down.description)); + ota_info.status = OTA_STATUS_READY; + strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message)); + UpdateOTAFlag(&ota_info); + } + else + { + ota_info.status = OTA_STATUS_ERROR; + strncpy(ota_info.error_message, "Failed to download firmware to download partition!",sizeof(ota_info.error_message)); + UpdateOTAFlag(&ota_info); } return ret; } @@ -575,14 +606,14 @@ void app_ota_by_4g(void) { struct ota_data recv_msg; char reply[16] = {0}; - int baud_rate = BAUD_RATE_115200; - int len = 0; + uint32_t baud_rate = BAUD_RATE_115200; int ret = 0; - - struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME); uint8 server_addr[64] = "115.238.53.60"; uint8 server_port[64] = "7777"; + mcuboot.flash_init(); + + struct Adapter* adapter = AdapterDeviceFindByName(ADAPTER_4G_NAME); adapter->socket.socket_id = 0; AdapterDeviceOpen(adapter); @@ -599,6 +630,13 @@ void app_ota_by_4g(void) { if(0 == strncmp("aiit_ota_start",recv_msg.frame.frame_data, strlen("aiit_ota_start"))) { + if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,recv_msg.header.total_len) != kStatus_Success) + { + printf("Failed to erase target fash!\n"); + break; + } else { + printf("Erase flash successful,Erase length is %d bytes.\n",recv_msg.header.total_len); + } memset(reply, 0, 16); memcpy(reply, "ready", strlen("ready")); printf("receive start signal,send [ready] signal to server\n"); @@ -608,7 +646,7 @@ send_ready_again: { goto send_ready_again; } - printf("start receive ota file.\n"); + printf("start receive ota bin file.\n"); /* step2:start receive bin file,first wait for 4s. */ PrivTaskDelay(4000); ret = ota_data_recv(adapter); @@ -632,7 +670,11 @@ send_ready_again: ret = AdapterDeviceSend(adapter, reply, strlen(reply)); } } + mcuboot.flash_deinit(); + PrivTaskDelay(2000); + printf("ota file done,start reboot.\n"); + mcuboot.op_reset(); } -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),ota4g, app_ota_by_4g, ota by 4g function); +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 \ No newline at end of file diff --git a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/server_tcp.c b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/server_tcp.c index 0dbca44b7..c256bf5aa 100644 --- a/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/server_tcp.c +++ b/Ubiquitous/XiZi_IIoT/tool/bootloader/ota/server_tcp.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -112,7 +113,7 @@ void sockt_init(void) if(serverfd == -1) { - perror("创建socket失败"); + perror("Failed to create socket"); exit(-1); } @@ -140,20 +141,20 @@ void sockt_init(void) timeout.tv_usec = 0; if(setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, &timeout, sizeof(struct timeval)) < 0) { - perror("端口设置失败"); + perror("Failed to set setsock option"); exit(-1); } if(bind(serverfd,(struct sockaddr*)&addr,sizeof(addr)) == -1) { - perror("绑定失败"); + perror("Failed to bind socket port"); exit(-1); } //监听最大连接数 if(listen(serverfd,SIZE) == -1) { - perror("设置监听失败"); + perror("Failed to set socket listening"); exit(-1); } } @@ -168,14 +169,39 @@ void sockt_init(void) void ota_start_signal(int fd) { struct ota_data data; + struct stat st; uint8_t buf[32]; - int ret; - int length = 0; + int ret, length = 0, file_size = 0, file_frame_cnt = 0; + + if (access(BIN_PATH, F_OK) == 0) + { + printf("%s exists\n", basename(BIN_PATH)); + } + else + { + printf("%s does not exist,please cheack!\n", BIN_PATH); + return; + } + + //获取文件大小(以字节为单位) + if(stat(BIN_PATH, &st) == 0) + { + file_size = st.st_size; + file_frame_cnt = ((file_size%LENGTH) != 0)? (file_size/LENGTH + 1):(file_size/LENGTH); + printf("%s size is %d bytes,frame count is %d!\n",basename(BIN_PATH), file_size, file_frame_cnt); + } + else + { + printf("Failed to get file size\n"); + return; + } while(1) { memset(&data, 0x0 , sizeof(struct ota_data)); data.header.frame_flag = 0x5A5A; + //发送起始帧时把bin文件的大小一并发送出去 + data.header.total_len = file_size; memcpy(data.frame.frame_data,"aiit_ota_start",strlen("aiit_ota_start")); data.frame.frame_len = strlen("aiit_ota_start"); @@ -183,14 +209,14 @@ void ota_start_signal(int fd) ret = send(fd, &data, sizeof(data), MSG_NOSIGNAL); if(ret > 0) { - printf("send %s[%d] Bytes\n",data.frame.frame_data,ret); + printf("send %s %d bytes\n",data.frame.frame_data,ret); } memset(buf, 0, 32); length = recv(fd, buf, sizeof(buf), 0); if(length > 0 && (0 == strncmp(buf, "ready", length))) { - printf("recv buf %s length %d\n",buf,length); + printf("recv buf %s length %d.\n",buf,length); break; } @@ -214,28 +240,15 @@ int ota_file_send(int fd) { unsigned char buf[32] = { 0 }; struct ota_data data; - struct stat st; FILE *file_fd; int length = 0; int try_times; int recv_end_times = 3; int ret = 0; - int file_frame_cnt, frame_cnt = 0; + int frame_cnt = 0; int file_length = 0; char * file_buf = NULL; - if(stat(BIN_PATH, &st) == 0) - { - //获取文件大小(以字节为单位) - file_frame_cnt = (st.st_size%LENGTH != 0)? (st.st_size/LENGTH + 1):(st.st_size/LENGTH); - printf("File size is %ld bytes,file frame count is %d!\n", st.st_size, file_frame_cnt); - } - else - { - printf("get file size failed.\n"); - return -1; - } - file_fd = fopen(BIN_PATH, "r"); if(NULL == file_fd) { @@ -254,7 +267,7 @@ int ota_file_send(int fd) length = fread(data.frame.frame_data, 1, LENGTH, file_fd); if(length > 0) { - printf("read %d Bytes\n",length); + printf("read %d bytes\n",length); data.frame.frame_id = frame_cnt; data.frame.frame_len = length; data.frame.crc = calculate_crc16(data.frame.frame_data, length); @@ -280,7 +293,7 @@ recv_again: } //接收到的回复不是ok,说明刚发的包有问题,需要再发一次 - printf("receive buf[%s] length = %d\n",buf, length); + printf("receive buf[%s] length %d.\n",buf, length); if(0 == strncmp(buf, "ok", length)) { try_times = 10; @@ -307,7 +320,7 @@ recv_again: /* finally,crc check total bin file.*/ if(ret == 0) { - printf("total send file length[%d] Bytes [%d] frames.\n",file_length,frame_cnt); + printf("total send file length %d bytes, %d frames.\n",file_length,frame_cnt); printf("now crc check total bin file.\n"); file_buf = malloc(file_length); memset(file_buf, 0, file_length);