From bbea30ec53e447f716f143f42a2f57b2c57a1317 Mon Sep 17 00:00:00 2001 From: murmur Date: Mon, 29 May 2023 20:49:30 +0800 Subject: [PATCH] =?UTF-8?q?pack=5FFile=20=E5=88=9D=E6=AD=A5=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E6=8A=A5=E9=94=99=E9=97=AE=E9=A2=98=20=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=20miniIni=20=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .config | 10 ++- applications/cfg.c | 60 ++++++++++++++++ applications/comp.h | 15 ++++ applications/cryp.h | 15 ++++ applications/demo.c | 19 +++++ applications/main.c | 48 +++++++------ applications/ttmsg/ttmsg.c | 144 +++++++++++++++++++++++-------------- drivers/board.h | 3 + packages/pkgs.json | 5 ++ rtconfig.h | 3 + 10 files changed, 246 insertions(+), 76 deletions(-) create mode 100644 applications/cfg.c create mode 100644 applications/comp.h create mode 100644 applications/cryp.h create mode 100644 applications/demo.c diff --git a/.config b/.config index cac8ad9..0151b3a 100644 --- a/.config +++ b/.config @@ -81,7 +81,7 @@ CONFIG_RT_USING_DEVICE=y # CONFIG_RT_USING_INTERRUPT_INFO is not set CONFIG_RT_USING_CONSOLE=y CONFIG_RT_CONSOLEBUF_SIZE=256 -CONFIG_RT_CONSOLE_DEVICE_NAME="uart1" +CONFIG_RT_CONSOLE_DEVICE_NAME="uart3" # end of Kernel Device Object CONFIG_RT_VER_NUM=0x40101 @@ -326,7 +326,7 @@ CONFIG_RT_USING_ULOG=y # CONFIG_ULOG_OUTPUT_LVL_I is not set CONFIG_ULOG_OUTPUT_LVL_D=y CONFIG_ULOG_OUTPUT_LVL=7 -# CONFIG_ULOG_USING_ISR_LOG is not set +CONFIG_ULOG_USING_ISR_LOG=y CONFIG_ULOG_ASSERT_ENABLE=y CONFIG_ULOG_LINE_BUF_SIZE=256 # CONFIG_ULOG_USING_ASYNC_OUTPUT is not set @@ -699,7 +699,11 @@ CONFIG_PKG_CMBACKTRACE_VER_NUM=0x10401 # CONFIG_PKG_USING_SYS_LOAD_MONITOR is not set # CONFIG_PKG_USING_PLCCORE is not set # CONFIG_PKG_USING_RAMDISK is not set -# CONFIG_PKG_USING_MININI is not set +CONFIG_PKG_USING_MININI=y +CONFIG_PKG_MININI_PATH="/packages/system/minIni" +CONFIG_PKG_USING_MININI_V120=y +# CONFIG_PKG_USING_MININI_LATEST_VERSION is not set +CONFIG_PKG_MININI_VER="v1.2.0" # CONFIG_PKG_USING_QBOOT is not set # CONFIG_PKG_USING_PPOOL is not set # CONFIG_PKG_USING_OPENAMP is not set diff --git a/applications/cfg.c b/applications/cfg.c new file mode 100644 index 0000000..5e0d770 --- /dev/null +++ b/applications/cfg.c @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-05-29 murmur the first version + */ +#include "../packages/minIni-v1.2.0/dev/minIni.h" + +#ifdef PKG_USING_MININI +#define LJW_CFG_FILE_NAME "/cfg/cfg.ini" + +void ini_test(void) +{ + char buf[16]; + char ip[16] = {0}, mask[16] = {0}, gw[16] = {0}; + int len; + + /* + ** IP地址,保存在config段 + */ + len = ini_gets("config", "IPAddr", "000000", buf, 16, LJW_CFG_FILE_NAME); + if(strcmp(buf, "000000") == 0) { + // 采用默认值 + len = ini_puts("config", "IPAddr", "192.168.1.253", LJW_CFG_FILE_NAME); + } + + /* + ** 子网掩码,保存在config段 + */ + len = ini_gets("config", "IPMask", "000000", buf, 16, LJW_CFG_FILE_NAME); + if(strcmp(buf, "000000") == 0) { + // 采用默认值 + len = ini_puts("config", "IPMask", "255.255.255.0", LJW_CFG_FILE_NAME); + } + + /* + ** 网关,保存在config段 + */ + len = ini_gets("config", "IPGateWay", "000000", buf, 16, LJW_CFG_FILE_NAME); + if(strcmp(buf, "000000") == 0) { + // 采用默认值 + len = ini_puts("config", "IPGateWay", "192.168.1.1", LJW_CFG_FILE_NAME); + } + + // 读取IP地址 + ini_gets("config", "IPAddr", "000000", ip, 16, LJW_CFG_FILE_NAME); + ini_gets("config", "IPMask", "000000", mask, 16, LJW_CFG_FILE_NAME); + ini_gets("config", "IPGateWay", "000000", gw, 16, LJW_CFG_FILE_NAME); +// set_if("e0",ip, gw,mask); +} + +#ifdef RT_USING_FINSH +#include +MSH_CMD_EXPORT(ini_test, "IP address save as ini files, write and read it") +#endif + +#endif diff --git a/applications/comp.h b/applications/comp.h new file mode 100644 index 0000000..869c9d0 --- /dev/null +++ b/applications/comp.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-05-22 murmur the first version + */ +#ifndef COMP_H_ +#define COMP_H_ + +int fastlz_compress_file(const char *fin, const char *fout); + +#endif /* COMP_H_ */ diff --git a/applications/cryp.h b/applications/cryp.h new file mode 100644 index 0000000..465b29c --- /dev/null +++ b/applications/cryp.h @@ -0,0 +1,15 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-05-22 murmur the first version + */ +#ifndef CRYP_H_ +#define CRYP_H_ + +void aes_file(const char *fin, const char *fout); + +#endif /* CRYP_H_ */ diff --git a/applications/demo.c b/applications/demo.c new file mode 100644 index 0000000..1f14130 --- /dev/null +++ b/applications/demo.c @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-05-18 murmur the first version + */ +#include +#define LOG_TAG "cfg" +#define LOG_LVL LOG_LVL_DBG +#include +#include +#include + + + + diff --git a/applications/main.c b/applications/main.c index 6c5cc45..5274a4a 100644 --- a/applications/main.c +++ b/applications/main.c @@ -31,8 +31,8 @@ void pfdemo(void) // #define cmb_println(...) rt_kprintf(__VA_ARGS__);rt_kprintf("\r\n"); // char *f = "1023_05_19_15_29_59_254.txt"; char *f = "2023_05_19_15_29_59_255.txt"; - static rt_uint8_t d[10][200] = { }; - static rt_uint8_t s[10] = { }; + rt_uint8_t d[10][200] = { }; + rt_uint8_t s[10] = { }; rt_uint8_t len = 0; LOG_D("%p--%p",d,s); LOG_I("pack %s ...",f); @@ -40,9 +40,12 @@ void pfdemo(void) rt_kprintf("len is %d\n", len); if (len) { - LOG_HEX("pkdata:",27,d[0],s[0]); - LOG_D("%p--%p",d,s); - LOG_D("%d--%d--%02X",len,s[0],d[0][0]); + for (size_t var = 0; var < len; ++var) { + LOG_HEX("d",27,d[var],s[var]); + } +// LOG_HEX("pkdata:",27,d[0],s[0]); +// LOG_D("%p--%p",d,s); +// LOG_D("%d--%d--%02X",len,s[0],d[0][0]); LOG_I("Done."); } } @@ -57,36 +60,41 @@ int main(void) // #define LED_HEART GET_PIN(E,3) /* 设置PIN脚模式为输出 */ rt_pin_mode(LED_HEART, PIN_MODE_OUTPUT); +// rt_pin_mode(LED_HEART_DEBUG, PIN_MODE_OUTPUT); rt_pin_mode(ETH_RESET_PIN, PIN_MODE_OUTPUT); + rt_pin_mode(TT_EN, PIN_MODE_OUTPUT); + // rt_pin_write(ETH_RESET_PIN, PIN_LOW);//关闭ETH -// rt_thread_mdelay(10000); -// pfdemo(); - char *f = "2023_05_19_15_29_59_255.txt"; - static rt_uint8_t d[35][200] = { }; - static rt_uint8_t s[35] = { }; - size_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) + + rt_err_t ret = RT_EOK; + /* 设 置 日 期 */ + ret = set_date(2018, 12, 3); + if (ret != RT_EOK) { - LOG_HEX("pkdata:",27,d[0],s[0]); - LOG_D("%p--%p",d,s); - LOG_D("%d--%d--%02X",len,s[0],d[0][0]); - LOG_I("Done."); + rt_kprintf("set RTC date failed\n"); + return ret; } + /* 设 置 时 间 */ + ret = set_time(11, 15, 50); + if (ret != RT_EOK) + { + rt_kprintf("set RTC time failed\n"); +// return ret; + } + while (1) { /* 拉低PIN脚 */ rt_pin_write(LED_HEART, PIN_LOW); +// rt_pin_write(LED_HEART_DEBUG, PIN_LOW); /* 延时1ms,省电 */ rt_thread_mdelay(1); //去掉延时,共用print替换 // rt_kprintf("Heartbeat.\n"); /* 拉高PIN脚 */ rt_pin_write(LED_HEART, PIN_HIGH); +// rt_pin_write(LED_HEART_DEBUG, PIN_HIGH); rt_thread_mdelay(1000); } diff --git a/applications/ttmsg/ttmsg.c b/applications/ttmsg/ttmsg.c index 32f659c..326abb7 100644 --- a/applications/ttmsg/ttmsg.c +++ b/applications/ttmsg/ttmsg.c @@ -32,7 +32,7 @@ #include #include "ttmsg.h" - +#include /** * @brief 按指定分隔符分割字符串 * @@ -67,7 +67,7 @@ static size_t split(const char *str, const char *deli, char (*out)[STR_LEN_MAX]) } #else // strtok_r版本 - static size_t split(const char *str, const char *delim, char (*out)[STR_LEN_MAX]) + static size_t split(const char *str, const char *delim, rt_uint8_t (*out)[STR_LEN_MAX]) { char buf[50]; strcpy(buf, str); // strtok会修改字符串 @@ -105,7 +105,7 @@ static size_t split(const char *str, const char *deli, char (*out)[STR_LEN_MAX]) * @param out 提取结果 * @return size_t ID长度 */ -static size_t getID(const char *str, unsigned char *out) +static size_t getID(const char *str, rt_uint8_t *out) { char id[10][STR_LEN_MAX] = {}; // 分割路径 @@ -144,7 +144,7 @@ static size_t getID(const char *str, unsigned char *out) * @param flag 参数配置,如信息类型、压缩方式、加密方式等 * @return 成功返回0 */ -rt_uint8_t packInit(MSG *tpl, const char *fin, rt_uint8_t flag) +rt_uint8_t packInit(MSG *tpl, const rt_uint8_t *fin, rt_uint8_t flag) { // memset(tpl, 0, sizeof(MSG)); // 分配空间 // 模板 @@ -214,29 +214,31 @@ rt_uint8_t packMsgs(MSG *cfg, rt_uint8_t *din, size_t len, rt_uint8_t w, rt_uint { w = len; } - static rt_uint8_t row = 0; - row = (len / w) + (len % w > 0 ? 1 : 0); + rt_uint8_t row = (len / w) + (len % w > 0 ? 1 : 0); + LOG_I("ROW = %d",row); // rt_uint8_t packdata[row][200]; // 打包后数据 rt_uint8_t tlen[row]; // 打包后数据各维长度 - static rt_uint8_t index = 0; +// static rt_uint8_t index = 0; // rt_uint8_t in[len]; // memcpy(in,din,len); cfg->fallpiece[0]=row; - for (size_t var = 0; var < len;var += w) + rt_uint8_t tmplen = 0; + size_t var=0; + for (rt_uint8_t index = 0; index< row;index++) { - size_t tmplen = len - var; // 当前待打包数据的长度 - tmplen = tmplen > w ? w : tmplen; // 判断最后一包数据的长度 +// rt_uint8_t tmplen = w; // 当前待打包数据的长度 + tmplen = (len - var) > w ? w : (len - var); // 判断最后一包数据的长度 cfg->fcurpiece[0] = index+1; rt_uint8_t col = packMsg(cfg, din+var, tmplen, dout[index]); arrlen[index] = col; - index++; -// LOG_D("index/row-var-tmplen"); -// LOG_D("%5d/%3d-%3d-%6d",index,row,var,tmplen); - LOG_HEX("per msg",27,dout[index-1],col); - LOG_D("--------"); -// LOG_D("addr is %d",din); +// index++; + var += w; + LOG_D("index/row-var-tmplen"); + LOG_D("%5d/%3d-%3d-%6d",index,row,var,tmplen); + LOG_HEX("per msg",27,dout[index],col); +// LOG_D("--------"); rt_thread_mdelay(100); } @@ -286,7 +288,7 @@ LOG_HEX("buf",16,buf,100); */ rt_uint8_t pack_File(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[200], rt_uint8_t *arrLen) { - MSG cfg; + static MSG cfg; rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间 rt_uint8_t rst = packInit(&cfg, fin, flag); if (rst != RT_EOK) @@ -312,7 +314,7 @@ rt_uint8_t pack_File(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[ 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; + rt_uint8_t *buffer=RT_NULL ; buffer = rt_malloc(file_size); if (buffer == RT_NULL) { LOG_E("No memory for read %s.",fin); @@ -342,17 +344,7 @@ rt_uint8_t pack_File(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[ LOG_I("len of data per pack is %d Bytes.",flen); LOG_I("cnt of packages is %d",(file_size / flen) + (file_size % flen > 0 ? 1 : 0)); -// len = packMsgs(&cfg, buffer, file_size, flen, dout, arrLen); -// arrLen[0]= packMsg(&cfg, buffer, flen, dout[0]); -// len=1; - -// len = 10; -// rt_memset(arrLen, 100, len); -// for (size_t var = 0; var < len; ++var) -// { -// rt_memset(dout[var], 0xab, arrLen[var]); -// } - + len = packMsgs(&cfg, buffer, file_size, flen, dout, arrLen); if (fd_in >= 0) { @@ -365,31 +357,30 @@ rt_uint8_t pack_File(const char *fin, rt_uint8_t flag, const rt_uint8_t (*dout)[ rt_free(buffer); LOG_D("free buf done."); } - LOG_D("%p--%p",dout,arrLen); +// LOG_D("%p--%p",dout,arrLen); return len; } - - -void pf_thread_entry(void* parameter) -{ static char d[40][200]; static char s[40]; +// static rt_uint8_t *buffer = RT_NULL; +// static MSG cfg; +static int pf_thread_entry(void* parameter) +{ + char *f = (char *) parameter; // static char d[5][200]; // static char s[5]; -// rt_memset(s, 0xab, 5); -// for (size_t var = 0; var < 5; ++var) { -// rt_memset(d[var], 0xab, 200); -// } - rt_uint8_t len = 0; + rt_uint8_t *buffer = RT_NULL; + static MSG cfg; + static rt_uint8_t len=0; // len = pack_File(f, 0, d, s); - - if (1) +#define DEMO +#ifdef DEMO { - MSG cfg; +// rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间 rt_uint8_t rst = packInit(&cfg, f, 0); if (rst != RT_EOK) @@ -401,7 +392,7 @@ void pf_thread_entry(void* parameter) static size_t file_size = 0; // rt_uint8_t *buffer = RT_NULL; - size_t flen = 150; // 每包数据初始长度,最大不超过 FRAME_DATA_LEN_MAX + static size_t flen = 150; // 每包数据初始长度,最大不超过 FRAME_DATA_LEN_MAX // rt_uint8_t len = 0; fd_in = open(f, O_RDONLY, 0); @@ -415,7 +406,7 @@ void pf_thread_entry(void* parameter) 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; +// static rt_uint8_t *buffer = RT_NULL; buffer = rt_malloc(file_size); if (buffer == RT_NULL) { @@ -424,9 +415,10 @@ void pf_thread_entry(void* parameter) } read(fd_in, buffer, file_size); +// LOG_HEX() close(fd_in); // 粗略调整每包大小 - if (file_size > FRAME_DATA_LEN_MAX) + if (file_size > (size_t)FRAME_DATA_LEN_MAX) { while (file_size % flen < flen / 1.2 && flen < FRAME_DATA_LEN_MAX) // 阈值约83% { @@ -445,6 +437,7 @@ void pf_thread_entry(void* parameter) { 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) @@ -459,22 +452,22 @@ void pf_thread_entry(void* parameter) LOG_D("free buf done."); } } +#endif rt_kprintf("len is %d\n", len); if (len) { -// LOG_HEX("pkdata:",27,d[0],s[0]); -// LOG_D("%d",s[0]); + for (size_t var = 0; var < len; var++) + { + LOG_D("i = %d",var); + LOG_HEX("d", 27, d[var], s[var]); + } LOG_I("Done."); } - LOG_HEX("s",27,s,len); -// for (size_t var = 0; var < len; ++var) { -// LOG_HEX("d",27,d[var],s[var]); -// } return RT_EOK; } -void pmsg(int argc, char **argv) +static int pmsg(int argc, char **argv) { if (argc == 2) { @@ -482,7 +475,7 @@ void pmsg(int argc, char **argv) strcpy(c, argv[1]); /* 创建线程 */ - rt_thread_t thread = rt_thread_create("pmsg", pf_thread_entry, (void *) c, 1024 * 4, 25, 10); + rt_thread_t thread = rt_thread_create("pmsg", pf_thread_entry, (void *) c, 1024 * 20, 25, 10); /* 创建成功则启动线程 */ if (thread != RT_NULL) { @@ -500,7 +493,52 @@ void pmsg(int argc, char **argv) rt_kprintf("Usage:\n"); rt_kprintf("pmsg [input_file] pack \"input_file\" \n"); } + return 0; } #include MSH_CMD_EXPORT(pmsg, 打包文件。); + + +void pwTT_thread_entry(void *parameter) +{ +// LOG_I("--%s--",parameter); + if (parameter == NULL) { + int f = rt_pin_read(TT_EN); + rt_pin_write(TT_EN, !f); + LOG_I("set TT %s",!f?"ON":"OFF"); + } + else + { + rt_bool_t flag = (rt_bool_t)parameter; + rt_pin_write(TT_EN, flag); + LOG_I("set TT %s",flag?"ON":"OFF"); + } + + + +} + +static int swTT(int argc, char **argv) +{ + int f = NULL;//不带参数时argv[1]为NULL + if (argc == 2) + { + f = atoi(argv[1]); + } + + /* 创建线程 */ + rt_thread_t thread = rt_thread_create("sw_TT", pwTT_thread_entry, (void *) f, 1024 * 1, 25, 10); + /* 创建成功则启动线程 */ + if (thread != RT_NULL) + { + rt_thread_startup(thread); + } + else + { + LOG_E("thread 'sw_TT' create failure."); + return RT_ERROR; + } +} + +MSH_CMD_EXPORT(swTT, 切换TT电源。不带参切换电源状态,支持参数); diff --git a/drivers/board.h b/drivers/board.h index 23aa99e..57ff4cf 100644 --- a/drivers/board.h +++ b/drivers/board.h @@ -70,6 +70,7 @@ extern "C" #define BSP_USING_UART1 #define BSP_UART1_TX_PIN "PB6" #define BSP_UART1_RX_PIN "PB7" +#define BSP_UART1_RX_USING_DMA #define BSP_USING_UART3 #define BSP_UART3_TX_PIN "PB10" @@ -371,6 +372,8 @@ extern "C" #define LED_HEART GET_PIN(E,3) +#define LED_HEART_DEBUG GET_PIN(B,3) +#define TT_EN GET_PIN(B,0) #ifndef ETH_RESET_PIN #define ETH_RESET_PIN GET_PIN(A, 4)//E-7 #endif diff --git a/packages/pkgs.json b/packages/pkgs.json index c8a2085..119404c 100644 --- a/packages/pkgs.json +++ b/packages/pkgs.json @@ -14,6 +14,11 @@ "ver": "v1.4.1", "name": "CMBACKTRACE" }, + { + "path": "/packages/system/minIni", + "ver": "v1.2.0", + "name": "MININI" + }, { "path": "/packages/misc/fastlz", "ver": "v1.0.1", diff --git a/rtconfig.h b/rtconfig.h index a647adb..7f206f4 100644 --- a/rtconfig.h +++ b/rtconfig.h @@ -194,6 +194,7 @@ #define RT_USING_ULOG #define ULOG_OUTPUT_LVL_D #define ULOG_OUTPUT_LVL 7 +#define ULOG_USING_ISR_LOG #define ULOG_ASSERT_ENABLE #define ULOG_LINE_BUF_SIZE 256 @@ -293,6 +294,8 @@ /* Micrium: Micrium software products porting for RT-Thread */ /* end of Micrium: Micrium software products porting for RT-Thread */ +#define PKG_USING_MININI +#define PKG_USING_MININI_V120 /* end of system packages */ /* peripheral libraries and drivers */