195 lines
4.3 KiB
C
195 lines
4.3 KiB
C
|
|
|
|
#include <rtthread.h>
|
|
#define LOG_TAG "tool"
|
|
#define LOG_LVL LOG_LVL_DBG
|
|
#include <ulog.h>
|
|
//#include <cJSON.h>
|
|
#include <dfs_file.h>
|
|
#include <usrcfg.h>
|
|
|
|
typedef struct
|
|
{
|
|
int sendInterval;
|
|
int maxTTWaitTime;
|
|
int maxTTRetryCnt;
|
|
int minTTPeriCnt;
|
|
int minTTsinal;
|
|
} SYS_CFG;
|
|
|
|
//#define _CFGALL
|
|
#ifdef _CFGALL
|
|
|
|
SYS_CFG scfg={
|
|
.sendInterval =60,
|
|
.maxTTWaitTime = 4,
|
|
.maxTTRetryCnt = 3,
|
|
.minTTPeriCnt=5,
|
|
.minTTsinal=5,
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
* 更新全局参数
|
|
*/
|
|
void updatecfg(void)
|
|
{
|
|
//因为不知原因,采用事件集独立更新配置出错
|
|
//独立响应单个参数更新事件,程序上更复杂也没特别必要
|
|
//现采用事件通知、统一全部重新加载
|
|
while(1)
|
|
{
|
|
maxTTWaitTime = get_cfg("maxTTWaitTime");
|
|
maxTTRetryCnt = get_cfg("maxTTRetryCnt");
|
|
minTTPeriCnt = get_cfg("minTTPeriCnt");
|
|
minTTsinal = get_cfg("minTTsinal");
|
|
}
|
|
}
|
|
#endif
|
|
|
|
|
|
/**
|
|
* 以HEX显示文件内容
|
|
*/
|
|
void hexFile_thread_entry(void* parameter)
|
|
{
|
|
int fd_in = -1;
|
|
int block_size = 0;
|
|
char *fin = (char *)parameter;
|
|
fd_in = open(fin, O_RDONLY, 0);
|
|
if (fd_in < 0)
|
|
{
|
|
LOG_E("[hex] open the input file : %s error.", fin);
|
|
return RT_ERROR;
|
|
}
|
|
size_t file_size = lseek(fd_in, 0, SEEK_END);
|
|
lseek(fd_in, 0, SEEK_SET);
|
|
|
|
rt_uint8_t *buffer = RT_NULL;
|
|
#define READ_BUFFER_SIZE 512
|
|
buffer = (rt_uint8_t *) malloc(READ_BUFFER_SIZE);
|
|
rt_kprintf("------Start------\n");
|
|
for (size_t i = 0; i < file_size; i += READ_BUFFER_SIZE)
|
|
{
|
|
if ((file_size - i) < READ_BUFFER_SIZE)
|
|
{
|
|
block_size = file_size - i;
|
|
}
|
|
else
|
|
{
|
|
block_size = READ_BUFFER_SIZE;
|
|
}
|
|
|
|
memset(buffer, 0x00, READ_BUFFER_SIZE);
|
|
read(fd_in, buffer, block_size);
|
|
LOG_HEX("hex_file",27,buffer,block_size);
|
|
}
|
|
|
|
rt_free(buffer);
|
|
close(fd_in);
|
|
rt_kprintf("------Done.------\n");
|
|
}
|
|
|
|
void hexFile(int argc, char **argv)
|
|
{
|
|
|
|
if (argc == 2)
|
|
{
|
|
static char f[30];
|
|
rt_strcpy(f,argv[1]);
|
|
/* 创建线程 */
|
|
rt_thread_t thread = rt_thread_create("hex_file", hexFile_thread_entry, (void *) f, 1024 * 1, 25, 10);
|
|
/* 创建成功则启动线程 */
|
|
if (thread != RT_NULL)
|
|
{
|
|
rt_thread_startup(thread);
|
|
}
|
|
else
|
|
{
|
|
LOG_E("thread 'hex_file' create failure.");
|
|
return RT_ERROR;
|
|
}
|
|
}
|
|
}
|
|
MSH_CMD_EXPORT(hexFile,以HEX方式显示文件内容);
|
|
|
|
/**
|
|
* 将当前时间转为字符串
|
|
* @param str 字符串buffer
|
|
* @return 字符串长度
|
|
*/
|
|
int time2Str(char *str)
|
|
{
|
|
struct tm *tm, tm_tmp;
|
|
time_t now = time(RT_NULL);
|
|
time_t t = (time_t)0;
|
|
|
|
if (gettimeofday(&now, RT_NULL) >= 0)
|
|
{
|
|
// t = now.tv_sec;
|
|
}
|
|
tm = localtime_r(&t, &tm_tmp);
|
|
|
|
// static time_t now;
|
|
// static struct tm *tm, tm_tmp;
|
|
//
|
|
// now = time(RT_NULL);
|
|
// tm = gmtime_r(&now, &tm_tmp);
|
|
|
|
/* show the time format MM-DD HH:MM:SS */
|
|
size_t len = rt_snprintf(str, 20, "%04d_%02d_%02d_%02d_%02d_%02d_%03d", tm->tm_year, tm->tm_mon,
|
|
tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
|
|
str[len]='\0';
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void sDemo()
|
|
{
|
|
// extern struct rt_event sw_check;//软件条件
|
|
// rt_event_send(&sw_check, FILE_IS_OK);
|
|
void upSWflag(void);
|
|
upSWflag();
|
|
}
|
|
|
|
MSH_CMD_EXPORT(sDemo,喂文件数据);
|
|
|
|
static struct rt_thread infoTT_thread;
|
|
void ttinfoInit()
|
|
{
|
|
static CFG cfg;
|
|
memset(&cfg, 0, sizeof(CFG));
|
|
|
|
cfg.s = 1;
|
|
cfg.cnt = 10; //避免通信异常
|
|
extern void getTTinfo_thread_entry(void* parameter);
|
|
// extern void getTTinfo_thread_entry(void* parameter);
|
|
// struct rt_thread infoTT_thread;
|
|
ALIGN(RT_ALIGN_SIZE)
|
|
static char thread2_stack[1024 * 3];
|
|
rt_thread_init(&infoTT_thread, "getTT", getTTinfo_thread_entry, (void *) &cfg, &thread2_stack[0],
|
|
sizeof(thread2_stack), 25, 10);
|
|
// rt_thread_startup(&infoTT_thread);
|
|
}
|
|
|
|
void startTTinfo(void)
|
|
{
|
|
if (&infoTT_thread == RT_NULL) {
|
|
//to Init
|
|
}
|
|
if ((infoTT_thread.stat & RT_THREAD_STAT_MASK) == RT_THREAD_INIT) {
|
|
|
|
}
|
|
rt_thread_startup(&infoTT_thread);
|
|
}
|
|
//MSH_CMD_EXPORT(ttinfoInit,init ttinfo);
|
|
//INIT_COMPONENT_EXPORT(ttinfoInit);
|
|
|