修复定时发送位置时间错乱的bug

修复event不能挂入链表的bug
TODO
rtc 容易导致程序崩溃(网口配置、关机复位)
alarmer 有时候start失败
This commit is contained in:
CSSC-WORK\murmur 2023-08-30 18:00:42 +08:00
parent 348f2688e2
commit b3f1856660
5 changed files with 150 additions and 115 deletions

View File

@ -1 +1,2 @@
rtc 容易导致程序崩溃
rtc 容易导致程序崩溃(网口配置、关机复位)
alarmer 有时候start失败

View File

@ -193,7 +193,7 @@ static void get_sta_all(void)
LOG_I("%16s = %s",kstr,buf);
}
}
MSH_CMD_EXPORT(cfg, config params. )
//以下针对stats
/**
@ -274,7 +274,7 @@ static void sta(int argc, char ** argv)
}
}
MSH_CMD_EXPORT(sta, )
static void clear_sta(void)
{
@ -461,6 +461,7 @@ char tmp[60];
MSH_CMD_EXPORT(gf, )
MSH_CMD_EXPORT_ALIAS(add, cf,)
MSH_CMD_EXPORT(sta, )
MSH_CMD_EXPORT(cfg, config params. )
//set_if()
#endif

View File

@ -38,6 +38,8 @@ rt_sem_t okTosend = RT_NULL;//可以发送后续文件
rt_sem_t okToreport = RT_NULL;//配置信息ACK
static rt_thread_t initThread=RT_NULL;
static rt_thread_t deinitThread=RT_NULL;
static rt_timer_t tmrToPNTT=RT_NULL;
static uint8_t isWindowMode=0;//自动或手动引起TT开机
//void TTisReady(void)
//{
// rt_sem_release(TTReady);
@ -91,7 +93,7 @@ static void initCfg(void)
"# 加密方式0-不加密\n"
"encrytType=1\n"
"# 开窗时间UTC+8,持续时间H。以【,】为分隔符\n"
"openWindowTime=1,0,2,0,7,0,8,0\n"
"openWindowTime=02,15,03,30,08,15,09,30\n"
"#超时时间M主动开机上报数据情况下最后一次通信后开始计时超时后关闭TT\n"
"timeout=5\n"
"# 工作模式1-TT2-BD\n"
@ -219,7 +221,7 @@ void sysEventInit(void)
void sysInit(void)
{
sysSemInit();
sysEventInit();
// sysEventInit();
if (isInWindowZone()) {//开机检查是否在开窗区间内是则给TT开机
initTT();
setWindowMode();
@ -242,9 +244,97 @@ void TTIsReady(void)
rt_event_send(&chkSta, TT_IS_OK);
stopTM();
}
void d_upSta()
/**
* TT
*/
void stopTM()
{
TTIsReady();
if (!tmrToPNTT) {
return;
}
rt_timer_stop(tmrToPNTT);
rt_timer_stop(tmrToPNTT);
}
/**
* flag以关闭超时机制
*/
void setWindowMode(void)
{
isWindowMode = 1;
stopTM();
}
/**
*
*/
void clearWindowMode(void)
{
isWindowMode = 0;
stopTM();
}
/**
* TT
* TT无接收数据则启动关机倒计时
*/
void resetTM()
{
// LOG_D("try to reset");
if (tmrToPNTT == RT_NULL) {
return;
}
if (isWindowMode) {
stopTM();
LOG_I("TIMEOUT stopped.");
return;
}
rt_tick_t t= rt_tick_from_millisecond(scfg.timeout*60*1000);
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_SET_TIME,(void*) &t);
rt_timer_stop(tmrToPNTT);
int i=0;
while(rt_timer_start(tmrToPNTT) != RT_EOK)
{
rt_thread_mdelay(200);
if (i++ > 10) {
LOG_E("TIMEOUT fault.");
break;
}
}
LOG_D("%d minutes from now to power down TT.",scfg.timeout);
}
void d_remain()
{
if (tmrToPNTT == RT_NULL) {
LOG_I("tmrToPNTT is NULL");
return;
}
rt_tick_t arg1,arg2;
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_TIME, (void*)&arg1);
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_REMAIN_TIME, (void*)&arg2);
int tmp=(arg2-rt_tick_get())/1000;
LOG_D("t=%ld,0x%X -> 0x%X",tmp,rt_tick_get(),arg2);
if (tmp>0) {
tmp=0;
}
LOG_D("%dm%ds / %d min",tmp/60,tmp%60,arg1/60000);
rt_uint32_t arg3=0;
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_STATE, (void*)&arg3);
LOG_D("%s",arg3?"YES":"NO");
}
/* 定时器超时函数 */
static void timeoutFunc(void *parameter)
{
LOG_W("time to shunt down TT");
// rt_thread_mdelay(3000);// no delay
deInitTT();//Function[rt_mutex_take] shall not be used in ISR
}
@ -355,6 +445,7 @@ void getAndSendFile()
}
else {
LOG_W("no files waiting to be sent.");
resetTM();//启动超时
return;
}
@ -385,23 +476,14 @@ static void chkAndSendFile_thread_entry()
{
while (1)
{
int sta=0;
// LOG_I("等待发送条件(文件就绪或定时%d分钟超时",scfg.sendInterval);
// int e;
LOG_I("等待发送条件(文件就绪或定时%d分钟超时",scfg.sendInterval);
if (rt_event_recv(&chkSta, FILE_IS_OK | TIMER_IS_OUT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
rt_tick_from_millisecond(5000), RT_NULL) == RT_EOK) //条件1满足
RT_WAITING_FOREVER, RT_NULL) == RT_EOK) //条件1满足
{
LOG_I("等待TT就绪");
LOG_I("当前规则为:连续%d个采集周期TT信号质量不低于%d",scfg.minTTPeriCnt,scfg.minTTsinal);
initTT();
sta = 1;
}
// else {
//// rt_thread_mdelay(500);
// sta = 0;
// continue;
// }
if (!sta) {
continue;
}
for (size_t var = 0; var < scfg.maxTTRetryCnt; var++) //轮询尝试
@ -430,7 +512,7 @@ static void chkAndSendFile_thread_entry()
}
}
}
LOG_W("---------------");
LOG_W("--------one mission DONE--------");
}
}
@ -440,6 +522,12 @@ static void chkAndSendFile_thread_entry()
*/
void chkAndSendFile()
{
sysEventInit();//必须此处初始化不然EVENT无法加入链表
if (!tmrToPNTT) {
tmrToPNTT = rt_timer_create("TTtimeout", timeoutFunc,
RT_NULL, rt_tick_from_millisecond(scfg.timeout*60*1000),
RT_TIMER_FLAG_ONE_SHOT|RT_TIMER_FLAG_SOFT_TIMER);
}
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("chk&send", chkAndSendFile_thread_entry, RT_NULL, 1024 * 5, 19, 10);
/* 创建成功则启动线程 */
@ -455,82 +543,6 @@ void chkAndSendFile()
}
INIT_APP_EXPORT(chkAndSendFile);
static rt_timer_t tmrToPNTT=RT_NULL;
static uint8_t isWindowMode=0;//自动或手动引起TT开机
/**
* TT
*/
void stopTM()
{
if (!tmrToPNTT) {
return;
}
rt_timer_stop(tmrToPNTT);
rt_timer_stop(tmrToPNTT);
}
/**
* flag以关闭超时机制
*/
void setWindowMode(void)
{
isWindowMode = 1;
stopTM();
}
/**
*
*/
void clearWindowMode(void)
{
isWindowMode = 0;
stopTM();
}
/**
* TT
* TT无接收数据则启动关机倒计时
*/
void resetTM()
{
// LOG_D("try to reset");
if (tmrToPNTT == RT_NULL) {
return;
}
if (isWindowMode) {
stopTM();
return;
}
rt_tick_t t= rt_tick_from_millisecond(scfg.timeout*60*1000);
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_SET_TIME,(void*) &t);
rt_timer_stop(tmrToPNTT);
rt_timer_start(tmrToPNTT);
LOG_D("%d minutes from now to power down TT.",scfg.timeout);
}
void d_remain()
{
if (tmrToPNTT == RT_NULL) {
LOG_I("tmrToPNTT is NULL");
return;
}
rt_tick_t arg1,arg2;
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_TIME, (void*)&arg1);
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_REMAIN_TIME, (void*)&arg2);
LOG_D("%d / %d min",arg2/60000,arg1/60000);
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_STATE, (void*)&arg1);
LOG_D("%s",arg1?"YES":"NO");
}
/* 定时器超时函数 */
static void timeoutFunc(void *parameter)
{
LOG_W("time to shunt down TT");
// rt_thread_mdelay(3000);// no delay
deInitTT();//Function[rt_mutex_take] shall not be used in ISR
// list_thread();
}
//RT_TICK_PER_SECOND
//tcp连接保活
//实际场景不存在中间断掉的可能
@ -576,7 +588,7 @@ void initTT_thread_entry()
recTT();
repGetTT();
reportLoc();
LOG_D("threads are ready.");
// resetTM();//启动超时,激活后开启
}
}
@ -601,7 +613,7 @@ void deInitTT_thread_entry()
}
pwTT_thread_entry("0");
startAlarm();
// startAlarm();
clearWindowMode();
LOG_W("shunt down TT DONE");
}
@ -660,10 +672,10 @@ void deInitTT()
#define FUNC_DEMO
#ifdef FUNC_DEMO //测试时导出命令到控制台
MSH_CMD_EXPORT_ALIAS(d_upSta,ttisok,TT_IS_OK);
MSH_CMD_EXPORT_ALIAS(TTIsReady,ttisok,TT_IS_OK);
MSH_CMD_EXPORT(fileIsReady,fileIsReady);
MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile);
MSH_CMD_EXPORT(getAndSendFile, getAndSendFile);
//MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile);
//MSH_CMD_EXPORT(getAndSendFile, getAndSendFile);
MSH_CMD_EXPORT(initTT,TT);
MSH_CMD_EXPORT(deInitTT,TT);
MSH_CMD_EXPORT(d_remain,);

View File

@ -363,7 +363,7 @@ void setCommWindow(uint8_t *t, size_t len)
// char tmpstr[20];
// bytes2str(t,len, 10, ",", tmpstr);
// set_cfgs("openWindowTime",tmpstr);
extern void updateAlarm(uint8_t *t, size_t len);
updateAlarm(t,len);
memcpy(scfg.openWindowTime,t,len);
rt_sem_release(cfgUpdate);
@ -666,13 +666,14 @@ int isInFence(uint8_t *loc);
* 10s更新一次
* @return
*/
static int itime=0;//间隔时间
void reportLoc_thread_entry(void *parameter)
{
#define CHECK_INTERVAL 6000
#define CHECK_INTERVAL 6
//static int i=0;
while (isTCPok())
{
static size_t i = 0;
// static uint32_t i = 0;
int isReadyToSendLoc=0;
uint8_t rst[200];
@ -689,19 +690,21 @@ void reportLoc_thread_entry(void *parameter)
rst[8]=1;
isReadyToSendLoc = 1;
}
rt_thread_mdelay(CHECK_INTERVAL); //默认4s刷新一次
i += 1 ;
if (i*CHECK_INTERVAL > scfg.locRepInterval * 60 * 1000)//定时发送默认5分钟
// rt_thread_mdelay(CHECK_INTERVAL*1000); //默认4s刷新一次
itime += 1 ;
if (itime*CHECK_INTERVAL > scfg.locRepInterval * 60)//定时发送默认5分钟
{
// LOG_D("%d/%d",i*CHECK_INTERVAL,scfg.locRepInterval * 60 *1000);
i=0;
itime = 0;
isReadyToSendLoc = 1;
}
if (isReadyToSendLoc) {
LOG_HEX("loc",16,rst,len);
upSend(rst, len);
}
//LOG_D("i=%d s",i*CHECK_INTERVAL/1000);
// LOG_D("i=%d s",i*CHECK_INTERVAL);
rt_thread_mdelay(CHECK_INTERVAL*1000); //默认4s刷新一次
}
}
@ -854,8 +857,9 @@ void reSend(uint8_t *din, uint8_t len)
* @param din
* @param len
*/
void ttRunCMD(uint8_t *din, size_t len)
void ttRunCMD_thread_entry(uint8_t *din, size_t len)
{
/**
+--------+--------+------------+------------+---------+--------+------+------+-------+-----+-------+---------+------+
| 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | ... | 8+N | 9+N | 10+N |
@ -910,7 +914,16 @@ void ttRunCMD(uint8_t *din, size_t len)
break;
}
}
void ttRunCMD(uint8_t *din, size_t len)
{
static SMSG msg;
memset(&msg, 0, sizeof(SMSG));
memcpy(msg.data,din,len);
msg.len=len;
ttRunCMD_thread_entry(din, len);
}
RT_WEAK int formatAndSendTo3S(uint8_t * din, size_t len)
{
@ -1032,6 +1045,7 @@ void chkACK(uint8_t *msg, size_t size)
LOG_W("INVALID DATA.");
}
}
//#define TR_USE_THREAD
#ifdef TR_USE_THREAD
/**
* @brief TT数据TT收到的指令必是单指令

View File

@ -437,6 +437,9 @@ void recTT_thread_entry()
// }
// LOG_D("TT server is ready.");
static TTRx_MSG msg;
static uint8_t bf[1024];
static dlen=0;
memset(bf,0,1024);
while (1)
{
if (!isTCPok()) {
@ -453,11 +456,15 @@ void recTT_thread_entry()
}
LOG_I("%d Bytes received from TT",msg.size);
LOG_HEX("TTrec", 27, msg.data, msg.size);
//复制数据
memcpy(bf,msg.data,msg.size);
dlen=msg.size;
//存入log
char tmp[200]="";//接收的数据不会很长200足矣
trDataTolog(bytes2str(msg.data, msg.size, 16, " ", tmp), strlen(tmp), 0);
trDataTolog(bytes2str(bf, dlen, 16, " ", tmp), strlen(tmp), 0);
//此处调用处理函数
parseTTData(msg.data,msg.size);
parseTTData(bf,dlen);
}
}
LOG_D("close recTT_thread_entry.");
@ -470,7 +477,7 @@ void recTT_thread_entry()
void recTT(void)
{
/* 创建线程 */
rt_thread_t thread = rt_thread_create("recTT", recTT_thread_entry, RT_NULL, 1024 * 6, 27-1, 10);
rt_thread_t thread = rt_thread_create("recTT", recTT_thread_entry, RT_NULL, 1024 * 10, 24, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{