aqhome: fixed mqtt message handling
PUBLISH: the message itself is NOT preceeded by size
This commit is contained in:
@@ -35,9 +35,8 @@ GWEN_MSG *GWEN_PublishMqttMsg_new(uint8_t flags, uint16_t packetId, const char *
|
||||
GWEN_Buffer_AppendByte(buf, (packetId>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, packetId & 0xff);
|
||||
}
|
||||
GWEN_Buffer_AppendByte(buf, (messageLen>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, messageLen & 0xff);
|
||||
GWEN_Buffer_AppendBytes(buf, (const char*) messagePtr, messageLen);
|
||||
if (messagePtr && messageLen)
|
||||
GWEN_Buffer_AppendBytes(buf, (const char*) messagePtr, messageLen);
|
||||
|
||||
msg=GWEN_MqttMsg_new(AQH_MQTTMSG_MSGTYPE_PUBLISH | flags, GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
@@ -86,6 +85,90 @@ void AQH_PublishMqttMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, con
|
||||
|
||||
|
||||
|
||||
char *AQH_PublishMqttMsg_ExtractTopic(const GWEN_MSG *msg)
|
||||
{
|
||||
const uint8_t *msgPtr;
|
||||
uint32_t msgLen;
|
||||
|
||||
msgPtr=GWEN_Msg_GetConstBuffer(msg);
|
||||
msgLen=GWEN_Msg_GetBytesInBuffer(msg);
|
||||
|
||||
if (msgLen>1) {
|
||||
uint32_t payloadLen;
|
||||
const uint8_t *payloadPtr;
|
||||
char *result;
|
||||
|
||||
payloadLen=GWEN_Msg_GetParsedPayloadSize(msg);
|
||||
payloadPtr=msgPtr+GWEN_Msg_GetParsedPayloadOffset(msg);
|
||||
|
||||
result=AQH_MqttMsg_ExtractStringAt(payloadPtr, payloadLen);
|
||||
if (result==NULL) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here");
|
||||
return NULL;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *AQH_PublishMqttMsg_ExtractValue(const GWEN_MSG *msg)
|
||||
{
|
||||
const uint8_t *msgPtr;
|
||||
uint32_t msgLen;
|
||||
|
||||
msgPtr=GWEN_Msg_GetConstBuffer(msg);
|
||||
msgLen=GWEN_Msg_GetBytesInBuffer(msg);
|
||||
|
||||
if (msgLen>1) {
|
||||
uint8_t flags;
|
||||
uint32_t payloadLen;
|
||||
const uint8_t *payloadPtr;
|
||||
int rv;
|
||||
|
||||
flags=msgPtr[0] & 0x0f;
|
||||
payloadLen=GWEN_Msg_GetParsedPayloadSize(msg);
|
||||
payloadPtr=msgPtr+GWEN_Msg_GetParsedPayloadOffset(msg);
|
||||
|
||||
rv=AQH_MqttMsg_SkipStringAt(payloadPtr, payloadLen);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return NULL;
|
||||
}
|
||||
payloadLen-=rv;
|
||||
payloadPtr+=rv;
|
||||
|
||||
if (flags & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
|
||||
if (payloadLen<2) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Msg too small");
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
payloadLen-=2;
|
||||
payloadPtr+=2;
|
||||
}
|
||||
}
|
||||
|
||||
if (payloadLen) {
|
||||
char *result;
|
||||
|
||||
result=(char*) malloc(payloadLen+1);
|
||||
memmove(result, payloadPtr, payloadLen);
|
||||
result[payloadLen]=0;
|
||||
return result;
|
||||
}
|
||||
else {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "No message");
|
||||
}
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen, GWEN_BUFFER *dbuf)
|
||||
{
|
||||
int rv;
|
||||
@@ -118,14 +201,10 @@ int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen,
|
||||
}
|
||||
}
|
||||
|
||||
GWEN_Buffer_AppendString(dbuf, " message: ");
|
||||
rv=AQH_MqttMsg_DumpString(payloadPtr, payloadLen, dbuf);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return rv;
|
||||
if (payloadLen) {
|
||||
GWEN_Buffer_AppendString(dbuf, " message: ");
|
||||
GWEN_Buffer_AppendBytes(dbuf, (const char*) payloadPtr, payloadLen);
|
||||
}
|
||||
payloadLen-=rv;
|
||||
payloadPtr+=rv;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user