TT12-MCU/applications/ttmsg/ttmsg.c

275 lines
7.7 KiB
C
Raw Normal View History

2023-05-18 00:58:26 +00:00
/*
*
*
* Esc!1 @2 #3 $4 %5 ^6 &7 *8 (9 )0 _- += |\ `~
*
* Tab Q W E R T Y U I O P {[ }] BS
*
* Ctrl A S D F G H J K L : ;" '│ Enter ││
*
* Shift Z X C V B N M < ,> .? /Shift Fn
*
* Fn Alt Space Alt Win HHKB
*
*
*/
/*
* @Author: murmur test@test.com
* @Date: 2023-05-05 10:07:30
* @LastEditors: murmur test@test.com
* @LastEditTime: 2023-05-11 16:34:31
* @FilePath: \TT12-MCU\applications\ttmsg\ttmsg.c
* @Description:
*
* Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
*/
2023-05-11 03:33:47 +00:00
#include <dfs_file.h>
#define LOG_TAG "ttmsg"
#define LOG_LVL LOG_LVL_DBG
#include <ulog.h>
#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
*/
2023-05-18 00:58:26 +00:00
static size_t split(const char *str, const char *deli, char (*out)[STR_LEN_MAX])
2023-05-11 03:33:47 +00:00
{
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长度
*/
2023-05-18 00:58:26 +00:00
static size_t getID(const char *str, unsigned char *out)
2023-05-11 03:33:47 +00:00
{
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;
}
/**
2023-05-18 00:58:26 +00:00
* @brief MSG
* @param *tpl MSG结构体
* @param fin ID
2023-05-11 03:33:47 +00:00
* @param flag
2023-05-18 00:58:26 +00:00
* @return 0
2023-05-11 03:33:47 +00:00
*/
2023-05-18 00:58:26 +00:00
rt_uint8_t packInit(MSG *tpl, const char *fin, rt_uint8_t flag)
2023-05-11 03:33:47 +00:00
{
//模板
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);
}
2023-05-18 00:58:26 +00:00
memcpy(tpl,&msgs_tpl,sizof(msgs_tpl));
return RT_EOK;
2023-05-11 03:33:47 +00:00
}
/**
* @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 {*}
*/
2023-05-18 00:58:26 +00:00
rt_uint8_t packMsg(MSG *p_msg, rt_uint8_t *din, rt_uint8_t len, rt_uint8_t *dout)
2023-05-11 03:33:47 +00:00
{
2023-05-18 00:58:26 +00:00
rt_uint8_t fralen=sizeof(MSG)+len;//len of current frame
2023-05-11 03:33:47 +00:00
// 计算各字段
p_msg->fdlen[0]=0;//单包长度必不会超过0xFF故高字节始终为0
p_msg->fdlen[1]= fralen - 13;//总长度-帧头长度
2023-05-18 00:58:26 +00:00
// memcpy(p_msg->findex,id,6);
2023-05-11 03:33:47 +00:00
// 将待发数据传入帧数据
memcpy(p_msg->fdata,din,len);
2023-05-18 00:58:26 +00:00
// 将打包后帧数据传入buff
2023-05-11 03:33:47 +00:00
memcpy(dout,p_msg,fralen);
// ! 释放内存
2023-05-18 00:58:26 +00:00
// free(p_msg);
// p_msg=NULL;
2023-05-11 03:33:47 +00:00
return fralen;
}
2023-05-18 00:58:26 +00:00
/**
*
* @param cfg MSG格式配置文件
* @param fin
* @param dout
* @return
*/
size_t pack_File(MSG *cfg, const char *fin, char (*dout)[200])
2023-05-11 03:33:47 +00:00
{
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;
}
}
2023-05-18 00:58:26 +00:00
rt_uint8_t packdata[file_size/flen+1][flen];
2023-05-11 03:33:47 +00:00
buffer = (rt_uint8_t *) malloc(file_size);
2023-05-18 00:58:26 +00:00
2023-05-11 03:33:47 +00:00
for (int var = 0; var < file_size; ++flen)
{
2023-05-18 00:58:26 +00:00
packMsg(cfg,buffer+var,flen,packdata[var]);
2023-05-11 03:33:47 +00:00
}
_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() {
}