func.c 更新upSend函数,屏蔽打包操作

tools.c 更新time2Str函数 添加isInByte函数
ttmsg.c 屏蔽多处错误调用tcpInit(),更新匹配ack的逻辑
This commit is contained in:
CSSC-WORK\murmur 2023-07-20 15:29:37 +08:00
parent a1e89cd86b
commit e8a8606bf3
6 changed files with 162 additions and 65 deletions

View File

@ -78,12 +78,12 @@ static void getFileData_thread_entry(void)
static rt_uint8_t d[10][200] = { }; static rt_uint8_t d[10][200] = { };
static rt_uint8_t s[10] = { }; static rt_uint8_t s[10] = { };
while (tcpInit() != RT_EOK) // while (tcpInit() != RT_EOK)
{ // {
LOG_W("TT server is not ready."); // LOG_W("TT server is not ready.");
rt_thread_mdelay(100); // rt_thread_mdelay(100);
} // }
LOG_D("TT server is ready."); // LOG_D("TT server is ready.");
while (1) while (1)
{ {
@ -131,6 +131,7 @@ static void gg(void)
} }
} }
static void try() static void try()
{ {
static rt_err_t rst = RT_ERROR; static rt_err_t rst = RT_ERROR;
@ -209,7 +210,7 @@ static void try()
} }
} }
MSH_CMD_EXPORT(gg, file);
void smsg() void smsg()
{ {
@ -227,4 +228,42 @@ void smsg()
} }
} }
//INIT_COMPONENT_EXPORT(smsg); //INIT_COMPONENT_EXPORT(smsg);
//tcp连接保活
//实际场景不存在中间断掉的可能
void iniCnt()
{
rt_hw_stm32_eth_init(); //激活网口
rt_thread_mdelay(5000);
while (1 && !isTCPok()) //判断TCP连接是否正常异常自动重连
{
// while (!isTCPok())
{
LOG_W("TT server is not ready.");
tcpInit();
LOG_D("s=%d",isTCPok());
rt_thread_mdelay(5000);
}
{
LOG_D("TT server is ready.");
tcpRecMQ();
recTT();
rt_thread_mdelay(1000);
}
}
}
void deiniCnt()
{
rt_hw_stm32_eth_deinit(); //qu激活网口
}
#define FUNC_DEMO
#ifdef FUNC_DEMO //测试时导出命令到控制台
MSH_CMD_EXPORT(smsg, smsg); MSH_CMD_EXPORT(smsg, smsg);
MSH_CMD_EXPORT(gg, file);
MSH_CMD_EXPORT(iniCnt,tcp连接);
MSH_CMD_EXPORT(deiniCnt,tcp连接);
#endif

View File

@ -92,6 +92,11 @@ return 0;
*/ */
RT_WEAK int upSend(uint8_t *din, size_t len) RT_WEAK int upSend(uint8_t *din, size_t len)
{ {
//此函数有打包操作,需线程操作
LOG_D("upsend.");
return 0;
uint8_t dout[200]; uint8_t dout[200];
//打包数据 //打包数据
static MSG cfg; static MSG cfg;
@ -100,7 +105,7 @@ RT_WEAK int upSend(uint8_t *din, size_t len)
time2Str(fin); time2Str(fin);
packInit(&cfg, fin, 0); //写入配置 packInit(&cfg, fin, 0); //写入配置
size_t rst = packMsg(&cfg, din, len, dout); size_t rst = packMsg(&cfg, din, len, dout);
LOG_HEX("upSend", 16, din, rst); LOG_HEX("upSend", 16, dout, rst);
return 0; return 0;
} }
@ -109,6 +114,7 @@ RT_WEAK int upSend(uint8_t *din, size_t len)
*/ */
void selfTest() void selfTest()
{ {
LOG_D("FUNC = selftest");
rt_uint8_t rst[100]={0x5A, 0xA5, 0x32, 0x3E, 0x0A, 0x41}; rt_uint8_t rst[100]={0x5A, 0xA5, 0x32, 0x3E, 0x0A, 0x41};
int p = 6; int p = 6;
rt_uint8_t sysSta=1,xh=0,jh=0,commSpeed=0; rt_uint8_t sysSta=1,xh=0,jh=0,commSpeed=0;
@ -498,9 +504,10 @@ void ttRunCMD(uint8_t *din, size_t len)
} }
RT_WEAK xpParse(uint8_t * din, size_t len) RT_WEAK int xpParse(uint8_t * din, size_t len)
{ {
LOG_D("直接调用小彭的函数进行处理。"); LOG_D("直接调用小彭的函数进行处理。");
return 0;
} }
//原计划将指令粗解析放在上位机考虑到上位机到位时间晚现放到MCU端 //原计划将指令粗解析放在上位机考虑到上位机到位时间晚现放到MCU端
@ -541,7 +548,7 @@ void parse3SData(uint8_t *din, size_t count)
// uint8_t dout[200]; // uint8_t dout[200];
// size_t len=0; // size_t len=0;
if (din[3] == ADDR_TT)//仅给TT的消息 if (din[2] == ADDR_TT)//仅给TT的消息
{ {
ttRunCMD(din,count); ttRunCMD(din,count);
} }
@ -592,7 +599,16 @@ void parseTTData(uint8_t *din, size_t len)
*/ */
uint8_t head[]={0x88,0xAA,0xBB,0x88, 0x00,0x01, 0x00,0x22, 0x70,0x21, 0x00,0xaa, 0x00, 0x27,0x22,0x22,0x22 }; uint8_t head[]={0x88,0xAA,0xBB,0x88, 0x00,0x01, 0x00,0x22, 0x70,0x21, 0x00,0xaa, 0x00, 0x27,0x22,0x22,0x22 };
//fstart[4] fnum[2]  bak[2]  ftype[2]  fdlen[2]  fcrc[1] ftccid[4] //fstart[4] fnum[2]  bak[2]  ftype[2]  fdlen[2]  fcrc[1] ftccid[4]
uint8_t rst = memcmp(din,head,sizeof(head)); // uint8_t rst[10];
// size_t n=isInByte(din, len, head, 10, rst);
// uint8_t ndin[200];
// for (size_t i = 0; i < n; i++)
// {
// int n=rst[i+1]
// memcpy(ndin,din,rst[i+1]-rst[i]);
// }
uint8_t rst = memcmp(din,head,10);//只比较到ftype
if (rst) if (rst)
{ {
LOG_W("帧头不匹配"); LOG_W("帧头不匹配");
@ -638,7 +654,12 @@ void parseTTData(uint8_t *din, size_t len)
} }
/**
*
* @param din
* @param len
* @return 0---1--
*/
int cacheData(uint8_t *din, size_t len) int cacheData(uint8_t *din, size_t len)
{ {
static int cnt = 0; static int cnt = 0;
@ -680,7 +701,7 @@ int cacheData(uint8_t *din, size_t len)
if (fd < 0) if (fd < 0)
{ {
LOG_E("open file %s failed!", f); LOG_E("open file %s failed!", f);
return RT_ERROR; return -RT_ERROR;
} }
else else
{ {
@ -689,7 +710,7 @@ int cacheData(uint8_t *din, size_t len)
{ {
LOG_E("write to file %s failed!", f); LOG_E("write to file %s failed!", f);
close(fd); close(fd);
return RT_ERROR; return -RT_ERROR;
} }
cnt += len; cnt += len;
close(fd); close(fd);

View File

@ -127,7 +127,11 @@ MSH_CMD_EXPORT(rt_hw_stm32_eth_init, 初始化网络。);
void show_version(void) void show_version(void)
{ {
rt_kprintf("SW Version: %s\n","1.41"); char str[30];
uint8_t t[10];
size_t len=time2Byte(t);
bytes2str(t, len, 10, "", str);
rt_kprintf("SW Version: %s build%s\n","1.43",bytes2str(t, len, 10, "", str));
} }
MSH_CMD_EXPORT(show_version,); MSH_CMD_EXPORT(show_version,);

View File

@ -143,14 +143,14 @@ char *bytes2str(uint8_t *din, size_t count, int radix, char *sep, char *str);
* @param str buffer * @param str buffer
* @return * @return
*/ */
int time2Str(char *str) char *time2Str(char *str)
{ {
uint8_t t[10]; uint8_t t[10];
size_t len=time2Byte(t); size_t len=time2Byte(t);
bytes2str(t, len, 10, "_", str); bytes2str(t, len, 10, "_", str);
len = strlen(str); len = strlen(str);
str[len]='\0'; str[len]='\0';
return len; return str;
} }
@ -334,7 +334,31 @@ uint8_t bccCRC(uint8_t *din, size_t count)
} }
/**
* @brief
*
* @param din
* @param len
* @param s
* @param slen
* @param dout
* @return int
*/
size_t isInByte(uint8_t *din, size_t len, uint8_t *s, size_t slen, uint8_t *dout)
{
size_t cnt = 0;
uint8_t tmp[len];
for (size_t i = 0; i < len - slen+1; i++)
{
if (memcmp(din + i, s, slen) == 0)
{
tmp[cnt++] = i;
i = i+slen-1;
}
}
memcpy(dout,tmp,cnt);
return cnt;
}
@ -389,6 +413,7 @@ void sysInit()
cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO); cfgUpdate = rt_sem_create("cfgUpdate", 0, RT_IPC_FLAG_PRIO);
rt_sem_release(cfgUpdate);//上电更新值 rt_sem_release(cfgUpdate);//上电更新值
// rt_hw_stm32_eth_init();//激活网口 // rt_hw_stm32_eth_init();//激活网口
// LOG_D("sysInit");
} }
INIT_APP_EXPORT(sysInit); INIT_APP_EXPORT(sysInit);

View File

@ -27,7 +27,7 @@
#define PORT 8005 #define PORT 8005
static int sock=0; static int sock=0;
static int flag=0;
void tcpClose(void) void tcpClose(void)
{ {
/* 关闭这个连接 */ /* 关闭这个连接 */
@ -38,48 +38,49 @@ void tcpClose(void)
} }
int isTCPok(void) int isTCPok(void)
{ {
return sock; return flag;
} }
int tcpInit(void) int tcpInit(void)
{ {
int ret; int ret;
const char *url = URL;
int port = PORT;
const char *url = URL; /* 通过函数入口参数url获得host地址如果是域名会做域名解析 */
int port = PORT; struct hostent *host;
host = gethostbyname(url);
/* 通过函数入口参数url获得host地址如果是域名会做域名解析 */ /* 创建一个socket类型是SOCKET_STREAMTCP类型 */
struct hostent *host; if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1)
host = gethostbyname(url); {
/* 创建socket失败 */
LOG_E("Socket error");
/* 创建一个socket类型是SOCKET_STREAMTCP类型 */ return RT_ERROR;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) == -1) }
{
/* 创建socket失败 */
LOG_E("Socket error");
return RT_ERROR; /* 初始化预连接的服务端地址 */
} struct sockaddr_in server_addr;
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *) host->h_addr);
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero));
/* 初始化预连接的服务端地址 */ /* 连接到服务端 */
struct sockaddr_in server_addr; if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1)
server_addr.sin_family = AF_INET; {
server_addr.sin_port = htons(port); /* 连接失败 */
server_addr.sin_addr = *((struct in_addr *)host->h_addr); LOG_E("Connect fail!\n");
rt_memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); tcpClose();
/* 连接到服务端 */ return RT_ERROR;
if (connect(sock, (struct sockaddr *)&server_addr, sizeof(struct sockaddr)) == -1) }
{ else
/* 连接失败 */ {
LOG_E("Connect fail!\n"); // tcpRecMQ();
tcpClose(); flag=1;
return RT_ERROR;
}
else {
tcpRecMQ();
return RT_EOK; return RT_EOK;
} }
} }
@ -166,12 +167,13 @@ void tcpRecMQ_thread_entry(void)
// /* 释放接收缓冲 */ // /* 释放接收缓冲 */
// break; // break;
// } // }
// LOG_D("%d Bytes received.",msg.size);
if (msg.size >0 ) if (msg.size >0 )
{ {
// LOG_D("%d Bytes received.",msg.size); // LOG_D("%d Bytes received.",msg.size);
// LOG_HEX("tcpTT", 16, msg.data, msg.size); // LOG_HEX("tcpTT", 16, msg.data, msg.size);
int result = rt_mq_send(&TTrx_mq, &msg, sizeof(msg)); int result = rt_mq_send(&TTrx_mq, &msg, sizeof(msg));
if ( result == -RT_EFULL) if (result == -RT_EFULL)
{ {
/* 消息队列满 */ /* 消息队列满 */
LOG_W("message queue full."); LOG_W("message queue full.");

View File

@ -526,12 +526,12 @@ MSH_CMD_EXPORT(swTT, 切换TT电源。不带参切换电源状态支持参数
extern struct rt_messagequeue TTrx_mq; extern struct rt_messagequeue TTrx_mq;
void recTT_thread_entry() void recTT_thread_entry()
{ {
while(tcpInit() != RT_EOK) // while(tcpInit() != RT_EOK)
{ // {
LOG_W("TT server is not ready."); // LOG_W("TT server is not ready.");
rt_thread_mdelay(100); // rt_thread_mdelay(100);
} // }
LOG_D("TT server is ready."); // LOG_D("TT server is ready.");
static TTRx_MSG msg; static TTRx_MSG msg;
while (1) while (1)
{ {
@ -539,23 +539,29 @@ void recTT_thread_entry()
rt_memset(&msg, 0, sizeof(msg)); rt_memset(&msg, 0, sizeof(msg));
if (rt_mq_recv(&TTrx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER) == RT_EOK) if (rt_mq_recv(&TTrx_mq, &msg, sizeof(msg), RT_WAITING_FOREVER) == RT_EOK)
{ {
// LOG_D("%d Bytes received...",msg.size); //
if (!msg.size) {//收到数据长度为0表示tcp断开 if (!msg.size)
{ //收到数据长度为0表示tcp断开
break; break;
} }
LOG_HEX("tcpTT", 16, msg.data, msg.size); LOG_D("%d Bytes received from TT",msg.size);
LOG_HEX("TTrec", 16, msg.data, msg.size);
//此处处理接收到数据 //此处处理接收到数据
rt_uint8_t rec_good[] = { 0x88, 0xAA, 0xBB, 0x88 };//前四字节 rt_uint8_t rec_good[] = { 0x88, 0xAA, 0xBB, 0x88, 0x41,0x43,0x4B }; //前四字节=帧头、后三字节=ACK
if (rt_memcmp(msg.data, rec_good, 4)==0) if (rt_memcmp(msg.data, rec_good, 4) == 0 && rt_memcmp(msg.data+msg.size-3, rec_good+4, 3) == 0)
{ {
LOG_I("ack is good."); LOG_I("ack is good.");
} }
else {
LOG_D("try to parse data.");
parseTTData(msg.data,msg.size);
}
} }
} }
} }
//INIT_COMPONENT_EXPORT(recTT_thread_entry); //INIT_COMPONENT_EXPORT(recTT_thread_entry);
static void recTT(void) void recTT(void)
{ {
/* 创建线程 */ /* 创建线程 */
rt_thread_t thread = rt_thread_create("recTT", recTT_thread_entry, RT_NULL, 1024 * 2, 27, 10); rt_thread_t thread = rt_thread_create("recTT", recTT_thread_entry, RT_NULL, 1024 * 2, 27, 10);