repair ota failed by add mutex protect

This commit is contained in:
Wang_Weigen 2021-11-25 14:24:11 +08:00
parent 9d970e7832
commit f24189d7e1
5 changed files with 222 additions and 111 deletions

View File

@ -47,7 +47,7 @@ struct ota_data
{
struct ota_header_t header;
struct ota_frame_t frame;
char end[2];
char end[4];
};
pthread_t ota_task;
@ -81,8 +81,11 @@ static int SaveAppBin(int fd, char* buf, int len)
{
// int fd = 0;
// fd = open( BOARD_APP_NAME, O_RDWR | O_CREAT );
write(fd, buf, len);
printf("write buf done\n");
lseek(fd, len, SEEK_CUR);
printf("lseek done\n");
// close(fd);
}
@ -146,8 +149,9 @@ static int OtaDataRecv(struct Adapter* adapter)
struct ota_data recv_msg;
char reply[16] = {0};
int ret = 0;
int try_times = 5;
int try_times = 10;
int fd = 0;
int frame_cnt = 0;
fd = open( BOARD_APP_NAME, O_RDWR | O_CREAT | O_TRUNC);
if(fd < 0)
@ -158,9 +162,12 @@ static int OtaDataRecv(struct Adapter* adapter)
while(1) {
memset(&recv_msg, 0, sizeof(struct ota_data));
ret = AdapterDeviceRecv(adapter, &recv_msg, 256);
if(ret > 0 && recv_msg.header.frame_flag == 0x5A5A)
printf("recv msg...\n");
ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data));
if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A)
{
frame_cnt = recv_msg.frame.frame_id;
if(0 == strncmp("aiit_ota_end",recv_msg.frame.frame_data, strlen("aiit_ota_end")))
{
printf("total [%d]frames [%d]Bytes,receive successful,\n",recv_msg.frame.frame_id,recv_msg.header.total_len);
@ -168,49 +175,74 @@ static int OtaDataRecv(struct Adapter* adapter)
{
printf("crc check %s bin failed.please try again.\n", BOARD_APP_NAME);
ret = -1;
}
break;
}
PrivTaskDelay(500);
memset(reply, 0, 16);
memcpy(reply, "ok", strlen("ok"));
AdapterDeviceSend(adapter, reply, strlen(reply));
ret = 0;
break;
}
if(0 == strncmp("wait_ok_timeout",recv_msg.frame.frame_data, strlen("wait_ok_timeout")))
{
printf("go to send ok again.\n");
goto send_ok_again;
}
if (recv_msg.frame.crc == OtaCrc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len))
{
printf("save current [%d] frame,length[%d] Bytes.\n",recv_msg.frame.frame_id,recv_msg.frame.frame_len);
printf("save current [%d] frame,length[%d] Bytes.\n",frame_cnt,recv_msg.frame.frame_len);
for(int i = 0; i < recv_msg.frame.frame_len;i++ ){
printf(" %x ",*((char *)&recv_msg.frame.frame_data + i));
}
printf("\n");
SaveAppBin(fd, recv_msg.frame.frame_data, recv_msg.frame.frame_len);
}
else
{
printf("current [%d] frame crc check failed,try again!\n",recv_msg.frame.frame_id);
printf("current [%d] frame crc check failed,try again!\n",frame_cnt);
goto try_again;
}
send_ok_again:
memset(reply, 0, 16);
memcpy(reply, "ok", strlen("ok"));
PrivTaskDelay(200);
send_ok_again:
// PrivTaskDelay(100);
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
if(ret < 0){
printf("send ok failed.\n");
goto send_ok_again;
}
try_times = 5;
printf("send reply[%s] done.\n",reply);
try_times = 10;
continue;
}
else
{
try_again:
if(try_times == 0)
{
printf("oops!!! current [%d] frame try 5 times failed,break out!\n",recv_msg.frame.frame_id);
printf("oops!!! current [%d] frame try 10 times failed,break out!\n",frame_cnt);
ret = -1;
break;
}
memset(reply, 0, 16);
memcpy(reply, "retry", strlen("retry"));
printf("[%d] frame receive failed. retry\n",frame_cnt);
AdapterDeviceSend(adapter, reply, strlen(reply));
try_times--;
continue;
}
}
close(fd);
if(0 == ret) {
printf("ota file done,start application.\n");
RestartApplication();
}
return ret;
@ -224,53 +256,57 @@ static void *OtaKTaskEntry(void *parameter)
int len = 0;
int ret = 0;
// struct Adapter* adapter = AdapterDeviceFindByName("4G");
// uint8 server_addr[64] = "115.238.53.61";
// uint8 server_port[64] = "9898";
struct Adapter* adapter = AdapterDeviceFindByName("4G");
uint8 server_addr[64] = "115.238.53.61";
uint8 server_port[64] = "9898";
// adapter->socket.socket_id = 0;
adapter->socket.socket_id = 0;
// AdapterDeviceOpen(adapter);
// AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
// AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
AdapterDeviceOpen(adapter);
AdapterDeviceControl(adapter, OPE_INT, &baud_rate);
AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4);
/* using nbiot as connection way*/
struct Adapter* adapter = AdapterDeviceFindByName("nbiot");
// struct Adapter* adapter = AdapterDeviceFindByName("nbiot");
while(1)
{
int connect_times = 5;
ret = AdapterDeviceOpen(adapter);
if(ret < 0)
{
printf("open adapter failed\n");
continue;
}
connect_again:
connect_times--;
ret = AdapterDeviceConnect(adapter, 1, "115.238.53.61","9898",1);
if(ret < 0)
{
if(connect_times > 0){
goto connect_again;
}
else
{
AdapterDeviceClose(adapter);
continue;
}
}
break;
}
// while(1)
// {
// int connect_times = 5;
// ret = AdapterDeviceOpen(adapter);
// if(ret < 0)
// {
// printf("open adapter failed\n");
// continue;
// }
// connect_again:
// connect_times--;
// ret = AdapterDeviceConnect(adapter, 1, "115.238.53.61","9898",1);
// if(ret < 0)
// {
// if(connect_times > 0){
// goto connect_again;
// }
// else
// {
// AdapterDeviceClose(adapter);
// continue;
// }
// }
// break;
// }
PrivTaskDelay(5000);
while(1)
{
memset(&recv_msg, 0, sizeof(struct ota_data));
/* step1: Confirm the start signal of transmission*/
ret = AdapterDeviceRecv(adapter, &recv_msg, 256);
printf("waiting for start msg...\n");
ret = AdapterDeviceRecv(adapter, &recv_msg, sizeof(struct ota_data));
for(int i = 0; i < sizeof(struct ota_data);i++ ){
printf(" %x ",*((char *)&recv_msg + i));
}
printf("\n");
if(ret >= 0 && recv_msg.header.frame_flag == 0x5A5A)
{
if (0 == strncmp("aiit_ota_start",recv_msg.frame.frame_data, strlen("aiit_ota_start")))
@ -292,7 +328,7 @@ send_ready_again:
if (0 != ret)
{
memset(reply, 0, 16);
memcpy(reply, "send_restart", strlen("send_restart"));
memcpy(reply, "ota_restart", strlen("ota_restart"));
AdapterDeviceSend(adapter, reply, strlen(reply));
continue;
}
@ -309,7 +345,7 @@ send_ready_again:
printf("ota status:not ready\n");
ret = AdapterDeviceSend(adapter, reply, strlen(reply));
}
PrivTaskDelay(5000); /* check ota signal every 5s */
PrivTaskDelay(3000); /* check ota signal every 5s */
}
AdapterDeviceClose(adapter);

View File

@ -26,7 +26,6 @@ int clientfd[100000];//客户端的socketfd,100个元素clientfd[0]~clientfd[
int size = 99999;//用来控制进入聊天室的人数为50以内
int PORT = 9898;//端口号
typedef struct sockaddr meng;
socklen_t len;
struct ota_header_t
{
@ -51,7 +50,7 @@ struct ota_data
{
struct ota_header_t header;
struct ota_frame_t frame;
char end[2];
char end[4];
};
pthread_t ota_ktask;
@ -98,8 +97,11 @@ void init(void)
addr.sin_addr.s_addr = htons(INADDR_ANY);//当sin_addr = INADDR_ANY时表示从本机的任一网卡接收数据
//绑定套接字
int on = 1;
if(setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0)
// int on = 1;
struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
if(setsockopt(serverfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
{
perror("端口设置失败");
exit(-1);
@ -123,8 +125,9 @@ int OtaFileSend(int fd)
struct ota_data data;
FILE *file_fd;
char ch;
int len = 0;
int try_times = 5;
int length = 0;
int try_times = 10;
int recv_end_times = 3;
int ret = 0;
int frame_cnt = 0;
int file_length = 0;
@ -137,30 +140,43 @@ int OtaFileSend(int fd)
}
fseek(file_fd, 0, SEEK_SET);
printf("start send file.\n");
// while((ch = fgetc(file_fd)) != EOF)
while(!feof(file_fd))
{
memset(&data, 0, sizeof(data));
data.header.frame_flag = 0x5A5A;
len = fread( data.frame.frame_data, 1, 64, file_fd );
if(len > 0)
length = fread( data.frame.frame_data, 1, 64, file_fd );
if(length > 0)
{
printf("read %d Bytes\n",length);
data.frame.frame_id = frame_cnt;
data.frame.frame_len = len;
data.frame.crc = OtaCrc16(data.frame.frame_data, len);
file_length += len;
data.frame.frame_len = length;
data.frame.crc = OtaCrc16(data.frame.frame_data, length);
file_length += length;
}
memcpy(data.end,"!@",2);
fseek(file_fd, len, SEEK_CUR);
try_again:
send(fd, &data, sizeof(data), MSG_NOSIGNAL);
len = recv(fd, buf, sizeof(buf), 0);
if(0 == strncmp(buf, "ok", len))
{
try_times = 5;
send_again:
usleep(50000);
printf("ota send current[%d] frame.\n",frame_cnt);
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if(length < 0){
printf("send [%d] frame faile.go to send again\n",frame_cnt);
goto send_again;
}
recv_again:
memset(buf, 0, 32);
length = recv(fd, buf, sizeof(buf), 0);
if(length < 0 ){
printf("[%d] frame waiting for ok timeout,receive again.\n",frame_cnt);
goto recv_again;
}
printf("receive buf[%s] length = %d\n",buf, length);
if(0 == strncmp(buf, "ok", length))
{
try_times = 10;
printf("[%d]frame data send done.\n",frame_cnt);
frame_cnt++;
continue;
}
@ -169,34 +185,68 @@ try_again:
if(try_times > 0)
{
try_times--;
goto try_again;
goto send_again;
}
else
{
printf("send frame[%d] 5 times failed.\n",frame_cnt);
printf("send frame[%d] 10 times failed.\n",frame_cnt);
ret = -1;
break;
}
}
}
/* finally,check total bin file.*/
if (ret == 0)
{
sleep(1);
printf("total send file length[%d] Bytes.\n",file_length);
printf("now check total bin file.\n");
file_buf = malloc(file_length);
memset(file_buf, 0, file_length);
memset(&data, 0, sizeof(data));
data.header.frame_flag = 0x5A5A;
len = fread(file_buf, file_length,1,file_fd);
if(len > 0) {
fseek(file_fd, 0, SEEK_SET);
length = fread(file_buf,1, file_length, file_fd);
if(length > 0) {
data.header.total_len = file_length;
data.frame.frame_len = strlen("aiit_ota_end");;
data.frame.crc = OtaCrc16(file_buf, len);
data.frame.frame_len = strlen("aiit_ota_end");
data.frame.crc = OtaCrc16(file_buf, length);
memcpy(data.frame.frame_data,"aiit_ota_end",strlen("aiit_ota_end"));
memcpy(data.end,"!@",2);
}
send(fd, &data, sizeof(data), MSG_NOSIGNAL);
send_end_signal:
printf("send aiit_ota_end signal.\n");
length = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if(length < 0){
printf("send end signal faile,send end signal again\n");
goto send_end_signal;
}
recv_end_signal:
memset(buf, 0, 32);
length = recv(fd, buf, sizeof(buf), 0);
if(length < 0 )
{
recv_end_times--;
printf("end signal waiting for ok timeout,receive again.\n");
if(recv_end_times > 0)
{
goto recv_end_signal;
}
else
{
ret = -1;
}
}
if(0 != strncmp(buf, "ok", length))
{
printf("error end !!!\n");
ret = -1;
}
free(file_buf);
}
@ -210,30 +260,33 @@ void* server_thread(void* p)
unsigned char buf[32] = { 0 };
struct ota_data data;
int ret = 0;
int length = 0;
printf("pthread = %d\n",fd);
// sleep(5);
sleep(8);
while(1)
{
memset(&data, 0 , sizeof(struct ota_data));
memset(&data, 0x0 , sizeof(struct ota_data));
data.header.frame_flag = 0x5A5A;
memcpy(data.frame.frame_data,"aiit_ota_start",strlen("aiit_ota_start"));
data.frame.frame_len = strlen("aiit_ota_start");
memcpy(data.end,"!@",2);
printf("send start signal.\n");
ret = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if (ret > 0){
printf("send %s[%d] Bytes\n",data.frame.frame_data,ret);
}
// sleep(1);
len = recv(fd, buf, sizeof(buf), 0);
if (len <= 0)
memset(buf, 0, 32);
length = recv(fd, buf, sizeof(buf), 0);
if (length <= 0)
{
continue;
}
else
{
printf("recv buf %s\n",buf);
if(0 == strncmp(buf, "ready", len))
printf("recv buf %s length %d\n",buf,length);
if(0 == strncmp(buf, "ready", length))
{
ret = OtaFileSend(fd);
if (ret == 0) {
@ -263,7 +316,7 @@ void server(void)
if (fd == -1)
{
printf("The client connection is wrong...\n");
// printf("The client connection is wrong...\n");
continue;
}

View File

@ -268,7 +268,7 @@ static int Ec200tSend(struct Adapter *adapter, const void *buf, size_t len)
static int Ec200tRecv(struct Adapter *adapter, void *buf, size_t len)
{
if (adapter->agent) {
return EntmRecv(adapter->agent, (char *)buf, len, 40000);
return EntmRecv(adapter->agent, (char *)buf, len, 6);
} else {
printf("Ec200tRecv can not find agent\n");
}

View File

@ -153,18 +153,23 @@ int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const ch
PrivMutexAbandon(&agent->lock);
if(agent->reply != NULL) {
PrivMutexObtain(&agent->lock);
reply->reply_len = 0;
va_start(params, cmd_expr);
ATSprintf(agent->fd, cmd_expr, params);
va_end(params);
PrivMutexAbandon(&agent->lock);
if (PrivSemaphoreObtainWait(&agent->rsp_sem, &abstime) != EOK) {
printf("take sem %d timeout\n",agent->rsp_sem);
result = -ETIMEOUT;
goto __out;
}
} else {
PrivMutexObtain(&agent->lock);
va_start(params, cmd_expr);
ATSprintf(agent->fd, cmd_expr, params);
va_end(params);
PrivMutexAbandon(&agent->lock);
goto __out;
}
@ -256,14 +261,22 @@ int AtSetReplyCharNum(ATAgentType agent, unsigned int num)
int EntmSend(ATAgentType agent, const char *data, int len)
{
char send_buf[128];
if(len > 128){
printf("send length %d more then max 128 Bytes.\n",len);
return -1;
}
PrivMutexObtain(&agent->lock);
memset(send_buf, 0, 128);
agent->receive_mode = ENTM_MODE;
memcpy(send_buf, data, len);
memcpy(send_buf + len, "!@", 2);
PrivWrite(agent->fd, send_buf, len + 2);
// memcpy(send_buf + len, "!@", 2);
PrivWrite(agent->fd, send_buf, len);
PrivMutexAbandon(&agent->lock);
printf("entm send %s length %d\n",send_buf, len);
return EOK;
}
@ -272,32 +285,32 @@ int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
struct timespec abstime;
abstime.tv_sec = timeout_s;
if(buffer_len > ENTM_RECV_MAX){
printf("read length more then max length[%d] Bytes",ENTM_RECV_MAX);
return -1;
}
PrivMutexObtain(&agent->lock);
agent->receive_mode = ENTM_MODE;
PrivTaskDelay(1000);
agent->read_len = buffer_len;
PrivMutexAbandon(&agent->lock);
// PrivTaskDelay(1000);
if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) {
printf("wait sem[%d] timeout\n",agent->entm_rx_notice);
return -ERROR;
}
PrivMutexObtain(&agent->lock);
if (buffer_len < agent->entm_recv_len) {
return -ERROR;
}
printf("EntmRecv once len %u.\n", agent->entm_recv_len);
printf("EntmRecv once len %d.\n", agent->entm_recv_len);
agent->entm_recv_buf[agent->entm_recv_len - 1] = '\0';
memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len - 2);
memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len);
memset(agent->entm_recv_buf, 0, ENTM_RECV_MAX);
agent->entm_recv_len = 0;
agent->read_len = 0;
PrivMutexAbandon(&agent->lock);
return EOK;
return buffer_len;
}
static int GetCompleteATReply(ATAgentType agent)
@ -311,13 +324,16 @@ static int GetCompleteATReply(ATAgentType agent)
memset(agent->maintain_buffer, 0x00, agent->maintain_max);
agent->maintain_len = 0;
memset(agent->entm_recv_buf, 0x00, 256);
agent->entm_recv_len = 0;
PrivMutexAbandon(&agent->lock);
while (1)
{
PrivRead(agent->fd, &ch, 1);
#ifdef CONNECTION_FRAMEWORK_DEBUG
// printf(" %c (0x%x)\n", ch, ch);
printf(" %c (0x%x)\n", ch, ch);
#endif
PrivMutexObtain(&agent->lock);
@ -328,13 +344,17 @@ static int GetCompleteATReply(ATAgentType agent)
agent->entm_recv_buf[agent->entm_recv_len] = ch;
agent->entm_recv_len++;
if (last_ch == '!' && ch == '@')
if(agent->entm_recv_len < agent->read_len){
PrivMutexAbandon(&agent->lock);
continue;
}
else
{
printf("ENTM_MODE recv %d Bytes done.\n",agent->entm_recv_len);
agent->receive_mode = DEFAULT_MODE;
PrivSemaphoreAbandon(&agent->entm_rx_notice);
}
last_ch = ch;
} else {
printf("entm_recv_buf is_full ...\n");
}
@ -364,12 +384,13 @@ static int GetCompleteATReply(ATAgentType agent)
}
printf("GetCompleteATReply done\n");
PrivMutexAbandon(&agent->lock);
agent->receive_mode = DEFAULT_MODE;
PrivMutexAbandon(&agent->lock);
break;
}
last_ch = ch;
}
PrivMutexAbandon(&agent->lock);
}
@ -432,8 +453,8 @@ static void *ATAgentReceiveProcess(void *param)
while (1) {
if (GetCompleteATReply(agent) > 0)
{
if (agent->reply != NULL){
PrivMutexObtain(&agent->lock);
if (agent->reply != NULL){
ATReplyType reply = agent->reply;
agent->maintain_buffer[agent->maintain_len] = '\0';
@ -446,9 +467,9 @@ static void *ATAgentReceiveProcess(void *param)
}
// agent->reply = NULL;
PrivMutexAbandon(&agent->lock);
PrivSemaphoreAbandon(&agent->rsp_sem);
}
PrivMutexAbandon(&agent->lock);
}
}
}
@ -473,13 +494,13 @@ static int ATAgentInit(ATAgentType agent)
printf("ATAgentInit create entm sem error\n");
goto __out;
}
printf("create entm_rx_notice_sem %d\n ",agent->entm_rx_notice);
result = PrivSemaphoreCreate(&agent->rsp_sem, 0, 0);
if (result < 0) {
printf("ATAgentInit create rsp sem error\n");
goto __out;
}
printf("create rsp_sem %d\n ",agent->rsp_sem);
if(PrivMutexCreate(&agent->lock, 0) < 0) {
printf("AdapterFrameworkInit mutex create failed.\n");
goto __out;

View File

@ -26,7 +26,7 @@
#include <stdio.h>
#include <sys/types.h>
#define REPLY_TIME_OUT 6
#define REPLY_TIME_OUT 10
enum ReceiveMode
{
@ -47,6 +47,7 @@ struct ATAgent
{
char agent_name[64];
int fd;
int read_len; ///< control data length of wanting reading
char *maintain_buffer;
uint32 maintain_len;