aqhome: Improved serial port handling, added parsers for different msg types.

This commit is contained in:
Martin Preuss
2023-02-04 00:58:28 +01:00
parent 4587dfb9dd
commit 2874e18048
14 changed files with 1117 additions and 211 deletions

View File

@@ -4,6 +4,9 @@
#endif
#include "aqhome/serial.h"
#include "aqhome/msg_value.h"
#include "aqhome/msg_sendstats.h"
#include "aqhome/msg_ping.h"
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
@@ -101,38 +104,35 @@ int testSend()
void _packetReceived(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
void _packetReceived(AQH_SERIAL *sr, AQH_MSG *msg)
{
const uint8_t *ptr;
uint8_t len;
uint8_t msgType;
int msgIsValid;
GWEN_BUFFER *dbuf;
GWEN_TIME *ti;
dbuf=GWEN_Buffer_new(0, 256, 0, 1);
ti=GWEN_CurrentTime();
GWEN_Time_toString(ti, "YYYY-MM-DD hh:mm:ss", dbuf);
fprintf(stdout, " %s: Received:\n", GWEN_Buffer_GetStart(dbuf));
GWEN_Text_DumpString(ptr, len, 6);
GWEN_Time_toString(ti, "YYYY-MM-DD hh:mm:ss ", dbuf);
GWEN_Time_free(ti);
GWEN_Buffer_free(dbuf);
ti=NULL;
if (ptr[2]==1) {
fprintf(stdout, "-> PING\n");
}
else if (ptr[2]==2) {
uint32_t secs;
int packetsOut;
int collisions;
int aborted;
msgIsValid=(AQH_Msg_IsChecksumValid(msg) && AQH_Msg_IsMsgComplete(msg));
ptr=AQH_Msg_GetBuffer(msg);
len=AQH_Msg_GetBytesInBuffer(msg);
msgType=AQH_Msg_GetMsgType(msg);
/* send error stats */
secs=(ptr[4])+(ptr[5]<<8)+(ptr[6]<<16)+(ptr[7]<<24);
packetsOut=(ptr[8])+(ptr[9]<<8);
collisions=(ptr[10])+(ptr[11]<<8);
aborted=(ptr[12])+(ptr[13]<<8);
fprintf(stdout, "-> SEND STATS: %08x packets sent=%d, collisions=%d, aborted=%d\n",
(unsigned int) secs,
packetsOut, collisions, aborted);
if (msgType==AQH_MSG_TYPE_PING) {
AQH_MsgPing_DumpToBuffer(msg, dbuf, "received");
fprintf(stdout, "%s", GWEN_Buffer_GetStart(dbuf));
}
else if (ptr[2]==4) {
else if (msgType==AQH_MSG_TYPE_COMSENDSTATS) { /* CPRO_CMD_COMSENDSTATS */
AQH_MsgSendStats_DumpToBuffer(msg, dbuf, "received");
fprintf(stdout, "%s", GWEN_Buffer_GetStart(dbuf));
}
else if (msgType==30) { /* CPRO_CMD_I2CBUSMEMBER */
int i2cAddr;
int availability;
@@ -140,7 +140,7 @@ void _packetReceived(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
availability=ptr[5];
fprintf(stdout, "-> I2C DEVICE %02x: %s\n", i2cAddr, (availability==0)?"not available":"FOUND");
}
else if (ptr[2]==5) {
else if (msgType==40) { /* CPRO_CMD_DEBUG */
uint8_t param1;
uint8_t param2;
@@ -148,33 +148,86 @@ void _packetReceived(AQH_SERIAL *sr, const uint8_t *ptr, uint8_t len)
param2=ptr[5];
fprintf(stdout, "-> Debug param1=%02x, param2=%02x\n", param1, param2);
}
else if (ptr[2]==6) {
uint32_t secs;
int valueId;
int valueType;
double value;
double denom;
double calcValue;
int intDenom;
const char *tn;
/* send error stats */
secs=(ptr[4])+(ptr[5]<<8)+(ptr[6]<<16)+(ptr[7]<<24);
valueId=ptr[8];
valueType=ptr[9];
value=(double)((ptr[10])+(ptr[11]<<8));
intDenom=(ptr[12])+(ptr[13]<<8);
denom=(double)(intDenom);
if (intDenom==0)
denom=1.0;
calcValue=value/denom;
switch(valueType) {
case 1: tn="temperature"; break;
case 2: tn="humidity"; break;
default: tn="unknown"; break;
}
fprintf(stdout, "-> VALUE: %08x %d=%.2f (%s)\n", (unsigned int) secs, valueId, calcValue, tn);
else if (msgType==AQH_MSG_TYPE_VALUE) { /* CPRO_CMD_VALUE */
AQH_MsgValue_DumpToBuffer(msg, dbuf, "received");
fprintf(stdout, "%s", GWEN_Buffer_GetStart(dbuf));
}
else {
fprintf(stdout, " %s: Received (%s):\n", GWEN_Buffer_GetStart(dbuf), msgIsValid?"valid":"invalid");
GWEN_Text_DumpString(ptr, len, 6);
}
GWEN_Buffer_free(dbuf);
AQH_Msg_free(msg);
}
AQH_MSG *createPingMsg(AQH_SERIAL *sr, uint8_t destAddr)
{
AQH_MSG *msg;
int rv;
msg=AQH_Msg_new();
rv=AQH_Msg_AddByte(msg, destAddr);
if (rv<0) {
fprintf(stderr, "ERROR1: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
rv=AQH_Msg_AddByte(msg, 6); /* msglen */
if (rv<0) {
fprintf(stderr, "ERROR2: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
rv=AQH_Msg_AddByte(msg, 10); /* ping */
if (rv<0) {
fprintf(stderr, "ERROR3: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
rv=AQH_Msg_AddByte(msg, AQH_Serial_GetAddress(sr)); /* src addr */
if (rv<0) {
fprintf(stderr, "ERROR4: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
rv=AQH_Msg_AddByte(msg, 0); /* timestamp */
if (rv<0) {
fprintf(stderr, "ERROR5: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
rv=AQH_Msg_AddByte(msg, 0); /* timestamp */
if (rv<0) {
fprintf(stderr, "ERROR6: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
rv=AQH_Msg_AddByte(msg, 0); /* timestamp */
if (rv<0) {
fprintf(stderr, "ERROR7: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
rv=AQH_Msg_AddByte(msg, 0); /* timestamp */
if (rv<0) {
fprintf(stderr, "ERROR8: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
rv=AQH_Msg_AddChecksum(msg);
if (rv<0) {
fprintf(stderr, "ERROR9: %d\n", rv);
AQH_Msg_free(msg);
return NULL;
}
return msg;
}
@@ -185,8 +238,6 @@ int testLoop()
int rv;
int i;
GWEN_BUFFER *dbuf;
// uint8_t sendBuf[5]={0x01, 0x02, 0xdb, 0x00, 0xd8 };
uint8_t sendBuf[5]={0x01, 0x02, 0x01, 0xdb, 0xd9 };
time_t tLast;
fprintf(stdout, "Opening device...\n");
@@ -212,16 +263,42 @@ int testLoop()
}
t=time(NULL);
if (difftime(t, tLast)>10) {
rv=AQH_Serial_StartWriting(sr, sendBuf, 5);
if (rv==0)
fprintf(stdout, "+ Sending data\n");
else if (rv==GWEN_ERROR_TRY_AGAIN)
fprintf(stdout, "W Outbuffer busy\n");
AQH_MSG *msg;
msg=createPingMsg(sr, 1);
if (msg) {
rv=AQH_Serial_AddMessageToSend(sr, msg);
if (rv<0) {
fprintf(stderr, "ERROR: %d\n", rv);
AQH_Msg_free(msg);
return rv;
}
if (rv==0) {
GWEN_BUFFER *dbuf;
GWEN_TIME *ti;
dbuf=GWEN_Buffer_new(0, 256, 0, 1);
ti=GWEN_CurrentTime();
GWEN_Time_toString(ti, "YYYY-MM-DD hh:mm:ss ", dbuf);
GWEN_Time_free(ti);
ti=NULL;
AQH_MsgPing_DumpToBuffer(msg, dbuf, "enqueued");
fprintf(stdout, "%s", GWEN_Buffer_GetStart(dbuf));
GWEN_Buffer_free(dbuf);
}
else {
DBG_ERROR(NULL, "here (%d)", rv);
AQH_Serial_Close(sr);
AQH_Serial_free(sr);
return rv;
}
}
else {
DBG_ERROR(NULL, "here (%d)", rv);
AQH_Serial_Close(sr);
AQH_Serial_free(sr);
return rv;
return GWEN_ERROR_INTERNAL;
}
tLast=t;
}