TT12-MCU/applications/_main.c
CSSC-WORK\murmur 656fde2902 tools.c update bytes2str()
数据解析同意放入 func.c 中有parseTTData() 判断
更新parseTTData函数,添加对粘包数据的支持
2023-07-20 16:18:42 +08:00

270 lines
6.5 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* 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
*/
#include <rtthread.h>
#define LOG_TAG "main_scha"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
#include <board.h>
#include <ttmsg/ttmsg.h>
//#include <cfg.h>
#include <usrcfg.h>
#include <ttTR.h>
static struct rt_event sw_check; //软件条件
static struct rt_event hw_check; //硬件条件
extern rt_sem_t TTReady;
#define ALL_READY 1
static int maxTTWaitTime = 4;
static int maxTTRetryCnt = 3;
static void initEvent(void)
{
/* 事 件 控 制 块 */
rt_err_t result = rt_event_init(&sw_check, "SHcheck", RT_IPC_FLAG_PRIO);
result = rt_event_init(&hw_check, "HWcheck", RT_IPC_FLAG_PRIO) | result;
if (result != RT_EOK)
{
LOG_E("init event failed.\n");
}
}
INIT_COMPONENT_EXPORT(initEvent);
void upSWflag(void)
{
rt_event_send(&sw_check, FILE_IS_OK);
}
void upTTflag(void)
{
rt_event_send(&sw_check, TT_IS_OK);
}
/**
* 更新各参数
*/
static void updatecfg()
{
maxTTWaitTime = get_cfg("maxTTWaitTime");
maxTTRetryCnt = get_cfg("maxTTRetryCnt");
}
//INIT_COMPONENT_EXPORT(t3);
extern void ttinfoInit(void);
extern void startTTinfo(void);
/**
* 监控TT状态。需求条件1TT连续5个周期为激活状态且信号强度不低于5。
*/
void checkTT()
{
repGetTT(); //持续更新
}
static void getFileData_thread_entry(void)
{
//等待事件
extern struct rt_event hw_check; //硬件条件
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,
RT_WAITING_FOREVER, RT_NULL) == RT_EOK)
{
//get file to be send
char *f = "2023_06_16_10_28_00_123.bin";
// char *f="1023_05_19_15_29_59_254.txt";
// pack file
rt_uint8_t len = 0;
len = pack_File(f, 0, d, s);
rt_kprintf("len is %d\n", len);
if (len)
{
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.");
}
}
}
}
//MSH_CMD_EXPORT(getFileData_thread_entry,tet);
static void gg(void)
{
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("PSmsg", getFileData_thread_entry, RT_NULL, 1024 * 12, 27, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'PSmsg' create failure.");
return;
}
}
static void try()
{
static rt_err_t rst = RT_ERROR;
updatecfg();
static rt_uint8_t d[10][200] = { };
static rt_uint8_t s[10] = { };
while (1)
{
// int e;
LOG_I("等待软件就绪");
rst = rt_event_recv(&sw_check, FILE_IS_OK | TIMER_IS_OUT, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR,
RT_WAITING_FOREVER, RT_NULL);
if (rst == RT_EOK) //软件条件满足
{
//检查硬件条件
LOG_I("软件就绪开始等待TT");
// continue;
checkTT();
//打包数据
}
for (size_t var = 0; var < maxTTRetryCnt; var++) //轮询尝试
{
LOG_I("第%d次尝试。", var + 1);
// rst = rt_event_recv(&sw_check, TT_IS_OK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, maxTTWaitTime*60*1000,
// RT_NULL);
rst = rt_sem_take(TTReady, rt_tick_from_millisecond(maxTTWaitTime * 60 * 1000));
if (rst == RT_EOK) //硬件条件满足
{
LOG_I("TT准备就绪");
rt_event_send(&hw_check, ALL_READY);
break;
}
else if (rst == -RT_ETIMEOUT)
; //超时则重试
{
LOG_W("第%d次尝试中TT准备失败。", var + 1);
// pwTT_thread_entry("0");
rt_thread_mdelay(1000);
checkTT();
// continue;
}
}
//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]);
}
LOG_I("Done.");
}
#endif
LOG_I("----------------------------");
//initEvent();
// rt_thread_mdelay(100);
}
}
void smsg()
{
/* 创建 serial 线程 */
rt_thread_t thread = rt_thread_create("checkSta", try, RT_NULL, 1024 * 5, 24, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
}
else
{
LOG_E("thread 'checkSta' create failure.");
return;
}
}
//INIT_COMPONENT_EXPORT(smsg);
//tcp连接保活
//实际场景不存在中间断掉的可能
void iniCnt()
{
rt_hw_stm32_eth_init(); //激活网口
rt_thread_mdelay(5000);
while (1 && !isTCPok()) //判断TCP连接是否正常异常自动重连
{
// while (!isTCPok())
{
LOG_W("TT server is not ready.");
tcpInit();
LOG_D("s=%d",isTCPok());
// rt_thread_mdelay(5000);
}
{
LOG_D("TT server is ready.");
tcpRecMQ();
recTT();
rt_thread_mdelay(1000);
}
}
}
void deiniCnt()
{
rt_hw_stm32_eth_deinit(); //qu激活网口
}
#define FUNC_DEMO
#ifdef FUNC_DEMO //测试时导出命令到控制台
MSH_CMD_EXPORT(smsg, smsg);
MSH_CMD_EXPORT(gg, file);
MSH_CMD_EXPORT(iniCnt,tcp连接);
MSH_CMD_EXPORT(deiniCnt,tcp连接);
#endif