diff --git a/applications/alarmer.c b/applications/alarmer.c index 1bcf053..032a108 100644 --- a/applications/alarmer.c +++ b/applications/alarmer.c @@ -73,7 +73,7 @@ void pdTT_callback(rt_alarm_t alarm, time_t timestamp) //每个开窗设置对应两个rtc闹钟,一个上电一个下电 //更新开窗设置前需要先清空闹钟设置。可改为调用rt_alarm_control()修改。 static struct rt_alarm *alarm[4];//支持2组开窗共4个闹钟 - +#ifndef al void InitAlarm() { struct rt_alarm_setup setup; @@ -130,6 +130,13 @@ void InitAlarm() } } } +#else +void updateAlarm(uint8_t *t, size_t len); +void InitAlarm() +{ + updateAlarm(scfg.openWindowTime, 8); +} +#endif INIT_APP_EXPORT(InitAlarm); /** * 更新rtc闹钟 @@ -170,8 +177,9 @@ void updateAlarm(uint8_t *t, size_t len) rt_alarm_delete(alarm[1]); // } alarm[1] = rt_alarm_create(pdTT_callback, &setup); + setup.wktime.tm_hour = t[4]; //开窗2开启点 - setup.wktime.tm_min = t[5]; //开窗1开启点 + setup.wktime.tm_min = t[5]; //开窗2开启点 if (RT_NULL != alarm[2]) { rt_alarm_delete(alarm[2]); // @@ -179,7 +187,7 @@ void updateAlarm(uint8_t *t, size_t len) alarm[2] = rt_alarm_create(poTT_callback, &setup); setup.wktime.tm_hour = t[6]; //开窗2闭合点 - setup.wktime.tm_min = t[7]; //开窗1开启点 + setup.wktime.tm_min = t[7]; //开窗2开启点 if (RT_NULL != alarm[3]) { rt_alarm_delete(alarm[3]); // diff --git a/applications/cfg.c b/applications/cfg.c index a276291..8d2624c 100644 --- a/applications/cfg.c +++ b/applications/cfg.c @@ -27,6 +27,7 @@ static void setLock() while(islock) { rt_thread_mdelay(1000); +// LOG_W("file locked"); } islock=1; } @@ -82,14 +83,14 @@ int set_cfg(const char *k, long v) { // setLock(); - if (rt_strcmp(v,"NULL") == 0) {//delete key - v = NULL; - - } +// if (rt_strcmp(v,"NULL") == 0) {//delete key +// v = NULL; +// +// } // clearLock(); if (v == get_cfg(k)) { LOG_D("nothing needs to change."); - clearLock(); +// clearLock(); return 1; } setLock(); @@ -113,12 +114,20 @@ int set_cfg(const char *k, long v) int set_cfgs(const char *k, const char * v) { - setLock(); +// setLock(); - if (rt_strcmp(v,"NULL") == 0) {//delete key + if (strcmp(v,"NULL") == 0) {//delete key v = NULL; } + char tmp[20]; + get_cfgs(k,tmp); + if (strcmp(v,tmp) == 0) { + LOG_D("nothing needs to change."); +// clearLock(); + return 1; + } + setLock(); int rst = ini_puts("config",k,v,LJW_CFG_FILE_NAME); // int rst = ini_putl("config", k, v, LJW_CFG_FILE_NAME); if (rst == 1) { @@ -162,7 +171,7 @@ int get_cfg(const char *k) return rst; } -int get_cfgs(const char *k) +int get_cfgs(const char *k, char *str) { setLock(); char buf[MAX_KEY_LEN]; @@ -178,6 +187,7 @@ int get_cfgs(const char *k) else { // LOG_I("%s = %s",k,buf); // LOG_I("%s = %d",k,rst); + strcpy(str,buf); } clearLock(); return rst; @@ -346,6 +356,7 @@ int setFileToSend(const char *f, int v) nclearLock(); return RT_ERROR; } + LOG_D("add %s-%d to list.",f,v); nclearLock(); return RT_EOK; } @@ -355,7 +366,7 @@ int setFileToSend(const char *f, int v) * @param v 对应包文件的索引,默认为0即全发 * @return 待发文件个数 */ -int getFilesToSend(char (*kstr)[MAX_KEY_LEN], int *v) +size_t getFilesToSend(char (*kstr)[MAX_KEY_LEN], int *v) { // char buf[MAX_KEY_LEN]; // char kstr[MAX_KEY_LEN]; @@ -364,6 +375,9 @@ int getFilesToSend(char (*kstr)[MAX_KEY_LEN], int *v) for (size_t k = 0; ini_getkey(SECTION_TO_SEND, k, kstr[len], MAX_KEY_LEN, FILE_TO_SEND) > 0; k++) { v[len] = ini_getl(SECTION_TO_SEND, kstr[len], -1, FILE_TO_SEND); len +=1; + if (len>5) { + break; + } } nclearLock(); return len; @@ -386,13 +400,21 @@ int clearFileToSend(const char *k) static void gf() { int v[MAX_KEY_LEN]; - char kstr[30][MAX_KEY_LEN]; + char kstr[10][MAX_KEY_LEN]; size_t cnt = getFilesToSend(kstr, v); - for (size_t var = 0; var < cnt; var++) + if (!cnt) { + LOG_D("no files waiting to be sent"); + } + else { - LOG_I("%s -- %d", kstr[var], v[var]); + for (size_t var = 0; var < cnt; var++) + { + LOG_I("%s -- %d", kstr[var], v[var]); + } +// cnt = getFilesToSend(kstr, v); } } + static void add(int argc, char **argv) { setFileToSend(argv[1],atoi(argv[2])); diff --git a/applications/cfg.h b/applications/cfg.h index 5883204..85ebbc7 100644 --- a/applications/cfg.h +++ b/applications/cfg.h @@ -42,5 +42,5 @@ long get_val(const char *k); int set_val(const char *k, long v); int setFileToSend(const char *f, int v); -int getFilesToSend(char (*kstr)[MAX_KEY_LEN], int *v); +size_t getFilesToSend(char (*kstr)[MAX_KEY_LEN], int *v); int clearFileToSend(const char *k); diff --git a/applications/_main.c b/applications/core.c similarity index 80% rename from applications/_main.c rename to applications/core.c index eacc69f..96c8402 100644 --- a/applications/_main.c +++ b/applications/core.c @@ -11,7 +11,7 @@ #include -#define LOG_TAG "main_scha" +#define LOG_TAG "core" #define LOG_LVL LOG_LVL_DBG #include #include @@ -34,7 +34,7 @@ static struct rt_event chkSta; //发送条件,含两部分,1-超时或文件 rt_sem_t TTReady= RT_NULL;//天通具备发送状态后 rt_sem_release(TTReady); rt_sem_t cfgUpdate = RT_NULL; rt_sem_t shuntDownTT = RT_NULL; - +rt_sem_t okTosend = RT_NULL; //void TTisReady(void) //{ @@ -48,7 +48,8 @@ SYS_CFG scfg={ .minTTPeriCnt=5, .minTTsinal=5, .timeout=5, - .maxSizePerFile=1024 + .maxSizePerFile=1024, +// .openWindowTime[]={0x02, 0x0F, 0x03, 0x1E, 0x08, 0x0F, 0x09, 0x1E} }; @@ -61,6 +62,7 @@ static void updatecfg(void) //因为不知原因,采用事件集独立更新配置出错,无精力深查 //独立响应单个参数更新事件,程序上更复杂也没特别必要 //现采用事件通知、统一全部重新加载 +// memcpy(scfg.openWindowTime, while(1) { if(rt_sem_take(cfgUpdate, RT_WAITING_FOREVER) == RT_EOK) @@ -71,6 +73,12 @@ static void updatecfg(void) scfg.minTTsinal = get_cfg("minTTsinal"); scfg.timeout = get_cfg("timeout"); scfg.maxSizePerFile = get_cfg("maxSizePerFile"); + + char str[20]; + get_cfgs("openWindowTime",str); + size_t len = str2Byte(str, 3, 10, scfg.openWindowTime); +//LOG_HEX("win",16,scfg.openWindowTime,8); + updateAlarm(scfg.openWindowTime, len); } LOG_D("cfg updated."); } @@ -79,13 +87,27 @@ static void updatecfg(void) void sysSemInit() { -// TTReady = rt_sem_create("TTisReady", 0, RT_IPC_FLAG_PRIO); + okTosend = rt_sem_create("okTosend", 0, RT_IPC_FLAG_PRIO); cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO); shuntDownTT = rt_sem_create("shuntDNTT", 0, RT_IPC_FLAG_PRIO); rt_event_init(&chkSta, "chkSta", RT_IPC_FLAG_PRIO); rt_sem_release(cfgUpdate); //上电更新值 + updatecfg(); + /* 创建 serial 线程 */ +// rt_thread_t thread = rt_thread_create("updatecfg", updatecfg, RT_NULL, 1024 * 1, 27, 10); +// /* 创建成功则启动线程 */ +// if (thread != RT_NULL) +// { +// rt_thread_startup(thread); +// } +// else +// { +// LOG_E("thread 'updatecfg' create failure."); +// return; +// } + // LOG_D("sysSemInit DONE."); } @@ -158,25 +180,35 @@ static void upSendFile_thread_entry(void *parameter) static rt_uint8_t d[BUFFER_ROW][200] = { }; static rt_uint8_t s[BUFFER_ROW] = { }; - LOG_D("%s---%d",f->fname,f->index); + if (getFileSize(f->fname) > scfg.maxSizePerFile+200) { + LOG_W("file '%s' is too large to send.",f->fname); + clearFileToSend(f->fname); + rt_sem_release(okTosend); + return; + } + LOG_D("ready to send '%s---%d'",f->fname,f->index); rt_uint8_t len = pack_File(f->fname, 0, d, s); // for (size_t i = 0; i < len; i++) { // LOG_HEX("rst",16,d[i],s[i]); // } // list_thread(); - if (len) + if (len)//部分demo数据体积>>1k { LOG_D("%d pack(s) to send", f->index ? 1 : len); - for (size_t var = 0; var < len; var++) + for (rt_uint8_t var = 0; var < len; var++) { if (!f->index || (var+1) == f->index) { //index=0 全发,或者仅发index if (sendMsg(d[var], s[var]) == RT_EOK) { LOG_D("send pack[%d] done.",var+1); }; + rt_thread_mdelay(1000); } } LOG_I("upSendFile done."); + clearFileToSend(f->fname); +// list_thread(); + rt_sem_release(okTosend); } } @@ -194,7 +226,7 @@ void upSendFile(const char *f, int index) info.index = index; /* 创建 serial 线程 */ - rt_thread_t thread = rt_thread_create("upSendFile", upSendFile_thread_entry, (void *) &info, 1024 * 2, 27, 10); + rt_thread_t thread = rt_thread_create("SendFile", upSendFile_thread_entry, (void *) &info, 1024 * 3, 27, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { @@ -220,21 +252,35 @@ void d_upSendFile(int argc, char **argv) */ void getAndSendFile() { - int index[MAX_KEY_LEN]; - char f[30][MAX_KEY_LEN]; - size_t cnt = getFilesToSend(f, index); - - LOG_I("%d files to be send"); - - for (size_t i = cnt-1; i > 0; i--) + static int index[MAX_KEY_LEN]; + static char f[10][MAX_KEY_LEN]; + int cnt = getFilesToSend(f, index); + if(cnt) { - upSendFile(f[i],index[i]); + LOG_I("ready to send files.[%d]",cnt); + } + else { + LOG_W("no files waiting to be sent."); + return; + } + + rt_sem_release(okTosend); + while(cnt) + { + for (int i = cnt-1; i > -1; i--) + { + if (rt_sem_take(okTosend, RT_WAITING_FOREVER) == RT_EOK) { + rt_thread_mdelay(1000); + upSendFile(f[i],index[i]); + } + } + cnt = getFilesToSend(f, index); } } static void chkAndSendFile_thread_entry() { - while (1 && isTTon()) + while (1) { // int e; LOG_I("等待文件就绪(或定时%d分钟超时)",scfg.sendInterval); @@ -242,6 +288,7 @@ static void chkAndSendFile_thread_entry() RT_WAITING_FOREVER, RT_NULL) == RT_EOK) //条件1满足 { LOG_I("等待TT就绪"); + initTT(); } for (size_t var = 0; var < scfg.maxTTRetryCnt; var++) //轮询尝试 @@ -249,7 +296,7 @@ static void chkAndSendFile_thread_entry() LOG_I("第%d/%d次尝试。", var + 1,scfg.maxTTRetryCnt); int rst = rt_event_recv(&chkSta, TT_IS_OK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, rt_tick_from_millisecond(scfg.maxTTWaitTime*60*1000), RT_NULL); -// rst = rt_sem_take(TTReady, rt_tick_from_millisecond(maxTTWaitTime * 60 * 1000)); + // rst = rt_sem_take(TTReady, rt_tick_from_millisecond(maxTTWaitTime * 60 * 1000)); if (rst == RT_EOK) //条件2满足 { LOG_I("TT准备就绪"); @@ -259,23 +306,12 @@ static void chkAndSendFile_thread_entry() else if (rst == -RT_ETIMEOUT)//超时则重试 { LOG_W("第%d次尝试中TT准备失败。", var + 1); -// pwTT_thread_entry("0"); rt_thread_mdelay(1000); -// checkTT(); -// continue; + if ((var+1) == scfg.maxTTRetryCnt) { + LOG_E("TT准备失败"); + } } } - LOG_E("TT失败"); - -//continue; -//发送数据 -//可能有多个待发文件,每个文件打包后为一个二维数组 -//如果先打包后发送,需要三维数组,和大内存占用 -//故改为“打包-发送”轮询操作 -// upSendFile(); - -// LOG_I("----------------------------"); - } } @@ -286,7 +322,7 @@ static void chkAndSendFile_thread_entry() void chkAndSendFile() { /* 创建 serial 线程 */ - rt_thread_t thread = rt_thread_create("chk&send", chkAndSendFile_thread_entry, RT_NULL, 1024 * 5, 24, 10); + rt_thread_t thread = rt_thread_create("chk&send", chkAndSendFile_thread_entry, RT_NULL, 1024 * 2, 19, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { @@ -298,7 +334,7 @@ void chkAndSendFile() return; } } -//INIT_COMPONENT_EXPORT(smsg); +INIT_APP_EXPORT(chkAndSendFile); static rt_timer_t tmrToPNTT=RT_NULL; static int isInWindow=0;//winow 关闭时复位 @@ -388,7 +424,7 @@ void initTT_thread_entry() LOG_D("TCP is ready."); tcpRecMQ(); //开启tcp接收线程 recTT(); - chkAndSendFile(); +// chkAndSendFile(); } } @@ -456,7 +492,7 @@ void deInitTT() #define FUNC_DEMO #ifdef FUNC_DEMO //测试时导出命令到控制台 -MSH_CMD_EXPORT(d_upSta,FILE_IS_OK); +MSH_CMD_EXPORT_ALIAS(d_upSta,ttisok,TT_IS_OK); MSH_CMD_EXPORT(fileIsReady,fileIsReady); MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile); MSH_CMD_EXPORT(upSendFile, upSendFile); diff --git a/applications/func/func.c b/applications/func/func.c index 3b88089..b9d5d0e 100644 --- a/applications/func/func.c +++ b/applications/func/func.c @@ -315,8 +315,16 @@ extern void updateAlarm(uint8_t *t, size_t len); void setCommWindow(uint8_t *t, size_t len) { LOG_I("FUNC = setCommWindow"); + + +//not working here +// char tmpstr[20]; +// bytes2str(t,len, 10, ",", tmpstr); +// set_cfgs("openWindowTime",tmpstr); + updateAlarm(t,len); - LOG_D("更新开窗时间完成。"); +// LOG_D("updated new CommWindow zone"); + } /** * 手动控制开窗 @@ -427,19 +435,15 @@ int reportDepth(uint8_t *din,size_t len) } //天通指令类型:0x70 0x01 /** - * 更新系统各项工作参数 + * 从数组更新系统各项工作参数 * @param cfg 参数数组 * @param len 数组长度 */ -void updateSysCfg(uint8_t *cfg, size_t len) +void updateAllSysCfg(uint8_t *cfg, size_t len) { LOG_I("FUNC = update SYSCFG"); SYS_CFG ts; memcpy(&scfg,cfg,len); -// LOG_D("--%d",scfg.maxSizePerFile); -// LOG_HEX("--",16,scfg.openWindowTime,8); -// return; -// scfg = (SYS_CFG)tmp; /** * uint8_t sendInterval; @@ -505,7 +509,7 @@ void reportSysCfg() // LOG_HEX("scfg",16,&scfg,size); // LOG_HEX("cfg",16,rst,p+1); -// updateSysCfg(rst+8, rst[7]); + updateAllSysCfg(rst+8, rst[7]); } //3.2.8定时自报位置信息 @@ -795,6 +799,11 @@ void ttRunCMD(uint8_t *din, size_t len) break; case _CFG_COMM_WINDOW: setCommWindow(din+8,din[7]);//只支持两组开窗,开窗时间为小时、分钟,UTC+0 + + char tmpstr[20]; + bytes2str(din+8,din[7], 10, ",", tmpstr); + set_cfgs("openWindowTime",tmpstr); + break; case _CMD_OPEN_WINDOW: openWindow(din[7]?((din[8] << 8) + din[9]):0);//两字节开窗时间 @@ -810,7 +819,7 @@ void ttRunCMD(uint8_t *din, size_t len) break; case _CFG_UPDATE_CFG: if (din[7]) {//数据长度不为0则为覆写参数 - updateSysCfg(din+8, din[7]); + updateAllSysCfg(din+8, din[7]); } else {//数据长度为0则为请求参数 reportSysCfg(); @@ -894,13 +903,13 @@ void parse3SData(uint8_t *din, size_t count) { ttRunCMD(din,count); } - if (din[2] == ADDR_3S)//给3S的指令,需要再加工,返回数据可能也需要再加工 + else if (din[2] == ADDR_3S)//给3S的指令,需要再加工,返回数据可能也需要再加工 { // formatAndSendTo3S(din,count); } - if (din[2] == ADDR_ANJI) + else if (din[2] == ADDR_ANJI) { //可能需要对回传信息再加工,如查询深度需要加入位置坐标 //或是缓存任务数据 @@ -912,6 +921,7 @@ void parse3SData(uint8_t *din, size_t count) reportDepth(din, count); break; default: + LOG_I("pass-through raw data."); if (isTTon()) {//如果TT为开机状态(具备通信状态?待商榷)则不缓存直接发 upSend(din, count); @@ -923,7 +933,10 @@ void parse3SData(uint8_t *din, size_t count) } // upSend(din, count); } - LOG_W("无法解析的目标地址"); + else { + LOG_W("无法解析的目标地址"); + } + } void chkACK(uint8_t *msg, size_t size) diff --git a/applications/ttTR.c b/applications/ttTR.c index 8cb87fe..913c802 100644 --- a/applications/ttTR.c +++ b/applications/ttTR.c @@ -284,6 +284,7 @@ int sendMsg(const rt_uint8_t *msg, size_t len) }; #endif + return RT_EOK; //已发送数据存入log文件 char str[400]; bytes2str(msg, len, 16, " ", str); diff --git a/applications/ttmsg/ttmsg.c b/applications/ttmsg/ttmsg.c index f656009..a236687 100644 --- a/applications/ttmsg/ttmsg.c +++ b/applications/ttmsg/ttmsg.c @@ -124,7 +124,7 @@ static size_t getID(const char *str, rt_uint8_t *out) } // LOG_HEX("file ID",16,tmp,len); char idstr[30]; - LOG_I("msg id:%s",bytes2str(tmp, len, 16, "_", idstr)); + LOG_I("msg id:%s",bytes2str(tmp, len, 10, "_", idstr)); memcpy(out, tmp, len); return len; } diff --git a/applications/uart_3S.c b/applications/uart_3S.c index e0b4d48..1e55a28 100644 --- a/applications/uart_3S.c +++ b/applications/uart_3S.c @@ -80,7 +80,7 @@ static void serial_thread_entry(void *parameter) // struct rx_msg msg; rt_err_t result; rt_uint32_t rx_length=0;//单次收到的数据长度 - static char rx_buffer[RT_SERIAL_RB_BUFSZ + 1]; + static char rx_buffer[RT_SERIAL_RB_BUFSZ/4 + 1]; while (1) {