Add new dms protocal

This commit is contained in:
kradchen
2023-11-15 16:19:48 +08:00
parent 940f986756
commit 45ad6dd921
3 changed files with 127 additions and 16 deletions

View File

@@ -91,7 +91,7 @@ void DeviceManager::initDevice()
emit initializeFinished(); emit initializeFinished();
return; return;
} }
dmsmq_init(); dmsmq_init(0);
// empty scan // empty scan
connect(EventCenter::Default(), &EventCenter::RequestEmptyScan, [=](QObject* sender, QObject* detail) connect(EventCenter::Default(), &EventCenter::RequestEmptyScan, [=](QObject* sender, QObject* detail)

View File

@@ -1,6 +1,8 @@
#ifndef _DMS_MQ_H_ #ifndef _DMS_MQ_H_
#define _DMS_MQ_H_ #define _DMS_MQ_H_
#define MAX_MQBLK_NO 5
enum{ enum{
MQERR_DISCONNECT = -1, MQERR_DISCONNECT = -1,
MQERR_FULL = -2, MQERR_FULL = -2,
@@ -12,8 +14,8 @@ enum{
MQERR_THDERR = -8, MQERR_THDERR = -8,
}; };
//初始化DMS的MQ序列 //初始化DMS的MQ序列 with_debug = 0, 不启用MQ的Debug日志 1 启用MQ的debug日志
int dmsmq_init( void ); int dmsmq_init( int with_debug );
//接收DMS的MQ序列阻塞方式。 //接收DMS的MQ序列阻塞方式。
// srvid 服务ID // srvid 服务ID

View File

@@ -2,11 +2,16 @@
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
#include <unistd.h> #include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <pthread.h> #include <pthread.h>
#include <stdarg.h> #include <stdarg.h>
#include <sys/msg.h> #include <sys/msg.h>
#include <errno.h> #include <errno.h>
#include <sys/time.h>
#include <time.h>
#include "dms_mq.h" #include "dms_mq.h"
@@ -55,13 +60,92 @@ typedef struct{
pthread_t pid = 0; pthread_t pid = 0;
int fd_mqlog = -1;
int mqlog( const char *fmt, ... )
{
if( fd_mqlog < 0 ){
return -1;
}
char buf[ 4096 ] = { 0x00 };
int len = 0;
struct timespec time;
struct tm now;
struct timeval tv;
clock_gettime(CLOCK_REALTIME, &time);
localtime_r(&time.tv_sec, &now);
gettimeofday( &tv, NULL );
len = sprintf( buf, "\nGUI : %04d-%02d-%02d %02d:%02d:%02d.%03d :: ",
now.tm_year + 1900, now.tm_mon + 1, now.tm_mday,
now.tm_hour, now.tm_min, now.tm_sec, ( int )( tv.tv_usec / 1000 ) );
va_list args;
va_start(args, fmt);
len += vsprintf( buf + len, fmt, args );
va_end( args );
write( fd_mqlog, ( void* )buf, len );
return 0;
}
int mqlogx( uint8_t *data, int dlen, const char *fmt, ... )
{
if( fd_mqlog < 0 ){
return -1;
}
char buf[ 4096 ] = { 0x00 };
int len = 0;
struct timespec time;
struct tm now;
struct timeval tv;
clock_gettime(CLOCK_REALTIME, &time);
localtime_r(&time.tv_sec, &now);
gettimeofday( &tv, NULL );
len = sprintf( buf, "\nGUI : %04d-%02d-%02d %02d:%02d:%02d.%03d :: ",
now.tm_year + 1900, now.tm_mon + 1, now.tm_mday,
now.tm_hour, now.tm_min, now.tm_sec, ( int )( tv.tv_usec / 1000 ) );
va_list args;
va_start(args, fmt);
len += vsprintf( buf + len, fmt, args );
va_end( args );
len += sprintf( buf + len, "ASCII Dump : " );
for( int idx = 0; idx < dlen; idx++ ){
if( data[ idx ] >= 0x20 && data[ idx ] <= 0x7E )
len += sprintf( buf + len, "%c", data[ idx ] );
else
len += sprintf( buf + len, " " );
}
len += sprintf( buf + len, "\nHEX Dump: " );
for( int idx = 0; idx < dlen; idx++ ){
len += sprintf( buf + len, "%02X ", data[ idx ] );
}
len += sprintf( buf + len, "\n" );
write( fd_mqlog, ( void* )buf, len );
return 0;
}
void *fn_heart( void *arg ) void *fn_heart( void *arg )
{ {
uint32_t cnt = 0; uint32_t cnt = 0;
for( ; ; ){ for( ; ; ){
cnt++; cnt++;
dmsmq_send( USRV_HEARTBEAT, ACT_HB_BEAT, ( uint8_t* )( &cnt ), sizeof( cnt ) ); // dmsmq_send( USRV_HEARTBEAT, ACT_HB_BEAT, ( uint8_t* )( &cnt ), sizeof( cnt ) );
// printf( "GUI Run cnt = %d\r", cnt ); // printf( "GUI Run cnt = %d\r", cnt );
// fflush( stdout ); // fflush( stdout );
sleep( 1 ); sleep( 1 );
@@ -69,11 +153,22 @@ void *fn_heart( void *arg )
} }
//初始化DMS的MQ序列 //初始化DMS的MQ序列
int dmsmq_init( void ) int dmsmq_init( int with_debug )
{ {
if( with_debug ){
system( "touch /mnt/mqlog_gui.log; chmod 777 /mnt/mqlog_gui.log" );
fd_mqlog = open( "/mnt/mqlog_gui.log", O_RDWR | O_APPEND );
if( fd_mqlog < 0 ){
printf( "\n\nGUI fd_mqlog open failed....\n\n" );
}else{
printf( "\n\nGUI fd_mqlog open success....\n\n" );
}
}
msgid_c2s = msgget( ( key_t)MSG_C2S, IPC_CREAT | 0600 ); msgid_c2s = msgget( ( key_t)MSG_C2S, IPC_CREAT | 0600 );
if( msgid_c2s == -1 ){ if( msgid_c2s == -1 ){
perror( "Msgid C2S create error" ); perror( "Msgid C2S create error" );
mqlog( "Msgid C2S create error" );
return MQERR_NOMSGID; return MQERR_NOMSGID;
} }
printf( "MSGID C2S = %d\n", msgid_c2s ); printf( "MSGID C2S = %d\n", msgid_c2s );
@@ -81,6 +176,7 @@ int dmsmq_init( void )
msgid_s2c = msgget( ( key_t)MSG_S2C, IPC_CREAT | 0600 ); msgid_s2c = msgget( ( key_t)MSG_S2C, IPC_CREAT | 0600 );
if( msgid_s2c == -1 ){ if( msgid_s2c == -1 ){
perror( "Msgid S2C create error" ); perror( "Msgid S2C create error" );
mqlog( "Msgid S2C create error" );
return MQERR_NOMSGID; return MQERR_NOMSGID;
} }
printf( "MSGID S2C = %d\n", msgid_s2c ); printf( "MSGID S2C = %d\n", msgid_s2c );
@@ -116,6 +212,7 @@ int dmsmq_init( void )
} }
} }
// FIXME 创建心跳线程监测状态
printf( "Start to create heartbeat thread.\n" ); printf( "Start to create heartbeat thread.\n" );
if( pid == 0 ){ if( pid == 0 ){
if( pthread_create( &pid, NULL, fn_heart, NULL ) < 0 ){ if( pthread_create( &pid, NULL, fn_heart, NULL ) < 0 ){
@@ -174,25 +271,36 @@ int dmsmq_recv( int *srvid, int *actid, uint8_t *data )
if( ret == -1 ){ if( ret == -1 ){
printf( "msgrecv error! msgid_s2c = %d\n", msgid_s2c ); printf( "msgrecv error! msgid_s2c = %d\n", msgid_s2c );
perror( "MSGRECV : " ); perror( "MSGRECV : " );
mqlog( "MQ Recv Error %d\n", ret );
msgid_s2c = msgget( ( key_t)MSG_S2C, IPC_CREAT | 0600 ); msgid_s2c = msgget( ( key_t)MSG_S2C, IPC_CREAT | 0600 );
if( msgid_s2c == -1 ){ if( msgid_s2c == -1 ){
perror( "Msgid C2S create error" ); perror( "Msgid C2S create error" );
mqlog( "Msgid C2S create error\n" );
return MQERR_NOMSGID; return MQERR_NOMSGID;
} }
printf( "MSGID S2C = %d\n", msgid_s2c ); printf( "MSGID S2C = %d\n", msgid_s2c );
mqlog( "MSGID S2C = %d\n", msgid_s2c );
return MQERR_LOSTMSG; return MQERR_LOSTMSG;
} }
// hexdump( mbuf->data, ret, "mbuf->data hexdump: " ); // hexdump( mbuf->data, ret, "mbuf->data hexdump: " );
if( ret < 1024 )
mqlogx( mbuf->data, ret, "MQ RX [ %d - %d ][ %d ] : \n", mbuf->data[ 1 ], mbuf->data[ 2 ], ret );
else
mqlog( "MQ RX data size is out of range [ %d ]...\n", ret );
if( ret < sizeof( prot_dbg_t ) ) if( ret < sizeof( prot_dbg_t ) ){
mqlog( "MQ RX data len too small! [ %d ]\n", ret );
return MQERR_PACKERR; return MQERR_PACKERR;
}
prot_dbg_t *prot = ( prot_dbg_t* )( mbuf->data ); prot_dbg_t *prot = ( prot_dbg_t* )( mbuf->data );
int head = prot->head; int head = prot->head;
int tail = ( mbuf->data )[ ret - 1 ]; int tail = ( mbuf->data )[ ret - 1 ];
if( head != DBG_HEAD || tail != DBG_TAIL ) if( head != DBG_HEAD || tail != DBG_TAIL ){
mqlog( "MQ RX head or tail error!\n" );
return MQERR_PACKERR; return MQERR_PACKERR;
}
*srvid = prot->srvid; *srvid = prot->srvid;
*actid = prot->actid; *actid = prot->actid;
if( prot->len > 0 ) if( prot->len > 0 )
@@ -207,7 +315,6 @@ int dmsmq_recv( int *srvid, int *actid, uint8_t *data )
// data 发送数据指针 // data 发送数据指针
// len 发送数据长度 // len 发送数据长度
// 返回值 0 成功,< 0 异常信息 // 返回值 0 成功,< 0 异常信息
int dmsmq_send( int srvid, int actid, uint8_t *data, int len ) int dmsmq_send( int srvid, int actid, uint8_t *data, int len )
{ {
uint8_t buf[ 4096 ] = { 0x00 }; uint8_t buf[ 4096 ] = { 0x00 };
@@ -227,14 +334,17 @@ int dmsmq_send( int srvid, int actid, uint8_t *data, int len )
struct msqid_ds mds; struct msqid_ds mds;
if( msgctl( msgid_c2s, MSG_STAT, &mds ) < 0 ){ if( msgctl( msgid_c2s, MSG_STAT, &mds ) < 0 ){
perror( "Get mq info failed..." ); perror( "Get mq info failed..." );
mqlog( "MQ TX : Get mq info failed...\n" );
return MQERR_MSGCTL_FAILED; return MQERR_MSGCTL_FAILED;
} }
if( mds.msg_qnum > 2 ){ if( mds.msg_qnum > MAX_MQBLK_NO ){
printf( "MQ Blocked!\n" ); printf( "MQ Blocked!\n" );
mqlog( "MQ TX : MQ Blocked!\n" );
return MQERR_BLOCKED; return MQERR_BLOCKED;
} }
int trytime = 1; mqlogx( mbuf->data, dlen, "MQ TX [ %d - %d ] [ %d ] : \n", srvid, actid, dlen );
int trytime = 2;
while( trytime-- >= 0 ){ while( trytime-- >= 0 ){
// printf( "mbuf type : %d, data : %s, dlen = %d\n", mbuf->type, mbuf->data + 5, dlen ); // printf( "mbuf type : %d, data : %s, dlen = %d\n", mbuf->type, mbuf->data + 5, dlen );
// printf( "mbuf type : %d, dlen = %d [ %d ]\n", mbuf->type, dlen, len ); // printf( "mbuf type : %d, dlen = %d [ %d ]\n", mbuf->type, dlen, len );
@@ -243,9 +353,11 @@ int dmsmq_send( int srvid, int actid, uint8_t *data, int len )
// printf( "[%d / %d ]mbuf type : %d, data : %s, dlen = %d\n", trytime, msgid_c2s, mbuf->type, mbuf->data + 5, dlen ); // printf( "[%d / %d ]mbuf type : %d, data : %s, dlen = %d\n", trytime, msgid_c2s, mbuf->type, mbuf->data + 5, dlen );
// printf( "Error no : %d ", errno ); // printf( "Error no : %d ", errno );
perror( "msgsnd error!" ); perror( "msgsnd error!" );
msgid_c2s = msgget( ( key_t)MSG_S2C, IPC_CREAT | 0600 ); mqlog( "MQ TX msgsnd error!" );
msgid_c2s = msgget( ( key_t)MSG_C2S, IPC_CREAT | 0600 );
if( msgid_c2s < 0 ){ if( msgid_c2s < 0 ){
perror( "Msgid S2C create error" ); perror( "Msgid S2C create error" );
mqlog( "MQ TX Msgid S2C create error\n" );
usleep( 10 * 1000 ); usleep( 10 * 1000 );
continue; continue;
} }
@@ -256,14 +368,13 @@ int dmsmq_send( int srvid, int actid, uint8_t *data, int len )
} }
if( trytime < 0 ){ if( trytime < 0 ){
mqlog( "MQ TX failed...\n" );
return MQERR_DISCONNECT; return MQERR_DISCONNECT;
} }
return 0; return 0;
} }
int dmsmq_sendx( int srvid, int actid, uint8_t *data, int len ) int dmsmq_sendx( int srvid, int actid, uint8_t *data, int len )
{ {
uint8_t buf[ 4096 ] = { 0x00 }; uint8_t buf[ 4096 ] = { 0x00 };
@@ -281,8 +392,6 @@ int dmsmq_sendx( int srvid, int actid, uint8_t *data, int len )
( mbuf->data )[ len + 5 ] = DBG_TAIL; ( mbuf->data )[ len + 5 ] = DBG_TAIL;
dlen = 5 + len + 1; dlen = 5 + len + 1;
struct msqid_ds mds; struct msqid_ds mds;
if( msgctl( msgid_c2s, MSG_STAT, &mds ) < 0 ){ if( msgctl( msgid_c2s, MSG_STAT, &mds ) < 0 ){
perror( "Get mq info failed..." ); perror( "Get mq info failed..." );