diff --git a/applications/_main.c b/applications/_main.c index c11a7e8..5a29a85 100644 --- a/applications/_main.c +++ b/applications/_main.c @@ -70,7 +70,7 @@ void checkTT() repGetTT(); //持续更新 } -static void getFileData_thread_entry(void) +static void upSendFile_thread_entry(void) { //等待事件 extern struct rt_event hw_check; //硬件条件 @@ -78,13 +78,6 @@ static void getFileData_thread_entry(void) static rt_uint8_t d[10][200] = { }; static rt_uint8_t s[10] = { }; -// while (tcpInit() != RT_EOK) -// { -// LOG_W("TT server is not ready."); -// rt_thread_mdelay(100); -// } -// LOG_D("TT server is ready."); - while (1) { if (rt_event_recv(&hw_check, ALL_READY, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, @@ -103,8 +96,6 @@ static void getFileData_thread_entry(void) for (size_t var = 0; var < len; var++) { sendMsg(d[var], s[var]); -// LOG_D("i = %d", var); -// LOG_HEX("d", 27, d[var], s[var]); } tcpClose(); LOG_I("Done."); @@ -115,10 +106,10 @@ static void getFileData_thread_entry(void) } //MSH_CMD_EXPORT(getFileData_thread_entry,tet); -static void gg(void) +static void upSendFile(void) { /* 创建 serial 线程 */ - rt_thread_t thread = rt_thread_create("PSmsg", getFileData_thread_entry, RT_NULL, 1024 * 12, 27, 10); + rt_thread_t thread = rt_thread_create("upSendFile", upSendFile_thread_entry, RT_NULL, 1024 * 12, 27, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { @@ -126,13 +117,13 @@ static void gg(void) } else { - LOG_E("thread 'PSmsg' create failure."); + LOG_E("thread 'upSendFile' create failure."); return; } } -static void try() +static void chkAndSendFile_thread_entry() { static rt_err_t rst = RT_ERROR; updatecfg(); @@ -178,44 +169,23 @@ static void try() } } //continue; - //发送数据 +//发送数据 //可能有多个待发文件,每个文件打包后为一个二维数组 -// 如果先打包后发送,需要三维数组,和大内存占用 -// 故改为“打包-发送”轮询操作 -//gg(); -#ifdef _NO - char *f="1023_05_19_15_29_59_254.txt"; - // pack file -// static rt_uint8_t d[10][200] = { }; -// static rt_uint8_t s[10] = { }; - rt_uint8_t len = 0; - // LOG_D("%p--%p",d,s); - // LOG_I("pack %s ...",f); - len = pack_File(f, 0, d, s); - rt_kprintf("len is %d\n", len); - if (len) - { - for (size_t var = 0; var < len; ++var) - { - // LOG_HEX("d",27,d[var],s[var]); +//如果先打包后发送,需要三维数组,和大内存占用 +//故改为“打包-发送”轮询操作 +// upSendFile(); - } - LOG_I("Done."); - } -#endif LOG_I("----------------------------"); -//initEvent(); -// rt_thread_mdelay(100); } } -void smsg() +void chkAndSendFile() { /* 创建 serial 线程 */ - rt_thread_t thread = rt_thread_create("checkSta", try, RT_NULL, 1024 * 5, 24, 10); + rt_thread_t thread = rt_thread_create("chk&send", chkAndSendFile_thread_entry, RT_NULL, 1024 * 5, 24, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { @@ -223,7 +193,7 @@ void smsg() } else { - LOG_E("thread 'checkSta' create failure."); + LOG_E("thread 'chk&send' create failure."); return; } } @@ -262,8 +232,8 @@ void deiniCnt() #define FUNC_DEMO #ifdef FUNC_DEMO //测试时导出命令到控制台 -MSH_CMD_EXPORT(smsg, smsg); -MSH_CMD_EXPORT(gg, file); +MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile); +MSH_CMD_EXPORT(upSendFile, upSendFile); MSH_CMD_EXPORT(iniCnt,初始化tcp连接); MSH_CMD_EXPORT(deiniCnt,初始化tcp连接); #endif diff --git a/applications/func/func.c b/applications/func/func.c index 6164745..97cbc14 100644 --- a/applications/func/func.c +++ b/applications/func/func.c @@ -15,6 +15,57 @@ #define LOG_LVL LOG_LVL_DBG #include +/** + * 将收发数据以ASCII字符形式存入log + * @param din 待存储数据 + * @param len 数据长度 + * @return 1-正常,0-异常 + */ +int trDataTolog(uint8_t *din, size_t len, uint8_t isTx) +{ + char rootDir[22] = ROOT_PATH_LOG; + mkdir(rootDir, 0); + + char tstmp[30] = ""; + char fn[50] = ""; + strcat(fn,rootDir); + strcat(fn,"20"); + strcat(fn,date2Str(tstmp));//以小时为独立文件 + fn[strlen(fn)-3]='\0'; + strcat(fn,"/"); + mkdir(fn, 0); + + strcat(fn,tstmp); + strcat(fn,".log"); + + int fd = open(fn, O_WRONLY | O_CREAT | O_APPEND); + if (fd < 0) + { + LOG_E("open file %s failed!", fn); + return -RT_ERROR; + } + else + { + char log[300]=""; + char tmp[200]=""; + strcat(log,getTimestmp(tmp)); + strcat(log,isTx?" [T]: ":" [R]: "); +// strcat(log,bytes2str(din, len, 16, " ", tmp)); + strcat(log,din); + strcat(log,"\r\n"); + + int rst = write(fd, log, strlen(log)); + if (rst != strlen(log)) + { + LOG_E("write to file %s failed!", fn); + close(fd); + return -RT_ERROR; + } + close(fd); + return RT_EOK; + } +} + RT_WEAK int set_cfg(const char *k, const char*v) { return; @@ -115,6 +166,7 @@ void upSend_thread_entry(void* parameter) { tcpClose(); LOG_I("send to TT Done."); + }; } return 0; @@ -127,6 +179,28 @@ void upSend_thread_entry(void* parameter) */ RT_WEAK int upSend(uint8_t *din, size_t len) { + LOG_D("try to upsend to TT."); + + uint8_t sta = 1; + while (0 && !isTTjh()) //判断TT状态 + { + static uint8_t trycnt = 0; + rt_thread_mdelay(4000); + trycnt += 1; + if (trycnt > 3) + { + sta = 0; //try 3 time + break; + } + } + if (!sta) { + //cache to file + LOG_W("TT is not ready, try to cache %d bytes data to file.",len); + cacheDataToFile(din, len); +// trDataTolog(din, len, 1); + return -RT_ERROR; + } + //此函数有打包操作,需线程操作 // LOG_D("upsend."); // return 0; @@ -137,16 +211,17 @@ RT_WEAK int upSend(uint8_t *din, size_t len) msg.len=len; /* 创建 serial 线程 */ - rt_thread_t thread = rt_thread_create("upSend", upSend_thread_entry, (void *) &msg, 1024 * 10, 27, 10); + rt_thread_t thread = rt_thread_create("upSend", upSend_thread_entry, (void *) &msg, 1024 * 5, 27, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { rt_thread_startup(thread); + return RT_EOK; } else { LOG_E("thread 'upSend' create failure."); - return; + return -RT_ERROR; } } @@ -567,7 +642,7 @@ void ttRunCMD(uint8_t *din, size_t len) } -RT_WEAK int xpParse(uint8_t * din, size_t len) +RT_WEAK int formatAndSendTo3S(uint8_t * din, size_t len) { // LOG_D("直接调用小彭的函数进行处理。"); char str[200] = "RCV:"; @@ -642,7 +717,7 @@ void parse3SData(uint8_t *din, size_t count) { // - xpParse(din,count); + formatAndSendTo3S(din,count); } if (din[2] == ADDR_ANJI) { @@ -656,7 +731,14 @@ void parse3SData(uint8_t *din, size_t count) reportDepth(din, count); break; default: - cacheData(din, count); + if (isTTon()) + {//如果TT为开机状态(具备通信状态?待商榷)则不缓存直接发 + upSend(din, count); + } + else { + cacheDataToFile(din, count); + } + } // upSend(din, count); } @@ -736,6 +818,40 @@ void parseTTData(uint8_t *din, size_t len) } +/** + * get file size + * @param file file name whith fullpath + */ +unsigned long getFileSize(char *file) +{ + struct stat stat; + char *fullpath, *path; + + #ifdef DFS_USING_WORKDIR + /* open current working directory */ + path = rt_strdup("/"); + #endif + + /* build full path for each file */ + fullpath = dfs_normalize_path(path, file); + + rt_memset(&stat, 0, sizeof(struct stat)); + if (dfs_file_stat(fullpath, &stat) == 0) + { + return (unsigned long) stat.st_size; + } + else + { + return 0; + } +} + +void d_getFileSize(int argc, char ** argv) +{ +// char *f; + uint16_t size = (uint16_t)getFileSize(argv[1]); + LOG_D("size = %d Byte.",size); +} /** * 缓存任务数据 @@ -743,11 +859,11 @@ void parseTTData(uint8_t *din, size_t len) * @param len 任务数据长度 * @return 0--正常,-1--异常 */ -int cacheData(uint8_t *din, size_t len) +int cacheDataToFile(uint8_t *din, size_t len) { LOG_I("FUNC = cache and upsend"); - static int cnt = 0; - LOG_D("cached size=%d", cnt); + static uint16_t cnt = 0; + LOG_D("already cached %d bytes.", cnt); char rootDir[22] = "/sd/rxdata/"; mkdir(rootDir, 0); @@ -772,10 +888,10 @@ int cacheData(uint8_t *din, size_t len) strcat(f, rootDir); // strcpy(f,rootDir); strcat(f, "23_07_19_16_38_36_36.bin"); - LOG_D("need to creat new file"); +// LOG_D("need to creat new file"); strncpy(f + strlen(rootDir), ts, strlen(ts)); } - LOG_D("f=%s", f); +// LOG_D("f=%s", f); int fd = open(f, O_WRONLY | O_CREAT | O_APPEND); if (fd < 0) { @@ -793,6 +909,7 @@ int cacheData(uint8_t *din, size_t len) } cnt += len; close(fd); + LOG_I("cached %d bytes data to '%s'.",cnt,f); } return RT_EOK; @@ -802,7 +919,7 @@ void d_cacheData() uint8_t demo[200]; size_t len = sizeof(demo); memset(demo,0xAB,len); - cacheData(demo, len); + cacheDataToFile(demo, len); } @@ -819,7 +936,9 @@ void parseRS232(uint8_t *din, size_t len) //check if (memcmp(din,asciiHead,sizeof(asciiHead)) == 0) { //ascii + trDataTolog(din, len, 0); LOG_I("type = ASCII"); + uint8_t index[10]; uint8_t tmpHead[]={0x35, 0x41, 0x41, 0x35}; size_t n=isInByte(din, len, tmpHead, sizeof(tmpHead), index); @@ -847,6 +966,8 @@ void parseRS232(uint8_t *din, size_t len) else//如果不是ASCII则统一按HEX计// if (memcmp(din,hexHead,sizeof(hexHead)) == 0) { //hex + char tmp[200]=""; + trDataTolog(bytes2str(din, len, 16, " ", tmp), strlen(tmp), 0); LOG_I("type = HEX"); uint8_t index[10]; @@ -891,6 +1012,7 @@ MSH_CMD_EXPORT(selfTest,sysSelfTest); MSH_CMD_EXPORT(d_packLocMsg,dpackLocMsg); MSH_CMD_EXPORT(packAndSendLoc,packAndSendLoc); MSH_CMD_EXPORT(d_cacheData,d_cacheData); +MSH_CMD_EXPORT(d_getFileSize,d_getFileSize); #endif diff --git a/applications/func/func.h b/applications/func/func.h index feaff34..bb853ed 100644 --- a/applications/func/func.h +++ b/applications/func/func.h @@ -79,6 +79,7 @@ enum #define ADDR_TT 0x41 #define ADDR_3S 0x32 - +#define ROOT_PATH_LOG "/sd/log/" +#define ROOT_PATH_DATA "/sd/rxdata/" #endif /* APPLICATIONS_FUNC_FUNC_H_ */ diff --git a/applications/getinfo.c b/applications/getinfo.c index a2c7b84..3f7f950 100644 --- a/applications/getinfo.c +++ b/applications/getinfo.c @@ -389,12 +389,24 @@ void getTT(int argc, char **argv) /* 导出到自动初始化 */ MSH_CMD_EXPORT(getTT, 获取天通信息,支持参数。 "getTT 3 1" means try 3 times with 1 second interval.); //MSH_CMD_EXPORT_ALIAS(webclient_get_data, gTTinfo, GET info of TT server.); - +/** + * 判断TT是否为开机状态 + * @return 1-开机,0-关机 + */ int isTTon(void) { return !rt_pin_read(TT_EN); } +/** + * 判断TT是否激活 + * @return 1-已激活,0-未激活 + */ +int isTTjh(void) +{ + return atoi(TTinfo->jh); +} + void repGetTT_thread_entry(void* parameter) { while(!isTTon())//第一次运行时未上电则等待 diff --git a/applications/tools.c b/applications/tools.c index e38fb3e..f554517 100644 --- a/applications/tools.c +++ b/applications/tools.c @@ -79,7 +79,8 @@ void hexFile_thread_entry(void* parameter) rt_uint8_t *buffer = RT_NULL; #define READ_BUFFER_SIZE 512 buffer = (rt_uint8_t *) malloc(READ_BUFFER_SIZE); - rt_kprintf("------Start------\n"); + rt_kprintf("\n------Start------\n"); + LOG_D("size = %d bytes.",file_size); for (size_t i = 0; i < file_size; i += READ_BUFFER_SIZE) { if ((file_size - i) < READ_BUFFER_SIZE) @@ -153,7 +154,29 @@ char *time2Str(char *str) return str; } +char *date2Str(char *str) +{ + uint8_t t[10]; + size_t len=time2Byte(t); + bytes2str(t, len, 10, "_", str); +// len = strlen(str); + str[11]='\0'; + return str; +} +char *getTimestmp(char *str) +{ + uint8_t t[10]; + size_t len=time2Byte(t); + str[0]='\0'; + strcat(str,"20"); + bytes2str(t, len, 10, "-", str+2); + str[10]='_'; + len = strlen(str); + str[len]='\0'; + return str; + +} void tm2str(int argc, char **argv) @@ -161,6 +184,8 @@ void tm2str(int argc, char **argv) char s[30]; time2Str(s); LOG_I("result is %s",s); + getTimestmp(s); + LOG_I("result is %s",s); } MSH_CMD_EXPORT(tm2str,时间转换为字符串) diff --git a/applications/ttTR.c b/applications/ttTR.c index ce0ee77..6080c9e 100644 --- a/applications/ttTR.c +++ b/applications/ttTR.c @@ -282,6 +282,9 @@ int sendMsg(const rt_uint8_t *msg, size_t len) }; #endif + char str[400]; + bytes2str(msg, len, 16, " ", str); + trDataTolog(str, strlen(str), 1); return RT_EOK; } diff --git a/applications/uart_3S.c b/applications/uart_3S.c index f80d395..ec8d601 100644 --- a/applications/uart_3S.c +++ b/applications/uart_3S.c @@ -119,26 +119,6 @@ static void serial_thread_entry(void *parameter) LOG_I("%d Bytes received from RS232",rx_length); LOG_HEX("232rx:",16,rx_buffer,rx_length);//print what received. parseRS232(rx_buffer, rx_length); -#ifdef TR_ACK - /* 回传收到的消息 */ - rt_device_write(serial, 0, rx_buffer+rx_length, rx_length); -#endif -// if (0 && currLen > 1024) //大于1k则存盘 -// { -// -// int fd =open(f,O_WRONLY | O_CREAT|O_APPEND); -// if(fd <0) -// { -// LOG_E("open file %s failed!", f); -// } -// else -// { -// write(fd,rx_buffer,rx_length); -// close(fd); -// currLen = 0; -// rx_length=0; -// } -// } } } @@ -202,7 +182,7 @@ static int uart_dma_sample(int argc, char *argv[]) /* 创建 serial 线程 */ - rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024*2, 25+1, 10); + rt_thread_t thread = rt_thread_create("serial", serial_thread_entry, RT_NULL, 1024*5, 25+1, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) {