更新 #define ROOT_PATH "/sd/debuglog" //设置保存路径

alarmer.c 添加定时启动时的sem   timerIsReady();
cfg.c 更新 set_cfg逻辑 及 updateAllSysCfg
main.c 清理

updateAllSysCfg 待完善,有bug
This commit is contained in:
CSSC-WORK\murmur 2023-08-29 19:29:58 +08:00
parent 1c3826c35d
commit 8050a35073
10 changed files with 140 additions and 160 deletions

View File

@ -1,4 +1 @@
setCommWindow 概率性崩溃,疑似与写入配置有关 rtc 容易导致程序崩溃
setCommWindow 崩溃,疑似传参不对
写入log到文件采用独立线程避免错误

View File

@ -76,6 +76,7 @@ void pdTT_callback(rt_alarm_t alarm, time_t timestamp)
void poTT2_callback(rt_alarm_t alarm, time_t timestamp) void poTT2_callback(rt_alarm_t alarm, time_t timestamp)
{ {
LOG_I("power UP TT by INTERVAL RTC"); LOG_I("power UP TT by INTERVAL RTC");
timerIsReady();
initTT(); initTT();
} }
//每个开窗设置对应两个rtc闹钟一个上电一个下电 //每个开窗设置对应两个rtc闹钟一个上电一个下电
@ -85,7 +86,7 @@ static struct rt_alarm *alarm[4+1];//支持2组开窗共4个闹钟加一个
void InitAlarm() void InitAlarm()
{ {
struct rt_alarm_setup setup; struct rt_alarm_setup setup;
time_t now; static time_t now;
struct tm p_tm; struct tm p_tm;
/* 获取当前时间戳,并把下一秒时间设置为闹钟时间 */ /* 获取当前时间戳,并把下一秒时间设置为闹钟时间 */
now = time(NULL); now = time(NULL);
@ -184,7 +185,7 @@ void updateAlarm(uint8_t *t, size_t len)
// rt_alarm_delete(alarm[0]); // // rt_alarm_delete(alarm[0]); //
// } // }
// alarm[0] = rt_alarm_create(poTT_callback, &setup); // alarm[0] = rt_alarm_create(poTT_callback, &setup);
rt_alarm_control(alarm[0], RT_ALARM_CTRL_MODIFY, (void *)&setup); rt_alarm_control(alarm[0], RT_ALARM_CTRL_MODIFY, &setup);
setup.wktime.tm_hour = t[2]; //开窗1闭合点 setup.wktime.tm_hour = t[2]; //开窗1闭合点
setup.wktime.tm_min = t[3]; //开窗1开启点 setup.wktime.tm_min = t[3]; //开窗1开启点
@ -291,7 +292,7 @@ int isInWindowZone()
if (isIn1 || isIn2) if (isIn1 || isIn2)
{ {
LOG_D("is in window zone."); LOG_I("is in window zone.");
return 1; return 1;
} }
else { else {

View File

@ -54,35 +54,25 @@ typedef struct
*/ */
int set_cfg(const char *k, long v) int set_cfg(const char *k, long v)
{ {
// return; int rst = 0;
// setLock();
// if (rt_strcmp(v,"NULL") == 0) {//delete key
// v = NULL;
//
// }
// clearLock();
if (v == get_cfg(k)) { if (v == get_cfg(k)) {
LOG_D("nothing needs to change."); // LOG_D("nothing needs to change.");
// clearLock(); rst = 1;
return 1; }
else {
setLock();
// int rst = ini_puts("config",k,v,LJW_CFG_FILE_NAME);
rst = ini_putl("config", k, v, LJW_CFG_FILE_NAME);
clearLock();
} }
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) { if (rst == 1) {
// LOG_I("set value success.");
LOG_I("set value success.");
//以下消息队列通知各线程更新参数
// rt_sem_release(cfgUpdate);
} }
else { else {
LOG_E("set value fault."); LOG_E("set value fault.");
} }
clearLock();
return rst; return rst;
} }

View File

@ -32,9 +32,10 @@ static struct rt_event chkSta; //发送条件含两部分1-超时或文件
rt_sem_t TTReady= RT_NULL;//天通具备发送状态后 rt_sem_release(TTReady); rt_sem_t TTReady= RT_NULL;//天通具备发送状态后 rt_sem_release(TTReady);
rt_sem_t cfgUpdate = RT_NULL; rt_sem_t cfgUpdate = RT_NULL;//配置文件更新
rt_sem_t shuntDownTT = RT_NULL; rt_sem_t shuntDownTT = RT_NULL;//关闭TT
rt_sem_t okTosend = RT_NULL; rt_sem_t okTosend = RT_NULL;//可以发送后续文件
rt_sem_t okToreport = RT_NULL;//配置信息ACK
static rt_thread_t initThread=RT_NULL; static rt_thread_t initThread=RT_NULL;
static rt_thread_t deinitThread=RT_NULL; static rt_thread_t deinitThread=RT_NULL;
//void TTisReady(void) //void TTisReady(void)
@ -58,7 +59,7 @@ SYS_CFG scfg={
/** /**
* *
*/ */
static void updatecfg(void) static void initCfg(void)
{ {
//无配置文件则新建 //无配置文件则新建
@ -134,6 +135,9 @@ static void updatecfg(void)
} }
} }
else {
close(fd);
}
scfg.maxTTWaitTime = get_cfg("maxTTWaitTime"); scfg.maxTTWaitTime = get_cfg("maxTTWaitTime");
scfg.maxTTRetryCnt = get_cfg("maxTTRetryCnt"); scfg.maxTTRetryCnt = get_cfg("maxTTRetryCnt");
@ -150,41 +154,49 @@ static void updatecfg(void)
updateAlarm(scfg.openWindowTime, len); updateAlarm(scfg.openWindowTime, len);
LOG_D("cfg updated."); LOG_D("cfg updated.");
}
static void updatecfg(void)
{
//等待更新 //等待更新
while(1) while(1)
{ {
if(rt_sem_take(cfgUpdate, rt_tick_from_millisecond(10*1000)) == RT_EOK)//10s写入一次数据到文件 rt_thread_mdelay(10*1000);//10s写入一次数据到文件
if(rt_sem_take(cfgUpdate, RT_WAITING_NO) == RT_EOK)
{ {
size_t len= sizeof(scfg); size_t len= sizeof(scfg);
updateAllSysCfg(&scfg, len); uint8_t tmp[len];
memcpy(tmp,&scfg,len);
updateAllSysCfg(tmp, len);
} }
} }
} }
void sysSemInit() void sysSemInit()
{ {
okTosend = rt_sem_create("okTosend", 1, RT_IPC_FLAG_PRIO);//同一时间仅一个线程发送文件 okTosend = rt_sem_create("okTosend", 0, RT_IPC_FLAG_PRIO);//同一时间仅一个线程发送文件
cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO);//更新cfg cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_FIFO);//更新cfg
shuntDownTT = rt_sem_create("shuntDNTT", 0, RT_IPC_FLAG_PRIO);//关闭TT shuntDownTT = rt_sem_create("shuntDNTT", 0, RT_IPC_FLAG_PRIO);//关闭TT
okToreport = rt_sem_create("reportINFO", 0, RT_IPC_FLAG_FIFO);//关闭TT
rt_event_init(&chkSta, "chkSta", RT_IPC_FLAG_PRIO);//检查天通状态 rt_event_init(&chkSta, "chkSta", RT_IPC_FLAG_PRIO);//检查天通状态
// rt_sem_release(cfgUpdate); //上电更新值 // rt_sem_release(cfgUpdate); //上电更新值
updatecfg(); initCfg();
// void reportINFO();
reportINFO();
/* 创建 serial 线程 */ /* 创建 serial 线程 */
// rt_thread_t thread = rt_thread_create("updatecfg", updatecfg, RT_NULL, 1024 * 1, 27, 10); rt_thread_t thread = rt_thread_create("updatecfg", updatecfg, RT_NULL, 1024 * 1, 27, 10);
// /* 创建成功则启动线程 */ /* 创建成功则启动线程 */
// if (thread != RT_NULL) if (thread != RT_NULL)
// { {
// rt_thread_startup(thread); rt_thread_startup(thread);
// } }
// else else
// { {
// LOG_E("thread 'updatecfg' create failure."); LOG_E("thread 'updatecfg' create failure.");
// return; return;
// } }
// LOG_D("sysSemInit DONE."); // LOG_D("sysSemInit DONE.");
} }
@ -210,7 +222,10 @@ void sysEventInit(void)
void sysInit(void) void sysInit(void)
{ {
sysSemInit(); sysSemInit();
// sysEventInit(); if (isInWindowZone()) {//开机检查是否在开窗区间内是则给TT开机
initTT();
setWindowMode();
}
} }
//INIT_COMPONENT_EXPORT(sysInit); //INIT_COMPONENT_EXPORT(sysInit);
@ -287,8 +302,10 @@ static void upSendFile_thread_entry(void *parameter)
clearFileToSend(f->fname); clearFileToSend(f->fname);
// list_thread(); // list_thread();
rt_sem_release(okTosend);
} }
//当前文件处理完后置位
rt_sem_release(okTosend);
} }
/** /**
@ -343,7 +360,7 @@ void getAndSendFile()
return; return;
} }
// rt_sem_release(okTosend);//初始赋值 rt_sem_release(okTosend);//初始赋值
while(cnt) while(cnt)
{ {
int i=0; int i=0;
@ -584,7 +601,7 @@ void deInitTT_thread_entry()
// initThread = RT_NULL; // initThread = RT_NULL;
deinitThread = RT_NULL; deinitThread = RT_NULL;
list_thread(); // list_thread();
} }
/** /**

View File

@ -18,7 +18,17 @@
extern SYS_CFG scfg; extern SYS_CFG scfg;
extern int cryptSingleMsg(uint8_t *din, size_t len, uint8_t *dout); extern int cryptSingleMsg(uint8_t *din, size_t len, uint8_t *dout);
extern int pointInPolygon(int polyCorners,float polyX[], float polyY[],float x,float y); extern int pointInPolygon(int polyCorners,float polyX[], float polyY[],float x,float y);
void selfTest(void); extern rt_sem_t okToreport;
#define CFG_ACK
/**
* ack信号
*/
void ack(void)
{
#ifdef CFG_ACK
rt_sem_release(okToreport);
#endif
}
/** /**
* ASCII字符形式存入log * ASCII字符形式存入log
* @param din * @param din
@ -86,7 +96,7 @@ void setCommMode(int isTT)
scfg.commMode=isTT; scfg.commMode=isTT;
rt_sem_release(cfgUpdate); rt_sem_release(cfgUpdate);
LOG_D("set commMode to %s",isTT?"TT":"BD"); LOG_D("set commMode to %s",isTT?"TT":"BD");
selfTest(); ack();
} }
int getCommMode() int getCommMode()
@ -214,7 +224,7 @@ void upSend_thread_entry(void* parameter)
* @param len * @param len
* @return * @return
*/ */
RT_WEAK int upSend(uint8_t *din, size_t len) int upSend(uint8_t *din, size_t len)
{ {
LOG_D("try to upsend to TT."); LOG_D("try to upsend to TT.");
//return; //return;
@ -358,6 +368,7 @@ void setCommWindow(uint8_t *t, size_t len)
memcpy(scfg.openWindowTime,t,len); memcpy(scfg.openWindowTime,t,len);
rt_sem_release(cfgUpdate); rt_sem_release(cfgUpdate);
// LOG_D("updated new CommWindow zone"); // LOG_D("updated new CommWindow zone");
ack();
} }
@ -398,7 +409,7 @@ void openWindow(int t)
rt_timer_start(timer1); rt_timer_start(timer1);
LOG_D("手动开窗完成,%d分钟后自动关窗。",t); LOG_D("手动开窗完成,%d分钟后自动关窗。",t);
} }
selfTest(); ack();
} }
/** /**
@ -500,7 +511,7 @@ int reportDepth(uint8_t *din,size_t len)
void updateAllSysCfg(uint8_t *cfg, size_t len) void updateAllSysCfg(uint8_t *cfg, size_t len)
{ {
LOG_I("FUNC = update SYSCFG"); LOG_I("FUNC = update SYSCFG");
SYS_CFG ts; // SYS_CFG ts;
memcpy(&scfg,cfg,len); memcpy(&scfg,cfg,len);
//详见SYS_CFG的定义 //详见SYS_CFG的定义
/** /**
@ -519,22 +530,26 @@ void updateAllSysCfg(uint8_t *cfg, size_t len)
uint8_t locRepInterval; uint8_t locRepInterval;
*/ */
set_cfg("sendInterval",scfg.sendInterval); int rst=1;
set_cfg("maxTTWaitTime",scfg.maxTTWaitTime); rst &= set_cfg("sendInterval",scfg.sendInterval);
set_cfg("maxTTRetryCnt",scfg.maxTTRetryCnt); rst &= set_cfg("maxTTWaitTime",scfg.maxTTWaitTime);
set_cfg("minTTPeriCnt",scfg.minTTPeriCnt); rst &= set_cfg("maxTTRetryCnt",scfg.maxTTRetryCnt);
set_cfg("minTTsinal",scfg.minTTsinal); rst &= set_cfg("minTTPeriCnt",scfg.minTTPeriCnt);
set_cfg("timeout",scfg.timeout); rst &= set_cfg("minTTsinal",scfg.minTTsinal);
rst &= set_cfg("timeout",scfg.timeout);
char tmpstr[20]; char tmpstr[20];
bytes2str(scfg.openWindowTime, 8, 10, ",", tmpstr); bytes2str(scfg.openWindowTime, 8, 10, ",", tmpstr);
set_cfgs("openWindowTime",tmpstr); set_cfgs("openWindowTime",tmpstr);
set_cfg("maxSizePerFile", scfg.maxSizePerFile); rst &= set_cfg("maxSizePerFile", scfg.maxSizePerFile);
set_cfg("commMode",scfg.commMode); rst &= set_cfg("commMode",scfg.commMode);
set_cfg("selfDesSW",scfg.selfDesSW); rst &= set_cfg("selfDesSW",scfg.selfDesSW);
set_cfg("locAlert",scfg.locAlert); rst &= set_cfg("locAlert",scfg.locAlert);
set_cfg("locRepInterval",scfg.locRepInterval); rst &= set_cfg("locRepInterval",scfg.locRepInterval);
if (rst) {
LOG_I("set value success.");
}
} }
/** /**
* *
@ -646,24 +661,24 @@ int isInFence(uint8_t *loc);
* 10s更新一次 * 10s更新一次
* @return * @return
*/ */
static void reportLoc_thread_entry(void *parameter) void reportLoc_thread_entry(void *parameter)
{ {
#define CHECK_INTERVAL 6000 #define CHECK_INTERVAL 6000
size_t i = 0; static size_t i = 0;
while (isTCPok()) while (1 || isTCPok())
{ {
// static size_t i = 0;
int isReadyToSendLoc=0; int isReadyToSendLoc=0;
uint8_t rst[200]; uint8_t rst[200];
int len = packLocMsg(rst); int len = packLocMsg(rst);
if (!len) {//无有效位置数据 if (!len) {//无有效位置数据
// return; // return;
rt_thread_mdelay(500);//添加延时避免死循环卡死线程
continue; continue;
} }
//检测是否在围栏内 //检测是否在围栏内
if (!isInFence(rst+len-11))//不在围栏内 if (len && !isInFence(rst+len-11))//不在围栏内
{ {
rst[8]=1; rst[8]=1;
isReadyToSendLoc = 1; isReadyToSendLoc = 1;
@ -680,58 +695,13 @@ static void reportLoc_thread_entry(void *parameter)
if (isReadyToSendLoc) { if (isReadyToSendLoc) {
upSend(rst, len); upSend(rst, len);
} }
//LOG_D("i=%d s",i*CHECK_INTERVAL/1000);
} }
} }
//创建定时任务默认时间5分钟采集发送
/**
*
*/
//static void upSendLoc_thread_entry(void *parameter)
//{
// while(1)
// {
// if (rt_sem_take(isReadyToSendLoc, RT_WAITING_FOREVER)) {
// uint8_t rst[200];
// size_t len = packAndChkLoc(rst);
// upSend(rst, len);
// }
// }
//}
//cb_upSendLoc()
//{
// if (isTCPok()) {
//// packAndSendLoc();
// rt_sem_release(isReadyToSendLoc);
// }
// else {
// rt_timer_delete(repLoc);
// }
//}
void reportLoc() void reportLoc()
{ {
// isReadyToSendLoc = rt_sem_create("SendLoc", 0, RT_IPC_FLAG_PRIO); rt_thread_t thread = rt_thread_create("RPLoc", reportLoc_thread_entry, RT_NULL, 1024 * 5, 27, 10);
// repLoc = rt_timer_create("repLoc", cb_upSendLoc,
// RT_NULL, 10,
// RT_TIMER_FLAG_PERIODIC);
/* 创建 serial 线程 */
// rt_thread_t thread = rt_thread_create("sendLoc", upSendLoc_thread_entry, RT_NULL, 1024 * 1, 27, 10);
// /* 创建成功则启动线程 */
// if (thread != RT_NULL)
// {
// rt_thread_startup(thread);
// }
// else
// {
// LOG_E("thread 'sendLoc' create failure.");
// return;
// }
rt_thread_t thread = rt_thread_create("RPLoc", reportLoc_thread_entry, RT_NULL, 1024 * 5, 28, 10);
/* 创建成功则启动线程 */ /* 创建成功则启动线程 */
if (thread != RT_NULL) if (thread != RT_NULL)
{ {
@ -764,9 +734,6 @@ int depthAlert(uint8_t *din, int len)
// rt_memcpy(dout+10, din, len);//5aa5从第11字节开始共len个字节 // rt_memcpy(dout+10, din, len);//5aa5从第11字节开始共len个字节
// LOG_HEX("depth",16,dout,len+10); // LOG_HEX("depth",16,dout,len+10);
// //加密。因加密后数据长度会变化,故不能只加密位置数据。 // //加密。因加密后数据长度会变化,故不能只加密位置数据。
// nlen = cryptSingleMsg(din, len+10, dout);
// LOG_HEX("crypt",16,dout,nlen);
// LOG_D("位置数据加密完成");
// upSend(dout, nlen); // upSend(dout, nlen);
} }
@ -908,11 +875,6 @@ void ttRunCMD(uint8_t *din, size_t len)
break; break;
case _CFG_COMM_WINDOW: case _CFG_COMM_WINDOW:
setCommWindow(din+8,din[7]);//只支持两组开窗开窗时间为小时、分钟UTC+0 setCommWindow(din+8,din[7]);//只支持两组开窗开窗时间为小时、分钟UTC+0
// char tmpstr[20];
// bytes2str(din+8,din[7], 10, ",", tmpstr);
// set_cfgs("openWindowTime",tmpstr);
break; break;
case _CMD_OPEN_WINDOW: case _CMD_OPEN_WINDOW:
openWindow(din[7]?((din[8] << 8) + din[9]):0);//两字节开窗时间 openWindow(din[7]?((din[8] << 8) + din[9]):0);//两字节开窗时间
@ -1521,7 +1483,35 @@ int isEthUP()
} }
void reportInfo_thread(void)
{
while (1)
{
if (rt_sem_take(okToreport, RT_WAITING_FOREVER) == RT_EOK)
{
LOG_D("send cfg ACK");
selfTest();
}
}
}
void reportINFO(void)
{
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("reportINFO", reportInfo_thread, RT_NULL, 1024 * 1, 27, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'updatecfg' create failure.");
return;
}
}
//INIT_APP_EXPORT(reportINFO);
#define FUNC_DEMO #define FUNC_DEMO

View File

@ -201,9 +201,9 @@ static void rulecheck(void)
} }
if (okCnt >= scfg.minTTPeriCnt) { if (okCnt >= scfg.minTTPeriCnt) {
//TT具备发送条件 //TT具备发送条件
LOG_I("----"); // LOG_I("----");
TTIsReady(); TTIsReady();
LOG_I("符合规则TT具备发送状态。"); // LOG_I("符合规则TT具备发送状态。");
okCnt = scfg.minTTPeriCnt -1; okCnt = scfg.minTTPeriCnt -1;
} }
// if (okCnt > minTTPeriCnt) { // if (okCnt > minTTPeriCnt) {

View File

@ -10,7 +10,7 @@
#include <..\rt-thread\components\utilities\ulog\backend\ulog_be.h> #include <..\rt-thread\components\utilities\ulog\backend\ulog_be.h>
#include "usrcfg.h"
/* /*
* *
*/ */
@ -34,7 +34,7 @@ typedef enum
motion_id, motion_id,
}ulog_file_be_name; }ulog_file_be_name;
#define ROOT_PATH "/sd/debuglog" //设置保存路径 #define ROOT_PATH ROOT_PATH_DEBUG //设置保存路径
#define FILE_SIZE 10*1024 //设置单个文件大小 #define FILE_SIZE 10*1024 //设置单个文件大小
#define BUFF_SIZE 1*1024 //设备缓存区大小 #define BUFF_SIZE 1*1024 //设备缓存区大小

View File

@ -24,7 +24,7 @@
// LOG_D("key irq callback"); // LOG_D("key irq callback");
//} //}
//extern void sysInit(void);
int main(void) int main(void)
{ {
@ -46,14 +46,14 @@ int main(void)
rt_pin_mode(TR485_RE, PIN_MODE_OUTPUT); rt_pin_mode(TR485_RE, PIN_MODE_OUTPUT);
rt_pin_write(TR485_RE, PIN_LOW); rt_pin_write(TR485_RE, PIN_LOW);
//sysSemInit
sysInit(); sysInit();
// sysEventInit(); // sysEventInit();
if (isInWindowZone()) {//开机检查是否在开窗区间内是则给TT开机 // if (isInWindowZone()) {//开机检查是否在开窗区间内是则给TT开机
initTT(); // initTT();
setWindowMode(); // setWindowMode();
// pwTT_thread_entry("1"); //// pwTT_thread_entry("1");
} // }
@ -78,22 +78,6 @@ int main(void)
//fastlz_test -c demo.bin f.bin //fastlz_test -c demo.bin f.bin
//
//RT_WEAK void initTT()
//{
//
//}
//
//RT_WEAK void deInitTT()
//{
//
//}
INIT_APP_EXPORT(main);
extern int rt_hw_stm32_eth_init(void); extern int rt_hw_stm32_eth_init(void);
MSH_CMD_EXPORT(rt_hw_stm32_eth_init, ); MSH_CMD_EXPORT(rt_hw_stm32_eth_init, );

View File

@ -158,7 +158,7 @@ static int uart_dma_sample(int argc, char *argv[])
#endif #endif
/* 创建 serial 线程 */ /* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("RS232", serial_thread_entry, RT_NULL, 1024*10, 25+1, 10); rt_thread_t thread = rt_thread_create("RS232", serial_thread_entry, RT_NULL, 1024*10, 25-1, 10);
/* 创建成功则启动线程 */ /* 创建成功则启动线程 */
if (thread != RT_NULL) if (thread != RT_NULL)
{ {

View File

@ -56,8 +56,9 @@ typedef struct
//struct rt_event sw_check;//软件条件 //struct rt_event sw_check;//软件条件
#define ROOT_PATH_LOG "/sd/log/" #define ROOT_PATH_LOG "/sd/log/" //收发数据
#define ROOT_PATH_DATA "/sd/rxdata/" #define ROOT_PATH_DATA "/sd/cdata/" //缓存数据
#define ROOT_PATH_DEBUG "/sd/debug/" //调试日志
#define CRYPT_BEFRE_PACK #define CRYPT_BEFRE_PACK
#define UPDATE_INTERVAL 10 #define UPDATE_INTERVAL 10