aqhome/mqtt: added messages regarding subscription.
This commit is contained in:
127
aqhome/mqtt/msg_mqtt_subscribe.c
Normal file
127
aqhome/mqtt/msg_mqtt_subscribe.c
Normal file
@@ -0,0 +1,127 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
|
||||
*
|
||||
* The license for this file can be found in the file COPYING which you
|
||||
* should have received along with this file.
|
||||
****************************************************************************/
|
||||
|
||||
#ifdef HAVE_CONFIG_H
|
||||
# include <config.h>
|
||||
#endif
|
||||
|
||||
|
||||
#include "aqhome/mqtt/msg_mqtt_subscribe.h"
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
static int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen, GWEN_BUFFER *dbuf);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
GWEN_MSG *GWEN_SubscribeMqttMsg_new(uint8_t flags, uint16_t packetId, const char *sTopic, uint8_t requestedQos)
|
||||
{
|
||||
if (sTopic && *sTopic) {
|
||||
GWEN_MSG *msg;
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 64, 0, 1);
|
||||
GWEN_Buffer_AppendByte(buf, (packetId>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, packetId & 0xff);
|
||||
|
||||
/* add topic filter / qos pair */
|
||||
AQH_MqttMsg_AppendStringWithLen(buf, sTopic);
|
||||
GWEN_Buffer_AppendByte(buf, requestedQos);
|
||||
|
||||
msg=GWEN_MqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE | flags,
|
||||
GWEN_Buffer_GetUsedBytes(buf),
|
||||
(const uint8_t*) GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
if (msg==NULL) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here");
|
||||
return NULL;
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
else {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Missing topic or message");
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_SubscribeMqttMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText)
|
||||
{
|
||||
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;
|
||||
int rv;
|
||||
|
||||
payloadLen=GWEN_Msg_GetParsedPayloadSize(msg);
|
||||
payloadPtr=msgPtr+GWEN_Msg_GetParsedPayloadOffset(msg);
|
||||
|
||||
GWEN_Buffer_AppendArgs(dbuf, "%s %s", AQH_MqttMsg_MsgTypeToString(msgPtr[0] & 0xf0), sText);
|
||||
|
||||
GWEN_Buffer_AppendString(dbuf, "(");
|
||||
rv=_dumpPayload((msgPtr[0] & 0x0f), payloadPtr, payloadLen, dbuf);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
}
|
||||
else {
|
||||
GWEN_Buffer_AppendString(dbuf, ")");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen, GWEN_BUFFER *dbuf)
|
||||
{
|
||||
int rv;
|
||||
|
||||
GWEN_Buffer_AppendString(dbuf, "flags:");
|
||||
if (flags & AQH_MQTTMSG_FLAGS_DUP)
|
||||
GWEN_Buffer_AppendString(dbuf, " DUP");
|
||||
if (flags & AQH_MQTTMSG_FLAGS_RETAIN)
|
||||
GWEN_Buffer_AppendString(dbuf, " RETAIN");
|
||||
GWEN_Buffer_AppendArgs(dbuf, " QOS%d", (flags>>1) & 0x03);
|
||||
|
||||
if (flags & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
|
||||
if (payloadLen<2) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Msg too small");
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
else {
|
||||
GWEN_Buffer_AppendArgs(dbuf, " packet id: %d", (payloadPtr[0]<<8)+payloadPtr[1]);
|
||||
payloadLen-=2;
|
||||
payloadPtr+=2;
|
||||
}
|
||||
}
|
||||
|
||||
while(payloadLen) {
|
||||
GWEN_Buffer_AppendString(dbuf, " topic: ");
|
||||
rv=AQH_MqttMsg_DumpString(payloadPtr, payloadLen, dbuf);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
payloadLen-=rv;
|
||||
payloadPtr+=rv;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user