/* * 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 #define LOG_TAG "main_scha" #define LOG_LVL LOG_LVL_DBG #include #include #include //#include #include #include 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状态。需求条件1:TT连续5个周期为激活状态且信号强度不低于5。 */ void checkTT() { repGetTT(); //持续更新 } static void upSendFile_thread_entry(void) { //等待事件 extern struct rt_event hw_check; //硬件条件 static rt_uint8_t d[10][200] = { }; static rt_uint8_t s[10] = { }; 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]); } tcpClose(); LOG_I("Done."); } } } } //MSH_CMD_EXPORT(getFileData_thread_entry,tet); static void upSendFile(void) { /* 创建 serial 线程 */ rt_thread_t thread = rt_thread_create("upSendFile", upSendFile_thread_entry, RT_NULL, 1024 * 12, 27, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { rt_thread_startup(thread); } else { LOG_E("thread 'upSendFile' create failure."); return; } } static void chkAndSendFile_thread_entry() { 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; //发送数据 //可能有多个待发文件,每个文件打包后为一个二维数组 //如果先打包后发送,需要三维数组,和大内存占用 //故改为“打包-发送”轮询操作 // upSendFile(); LOG_I("----------------------------"); } } void chkAndSendFile() { /* 创建 serial 线程 */ rt_thread_t thread = rt_thread_create("chk&send", chkAndSendFile_thread_entry, RT_NULL, 1024 * 5, 24, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { rt_thread_startup(thread); } else { LOG_E("thread 'chk&send' create failure."); return; } } //INIT_COMPONENT_EXPORT(smsg); //tcp连接保活 //实际场景不存在中间断掉的可能 void initTT_thread_entry() { while (1) { if (!isTTon()) { break; } if (!isEthUP()) { //只初始化一次 LOG_D("init eth..."); if (rt_hw_stm32_eth_init() == RT_EOK) { LOG_D("eth inited DONE."); }; //激活网口 } else if (!isTCPok()) //判断TCP连接是否正常,异常自动重连 { // LOG_W("TT server is not ready,--%d",isTCPok()); tcpInit(); // rt_thread_mdelay(1000); if (isTCPok()) { LOG_D("TT server is ready."); tcpRecMQ(); //开启tcp接收线程 recTT(); } } rt_thread_mdelay(3000); //chk with 3 second interval // LOG_D("rechk-%d",isTCPok()); } } void initTT() { /* 创建 serial 线程 */ rt_thread_t thread = rt_thread_create("initTT", initTT_thread_entry, RT_NULL, 1024 * 1.5, 24, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { rt_thread_startup(thread); } else { LOG_E("thread 'initTT' create failure."); return; } } void deInitTT() { tcpClose(); rt_hw_stm32_eth_deinit(); //qu激活网口 } #define FUNC_DEMO #ifdef FUNC_DEMO //测试时导出命令到控制台 MSH_CMD_EXPORT(chkAndSendFile, chkAndSendFile); MSH_CMD_EXPORT(upSendFile, upSendFile); MSH_CMD_EXPORT(initTT,初始化tcp连接); MSH_CMD_EXPORT(deInitTT,初始化tcp连接); #endif