From 224201ba55b955aecfff043db11bf144aa1cda9e Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Fri, 5 Nov 2021 17:30:00 +0800 Subject: [PATCH 01/17] add application bin ota function by 4G adapter,this function need save the app bin to sdcard and startup the app from sdcrad --- APP_Framework/Applications/Kconfig | 2 + APP_Framework/Applications/Makefile | 8 +- APP_Framework/Applications/main.c | 3 +- APP_Framework/Applications/ota/Kconfig | 7 + APP_Framework/Applications/ota/Makefile | 3 + APP_Framework/Applications/ota/README.md | 19 ++ APP_Framework/Applications/ota/ota.c | 231 +++++++++++++++ APP_Framework/Applications/ota/ota_server.c | 297 ++++++++++++++++++++ 8 files changed, 567 insertions(+), 3 deletions(-) create mode 100755 APP_Framework/Applications/ota/Kconfig create mode 100644 APP_Framework/Applications/ota/Makefile create mode 100644 APP_Framework/Applications/ota/README.md create mode 100644 APP_Framework/Applications/ota/ota.c create mode 100644 APP_Framework/Applications/ota/ota_server.c diff --git a/APP_Framework/Applications/Kconfig b/APP_Framework/Applications/Kconfig index 7c5592045..7639f0449 100644 --- a/APP_Framework/Applications/Kconfig +++ b/APP_Framework/Applications/Kconfig @@ -11,6 +11,8 @@ menu "Applications" default 85 if KTASK_PRIORITY_256 endmenu + source "$APP_DIR/Applications/ota/Kconfig" + source "$APP_DIR/Applications/app_test/Kconfig" source "$APP_DIR/Applications/connection_app/Kconfig" source "$APP_DIR/Applications/control_app/Kconfig" diff --git a/APP_Framework/Applications/Makefile b/APP_Framework/Applications/Makefile index 7451edb74..af2f86d2c 100644 --- a/APP_Framework/Applications/Makefile +++ b/APP_Framework/Applications/Makefile @@ -1,6 +1,10 @@ -SRC_DIR := general_functions +SRC_DIR := general_functions app_test -SRC_FILES := main.c framework_init.c +SRC_FILES := main.c framework_init.c + +ifeq ($(CONFIG_APPLICATION_OTA),y) + SRC_DIR += ota +endif ifeq ($(CONFIG_APPLICATION_SENSOR),y) SRC_DIR += sensor_app diff --git a/APP_Framework/Applications/main.c b/APP_Framework/Applications/main.c index 5a7524f5b..3cb6142b5 100644 --- a/APP_Framework/Applications/main.c +++ b/APP_Framework/Applications/main.c @@ -15,11 +15,12 @@ #include extern int FrameworkInit(); - +extern void ApplicationOtaTaskInit(void); int main(void) { printf("Hello, world!\n"); FrameworkInit(); + ApplicationOtaTaskInit(); return 0; } // int cppmain(void); diff --git a/APP_Framework/Applications/ota/Kconfig b/APP_Framework/Applications/ota/Kconfig new file mode 100755 index 000000000..2943bdb32 --- /dev/null +++ b/APP_Framework/Applications/ota/Kconfig @@ -0,0 +1,7 @@ +menu "config application bin ota " + menuconfig APPLICATION_OTA + bool "Using app bin ota" + default n + + +endmenu diff --git a/APP_Framework/Applications/ota/Makefile b/APP_Framework/Applications/ota/Makefile new file mode 100644 index 000000000..2f5316c0d --- /dev/null +++ b/APP_Framework/Applications/ota/Makefile @@ -0,0 +1,3 @@ +SRC_FILES := ota.c + +include $(KERNEL_ROOT)/compiler.mk diff --git a/APP_Framework/Applications/ota/README.md b/APP_Framework/Applications/ota/README.md new file mode 100644 index 000000000..052b383ab --- /dev/null +++ b/APP_Framework/Applications/ota/README.md @@ -0,0 +1,19 @@ +# OTA README + +xiuos当前的ota功能允许应用bin文件可以通过4G实现远程的bin文件更新(限制:1、bin文件存放在设备SD卡并且应用从SD卡启动;2、暂且支持4G实现;3、暂时只支持aiit终端)。 + +## 文件说明 + +| 名称 | 说明 | +| -- | -- | +| ota.c| 设备OTA代码 | +| ota_server.c | pc服务端的实例代码供参考 | + + +## 使用说明 +xiuos的应用bin文件更新依赖上层的adapter框架,因此需要在menuconfig同时配置以下选项: +1、ota开关APPLICATION_OTA打开; +2、adapter的4G功能开关; +3、拆分的应用启动SEPARATE_COMPILE开关从SD卡启动的配置开关APP_STARTUP_FROM_SDCARD。 + + diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c new file mode 100644 index 000000000..7f6200f58 --- /dev/null +++ b/APP_Framework/Applications/ota/ota.c @@ -0,0 +1,231 @@ + +/* +* Copyright (c) 2020 AIIT XUOS Lab +* XiUOS is licensed under Mulan PSL v2. +* You can use this software according to the terms and conditions of the Mulan PSL v2. +* You may obtain a copy of Mulan PSL v2 at: +* http://license.coscl.org.cn/MulanPSL2 +* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, +* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, +* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. +* See the Mulan PSL v2 for more details. +*/ + +/** +* @file: ota.c +* @brief: a application ota task of system +* @version: 1.0 +* @author: AIIT XUOS Lab +* @date: 2021/11/3 +* +*/ +#include +#include + +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 +}; + +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 + uint32 crc; ///< Current frame data crc +}; + +struct ota_data +{ + struct ota_header_t header; + struct ota_frame_t frame; +}; + +pthread_t ota_ktask; + +/** + * @description: CRC16 check + * @param data data buffer + * @param length data length + * @return check code + */ +uint32_t OtaCrc16(uint8_t * data, uint8_t length) +{ + int j; + unsigned int reg_crc=0xFFFF; + + while (length--) { + reg_crc ^= *data++; + for (j=0;j<8;j++) { + if(reg_crc & 0x01) + reg_crc=reg_crc >>1 ^ 0xA001; + else + reg_crc=reg_crc >>1; + } + } + + return reg_crc; +} + +static int SaveAppBin(char* buf, int len) +{ + int fd = 0; + fd = open( BOARD_APP_NAME, O_RDWR | O_CREAT ); + lseek(fd, 0, SEEK_END); + write(fd, buf, len); + close(fd); +} +static int CrcFileCheck(uint32 crc_check, unsigned long total_len) +{ + int ret = 0; + int fd = 0; + int len = 0; + char *buf = NULL; + + fd = open( BOARD_APP_NAME, O_RDONLY ); + if(fd < 0){ + printf("open %s bin failed.\n",BOARD_APP_NAME); + return -1; + } + + buf = PrivMalloc(total_len); + if(NULL == buf) + { + printf("malloc failed.\n"); + close(fd); + return -1; + } + + len = read(fd, buf, total_len); + + if (crc_check != OtaCrc16(buf, len)) + { + ret =-1; + } + + PrivFree(buf); + close(fd); + + return ret; +} + +static int OtaDataRecv(struct Adapter* adapter) +{ + struct ota_data recv_msg; + char reply[16] = {0}; + int ret = 0; + int try_times = 5; + + 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) + { + 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); + if(0 != CrcFileCheck(recv_msg.frame.crc, recv_msg.header.total_len)) + { + printf("crc check %s bin failed.please try again.\n", BOARD_APP_NAME); + ret = -1; + } + break; + } + + if (recv_msg.frame.crc == OtaCrc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len)) + { + SaveAppBin(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); + goto try_again; + } + memset(reply, 0, 16); + memcpy(reply, "ok", strlen("ok")); + } + else + { +try_again: + if(try_times == 0) + { + 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")); + AdapterDeviceSend(adapter, reply, strlen(reply)); + try_times--; + continue; + } + } + return ret; +} + +static void *OtaKTaskEntry(void *parameter) +{ + struct ota_data recv_msg; + char reply[4] = {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"; + + adapter->socket.socket_id = 0; + + AdapterDeviceOpen(adapter); + AdapterDeviceControl(adapter, OPE_INT, &baud_rate); + AdapterDeviceConnect(adapter, CLIENT, server_addr, server_port, IPV4); + + 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 (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)); + + /* step2: start receive source bin file of application*/ + ret = OtaDataRecv(adapter); + if (0 != ret) + { + memset(reply, 0, 4); + memcpy(reply, "send_restart", strlen("send_restart")); + AdapterDeviceSend(adapter, reply, strlen(reply)); + continue; + } + else + { + break; + } + } + } + PrivTaskDelay(3000); /* check ota signal every 3s */ + } + AdapterDeviceClose(adapter); +} + +void ApplicationOtaTaskInit(void) +{ + pthread_attr_t attr; + attr.schedparam.sched_priority = 10; + attr.stacksize = 2048; + + PrivTaskCreate(&ota_ktask, &attr, OtaKTaskEntry, NULL); + +} \ No newline at end of file diff --git a/APP_Framework/Applications/ota/ota_server.c b/APP_Framework/Applications/ota/ota_server.c new file mode 100644 index 000000000..eb71b1202 --- /dev/null +++ b/APP_Framework/Applications/ota/ota_server.c @@ -0,0 +1,297 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +#include +#include + +/*******************************mail*****************************/ + +#define MAX_BUFF_SIZE (2048) +#define IP_ADDR ("smtp.163.com") +#define IP_ADDR_PORT (25) +#define USERNAME ("18535861947@163.com") +#define TO_ADDRESS ("522736215@qq.com") +#define ACCESS_PORT ("有链接已接入服务器端口") +#define USING_DEVICE_AUTH 1 + + +#define BUFLEN 1024 +#define LISTNUM 20 +#define IP_ADDRESS "0.0.0.0" + +/*宏定义*/ +#define SID_MAX_LENGHT 16 +#define NAME_MAX_LENGHT 16 +#define MAJOR_MAX_LENGHT 64 +#define CMD_MAX_LENGHT 1024 +#define REDIS_SERVER_IP "127.0.0.1" +#define REDIS_SERVER_PORT 6379 + +typedef int BOOL; +#define true 1 +#define false 0 + +int serverfd;//服务器socket +int clientfd[100000];//客户端的socketfd,100个元素,clientfd[0]~clientfd[99] +int size = 99999;//用来控制进入聊天室的人数为50以内 +int PORT = 9898;//端口号 +typedef struct sockaddr meng; +socklen_t len; + +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 +}; + +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 + uint32 crc; ///< Current frame data crc +}; + +struct ota_data +{ + struct ota_header_t header; + struct ota_frame_t frame; +}; + +pthread_t ota_ktask; + +/** + * @description: CRC16 check + * @param data data buffer + * @param length data length + * @return check code + */ +uint32_t OtaCrc16(uint8_t * data, uint8_t length) +{ + int j; + unsigned int reg_crc=0xFFFF; + + while (length--) { + reg_crc ^= *data++; + for (j=0;j<8;j++) { + if(reg_crc & 0x01) + reg_crc=reg_crc >>1 ^ 0xA001; + else + reg_crc=reg_crc >>1; + } + } + + return reg_crc; +} + +void init(void) +{ + serverfd = socket(PF_INET,SOCK_STREAM,0); + + if (serverfd == -1) + { + perror("创建socket失败"); + exit(-1); + } + +//为套接字设置ip协议 设置端口号 并自动获取本机ip转化为网络ip + + struct sockaddr_in addr;//存储套接字的信息 + addr.sin_family = AF_INET;//地址族 + addr.sin_port = htons(PORT);//设置server端端口号,你可以随便设置,当sin_port = 0时,系统随机选择一个未被使用的端口号 + 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) + { + perror("端口设置失败"); + exit(-1); + } + + if (bind(serverfd,(meng*)&addr,sizeof(addr)) == -1) + { + perror("绑定失败"); + exit(-1); + } + + if (listen(serverfd,100) == -1) + {//监听最大连接数 + perror("设置监听失败"); + exit(-1); + } +} +int OtaFileSend(int fd) +{ + unsigned char buf[32] = { 0 }; + struct ota_data data; + FILE *file_fd; + char ch; + int len = 0; + int try_times = 5; + int ret = 0; + int frame_cnt = 0; + int file_length = 0; + char * file_buf = NULL; + + file_fd = fopen("/tmp/xiuos_app.bin", "r"); + + while((ch = fgetc(file_fd)) != EOF) + { + memset(&data, 0, sizeof(data)); + + data.header.frame_flag = 0x5A5A; + len = read(file_fd, data.frame.frame_data, 200 ); + 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; + } + lseek(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)) + { + frame_cnt++; + continue; + } + else + { + if(try_times > 0) + { + try_times--; + goto try_again; + } + else + { + printf("send frame[%d] 5 times failed.\n",frame_cnt); + ret = -1; + break; + } + } + } + + if (ret == 0) + { + file_buf = malloc(file_length); + memset(file_buf, 0, file_length); + memset(&data, 0, sizeof(data)); + + data.header.frame_flag = 0x5A5A; + + len = read(file_fd, file_buf, file_length); + if(len > 0) { + 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!@")); + } + send(fd, &data, sizeof(data), MSG_NOSIGNAL); + free(file_buf); + } + + fclose(file_fd); + return ret; +} + +void* server_thread(void* p) +{ + int fd = *(int*)p; + unsigned char buf[32] = { 0 }; + struct ota_data data; + + printf("pthread = %d\n",fd); + + 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); + + len = recv(fd, buf, sizeof(buf),0); + if (len <= 0) + { + continue; + } + else + { + if(0 == strncmp(buf, "ok", len)) + { + OtaFileSend(fd); + } + } + } +} + +void server(void) +{ + printf("Server startup\n"); + while(1) + { + struct sockaddr_in fromaddr; + socklen_t len = sizeof(fromaddr); + int fd = accept(serverfd,(meng*)&fromaddr,&len); + +//调用accept进入堵塞状态,等待客户端的连接 + + if (fd == -1) + { + printf("The client connection is wrong...\n"); + continue; + } + + int i = 0; + for (i = 0;i < size;i++) + { + if (clientfd[i] == 0) + { + //记录客户端的socket + clientfd[i] = fd; + + //有客户端连接之后,启动线程给此客户服务 + pthread_t tid; + pthread_create(&tid,0,server_thread,&fd); + break; + } + + if (size == i) + { + //发送给客户端说聊天室满了 + char* str = "Devices full"; + printf("%s", str); + send(fd,str,strlen(str),0); + close(fd); + } + } + } +} + +int main(void) +{ + init(); + server(); +} + From 8b2eee0aa09dedfe01def969fb2844c816ac400d Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Mon, 8 Nov 2021 15:56:28 +0800 Subject: [PATCH 02/17] reapir ota save bin process --- APP_Framework/Applications/ota/ota.c | 19 +++++++++++------ APP_Framework/Applications/ota/ota_server.c | 21 +++++++++++++++---- .../Framework/connection/4g/adapter_4g.c | 8 +++---- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index 7f6200f58..52f13bb8a 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -73,14 +73,16 @@ uint32_t OtaCrc16(uint8_t * data, uint8_t length) return reg_crc; } -static int SaveAppBin(char* buf, int len) +static int SaveAppBin(int fd, char* buf, int len) { - int fd = 0; - fd = open( BOARD_APP_NAME, O_RDWR | O_CREAT ); - lseek(fd, 0, SEEK_END); + // int fd = 0; + // fd = open( BOARD_APP_NAME, O_RDWR | O_CREAT ); write(fd, buf, len); - close(fd); + lseek(fd, len, SEEK_CUR); + + // close(fd); } + static int CrcFileCheck(uint32 crc_check, unsigned long total_len) { int ret = 0; @@ -121,6 +123,9 @@ static int OtaDataRecv(struct Adapter* adapter) char reply[16] = {0}; int ret = 0; int try_times = 5; + int fd = 0; + + fd = open( BOARD_APP_NAME, O_RDWR | O_CREAT | O_TRUNC); while(1) { memset(&recv_msg, 0, sizeof(struct ota_data)); @@ -140,7 +145,7 @@ static int OtaDataRecv(struct Adapter* adapter) if (recv_msg.frame.crc == OtaCrc16(recv_msg.frame.frame_data,recv_msg.frame.frame_len)) { - SaveAppBin(recv_msg.frame.frame_data, recv_msg.frame.frame_len); + SaveAppBin(fd, recv_msg.frame.frame_data, recv_msg.frame.frame_len); } else { @@ -166,6 +171,7 @@ try_again: continue; } } + close(fd); return ret; } @@ -218,6 +224,7 @@ static void *OtaKTaskEntry(void *parameter) PrivTaskDelay(3000); /* check ota signal every 3s */ } AdapterDeviceClose(adapter); + } void ApplicationOtaTaskInit(void) diff --git a/APP_Framework/Applications/ota/ota_server.c b/APP_Framework/Applications/ota/ota_server.c index eb71b1202..84ab322d8 100644 --- a/APP_Framework/Applications/ota/ota_server.c +++ b/APP_Framework/Applications/ota/ota_server.c @@ -153,6 +153,10 @@ int OtaFileSend(int fd) char * file_buf = NULL; file_fd = fopen("/tmp/xiuos_app.bin", "r"); + if (NULL == file_fd){ + printf("open file failed.\n"); + return -1; + } while((ch = fgetc(file_fd)) != EOF) { @@ -166,12 +170,13 @@ int OtaFileSend(int fd) data.frame.crc = OtaCrc16(data.frame.frame_data, len); file_length += len; } + memcpy(data.frame.frame_data[len], "!@", strlen("!@")); /* add '!@' as ending flag */ lseek(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)) { frame_cnt++; continue; @@ -219,6 +224,7 @@ void* server_thread(void* p) int fd = *(int*)p; unsigned char buf[32] = { 0 }; struct ota_data data; + int ret = 0; printf("pthread = %d\n",fd); @@ -231,19 +237,26 @@ void* server_thread(void* p) send(fd, &data, sizeof(data), MSG_NOSIGNAL); - len = recv(fd, buf, sizeof(buf),0); + len = recv(fd, buf, sizeof(buf), 0); if (len <= 0) { continue; } else { - if(0 == strncmp(buf, "ok", len)) + if(0 == strncmp(buf, "ok!@", len)) { - OtaFileSend(fd); + ret = OtaFileSend(fd); + if (ret == 0) { + printf("ota file send successful.\n"); + break; + } else { /* ota failed then restart the ota process */ + continue; + } } } } + close(fd); } void server(void) diff --git a/APP_Framework/Framework/connection/4g/adapter_4g.c b/APP_Framework/Framework/connection/4g/adapter_4g.c index 00bf150db..3a3b40dbe 100644 --- a/APP_Framework/Framework/connection/4g/adapter_4g.c +++ b/APP_Framework/Framework/connection/4g/adapter_4g.c @@ -51,7 +51,7 @@ int Adapter4GInit(void) struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter)); if (!adapter) { - free(adapter); + Privfree(adapter); return -1; } @@ -60,7 +60,7 @@ int Adapter4GInit(void) ret = Adapter4GRegister(adapter); if (ret < 0) { printf("Adapter4GInit register 4G adapter error\n"); - free(adapter); + Privfree(adapter); return -1; } @@ -68,7 +68,7 @@ int Adapter4GInit(void) AdapterProductInfoType product_info = Ec200tAttach(adapter); if (!product_info) { printf("Adapter4GInit ec200t attach error\n"); - free(adapter); + Privfree(adapter); return -1; } @@ -112,4 +112,4 @@ int Adapter4GTest(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, Adapter4GTest, Adapter4GTest, show adapter 4G information); +// SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0)|SHELL_CMD_DISABLE_RETURN, Adapter4GTest, Adapter4GTest, show adapter 4G information); From 9959ec6470c70967db3dd31157695dca4719dd25 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Tue, 9 Nov 2021 16:12:07 +0800 Subject: [PATCH 03/17] 1.repair the separate compile error;2.modify ota init task --- APP_Framework/Applications/main.c | 2 + APP_Framework/Applications/ota/Kconfig | 2 +- .../Framework/connection/4g/adapter_4g.c | 6 +- .../transform_layer/xiuos/transform.c | 4 +- .../transform_layer/xiuos/transform.h | 4 +- .../xiuos/user_api/switch_api/user_api.h | 56 ++++++++++--------- .../XiUOS/kernel/kernel_service/xs_service.c | 2 + 7 files changed, 42 insertions(+), 34 deletions(-) diff --git a/APP_Framework/Applications/main.c b/APP_Framework/Applications/main.c index 3cb6142b5..bfa04753e 100644 --- a/APP_Framework/Applications/main.c +++ b/APP_Framework/Applications/main.c @@ -20,7 +20,9 @@ int main(void) { printf("Hello, world!\n"); FrameworkInit(); +#ifdef APPLICATION_OTA ApplicationOtaTaskInit(); +#endif return 0; } // int cppmain(void); diff --git a/APP_Framework/Applications/ota/Kconfig b/APP_Framework/Applications/ota/Kconfig index 2943bdb32..097519d9b 100755 --- a/APP_Framework/Applications/ota/Kconfig +++ b/APP_Framework/Applications/ota/Kconfig @@ -1,4 +1,4 @@ -menu "config application bin ota " +menu "ota app " menuconfig APPLICATION_OTA bool "Using app bin ota" default n diff --git a/APP_Framework/Framework/connection/4g/adapter_4g.c b/APP_Framework/Framework/connection/4g/adapter_4g.c index 3a3b40dbe..ab288a654 100644 --- a/APP_Framework/Framework/connection/4g/adapter_4g.c +++ b/APP_Framework/Framework/connection/4g/adapter_4g.c @@ -51,7 +51,7 @@ int Adapter4GInit(void) struct Adapter *adapter = PrivMalloc(sizeof(struct Adapter)); if (!adapter) { - Privfree(adapter); + PrivFree(adapter); return -1; } @@ -60,7 +60,7 @@ int Adapter4GInit(void) ret = Adapter4GRegister(adapter); if (ret < 0) { printf("Adapter4GInit register 4G adapter error\n"); - Privfree(adapter); + PrivFree(adapter); return -1; } @@ -68,7 +68,7 @@ int Adapter4GInit(void) AdapterProductInfoType product_info = Ec200tAttach(adapter); if (!product_info) { printf("Adapter4GInit ec200t attach error\n"); - Privfree(adapter); + PrivFree(adapter); return -1; } diff --git a/APP_Framework/Framework/transform_layer/xiuos/transform.c b/APP_Framework/Framework/transform_layer/xiuos/transform.c index 7f53b96e7..bc82fdf62 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/transform.c +++ b/APP_Framework/Framework/transform_layer/xiuos/transform.c @@ -96,7 +96,7 @@ int PrivTaskDelay(int32_t ms) } /*********************fs**************************/ - +#ifdef FS_VFS /************************Driver Posix Transform***********************/ int PrivOpen(const char *path, int flags) { @@ -150,7 +150,7 @@ int PrivIoctl(int fd, int cmd, void *args) return ret; } - +#endif /********************memory api************/ void *PrivMalloc(size_t size) { diff --git a/APP_Framework/Framework/transform_layer/xiuos/transform.h b/APP_Framework/Framework/transform_layer/xiuos/transform.h index 38da64755..edd22e618 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/transform.h +++ b/APP_Framework/Framework/transform_layer/xiuos/transform.h @@ -101,7 +101,7 @@ struct PinParam long pin;//< pin number int mode;//< pin mode: input/output struct PinDevIrq irq_set;//< pin irq set - uint64 arg; + uint64_t arg; }; struct PinStat @@ -128,7 +128,7 @@ struct SerialDataCfg uint8_t serial_invert_mode; uint16_t serial_buffer_size; - uint8 ext_uart_no; + uint8_t ext_uart_no; enum ExtSerialPortConfigure port_configure; }; diff --git a/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_api.h b/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_api.h index 161e6add9..803af707f 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_api.h +++ b/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_api.h @@ -22,16 +22,16 @@ #ifndef XS_USER_API_H #define XS_USER_API_H -#include #include #include #include #include -#include "../../../../../../Ubiquitous/XiUOS/arch/kswitch.h" #ifdef SEPARATE_COMPILE +#include "../../../../../../Ubiquitous/XiUOS/arch/kswitch.h" + #define TASK_INFO 1 #define MEM_INFO 2 #define SEM_INFO 3 @@ -58,22 +58,22 @@ typedef void DIR; int32_t UserTaskCreate(UtaskType utask); -x_err_t UserTaskStartup(int32_t id); -x_err_t UserTaskDelete(int32_t id); +long UserTaskStartup(int32_t id); +long UserTaskDelete(int32_t id); void UserTaskQuit(void); -x_err_t UserTaskDelay(int32_t ms); -x_err_t UserGetTaskName(int32_t id ,char *name); +long UserTaskDelay(int32_t ms); +long UserGetTaskName(int32_t id ,char *name); int32_t UserGetTaskID(void); uint8_t UserGetTaskStat(int32_t id); #ifdef ARCH_SMP -x_err_t UserTaskCoreCombine(int32_t id,uint8_t core_id); -x_err_t UserTaskCoreUnCombine(int32_t id); +long UserTaskCoreCombine(int32_t id,uint8_t core_id); +long UserTaskCoreUnCombine(int32_t id); uint8_t UserGetTaskCombinedCore(int32_t id); uint8_t UserGetTaskRunningCore(int32_t id); #endif -x_err_t UserGetTaskErrorstatus(int32_t id); +long UserGetTaskErrorstatus(int32_t id); uint8_t UserGetTaskPriority(int32_t id); @@ -93,10 +93,10 @@ int32_t UserMutexAbandon(int32_t mutex); #ifdef KERNEL_SEMAPHORE typedef int32 sem_t; sem_t UserSemaphoreCreate(uint16_t val); -x_err_t UserSemaphoreDelete(sem_t sem); -x_err_t UserSemaphoreObtain(sem_t sem, int32_t wait_time); -x_err_t UserSemaphoreAbandon(sem_t sem); -x_err_t UserSemaphoreSetValue(sem_t sem, uint16_t val); +long UserSemaphoreDelete(sem_t sem); +long UserSemaphoreObtain(sem_t sem, int32_t wait_time); +long UserSemaphoreAbandon(sem_t sem); +long UserSemaphoreSetValue(sem_t sem, uint16_t val); #endif @@ -104,22 +104,22 @@ x_err_t UserSemaphoreSetValue(sem_t sem, uint16_t val); typedef int32 EventIdType; EventIdType UserEventCreate(uint8_t flag); void UserEventDelete(EventIdType event); -x_err_t UserEventTrigger(EventIdType event, uint32_t set); -x_err_t UserEventProcess(EventIdType event, uint32_t set, uint8_t option, +long UserEventTrigger(EventIdType event, uint32_t set); +long UserEventProcess(EventIdType event, uint32_t set, uint8_t option, int32_t wait_time, uint32_t *Recved); -x_err_t UserEventReinit(EventIdType event); +long UserEventReinit(EventIdType event); #endif #ifdef KERNEL_MESSAGEQUEUE int32_t UserMsgQueueCreate(size_t msg_size, size_t max_msgs); -x_err_t UserMsgQueueDelete(int32_t mq ); -x_err_t UserMsgQueueSendwait(int32_t mq, const void *buffer, +long UserMsgQueueDelete(int32_t mq ); +long UserMsgQueueSendwait(int32_t mq, const void *buffer, size_t size, int32_t wait_time); -x_err_t UserMsgQueueSend(int32_t mq, const void *buffer, size_t size); -x_err_t UserMsgQueueUrgentSend(int32_t mq, const void *buffer, size_t size); -x_err_t UserMsgQueueRecv(int32_t mq, void *buffer, size_t size,int32_t wait_time); -x_err_t UserMsgQueueReinit(int32_t mq); +long UserMsgQueueSend(int32_t mq, const void *buffer, size_t size); +long UserMsgQueueUrgentSend(int32_t mq, const void *buffer, size_t size); +long UserMsgQueueRecv(int32_t mq, void *buffer, size_t size,int32_t wait_time); +long UserMsgQueueReinit(int32_t mq); #endif int open(const char *path, int flags, ...); @@ -154,14 +154,18 @@ struct statfs { }; int statfs(const char *path, struct statfs *buf); -#endif +/* NOTE!!!: when cutting out file system, the 'printf' function can not output angthing */ int Userprintf(const char *fmt, ...); +#define printf Userprintf -#define printf Userprintf +#endif + #else +#include + #ifdef FS_VFS #include #endif @@ -182,7 +186,7 @@ int32_t UserTaskCreate(UtaskType utask); #define UserTaskQuit KTaskQuit #define UserTaskDelay MdelayKTask -x_err_t UserGetTaskName(int32_t id ,char *name); +long UserGetTaskName(int32_t id ,char *name); int32_t UserGetTaskID(void); uint8_t UserGetTaskStat(int32_t id); @@ -194,7 +198,7 @@ uint8_t UserGetTaskCombinedCore(int32_t id); uint8_t UserGetTaskRunningCore(int32_t id); #endif -x_err_t UserGetTaskErrorstatus(int32_t id); +long UserGetTaskErrorstatus(int32_t id); uint8_t UserGetTaskPriority(int32_t id); #define UserMalloc x_malloc diff --git a/Ubiquitous/XiUOS/kernel/kernel_service/xs_service.c b/Ubiquitous/XiUOS/kernel/kernel_service/xs_service.c index f6acd21d8..639a13553 100644 --- a/Ubiquitous/XiUOS/kernel/kernel_service/xs_service.c +++ b/Ubiquitous/XiUOS/kernel/kernel_service/xs_service.c @@ -25,6 +25,8 @@ #include #include #include +#include +#include #ifdef FS_VFS #include From 8609045f2ad13a50ababd5da1eaed20070e83244 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Tue, 9 Nov 2021 17:13:07 +0800 Subject: [PATCH 04/17] repair 'mount sd card' --- .../XiUOS/board/aiit-arm32-board/Kconfig | 6 +++++ .../XiUOS/board/aiit-arm32-board/board.c | 18 +++++++++++++ .../XiUOS/board/aiit-riscv64-board/Kconfig | 7 +++++ .../XiUOS/board/aiit-riscv64-board/board.c | 22 ++++++++++++++++ Ubiquitous/XiUOS/board/kd233/Kconfig | 9 +++++++ Ubiquitous/XiUOS/board/kd233/board.c | 26 +++++++++++++++++++ Ubiquitous/XiUOS/kernel/thread/init.c | 10 ++++--- 7 files changed, 95 insertions(+), 3 deletions(-) diff --git a/Ubiquitous/XiUOS/board/aiit-arm32-board/Kconfig b/Ubiquitous/XiUOS/board/aiit-arm32-board/Kconfig index 5ded0a288..ed0e802b7 100644 --- a/Ubiquitous/XiUOS/board/aiit-arm32-board/Kconfig +++ b/Ubiquitous/XiUOS/board/aiit-arm32-board/Kconfig @@ -51,6 +51,12 @@ menu "aiit-arm32-board feature" endif endmenu + menu "config board peripheral" + config MOUNT_SDCARD + bool "mount cd card" + default n + select BSP_USING_SDIO + endmenu endmenu endmenu diff --git a/Ubiquitous/XiUOS/board/aiit-arm32-board/board.c b/Ubiquitous/XiUOS/board/aiit-arm32-board/board.c index b25c2266a..5bad81d7c 100644 --- a/Ubiquitous/XiUOS/board/aiit-arm32-board/board.c +++ b/Ubiquitous/XiUOS/board/aiit-arm32-board/board.c @@ -150,6 +150,24 @@ struct InitSequenceDesc _board_init[] = { " NONE ",NONE }, }; +#if defined(FS_VFS) && defined(MOUNT_SDCARD) +#include + +/** + * @description: Mount SD card + * @return 0 + */ +int MountSDCard(void) +{ + if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, FSTYPE_FATFS, "/") == 0) + DBG("sd card mount to '/'"); + else + SYS_WARN("sd card mount to '/' failed!"); + + return 0; +} +#endif + void InitBoardHardware() { int i = 0; diff --git a/Ubiquitous/XiUOS/board/aiit-riscv64-board/Kconfig b/Ubiquitous/XiUOS/board/aiit-riscv64-board/Kconfig index 53ce37e90..65db341b6 100644 --- a/Ubiquitous/XiUOS/board/aiit-riscv64-board/Kconfig +++ b/Ubiquitous/XiUOS/board/aiit-riscv64-board/Kconfig @@ -53,6 +53,13 @@ menu "aiit-riscv64-board feature" endif endmenu + menu "config board peripheral" + config MOUNT_SDCARD + bool "mount cd card" + default n + select BSP_USING_SDIO + endmenu + endmenu config __STACKSIZE__ diff --git a/Ubiquitous/XiUOS/board/aiit-riscv64-board/board.c b/Ubiquitous/XiUOS/board/aiit-riscv64-board/board.c index f92e554fc..17f15197c 100644 --- a/Ubiquitous/XiUOS/board/aiit-riscv64-board/board.c +++ b/Ubiquitous/XiUOS/board/aiit-riscv64-board/board.c @@ -64,6 +64,28 @@ extern int HwRtcInit(void); extern int HwTouchBusInit(void); extern int HwCh376Init(void); +#if defined(FS_VFS) && defined(MOUNT_SDCARD) +#include + +/** + * @description: Mount SD card + * @return 0 + */ +/** + * @description: Mount SD card + * @return 0 + */ +int MountSDCard(void) +{ + if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, FSTYPE_CH376, "/") == 0) + DBG("sd card mount to '/'"); + else + SYS_WARN("sd card mount to '/' failed!"); + + return 0; +} +#endif + void init_bss(void) { unsigned int *dst; diff --git a/Ubiquitous/XiUOS/board/kd233/Kconfig b/Ubiquitous/XiUOS/board/kd233/Kconfig index a416411f6..d65c31c13 100644 --- a/Ubiquitous/XiUOS/board/kd233/Kconfig +++ b/Ubiquitous/XiUOS/board/kd233/Kconfig @@ -58,6 +58,15 @@ menu "kd233 feature" endmenu + menu "config board peripheral" + config MOUNT_SDCARD + bool "mount cd card" + default n + select BSP_USING_SPI + select BSP_USING_SPI1 + select RESOURCES_SPI_SD + endmenu + endmenu config __STACKSIZE__ diff --git a/Ubiquitous/XiUOS/board/kd233/board.c b/Ubiquitous/XiUOS/board/kd233/board.c index 0aedededd..8bcb43525 100644 --- a/Ubiquitous/XiUOS/board/kd233/board.c +++ b/Ubiquitous/XiUOS/board/kd233/board.c @@ -62,6 +62,32 @@ extern int HwWdtInit(void); extern int HwLcdInit(void); extern int HwTimerInit(void); +#if defined(FS_VFS) && defined (MOUNT_SDCARD) +#include +#include +extern SpiSdDeviceType SpiSdInit(struct Bus *bus, const char *dev_name, const char *drv_name, const char *sd_name); + +/** + * @description: Mount SD card + * @return 0 + */ +int MountSDCard(void) +{ + struct Bus *spi_bus; + spi_bus = BusFind(SPI_BUS_NAME_1); + + if (NONE == SpiSdInit(spi_bus, SPI_1_DEVICE_NAME_0, SPI_1_DRV_NAME, SPI_SD_NAME)) { + KPrintf("MountSDCard SpiSdInit error!\n"); + return 0; + } + + if (EOK == MountFilesystem(SPI_BUS_NAME_1, SPI_SD_NAME, SPI_1_DRV_NAME, FSTYPE_FATFS, "/")) + KPrintf("SPI SD card fatfs mounted\n"); + + return 0; +} +#endif + void InitBss(void) { unsigned int *dst; diff --git a/Ubiquitous/XiUOS/kernel/thread/init.c b/Ubiquitous/XiUOS/kernel/thread/init.c index 6369e3529..6621d1c09 100644 --- a/Ubiquitous/XiUOS/kernel/thread/init.c +++ b/Ubiquitous/XiUOS/kernel/thread/init.c @@ -100,11 +100,15 @@ struct InitSequenceDesc components_init[] = }; struct InitSequenceDesc env_init[] = { -#ifdef ARCH_RISCV -#if defined (RESOURCES_SPI_SD)|| defined(RESOURCES_SDIO ) +// #ifdef ARCH_RISCV +// #if defined (RESOURCES_SPI_SD)|| defined(RESOURCES_SDIO ) +// { "MountSDCard", MountSDCard }, +// #endif +// #endif +#ifdef MOUNT_SDCARD { "MountSDCard", MountSDCard }, #endif -#endif + #ifdef FS_VFS_MNTTABLE { "DfsMountTable", DfsMountTable }, #endif From b3c770dd260f1482f233b2b0ef88cb305b04904f Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Tue, 9 Nov 2021 17:14:47 +0800 Subject: [PATCH 05/17] delete useless code --- .../Applications/app_test/sd_card_mount.c | 41 ----------------- .../Applications/app_test/spi_sd_card_mount.c | 46 ------------------- Ubiquitous/XiUOS/kernel/thread/init.c | 9 +--- 3 files changed, 1 insertion(+), 95 deletions(-) delete mode 100644 APP_Framework/Applications/app_test/sd_card_mount.c delete mode 100644 APP_Framework/Applications/app_test/spi_sd_card_mount.c diff --git a/APP_Framework/Applications/app_test/sd_card_mount.c b/APP_Framework/Applications/app_test/sd_card_mount.c deleted file mode 100644 index 3b660b2a3..000000000 --- a/APP_Framework/Applications/app_test/sd_card_mount.c +++ /dev/null @@ -1,41 +0,0 @@ -/* -* Copyright (c) 2020 AIIT XUOS Lab -* XiUOS is licensed under Mulan PSL v2. -* You can use this software according to the terms and conditions of the Mulan PSL v2. -* You may obtain a copy of Mulan PSL v2 at: -* http://license.coscl.org.cn/MulanPSL2 -* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, -* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, -* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. -* See the Mulan PSL v2 for more details. -*/ - -/** - * @file sd_card_mount.c - * @brief Mount SD card when opened SDIO - * @version 1.0 - * @author AIIT XUOS Lab - * @date 2021.04.19 - */ - -#include "user_api/switch_api/user_api.h" -#include - -#if defined(FS_VFS) -#include - -/** - * @description: Mount SD card - * @return 0 - */ -int MountSDCard(void) -{ - if (MountFilesystem(SDIO_BUS_NAME, SDIO_DEVICE_NAME, SDIO_DRIVER_NAME, FSTYPE_FATFS, "/") == 0) - DBG("sd card mount to '/'"); - else - SYS_WARN("sd card mount to '/' failed!"); - - return 0; -} -#endif -SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHELL_CMD_PARAM_NUM(0),MountSDCard, MountSDCard, MountSDCard ); diff --git a/APP_Framework/Applications/app_test/spi_sd_card_mount.c b/APP_Framework/Applications/app_test/spi_sd_card_mount.c deleted file mode 100644 index 4fa35b054..000000000 --- a/APP_Framework/Applications/app_test/spi_sd_card_mount.c +++ /dev/null @@ -1,46 +0,0 @@ -/* -* Copyright (c) 2020 AIIT XUOS Lab -* XiUOS is licensed under Mulan PSL v2. -* You can use this software according to the terms and conditions of the Mulan PSL v2. -* You may obtain a copy of Mulan PSL v2 at: -* http://license.coscl.org.cn/MulanPSL2 -* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, -* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, -* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. -* See the Mulan PSL v2 for more details. -*/ - -/** - * @file spi_sd_card_mount.c - * @brief Mount SD card when opened SPI SD card - * @version 1.0 - * @author AIIT XUOS Lab - * @date 2021.04.01 - */ - -#include "user_api/switch_api/user_api.h" -#include - -#if defined(FS_VFS) -#include - -/** - * @description: Mount SD card - * @return 0 - */ -int MountSDCard(void) -{ - struct Bus *spi_bus; - spi_bus = BusFind(SPI_BUS_NAME_1); - - if (NONE == SpiSdInit(spi_bus, SPI_1_DEVICE_NAME_0, SPI_1_DRV_NAME, SPI_SD_NAME)) { - KPrintf("MountSDCard SpiSdInit error!\n"); - return 0; - } - - if (EOK == MountFilesystem(SPI_BUS_NAME_1, SPI_SD_NAME, SPI_1_DRV_NAME, FSTYPE_FATFS, "/")) - KPrintf("SPI SD card fatfs mounted\n"); - - return 0; -} -#endif diff --git a/Ubiquitous/XiUOS/kernel/thread/init.c b/Ubiquitous/XiUOS/kernel/thread/init.c index 6621d1c09..adc4c7377 100644 --- a/Ubiquitous/XiUOS/kernel/thread/init.c +++ b/Ubiquitous/XiUOS/kernel/thread/init.c @@ -100,11 +100,7 @@ struct InitSequenceDesc components_init[] = }; struct InitSequenceDesc env_init[] = { -// #ifdef ARCH_RISCV -// #if defined (RESOURCES_SPI_SD)|| defined(RESOURCES_SDIO ) -// { "MountSDCard", MountSDCard }, -// #endif -// #endif + #ifdef MOUNT_SDCARD { "MountSDCard", MountSDCard }, #endif @@ -119,9 +115,6 @@ struct InitSequenceDesc env_init[] = }; struct InitSequenceDesc communication_init[] = { -// #ifdef BSP_USING_SDIO -// { "Stm32SdcardMount",Stm32SdcardMount }, -// #endif #ifdef BSP_USING_USBH { "STM32USBHostRegister", STM32USBHostRegister }, { "hw usb", Stm32HwUsbInit }, From f3d527cf705626a31cd02d7d1dc0e76069e861fb Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Tue, 9 Nov 2021 17:18:21 +0800 Subject: [PATCH 06/17] delete useless code --- Ubiquitous/XiUOS/kernel/include/xs_init.h | 1 - 1 file changed, 1 deletion(-) diff --git a/Ubiquitous/XiUOS/kernel/include/xs_init.h b/Ubiquitous/XiUOS/kernel/include/xs_init.h index 11b2ac6b3..1a82e0c30 100644 --- a/Ubiquitous/XiUOS/kernel/include/xs_init.h +++ b/Ubiquitous/XiUOS/kernel/include/xs_init.h @@ -44,7 +44,6 @@ extern int RtcNtpSyncInit(void); extern int MountSDCard(void); extern int DfsMountTable(void); extern int userShellInit(void); -extern int Stm32SdcardMount(void); extern int STM32USBHostRegister(void); extern int WorkSysWorkQueueInit(void); From eb146778325997a72f0a2ec48bdba2fa7ee6af21 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Wed, 10 Nov 2021 17:31:39 +0800 Subject: [PATCH 07/17] 1.add an API of finding the first user task in xiuos task manage list; 2.add restart application after ota --- APP_Framework/Applications/ota/README.md | 4 +-- APP_Framework/Applications/ota/ota.c | 28 +++++++++++++++-- .../transform_layer/xiuos/transform.c | 5 ++++ .../transform_layer/xiuos/transform.h | 1 + .../xiuos/user_api/posix_support/pthread.c | 1 + .../xiuos/user_api/switch_api/user_api.h | 2 ++ .../xiuos/user_api/switch_api/user_task.c | 11 +++++++ Ubiquitous/XiUOS/kernel/include/xs_ktask.h | 1 + Ubiquitous/XiUOS/kernel/include/xs_service.h | 1 + .../XiUOS/kernel/kernel_service/xs_service.c | 6 ++++ Ubiquitous/XiUOS/kernel/thread/ktask.c | 30 +++++++++++++++++++ 11 files changed, 86 insertions(+), 4 deletions(-) diff --git a/APP_Framework/Applications/ota/README.md b/APP_Framework/Applications/ota/README.md index 052b383ab..a8243454a 100644 --- a/APP_Framework/Applications/ota/README.md +++ b/APP_Framework/Applications/ota/README.md @@ -1,12 +1,12 @@ # OTA README -xiuos当前的ota功能允许应用bin文件可以通过4G实现远程的bin文件更新(限制:1、bin文件存放在设备SD卡并且应用从SD卡启动;2、暂且支持4G实现;3、暂时只支持aiit终端)。 +xiuos当前的ota功能允许应用bin文件可以通过4G实现远程的bin文件更新(限制:1、bin文件存放在设备SD卡并且应用从SD卡启动;2、暂且支持4G实现;3、暂时只支持aiit终端;4、只支持xiuos内核)。 ## 文件说明 | 名称 | 说明 | | -- | -- | -| ota.c| 设备OTA代码 | +| ota.c| xiuos设备OTA代码 | | ota_server.c | pc服务端的实例代码供参考 | diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index 52f13bb8a..4700f2da3 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -22,6 +22,8 @@ #include #include +extern int main(void); + struct ota_header_t { int16 frame_flag; ///< frame start flag 2 Bytes @@ -47,7 +49,8 @@ struct ota_data struct ota_frame_t frame; }; -pthread_t ota_ktask; +pthread_t ota_task; +pthread_t restart_main; /** * @description: CRC16 check @@ -117,6 +120,26 @@ static int CrcFileCheck(uint32 crc_check, unsigned long total_len) return ret; } +static void RestartApplication(void) +{ + pthread_attr_t attr; + attr.schedparam.sched_priority = 10; + attr.stacksize = 2048; + + while(1) + { + unsigned long pid = PrivUserTaskSearch(); + if (pid > 0) + { + PrivTaskDelete(pid, 0); + } + else + { + break; + } + } + PrivTaskCreate(&restart_main, &attr, (void *)main, NULL); +} static int OtaDataRecv(struct Adapter* adapter) { struct ota_data recv_msg; @@ -172,6 +195,7 @@ try_again: } } close(fd); + RestartApplication(); return ret; } @@ -233,6 +257,6 @@ void ApplicationOtaTaskInit(void) attr.schedparam.sched_priority = 10; attr.stacksize = 2048; - PrivTaskCreate(&ota_ktask, &attr, OtaKTaskEntry, NULL); + PrivTaskCreate(&ota_task, &attr, OtaKTaskEntry, NULL); } \ No newline at end of file diff --git a/APP_Framework/Framework/transform_layer/xiuos/transform.c b/APP_Framework/Framework/transform_layer/xiuos/transform.c index bc82fdf62..2eb00b8a3 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/transform.c +++ b/APP_Framework/Framework/transform_layer/xiuos/transform.c @@ -79,6 +79,11 @@ int PrivTaskStartup(pthread_t *thread) { return 0; } +/* private API of xiuos to search the first user task in manage list */ +int PrivUserTaskSearch(void) +{ + return UserTaskSearch(); +} int PrivTaskDelete(pthread_t thread, int sig) { diff --git a/APP_Framework/Framework/transform_layer/xiuos/transform.h b/APP_Framework/Framework/transform_layer/xiuos/transform.h index edd22e618..b2996fd44 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/transform.h +++ b/APP_Framework/Framework/transform_layer/xiuos/transform.h @@ -171,6 +171,7 @@ int PrivTaskStartup(pthread_t *thread); int PrivTaskDelete(pthread_t thread, int sig); void PrivTaskQuit(void *value_ptr); int PrivTaskDelay(int32_t ms); +int PrivUserTaskSearch(void); /*********************driver*************************/ 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 12a181418..6cf2ed4a9 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 @@ -99,6 +99,7 @@ int pthread_setcanceltype(int type, int *oldtype) int pthread_kill(pthread_t thread, int sig) { /* This api should not be used, and will not be supported */ + UserTaskDelete(thread); return -1; } diff --git a/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_api.h b/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_api.h index 803af707f..fc55b7433 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_api.h +++ b/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_api.h @@ -60,6 +60,7 @@ int32_t UserTaskCreate(UtaskType utask); long UserTaskStartup(int32_t id); long UserTaskDelete(int32_t id); +long UserTaskSearch(void); void UserTaskQuit(void); long UserTaskDelay(int32_t ms); long UserGetTaskName(int32_t id ,char *name); @@ -185,6 +186,7 @@ int32_t UserTaskCreate(UtaskType utask); #define UserTaskDelete KTaskDelete #define UserTaskQuit KTaskQuit #define UserTaskDelay MdelayKTask +#define UserTaskSearch UTaskSearch long UserGetTaskName(int32_t id ,char *name); int32_t UserGetTaskID(void); diff --git a/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_task.c b/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_task.c index 5a41b19e3..3663b1d16 100644 --- a/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_task.c +++ b/APP_Framework/Framework/transform_layer/xiuos/user_api/switch_api/user_task.c @@ -60,6 +60,17 @@ x_err_t UserTaskDelete(int32_t id){ return (x_err_t)KSwitch1(KS_USER_TASK_DELETE,(uintptr_t)id); } +/** + * This function will find the first user task in task manage list. + * + * @param + * + * @return task id on success; EINVALED on failure + */ +x_err_t UserTaskSearch(void){ + return KSwitch0(KS_USER_TASK_SEARCH); +} + void UserTaskQuit(void){ KSwitch0(KS_USER_TASK_EXECEXIT); return ; diff --git a/Ubiquitous/XiUOS/kernel/include/xs_ktask.h b/Ubiquitous/XiUOS/kernel/include/xs_ktask.h index 05bde8561..566b35824 100644 --- a/Ubiquitous/XiUOS/kernel/include/xs_ktask.h +++ b/Ubiquitous/XiUOS/kernel/include/xs_ktask.h @@ -159,6 +159,7 @@ int32 KTaskCreate(const char *name, void KTaskQuit(void); #ifdef SEPARATE_COMPILE +int UTaskSearch(void); int32 UTaskCreate(const char *name, void (*entry)(void *parameter), void *parameter, diff --git a/Ubiquitous/XiUOS/kernel/include/xs_service.h b/Ubiquitous/XiUOS/kernel/include/xs_service.h index db383b63e..d213f9f9f 100644 --- a/Ubiquitous/XiUOS/kernel/include/xs_service.h +++ b/Ubiquitous/XiUOS/kernel/include/xs_service.h @@ -33,6 +33,7 @@ enum KernelServiceEnum KS_USER_TASK_CREATE , KS_USER_TASK_STARTUP , KS_USER_TASK_DELETE, + KS_USER_TASK_SEARCH, KS_USER_TASK_EXECEXIT, KS_USER_TASK_CORE_COMBINE, KS_USER_TASK_CORE_UNCOMBINE, diff --git a/Ubiquitous/XiUOS/kernel/kernel_service/xs_service.c b/Ubiquitous/XiUOS/kernel/kernel_service/xs_service.c index 639a13553..ecea0e990 100644 --- a/Ubiquitous/XiUOS/kernel/kernel_service/xs_service.c +++ b/Ubiquitous/XiUOS/kernel/kernel_service/xs_service.c @@ -120,6 +120,11 @@ uintptr_t KsTaskDelete(uint32_t knum,uintptr_t *param, uint8_t num) return (uintptr_t)ret; } +uintptr_t KsUserTaskSerach(uint32_t knum,uintptr_t *param, uint8_t num ) +{ + return (uintptr_t)UTaskSearch(); +} + extern void KTaskQuit(void); uintptr_t KsTaskQuit(uint32_t knum,uintptr_t *param, uint8_t num ) { @@ -567,6 +572,7 @@ struct KernelService g_service_table[256] __attribute__ ((section (".g_service_ [KS_USER_TASK_CREATE] = { KsTaskCreate, 5 }, [KS_USER_TASK_STARTUP] = { KsStartupTask, 1 }, [KS_USER_TASK_DELETE] = { KsTaskDelete, 1 }, + [KS_USER_TASK_SEARCH] = { KsUserTaskSerach, 0 }, [KS_USER_TASK_EXECEXIT] = { KsTaskQuit, 0 }, [KS_USER_TASK_CORE_COMBINE] = { KsTaskCoreCombine, 2 }, [KS_USER_TASK_CORE_UNCOMBINE] = { KsTaskCoreUnCombine, 1 }, diff --git a/Ubiquitous/XiUOS/kernel/thread/ktask.c b/Ubiquitous/XiUOS/kernel/thread/ktask.c index 4753c7ad3..393d6c969 100644 --- a/Ubiquitous/XiUOS/kernel/thread/ktask.c +++ b/Ubiquitous/XiUOS/kernel/thread/ktask.c @@ -1052,6 +1052,36 @@ x_err_t KTaskWakeup(int32 id) #ifdef SEPARATE_COMPILE +/** + * find the first user task in manage list. + * + * @param + * + * @note in interrupt status,this function is not permitted to call. + */ +int UTaskSearch(void) +{ + x_base lock = 0; + KTaskDescriptorType temp_task = NONE; + struct SysDoubleLinklistNode *node = NONE; + + lock = CriticalAreaLock(); + + DOUBLE_LINKLIST_FOR_EACH(node,&xiaoshan_task_head) + { + temp_task = SYS_DOUBLE_LINKLIST_ENTRY(node, struct TaskDescriptor, link); + if (1 == temp_task->task_dync_sched_member.isolation_flag) + { + CriticalAreaUnLock(lock); + return temp_task->id.id; + } + } + + CriticalAreaUnLock(lock); + + return -1; +} + /** * * This function init a user task in dynamic way . From 25cd73821f81b2a0fbd2f979c2b79356c7a9181d Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Fri, 12 Nov 2021 18:24:52 +0800 Subject: [PATCH 08/17] fix a bug of malloc big memory failed --- Ubiquitous/XiUOS/kernel/memory/byte_manage.c | 43 +++++++++++++++----- 1 file changed, 33 insertions(+), 10 deletions(-) diff --git a/Ubiquitous/XiUOS/kernel/memory/byte_manage.c b/Ubiquitous/XiUOS/kernel/memory/byte_manage.c index e3e1f1662..1f447528d 100644 --- a/Ubiquitous/XiUOS/kernel/memory/byte_manage.c +++ b/Ubiquitous/XiUOS/kernel/memory/byte_manage.c @@ -350,11 +350,12 @@ static void* BigMemMalloc(struct DynamicBuddyMemory *dynamic_buddy, x_size_t siz /* best-fit method */ for (node = dynamic_buddy->mm_freenode_list[ndx].next; - node && (node->size < allocsize); + (ndx < MEM_LINKNRS ) && (node->size < allocsize); node = node->next) { + ndx++; }; /* get the best-fit freeNode */ - if (node && (node->size > allocsize)) { + if (node && (node->size >= allocsize)) { struct DynamicFreeNode *remainder; struct DynamicFreeNode *next; uint32 remaining; @@ -1197,25 +1198,47 @@ SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC)|SHE */ void ShowBuddy(void) { + int i = 0; int lock = 0; struct DynamicFreeNode *debug = NONE; lock = CriticalAreaLock(); KPrintf("\n\033[41;1mlist memory information\033[0m\n", __func__); - for (debug = ByteManager.dynamic_buddy_manager.mm_freenode_list[0].next; - debug;debug = debug->next){ - KPrintf("%s,current is %x,next is %x, size %u, flag %x\n",__func__, debug, debug->next,debug->size,debug->flag); - }; + for(int level = 0; level < MEM_LINKNRS; level++) { + KPrintf("\n %s level [%d],memory size[2^%d] \n",__func__, level,level +6); + for (debug = &ByteManager.dynamic_buddy_manager.mm_freenode_list[level]; ; ) { + if(debug->size > 0) + KPrintf(" [current node %x,next node %x, size %u, flag %x]\n",debug, debug->next,debug->size,debug->flag); + else + KPrintf(" [listhead node %x,next node %x]\n",debug, debug->next); + if(debug->next) + debug = debug->next; + + if(debug->size == 0) + break; + }; + } + KPrintf("\nlist memory information\n\n"); #ifdef MEM_EXTERN_SRAM for(i = 0; i < EXTSRAM_MAX_NUM; i++) { if(NONE != ExtByteManager[i].done){ KPrintf("\nlist extern sram[%d] memory information\n\n",i); - for (debug = ExtByteManager[i].dynamic_buddy_manager.mm_freenode_list[0].next; - debug;debug = debug->next){ - KPrintf("%s,current is %x,next is %x, size %u, flag %x\n",__func__, debug, debug->next,debug->size,debug->flag); - }; + for(int lev = 0; lev < MEM_LINKNRS; lev++) { + KPrintf("\n %s level [%d],memory size[2^%d] \n",__func__, lev,lev +6); + for (debug = & ExtByteManager[i].dynamic_buddy_manager.mm_freenode_list[lev]; ; ) { + if(debug->size > 0) + KPrintf(" [current node %x,next node %x, size %u, flag %x]\n",debug, debug->next,debug->size,debug->flag); + else + KPrintf(" [listhead node %x,next node %x]\n",debug, debug->next); + if(debug->next) + debug = debug->next; + + if(debug->size == 0) + break; + } + } } } From 2930757728f22eb19276d3bd0a87bf3f42c24762 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Mon, 15 Nov 2021 10:12:45 +0800 Subject: [PATCH 09/17] add the totle length with the last frame of ota server --- APP_Framework/Applications/ota/ota_server.c | 1 + 1 file changed, 1 insertion(+) diff --git a/APP_Framework/Applications/ota/ota_server.c b/APP_Framework/Applications/ota/ota_server.c index 84ab322d8..ba01e6d48 100644 --- a/APP_Framework/Applications/ota/ota_server.c +++ b/APP_Framework/Applications/ota/ota_server.c @@ -207,6 +207,7 @@ try_again: len = read(file_fd, file_buf, file_length); if(len > 0) { + data.header.total_len = file_length; 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!@")); From 251846604a65c1d06a1df9a80dda9e2cf76aa12c Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Tue, 16 Nov 2021 09:43:54 +0800 Subject: [PATCH 10/17] repair ota_server.c --- APP_Framework/Applications/ota/ota_server.c | 43 +++++---------------- 1 file changed, 10 insertions(+), 33 deletions(-) diff --git a/APP_Framework/Applications/ota/ota_server.c b/APP_Framework/Applications/ota/ota_server.c index ba01e6d48..e59d184c5 100644 --- a/APP_Framework/Applications/ota/ota_server.c +++ b/APP_Framework/Applications/ota/ota_server.c @@ -17,29 +17,6 @@ #include #include -/*******************************mail*****************************/ - -#define MAX_BUFF_SIZE (2048) -#define IP_ADDR ("smtp.163.com") -#define IP_ADDR_PORT (25) -#define USERNAME ("18535861947@163.com") -#define TO_ADDRESS ("522736215@qq.com") -#define ACCESS_PORT ("有链接已接入服务器端口") -#define USING_DEVICE_AUTH 1 - - -#define BUFLEN 1024 -#define LISTNUM 20 -#define IP_ADDRESS "0.0.0.0" - -/*宏定义*/ -#define SID_MAX_LENGHT 16 -#define NAME_MAX_LENGHT 16 -#define MAJOR_MAX_LENGHT 64 -#define CMD_MAX_LENGHT 1024 -#define REDIS_SERVER_IP "127.0.0.1" -#define REDIS_SERVER_PORT 6379 - typedef int BOOL; #define true 1 #define false 0 @@ -53,9 +30,9 @@ socklen_t len; struct ota_header_t { - int16 frame_flag; ///< frame start flag 2 Bytes - uint8 dev_type; ///< device type - uint8 burn_mode; ///< data burn way + 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 @@ -64,10 +41,10 @@ struct ota_header_t struct ota_frame_t { - uint32 frame_id; ///< Current frame id - uint32 frame_len; ///< Current frame data length + 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 - uint32 crc; ///< Current frame data crc + uint32_t crc; ///< Current frame data crc }; struct ota_data @@ -163,15 +140,15 @@ int OtaFileSend(int fd) memset(&data, 0, sizeof(data)); data.header.frame_flag = 0x5A5A; - len = read(file_fd, data.frame.frame_data, 200 ); + len = fread( data.frame.frame_data, 200, 1, 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 */ - lseek(file_fd, len, SEEK_CUR); + memcpy(&data.frame.frame_data[len], "!@", strlen("!@")); /* add '!@' as ending flag */ + fseek(file_fd, len, SEEK_CUR); try_again: send(fd, &data, sizeof(data), MSG_NOSIGNAL); @@ -205,7 +182,7 @@ try_again: data.header.frame_flag = 0x5A5A; - len = read(file_fd, file_buf, file_length); + 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!@");; From 5a8b6d79aa96ea8a757a403e05d93efce31d9564 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Sat, 20 Nov 2021 16:52:16 +0800 Subject: [PATCH 11/17] repair somebug of ota/nbiot/adapter agent --- APP_Framework/Applications/ota/ota.c | 102 +++++-- APP_Framework/Applications/ota/ota_server.c | 57 ++-- .../Framework/connection/adapter_agent.c | 35 +-- APP_Framework/Framework/connection/at_agent.h | 3 +- .../connection/nbiot/adapter_nbiot.c | 19 +- .../Framework/connection/nbiot/bc28/bc28.c | 249 ++++++++++++++++-- .../xiuos/user_api/posix_support/pthread.c | 2 + 7 files changed, 369 insertions(+), 98 deletions(-) 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; } From 9d970e7832dab194d340ec32e1f386039bc21c42 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Mon, 22 Nov 2021 17:50:27 +0800 Subject: [PATCH 12/17] add mutex protection for adapter agent --- APP_Framework/Applications/ota/ota.c | 24 ++- APP_Framework/Applications/ota/ota_server.c | 2 +- .../Framework/connection/adapter_agent.c | 71 +++++-- .../Framework/connection/nbiot/bc28/bc28.c | 180 +++++++++++------- .../transform_layer/xiuos/transform.c | 2 + 5 files changed, 183 insertions(+), 96 deletions(-) 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) { From f24189d7e1a92476ea6dd4ab49acda90399a3d51 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Thu, 25 Nov 2021 14:24:11 +0800 Subject: [PATCH 13/17] repair ota failed by add mutex protect --- APP_Framework/Applications/ota/ota.c | 132 +++++++++++------- APP_Framework/Applications/ota/ota_server.c | 127 ++++++++++++----- .../Framework/connection/4g/ec200t/ec200t.c | 2 +- .../Framework/connection/adapter_agent.c | 69 +++++---- APP_Framework/Framework/connection/at_agent.h | 3 +- 5 files changed, 222 insertions(+), 111 deletions(-) diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index 649e21587..c8a803797 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -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; - } +// 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; - } - +// 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); diff --git a/APP_Framework/Applications/ota/ota_server.c b/APP_Framework/Applications/ota/ota_server.c index 604d94da1..f8cabcdc7 100644 --- a/APP_Framework/Applications/ota/ota_server.c +++ b/APP_Framework/Applications/ota/ota_server.c @@ -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)) +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 = 5; - printf("ota send current[%d] frame.\n",frame_cnt); + 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; } diff --git a/APP_Framework/Framework/connection/4g/ec200t/ec200t.c b/APP_Framework/Framework/connection/4g/ec200t/ec200t.c index 25994ab64..bfd2f89b7 100644 --- a/APP_Framework/Framework/connection/4g/ec200t/ec200t.c +++ b/APP_Framework/Framework/connection/4g/ec200t/ec200t.c @@ -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"); } diff --git a/APP_Framework/Framework/connection/adapter_agent.c b/APP_Framework/Framework/connection/adapter_agent.c index 85d543bfa..4a0c6d246 100755 --- a/APP_Framework/Framework/connection/adapter_agent.c +++ b/APP_Framework/Framework/connection/adapter_agent.c @@ -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) { + PrivMutexObtain(&agent->lock); if (agent->reply != NULL){ - PrivMutexObtain(&agent->lock); 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; diff --git a/APP_Framework/Framework/connection/at_agent.h b/APP_Framework/Framework/connection/at_agent.h index 20f4e875b..3267698cf 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 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; From 32cb32432472e20a02f4119e2b1f6c544551b5b6 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Mon, 29 Nov 2021 17:33:19 +0800 Subject: [PATCH 14/17] repair the total bin crc failed problem --- APP_Framework/Applications/main.c | 1 + APP_Framework/Applications/ota/ota.c | 86 ++++++++++++++++----- APP_Framework/Applications/ota/ota_server.c | 22 ++++-- 3 files changed, 83 insertions(+), 26 deletions(-) diff --git a/APP_Framework/Applications/main.c b/APP_Framework/Applications/main.c index bfa04753e..b23c8d881 100644 --- a/APP_Framework/Applications/main.c +++ b/APP_Framework/Applications/main.c @@ -13,6 +13,7 @@ #include #include #include +#include extern int FrameworkInit(); extern void ApplicationOtaTaskInit(void); diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index c8a803797..12de96cca 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -59,11 +59,12 @@ pthread_t restart_main; * @param length data length * @return check code */ -uint32_t OtaCrc16(uint8_t * data, uint8_t length) +uint32_t OtaCrc16(uint8_t * data, uint32_t length) { int j; unsigned int reg_crc=0xFFFF; - + printf("crc data length[%d] Bytes,",length); + while (length--) { reg_crc ^= *data++; for (j=0;j<8;j++) { @@ -73,21 +74,46 @@ uint32_t OtaCrc16(uint8_t * data, uint8_t length) reg_crc=reg_crc >>1; } } - + printf(" crc = [0x%x]\n",reg_crc); return reg_crc; } +uint32_t FileCrc16(uint8_t * data, uint32_t length, unsigned int last_crc) +{ + int j; + //printf("crc data length[%d] Bytes,",length); + + while (length--) { + last_crc ^= *data++; + for (j=0;j<8;j++) { + if(last_crc & 0x01) + last_crc = last_crc >>1 ^ 0xA001; + else + last_crc = last_crc >>1; + } + } + + //printf(" crc = [0x%x]\n",last_crc); + + return last_crc; +} + + 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"); + int ret = 0; + int fd_t = 0; + fd_t = open( BOARD_APP_NAME, O_RDWR | O_APPEND); + ret = write(fd, buf, len); + if(ret < 0){ + printf("fd = %d write buf len[%d] failed.ret = %d\n",fd_t,len,ret); + } + else + { + printf("fd[%d] write buf length[%d] done.\n",fd_t,ret); + } - // close(fd); + close(fd_t); } static int CrcFileCheck(uint32 crc_check, unsigned long total_len) @@ -96,6 +122,8 @@ static int CrcFileCheck(uint32 crc_check, unsigned long total_len) int fd = 0; int len = 0; char *buf = NULL; + unsigned int last_crc = 0xffff; + unsigned long already_crc_length = 0; fd = open( BOARD_APP_NAME, O_RDONLY ); if(fd < 0){ @@ -103,18 +131,35 @@ static int CrcFileCheck(uint32 crc_check, unsigned long total_len) return -1; } - buf = PrivMalloc(total_len); + buf = PrivMalloc(128); if(NULL == buf) { printf("malloc failed.\n"); close(fd); - return -1; + return 0; } - len = read(fd, buf, total_len); - - if (crc_check != OtaCrc16(buf, len)) + /* crc check every 1024 Bytes until crc all the total file */ + while(already_crc_length != total_len) { + memset(buf , 0 , 128); + len = read(fd, buf, 128); + if(len < 0) + { + printf("file read failed.ret = %d\n",len); + ret = -1; + break; + } + + last_crc = FileCrc16(buf, len, last_crc); + already_crc_length += len; + printf("read len[%d] Bytes,already_crc_length[%d]\n",len,already_crc_length); + } + + + if (last_crc != crc_check) + { + printf("file crc error!!! last crc[%x] != check[%x]\n",last_crc,crc_check); ret =-1; } @@ -153,12 +198,13 @@ static int OtaDataRecv(struct Adapter* adapter) 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, S_IRUSR | S_IWUSR); if(fd < 0) { printf("open %s failed\n",BOARD_APP_NAME); return -1; } + close(fd); while(1) { memset(&recv_msg, 0, sizeof(struct ota_data)); @@ -166,11 +212,9 @@ static int OtaDataRecv(struct Adapter* adapter) 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); + printf("total [%d]frames [%d]Bytes crc[%x],receive successful,\n",frame_cnt,recv_msg.header.total_len,recv_msg.frame.crc); if(0 != CrcFileCheck(recv_msg.frame.crc, recv_msg.header.total_len)) { printf("crc check %s bin failed.please try again.\n", BOARD_APP_NAME); @@ -178,6 +222,7 @@ static int OtaDataRecv(struct Adapter* adapter) break; } PrivTaskDelay(500); + printf("tolal file crc done.send ok\n"); memset(reply, 0, 16); memcpy(reply, "ok", strlen("ok")); @@ -185,6 +230,7 @@ static int OtaDataRecv(struct Adapter* adapter) ret = 0; break; } + frame_cnt = recv_msg.frame.frame_id; if(0 == strncmp("wait_ok_timeout",recv_msg.frame.frame_data, strlen("wait_ok_timeout"))) { diff --git a/APP_Framework/Applications/ota/ota_server.c b/APP_Framework/Applications/ota/ota_server.c index f8cabcdc7..e5760f065 100644 --- a/APP_Framework/Applications/ota/ota_server.c +++ b/APP_Framework/Applications/ota/ota_server.c @@ -61,11 +61,13 @@ pthread_t ota_ktask; * @param length data length * @return check code */ -uint32_t OtaCrc16(uint8_t * data, uint8_t length) +uint32_t OtaCrc16(uint8_t * data, uint32_t length) { int j; unsigned int reg_crc=0xFFFF; - + + printf("crc data length[%d] Bytes,",length); + while (length--) { reg_crc ^= *data++; for (j=0;j<8;j++) { @@ -75,7 +77,7 @@ uint32_t OtaCrc16(uint8_t * data, uint8_t length) reg_crc=reg_crc >>1; } } - + printf(" crc = [0x%x]\n",reg_crc); return reg_crc; } @@ -196,20 +198,28 @@ recv_again: } } - /* finally,check total bin file.*/ + /* finally,crc 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"); + printf("total send file length[%d] Bytes [%d] frames.\n",file_length,frame_cnt); + printf("now crc 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; + + file_fd = fopen("/home/aep04/wwg/XiUOS_aiit-arm32-board_app.bin", "r"); + if (NULL == file_fd){ + printf("open file failed.\n"); + return -1; + } fseek(file_fd, 0, SEEK_SET); length = fread(file_buf,1, file_length, file_fd); + printf("read file length = %d\n",length); if(length > 0) { + data.frame.frame_id = frame_cnt; data.header.total_len = file_length; data.frame.frame_len = strlen("aiit_ota_end"); data.frame.crc = OtaCrc16(file_buf, length); From 1c60efe8a8cfffe3575fc2825e3c229ab41f00f0 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Mon, 29 Nov 2021 17:46:18 +0800 Subject: [PATCH 15/17] add debug info --- APP_Framework/Applications/ota/ota.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index 12de96cca..807adf9db 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -180,6 +180,7 @@ static void RestartApplication(void) unsigned long pid = PrivUserTaskSearch(); if (pid > 0) { + printf("kill usertask pid[%d]\n",pid); PrivTaskDelete(pid, 0); } else @@ -187,6 +188,7 @@ static void RestartApplication(void) break; } } + printf("restart main.\n"); PrivTaskCreate(&restart_main, &attr, (void *)main, NULL); } static int OtaDataRecv(struct Adapter* adapter) From ac69e135ca999c879c3acae655ec4b2a07efc895 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Tue, 30 Nov 2021 10:12:42 +0800 Subject: [PATCH 16/17] repair a bug of restart main after ota --- APP_Framework/Applications/ota/ota.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index 807adf9db..42561b3df 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -178,10 +178,11 @@ static void RestartApplication(void) while(1) { unsigned long pid = PrivUserTaskSearch(); - if (pid > 0) + if ((pid > 0) && (pid != pthread_self())) { printf("kill usertask pid[%d]\n",pid); PrivTaskDelete(pid, 0); + PrivTaskDelay(1000); } else { From 4fc79c211ab7b8f949b29f4c1d9bfabbf0700220 Mon Sep 17 00:00:00 2001 From: Wang_Weigen Date: Tue, 30 Nov 2021 10:34:08 +0800 Subject: [PATCH 17/17] add a tip --- APP_Framework/Applications/ota/ota.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/APP_Framework/Applications/ota/ota.c b/APP_Framework/Applications/ota/ota.c index 42561b3df..d361a0f78 100644 --- a/APP_Framework/Applications/ota/ota.c +++ b/APP_Framework/Applications/ota/ota.c @@ -182,7 +182,7 @@ static void RestartApplication(void) { printf("kill usertask pid[%d]\n",pid); PrivTaskDelete(pid, 0); - PrivTaskDelay(1000); + PrivTaskDelay(1000); /* NOTE:this delay will make a schedule and recycle all user task */ } else {