/* * 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;//硬件条件 //struct rt_event tcp_chk;//tcp条件 //#define FILE_IS_OK 1 //#define TIMER_IS_OUT 1<<1 //#define TT_IS_OK 1<<2 #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; // result = rt_event_init(&tcp_chk, "tcpTT", 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"); } //extern struct rt_event update_cfg; //void t3(void) //{ // int32_t e; // int rst = rt_event_recv(&update_cfg, CFGCHANGEED, RT_EVENT_FLAG_OR , RT_WAITING_FOREVER, &e); // if (rst == RT_EOK) { //// update_cfg(); // LOG_I("ggxb"); // } //} //INIT_COMPONENT_EXPORT(t3); extern void ttinfoInit(void); extern void startTTinfo(void); /** * 监控TT状态。需求条件1:TT连续5个周期为激活状态且信号强度不低于5。 */ void checkTT() { static rt_thread_t thread = RT_NULL; //上电 // pwTT_thread_entry("1"); //延时30s等待系统启动、可以GET信息,避免不必要的错误 // rt_thread_mdelay(1000*3); // char c[3][10]={"getTT","10","1"} ; //// getTT(3,c); // getTT(1, RT_NULL); // getTTinfo_thread_entry() //#define NEW_THREAD static CFG cfg; memset(&cfg, 0, sizeof(CFG)); // cfg.s = 1; cfg.cnt = maxTTWaitTime*60/cfg.s-2;//避免通信异常 extern void getTTinfo_thread_entry(void* parameter); #define NEW_THREAD #ifdef NEW_THREAD /* 创建 serial 线程 */ thread = rt_thread_create("getTT", getTTinfo_thread_entry, (void *) &cfg, 1024 * 3, 25, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { rt_thread_startup(thread); } else { LOG_E("thread 'getTT' create failure."); return; } #endif // ttinfoInit(); // startTTinfo(); // rt_thread_create("getS", startTTinfo, RT_NULL, 1024 * 3, 25, 10); } static void getFileData_thread_entry(void) { //等待事件 extern struct rt_event hw_check;//硬件条件 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 static rt_uint8_t d[10][200] = { }; static rt_uint8_t s[10] = { }; rt_uint8_t len = 0; #define __PCK #ifdef __PCK // LOG_D("%p--%p",d,s); // LOG_I("pack %s ...",f); // static MSG cfg; // rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间 len = pack_File(f, 0, d, s); rt_kprintf("len is %d\n", len); if (len && (tcpInit() == RT_EOK)) { // rt_event_send(&tcp_chk, TT_TCP_OK); 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."); } #else rt_uint8_t *buffer = RT_NULL; static MSG cfg; // static rt_uint8_t len=0; { // rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间 rt_uint8_t rst = packInit(&cfg, f, 0); if (rst != RT_EOK) { return 0; } int fd_in = -1; //, fd_out = -1; static size_t file_size = 0; // rt_uint8_t *buffer = RT_NULL; static size_t flen = 150; // 每包数据初始长度,最大不超过 FRAME_DATA_LEN_MAX // rt_uint8_t len = 0; fd_in = open(f, O_RDONLY, 0); if (fd_in < 0) { LOG_E("open the input file : %s error!\n", f); close(fd_in); return len; } file_size = lseek(fd_in, 0, SEEK_END); LOG_I("file size is %d bytes.", file_size); lseek(fd_in, 0, SEEK_SET); // static rt_uint8_t *buffer = RT_NULL; buffer = rt_malloc(file_size); if (buffer == RT_NULL) { LOG_E("No memory for read %s.", f); return 0; } read(fd_in, buffer, file_size); // LOG_HEX() close(fd_in); // 粗略调整每包大小 if (file_size > (size_t)FRAME_DATA_LEN_MAX) { while (file_size % flen < flen / 1.2 && flen < FRAME_DATA_LEN_MAX) // 阈值约83% { flen += 1; } if (file_size % flen < flen / 1.2) { // 到达限制仍不符合要求 flen = 150; while (file_size % flen < flen / 2.5 && flen < FRAME_DATA_LEN_MAX) // 调整阈值为40% { flen += 1; } } } else { flen = file_size; } LOG_I("flen is %d",flen); // s[0] = packMsg(&cfg, buffer, flen, d[0]); len = packMsgs(&cfg, buffer, file_size, flen, d, s); if (fd_in >= 0) { close(fd_in); } if (buffer != RT_NULL) { LOG_D("try to free buf."); rt_free(buffer); LOG_D("free buf done."); } } #endif } } } //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); //rt_tick_from_millisecond(maxTTWaitTime * 1000) rst = rt_event_recv(&sw_check, TT_IS_OK, RT_EVENT_FLAG_OR | RT_EVENT_FLAG_CLEAR, maxTTWaitTime*60*1000, RT_NULL); 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); } } MSH_CMD_EXPORT(gg,file); 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); MSH_CMD_EXPORT(smsg,smsg);