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

View File

@ -26,7 +26,6 @@ int clientfd[100000];//客户端的socketfd,100个元素clientfd[0]~clientfd[
int size = 99999;//用来控制进入聊天室的人数为50以内 int size = 99999;//用来控制进入聊天室的人数为50以内
int PORT = 9898;//端口号 int PORT = 9898;//端口号
typedef struct sockaddr meng; typedef struct sockaddr meng;
socklen_t len;
struct ota_header_t struct ota_header_t
{ {
@ -51,7 +50,7 @@ struct ota_data
{ {
struct ota_header_t header; struct ota_header_t header;
struct ota_frame_t frame; struct ota_frame_t frame;
char end[2]; char end[4];
}; };
pthread_t ota_ktask; pthread_t ota_ktask;
@ -98,8 +97,11 @@ void init(void)
addr.sin_addr.s_addr = htons(INADDR_ANY);//当sin_addr = INADDR_ANY时表示从本机的任一网卡接收数据 addr.sin_addr.s_addr = htons(INADDR_ANY);//当sin_addr = INADDR_ANY时表示从本机的任一网卡接收数据
//绑定套接字 //绑定套接字
int on = 1; // int on = 1;
if(setsockopt(serverfd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(int)) < 0) struct timeval timeout;
timeout.tv_sec = 5;
timeout.tv_usec = 0;
if(setsockopt(serverfd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
{ {
perror("端口设置失败"); perror("端口设置失败");
exit(-1); exit(-1);
@ -123,8 +125,9 @@ int OtaFileSend(int fd)
struct ota_data data; struct ota_data data;
FILE *file_fd; FILE *file_fd;
char ch; char ch;
int len = 0; int length = 0;
int try_times = 5; int try_times = 10;
int recv_end_times = 3;
int ret = 0; int ret = 0;
int frame_cnt = 0; int frame_cnt = 0;
int file_length = 0; int file_length = 0;
@ -137,30 +140,43 @@ int OtaFileSend(int fd)
} }
fseek(file_fd, 0, SEEK_SET); fseek(file_fd, 0, SEEK_SET);
printf("start send file.\n"); printf("start send file.\n");
// while((ch = fgetc(file_fd)) != EOF)
while(!feof(file_fd)) while(!feof(file_fd))
{ {
memset(&data, 0, sizeof(data)); memset(&data, 0, sizeof(data));
data.header.frame_flag = 0x5A5A; data.header.frame_flag = 0x5A5A;
len = fread( data.frame.frame_data, 1, 64, file_fd ); length = fread( data.frame.frame_data, 1, 64, file_fd );
if(len > 0) if(length > 0)
{ {
printf("read %d Bytes\n",length);
data.frame.frame_id = frame_cnt; data.frame.frame_id = frame_cnt;
data.frame.frame_len = len; data.frame.frame_len = length;
data.frame.crc = OtaCrc16(data.frame.frame_data, len); data.frame.crc = OtaCrc16(data.frame.frame_data, length);
file_length += len; file_length += length;
} }
memcpy(data.end,"!@",2);
fseek(file_fd, len, SEEK_CUR);
try_again: send_again:
send(fd, &data, sizeof(data), MSG_NOSIGNAL); usleep(50000);
len = recv(fd, buf, sizeof(buf), 0); printf("ota send current[%d] frame.\n",frame_cnt);
if(0 == strncmp(buf, "ok", len)) 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 = 5; try_times = 10;
printf("ota send current[%d] frame.\n",frame_cnt); printf("[%d]frame data send done.\n",frame_cnt);
frame_cnt++; frame_cnt++;
continue; continue;
} }
@ -169,34 +185,68 @@ try_again:
if(try_times > 0) if(try_times > 0)
{ {
try_times--; try_times--;
goto try_again; goto send_again;
} }
else else
{ {
printf("send frame[%d] 5 times failed.\n",frame_cnt); printf("send frame[%d] 10 times failed.\n",frame_cnt);
ret = -1; ret = -1;
break; break;
} }
} }
} }
/* finally,check total bin file.*/
if (ret == 0) 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); file_buf = malloc(file_length);
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 = 0x5A5A;
fseek(file_fd, 0, SEEK_SET);
len = fread(file_buf, file_length,1,file_fd); length = fread(file_buf,1, file_length, file_fd);
if(len > 0) { if(length > 0) {
data.header.total_len = file_length; data.header.total_len = file_length;
data.frame.frame_len = strlen("aiit_ota_end");; data.frame.frame_len = strlen("aiit_ota_end");
data.frame.crc = OtaCrc16(file_buf, len); data.frame.crc = OtaCrc16(file_buf, length);
memcpy(data.frame.frame_data,"aiit_ota_end",strlen("aiit_ota_end")); 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); free(file_buf);
} }
@ -210,30 +260,33 @@ void* server_thread(void* p)
unsigned char buf[32] = { 0 }; unsigned char buf[32] = { 0 };
struct ota_data data; struct ota_data data;
int ret = 0; int ret = 0;
int length = 0;
printf("pthread = %d\n",fd); printf("pthread = %d\n",fd);
// sleep(5); sleep(8);
while(1) while(1)
{ {
memset(&data, 0 , sizeof(struct ota_data)); memset(&data, 0x0 , sizeof(struct ota_data));
data.header.frame_flag = 0x5A5A; data.header.frame_flag = 0x5A5A;
memcpy(data.frame.frame_data,"aiit_ota_start",strlen("aiit_ota_start")); memcpy(data.frame.frame_data,"aiit_ota_start",strlen("aiit_ota_start"));
data.frame.frame_len = 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); ret = send(fd, &data, sizeof(data), MSG_NOSIGNAL);
if (ret > 0){ 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);
} }
// sleep(1); // sleep(1);
len = recv(fd, buf, sizeof(buf), 0); memset(buf, 0, 32);
if (len <= 0) length = recv(fd, buf, sizeof(buf), 0);
if (length <= 0)
{ {
continue; continue;
} }
else else
{ {
printf("recv buf %s\n",buf); printf("recv buf %s length %d\n",buf,length);
if(0 == strncmp(buf, "ready", len)) if(0 == strncmp(buf, "ready", length))
{ {
ret = OtaFileSend(fd); ret = OtaFileSend(fd);
if (ret == 0) { if (ret == 0) {
@ -263,7 +316,7 @@ void server(void)
if (fd == -1) if (fd == -1)
{ {
printf("The client connection is wrong...\n"); // printf("The client connection is wrong...\n");
continue; 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) static int Ec200tRecv(struct Adapter *adapter, void *buf, size_t len)
{ {
if (adapter->agent) { if (adapter->agent) {
return EntmRecv(adapter->agent, (char *)buf, len, 40000); return EntmRecv(adapter->agent, (char *)buf, len, 6);
} else { } else {
printf("Ec200tRecv can not find agent\n"); 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); PrivMutexAbandon(&agent->lock);
if(agent->reply != NULL) { if(agent->reply != NULL) {
PrivMutexObtain(&agent->lock);
reply->reply_len = 0; reply->reply_len = 0;
va_start(params, cmd_expr); va_start(params, cmd_expr);
ATSprintf(agent->fd, cmd_expr, params); ATSprintf(agent->fd, cmd_expr, params);
va_end(params); va_end(params);
PrivMutexAbandon(&agent->lock);
if (PrivSemaphoreObtainWait(&agent->rsp_sem, &abstime) != EOK) { if (PrivSemaphoreObtainWait(&agent->rsp_sem, &abstime) != EOK) {
printf("take sem %d timeout\n",agent->rsp_sem);
result = -ETIMEOUT; result = -ETIMEOUT;
goto __out; goto __out;
} }
} else { } else {
PrivMutexObtain(&agent->lock);
va_start(params, cmd_expr); va_start(params, cmd_expr);
ATSprintf(agent->fd, cmd_expr, params); ATSprintf(agent->fd, cmd_expr, params);
va_end(params); va_end(params);
PrivMutexAbandon(&agent->lock);
goto __out; goto __out;
} }
@ -256,14 +261,22 @@ 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]; 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); memset(send_buf, 0, 128);
agent->receive_mode = ENTM_MODE; agent->receive_mode = ENTM_MODE;
memcpy(send_buf, data, len); memcpy(send_buf, data, len);
memcpy(send_buf + len, "!@", 2); // memcpy(send_buf + len, "!@", 2);
PrivWrite(agent->fd, 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; return EOK;
} }
@ -272,32 +285,32 @@ int EntmRecv(ATAgentType agent, char *rev_buffer, int buffer_len, int timeout_s)
struct timespec abstime; struct timespec abstime;
abstime.tv_sec = timeout_s; 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; agent->receive_mode = ENTM_MODE;
agent->read_len = buffer_len;
PrivTaskDelay(1000); PrivMutexAbandon(&agent->lock);
// PrivTaskDelay(1000);
if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) { if (PrivSemaphoreObtainWait(&agent->entm_rx_notice, &abstime)) {
printf("wait sem[%d] timeout\n",agent->entm_rx_notice);
return -ERROR; return -ERROR;
} }
PrivMutexObtain(&agent->lock); 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);
memcpy(rev_buffer, agent->entm_recv_buf, agent->entm_recv_len - 2);
memset(agent->entm_recv_buf, 0, ENTM_RECV_MAX); memset(agent->entm_recv_buf, 0, ENTM_RECV_MAX);
agent->entm_recv_len = 0; agent->entm_recv_len = 0;
agent->read_len = 0;
PrivMutexAbandon(&agent->lock); PrivMutexAbandon(&agent->lock);
return EOK; return buffer_len;
} }
static int GetCompleteATReply(ATAgentType agent) static int GetCompleteATReply(ATAgentType agent)
@ -311,13 +324,16 @@ static int GetCompleteATReply(ATAgentType agent)
memset(agent->maintain_buffer, 0x00, agent->maintain_max); memset(agent->maintain_buffer, 0x00, agent->maintain_max);
agent->maintain_len = 0; agent->maintain_len = 0;
memset(agent->entm_recv_buf, 0x00, 256);
agent->entm_recv_len = 0;
PrivMutexAbandon(&agent->lock); PrivMutexAbandon(&agent->lock);
while (1) while (1)
{ {
PrivRead(agent->fd, &ch, 1); PrivRead(agent->fd, &ch, 1);
#ifdef CONNECTION_FRAMEWORK_DEBUG #ifdef CONNECTION_FRAMEWORK_DEBUG
// printf(" %c (0x%x)\n", ch, ch); printf(" %c (0x%x)\n", ch, ch);
#endif #endif
PrivMutexObtain(&agent->lock); PrivMutexObtain(&agent->lock);
@ -328,13 +344,17 @@ static int GetCompleteATReply(ATAgentType agent)
agent->entm_recv_buf[agent->entm_recv_len] = ch; agent->entm_recv_buf[agent->entm_recv_len] = ch;
agent->entm_recv_len++; 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; agent->receive_mode = DEFAULT_MODE;
PrivSemaphoreAbandon(&agent->entm_rx_notice); PrivSemaphoreAbandon(&agent->entm_rx_notice);
} }
last_ch = ch;
} else { } else {
printf("entm_recv_buf is_full ...\n"); printf("entm_recv_buf is_full ...\n");
} }
@ -364,12 +384,13 @@ static int GetCompleteATReply(ATAgentType agent)
} }
printf("GetCompleteATReply done\n"); printf("GetCompleteATReply done\n");
PrivMutexAbandon(&agent->lock);
agent->receive_mode = DEFAULT_MODE; agent->receive_mode = DEFAULT_MODE;
PrivMutexAbandon(&agent->lock);
break; break;
} }
last_ch = ch; last_ch = ch;
} }
PrivMutexAbandon(&agent->lock); PrivMutexAbandon(&agent->lock);
} }
@ -432,8 +453,8 @@ static void *ATAgentReceiveProcess(void *param)
while (1) { while (1) {
if (GetCompleteATReply(agent) > 0) if (GetCompleteATReply(agent) > 0)
{ {
PrivMutexObtain(&agent->lock);
if (agent->reply != NULL){ if (agent->reply != NULL){
PrivMutexObtain(&agent->lock);
ATReplyType reply = agent->reply; ATReplyType reply = agent->reply;
agent->maintain_buffer[agent->maintain_len] = '\0'; agent->maintain_buffer[agent->maintain_len] = '\0';
@ -446,9 +467,9 @@ static void *ATAgentReceiveProcess(void *param)
} }
// agent->reply = NULL; // agent->reply = NULL;
PrivMutexAbandon(&agent->lock);
PrivSemaphoreAbandon(&agent->rsp_sem); PrivSemaphoreAbandon(&agent->rsp_sem);
} }
PrivMutexAbandon(&agent->lock);
} }
} }
} }
@ -473,13 +494,13 @@ static int ATAgentInit(ATAgentType agent)
printf("ATAgentInit create entm sem error\n"); printf("ATAgentInit create entm sem error\n");
goto __out; goto __out;
} }
printf("create entm_rx_notice_sem %d\n ",agent->entm_rx_notice);
result = PrivSemaphoreCreate(&agent->rsp_sem, 0, 0); result = PrivSemaphoreCreate(&agent->rsp_sem, 0, 0);
if (result < 0) { if (result < 0) {
printf("ATAgentInit create rsp sem error\n"); printf("ATAgentInit create rsp sem error\n");
goto __out; goto __out;
} }
printf("create rsp_sem %d\n ",agent->rsp_sem);
if(PrivMutexCreate(&agent->lock, 0) < 0) { if(PrivMutexCreate(&agent->lock, 0) < 0) {
printf("AdapterFrameworkInit mutex create failed.\n"); printf("AdapterFrameworkInit mutex create failed.\n");
goto __out; goto __out;

View File

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