diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index 2d2412802..649e21587 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -240,6 +240,7 @@ static void *OtaKTaskEntry(void *parameter) while(1) { + int connect_times = 5; ret = AdapterDeviceOpen(adapter); if(ret < 0) { @@ -248,12 +249,18 @@ static void *OtaKTaskEntry(void *parameter) } connect_again: + connect_times--; ret = AdapterDeviceConnect(adapter, 1, "115.238.53.61","9898",1); if(ret < 0) { - // AdapterDeviceClose(adapter); - // continue; - goto connect_again; + if(connect_times > 0){ + goto connect_again; + } + else + { + AdapterDeviceClose(adapter); + continue; + } } break; } @@ -270,7 +277,7 @@ connect_again: { memset(reply, 0, 16); memcpy(reply, "ready", strlen("ready")); - PrivTaskDelay(3000); + // PrivTaskDelay(3000); printf("receive start signal,send [ready] signal to server\n"); send_ready_again: ret = AdapterDeviceSend(adapter, reply, strlen(reply)); @@ -278,7 +285,7 @@ send_ready_again: { goto send_ready_again; } - + PrivTaskDelay(3000); printf("start receive ota file.\n"); /* step2: start receive source bin file of application*/ ret = OtaDataRecv(adapter); @@ -299,9 +306,10 @@ send_ready_again: { memset(reply, 0, 16); memcpy(reply, "notready", strlen("notready")); + printf("ota status:not ready\n"); ret = AdapterDeviceSend(adapter, reply, strlen(reply)); } - PrivTaskDelay(3000); /* check ota signal every 3s */ + PrivTaskDelay(5000); /* check ota signal every 5s */ } AdapterDeviceClose(adapter); @@ -310,8 +318,8 @@ send_ready_again: void ApplicationOtaTaskInit(void) { pthread_attr_t attr; - attr.schedparam.sched_priority = 10; - attr.stacksize = 2048; + attr.schedparam.sched_priority = 20; + attr.stacksize = 4096; PrivTaskCreate(&ota_task, &attr, OtaKTaskEntry, NULL); diff --git a/APP_Framework/Applications/ota/ota_server.c b/APP_Framework/Applications/ota/ota_server.c index f84b4ae58..604d94da1 100644 --- a/APP_Framework/Applications/ota/ota_server.c +++ b/APP_Framework/Applications/ota/ota_server.c @@ -212,7 +212,7 @@ void* server_thread(void* p) int ret = 0; printf("pthread = %d\n",fd); - sleep(10); + // sleep(5); while(1) { memset(&data, 0 , sizeof(struct ota_data)); diff --git a/APP_Framework/Framework/connection/adapter_agent.c b/APP_Framework/Framework/connection/adapter_agent.c index 93a28356f..85d543bfa 100755 --- a/APP_Framework/Framework/connection/adapter_agent.c +++ b/APP_Framework/Framework/connection/adapter_agent.c @@ -135,6 +135,7 @@ int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const ch abstime.tv_sec = timeout_s; + PrivMutexObtain(&agent->lock); agent->receive_mode = AT_MODE; memset(agent->maintain_buffer, 0x00, agent->maintain_max); @@ -147,10 +148,10 @@ int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const ch uint32 cmd_size = 0; uint32 result = EOK; const char *cmd = NULL; - PrivMutexObtain(&agent->lock); agent->reply = reply; - + PrivMutexAbandon(&agent->lock); + if(agent->reply != NULL) { reply->reply_len = 0; va_start(params, cmd_expr); @@ -169,8 +170,6 @@ int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const ch __out: // agent->reply = NULL; - PrivMutexAbandon(&agent->lock); - return result; } @@ -188,7 +187,12 @@ int AtCmdConfigAndCheck(ATAgentType agent, char *cmd, char *check) ret = -1; goto __exit; } - ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd); + ret = ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd); + if(ret < 0){ + printf("%s %d ATOrderSend failed.\n",__func__,__LINE__); + ret = -1; + goto __exit; + } // PrivTaskDelay(3000); result = GetReplyText(reply); @@ -302,24 +306,31 @@ static int GetCompleteATReply(ATAgentType agent) char ch = 0, last_ch = 0; bool is_full = false; + PrivMutexObtain(&agent->lock); + memset(agent->maintain_buffer, 0x00, agent->maintain_max); agent->maintain_len = 0; - while (1) { + PrivMutexAbandon(&agent->lock); + + while (1) + { PrivRead(agent->fd, &ch, 1); #ifdef CONNECTION_FRAMEWORK_DEBUG - printf("data[%d] %c (0x%x)\n",agent->maintain_len, ch, ch); + // printf(" %c (0x%x)\n", ch, ch); #endif - if (agent->receive_mode == ENTM_MODE){ - if (agent->entm_recv_len < ENTM_RECV_MAX) { - PrivMutexObtain(&agent->lock); + PrivMutexObtain(&agent->lock); + if (agent->receive_mode == ENTM_MODE) + { + if (agent->entm_recv_len < ENTM_RECV_MAX) + { agent->entm_recv_buf[agent->entm_recv_len] = ch; agent->entm_recv_len++; - PrivMutexAbandon(&agent->lock); - - if (last_ch == '!' && ch == '@'){ + if (last_ch == '!' && ch == '@') + { + agent->receive_mode = DEFAULT_MODE; PrivSemaphoreAbandon(&agent->entm_rx_notice); } @@ -327,8 +338,11 @@ static int GetCompleteATReply(ATAgentType agent) } else { printf("entm_recv_buf is_full ...\n"); } - } else if (agent->receive_mode == AT_MODE) { - if (read_len < agent->maintain_max){ + } + else if (agent->receive_mode == AT_MODE) + { + if (read_len < agent->maintain_max) + { agent->maintain_buffer[read_len] = ch; read_len++; agent->maintain_len = read_len; @@ -345,13 +359,18 @@ static int GetCompleteATReply(ATAgentType agent) printf("read line failed. The line data length is out of buffer size(%d)!", agent->maintain_max); memset(agent->maintain_buffer, 0x00, agent->maintain_max); agent->maintain_len = 0; + PrivMutexAbandon(&agent->lock); return -ERROR; } + printf("GetCompleteATReply done\n"); + PrivMutexAbandon(&agent->lock); + agent->receive_mode = DEFAULT_MODE; break; } last_ch = ch; } + PrivMutexAbandon(&agent->lock); } return read_len; @@ -373,21 +392,27 @@ ATAgentType GetATAgent(const char *agent_name) int DeleteATAgent(ATAgentType agent) { printf("delete agent->at_handler = %d\n",agent->at_handler); - PrivTaskDelete(agent->at_handler, 0); - + if(agent->at_handler > 0){ + PrivTaskDelete(agent->at_handler, 0); + } + if (agent->fd > 0) { + printf("close agent fd = %d\n",agent->fd); PrivClose(agent->fd); } if (agent->lock) { + printf("delete agent lock = %d\n",agent->lock); PrivMutexDelete(&agent->lock); } if (agent->entm_rx_notice) { + printf("delete agent entm_rx_notice = %d\n",agent->entm_rx_notice); PrivSemaphoreDelete(&agent->entm_rx_notice); } if (agent->rsp_sem) { + printf("delete agent rsp_sem = %d\n",agent->rsp_sem); PrivSemaphoreDelete(&agent->rsp_sem); } @@ -396,6 +421,7 @@ int DeleteATAgent(ATAgentType agent) } memset(agent, 0x00, sizeof(struct ATAgent)); + printf("delete ATagent\n"); } static void *ATAgentReceiveProcess(void *param) @@ -404,8 +430,10 @@ static void *ATAgentReceiveProcess(void *param) const struct at_urc *urc; while (1) { - if (GetCompleteATReply(agent) > 0) { + if (GetCompleteATReply(agent) > 0) + { if (agent->reply != NULL){ + PrivMutexObtain(&agent->lock); ATReplyType reply = agent->reply; agent->maintain_buffer[agent->maintain_len] = '\0'; @@ -418,7 +446,7 @@ static void *ATAgentReceiveProcess(void *param) } // agent->reply = NULL; - agent->receive_mode = DEFAULT_MODE; + PrivMutexAbandon(&agent->lock); PrivSemaphoreAbandon(&agent->rsp_sem); } } @@ -457,11 +485,11 @@ static int ATAgentInit(ATAgentType agent) goto __out; } - agent->receive_mode = ENTM_MODE; + agent->receive_mode = DEFAULT_MODE; pthread_attr_t attr; attr.schedparam.sched_priority = 18; - attr.stacksize = 2048; + attr.stacksize = 4096; PrivTaskCreate(&agent->at_handler, &attr, ATAgentReceiveProcess, agent); printf("create agent->at_handler = %d\n",agent->at_handler); @@ -520,6 +548,7 @@ ATReplyType CreateATReply(uint32 reply_max_len) printf("no more memory\n"); return NULL; } + memset(reply, 0, sizeof(struct ATReply)); reply->reply_max_len = reply_max_len; diff --git a/APP_Framework/Framework/connection/nbiot/bc28/bc28.c b/APP_Framework/Framework/connection/nbiot/bc28/bc28.c index a5e1d2fc5..e9c83ac34 100644 --- a/APP_Framework/Framework/connection/nbiot/bc28/bc28.c +++ b/APP_Framework/Framework/connection/nbiot/bc28/bc28.c @@ -30,7 +30,7 @@ #define NET_TYPE_AF_INET6 (1) #define SOCKET_INVALID_ID (-1) - +static int nbiot_lock; static int BC28UartOpen(struct Adapter *adapter) { @@ -113,7 +113,14 @@ int NBIoTStatusCheck(struct Adapter *adapter ) char at_cmd[64] = {0}; AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B); /* set receive end flag as 'OK'*/ - + memset(at_cmd, 0 ,64); + memcpy(at_cmd, "AT+NCONFIG=AUTOCONNECT,TRUE", 27); + strcat(at_cmd, "\n"); + printf("cmd : %s\n", at_cmd); + ATOrderSend(adapter->agent, 0, NULL, at_cmd); + + PrivTaskDelay(10000); + // memset(at_cmd, 0 ,64); // memcpy(at_cmd, "AT+NRB", 6); // strcat(at_cmd, "\n"); @@ -139,19 +146,8 @@ int NBIoTStatusCheck(struct Adapter *adapter ) // } - memset(at_cmd, 0 ,64); - memcpy(at_cmd, "AT+CFUN=1", 10); - strcat(at_cmd, "\n"); - printf("cmd : %s\n", at_cmd); - result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); - if(result < 0) { - printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); - result = -1; - goto out; - } - // memset(at_cmd, 0 ,64); - // memcpy(at_cmd, "AT+CFUN?", 8); + // memcpy(at_cmd, "AT+CFUN=1", 10); // strcat(at_cmd, "\n"); // printf("cmd : %s\n", at_cmd); // result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); @@ -161,6 +157,18 @@ int NBIoTStatusCheck(struct Adapter *adapter ) // goto out; // } + AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B); /* set receive end flag as 'OK'*/ + memset(at_cmd, 0 ,64); + memcpy(at_cmd, "AT+CFUN?", 8); + strcat(at_cmd, "\n"); + printf("cmd : %s\n", at_cmd); + result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); + if(result < 0) { + printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); + result = -1; + goto out; + } + memset(at_cmd, 0 ,64); memcpy(at_cmd, "AT+CIMI", 7); strcat(at_cmd, "\n"); @@ -173,10 +181,10 @@ int NBIoTStatusCheck(struct Adapter *adapter ) } memset(at_cmd, 0 ,64); - memcpy(at_cmd, "AT+CGATT=1", 10); + memcpy(at_cmd, "AT+CEREG?", 9); strcat(at_cmd, "\n"); printf("cmd : %s\n", at_cmd); - result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); + result = AtCmdConfigAndCheck(adapter->agent, at_cmd, ",1"); if(result < 0) { printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); result = -1; @@ -184,18 +192,7 @@ int NBIoTStatusCheck(struct Adapter *adapter ) } // memset(at_cmd, 0 ,64); - // memcpy(at_cmd, "AT+CGATT?", 9); - // strcat(at_cmd, "\n"); - // printf("cmd : %s\n", at_cmd); - // result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); - // if(result < 0) { - // printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); - // result = -1; - // goto out; - // } - - // memset(at_cmd, 0 ,64); - // memcpy(at_cmd, "AT+CEREG?", 9); + // memcpy(at_cmd, "AT+CGATT=1", 10); // strcat(at_cmd, "\n"); // printf("cmd : %s\n", at_cmd); // result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); @@ -206,7 +203,7 @@ int NBIoTStatusCheck(struct Adapter *adapter ) // } memset(at_cmd, 0 ,64); - memcpy(at_cmd, "AT+QREGSWT=2", 12); + memcpy(at_cmd, "AT+CGATT?", 9); strcat(at_cmd, "\n"); printf("cmd : %s\n", at_cmd); result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); @@ -221,12 +218,12 @@ int NBIoTStatusCheck(struct Adapter *adapter ) strcat(at_cmd, "\n"); printf("cmd : %s\n", at_cmd); result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); - // if(result < 0) { - // printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); - // result = -1; - // goto out; - // } - + if(result < 0) { + printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); + result = -1; + goto out; + } + memset(at_cmd, 0 ,64); memcpy(at_cmd, "AT+CGPADDR", 10); strcat(at_cmd, "\n"); @@ -236,6 +233,18 @@ int NBIoTStatusCheck(struct Adapter *adapter ) printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); result = -1; } + + memset(at_cmd, 0 ,64); + memcpy(at_cmd, "AT+QREGSWT=2", 12); + strcat(at_cmd, "\n"); + printf("cmd : %s\n", at_cmd); + result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); + if(result < 0) { + printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); + result = -1; + goto out; + } + out: return result; } @@ -464,44 +473,56 @@ static int BC28Open(struct Adapter *adapter) if (NULL == adapter) { return -1; } + + if(PrivMutexCreate(&nbiot_lock, 0) < 0) { + printf("nbiot_lock mutex create failed.\n"); + return -1; + } + /*step1: open BC8 serial port*/ ret = BC28UartOpen(adapter); if (ret < 0) { printf("bc18 setup failed.\n"); + PrivMutexDelete(&nbiot_lock); return -1; } + /*step2: init AT agent*/ if (!adapter->agent) { char *agent_name = "niot_device"; if (EOK != InitATAgent(agent_name, adapter->fd, 512)) { PrivClose(adapter->fd); + PrivMutexDelete(&nbiot_lock); printf("at agent init failed !\n"); return -1; } ATAgentType at_agent = GetATAgent(agent_name); adapter->agent = at_agent; } + + // BC28PowerSet(); /* reset bc28 module by set reset pin */ + PrivTaskDelay(6000); + + ret = NBIoTStatusCheck(adapter); /* ask module status*/ + if(ret < 0){ + DeleteATAgent(adapter->agent); + adapter->agent = NULL; + PrivMutexDelete(&nbiot_lock); + printf("NBIot status check failed.\n"); + return -1; + } + create_socket.type = SOCKET_TYPE_STREAM; create_socket.listen_port = 0; create_socket.socket_id = 1; create_socket.af_type = NET_TYPE_AF_INET; - BC28PowerSet(); /* reset bc28 module by set reset pin */ - PrivTaskDelay(6000); - - ret = NBIoTStatusCheck(adapter); /* ask module status*/ - // if(ret < 0){ - // PrivClose(adapter->fd); - // printf("NBIot status check failed.\n"); - // return -1; - // } - /*step3: create a tcp socket default */ ret = NBIoTSocketCreate(adapter, &create_socket); - if(ret < 0){ + if(ret < 0) { DeleteATAgent(adapter->agent); - // adapter->agent = NULL; - PrivClose(adapter->fd); + adapter->agent = NULL; + PrivMutexDelete(&nbiot_lock); printf("NBIot create tcp socket failed.\n"); return -1; } @@ -512,16 +533,25 @@ static int BC28Open(struct Adapter *adapter) static int BC28Close(struct Adapter *adapter) { - NBIoTSocketDelete(adapter); - DeleteATAgent(adapter->agent); - adapter->agent = NULL; - PrivClose(adapter->fd); + + if(&adapter->socket){ + NBIoTSocketDelete(adapter); + } + + if(adapter->agent) + { + DeleteATAgent(adapter->agent); + adapter->agent = NULL; + } + + PrivMutexDelete(&nbiot_lock); return 0; } static int BC28Ioctl(struct Adapter *adapter, int cmd, void *args) { int ret = 0; + PrivMutexObtain(&nbiot_lock); switch (cmd) { case CONFIG_NBIOT_RESET: /* reset nbiot */ @@ -541,7 +571,7 @@ static int BC28Ioctl(struct Adapter *adapter, int cmd, void *args) ret = -1; break; } - + PrivMutexAbandon(&nbiot_lock); return ret; } @@ -564,6 +594,7 @@ static int BC28Connect(struct Adapter *adapter, enum NetRoleType net_role, const char at_cmd[64] = {0}; char str_fd[2] = {0}; + PrivMutexObtain(&nbiot_lock); itoa(adapter->socket.socket_id, str_fd, 10); memset(at_cmd, 0 ,64); @@ -582,7 +613,7 @@ static int BC28Connect(struct Adapter *adapter, enum NetRoleType net_role, const printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); result = -1; } - + PrivMutexAbandon(&nbiot_lock); __exit: return result; } @@ -593,11 +624,14 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len) char at_cmd[64] = {0}; char str_fd[2] = {0}; char assic_str[2] = {0}; - - char *nbdata = PrivMalloc(2 * len); int assic_val = 0; int length = 0; + PrivMutexObtain(&nbiot_lock); + + char *nbdata = PrivMalloc(2 * len); + memset(nbdata, 0 ,2 * len); + for(int i = 0; i < len; i++) { assic_val = 0; @@ -656,7 +690,7 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len) printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); result = -1; } - + PrivMutexAbandon(&nbiot_lock); return result; } @@ -669,10 +703,12 @@ static int BC28Recv(struct Adapter *adapter, void *buf, size_t len) int ret = 0; char *result = NULL; + PrivMutexObtain(&nbiot_lock); - ATReplyType reply = CreateATReply(256); + ATReplyType reply = CreateATReply(512); if (NULL == reply) { printf("at create failed ! \n"); + PrivMutexAbandon(&nbiot_lock); return -1; } @@ -687,18 +723,30 @@ static int BC28Recv(struct Adapter *adapter, void *buf, size_t len) strcat(at_cmd, "\n"); printf("cmd : %s\n", at_cmd); - ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd); - PrivTaskDelay(300); - - ret = BC28ParseData(buf, reply); - if (ret < 0){ - return ret; + ret = ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd); + if(ret < 0) + { + printf("NBiot receive timeout\n"); + DeleteATReply(reply); + return -1; } - if (reply) { + if(reply) + { + ret = BC28ParseData(buf, reply); + if (ret < 0) + { + PrivMutexAbandon(&nbiot_lock); + if (reply) + { + DeleteATReply(reply); + } + return ret; + } DeleteATReply(reply); } - + + PrivMutexAbandon(&nbiot_lock); return ret; } diff --git a/APP_Framework/Framework/transform_layer/xiuos/transform.c b/APP_Framework/Framework/transform_layer/xiuos/transform.c index 2eb00b8a3..e7eb652c9 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/transform.c +++ b/APP_Framework/Framework/transform_layer/xiuos/transform.c @@ -79,11 +79,13 @@ int PrivTaskStartup(pthread_t *thread) { return 0; } +#ifdef SEPARATE_COMPILE /* private API of xiuos to search the first user task in manage list */ int PrivUserTaskSearch(void) { return UserTaskSearch(); } +#endif int PrivTaskDelete(pthread_t thread, int sig) {