aqhome/mqtt: added messages regarding subscription.
This commit is contained in:
@@ -51,6 +51,8 @@
|
||||
msg_mqtt_connack.h
|
||||
msg_mqtt_publish.h
|
||||
msg_mqtt_pubresponse.h
|
||||
msg_mqtt_subscribe.h
|
||||
msg_mqtt_suback.h
|
||||
</headers>
|
||||
|
||||
|
||||
@@ -68,6 +70,8 @@
|
||||
msg_mqtt_connack.c
|
||||
msg_mqtt_publish.c
|
||||
msg_mqtt_pubresponse.c
|
||||
msg_mqtt_subscribe.c
|
||||
msg_mqtt_suback.c
|
||||
</sources>
|
||||
|
||||
|
||||
|
||||
@@ -45,7 +45,6 @@ static void _processRecvStatsMessage(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *nodeMsg);
|
||||
static void _publishDouble(GWEN_MSG_ENDPOINT *ep, uint32_t uid, int valueId, const char *valuePath, double v);
|
||||
static void _publishInt(GWEN_MSG_ENDPOINT *ep, uint32_t uid, int valueId, const char *valuePath, int v);
|
||||
static void _publishString(GWEN_MSG_ENDPOINT *ep, uint32_t uid, int valueId, const char *valuePath, const char *v);
|
||||
static const char *_valueTypeToString(int t);
|
||||
static int _isMsgComplete(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg);
|
||||
static int _calcAndSetPayloadSizeAndOffset(GWEN_MSG *msg);
|
||||
|
||||
@@ -318,11 +317,18 @@ void _processOutMessage(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *nodeMsg)
|
||||
|
||||
void _processValue2Message(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *nodeMsg)
|
||||
{
|
||||
_publishDouble(ep,
|
||||
AQH_Value2Msg_GetUid(nodeMsg),
|
||||
AQH_Value2Msg_GetValueId(nodeMsg),
|
||||
_valueTypeToString(AQH_Value2Msg_GetValueId(nodeMsg)),
|
||||
AQH_Value2Msg_GetValue(nodeMsg));
|
||||
if (AQH_Value2Msg_GetValueType(nodeMsg)==AQH_MSG_VALUE2_TYPE_DOOR)
|
||||
_publishString(ep,
|
||||
AQH_Value2Msg_GetUid(nodeMsg),
|
||||
AQH_Value2Msg_GetValueId(nodeMsg),
|
||||
AQH_Value2Msg_GetValueTypeName(nodeMsg),
|
||||
AQH_Value2Msg_GetValueAsWindowStateString(nodeMsg));
|
||||
else
|
||||
_publishDouble(ep,
|
||||
AQH_Value2Msg_GetUid(nodeMsg),
|
||||
AQH_Value2Msg_GetValueId(nodeMsg),
|
||||
AQH_Value2Msg_GetValueTypeName(nodeMsg),
|
||||
AQH_Value2Msg_GetValue(nodeMsg));
|
||||
}
|
||||
|
||||
|
||||
@@ -437,17 +443,6 @@ void _publishString(GWEN_MSG_ENDPOINT *ep, uint32_t uid, int valueId, const char
|
||||
|
||||
|
||||
|
||||
const char *_valueTypeToString(int t)
|
||||
{
|
||||
switch(t) {
|
||||
case AQH_MSG_VALUE2_TYPE_TEMP: return "temperature";
|
||||
case AQH_MSG_VALUE2_TYPE_HUMIDITY: return "humidity";
|
||||
default: return "unknown";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _isMsgComplete(GWEN_UNUSED GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg)
|
||||
{
|
||||
int rv;
|
||||
|
||||
@@ -168,6 +168,40 @@ void _flagsToDb(uint8_t flags, GWEN_DB_NODE *dbDest, const char *varNameFlags, c
|
||||
|
||||
|
||||
|
||||
/* helper functions */
|
||||
|
||||
|
||||
void AQH_MqttMsg_AppendStringWithLen(GWEN_BUFFER *buf, const char *s)
|
||||
{
|
||||
unsigned int len;
|
||||
|
||||
len=strlen(s);
|
||||
GWEN_Buffer_AppendByte(buf, (len>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, len & 0xff);
|
||||
if (s && *s)
|
||||
GWEN_Buffer_AppendString(buf, s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_MqttMsg_DumpString(const uint8_t *ptr, uint32_t len, GWEN_BUFFER *buf)
|
||||
{
|
||||
if (len>1) {
|
||||
int slen;
|
||||
|
||||
slen=(ptr[0]<<8)+ptr[1];
|
||||
if (slen) {
|
||||
if (slen>(len-2)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Invalid string length (%lu, remaining %lu)",
|
||||
(unsigned long int) slen, (unsigned long int) len);
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
GWEN_Buffer_AppendBytes(buf, (const char*) ptr+2, slen);
|
||||
}
|
||||
return slen+2;
|
||||
}
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -57,5 +57,10 @@ AQHOME_API const char *AQH_MqttMsg_MsgTypeToString(uint8_t t);
|
||||
AQHOME_API uint8_t AQH_MqttMsg_GetMsgTypeAndFlags(const GWEN_MSG *msg);
|
||||
|
||||
|
||||
/* helper functions */
|
||||
AQHOME_API void AQH_MqttMsg_AppendStringWithLen(GWEN_BUFFER *buf, const char *s);
|
||||
AQHOME_API int AQH_MqttMsg_DumpString(const uint8_t *ptr, uint32_t len, GWEN_BUFFER *buf);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -18,8 +18,6 @@
|
||||
|
||||
|
||||
static int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen, GWEN_BUFFER *dbuf);
|
||||
static void _appendStringWithLen(GWEN_BUFFER *buf, const char *s);
|
||||
static int _dumpString(const uint8_t *ptr, uint32_t len, GWEN_BUFFER *buf);
|
||||
|
||||
|
||||
|
||||
@@ -32,7 +30,7 @@ GWEN_MSG *GWEN_PublishMqttMsg_new(uint8_t flags, uint16_t packetId, const char *
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 64, 0, 1);
|
||||
_appendStringWithLen(buf, sTopic);
|
||||
AQH_MqttMsg_AppendStringWithLen(buf, sTopic);
|
||||
if (flags & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
|
||||
GWEN_Buffer_AppendByte(buf, (packetId>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, packetId & 0xff);
|
||||
@@ -100,7 +98,7 @@ int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen,
|
||||
GWEN_Buffer_AppendArgs(dbuf, " QOS%d", (flags>>1) & 0x03);
|
||||
|
||||
GWEN_Buffer_AppendString(dbuf, " topic: ");
|
||||
rv=_dumpString(payloadPtr, payloadLen, dbuf);
|
||||
rv=AQH_MqttMsg_DumpString(payloadPtr, payloadLen, dbuf);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return rv;
|
||||
@@ -121,7 +119,7 @@ int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen,
|
||||
}
|
||||
|
||||
GWEN_Buffer_AppendString(dbuf, " message: ");
|
||||
rv=_dumpString(payloadPtr, payloadLen, dbuf);
|
||||
rv=AQH_MqttMsg_DumpString(payloadPtr, payloadLen, dbuf);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return rv;
|
||||
@@ -134,37 +132,3 @@ int _dumpPayload(uint8_t flags, const uint8_t *payloadPtr, uint32_t payloadLen,
|
||||
|
||||
|
||||
|
||||
void _appendStringWithLen(GWEN_BUFFER *buf, const char *s)
|
||||
{
|
||||
unsigned int len;
|
||||
|
||||
len=strlen(s);
|
||||
GWEN_Buffer_AppendByte(buf, (len>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, len & 0xff);
|
||||
if (s && *s)
|
||||
GWEN_Buffer_AppendString(buf, s);
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _dumpString(const uint8_t *ptr, uint32_t len, GWEN_BUFFER *buf)
|
||||
{
|
||||
if (len>1) {
|
||||
int slen;
|
||||
|
||||
slen=(ptr[0]<<8)+ptr[1];
|
||||
if (slen) {
|
||||
if (slen>(len-2)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Invalid string length (%lu, remaining %lu)",
|
||||
(unsigned long int) slen, (unsigned long int) len);
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
GWEN_Buffer_AppendBytes(buf, (const char*) ptr+2, slen);
|
||||
}
|
||||
return slen+2;
|
||||
}
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
68
aqhome/mqtt/msg_mqtt_suback.c
Normal file
68
aqhome/mqtt/msg_mqtt_suback.c
Normal file
@@ -0,0 +1,68 @@
|
||||
/****************************************************************************
|
||||
* 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_suback.h"
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
GWEN_MSG *GWEN_SubAckMqttMsg_new(uint8_t flags, uint16_t packetId, uint8_t result)
|
||||
{
|
||||
GWEN_MSG *msg;
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 64, 0, 1);
|
||||
if (flags & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
|
||||
GWEN_Buffer_AppendByte(buf, (packetId>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, packetId & 0xff);
|
||||
}
|
||||
/* payload */
|
||||
GWEN_Buffer_AppendByte(buf, result);
|
||||
|
||||
msg=GWEN_MqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBACK | 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_SubAckMqttMsg_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) {
|
||||
const uint8_t *payloadPtr;
|
||||
uint32_t payloadLen;
|
||||
|
||||
GWEN_Buffer_AppendArgs(dbuf, "%s %s", AQH_MqttMsg_MsgTypeToString(msgPtr[0] & 0xf0), sText);
|
||||
payloadLen=GWEN_Msg_GetParsedPayloadSize(msg);
|
||||
payloadPtr=msgPtr+GWEN_Msg_GetParsedPayloadOffset(msg);
|
||||
if (payloadLen>=1)
|
||||
GWEN_Buffer_AppendArgs(dbuf, "(result=%d)", payloadPtr[0]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
27
aqhome/mqtt/msg_mqtt_suback.h
Normal file
27
aqhome/mqtt/msg_mqtt_suback.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/****************************************************************************
|
||||
* 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.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef AQH_MSG_MQTT_SUBACK_H
|
||||
#define AQH_MSG_MQTT_SUBACK_H
|
||||
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/mqtt/msg_mqtt.h>
|
||||
|
||||
#include <gwenhywfar/msg.h>
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
|
||||
AQHOME_API GWEN_MSG *GWEN_SubAckMqttMsg_new(uint8_t flags, uint16_t packetId, uint8_t result);
|
||||
AQHOME_API void AQH_SubAckMqttMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
27
aqhome/mqtt/msg_mqtt_subscribe.h
Normal file
27
aqhome/mqtt/msg_mqtt_subscribe.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/****************************************************************************
|
||||
* 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.
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef AQH_MSG_MQTT_SUBSCRIBE_H
|
||||
#define AQH_MSG_MQTT_SUBSCRIBE_H
|
||||
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/mqtt/msg_mqtt.h>
|
||||
|
||||
#include <gwenhywfar/msg.h>
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
|
||||
AQHOME_API GWEN_MSG *GWEN_SubscribeMqttMsg_new(uint8_t flags, uint16_t packetId, const char *sTopic, uint8_t requestedQos);
|
||||
AQHOME_API void AQH_SubscribeMqttMsg_DumpToBuffer(const GWEN_MSG *msg, GWEN_BUFFER *dbuf, const char *sText);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user