移植工程到新PCB

ttmsg.c 基本工作,但是遇到malloc内存后地址被篡改的问题,未定位。
This commit is contained in:
murmur 2023-05-23 14:59:35 +08:00
parent 3db47098e7
commit bde51765d0
10 changed files with 1351 additions and 171 deletions

1005
.config Normal file

File diff suppressed because it is too large Load Diff

View File

@ -157,7 +157,10 @@ _exit:
return ret;
}
static int compress_file(int fd_in, int *fout)
{
}
static int fastlz_decompress_file(int fd_in, int fd_out)
{
/* Start to decompress file */

View File

@ -55,9 +55,9 @@ int aes_128_cbc_pkcs7(rt_uint8_t *data, rt_uint16_t len, rt_uint8_t *dout)
unsigned char edata[cnt];//输出buff
mbedtls_aes_context ctx; //创建结构体
mbedtls_aes_init(&ctx); //初始化
int rst = mbedtls_aes_setkey_enc(&ctx, key, KEY_LEN * 8); //设置密钥
mbedtls_aes_setkey_enc(&ctx, key, KEY_LEN * 8); //设置密钥
// rt_kprintf("--%d\n", rst);
rst = mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, (m + 1) * 16, iv, pbuff, edata); //加密
mbedtls_aes_crypt_cbc(&ctx, MBEDTLS_AES_ENCRYPT, (m + 1) * 16, iv, pbuff, edata); //加密
// rt_kprintf("--%d\n", rst);
mbedtls_aes_free(&ctx); //释放结构体
@ -154,7 +154,7 @@ void aes_file(const char *fin, const char *fout)
close(fd_out);
}
return RT_EOK;
// return RT_EOK;
}
void aes_file_test(int argc, char **argv)

View File

@ -143,6 +143,7 @@ static void tt_parse(rt_uint8_t *data)
// rt_kprintf("%5s%5s%5s%5s%15s%15s%10s\n", TTinfo->sim, TTinfo->xh, TTinfo->rw,
// TTinfo->jh, TTinfo->jd, TTinfo->wd,TTinfo->ele);
LOG_D("Get RAW data done.");
cJSON_Delete(root);
return RT_EOK;
}
@ -174,8 +175,8 @@ static int webclient_get_data(const char *url)
return -RT_ERROR;
}
// LOG_D("webclient GET request response data :");
// LOG_D("%s", buffer);
LOG_D("webclient GET request response data :");
LOG_D("%s", buffer);
tt_parse(buffer);
if (buffer)
@ -205,11 +206,12 @@ void getTTinfo_thread_entry(void* parameter)
url = web_strdup(*(infoH + var));
if (url == RT_NULL)
{
rt_kprintf("no memory for create getTT url buffer.\n");
return -RT_ENOMEM;
LOG_E("no memory for create getTT url buffer.\n");
// return -RT_ENOMEM;
}
webclient_get_data(url);
web_free(url);
rt_thread_mdelay(100);
}
// rt_kprintf("%5d++%5d --%5d\n",TTinfo->xh,tmp.xh,TTinfo->sim);
// rt_kprintf("%5d%5d%5d%5d%15s%15s%10s\n", TTinfo.sim, TTinfo.xh, TTinfo.rw, TTinfo.jh, TTinfo.jd, TTinfo.wd,TTinfo.ele);

View File

@ -80,4 +80,4 @@ static int iwdg_sample(int argc, char *argv[])
/* 导出到 msh 命令列表中 */
//MSH_CMD_EXPORT(iwdg_sample, iwdg sample);
/* 导出到自动初始化 */
INIT_DEVICE_EXPORT(iwdg_sample);
//INIT_DEVICE_EXPORT(iwdg_sample);

View File

@ -20,33 +20,18 @@
* @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.
* @Description:
*
* Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
*/
#include <dfs_file.h>
// #include <dfs_file.h>
#define LOG_TAG "ttmsg"
#define LOG_LVL LOG_LVL_DBG
#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
@ -56,29 +41,61 @@ MSG msgs_tpl={
* @param out
* @return size_t
*/
#ifdef USE_STRTOK
static 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];
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);
}
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]);
}
// 更新out
for (size_t ii = 0; ii < i; ii++)
{
// out[ii] = id[ii];
strcpy(out[ii], rst[ii]);
}
return i;
return i;
}
#else
// strtok_r版本
static size_t split(const char *str, const char *delim, char (*out)[STR_LEN_MAX])
{
char buf[50];
strcpy(buf, str); // strtok会修改字符串
char *token = NULL;
char *saveptr = NULL;
char *rst[STR_LEN_MAX];
int idx = 0;
token = strtok_r(buf, delim, &saveptr);
while (token)
{
LOG_D("---%s---",token);
rst[idx++] = token;
token = strtok_r(NULL, delim, &saveptr);
}
// 更新out
for (size_t ii = 0; ii < idx; ii++)
{
// out[ii] = id[ii];
strcpy(out[ii], rst[ii]);
}
return idx;
}
#endif
/**
@ -90,32 +107,34 @@ static size_t split(const char *str, const char *deli, char (*out)[STR_LEN_MAX])
*/
static size_t getID(const char *str, unsigned char *out)
{
char id[10][STR_LEN_MAX] = {};
// 分割路径
size_t len = split(str, "/", id);
char id[10][STR_LEN_MAX] = {};
// 分割路径
size_t len = split(str, "/", id);
// 分割后缀
len = split(id[len - 1], ".", id);
// 分割后缀
LOG_D("file is %s",id[len-1]);
len = split(id[len - 1], ".", id);
// 分割时间
len = split(id[0], "_", id);
// 分割时间
LOG_D("file name is %s",id[0]);
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)
unsigned char tmp[len];
for (size_t i = 0; i < len; i++)
{
v = v - 2000;
unsigned int v = atoi(id[i]);
// 去除代表20的2000
if (v > 0xff)
{
v = v - 2000;
}
tmp[i] = v;
}
tmp[i] = v;
}
memcpy(out, tmp, len);
return len;
LOG_HEX("file ID",16,tmp,len);
memcpy(out, tmp, len);
return len;
}
/**
@ -127,148 +146,269 @@ static size_t getID(const char *str, unsigned char *out)
*/
rt_uint8_t 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},
// memset(tpl, 0, sizeof(MSG)); // 分配空间
// 模板
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},
.ftccid = {0x37, 0x32, 0x32, 0x32},
// .findex = {0x00,0x01,0x02,0x03,0x04,0x05},
.fcfg = flag,
.fcfg = flag,
};
rt_memcpy(tpl, &msgs_tpl, sizeof(MSG));
// 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);
size_t len = getID(fin, tpl->findex);
if (len != 7)
{
LOG_E("file name %s format error", fin);
return RT_ERROR;
}
memcpy(tpl,&msgs_tpl,sizof(msgs_tpl));
return RT_EOK;
}
/**
* @description: MSG帧格式拼接din数据后输出到dout
* @param *p_msg MSG格式配置文件
* @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
* @param {rt_uint8_t} *dout
* @return {*}
*/
rt_uint8_t packMsg(MSG *p_msg, rt_uint8_t *din, rt_uint8_t len, rt_uint8_t *dout)
{
rt_uint8_t fralen=sizeof(MSG)+len;//len of current frame
rt_uint8_t fralen = sizeof(MSG) + len; // len of current frame
// 计算各字段
p_msg->fdlen[0]=0;//单包长度必不会超过0xFF故高字节始终为0
p_msg->fdlen[1]= fralen - 13;//总长度-帧头长度
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);
rt_memcpy(p_msg->fdata, din, len);
// 将打包后帧数据传入buff
memcpy(dout,p_msg,fralen);
rt_memcpy(dout, p_msg, fralen);
// ! 释放内存
// free(p_msg);
// p_msg=NULL;
return fralen;
}
/**
* @description: din按指定长度w分割MSG帧格式拼接
* @param *cfg MSG格式配置文件
* @param {rt_uint8_t} *din
* @param {rt_uint8_t} len
* @param {rt_uint8_t} w
* @param {rt_uint8_t} *dout
* @return {*}
*/
size_t packMsgs(MSG *cfg, rt_uint8_t *din, size_t len, rt_uint8_t w, rt_uint8_t (*dout)[200], rt_uint8_t *arrlen)
{
if (!(w > 0)) // 无效长度
{
w = len;
}
rt_uint8_t row = (len / w) + (len % w > 0 ? 1 : 0);
// rt_uint8_t packdata[row][200]; // 打包后数据
rt_uint8_t tlen[row]; // 打包后数据各维长度
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)
{
size_t tmplen = len - var; // 当前待打包数据的长度
tmplen = tmplen > w ? w : tmplen; // 判断最后一包数据的长度
cfg->fcurpiece[0] = index+1;
size_t col = packMsg(cfg, din+var, tmplen, dout[index]);
tlen[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);
rt_thread_mdelay(100);
}
// for (size_t i = 0; i < row; i++)
// {
// memcpy(dout[i], packdata[i], tlen[i]);
// }
memcpy(arrlen, tlen, row);
// LOG_D("--addr is %d",din);
return row;
}
size_t demo (uint8_t *din)
{
size_t s= 1024*10;
rt_uint8_t *buf = rt_malloc(s);
if (buf!=RT_NULL) {
LOG_D("mem ok, addr is %d",buf);
LOG_D("addr is %d",rt_memset(buf, 0xab, s));
}
else {
LOG_E("no mem.");
}
for (size_t var = 0; var < s; ++ var) {
rt_memcpy(buf+var, din+var, 1);
}
LOG_HEX("buf",16,buf,100);
LOG_D("addr is %d",din);
if (buf) {
rt_free(buf);
}
return 1;
}
/**
*
* @param cfg MSG格式配置文件
* @param *cfg MSG格式配置文件
* @param fin
* @param flag
* @param dout
* @param arrlen
* @return
*/
size_t pack_File(MSG *cfg, const char *fin, char (*dout)[200])
size_t pack_File(const char *fin, rt_uint8_t flag, char (*dout)[200], char *arrLen)
{
int fd_in = -1, fd_out = -1;
int ret = 0;
MSG cfg;
rt_memset(&cfg, 0, sizeof(MSG)); // 分配空间
rt_uint8_t rst = packInit(&cfg, fin, flag);
if (rst!= RT_EOK) {
return 0;
}
size_t file_size = 0;
rt_uint8_t *buffer = RT_NULL;
size_t flen = 150; //每包数据初始长度,最大不超过 FRAME_DATA_LEN_MAX
int fd_in = -1; //, fd_out = -1;
static size_t file_size = 0;
// rt_uint8_t *buffer = RT_NULL;
size_t flen = 150; // 每包数据初始长度,最大不超过 FRAME_DATA_LEN_MAX
size_t len = 0;
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;
LOG_E("open the input file : %s error!\n", fin);
close(fd_in);
return len;
}
// 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);
LOG_I("file size is %d bytes.",file_size);
lseek(fd_in, 0, SEEK_SET);
//粗略调整每包大小
while (file_size % flen < flen / 1.2 && flen < FRAME_DATA_LEN_MAX) //阈值约83%
{
flen += 1;
rt_uint8_t *buffer = rt_malloc(file_size);
if (buffer == RT_NULL) {
LOG_E("No memory for read %s.",fin);
return 0;
}
if (file_size % flen < flen / 1.2)
{ //到达限制仍不符合要求
flen = 150;
while (file_size % flen < flen / 2.5 && flen < FRAME_DATA_LEN_MAX) //调整阈值为40%
// rt_uint8_t buffer[file_size];
rt_memset(buffer, 0x00, file_size);
read(fd_in, buffer, file_size);
close(fd_in);
// 粗略调整每包大小
if (file_size > FRAME_DATA_LEN_MAX) {
while (file_size % flen < flen / 1.2 && flen < FRAME_DATA_LEN_MAX) // 阈值约83%
{
flen += 1;
}
}
rt_uint8_t packdata[file_size/flen+1][flen];
buffer = (rt_uint8_t *) malloc(file_size);
for (int var = 0; var < file_size; ++flen)
{
packMsg(cfg,buffer+var,flen,packdata[var]);
}
_exit:
if(fd_in >= 0)
{
close(fd_in);
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("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));
// LOG_HEX("BEFORE:",16,buffer,file_size);
LOG_D("1-addr is %p",buffer);
// if(fd_out >= 0)
// {
// close(fd_out);
// }
// if (cmprs_buffer)
// {
// free(cmprs_buffer);
// }
//
// if (buffer)
// {
// free(buffer);
// }
// return ret;
len = packMsgs(&cfg, buffer, file_size, flen, dout, arrLen);
LOG_D("2-addr is %p",buffer);
// len = demo(buffer);
}
LOG_D("len is %d",len);
LOG_I("file size is %d bytes.",file_size);
// LOG_HEX("AFTER:",16,buffer,file_size);
LOG_D("addr is %p",buffer);
void demomsg() {
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.");
}
return len;
}
void pf_thread_entry(void* parameter)
{
char *f = (char *) parameter;
rt_uint8_t d[50][200];
rt_uint8_t s[50];
size_t len = pack_File(f, 0, d, s);
if (len)
{
LOG_I("Done.");
}
}
void pmsg(int argc, char **argv)
{
if (argc == 2)
{
static char c[STR_LEN_MAX];
strcpy(c, argv[1]);
/* 创建线程 */
rt_thread_t thread = rt_thread_create("pmsg", pf_thread_entry, (void *) c, 1024 * 30, 25, 10);
/* 创建成功则启动线程 */
if (thread != RT_NULL)
{
rt_thread_startup(thread);
// rt_kprintf("done");
}
else
{
LOG_E("thread 'pmsg' create failure.");
return RT_ERROR;
}
}
else
{
rt_kprintf("Usage:\n");
rt_kprintf("pmsg [input_file] pack \"input_file\" \n");
}
}
#include <finsh.h>
MSH_CMD_EXPORT(pmsg, );

View File

@ -11,15 +11,18 @@
#define APPLICATIONS_TTMSG_TTMSG_H_
#include <rtthread.h>
#include <stdio.h>
#include <stdlib.h>
//#include <stdio.h>
//#include <stdlib.h>
#include <stdint.h>
#include <string.h>
//#include <stdint.h>
//#include <string.h>
//#include "posix/string.h"
#include <dfs_file.h>
#define FRAME_DATA_LEN_MAX 180
#define STR_LEN_MAX 30
/*
typedef enum{
//
@ -46,8 +49,30 @@ typedef enum{
//
}fopt_e;
*/
//控制字,总两字节
//加密低2位
#define CRY_NONE 0
#define CRY_AES 1
#define CRY_RSA 2
#pragma pack(1)
//压缩次高2位
#define COMP_NONE (0<<2)
#define COMP_FAST_LZ (1<<2)//压缩率中等demo约1/0.62,资源占用少
#define COMP_QUICK_LZ (2<<2)
#define COMP_LZMA (3<<2)//压缩率可能最高,样例报错还未定位,猜测是内存不足
//数据类型子类随CTRL_MODE不同而不同共3位8种
#define MODE_0 (0<<4)
#define MODE_1 (1<<4)
// 。。。
#define MODE_7 (7<<4)
//数据类型,最高位
#define CTRL_MODE (0<<7)
#define DATA_MODE (1<<7)
//#pragma pack(1)
typedef struct{
//帧头
//格式是TT厂家确定的厂家不变则不会变13字节
@ -71,7 +96,7 @@ typedef struct{
}MSG;
rt_uint8_t packMsg(MSG *p_msg, rt_uint8_t *din, rt_uint8_t len, rt_uint8_t *dout);
//rt_uint8_t packMsg(MSG *p_msg, rt_uint8_t *din, rt_uint8_t len, rt_uint8_t *dout);
#endif /* APPLICATIONS_TTMSG_TTMSG_H_ */

View File

@ -13,7 +13,7 @@
//#define LOG_LVL LOG_LVL_DBG
//#include <ulog.h>
#define DBG_TAG "main"
#define DBG_TAG "w25q"
#define DBG_LVL DBG_LOG
#include <rtdbg.h>
@ -23,8 +23,8 @@
int rt_hw_spi_flash_init(void)
{
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
rt_hw_spi_device_attach("spi1", "spi10", GPIOG, GPIO_PIN_8);
// __HAL_RCC_GPIOD_CLK_ENABLE();
rt_hw_spi_device_attach("spi2", "spi10", GPIOB, GPIO_PIN_12);
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
{

View File

@ -46,13 +46,13 @@ RT_WEAK void rt_hw_board_init()
void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(hspi->Instance==SPI1)
if(hspi->Instance==SPI2)
{
/* USER CODE BEGIN SPI1_MspInit 0 */
/* USER CODE END SPI1_MspInit 0 */
/* Peripheral clock enable */
__HAL_RCC_SPI1_CLK_ENABLE();
__HAL_RCC_SPI2_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
@ -61,12 +61,17 @@ void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
PB4 ------> SPI1_MISO
PB5 ------> SPI1_MOSI
*/
/**SPI2 GPIO Configuration
PB13 ------> SPI1_SCK
PB14 ------> SPI1_MISO
PB15 ------> SPI1_MOSI
*/
GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5;
GPIO_InitStruct.Pin = GPIO_PIN_13|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
GPIO_InitStruct.Alternate = GPIO_AF5_SPI2;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN SPI1_MspInit 1 */

View File

@ -68,8 +68,8 @@ extern "C"
*/
#define BSP_USING_UART1
#define BSP_UART1_TX_PIN "PA9"
#define BSP_UART1_RX_PIN "PA10"
#define BSP_UART1_TX_PIN "PB6"
#define BSP_UART1_RX_PIN "PB7"
#define BSP_USING_UART3
#define BSP_UART3_TX_PIN "PB10"
@ -123,8 +123,8 @@ extern "C"
* such as #define HAL_SPI_MODULE_ENABLED
*/
#define BSP_USING_SPI1
/*#define BSP_USING_SPI2*/
#define BSP_USING_SPI2
/*#define BSP_USING_SPI1*/
/*#define BSP_USING_SPI3*/
/*-------------------------- SPI CONFIG END --------------------------*/
@ -372,7 +372,7 @@ extern "C"
#define LED_HEART GET_PIN(E,3)
#ifndef ETH_RESET_PIN
#define ETH_RESET_PIN GET_PIN(E, 7)//71
#define ETH_RESET_PIN GET_PIN(A, 4)//E-7
#endif
////#define RESET_LB GET_PIN(E, 1)
////#define RESET_UB GET_PIN(E, 0)