diff --git a/applications/cryp.c b/applications/cryp.c index 51b305b..7b46764 100644 --- a/applications/cryp.c +++ b/applications/cryp.c @@ -13,7 +13,7 @@ //#include #include -#define LOG_TAG "aes" +#define LOG_TAG "cryp" #define LOG_LVL LOG_LVL_DBG #include @@ -92,7 +92,11 @@ void aes_string_test(int argc, char **argv) rt_kprintf("\n---DONE.---\n"); } - +/** + * 将文件内容采用AES-256方式加密 + * @param fin 待加密的文件名称字符串 + * @param fout 加密后文件名称字符串 + */ void aes_file(const char *fin, const char *fout) { rt_uint8_t *buffer = RT_NULL, *outbuffer = RT_NULL; diff --git a/applications/ttmsg/ttmsg.c b/applications/ttmsg/ttmsg.c new file mode 100644 index 0000000..043f605 --- /dev/null +++ b/applications/ttmsg/ttmsg.c @@ -0,0 +1,262 @@ + +#include + +#define LOG_TAG "ttmsg" +#define LOG_LVL LOG_LVL_DBG +#include + +#include "ttmsg.h" +#define STR_LEN_MAX 30 +//模板 +MSG msgs_tpl={ + .fstart = {0x88,0xAA,0xBB,0x88}, + .fnum = {0x00,0x01}, + .fbak = {0x00,0x02}, + .ftype = {0x70,0x21}, + .fdlen = {0x00,0x00}, + + .ftccid = {0x37,0x32,0x32,0x32}, + +// .findex = {0x00,0x01,0x02,0x03,0x04,0x05}, +// .fcfg = {DATA_MODE | CRY_AES}, +}; + +/** + * @brief 按指定分隔符分割字符串 + * + * @param str 待分割字符串 + * @param deli 分隔符 + * @param out 输出字符串数组 + * @return size_t 分割后字符串个数 + */ +size_t split(const char *str, const char *deli, char (*out)[STR_LEN_MAX]) +{ + char buf[50]; + strcpy(buf, str); // strtok会修改字符串 + char *token = strtok(buf, deli); + char *rst[STR_LEN_MAX]; + + size_t i = 0; + while (token) + { + rst[i++] = token; + token = strtok(NULL, deli); + } + + // 更新out + for (size_t ii = 0; ii < i; ii++) + { + // out[ii] = id[ii]; + strcpy(out[ii], rst[ii]); + } + + return i; +} + + +/** + * @brief 从文件名字符串提取ID + * + * @param fin 文件名,支持路径 + * @param out 提取结果 + * @return size_t ID长度 + */ +size_t getID(const char *str, char *out) +{ + char id[10][STR_LEN_MAX] = {}; + // 分割路径 + size_t len = split(str, "/", id); + + // 分割后缀 + len = split(id[len - 1], ".", id); + + // 分割时间 + len = split(id[0], "_", id); + + unsigned char tmp[len]; + for (size_t i = 0; i < len; i++) + { + unsigned int v = atoi(id[i]); + + // 去除代表20的2000 + if (v > 0xff) + { + v = v - 2000; + } + + tmp[i] = v; + } + + memcpy(out, tmp, len); + return len; +} + +/** + * @description 初始化打包配置文件 + * @param id 时间戳,作为数据包唯一ID + * @param flag 参数配置,如信息类型、压缩方式、加密方式等 + */ +packInit(MSG tpl, const char *fin, rt_uint8_t flag) +{ + //模板 + MSG msgs_tpl={ + .fstart = {0x88,0xAA,0xBB,0x88}, + .fnum = {0x00,0x01}, + .fbak = {0x00,0x02}, + .ftype = {0x70,0x21}, + .fdlen = {0x00,0x00}, + + .ftccid = {0x37,0x32,0x32,0x32}, + + // .findex = {0x00,0x01,0x02,0x03,0x04,0x05}, + .fcfg = flag, + }; + +// char *fin="log/2023_05_05/2023_05_05_10_59_23_123"; +// unsigned char id[7]; + size_t len = getID(fin, msgs_tpl.findex); + if (len != 7) { + LOG_I("file name %s format error",fin); + } + +} + + +/** + * @description: 按MSG帧格式拼接din数据后输出到dout + * @param {rt_uint8_t} *din 待打包数据 + * @param {rt_uint8_t} len 待打包数据长度 + * @param {rt_uint8_t} *id 时间戳,作为数据包唯一ID + * @param {rt_uint8_t} flag 参数配置,如信息类型、压缩方式、加密方式等 + * @param {rt_uint8_t} *dout 输出buff + * @return {*} 打包后数据总长度 + */ +rt_uint8_t packMsg(rt_uint8_t *din, rt_uint8_t len, rt_uint8_t *id, rt_uint8_t flag, rt_uint8_t *dout) +{ + MSG *p_msg = NULL; + rt_uint8_t fralen=sizeof(MSG)+len;//len of current frame + // printf("len is %d\n",fralen); + + // 为帧数据申请内存 + p_msg = (MSG*)malloc(fralen);//分配空间 + if (p_msg == NULL) + { + printf("malloc error, no enough memory."); /* code */ + return 0; + } + // 复制模板内容 + memcpy(p_msg,&msgs_tpl,sizeof(MSG)); + p_msg->fcfg[0] = flag; + + // 计算各字段 + p_msg->fdlen[0]=0;//单包长度必不会超过0xFF,故高字节始终为0 + p_msg->fdlen[1]= fralen - 13;//总长度-帧头长度 + memcpy(p_msg->findex,id,6); + + // 将待发数据传入帧数据 + memcpy(p_msg->fdata,din,len); + + // 将帧数据传入buff + memcpy(dout,p_msg,fralen); + + // ! 释放内存 + free(p_msg); + p_msg=NULL; + + return fralen; +} + + +size_t pack_File(const char *fin, char (*dout)[200]) +{ + int fd_in = -1, fd_out = -1; + int ret = 0; + + size_t file_size = 0; + rt_uint8_t *buffer = RT_NULL; + size_t flen = 150; //每包数据初始长度,最大不超过 FRAME_DATA_LEN_MAX + + fd_in = open(fin, O_RDONLY, 0); + if (fd_in < 0) + { + rt_kprintf("[fastlz] open the input file : %s error!\n", fin); + ret = -1; + goto _exit; + } + +// fd_out = open(fout, O_WRONLY | O_CREAT | O_TRUNC, 0); +// if (fd_out < 0) +// { +// rt_kprintf("[fastlz] open the output file : %s error!\n", fout); +// ret = -1; +// goto _exit; +// } + + file_size = lseek(fd_in, 0, SEEK_END); + lseek(fd_in, 0, SEEK_SET); + + //粗略调整每包大小 + 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; + } + } + buffer = (rt_uint8_t *) malloc(file_size); + + for (int var = 0; var < file_size; ++flen) + { + + } + + _exit: + if(fd_in >= 0) + { + close(fd_in); + } + +// if(fd_out >= 0) +// { +// close(fd_out); +// } +// if (cmprs_buffer) +// { +// free(cmprs_buffer); +// } +// +// if (buffer) +// { +// free(buffer); +// } + +// return ret; + +} + +void demomsg() { + + printf("Hello, World!\n"); + printf("MSG size is %d \n",sizeof(MSG)); + printf("msgs size is %d \n",sizeof(msgs_tpl)); + printf("uint8_t size is %d \n",sizeof(rt_uint8_t)); + rt_uint8_t d[]={0xAA,0xBB,0x88,0xFF,0xFF,0x00,0x00,0x70,0x21,0x00,0x8F,0xD3,0x31,0x33}; + rt_uint8_t p_d[200]; + rt_uint8_t index[]={0x63,0x30,0x20,0x33,0x33,0x20}; + rt_uint8_t len=0; + len = packMsg(&msgs_tpl, d,sizeof(d),index,p_d); + printf("len is %d\n",len); + for (size_t i = 0; i < len; i++) + { + /* code */ + printf("%d -- 0x%02X \n",i,p_d[i]); + } + p_d[len]='\0'; +} + + diff --git a/applications/ttmsg/ttmsg.h b/applications/ttmsg/ttmsg.h new file mode 100644 index 0000000..81ae526 --- /dev/null +++ b/applications/ttmsg/ttmsg.h @@ -0,0 +1,74 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-04-21 murmur the first version + */ +#ifndef APPLICATIONS_TTMSG_TTMSG_H_ +#define APPLICATIONS_TTMSG_TTMSG_H_ + +#include +#include +#include + +#include +#include +#include + +#define FRAME_DATA_LEN_MAX 180 + +typedef enum{ + // + + //加密,低2位 + CRY_NONE=0, + CRY_AES, + CRY_RSA, + + //压缩,次高2位 + COMP_NONE=(0<<2), + COMP_QUICK_LZ=(1<<2),//压缩率中等,demo约1/0.62,资源占用少 + COMP_LZMA=(2<<2),//压缩率可能最高,样例报错还未定位,猜测是内存不足 + + //数据类型子类,随CTRL_MODE不同而不同,共3位8种 + MODE_0=(0<<4), + MODE_1=(1<<4), + //数据类型,最高位 + CTRL_MODE=(0<<7), + DATA_MODE=(1<<7), + + // + +}fopt_e; + +#pragma pack(1) +typedef struct{ + //帧头 + //格式是TT厂家确定的,厂家不变则不会变,13字节 + rt_uint8_t fstart[4];// + rt_uint8_t fnum[2] ; + rt_uint8_t fbak[2] ; + rt_uint8_t ftype[2] ;//固定位0x7021 + rt_uint8_t fdlen[2] ;//总数据长度,从ccid开始计算 + rt_uint8_t fcrc[1] ;// + + //数据体,部分自定义 + //目标终端前4字节必须为CCID,厂家定义 + rt_uint8_t ftccid[4] ;// + + //自定义数据,格式如下 + rt_uint8_t findex[6] ;//唯一ID号,索引号:年月日时分秒+1字节随机码 年取后两位(2023即23),随机码为防止RTC复位后时间错乱 + rt_uint8_t fcfg[1] ;//数据类型 |压缩方式|加密方式 + rt_uint8_t fcurpiece[1];//当前分片数 + rt_uint8_t fallpiece[1];//总分片数 + rt_uint8_t fdata[];//当前分片数据,为待发数据 + +}MSG; + +rt_uint8_t packMsg(rt_uint8_t *din, rt_uint8_t len, rt_uint8_t *id, rt_uint8_t flag, rt_uint8_t *dout); + + +#endif /* APPLICATIONS_TTMSG_TTMSG_H_ */ diff --git a/rtconfig.h b/rtconfig.h index 3ccaf80..6a21ad8 100644 --- a/rtconfig.h +++ b/rtconfig.h @@ -71,7 +71,7 @@ #define FINSH_THREAD_PRIORITY 20 #define FINSH_THREAD_STACK_SIZE 4096 #define FINSH_USING_HISTORY -#define FINSH_HISTORY_LINES 10 +#define FINSH_HISTORY_LINES 5 #define FINSH_USING_SYMTAB #define FINSH_CMD_SIZE 80 #define MSH_USING_BUILT_IN_COMMANDS