diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index 4700f2da3..2d2412802 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -29,17 +29,17 @@ struct ota_header_t int16 frame_flag; ///< frame start flag 2 Bytes uint8 dev_type; ///< device type uint8 burn_mode; ///< data burn way - unsigned long total_len; ///< send data total length caculated from each frame_len - unsigned long dev_hid; ///< device hardware version - unsigned long dev_sid; ///< device software version - char resv[32]; ///< reserve + uint32 total_len; ///< send data total length caculated from each frame_len + uint32 dev_hid; ///< device hardware version + uint32 dev_sid; ///< device software version + char resv[8]; ///< reserve }; struct ota_frame_t { uint32 frame_id; ///< Current frame id uint32 frame_len; ///< Current frame data length - char frame_data[224]; ///< Current frame data,max length 224 + char frame_data[64]; ///< Current frame data,max length 64 uint32 crc; ///< Current frame data crc }; @@ -47,6 +47,7 @@ struct ota_data { struct ota_header_t header; struct ota_frame_t frame; + char end[2]; }; pthread_t ota_task; @@ -149,6 +150,11 @@ static int OtaDataRecv(struct Adapter* adapter) int fd = 0; fd = open( BOARD_APP_NAME, O_RDWR | O_CREAT | O_TRUNC); + if(fd < 0) + { + printf("open %s failed\n",BOARD_APP_NAME); + return -1; + } while(1) { memset(&recv_msg, 0, sizeof(struct ota_data)); @@ -168,73 +174,117 @@ static int OtaDataRecv(struct Adapter* adapter) 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); 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",recv_msg.frame.frame_id); goto try_again; } memset(reply, 0, 16); memcpy(reply, "ok", strlen("ok")); + PrivTaskDelay(200); +send_ok_again: + ret = AdapterDeviceSend(adapter, reply, strlen(reply)); + if(ret < 0){ + printf("send ok failed.\n"); + goto send_ok_again; + } + try_times = 5; } 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 5 times failed,break out!\n",recv_msg.frame.frame_id); ret = -1; break; } memset(reply, 0, 16); - memcpy(reply, "try_again", strlen("try_again")); + memcpy(reply, "retry", strlen("retry")); AdapterDeviceSend(adapter, reply, strlen(reply)); try_times--; - continue; } } close(fd); - RestartApplication(); + + if(0 == ret) { + RestartApplication(); + } return ret; } static void *OtaKTaskEntry(void *parameter) { struct ota_data recv_msg; - char reply[4] = {0}; + char reply[16] = {0}; int baud_rate = BAUD_RATE_115200; int len = 0; int ret = 0; - struct Adapter* adapter = AdapterDeviceFindByName("4G"); - uint8 server_addr[64] = "101.68.82.219"; - 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"); + + while(1) + { + ret = AdapterDeviceOpen(adapter); + if(ret < 0) + { + printf("open adapter failed\n"); + continue; + } + +connect_again: + ret = AdapterDeviceConnect(adapter, 1, "115.238.53.61","9898",1); + if(ret < 0) + { + // AdapterDeviceClose(adapter); + // continue; + goto connect_again; + } + break; + } + while(1) { memset(&recv_msg, 0, sizeof(struct ota_data)); /* step1: Confirm the start signal of transmission*/ + ret = AdapterDeviceRecv(adapter, &recv_msg, 256); - 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"))) { - memset(reply, 0, 4); - memcpy(reply, "ok", strlen("ok")); - AdapterDeviceSend(adapter, reply, strlen(reply)); + memset(reply, 0, 16); + memcpy(reply, "ready", strlen("ready")); + PrivTaskDelay(3000); + printf("receive start signal,send [ready] signal to server\n"); +send_ready_again: + ret = AdapterDeviceSend(adapter, reply, strlen(reply)); + if(ret < 0) + { + goto send_ready_again; + } + printf("start receive ota file.\n"); /* step2: start receive source bin file of application*/ ret = OtaDataRecv(adapter); if (0 != ret) { - memset(reply, 0, 4); + memset(reply, 0, 16); memcpy(reply, "send_restart", strlen("send_restart")); AdapterDeviceSend(adapter, reply, strlen(reply)); continue; @@ -245,6 +295,12 @@ static void *OtaKTaskEntry(void *parameter) } } } + else + { + memset(reply, 0, 16); + memcpy(reply, "notready", strlen("notready")); + ret = AdapterDeviceSend(adapter, reply, strlen(reply)); + } PrivTaskDelay(3000); /* check ota signal every 3s */ } AdapterDeviceClose(adapter); diff --git a/APP_Framework/Applications/ota/ota_server.c b/APP_Framework/Applications/ota/ota_server.c index e59d184c5..f84b4ae58 100644 --- a/APP_Framework/Applications/ota/ota_server.c +++ b/APP_Framework/Applications/ota/ota_server.c @@ -33,17 +33,17 @@ struct ota_header_t int16_t frame_flag; ///< frame start flag 2 Bytes uint8_t dev_type; ///< device type uint8_t burn_mode; ///< data burn way - unsigned long total_len; ///< send data total length caculated from each frame_len - unsigned long dev_hid; ///< device hardware version - unsigned long dev_sid; ///< device software version - char resv[32]; ///< reserve + uint32_t total_len; ///< send data total length caculated from each frame_len + uint32_t dev_hid; ///< device hardware version + uint32_t dev_sid; ///< device software version + char resv[8]; ///< reserve }; struct ota_frame_t { uint32_t frame_id; ///< Current frame id uint32_t frame_len; ///< Current frame data length - char frame_data[224]; ///< Current frame data,max length 224 + char frame_data[64]; ///< Current frame data,max length 224 uint32_t crc; ///< Current frame data crc }; @@ -51,6 +51,7 @@ struct ota_data { struct ota_header_t header; struct ota_frame_t frame; + char end[2]; }; pthread_t ota_ktask; @@ -129,32 +130,37 @@ int OtaFileSend(int fd) int file_length = 0; char * file_buf = NULL; - file_fd = fopen("/tmp/xiuos_app.bin", "r"); + file_fd = fopen("/home/aep04/wwg/XiUOS_aiit-arm32-board_app.bin", "r"); if (NULL == file_fd){ printf("open file failed.\n"); return -1; } - - while((ch = fgetc(file_fd)) != EOF) + 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, 200, 1, file_fd ); - if(len > 0) { + len = fread( data.frame.frame_data, 1, 64, file_fd ); + if(len > 0) + { data.frame.frame_id = frame_cnt; data.frame.frame_len = len; data.frame.crc = OtaCrc16(data.frame.frame_data, len); file_length += len; } - memcpy(&data.frame.frame_data[len], "!@", strlen("!@")); /* add '!@' as ending flag */ + 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)) + if(0 == strncmp(buf, "ok", len)) { + try_times = 5; + printf("ota send current[%d] frame.\n",frame_cnt); frame_cnt++; continue; } @@ -185,9 +191,10 @@ try_again: len = fread(file_buf, file_length,1,file_fd); if(len > 0) { 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); - 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); free(file_buf); @@ -205,16 +212,19 @@ void* server_thread(void* p) int ret = 0; printf("pthread = %d\n",fd); - + sleep(10); while(1) { memset(&data, 0 , 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!@"); - - send(fd, &data, sizeof(data), MSG_NOSIGNAL); - + memcpy(data.frame.frame_data,"aiit_ota_start",strlen("aiit_ota_start")); + data.frame.frame_len = strlen("aiit_ota_start"); + memcpy(data.end,"!@",2); + 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) { @@ -222,7 +232,8 @@ void* server_thread(void* p) } else { - if(0 == strncmp(buf, "ok!@", len)) + printf("recv buf %s\n",buf); + if(0 == strncmp(buf, "ready", len)) { ret = OtaFileSend(fd); if (ret == 0) { @@ -234,12 +245,14 @@ void* server_thread(void* p) } } } + printf("exit fd = %d\n",fd); close(fd); + pthread_exit(0); } void server(void) { - printf("Server startup\n"); + printf("ota Server startup\n"); while(1) { struct sockaddr_in fromaddr; diff --git a/APP_Framework/Framework/connection/adapter_agent.c b/APP_Framework/Framework/connection/adapter_agent.c index 5cc8d9d5b..93a28356f 100755 --- a/APP_Framework/Framework/connection/adapter_agent.c +++ b/APP_Framework/Framework/connection/adapter_agent.c @@ -168,9 +168,8 @@ int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const ch } __out: - agent->reply = NULL; + // agent->reply = NULL; PrivMutexAbandon(&agent->lock); - agent->receive_mode = DEFAULT_MODE; return result; } @@ -183,15 +182,14 @@ int AtCmdConfigAndCheck(ATAgentType agent, char *cmd, char *check) return -1; } - ATReplyType reply = CreateATReply(64); + ATReplyType reply = CreateATReply(256); if (NULL == reply) { printf("%s %d at_create_resp failed!\n",__func__,__LINE__); ret = -1; goto __exit; } - ATOrderSend(agent, REPLY_TIME_OUT, reply, cmd); - PrivTaskDelay(3000); + // PrivTaskDelay(3000); result = GetReplyText(reply); if (!result) { @@ -310,7 +308,7 @@ static int GetCompleteATReply(ATAgentType agent) while (1) { PrivRead(agent->fd, &ch, 1); #ifdef CONNECTION_FRAMEWORK_DEBUG - printf(" %c (0x%x)\n", ch, ch); + printf("data[%d] %c (0x%x)\n",agent->maintain_len, ch, ch); #endif if (agent->receive_mode == ENTM_MODE){ if (agent->entm_recv_len < ENTM_RECV_MAX) { @@ -372,8 +370,15 @@ ATAgentType GetATAgent(const char *agent_name) } -static int DeleteATAgent(ATAgentType agent) +int DeleteATAgent(ATAgentType agent) { + printf("delete agent->at_handler = %d\n",agent->at_handler); + PrivTaskDelete(agent->at_handler, 0); + + if (agent->fd > 0) { + PrivClose(agent->fd); + } + if (agent->lock) { PrivMutexDelete(&agent->lock); } @@ -382,10 +387,6 @@ static int DeleteATAgent(ATAgentType agent) PrivSemaphoreDelete(&agent->entm_rx_notice); } - if (agent->fd > 0) { - PrivClose(agent->fd); - } - if (agent->rsp_sem) { PrivSemaphoreDelete(&agent->rsp_sem); } @@ -408,16 +409,16 @@ static void *ATAgentReceiveProcess(void *param) ATReplyType reply = agent->reply; agent->maintain_buffer[agent->maintain_len] = '\0'; - - if (agent->maintain_len < reply->reply_max_len) { + if (agent->maintain_len <= reply->reply_max_len) { + memset(reply->reply_buffer, 0 , reply->reply_max_len); memcpy(reply->reply_buffer, agent->maintain_buffer, agent->maintain_len); - reply->reply_len = agent->maintain_len; } else { - printf("out of memory (%d)!", reply->reply_max_len); + printf("out of memory (%d)!\n", reply->reply_max_len); } - agent->reply = NULL; + // agent->reply = NULL; + agent->receive_mode = DEFAULT_MODE; PrivSemaphoreAbandon(&agent->rsp_sem); } } @@ -463,7 +464,7 @@ static int ATAgentInit(ATAgentType agent) attr.stacksize = 2048; PrivTaskCreate(&agent->at_handler, &attr, ATAgentReceiveProcess, agent); - + printf("create agent->at_handler = %d\n",agent->at_handler); return result; __out: diff --git a/APP_Framework/Framework/connection/at_agent.h b/APP_Framework/Framework/connection/at_agent.h index 794c27bb2..20f4e875b 100755 --- a/APP_Framework/Framework/connection/at_agent.h +++ b/APP_Framework/Framework/connection/at_agent.h @@ -26,7 +26,7 @@ #include #include -#define REPLY_TIME_OUT 3 +#define REPLY_TIME_OUT 6 enum ReceiveMode { @@ -83,6 +83,7 @@ void SwapStr(char *str, int begin, int end); char* IpTstr(unsigned int ipint); ATAgentType GetATAgent(const char *agent_name); int InitATAgent(const char *agent_name, int fd, uint32 maintain_max); +int DeleteATAgent(ATAgentType agent); int ParseATReply(char* str, const char *format, ...); void DeleteATReply(ATReplyType reply); int ATOrderSend(ATAgentType agent, uint32 timeout_s, ATReplyType reply, const char *cmd_expr, ...); diff --git a/APP_Framework/Framework/connection/nbiot/adapter_nbiot.c b/APP_Framework/Framework/connection/nbiot/adapter_nbiot.c index e4ef085dd..6126a723a 100644 --- a/APP_Framework/Framework/connection/nbiot/adapter_nbiot.c +++ b/APP_Framework/Framework/connection/nbiot/adapter_nbiot.c @@ -17,8 +17,9 @@ * @author AIIT XUOS Lab * @date 2021.06.25 */ +#include #include - +#include #ifdef ADAPTER_BC28 extern AdapterProductInfoType BC28Attach(struct Adapter *adapter); @@ -50,7 +51,7 @@ int AdapterNbiotInit(void) { int ret = 0; - struct Adapter *adapter = malloc(sizeof(struct Adapter)); + struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter)); if (!adapter) { printf("malloc adapter failed.\n"); return -1; @@ -59,14 +60,14 @@ int AdapterNbiotInit(void) ret = AdapterNbiotRegister(adapter); if (ret < 0) { printf("register nbiot adapter error\n"); - free(adapter); + PrivFree(adapter); return -1; } #ifdef ADAPTER_BC28 AdapterProductInfoType product_info = BC28Attach(adapter); if (!product_info) { printf("bc28 attach error\n"); - free(adapter); + PrivFree(adapter); return -1; } @@ -96,7 +97,7 @@ int opennb(void) return 0; } - SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, opennb, opennb, show adapter nb information); +// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, opennb, opennb, show adapter nb information); int closenb(void) { int ret = 0; @@ -114,7 +115,7 @@ int closenb(void) return 0; } - SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, closenb, closenb, show adapter nb information); +// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, closenb, closenb, show adapter nb information); int connectnb(int argc, char *argv[]) { @@ -134,7 +135,7 @@ int closenb(void) return 0; } - SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, connectnb, connectnb, show adapter nb information); +// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, connectnb, connectnb, show adapter nb information); int sendnb(int argc, char *argv[]) { @@ -155,7 +156,7 @@ int closenb(void) return 0; } - SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, sendnb, sendnb, show adapter nb information); +// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN)|SHELL_CMD_PARAM_NUM(2)|SHELL_CMD_DISABLE_RETURN, sendnb, sendnb, show adapter nb information); int recvnb(void) { @@ -168,5 +169,5 @@ int closenb(void) return 0; } - SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, recvnb, recvnb, show adapter nb information); +// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, recvnb, recvnb, show adapter nb information); diff --git a/APP_Framework/Framework/connection/nbiot/bc28/bc28.c b/APP_Framework/Framework/connection/nbiot/bc28/bc28.c index 7135b0913..a5e1d2fc5 100644 --- a/APP_Framework/Framework/connection/nbiot/bc28/bc28.c +++ b/APP_Framework/Framework/connection/nbiot/bc28/bc28.c @@ -21,6 +21,7 @@ #include #include #include "../adapter_nbiot.h" +#include #define SOCKET_PROTOCOL_TCP (6) #define SOCKET_PROTOCOL_UDP (17) @@ -112,25 +113,55 @@ 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+NRB", 6); + // 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; + // } + // BC28PowerSet(); /* reset bc28 module by set reset pin */ + // PrivTaskDelay(6000); - memcpy(at_cmd, "AT+CSQ", 6); + // memset(at_cmd, 0 ,64); + // memcpy(at_cmd, "AT+NBAND=5", 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=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; } - 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; - } + // 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"); printf("cmd : %s\n", at_cmd); @@ -138,26 +169,65 @@ int NBIoTStatusCheck(struct Adapter *adapter ) if(result < 0) { printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); result = -1; + goto out; } - memcpy(at_cmd, "AT+CEREG?", 9); + memset(at_cmd, 0 ,64); + memcpy(at_cmd, "AT+CGATT=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; + } - memcpy(at_cmd, "AT+CGATT?", 9); + // 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); + // 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+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; } + memset(at_cmd, 0 ,64); + memcpy(at_cmd, "AT+CSQ", 6); + 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+CGPADDR", 10); strcat(at_cmd, "\n"); printf("cmd : %s\n", at_cmd); @@ -166,7 +236,7 @@ int NBIoTStatusCheck(struct Adapter *adapter ) printf("%s %d cmd[%s] config failed!\n",__func__,__LINE__,at_cmd); result = -1; } - +out: return result; } @@ -231,7 +301,7 @@ int NBIoTSocketCreate(struct Adapter *adapter, struct Socket *socket ) result = -1; goto out; } - + memset(at_cmd, 0 ,64); memcpy(at_cmd, "AT+NSOCR=", 9); strcat(at_cmd, str_type); strcat(at_cmd, ","); @@ -258,6 +328,96 @@ out: return result; } +static int BC28ParseData(void *dst, ATReplyType reply) +{ + int nbdata_len = 0; + int check_cnt = 0; + int left = 0; + int right = 0; + int tag = 0; + char *nbdata = NULL; + char *result = NULL; + + if (NULL == reply) { + printf("at create failed ! \n"); + return -1; + } + + result = GetReplyText(reply); + if (!result) { + printf("%s %n get reply failed.\n",__func__,__LINE__); + return -1; + } + + /*the data style of BC28: eg.',,,,,,' ,its assic code*/ + + /*step1: get the section between the fourth and fifth ','*/ + for(int i = 0; i < reply->reply_len; i++) + { + if(',' == *(result + i)) + { + check_cnt++; + if(4 == check_cnt) /*the fourth ','*/ + { + left = i; + } + + if(5 == check_cnt) /*the fifth ',' */ + { + right = i; + break; + } + } + } + if(left > 0 && right > 0) + { + /*step2: transform assic code as hexadecimal*/ + for(int j = (left + 1);j <= (right - 1);j++) + { + if(*(result + j) >= 0x30 && *(result + j) <= 0x39){ + *(result + j) = (*(result + j) - 0x30); /* transform [0-9]*/ + } + else if(*(result + j) >= 0x41 && *(result + j) <= 0x46) + { + *(result + j) = (*(result + j) - 0x37); /* transform [A-F],NOTE!!! A-F can not be regarded as a char,only can be reagrded as a number*/ + } + + printf("0x%x ",*(result + j)); + } + + printf("\n"); + nbdata_len = (right - left - 1) / 2; /*caculate the data length */ + nbdata = PrivMalloc(nbdata_len); + if (!nbdata) { + printf("%s %n malloc failed.\n",__func__,__LINE__); + return -1; + } + + tag = left; + printf("left = %d right = %d nbdata_len = %d\n",left ,right ,nbdata_len); + + /*step3: transform hexadecimal as actual data */ + for(int k = 0; k < nbdata_len; k++ ) + { + *(nbdata + k) = (*(result + tag + 1) * 16 + *(result + tag + 2)); + tag = tag + 2; /* transform with two data*/ + printf("0x%x ",*(nbdata + k)); + } + printf("\n"); + + memcpy(dst, nbdata, nbdata_len); + + PrivFree(nbdata); + + return nbdata_len; + } + else + { + return 0; + } + +} + /** * @description: NBIoT device close a socket connection * @param adapter - NBIoT adapter AT @@ -277,9 +437,11 @@ int NBIoTSocketDelete(struct Adapter *adapter ) } char str_fd[2] = {0}; - char at_cmd[16] = {0}; + char at_cmd[32] = {0}; + printf("NBIOT close socket id = %d\n",adapter->socket.socket_id); itoa(adapter->socket.socket_id, str_fd, 10); + memset(at_cmd, 0 ,32); memcpy(at_cmd, "AT+NSOCL=", 9); strcat(at_cmd, str_fd); strcat(at_cmd, "\n"); @@ -312,6 +474,7 @@ static int BC28Open(struct Adapter *adapter) if (!adapter->agent) { char *agent_name = "niot_device"; if (EOK != InitATAgent(agent_name, adapter->fd, 512)) { + PrivClose(adapter->fd); printf("at agent init failed !\n"); return -1; } @@ -326,11 +489,19 @@ static int BC28Open(struct Adapter *adapter) BC28PowerSet(); /* reset bc28 module by set reset pin */ PrivTaskDelay(6000); - NBIoTStatusCheck(adapter); /* ask module status*/ + 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){ + DeleteATAgent(adapter->agent); + // adapter->agent = NULL; + PrivClose(adapter->fd); printf("NBIot create tcp socket failed.\n"); return -1; } @@ -342,6 +513,8 @@ static int BC28Open(struct Adapter *adapter) static int BC28Close(struct Adapter *adapter) { NBIoTSocketDelete(adapter); + DeleteATAgent(adapter->agent); + adapter->agent = NULL; PrivClose(adapter->fd); return 0; } @@ -393,6 +566,7 @@ static int BC28Connect(struct Adapter *adapter, enum NetRoleType net_role, const itoa(adapter->socket.socket_id, str_fd, 10); + memset(at_cmd, 0 ,64); memcpy(at_cmd, "AT+NSOCO=", 9); strcat(at_cmd, str_fd); strcat(at_cmd, ","); @@ -418,8 +592,26 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len) uint32_t result = 0; 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; + for(int i = 0; i < len; i++) + { + assic_val = 0; + for( ; assic_val <= 0x7f; assic_val++) + { + if(*(char*)(buf + i) == assic_val) + { + break; + } + } + itoa(assic_val, assic_str, 16); + memcpy(nbdata + length, assic_str, 2); + length = length + 2; + } if (adapter->socket.type == SOCKET_TYPE_STREAM ) { char size[2] = {0}; @@ -427,12 +619,13 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len) itoa(adapter->socket.socket_id, str_fd, 10); size[0] = len + '0'; + memset(at_cmd, 0 ,64); memcpy(at_cmd, "AT+NSOSD=", 9); strcat(at_cmd, str_fd); strcat(at_cmd, ","); strcat(at_cmd, size); strcat(at_cmd, ","); - strcat(at_cmd, buf); + strncat(at_cmd, nbdata,length); strcat(at_cmd, "\n"); } else if(adapter->socket.type == SOCKET_TYPE_DGRAM ) { @@ -443,6 +636,7 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len) itoa(adapter->socket.listen_port, listen_port, 10); + memset(at_cmd, 0 ,64); memcpy(at_cmd, "AT+NSOST=", 9); strcat(at_cmd, str_fd); strcat(at_cmd, ","); @@ -450,11 +644,11 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len) strcat(at_cmd, ","); strcat(at_cmd, listen_port); strcat(at_cmd, ","); - strcat(at_cmd, buf); + strncat(at_cmd, nbdata,length); strcat(at_cmd, "\n"); } - + PrivFree(nbdata); printf("cmd : %s\n", at_cmd); AtSetReplyEndChar(adapter->agent, 0x4F, 0x4B); result = AtCmdConfigAndCheck(adapter->agent, at_cmd, "OK"); @@ -466,14 +660,17 @@ static int BC28Send(struct Adapter *adapter, const void *buf, size_t len) return result; } + static int BC28Recv(struct Adapter *adapter, void *buf, size_t len) { char at_cmd[64] = {0}; char str_fd[2] = {0}; char size[2] = {0}; + int ret = 0; char *result = NULL; + - ATReplyType reply = CreateATReply(64); + ATReplyType reply = CreateATReply(256); if (NULL == reply) { printf("at create failed ! \n"); return -1; @@ -482,6 +679,7 @@ static int BC28Recv(struct Adapter *adapter, void *buf, size_t len) itoa(adapter->socket.socket_id, str_fd, 10); itoa(len, size, 10); + memset(at_cmd, 0 ,64); memcpy(at_cmd, "AT+NSORF=", 9); strcat(at_cmd, str_fd); strcat(at_cmd, ","); @@ -492,17 +690,16 @@ static int BC28Recv(struct Adapter *adapter, void *buf, size_t len) ATOrderSend(adapter->agent, REPLY_TIME_OUT, reply, at_cmd); PrivTaskDelay(300); - result = GetReplyText(reply); - if (!result) { - printf("%s %n get reply failed.\n",__func__,__LINE__); + ret = BC28ParseData(buf, reply); + if (ret < 0){ + return ret; } - memcpy(buf, result, reply->reply_len); if (reply) { DeleteATReply(reply); } - return 0; + return ret; } static int BC28Disconnect(struct Adapter *adapter) @@ -534,7 +731,7 @@ static const struct IpProtocolDone BC28_done = AdapterProductInfoType BC28Attach(struct Adapter *adapter) { - struct AdapterProductInfo *product_info = malloc(sizeof(struct AdapterProductInfo)); + struct AdapterProductInfo *product_info = PrivMalloc(sizeof(struct AdapterProductInfo)); if (!product_info) { printf("BC28Attach malloc product_info error\n"); return NULL; diff --git a/APP_Framework/Framework/transform_layer/xiuos/user_api/posix_support/pthread.c b/APP_Framework/Framework/transform_layer/xiuos/user_api/posix_support/pthread.c index 6cf2ed4a9..7d1cef960 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/user_api/posix_support/pthread.c +++ b/APP_Framework/Framework/transform_layer/xiuos/user_api/posix_support/pthread.c @@ -49,6 +49,8 @@ int pthread_create(pthread_t *thread, const pthread_attr_t *attr, return -1 ; ret = UserTaskStartup(pid); + *thread = pid; + return ret; }