Update dms connection code.
This commit is contained in:
@@ -33,6 +33,7 @@ enum{
|
||||
ACT_SCAN_SIMULATOR, //配置模拟模式 1:模拟模式,0:真实模式,默认是0
|
||||
ACT_SCAN_PRESIG, //预扫数据产生完成信号
|
||||
ACT_SCAN_PRERESP, //上位机处理完数据后的响应
|
||||
ACT_SCAN_CE_STATUS, //是否已经完成当天的扫查
|
||||
};
|
||||
|
||||
//数据传输服务
|
||||
@@ -79,6 +80,7 @@ enum{
|
||||
ACT_CTL_FEB_CMD,
|
||||
ACT_CTL_FEB_RESP,
|
||||
ACT_CTL_FEB_RESET,
|
||||
ACT_CTL_PUMP, //水泵启停
|
||||
};
|
||||
|
||||
//设备升级
|
||||
|
||||
@@ -29,5 +29,5 @@ int dmsmq_recv( int *srvid, int *actid, uint8_t *data );
|
||||
// len 发送数据长度
|
||||
// 返回值 0 成功,< 0 异常信息
|
||||
int dmsmq_send( int srvid, int actid, uint8_t *data, int len );
|
||||
|
||||
int dmsmq_sendx( int srvid, int actid, uint8_t *data, int len );
|
||||
#endif
|
||||
@@ -6,8 +6,10 @@
|
||||
#include <stdarg.h>
|
||||
|
||||
#include <sys/msg.h>
|
||||
#include <errno.h>
|
||||
|
||||
#include "dms_mq.h"
|
||||
|
||||
enum{
|
||||
USRV_NONE = 0,
|
||||
USRV_SCAN, //扫查服务
|
||||
@@ -47,7 +49,7 @@ typedef struct{
|
||||
#pragma pack( 0 )
|
||||
|
||||
typedef struct{
|
||||
uint32_t type;
|
||||
long type;
|
||||
uint8_t data[ 0 ];
|
||||
}msgbuf_t;
|
||||
|
||||
@@ -114,7 +116,6 @@ int dmsmq_init( void )
|
||||
}
|
||||
}
|
||||
|
||||
//FIXME 创建心跳线程监测状态
|
||||
printf( "Start to create heartbeat thread.\n" );
|
||||
if( pid == 0 ){
|
||||
if( pthread_create( &pid, NULL, fn_heart, NULL ) < 0 ){
|
||||
@@ -163,7 +164,6 @@ int hexdump(uint8_t* data, int len, const char* str, ...)
|
||||
// actid 动作ID
|
||||
// data 数据指针
|
||||
// 返回值 >= 0 数据长度, < 0 异常信息
|
||||
#include <iostream>
|
||||
int dmsmq_recv( int *srvid, int *actid, uint8_t *data )
|
||||
{
|
||||
int ret;
|
||||
@@ -207,6 +207,7 @@ int dmsmq_recv( int *srvid, int *actid, uint8_t *data )
|
||||
// data 发送数据指针
|
||||
// len 发送数据长度
|
||||
// 返回值 0 成功,< 0 异常信息
|
||||
|
||||
int dmsmq_send( int srvid, int actid, uint8_t *data, int len )
|
||||
{
|
||||
uint8_t buf[ 4096 ] = { 0x00 };
|
||||
@@ -222,19 +223,25 @@ int dmsmq_send( int srvid, int actid, uint8_t *data, int len )
|
||||
memcpy( mbuf->data + 5, data, len );
|
||||
( mbuf->data )[ len + 5 ] = DBG_TAIL;
|
||||
dlen = 5 + len + 1;
|
||||
|
||||
struct msqid_ds mds;
|
||||
if( msgctl( msgid_c2s, MSG_STAT, &mds ) < 0 ){
|
||||
perror( "Get mq info failed..." );
|
||||
return MQERR_MSGCTL_FAILED;
|
||||
}
|
||||
if( mds.msg_qnum > 2 ){
|
||||
//printf( "MQ Blocked!\n" );
|
||||
printf( "MQ Blocked!\n" );
|
||||
return MQERR_BLOCKED;
|
||||
}
|
||||
|
||||
int trytime = 3;
|
||||
int trytime = 1;
|
||||
while( trytime-- >= 0 ){
|
||||
// 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 );
|
||||
// hexdump( ( uint8_t* )mbuf, dlen + 4, "msgsnd:" );
|
||||
if( msgsnd( msgid_c2s, mbuf, dlen, 0 ) == -1 ){
|
||||
// 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 );
|
||||
perror( "msgsnd error!" );
|
||||
msgid_c2s = msgget( ( key_t)MSG_S2C, IPC_CREAT | 0600 );
|
||||
if( msgid_c2s < 0 ){
|
||||
@@ -242,6 +249,8 @@ int dmsmq_send( int srvid, int actid, uint8_t *data, int len )
|
||||
usleep( 10 * 1000 );
|
||||
continue;
|
||||
}
|
||||
printf( "trytime = %d\n", trytime );
|
||||
continue;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -249,6 +258,46 @@ int dmsmq_send( int srvid, int actid, uint8_t *data, int len )
|
||||
if( trytime < 0 ){
|
||||
return MQERR_DISCONNECT;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int dmsmq_sendx( int srvid, int actid, uint8_t *data, int len )
|
||||
{
|
||||
uint8_t buf[ 4096 ] = { 0x00 };
|
||||
int dlen = 0;
|
||||
msgbuf_t *mbuf = ( msgbuf_t* )buf;
|
||||
mbuf->type = srvid;
|
||||
mbuf->data[ 0 ] = DBG_HEAD;
|
||||
mbuf->data[ 1 ] = srvid;
|
||||
mbuf->data[ 2 ] = actid;
|
||||
*( uint16_t* )( mbuf->data + 3 ) = len;
|
||||
// mbuf->data[ 3 ] = 127;//len % 0x100;
|
||||
// mbuf->data[ 4 ] = len / 0x100;
|
||||
if( len > 0 )
|
||||
memcpy( mbuf->data + 5, data, len );
|
||||
( mbuf->data )[ len + 5 ] = DBG_TAIL;
|
||||
dlen = 5 + len + 1;
|
||||
|
||||
|
||||
|
||||
struct msqid_ds mds;
|
||||
if( msgctl( msgid_c2s, MSG_STAT, &mds ) < 0 ){
|
||||
perror( "Get mq info failed..." );
|
||||
return MQERR_MSGCTL_FAILED;
|
||||
}
|
||||
if( mds.msg_qnum > 2 ){
|
||||
printf( "MQ Blocked!\n" );
|
||||
return MQERR_BLOCKED;
|
||||
}
|
||||
|
||||
hexdump( ( uint8_t* )mbuf, dlen + 4 + 4, "xmsgsnd:" );
|
||||
if( msgsnd( msgid_c2s, mbuf, dlen, 0 ) == -1 ){
|
||||
perror( "dmsmq_sendx: msgsnd error!" );
|
||||
}else{
|
||||
printf( "xdmsmq_sendx send msg %d success!\n", len );
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user