forked from xuos/xiuos
1、Increase the size of the EntmSend function send_buff
2、Optimize ota.c、ota.h and server_tcp.c files
This commit is contained in:
parent
dcea61d5e0
commit
fb9250181f
|
@ -264,23 +264,25 @@ int AtSetReplyCharNum(ATAgentType agent, unsigned int num)
|
||||||
|
|
||||||
int EntmSend(ATAgentType agent, const char *data, int len)
|
int EntmSend(ATAgentType agent, const char *data, int len)
|
||||||
{
|
{
|
||||||
char send_buf[128];
|
if(len > 256){
|
||||||
if(len > 128){
|
printf("send length %d more then max 256 Bytes.\n",len);
|
||||||
printf("send length %d more then max 128 Bytes.\n",len);
|
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
char *send_buff = (char *)PrivMalloc(256);
|
||||||
|
|
||||||
PrivMutexObtain(&agent->lock);
|
PrivMutexObtain(&agent->lock);
|
||||||
memset(send_buf, 0, 128);
|
memset(send_buff, 0, 256);
|
||||||
|
|
||||||
agent->receive_mode = ENTM_MODE;
|
agent->receive_mode = ENTM_MODE;
|
||||||
|
|
||||||
memcpy(send_buf, data, len);
|
memcpy(send_buff, data, len);
|
||||||
// memcpy(send_buf + len, "!@", 2);
|
|
||||||
|
|
||||||
PrivWrite(agent->fd, send_buf, len);
|
PrivWrite(agent->fd, send_buff, len);
|
||||||
PrivMutexAbandon(&agent->lock);
|
PrivMutexAbandon(&agent->lock);
|
||||||
printf("entm send %s length %d\n",send_buf, len);
|
printf("entm send length %d\n", len);
|
||||||
|
|
||||||
|
PrivFree(send_buff);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -104,6 +104,10 @@ static const uint32_t crc32tab[] = {
|
||||||
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d
|
||||||
};
|
};
|
||||||
|
|
||||||
|
#ifdef CONNECTION_ADAPTER_4G
|
||||||
|
static ota_data start_msg;
|
||||||
|
static ota_data recv_msg;
|
||||||
|
#endif
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* 函 数 名: calculate_crc32
|
* 函 数 名: calculate_crc32
|
||||||
|
@ -147,7 +151,7 @@ static uint16_t calculate_crc16(uint8_t * data, uint32_t len)
|
||||||
reg_crc=reg_crc >>1;
|
reg_crc=reg_crc >>1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
printf("crc = [0x%x]\n",reg_crc);
|
KPrintf("crc = [0x%x]\n",reg_crc);
|
||||||
return reg_crc;
|
return reg_crc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,6 +168,7 @@ static void UpdateNewApplication(void)
|
||||||
status_t status;
|
status_t status;
|
||||||
ota_info_t ota_info; // 定义OTA信息结构体
|
ota_info_t ota_info; // 定义OTA信息结构体
|
||||||
|
|
||||||
|
memset(&ota_info, 0, sizeof(ota_info_t));
|
||||||
// 从Flash中读取OTA信息
|
// 从Flash中读取OTA信息
|
||||||
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
||||||
|
|
||||||
|
@ -260,7 +265,7 @@ static void InitialVersion(void)
|
||||||
ota_info.os.size = size;
|
ota_info.os.size = size;
|
||||||
ota_info.os.crc32 = calculate_crc32(XIUOS_FLAH_ADDRESS, size);
|
ota_info.os.crc32 = calculate_crc32(XIUOS_FLAH_ADDRESS, size);
|
||||||
ota_info.os.version = 0x1;
|
ota_info.os.version = 0x1;
|
||||||
strncpy(ota_info.os.description, "This is the initial firmware for the device!", sizeof(ota_info.os.description));
|
strncpy(ota_info.os.description, "The initial firmware.", sizeof(ota_info.os.description));
|
||||||
UpdateOTAFlag(&ota_info);
|
UpdateOTAFlag(&ota_info);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -275,7 +280,9 @@ static void InitialVersion(void)
|
||||||
static void BackupVersion(void)
|
static void BackupVersion(void)
|
||||||
{
|
{
|
||||||
status_t status;
|
status_t status;
|
||||||
|
|
||||||
ota_info_t ota_info;
|
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));
|
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
||||||
|
|
||||||
ota_info.status = OTA_STATUS_BACKUP;
|
ota_info.status = OTA_STATUS_BACKUP;
|
||||||
|
@ -309,7 +316,9 @@ static void BackupVersion(void)
|
||||||
static void BootLoaderJumpApp(void)
|
static void BootLoaderJumpApp(void)
|
||||||
{
|
{
|
||||||
ota_info_t ota_info;
|
ota_info_t ota_info;
|
||||||
|
|
||||||
mcuboot.flash_init();
|
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));
|
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
||||||
|
|
||||||
if(ota_info.lastjumpflag == JUMP_FAILED_FLAG)
|
if(ota_info.lastjumpflag == JUMP_FAILED_FLAG)
|
||||||
|
@ -361,7 +370,7 @@ static void app_ota_by_iap(void)
|
||||||
|
|
||||||
mcuboot.flash_init();
|
mcuboot.flash_init();
|
||||||
mcuboot.serial_init();
|
mcuboot.serial_init();
|
||||||
|
memset(&ota_info, 0, sizeof(ota_info_t));
|
||||||
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
||||||
ota_info.status = OTA_STATUS_DOWNLOADING;
|
ota_info.status = OTA_STATUS_DOWNLOADING;
|
||||||
UpdateOTAFlag(&ota_info);
|
UpdateOTAFlag(&ota_info);
|
||||||
|
@ -373,7 +382,7 @@ static void app_ota_by_iap(void)
|
||||||
ota_info.down.size = size;
|
ota_info.down.size = size;
|
||||||
ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, size);
|
ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, size);
|
||||||
ota_info.down.version = ota_info.os.version + 1;
|
ota_info.down.version = ota_info.os.version + 1;
|
||||||
strncpy(ota_info.down.description, "OTA Test!",sizeof(ota_info.down.description));
|
strncpy(ota_info.down.description, "OTA Test bin.",sizeof(ota_info.down.description));
|
||||||
ota_info.status = OTA_STATUS_READY;
|
ota_info.status = OTA_STATUS_READY;
|
||||||
strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message));
|
strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message));
|
||||||
UpdateOTAFlag(&ota_info);
|
UpdateOTAFlag(&ota_info);
|
||||||
|
@ -398,9 +407,10 @@ SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHE
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
void app_clear_jumpflag(void)
|
void app_clear_jumpflag(void)
|
||||||
{
|
{
|
||||||
|
ota_info_t ota_info;
|
||||||
mcuboot.flash_init();
|
mcuboot.flash_init();
|
||||||
//跳转成功设置lastjumpflag为JUMP_SUCCESS_FLAG
|
//跳转成功设置lastjumpflag为JUMP_SUCCESS_FLAG
|
||||||
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));
|
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
||||||
ota_info.lastjumpflag = JUMP_SUCCESS_FLAG;
|
ota_info.lastjumpflag = JUMP_SUCCESS_FLAG;
|
||||||
UpdateOTAFlag(&ota_info);
|
UpdateOTAFlag(&ota_info);
|
||||||
|
@ -418,6 +428,7 @@ static void Update(void)
|
||||||
{
|
{
|
||||||
ota_info_t ota_info;
|
ota_info_t ota_info;
|
||||||
mcuboot.flash_init();
|
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));
|
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
||||||
/* 此时APP分区还没有有效的固件,需要在bootloader下通过iap烧写出厂固件 */
|
/* 此时APP分区还没有有效的固件,需要在bootloader下通过iap烧写出厂固件 */
|
||||||
if((ota_info.os.size > APP_FLASH_SIZE) || (calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.os.size) != ota_info.os.crc32))
|
if((ota_info.os.size > APP_FLASH_SIZE) || (calculate_crc32(XIUOS_FLAH_ADDRESS, ota_info.os.size) != ota_info.os.crc32))
|
||||||
|
@ -443,7 +454,6 @@ void ota_entry(void)
|
||||||
{
|
{
|
||||||
uint8_t ch1, ch2;
|
uint8_t ch1, ch2;
|
||||||
uint32_t ret;
|
uint32_t ret;
|
||||||
ota_info_t ota_info;
|
|
||||||
uint32_t timeout = 1000;
|
uint32_t timeout = 1000;
|
||||||
|
|
||||||
mcuboot.board_init();
|
mcuboot.board_init();
|
||||||
|
@ -500,51 +510,48 @@ void ota_entry(void)
|
||||||
|
|
||||||
#ifdef CONNECTION_ADAPTER_4G
|
#ifdef CONNECTION_ADAPTER_4G
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* 函 数 名: ota_data_recv
|
* 函 数 名: get_start_signal
|
||||||
* 功能描述: 通过4G方式从服务端接收开始信号
|
* 功能描述: 通过4G方式从服务端接收开始信号
|
||||||
* 形 参: adapter:Adapter指针,指向注册的4G设备
|
* 形 参: adapter:Adapter指针,指向注册的4G设备
|
||||||
* 返 回 值: 0:传输成功,-1:传输失败
|
* 返 回 值: 0:传输成功,-1:传输失败
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
static void get_start_signal(struct Adapter* adapter)
|
static void get_start_signal(struct Adapter* adapter)
|
||||||
{
|
{
|
||||||
struct ota_data recv_msg;
|
|
||||||
ota_info_t ota_info;
|
ota_info_t ota_info;
|
||||||
char reply[16] = {0};
|
char reply[16] = {0};
|
||||||
uint32_t flashdestination = DOWN_FLAH_ADDRESS;
|
uint32_t flashdestination = DOWN_FLAH_ADDRESS;
|
||||||
|
|
||||||
|
memset(&ota_info, 0, sizeof(ota_info_t));
|
||||||
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
||||||
ota_info.status = OTA_STATUS_DOWNLOADING;
|
ota_info.status = OTA_STATUS_DOWNLOADING;
|
||||||
UpdateOTAFlag(&ota_info);
|
UpdateOTAFlag(&ota_info);
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
memset(&recv_msg, 0, sizeof(recv_msg));
|
memset(&start_msg, 0, sizeof(ota_data));
|
||||||
/* step1:Confirm the start signal of transmission. */
|
/* step1:Confirm the start signal of transmission. */
|
||||||
printf("waiting for start msg...\n");
|
KPrintf("waiting for start msg...\n");
|
||||||
if(AdapterDeviceRecv(adapter, &recv_msg, sizeof(recv_msg)) >= 0 && recv_msg.header.frame_flag == 0x5A5A)
|
if(AdapterDeviceRecv(adapter, &start_msg, sizeof(start_msg)) >= 0 && start_msg.header.frame_flag == STARTFLAG)
|
||||||
{
|
{
|
||||||
if(0 == strncmp("ota_start_signal",recv_msg.frame.frame_data, strlen("ota_start_signal")))
|
if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,start_msg.header.total_len) != kStatus_Success)
|
||||||
{
|
{
|
||||||
if(mcuboot.op_flash_erase(DOWN_FLAH_ADDRESS,recv_msg.header.total_len) != kStatus_Success)
|
KPrintf("Failed to erase target fash!\n");
|
||||||
{
|
|
||||||
printf("Failed to erase target fash!\n");
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
printf("Erase flash successful,erase length is %d bytes.\n",recv_msg.header.total_len);
|
KPrintf("Erase flash successful,erase length is %d bytes.\n",start_msg.header.total_len);
|
||||||
}
|
}
|
||||||
memset(reply, 0, sizeof(reply));
|
memset(reply, 0, sizeof(reply));
|
||||||
memcpy(reply, "ready", strlen("ready"));
|
memcpy(reply, "ready", strlen("ready"));
|
||||||
printf("receive start signal,send [ready] signal to server\n");
|
KPrintf("receive start signal,send [ready] signal to server\n");
|
||||||
while(AdapterDeviceSend(adapter, reply, strlen(reply)) < 0);
|
while(AdapterDeviceSend(adapter, reply, strlen(reply)) < 0);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
memset(reply, 0, sizeof(reply));
|
memset(reply, 0, sizeof(reply));
|
||||||
memcpy(reply, "notready", strlen("notready"));
|
memcpy(reply, "notready", strlen("notready"));
|
||||||
printf("not receive start signal,send [notready] signal to server\n");
|
KPrintf("not receive start signal,send [notready] signal to server\n");
|
||||||
while(AdapterDeviceSend(adapter, reply, strlen(reply)) < 0);
|
while(AdapterDeviceSend(adapter, reply, strlen(reply)) < 0);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
@ -560,31 +567,53 @@ static void get_start_signal(struct Adapter* adapter)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
static int ota_data_recv(struct Adapter* adapter)
|
static int ota_data_recv(struct Adapter* adapter)
|
||||||
{
|
{
|
||||||
struct ota_data recv_msg;
|
|
||||||
ota_info_t ota_info;
|
ota_info_t ota_info;
|
||||||
char reply[16] = {0};
|
char reply[16] = {0};
|
||||||
int ret = 0, frame_cnt = 0, try_times = 5;
|
int ret = 0, frame_cnt = 0, try_times = 5;
|
||||||
uint32_t file_size = 0;
|
uint32_t file_size = 0;
|
||||||
uint32_t flashdestination = DOWN_FLAH_ADDRESS;
|
uint32_t flashdestination = DOWN_FLAH_ADDRESS;
|
||||||
|
|
||||||
|
memset(&ota_info, 0, sizeof(ota_info_t));
|
||||||
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
mcuboot.op_flash_read(FLAG_FLAH_ADDRESS, (void*)&ota_info, sizeof(ota_info_t));
|
||||||
ota_info.status = OTA_STATUS_DOWNLOADING;
|
ota_info.status = OTA_STATUS_DOWNLOADING;
|
||||||
UpdateOTAFlag(&ota_info);
|
UpdateOTAFlag(&ota_info);
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
memset(&recv_msg, 0, sizeof(recv_msg));
|
memset(&recv_msg, 0, sizeof(ota_data));
|
||||||
if(AdapterDeviceRecv(adapter, &recv_msg, sizeof(recv_msg)) >= 0 && recv_msg.header.frame_flag == 0x5A5A)
|
if(AdapterDeviceRecv(adapter, &recv_msg, sizeof(recv_msg)) >= 0)
|
||||||
{
|
{
|
||||||
if(0 == strncmp("ota_start_signal",recv_msg.frame.frame_data, strlen("ota_start_signal")))
|
if(recv_msg.header.frame_flag == STARTFLAG) //这里不应该再出现开始帧,丢弃当前数据继续接收
|
||||||
{
|
{
|
||||||
//这里不应该再出现开始信号,丢弃当前数据继续接收
|
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
else if(recv_msg.header.frame_flag == DATAFLAG) //说明当前是bin包里数据封装成的数据帧
|
||||||
if(0 == strncmp("ota_end_signal",recv_msg.frame.frame_data, strlen("ota_end_signal"))) //说明当前是结束帧
|
|
||||||
{
|
{
|
||||||
printf("total %d frames %d bytes crc[0x%x],receive successful.\n",frame_cnt,recv_msg.header.total_len,recv_msg.frame.crc);
|
frame_cnt = recv_msg.frame.frame_id;
|
||||||
|
if(recv_msg.frame.crc == calculate_crc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len))
|
||||||
|
{
|
||||||
|
KPrintf("current frame[%d],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)
|
||||||
|
{
|
||||||
|
KPrintf("current frame[%d] flash failed.\n",frame_cnt);
|
||||||
|
ret = -1;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
KPrintf("current frame[%d] is written to flash 0x%x address successful.\n", frame_cnt, flashdestination);
|
||||||
|
flashdestination += recv_msg.frame.frame_len;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
KPrintf("current frame[%d] crc check failed,try again!\n",frame_cnt);
|
||||||
|
goto try_again;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else if(recv_msg.header.frame_flag == ENDTFLAG) //说明当前是结束帧
|
||||||
|
{
|
||||||
|
KPrintf("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, sizeof(reply));
|
memset(reply, 0, sizeof(reply));
|
||||||
memcpy(reply, "ok", strlen("ok"));
|
memcpy(reply, "ok", strlen("ok"));
|
||||||
AdapterDeviceSend(adapter, reply, strlen(reply));
|
AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||||
|
@ -596,25 +625,8 @@ static int ota_data_recv(struct Adapter* adapter)
|
||||||
ret = 0;
|
ret = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
frame_cnt = recv_msg.frame.frame_id;
|
else //说明当前接收的数据帧不是上述三种数据帧的任意一种
|
||||||
if(recv_msg.frame.crc == calculate_crc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len))
|
|
||||||
{
|
{
|
||||||
printf("current frame[%d],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 frame[%d] flash failed.\n",frame_cnt);
|
|
||||||
ret = -1;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("current frame[%d] is written to flash 0x%x address successful.\n", frame_cnt, flashdestination);
|
|
||||||
flashdestination += recv_msg.frame.frame_len;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
printf("current frame[%d] crc check failed,try again!\n",frame_cnt);
|
|
||||||
goto try_again;
|
goto try_again;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -625,28 +637,28 @@ send_ok_again:
|
||||||
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
|
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||||
if(ret < 0)
|
if(ret < 0)
|
||||||
{
|
{
|
||||||
printf("send ok failed.\n");
|
KPrintf("send ok failed.\n");
|
||||||
goto send_ok_again;
|
goto send_ok_again;
|
||||||
}
|
}
|
||||||
printf("send reply[%s] done.\n",reply);
|
KPrintf("send reply[%s] done.\n",reply);
|
||||||
//send ok后把try_times重置为5
|
//send ok后把try_times重置为5
|
||||||
try_times = 5;
|
try_times = 5;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
//没有接收到数据或者接收到的数据帧frame_flag不等于0x5A5A,需要发个retry的命令告诉服务器需要重传
|
//没有接收到数据或者接收到的数据帧不满足条件,需要发个retry的命令告诉服务器需要重传
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
try_again:
|
try_again:
|
||||||
if(try_times == 0)
|
if(try_times == 0)
|
||||||
{
|
{
|
||||||
printf("current frame[%d] try 5 times failed,break out!\n",frame_cnt);
|
KPrintf("current frame[%d] try 5 times failed,break out!\n",frame_cnt);
|
||||||
ret = -1;
|
ret = -1;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
memset(reply, 0, sizeof(reply));
|
memset(reply, 0, sizeof(reply));
|
||||||
memcpy(reply, "retry", strlen("retry"));
|
memcpy(reply, "retry", strlen("retry"));
|
||||||
printf("current frame[%d] receive failed. retry\n",frame_cnt);
|
KPrintf("current frame[%d] receive failed. retry\n",frame_cnt);
|
||||||
AdapterDeviceSend(adapter, reply, strlen(reply));
|
AdapterDeviceSend(adapter, reply, strlen(reply));
|
||||||
try_times--;
|
try_times--;
|
||||||
continue;
|
continue;
|
||||||
|
@ -659,7 +671,7 @@ try_again:
|
||||||
ota_info.down.size = file_size;
|
ota_info.down.size = file_size;
|
||||||
ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, file_size);
|
ota_info.down.crc32= calculate_crc32(DOWN_FLAH_ADDRESS, file_size);
|
||||||
ota_info.down.version = ota_info.os.version + 1;
|
ota_info.down.version = ota_info.os.version + 1;
|
||||||
strncpy(ota_info.down.description, "4G OTA bin package !",sizeof(ota_info.down.description));
|
strncpy(ota_info.down.description, "4G OTA bin.",sizeof(ota_info.down.description));
|
||||||
ota_info.status = OTA_STATUS_READY;
|
ota_info.status = OTA_STATUS_READY;
|
||||||
strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message));
|
strncpy(ota_info.error_message, "No error message!",sizeof(ota_info.error_message));
|
||||||
UpdateOTAFlag(&ota_info);
|
UpdateOTAFlag(&ota_info);
|
||||||
|
@ -675,18 +687,17 @@ try_again:
|
||||||
|
|
||||||
|
|
||||||
/*******************************************************************************
|
/*******************************************************************************
|
||||||
* 函 数 名: OtaKTaskEntry
|
* 函 数 名: app_ota_by_4g
|
||||||
* 功能描述: 通过命令来进行ota升级,该函数与升级的命令关联,通过4g方式传输bin文件
|
* 功能描述: 通过命令来进行ota升级,该函数与升级的命令关联,通过4g方式传输bin文件
|
||||||
* 形 参: adapter:Adapter指针,指向注册的4G设备
|
* 形 参: adapter:Adapter指针,指向注册的4G设备
|
||||||
* 返 回 值: 0:传输成功,-1:传输失败
|
* 返 回 值: 0:传输成功,-1:传输失败
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
static void app_ota_by_4g(void)
|
static void app_ota_by_4g(void)
|
||||||
{
|
{
|
||||||
struct ota_data recv_msg;
|
|
||||||
char reply[16] = {0};
|
char reply[16] = {0};
|
||||||
uint32_t baud_rate = BAUD_RATE_115200;
|
uint32_t baud_rate = BAUD_RATE_115200;
|
||||||
uint8 server_addr[64] = "115.238.53.60";
|
uint8_t server_addr[16] = "115.238.53.60";
|
||||||
uint8 server_port[64] = "7777";
|
uint8_t server_port[8] = "7777";
|
||||||
|
|
||||||
mcuboot.flash_init();
|
mcuboot.flash_init();
|
||||||
|
|
||||||
|
@ -701,7 +712,7 @@ static void app_ota_by_4g(void)
|
||||||
{
|
{
|
||||||
/* step1:Confirm the start signal of transmission. */
|
/* step1:Confirm the start signal of transmission. */
|
||||||
get_start_signal(adapter);
|
get_start_signal(adapter);
|
||||||
printf("start receive ota bin file.\n");
|
KPrintf("start receive ota bin file.\n");
|
||||||
/* step2:start receive bin file,first wait for 4s. */
|
/* step2:start receive bin file,first wait for 4s. */
|
||||||
PrivTaskDelay(4000);
|
PrivTaskDelay(4000);
|
||||||
if(0 == ota_data_recv(adapter))
|
if(0 == ota_data_recv(adapter))
|
||||||
|
@ -710,7 +721,7 @@ static void app_ota_by_4g(void)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mcuboot.flash_deinit();
|
mcuboot.flash_deinit();
|
||||||
printf("ota file done,start reboot.\n");
|
KPrintf("ota file done,start reboot.\n");
|
||||||
PrivTaskDelay(2000);
|
PrivTaskDelay(2000);
|
||||||
mcuboot.op_reset();
|
mcuboot.op_reset();
|
||||||
}
|
}
|
||||||
|
|
|
@ -25,6 +25,9 @@
|
||||||
|
|
||||||
#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 //每帧数据的数据包长度
|
#define LENGTH 1024 //每帧数据的数据包长度
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
|
@ -43,8 +46,7 @@ typedef struct {
|
||||||
uint32_t size; // 应用程序大小,记录分区固件的大小
|
uint32_t size; // 应用程序大小,记录分区固件的大小
|
||||||
uint32_t crc32; // 应用程序CRC32校验值,记录分区固件的crc32值
|
uint32_t crc32; // 应用程序CRC32校验值,记录分区固件的crc32值
|
||||||
uint32_t version; // 应用程序版本号,记录分区固件的版本号
|
uint32_t version; // 应用程序版本号,记录分区固件的版本号
|
||||||
uint32_t reserve; // 保留字段
|
uint8_t description[32]; // 固件的描述信息,最多32个字符
|
||||||
uint8_t description[128]; // 固件的描述信息,最多128个字符
|
|
||||||
} firmware_t;
|
} firmware_t;
|
||||||
|
|
||||||
|
|
||||||
|
@ -55,32 +57,31 @@ typedef struct {
|
||||||
firmware_t down; // Download分区属性信息
|
firmware_t down; // Download分区属性信息
|
||||||
uint32_t status; // 升级状态,取值来自于ota_status_t类型
|
uint32_t status; // 升级状态,取值来自于ota_status_t类型
|
||||||
uint32_t lastjumpflag; // bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0xCDCDCDCD
|
uint32_t lastjumpflag; // bootloaer跳转失败的标志,bootloader里置0xABABABAB,跳转成功后在应用里置0xCDCDCDCD
|
||||||
uint32_t reserve[2]; // 保留字段
|
uint8_t error_message[64]; // 错误信息,最多64个字符
|
||||||
uint8_t error_message[128]; // 错误信息,最多128个字符
|
|
||||||
} ota_info_t;
|
} ota_info_t;
|
||||||
|
|
||||||
|
|
||||||
/*bin包传输过程中的数据帧相关的结构体*/
|
/*bin包传输过程中的数据帧相关的结构体*/
|
||||||
struct ota_header_t
|
typedef struct
|
||||||
{
|
{
|
||||||
uint16_t frame_flag; // frame start flag 2 Bytes
|
uint16_t frame_flag; // frame start flag 2 Bytes
|
||||||
uint16_t dev_sid; // device software version
|
uint16_t dev_sid; // device software version
|
||||||
uint32_t total_len; // send data total length caculated from each frame_len
|
uint32_t total_len; // send data total length caculated from each frame_len
|
||||||
};
|
} ota_header_t;
|
||||||
|
|
||||||
struct ota_frame_t
|
typedef struct
|
||||||
{
|
{
|
||||||
uint32_t frame_id; // Current frame id
|
uint32_t frame_id; // Current frame id
|
||||||
uint8_t frame_data[LENGTH]; // Current frame data,max length 256
|
uint8_t frame_data[LENGTH]; // Current frame data
|
||||||
uint16_t frame_len; // Current frame data length
|
uint16_t frame_len; // Current frame data length
|
||||||
uint16_t crc; // Current frame data crc
|
uint16_t crc; // Current frame data crc
|
||||||
};
|
} ota_frame_t;
|
||||||
|
|
||||||
struct ota_data
|
typedef struct
|
||||||
{
|
{
|
||||||
struct ota_header_t header;
|
ota_header_t header;
|
||||||
struct ota_frame_t frame;
|
ota_frame_t frame;
|
||||||
};
|
} ota_data;
|
||||||
|
|
||||||
|
|
||||||
void app_clear_jumpflag(void);
|
void app_clear_jumpflag(void);
|
||||||
|
|
|
@ -38,32 +38,34 @@
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <ifaddrs.h>
|
#include <ifaddrs.h>
|
||||||
|
|
||||||
|
#define STARTFLAG 0x1A2B //数据帧开始信号标记
|
||||||
|
#define DATAFLAG 0x3C4D //数据帧数据信号标记
|
||||||
|
#define ENDTFLAG 0x5E6F //数据帧结束信号标记
|
||||||
#define PORT 7777 //socket端口号
|
#define PORT 7777 //socket端口号
|
||||||
#define SIZE 100 //socket链接限制为100
|
#define SIZE 100 //socket链接限制为100
|
||||||
#define LENGTH 1024 //每帧数据的数据包长度
|
#define LENGTH 1024 //每帧数据的数据包长度
|
||||||
#define BIN_PATH "/home/aep05/wgz/XiZi-xidatong-arm32-app.bin" //bin包的路径
|
#define BIN_PATH "/home/aep05/wgz/XiZi-xidatong-arm32-app.bin" //bin包的路径
|
||||||
|
|
||||||
struct ota_header_t
|
typedef struct
|
||||||
{
|
{
|
||||||
uint16_t frame_flag; // frame start flag 2 Bytes
|
uint16_t frame_flag; // frame start flag 2 Bytes
|
||||||
uint16_t dev_sid; // device software version
|
uint16_t dev_sid; // device software version
|
||||||
uint32_t total_len; // send data total length caculated from each frame_len
|
uint32_t total_len; // send data total length caculated from each frame_len
|
||||||
};
|
} ota_header_t;
|
||||||
|
|
||||||
struct ota_frame_t
|
typedef struct
|
||||||
{
|
{
|
||||||
uint32_t frame_id; // Current frame id
|
uint32_t frame_id; // Current frame id
|
||||||
uint8_t frame_data[LENGTH]; // Current frame data,max length 256
|
uint8_t frame_data[LENGTH]; // Current frame data
|
||||||
uint16_t frame_len; // Current frame data length
|
uint16_t frame_len; // Current frame data length
|
||||||
uint16_t crc; // Current frame data crc
|
uint16_t crc; // Current frame data crc
|
||||||
};
|
} ota_frame_t;
|
||||||
|
|
||||||
struct ota_data
|
typedef struct
|
||||||
{
|
{
|
||||||
struct ota_header_t header;
|
ota_header_t header;
|
||||||
struct ota_frame_t frame;
|
ota_frame_t frame;
|
||||||
};
|
} ota_data;
|
||||||
|
|
||||||
|
|
||||||
static int serverfd; // 服务器socket
|
static int serverfd; // 服务器socket
|
||||||
|
@ -168,7 +170,7 @@ void sockt_init(void)
|
||||||
*******************************************************************************/
|
*******************************************************************************/
|
||||||
void ota_start_signal(int fd)
|
void ota_start_signal(int fd)
|
||||||
{
|
{
|
||||||
struct ota_data data;
|
ota_data data;
|
||||||
struct stat st;
|
struct stat st;
|
||||||
uint8_t buf[32];
|
uint8_t buf[32];
|
||||||
int file_size = 0, file_frame_cnt = 0, length = 0;
|
int file_size = 0, file_frame_cnt = 0, length = 0;
|
||||||
|
@ -198,12 +200,10 @@ void ota_start_signal(int fd)
|
||||||
|
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
memset(&data, 0x0, sizeof(struct ota_data));
|
memset(&data, 0x0, sizeof(ota_data));
|
||||||
data.header.frame_flag = 0x5A5A;
|
data.header.frame_flag = STARTFLAG;
|
||||||
//发送起始帧时把bin文件的大小一并发送出去
|
//发送起始帧时把bin文件的大小一并发送出去
|
||||||
data.header.total_len = file_size;
|
data.header.total_len = file_size;
|
||||||
memcpy(data.frame.frame_data,"ota_start_signal",strlen("ota_start_signal"));
|
|
||||||
data.frame.frame_len = strlen("ota_start_signal");
|
|
||||||
|
|
||||||
while(send(fd, &data, sizeof(data), MSG_NOSIGNAL) <= 0);
|
while(send(fd, &data, sizeof(data), MSG_NOSIGNAL) <= 0);
|
||||||
printf("send start signal to client %d.\n", fd);
|
printf("send start signal to client %d.\n", fd);
|
||||||
|
@ -240,7 +240,7 @@ void ota_start_signal(int fd)
|
||||||
int ota_file_send(int fd)
|
int ota_file_send(int fd)
|
||||||
{
|
{
|
||||||
unsigned char buf[32] = { 0 };
|
unsigned char buf[32] = { 0 };
|
||||||
struct ota_data data;
|
ota_data data;
|
||||||
FILE *file_fd;
|
FILE *file_fd;
|
||||||
int length = 0;
|
int length = 0;
|
||||||
int try_times;
|
int try_times;
|
||||||
|
@ -265,7 +265,7 @@ int ota_file_send(int fd)
|
||||||
{
|
{
|
||||||
memset(&data, 0, sizeof(data));
|
memset(&data, 0, sizeof(data));
|
||||||
|
|
||||||
data.header.frame_flag = 0x5A5A;
|
data.header.frame_flag = DATAFLAG;
|
||||||
length = fread(data.frame.frame_data, 1, LENGTH, file_fd);
|
length = fread(data.frame.frame_data, 1, LENGTH, file_fd);
|
||||||
if(length == LENGTH)
|
if(length == LENGTH)
|
||||||
{
|
{
|
||||||
|
@ -359,7 +359,7 @@ recv_again:
|
||||||
memset(file_buf, 0, file_length);
|
memset(file_buf, 0, file_length);
|
||||||
memset(&data, 0, sizeof(data));
|
memset(&data, 0, sizeof(data));
|
||||||
|
|
||||||
data.header.frame_flag = 0x5A5A;
|
data.header.frame_flag = ENDTFLAG;
|
||||||
|
|
||||||
file_fd = fopen(BIN_PATH, "r");
|
file_fd = fopen(BIN_PATH, "r");
|
||||||
if(NULL == file_fd)
|
if(NULL == file_fd)
|
||||||
|
@ -374,9 +374,7 @@ recv_again:
|
||||||
{
|
{
|
||||||
data.frame.frame_id = frame_cnt;
|
data.frame.frame_id = frame_cnt;
|
||||||
data.header.total_len = file_length;
|
data.header.total_len = file_length;
|
||||||
data.frame.frame_len = strlen("ota_end_signal");
|
|
||||||
data.frame.crc = calculate_crc16(file_buf, length);
|
data.frame.crc = calculate_crc16(file_buf, length);
|
||||||
memcpy(data.frame.frame_data,"ota_end_signal",strlen("ota_end_signal"));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
send_end_signal:
|
send_end_signal:
|
||||||
|
@ -434,7 +432,7 @@ void* server_thread(void* p)
|
||||||
{
|
{
|
||||||
int fd = *(int*)p;
|
int fd = *(int*)p;
|
||||||
unsigned char buf[32] = { 0 };
|
unsigned char buf[32] = { 0 };
|
||||||
struct ota_data data;
|
ota_data data;
|
||||||
|
|
||||||
printf("pthread = %d\n",fd);
|
printf("pthread = %d\n",fd);
|
||||||
sleep(3);
|
sleep(3);
|
||||||
|
|
Loading…
Reference in New Issue