2023-06-01 09:10:00 +00:00
|
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2006-2021, RT-Thread Development Team
|
|
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
*
|
|
|
|
|
* Change Logs:
|
|
|
|
|
* Date Author Notes
|
|
|
|
|
* 2023-06-01 murmur the first version
|
2023-07-28 08:29:57 +00:00
|
|
|
|
* 处理TT相关的初始化、状态切换等逻辑
|
2023-06-01 09:10:00 +00:00
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <rtthread.h>
|
|
|
|
|
|
2023-08-08 06:31:16 +00:00
|
|
|
|
#define LOG_TAG "core"
|
2023-06-01 09:10:00 +00:00
|
|
|
|
#define LOG_LVL LOG_LVL_DBG
|
|
|
|
|
#include <ulog.h>
|
|
|
|
|
#include <board.h>
|
|
|
|
|
#include <ttmsg/ttmsg.h>
|
2023-06-20 10:09:07 +00:00
|
|
|
|
//#include <cfg.h>
|
2023-06-02 02:23:26 +00:00
|
|
|
|
#include <usrcfg.h>
|
2023-06-06 07:45:13 +00:00
|
|
|
|
#include <ttTR.h>
|
2023-07-28 08:29:57 +00:00
|
|
|
|
|
2023-08-01 08:27:21 +00:00
|
|
|
|
//static struct rt_event sw_check; //软件条件
|
|
|
|
|
static struct rt_event chkSta; //发送条件,含两部分,1-超时或文件准备好,2-TT满足通信条件
|
2023-06-20 10:09:07 +00:00
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
|
2023-06-06 07:45:13 +00:00
|
|
|
|
#define ALL_READY 1
|
2023-06-01 09:10:00 +00:00
|
|
|
|
|
2023-08-04 10:13:59 +00:00
|
|
|
|
//static int maxTTWaitTime = 4;
|
|
|
|
|
//static int maxTTRetryCnt = 3;
|
2023-06-01 09:10:00 +00:00
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
rt_sem_t TTReady= RT_NULL;//天通具备发送状态后 rt_sem_release(TTReady);
|
|
|
|
|
rt_sem_t cfgUpdate = RT_NULL;
|
|
|
|
|
rt_sem_t shuntDownTT = RT_NULL;
|
2023-08-08 06:31:16 +00:00
|
|
|
|
rt_sem_t okTosend = RT_NULL;
|
2023-08-08 08:38:50 +00:00
|
|
|
|
static rt_thread_t initThread=RT_NULL;
|
|
|
|
|
static rt_thread_t deinitThread=RT_NULL;
|
2023-08-01 08:27:21 +00:00
|
|
|
|
//void TTisReady(void)
|
|
|
|
|
//{
|
|
|
|
|
// rt_sem_release(TTReady);
|
|
|
|
|
//}
|
2023-07-28 08:29:57 +00:00
|
|
|
|
|
|
|
|
|
SYS_CFG scfg={
|
|
|
|
|
.sendInterval =60,
|
|
|
|
|
.maxTTWaitTime = 4,
|
|
|
|
|
.maxTTRetryCnt = 3,
|
|
|
|
|
.minTTPeriCnt=5,
|
|
|
|
|
.minTTsinal=5,
|
2023-07-29 08:08:15 +00:00
|
|
|
|
.timeout=5,
|
2023-08-08 06:31:16 +00:00
|
|
|
|
.maxSizePerFile=1024,
|
|
|
|
|
// .openWindowTime[]={0x02, 0x0F, 0x03, 0x1E, 0x08, 0x0F, 0x09, 0x1E}
|
2023-07-28 08:29:57 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新全局参数
|
|
|
|
|
*/
|
|
|
|
|
static void updatecfg(void)
|
|
|
|
|
{
|
|
|
|
|
//因为不知原因,采用事件集独立更新配置出错,无精力深查
|
|
|
|
|
//独立响应单个参数更新事件,程序上更复杂也没特别必要
|
|
|
|
|
//现采用事件通知、统一全部重新加载
|
2023-08-08 06:31:16 +00:00
|
|
|
|
// memcpy(scfg.openWindowTime,
|
2023-07-28 08:29:57 +00:00
|
|
|
|
while(1)
|
|
|
|
|
{
|
|
|
|
|
if(rt_sem_take(cfgUpdate, RT_WAITING_FOREVER) == RT_EOK)
|
|
|
|
|
{
|
|
|
|
|
scfg.maxTTWaitTime = get_cfg("maxTTWaitTime");
|
|
|
|
|
scfg.maxTTRetryCnt = get_cfg("maxTTRetryCnt");
|
|
|
|
|
scfg.minTTPeriCnt = get_cfg("minTTPeriCnt");
|
|
|
|
|
scfg.minTTsinal = get_cfg("minTTsinal");
|
|
|
|
|
scfg.timeout = get_cfg("timeout");
|
2023-07-29 08:08:15 +00:00
|
|
|
|
scfg.maxSizePerFile = get_cfg("maxSizePerFile");
|
2023-08-08 06:31:16 +00:00
|
|
|
|
|
|
|
|
|
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);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
LOG_D("cfg updated.");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sysSemInit()
|
|
|
|
|
{
|
2023-08-08 06:31:16 +00:00
|
|
|
|
okTosend = rt_sem_create("okTosend", 0, RT_IPC_FLAG_PRIO);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO);
|
|
|
|
|
shuntDownTT = rt_sem_create("shuntDNTT", 0, RT_IPC_FLAG_PRIO);
|
2023-08-04 06:09:39 +00:00
|
|
|
|
rt_event_init(&chkSta, "chkSta", RT_IPC_FLAG_PRIO);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
rt_sem_release(cfgUpdate); //上电更新值
|
2023-08-08 06:31:16 +00:00
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
updatecfg();
|
2023-08-04 06:09:39 +00:00
|
|
|
|
|
2023-08-08 06:31:16 +00:00
|
|
|
|
/* 创建 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;
|
|
|
|
|
// }
|
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
// LOG_D("sysSemInit DONE.");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-08-04 06:09:39 +00:00
|
|
|
|
void sysEventInit(void)
|
2023-06-01 09:10:00 +00:00
|
|
|
|
{
|
2023-08-01 08:27:21 +00:00
|
|
|
|
rt_err_t result = rt_event_init(&chkSta, "chkSta", RT_IPC_FLAG_PRIO);
|
2023-06-16 03:18:52 +00:00
|
|
|
|
|
2023-06-01 09:10:00 +00:00
|
|
|
|
if (result != RT_EOK)
|
|
|
|
|
{
|
|
|
|
|
LOG_E("init event failed.\n");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 08:27:21 +00:00
|
|
|
|
void sysInit(void)
|
|
|
|
|
{
|
|
|
|
|
sysSemInit();
|
2023-08-04 06:09:39 +00:00
|
|
|
|
// sysEventInit();
|
2023-08-01 08:27:21 +00:00
|
|
|
|
}
|
|
|
|
|
//INIT_COMPONENT_EXPORT(sysInit);
|
|
|
|
|
|
|
|
|
|
void timerIsReady(void)
|
|
|
|
|
{
|
2023-08-04 06:09:39 +00:00
|
|
|
|
rt_event_send(&chkSta, TIMER_IS_OUT);
|
|
|
|
|
stopTM();
|
2023-08-01 08:27:21 +00:00
|
|
|
|
}
|
|
|
|
|
void fileIsReady(void)
|
2023-06-06 07:45:13 +00:00
|
|
|
|
{
|
2023-08-04 06:09:39 +00:00
|
|
|
|
rt_event_send(&chkSta, FILE_IS_OK);
|
|
|
|
|
stopTM();
|
2023-06-06 07:45:13 +00:00
|
|
|
|
}
|
2023-08-01 08:27:21 +00:00
|
|
|
|
void TTIsReady(void)
|
2023-06-06 07:45:13 +00:00
|
|
|
|
{
|
2023-08-04 06:09:39 +00:00
|
|
|
|
rt_event_send(&chkSta, TT_IS_OK);
|
|
|
|
|
stopTM();
|
2023-06-06 07:45:13 +00:00
|
|
|
|
}
|
2023-08-01 08:27:21 +00:00
|
|
|
|
void d_upSta()
|
2023-06-01 09:10:00 +00:00
|
|
|
|
{
|
2023-08-01 08:27:21 +00:00
|
|
|
|
TTIsReady();
|
2023-06-01 09:10:00 +00:00
|
|
|
|
}
|
2023-06-02 06:37:58 +00:00
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
|
|
|
|
|
//extern void ttinfoInit(void);
|
|
|
|
|
//extern void startTTinfo(void);
|
2023-06-01 09:10:00 +00:00
|
|
|
|
/**
|
|
|
|
|
* 监控TT状态。需求条件1:TT连续5个周期为激活状态且信号强度不低于5。
|
|
|
|
|
*/
|
|
|
|
|
void checkTT()
|
|
|
|
|
{
|
2023-06-20 10:09:07 +00:00
|
|
|
|
repGetTT(); //持续更新
|
2023-06-06 07:45:13 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 08:27:21 +00:00
|
|
|
|
typedef struct
|
|
|
|
|
{
|
|
|
|
|
char fname[60];
|
|
|
|
|
uint8_t index;
|
|
|
|
|
}FILE_INFO;
|
|
|
|
|
|
|
|
|
|
static void upSendFile_thread_entry(void *parameter)
|
2023-06-06 07:45:13 +00:00
|
|
|
|
{
|
2023-08-01 08:27:21 +00:00
|
|
|
|
FILE_INFO *f = RT_NULL;
|
|
|
|
|
f = (FILE_INFO *) parameter;
|
2023-06-06 07:45:13 +00:00
|
|
|
|
|
2023-08-04 10:13:59 +00:00
|
|
|
|
static rt_uint8_t d[BUFFER_ROW][200] = { };
|
|
|
|
|
static rt_uint8_t s[BUFFER_ROW] = { };
|
2023-08-08 06:31:16 +00:00
|
|
|
|
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);
|
2023-08-01 08:27:21 +00:00
|
|
|
|
rt_uint8_t len = pack_File(f->fname, 0, d, s);
|
2023-08-04 10:13:59 +00:00
|
|
|
|
// for (size_t i = 0; i < len; i++) {
|
|
|
|
|
// LOG_HEX("rst",16,d[i],s[i]);
|
|
|
|
|
// }
|
|
|
|
|
// list_thread();
|
2023-08-08 06:31:16 +00:00
|
|
|
|
if (len)//部分demo数据体积>>1k
|
2023-06-20 10:09:07 +00:00
|
|
|
|
{
|
2023-08-01 08:27:21 +00:00
|
|
|
|
LOG_D("%d pack(s) to send", f->index ? 1 : len);
|
2023-08-08 06:31:16 +00:00
|
|
|
|
for (rt_uint8_t var = 0; var < len; var++)
|
2023-06-06 07:45:13 +00:00
|
|
|
|
{
|
2023-08-01 08:27:21 +00:00
|
|
|
|
if (!f->index || (var+1) == f->index)
|
|
|
|
|
{ //index=0 全发,或者仅发index
|
2023-08-04 10:13:59 +00:00
|
|
|
|
if (sendMsg(d[var], s[var]) == RT_EOK) {
|
|
|
|
|
LOG_D("send pack[%d] done.",var+1);
|
|
|
|
|
};
|
2023-08-08 06:31:16 +00:00
|
|
|
|
rt_thread_mdelay(1000);
|
2023-06-06 07:45:13 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-04 10:13:59 +00:00
|
|
|
|
LOG_I("upSendFile done.");
|
2023-08-08 06:31:16 +00:00
|
|
|
|
clearFileToSend(f->fname);
|
2023-08-08 08:38:50 +00:00
|
|
|
|
|
2023-08-08 06:31:16 +00:00
|
|
|
|
// list_thread();
|
|
|
|
|
rt_sem_release(okTosend);
|
2023-06-06 07:45:13 +00:00
|
|
|
|
}
|
2023-06-01 09:10:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 08:27:21 +00:00
|
|
|
|
/**
|
|
|
|
|
* 发送文件
|
|
|
|
|
* @param f 待发文件,指完整路径名
|
|
|
|
|
* @param index 指定发送的切片索引,为0时表示全部发送
|
|
|
|
|
*/
|
|
|
|
|
void upSendFile(const char *f, int index)
|
2023-06-06 07:45:13 +00:00
|
|
|
|
{
|
2023-08-01 08:27:21 +00:00
|
|
|
|
|
|
|
|
|
static FILE_INFO info;
|
|
|
|
|
memset(&info, 0, sizeof(FILE_INFO));
|
|
|
|
|
strcpy(info.fname, f);
|
|
|
|
|
info.index = index;
|
|
|
|
|
|
2023-06-06 07:45:13 +00:00
|
|
|
|
/* 创建 serial 线程 */
|
2023-08-08 06:31:16 +00:00
|
|
|
|
rt_thread_t thread = rt_thread_create("SendFile", upSendFile_thread_entry, (void *) &info, 1024 * 3, 27, 10);
|
2023-06-20 10:09:07 +00:00
|
|
|
|
/* 创建成功则启动线程 */
|
|
|
|
|
if (thread != RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_startup(thread);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-07-26 08:26:53 +00:00
|
|
|
|
LOG_E("thread 'upSendFile' create failure.");
|
2023-06-20 10:09:07 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-06 07:45:13 +00:00
|
|
|
|
}
|
2023-08-01 08:27:21 +00:00
|
|
|
|
void d_upSendFile(int argc, char **argv)
|
|
|
|
|
{
|
|
|
|
|
int index=0;
|
|
|
|
|
if (argc == 3) {
|
|
|
|
|
index = atoi(argv[2]);
|
|
|
|
|
}
|
|
|
|
|
upSendFile(argv[1], index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 获取本地待发文件列表并倒序发送
|
|
|
|
|
*/
|
|
|
|
|
void getAndSendFile()
|
|
|
|
|
{
|
2023-08-08 06:31:16 +00:00
|
|
|
|
static int index[MAX_KEY_LEN];
|
|
|
|
|
static char f[10][MAX_KEY_LEN];
|
|
|
|
|
int cnt = getFilesToSend(f, index);
|
|
|
|
|
if(cnt)
|
|
|
|
|
{
|
2023-08-08 08:38:50 +00:00
|
|
|
|
LOG_I("ready to send %d file(s).",cnt);
|
2023-08-08 06:31:16 +00:00
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
LOG_W("no files waiting to be sent.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-08-01 08:27:21 +00:00
|
|
|
|
|
2023-08-08 06:31:16 +00:00
|
|
|
|
rt_sem_release(okTosend);
|
|
|
|
|
while(cnt)
|
2023-08-01 08:27:21 +00:00
|
|
|
|
{
|
2023-08-08 08:38:50 +00:00
|
|
|
|
int i=0;
|
|
|
|
|
for (i = cnt-1; i > -1; i--)
|
2023-08-08 06:31:16 +00:00
|
|
|
|
{
|
|
|
|
|
if (rt_sem_take(okTosend, RT_WAITING_FOREVER) == RT_EOK) {
|
2023-08-08 08:38:50 +00:00
|
|
|
|
rt_thread_mdelay(1000);//thread close
|
2023-08-08 06:31:16 +00:00
|
|
|
|
upSendFile(f[i],index[i]);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-08-08 08:38:50 +00:00
|
|
|
|
if (i == -1) {//上一队列发送完成
|
|
|
|
|
cnt = getFilesToSend(f, index);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 08:27:21 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-07-20 07:29:37 +00:00
|
|
|
|
|
2023-07-26 08:26:53 +00:00
|
|
|
|
static void chkAndSendFile_thread_entry()
|
2023-06-01 09:10:00 +00:00
|
|
|
|
{
|
2023-08-08 06:31:16 +00:00
|
|
|
|
while (1)
|
2023-06-01 09:10:00 +00:00
|
|
|
|
{
|
2023-06-06 07:45:13 +00:00
|
|
|
|
// int e;
|
2023-08-01 08:27:21 +00:00
|
|
|
|
LOG_I("等待文件就绪(或定时%d分钟超时)",scfg.sendInterval);
|
2023-08-04 06:09:39 +00:00
|
|
|
|
if (rt_event_recv(&chkSta, FILE_IS_OK | TIMER_IS_OUT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
|
|
|
|
|
RT_WAITING_FOREVER, RT_NULL) == RT_EOK) //条件1满足
|
2023-06-01 09:10:00 +00:00
|
|
|
|
{
|
2023-08-01 08:27:21 +00:00
|
|
|
|
LOG_I("等待TT就绪");
|
2023-08-08 06:31:16 +00:00
|
|
|
|
initTT();
|
2023-06-01 09:10:00 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-08-01 08:27:21 +00:00
|
|
|
|
for (size_t var = 0; var < scfg.maxTTRetryCnt; var++) //轮询尝试
|
2023-06-01 09:10:00 +00:00
|
|
|
|
{
|
2023-08-01 08:27:21 +00:00
|
|
|
|
LOG_I("第%d/%d次尝试。", var + 1,scfg.maxTTRetryCnt);
|
2023-08-04 06:09:39 +00:00
|
|
|
|
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),
|
2023-08-01 08:27:21 +00:00
|
|
|
|
RT_NULL);
|
2023-08-08 06:31:16 +00:00
|
|
|
|
// rst = rt_sem_take(TTReady, rt_tick_from_millisecond(maxTTWaitTime * 60 * 1000));
|
2023-08-01 08:27:21 +00:00
|
|
|
|
if (rst == RT_EOK) //条件2满足
|
2023-06-01 09:10:00 +00:00
|
|
|
|
{
|
2023-06-16 08:20:18 +00:00
|
|
|
|
LOG_I("TT准备就绪");
|
2023-08-01 08:27:21 +00:00
|
|
|
|
getAndSendFile();
|
2023-06-01 09:10:00 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
2023-08-01 08:27:21 +00:00
|
|
|
|
else if (rst == -RT_ETIMEOUT)//超时则重试
|
2023-06-01 09:10:00 +00:00
|
|
|
|
{
|
2023-06-20 10:09:07 +00:00
|
|
|
|
LOG_W("第%d次尝试中TT准备失败。", var + 1);
|
2023-06-02 02:23:26 +00:00
|
|
|
|
rt_thread_mdelay(1000);
|
2023-08-08 06:31:16 +00:00
|
|
|
|
if ((var+1) == scfg.maxTTRetryCnt) {
|
|
|
|
|
LOG_E("TT准备失败");
|
|
|
|
|
}
|
2023-06-01 09:10:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-06 07:45:13 +00:00
|
|
|
|
|
2023-07-20 07:29:37 +00:00
|
|
|
|
|
2023-08-01 08:27:21 +00:00
|
|
|
|
/**
|
|
|
|
|
* 检查状态并发送文件
|
|
|
|
|
*/
|
2023-07-26 08:26:53 +00:00
|
|
|
|
void chkAndSendFile()
|
2023-06-06 07:45:13 +00:00
|
|
|
|
{
|
|
|
|
|
/* 创建 serial 线程 */
|
2023-08-08 06:31:16 +00:00
|
|
|
|
rt_thread_t thread = rt_thread_create("chk&send", chkAndSendFile_thread_entry, RT_NULL, 1024 * 2, 19, 10);
|
2023-06-20 10:09:07 +00:00
|
|
|
|
/* 创建成功则启动线程 */
|
|
|
|
|
if (thread != RT_NULL)
|
|
|
|
|
{
|
|
|
|
|
rt_thread_startup(thread);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2023-07-26 08:26:53 +00:00
|
|
|
|
LOG_E("thread 'chk&send' create failure.");
|
2023-06-20 10:09:07 +00:00
|
|
|
|
return;
|
|
|
|
|
}
|
2023-06-06 07:45:13 +00:00
|
|
|
|
}
|
2023-08-08 06:31:16 +00:00
|
|
|
|
INIT_APP_EXPORT(chkAndSendFile);
|
2023-07-20 07:29:37 +00:00
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
static rt_timer_t tmrToPNTT=RT_NULL;
|
|
|
|
|
static int isInWindow=0;//winow 关闭时复位
|
|
|
|
|
/**
|
|
|
|
|
* 关闭超时定时器,定时时间到后关闭TT
|
|
|
|
|
*/
|
|
|
|
|
void stopTM()
|
|
|
|
|
{
|
|
|
|
|
if (!tmrToPNTT) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
rt_timer_stop(tmrToPNTT);
|
|
|
|
|
rt_timer_stop(tmrToPNTT);
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 重启超时定时器,定时时间到后关闭TT
|
|
|
|
|
*/
|
|
|
|
|
void resetTM()
|
|
|
|
|
{
|
|
|
|
|
// LOG_D("try to reset");
|
|
|
|
|
if (tmrToPNTT == RT_NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (isInWindow) {
|
|
|
|
|
stopTM();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-29 08:08:15 +00:00
|
|
|
|
rt_tick_t t= rt_tick_from_millisecond(scfg.timeout*60*1000);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_SET_TIME,(void*) &t);
|
|
|
|
|
rt_timer_stop(tmrToPNTT);
|
|
|
|
|
rt_timer_start(tmrToPNTT);
|
2023-08-04 06:09:39 +00:00
|
|
|
|
LOG_D("%d minutes from now to power down TT.",scfg.timeout);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
void d_getRemain()
|
|
|
|
|
{
|
|
|
|
|
LOG_D("=%d=",rt_tick_from_millisecond(10*60*1000));
|
|
|
|
|
rt_tick_t *arg1,*arg2;
|
|
|
|
|
arg1=rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_TIME, (void*)arg1);
|
|
|
|
|
arg2=rt_timer_control(tmrToPNTT, RT_TIMER_CTRL_GET_REMAIN_TIME, (void*)arg2);
|
|
|
|
|
LOG_D("tm=%d-%d",&arg1,&arg2);
|
|
|
|
|
}
|
2023-08-01 08:27:21 +00:00
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
/* 定时器超时函数 */
|
|
|
|
|
static void timeoutFunc(void *parameter)
|
|
|
|
|
{
|
2023-08-04 06:09:39 +00:00
|
|
|
|
LOG_W("time to shunt down TT");
|
|
|
|
|
// rt_thread_mdelay(3000);// no delay
|
2023-07-28 08:29:57 +00:00
|
|
|
|
deInitTT();//Function[rt_mutex_take] shall not be used in ISR
|
2023-08-04 06:09:39 +00:00
|
|
|
|
// list_thread();
|
2023-07-28 08:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//RT_TICK_PER_SECOND
|
2023-07-20 07:29:37 +00:00
|
|
|
|
//tcp连接保活
|
|
|
|
|
//实际场景不存在中间断掉的可能
|
2023-07-27 07:46:45 +00:00
|
|
|
|
void initTT_thread_entry()
|
2023-07-20 07:29:37 +00:00
|
|
|
|
{
|
2023-07-28 08:29:57 +00:00
|
|
|
|
pwTT_thread_entry("1");
|
|
|
|
|
|
2023-08-08 08:38:50 +00:00
|
|
|
|
while (1)
|
2023-07-20 07:29:37 +00:00
|
|
|
|
{
|
2023-08-04 06:09:39 +00:00
|
|
|
|
if (!isTTon()) {
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
if (!tmrToPNTT) {
|
|
|
|
|
tmrToPNTT = rt_timer_create("TTtimeout", timeoutFunc,
|
|
|
|
|
RT_NULL, rt_tick_from_millisecond(scfg.timeout*60*1000),
|
2023-08-03 03:04:50 +00:00
|
|
|
|
RT_TIMER_FLAG_ONE_SHOT);//|RT_TIMER_FLAG_SOFT_TIMER);
|
2023-07-20 07:29:37 +00:00
|
|
|
|
}
|
2023-08-04 06:09:39 +00:00
|
|
|
|
|
2023-07-27 07:46:45 +00:00
|
|
|
|
if (!isEthUP())
|
|
|
|
|
{ //只初始化一次
|
|
|
|
|
LOG_D("init eth...");
|
|
|
|
|
if (rt_hw_stm32_eth_init() == RT_EOK)
|
|
|
|
|
{
|
|
|
|
|
LOG_D("eth inited DONE.");
|
|
|
|
|
}; //激活网口
|
|
|
|
|
}
|
|
|
|
|
else if (!isTCPok()) //判断TCP连接是否正常,异常自动重连
|
2023-07-20 07:29:37 +00:00
|
|
|
|
{
|
2023-07-27 07:46:45 +00:00
|
|
|
|
// LOG_W("TT server is not ready,--%d",isTCPok());
|
|
|
|
|
tcpInit();
|
|
|
|
|
// rt_thread_mdelay(1000);
|
|
|
|
|
if (isTCPok())
|
|
|
|
|
{
|
2023-07-28 08:29:57 +00:00
|
|
|
|
LOG_D("TCP is ready.");
|
2023-07-27 07:46:45 +00:00
|
|
|
|
tcpRecMQ(); //开启tcp接收线程
|
|
|
|
|
recTT();
|
2023-08-08 06:31:16 +00:00
|
|
|
|
// chkAndSendFile();
|
2023-07-27 07:46:45 +00:00
|
|
|
|
}
|
2023-07-20 07:29:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-27 07:46:45 +00:00
|
|
|
|
rt_thread_mdelay(3000); //chk with 3 second interval
|
2023-07-20 07:29:37 +00:00
|
|
|
|
}
|
2023-08-08 08:38:50 +00:00
|
|
|
|
|
|
|
|
|
initThread=RT_NULL;
|
2023-07-20 07:29:37 +00:00
|
|
|
|
}
|
2023-07-28 08:29:57 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void deInitTT_thread_entry()
|
2023-07-27 07:46:45 +00:00
|
|
|
|
{
|
2023-07-28 08:29:57 +00:00
|
|
|
|
if (rt_sem_take(shuntDownTT, RT_WAITING_FOREVER) == RT_EOK)
|
|
|
|
|
{
|
2023-08-04 06:09:39 +00:00
|
|
|
|
tcpClose();
|
2023-07-28 08:29:57 +00:00
|
|
|
|
rt_hw_stm32_eth_deinit(); //qu激活网口
|
2023-08-04 06:09:39 +00:00
|
|
|
|
|
|
|
|
|
pwTT_thread_entry("0");
|
2023-08-08 08:38:50 +00:00
|
|
|
|
// if (0&& tmrToPNTT)
|
|
|
|
|
// {
|
|
|
|
|
// rt_timer_delete(tmrToPNTT); //关闭倒计时
|
|
|
|
|
// tmrToPNTT = RT_NULL;
|
|
|
|
|
// }
|
2023-07-28 08:29:57 +00:00
|
|
|
|
LOG_W("shunt down TT DONE");
|
|
|
|
|
}
|
2023-08-08 08:38:50 +00:00
|
|
|
|
|
|
|
|
|
deinitThread =RT_NULL;
|
2023-07-27 07:46:45 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-28 08:29:57 +00:00
|
|
|
|
/**
|
|
|
|
|
* TT上电,初始化TT相关的网络、TCP等,并保活
|
|
|
|
|
*/
|
|
|
|
|
void initTT()
|
|
|
|
|
{
|
|
|
|
|
/* 创建 serial 线程 */
|
2023-08-08 08:38:50 +00:00
|
|
|
|
if (initThread != RT_NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
initThread = rt_thread_create("initTT", initTT_thread_entry, RT_NULL, 1024 * 1.5, 20, 10);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
/* 创建成功则启动线程 */
|
2023-08-08 08:38:50 +00:00
|
|
|
|
if (initThread != RT_NULL)
|
2023-07-28 08:29:57 +00:00
|
|
|
|
{
|
2023-08-08 08:38:50 +00:00
|
|
|
|
rt_thread_startup(initThread);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LOG_E("thread 'initTT' create failure.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* 创建 serial 线程 */
|
2023-08-08 08:38:50 +00:00
|
|
|
|
if (deinitThread != RT_NULL) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
deinitThread = rt_thread_create("deInitTT", deInitTT_thread_entry, RT_NULL, 1024 * 2, 19, 10);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
/* 创建成功则启动线程 */
|
2023-08-08 08:38:50 +00:00
|
|
|
|
if (deinitThread != RT_NULL)
|
2023-07-28 08:29:57 +00:00
|
|
|
|
{
|
2023-08-08 08:38:50 +00:00
|
|
|
|
rt_thread_startup(deinitThread);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
LOG_E("thread 'deInitTT' create failure.");
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* TT上电,初始化TT相关的网络、TCP等,并保活
|
|
|
|
|
*/
|
2023-07-27 07:46:45 +00:00
|
|
|
|
void deInitTT()
|
2023-07-20 07:29:37 +00:00
|
|
|
|
{
|
2023-07-28 08:29:57 +00:00
|
|
|
|
rt_sem_release(shuntDownTT);
|
2023-07-20 07:29:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define FUNC_DEMO
|
|
|
|
|
#ifdef FUNC_DEMO //测试时导出命令到控制台
|
2023-08-08 06:31:16 +00:00
|
|
|
|
MSH_CMD_EXPORT_ALIAS(d_upSta,ttisok,TT_IS_OK);
|
2023-08-04 06:09:39 +00:00
|
|
|
|
MSH_CMD_EXPORT(fileIsReady,fileIsReady);
|
2023-07-26 08:26:53 +00:00
|
|
|
|
MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile);
|
2023-08-08 08:38:50 +00:00
|
|
|
|
//MSH_CMD_EXPORT(upSendFile, upSendFile);
|
2023-07-28 08:29:57 +00:00
|
|
|
|
MSH_CMD_EXPORT(initTT,初始化TT);
|
|
|
|
|
MSH_CMD_EXPORT(deInitTT,初始化TT);
|
|
|
|
|
MSH_CMD_EXPORT(d_getRemain,d_getRemain);
|
2023-08-01 08:27:21 +00:00
|
|
|
|
MSH_CMD_EXPORT(d_upSendFile,d_upSendFile);
|
2023-07-20 07:29:37 +00:00
|
|
|
|
#endif
|