aqhome: adapted server aqhome-mqttlog to events2 api.
This commit is contained in:
@@ -110,11 +110,13 @@
|
||||
<useTargets>
|
||||
aqhmsg_node
|
||||
aqhmsg_ipc
|
||||
aqhmsg_mqtt
|
||||
</useTargets>
|
||||
|
||||
<subdirs>
|
||||
node
|
||||
ipc
|
||||
mqtt
|
||||
</subdirs>
|
||||
|
||||
|
||||
|
||||
88
aqhome/msg/mqtt/0BUILD
Normal file
88
aqhome/msg/mqtt/0BUILD
Normal file
@@ -0,0 +1,88 @@
|
||||
<?xml?>
|
||||
|
||||
<gwbuild>
|
||||
|
||||
<target type="ConvenienceLibrary" name="aqhmsg_mqtt" >
|
||||
|
||||
<includes type="c" >
|
||||
$(gwenhywfar_cflags)
|
||||
-I$(topsrcdir)
|
||||
-I$(topbuilddir)
|
||||
</includes>
|
||||
|
||||
<includes type="tm2" >
|
||||
--include=$(builddir)
|
||||
--include=$(srcdir)
|
||||
</includes>
|
||||
|
||||
|
||||
<define name="BUILDING_AQHOME" />
|
||||
|
||||
<setVar name="local/cflags">$(visibility_cflags)</setVar>
|
||||
|
||||
|
||||
<setVar name="tm2flags" >
|
||||
--api=AQHOME_API
|
||||
</setVar>
|
||||
|
||||
<setVar name="local/typefiles" >
|
||||
</setVar>
|
||||
|
||||
<setVar name="local/built_sources" >
|
||||
</setVar>
|
||||
|
||||
<setVar name="local/built_headers_pub">
|
||||
</setVar>
|
||||
|
||||
|
||||
<setVar name="local/built_headers_priv" >
|
||||
</setVar>
|
||||
|
||||
|
||||
<headers dist="false" install="$(pkgincludedir)/msg" >
|
||||
$(local/built_headers_pub)
|
||||
</headers>
|
||||
|
||||
|
||||
<headers dist="true" install="$(pkgincludedir)/msg" >
|
||||
m_mqtt.h
|
||||
m_mqtt_connect.h
|
||||
m_mqtt_connack.h
|
||||
m_mqtt_subscribe.h
|
||||
m_mqtt_suback.h
|
||||
m_mqtt_publish.h
|
||||
</headers>
|
||||
|
||||
|
||||
<headers dist="true" >
|
||||
</headers>
|
||||
|
||||
|
||||
<sources>
|
||||
$(local/typefiles)
|
||||
|
||||
m_mqtt.c
|
||||
m_mqtt_connect.c
|
||||
m_mqtt_connack.c
|
||||
m_mqtt_subscribe.c
|
||||
m_mqtt_suback.c
|
||||
m_mqtt_publish.c
|
||||
</sources>
|
||||
|
||||
|
||||
<extradist>
|
||||
</extradist>
|
||||
|
||||
|
||||
<useTargets>
|
||||
</useTargets>
|
||||
|
||||
<subdirs>
|
||||
</subdirs>
|
||||
|
||||
|
||||
|
||||
|
||||
</target>
|
||||
|
||||
</gwbuild>
|
||||
205
aqhome/msg/mqtt/m_mqtt.c
Normal file
205
aqhome/msg/mqtt/m_mqtt.c
Normal file
@@ -0,0 +1,205 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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/msg/mqtt/m_mqtt.h"
|
||||
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementation
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQH_MESSAGE *AQH_MqttMessage_new(uint8_t typeAndFlags, uint32_t payloadLen, const uint8_t *payload)
|
||||
{
|
||||
if (payloadLen>0xfffffffu) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Too many bytes in payload, can't encode into MQTT message");
|
||||
return NULL;
|
||||
}
|
||||
else {
|
||||
AQH_MESSAGE *msg;
|
||||
uint8_t *ptr;
|
||||
uint32_t msgBufLen;
|
||||
uint32_t len;
|
||||
uint32_t bytesUsed=0;
|
||||
int i=0;
|
||||
|
||||
msgBufLen=16+payloadLen;
|
||||
msg=AQH_Message_new();
|
||||
AQH_Message_SetData(msg, NULL, msgBufLen); /* auto-malloc len bytes */
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg);
|
||||
*(ptr++)=typeAndFlags;
|
||||
bytesUsed++;
|
||||
|
||||
/* store address length */
|
||||
len=payloadLen;
|
||||
do {
|
||||
uint8_t b;
|
||||
|
||||
b=len & 0x7f;
|
||||
len>>=7;
|
||||
if (len)
|
||||
b|=0x80;
|
||||
*(ptr++)=b;
|
||||
bytesUsed++;
|
||||
} while(len && i<4);
|
||||
if (payload && payloadLen) {
|
||||
memmove(ptr, payload, payloadLen);
|
||||
bytesUsed+=payloadLen;
|
||||
}
|
||||
AQH_Message_SetUsedSize(msg, bytesUsed);
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void AQH_MqttMessage_AppendStringWithLenToBuffer(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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *AQH_MqttMessage_ExtractStringAt(const uint8_t *ptr, uint32_t len)
|
||||
{
|
||||
if (len>1) {
|
||||
uint32_t slen;
|
||||
|
||||
slen=(ptr[0]<<8)+ptr[1];
|
||||
if (slen) {
|
||||
char *result;
|
||||
|
||||
if (slen>(len-2)) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Invalid string length (%lu, remaining %lu)",
|
||||
(unsigned long int) slen, (unsigned long int) len);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
result=(char*) malloc(slen+1);
|
||||
if (result==NULL) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error on malloc");
|
||||
return NULL;
|
||||
}
|
||||
memmove(result, ptr+2, slen);
|
||||
result[slen]=0;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_MqttMessage_GetOffsetOfPayload(const AQH_MESSAGE *msg)
|
||||
{
|
||||
const uint8_t *ptr;
|
||||
int usedBytes;
|
||||
int idx;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg);
|
||||
usedBytes=AQH_Message_GetUsedSize(msg);
|
||||
if (usedBytes>5)
|
||||
usedBytes=5;
|
||||
for(idx=1; idx<usedBytes; idx++) {
|
||||
uint8_t b;
|
||||
|
||||
b=ptr[idx];
|
||||
if (!(b & 0x80))
|
||||
break;
|
||||
}
|
||||
if (idx>=usedBytes) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Could not determine length of size field");
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
return idx+1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_MqttMessage_GetTypeAndFlags(const AQH_MESSAGE *msg)
|
||||
{
|
||||
const uint8_t *ptr;
|
||||
uint32_t usedBytes;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg);
|
||||
usedBytes=AQH_Message_GetUsedSize(msg);
|
||||
if (usedBytes)
|
||||
return *ptr;
|
||||
else {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "No data in message");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_MqttMessage_SkipStringAt(const uint8_t *ptr, uint32_t len)
|
||||
{
|
||||
if (len>1) {
|
||||
uint32_t 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;
|
||||
}
|
||||
}
|
||||
return slen+2;
|
||||
}
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const char *AQH_MqttMessage_MsgTypeToString(uint8_t t)
|
||||
{
|
||||
switch(t & 0xf0) {
|
||||
case (AQH_MQTTMSG_MSGTYPE_CONNECT & 0xf0): return "CONNECT";
|
||||
case (AQH_MQTTMSG_MSGTYPE_CONNACK & 0xf0): return "CONACK";
|
||||
case (AQH_MQTTMSG_MSGTYPE_PUBLISH & 0xf0): return "PUBLISH";
|
||||
case (AQH_MQTTMSG_MSGTYPE_PUBACK & 0xf0): return "PUBACK";
|
||||
case (AQH_MQTTMSG_MSGTYPE_PUBREC & 0xf0): return "PUBREC";
|
||||
case (AQH_MQTTMSG_MSGTYPE_PUBREL & 0xf0): return "PUBREL";
|
||||
case (AQH_MQTTMSG_MSGTYPE_PUBCOMP & 0xf0): return "PUBCOMP";
|
||||
case (AQH_MQTTMSG_MSGTYPE_SUBSCRIBE & 0xf0): return "SUBSCRIBE";
|
||||
case (AQH_MQTTMSG_MSGTYPE_SUBACK & 0xf0): return "SUBACK";
|
||||
case (AQH_MQTTMSG_MSGTYPE_UNSUBSCRIBE & 0xf0): return "UNSUBSCRIBE";
|
||||
case (AQH_MQTTMSG_MSGTYPE_UNSUBACK & 0xf0): return "UNSUBACK";
|
||||
case (AQH_MQTTMSG_MSGTYPE_PINGREQ & 0xf0): return "PINGREQ";
|
||||
case (AQH_MQTTMSG_MSGTYPE_PINGRESP & 0xf0): return "PINGRESP";
|
||||
case (AQH_MQTTMSG_MSGTYPE_DISCONNECT & 0xf0): return "DISCONNECT";
|
||||
default: return "(unknown)";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
63
aqhome/msg/mqtt/m_mqtt.h
Normal file
63
aqhome/msg/mqtt/m_mqtt.h
Normal file
@@ -0,0 +1,63 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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_M_MQTT_H
|
||||
#define AQH_M_MQTT_H
|
||||
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/ipc2/message.h>
|
||||
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
|
||||
#define AQH_MQTTMSG_OFFS_CONTROL 0
|
||||
#define AQH_MQTTMSG_OFFS_REMAINING_LENGTH 1
|
||||
|
||||
|
||||
/* from https://docs.solace.com/API/MQTT-311-Prtl-Conformance-Spec/MQTT%20Control%20Packet%20format.htm
|
||||
*/
|
||||
#define AQH_MQTTMSG_MSGTYPE_CONNECT 0x10u
|
||||
#define AQH_MQTTMSG_MSGTYPE_CONNACK 0x20u
|
||||
#define AQH_MQTTMSG_MSGTYPE_PUBLISH 0x30u
|
||||
#define AQH_MQTTMSG_MSGTYPE_PUBACK 0x40u
|
||||
#define AQH_MQTTMSG_MSGTYPE_PUBREC 0x50u /* assured delivery part 1 */
|
||||
#define AQH_MQTTMSG_MSGTYPE_PUBREL 0x62u /* assured delivery part 2 */
|
||||
#define AQH_MQTTMSG_MSGTYPE_PUBCOMP 0x70u /* assured delivery part 3 */
|
||||
#define AQH_MQTTMSG_MSGTYPE_SUBSCRIBE 0x82u
|
||||
#define AQH_MQTTMSG_MSGTYPE_SUBACK 0x90u
|
||||
#define AQH_MQTTMSG_MSGTYPE_UNSUBSCRIBE 0xa2u
|
||||
#define AQH_MQTTMSG_MSGTYPE_UNSUBACK 0xb0u
|
||||
#define AQH_MQTTMSG_MSGTYPE_PINGREQ 0xc0u
|
||||
#define AQH_MQTTMSG_MSGTYPE_PINGRESP 0xd0u
|
||||
#define AQH_MQTTMSG_MSGTYPE_DISCONNECT 0xe0u
|
||||
|
||||
|
||||
#define AQH_MQTTMSG_FLAGS_DUP 0x08u
|
||||
#define AQH_MQTTMSG_FLAGS_QOS2 0x04u
|
||||
#define AQH_MQTTMSG_FLAGS_QOS1 0x02u
|
||||
#define AQH_MQTTMSG_FLAGS_RETAIN 0x01u
|
||||
|
||||
|
||||
|
||||
AQHOME_API AQH_MESSAGE *AQH_MqttMessage_new(uint8_t typeAndFlags, uint32_t payloadLen, const uint8_t *payload);
|
||||
|
||||
AQHOME_API int AQH_MqttMessage_GetTypeAndFlags(const AQH_MESSAGE *msg);
|
||||
|
||||
AQHOME_API void AQH_MqttMessage_AppendStringWithLenToBuffer(GWEN_BUFFER *buf, const char *s);
|
||||
AQHOME_API char *AQH_MqttMessage_ExtractStringAt(const uint8_t *ptr, uint32_t len);
|
||||
AQHOME_API int AQH_MqttMessage_SkipStringAt(const uint8_t *ptr, uint32_t len);
|
||||
|
||||
AQHOME_API int AQH_MqttMessage_GetOffsetOfPayload(const AQH_MESSAGE *msg);
|
||||
|
||||
AQHOME_API const char *AQH_MqttMessage_MsgTypeToString(uint8_t t);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
88
aqhome/msg/mqtt/m_mqtt_connack.c
Normal file
88
aqhome/msg/mqtt/m_mqtt_connack.c
Normal file
@@ -0,0 +1,88 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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/msg/mqtt/m_mqtt_connack.h"
|
||||
#include "aqhome/msg/mqtt/m_mqtt.h"
|
||||
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementation
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQH_MESSAGE *AQH_MqttMessageConnAck_new(uint8_t flags, uint8_t result)
|
||||
{
|
||||
AQH_MESSAGE *msg;
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 64, 0, 1);
|
||||
GWEN_Buffer_AppendByte(buf, flags);
|
||||
GWEN_Buffer_AppendByte(buf, result);
|
||||
|
||||
msg=AQH_MqttMessage_new(AQH_MQTTMSG_MSGTYPE_CONNECT, GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t AQH_MqttMessageConnAck_GetResultFlags(const AQH_MESSAGE *msg)
|
||||
{
|
||||
int idx;
|
||||
|
||||
idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
|
||||
if (idx>0) {
|
||||
const uint8_t *ptr;
|
||||
int len;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg)+idx;
|
||||
len=(int)AQH_Message_GetUsedSize(msg)-idx;
|
||||
if (len>0) {
|
||||
return ptr[0];
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
uint8_t AQH_MqttMessageConnAck_GetResultCode(const AQH_MESSAGE *msg)
|
||||
{
|
||||
int idx;
|
||||
|
||||
idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
|
||||
if (idx>0) {
|
||||
const uint8_t *ptr;
|
||||
int len;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg)+idx;
|
||||
len=(int)AQH_Message_GetUsedSize(msg)-idx;
|
||||
if (len>1) {
|
||||
return ptr[1];
|
||||
}
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
39
aqhome/msg/mqtt/m_mqtt_connack.h
Normal file
39
aqhome/msg/mqtt/m_mqtt_connack.h
Normal file
@@ -0,0 +1,39 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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_M_MQTT_CONNACK_H
|
||||
#define AQH_M_MQTT_CONNACK_H
|
||||
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/ipc2/message.h>
|
||||
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
|
||||
#define AQH_MQTTMSG_CONNACK_FLAGS_HAVE_SESSION 0x01u
|
||||
|
||||
#define AQH_MQTTMSG_CONNACK_RESULT_ACCEPTED 0x00u
|
||||
#define AQH_MQTTMSG_CONNACK_RESULT_BAD_PROTO 0x01u
|
||||
#define AQH_MQTTMSG_CONNACK_RESULT_BAD_CLIENTID 0x02u
|
||||
#define AQH_MQTTMSG_CONNACK_RESULT_UNAVAILABLE 0x03u
|
||||
#define AQH_MQTTMSG_CONNACK_RESULT_BAD_CREDENTIALS 0x04u
|
||||
#define AQH_MQTTMSG_CONNACK_RESULT_UNAUTHORIZED 0x05u
|
||||
|
||||
|
||||
|
||||
AQHOME_API AQH_MESSAGE *AQH_MqttMessageConnAck_new(uint8_t flags, uint8_t result);
|
||||
AQHOME_API uint8_t AQH_MqttMessageConnAck_GetResultFlags(const AQH_MESSAGE *msg);
|
||||
AQHOME_API uint8_t AQH_MqttMessageConnAck_GetResultCode(const AQH_MESSAGE *msg);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
62
aqhome/msg/mqtt/m_mqtt_connect.c
Normal file
62
aqhome/msg/mqtt/m_mqtt_connect.c
Normal file
@@ -0,0 +1,62 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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/msg/mqtt/m_mqtt_connect.h"
|
||||
#include "aqhome/msg/mqtt/m_mqtt.h"
|
||||
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementation
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQH_MESSAGE *AQH_MqttMessageConnect_new(const char *protoName,
|
||||
uint8_t protoLevel,
|
||||
uint8_t connectFlags,
|
||||
uint16_t keepAliveTime,
|
||||
const char *clientId,
|
||||
const char *userName,
|
||||
const char *password)
|
||||
{
|
||||
AQH_MESSAGE *msg;
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 64, 0, 1);
|
||||
AQH_MqttMessage_AppendStringWithLenToBuffer(buf, protoName?protoName:"MQTT");
|
||||
GWEN_Buffer_AppendByte(buf, protoLevel?protoLevel:4);
|
||||
GWEN_Buffer_AppendByte(buf, connectFlags);
|
||||
GWEN_Buffer_AppendByte(buf, (keepAliveTime>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, keepAliveTime & 0xff);
|
||||
|
||||
AQH_MqttMessage_AppendStringWithLenToBuffer(buf, clientId);
|
||||
/* here could be inserted: will topic, will message */
|
||||
if (connectFlags & AQH_MQTTMSG_CONNECT_FLAGS_USERNAME)
|
||||
AQH_MqttMessage_AppendStringWithLenToBuffer(buf, userName);
|
||||
if (connectFlags & AQH_MQTTMSG_CONNECT_FLAGS_PASSWD)
|
||||
AQH_MqttMessage_AppendStringWithLenToBuffer(buf, password);
|
||||
|
||||
msg=AQH_MqttMessage_new(AQH_MQTTMSG_MSGTYPE_CONNECT, GWEN_Buffer_GetUsedBytes(buf), (const uint8_t*) GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
40
aqhome/msg/mqtt/m_mqtt_connect.h
Normal file
40
aqhome/msg/mqtt/m_mqtt_connect.h
Normal file
@@ -0,0 +1,40 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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_M_MQTT_CONNECT_H
|
||||
#define AQH_M_MQTT_CONNECT_H
|
||||
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/ipc2/message.h>
|
||||
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
#define AQH_MQTTMSG_CONNECT_FLAGS_USERNAME 0x80u
|
||||
#define AQH_MQTTMSG_CONNECT_FLAGS_PASSWD 0x40u
|
||||
#define AQH_MQTTMSG_CONNECT_FLAGS_WILL_RETAIN 0x20u
|
||||
#define AQH_MQTTMSG_CONNECT_FLAGS_WILL_QOS2 0x10u
|
||||
#define AQH_MQTTMSG_CONNECT_FLAGS_WILL_QOS1 0x08u
|
||||
#define AQH_MQTTMSG_CONNECT_FLAGS_WILL_FLAG 0x04u
|
||||
#define AQH_MQTTMSG_CONNECT_FLAGS_CLEAN_SESSION 0x01u
|
||||
|
||||
|
||||
|
||||
AQHOME_API AQH_MESSAGE *AQH_MqttMessageConnect_new(const char *protoName,
|
||||
uint8_t protoLevel,
|
||||
uint8_t connectFlags,
|
||||
uint16_t keepAliveTime,
|
||||
const char *clientId,
|
||||
const char *userName,
|
||||
const char *password);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
145
aqhome/msg/mqtt/m_mqtt_publish.c
Normal file
145
aqhome/msg/mqtt/m_mqtt_publish.c
Normal file
@@ -0,0 +1,145 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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/msg/mqtt/m_mqtt_publish.h"
|
||||
#include "aqhome/msg/mqtt/m_mqtt.h"
|
||||
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementation
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
AQH_MESSAGE *AQH_MqttMessagePublish_new(uint8_t flags, uint16_t packetId,
|
||||
const char *sTopic, const uint8_t *messagePtr, uint32_t messageLen)
|
||||
{
|
||||
AQH_MESSAGE *msg;
|
||||
GWEN_BUFFER *buf;
|
||||
|
||||
buf=GWEN_Buffer_new(0, 64, 0, 1);
|
||||
AQH_MqttMessage_AppendStringWithLenToBuffer(buf, sTopic?sTopic:"");
|
||||
if (flags & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
|
||||
GWEN_Buffer_AppendByte(buf, (packetId>>8) & 0xff);
|
||||
GWEN_Buffer_AppendByte(buf, packetId & 0xff);
|
||||
}
|
||||
|
||||
/* payload */
|
||||
if (messagePtr && messageLen)
|
||||
GWEN_Buffer_AppendBytes(buf, (const char*) messagePtr, messageLen);
|
||||
|
||||
msg=AQH_MqttMessage_new(AQH_MQTTMSG_MSGTYPE_PUBLISH | flags,
|
||||
GWEN_Buffer_GetUsedBytes(buf),
|
||||
(const uint8_t*) GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_MqttMessagePublish_GetPacketId(const AQH_MESSAGE *msg)
|
||||
{
|
||||
if (AQH_MqttMessage_GetTypeAndFlags(msg) & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
|
||||
int idx;
|
||||
|
||||
idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
|
||||
if (idx>0) {
|
||||
int len;
|
||||
|
||||
len=(int)AQH_Message_GetUsedSize(msg)-idx;
|
||||
if (len>1) {
|
||||
uint8_t *ptr;
|
||||
int rv;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg);
|
||||
rv=AQH_MqttMessage_SkipStringAt(ptr, len);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
idx+=rv;
|
||||
ptr+=rv;
|
||||
len-=rv;
|
||||
if (idx<len)
|
||||
return (ptr[0]<<8)+ptr[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *AQH_MqttMessagePublish_ExtractTopic(const AQH_MESSAGE *msg)
|
||||
{
|
||||
int idx;
|
||||
|
||||
idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
|
||||
if (idx>0) {
|
||||
uint8_t *ptr;
|
||||
int len;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg)+idx;
|
||||
len=(int)AQH_Message_GetUsedSize(msg)-idx;
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Extracting string from %d (remaining len=%d)", idx, len);
|
||||
if (len>1)
|
||||
return AQH_MqttMessage_ExtractStringAt(ptr, len);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char *AQH_MqttMessagePublish_ExtractValue(const AQH_MESSAGE *msg)
|
||||
{
|
||||
int idx;
|
||||
|
||||
idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
|
||||
if (idx>0) {
|
||||
uint8_t *ptr;
|
||||
int len;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg)+idx;
|
||||
len=(int)AQH_Message_GetUsedSize(msg)-idx;
|
||||
if (len>1) {
|
||||
int rv;
|
||||
|
||||
rv=AQH_MqttMessage_SkipStringAt(ptr, len);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return NULL;
|
||||
}
|
||||
ptr+=rv;
|
||||
len-=rv;
|
||||
if (AQH_MqttMessage_GetTypeAndFlags(msg) & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
|
||||
ptr+=2;
|
||||
len-=2;
|
||||
}
|
||||
if (len>0) {
|
||||
char *result;
|
||||
|
||||
result=(char*) malloc(len+1);
|
||||
if (result) {
|
||||
memmove(result, ptr, len);
|
||||
result[len]=0;
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
29
aqhome/msg/mqtt/m_mqtt_publish.h
Normal file
29
aqhome/msg/mqtt/m_mqtt_publish.h
Normal file
@@ -0,0 +1,29 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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_M_MQTT_PUBLISH_H
|
||||
#define AQH_M_MQTT_PUBLISH_H
|
||||
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/ipc2/message.h>
|
||||
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
|
||||
AQHOME_API AQH_MESSAGE *AQH_MqttMessagePublish_new(uint8_t flags, uint16_t packetId,
|
||||
const char *sTopic, const uint8_t *messagePtr, uint32_t messageLen);
|
||||
AQHOME_API int AQH_MqttMessagePublish_GetPacketId(const AQH_MESSAGE *msg);
|
||||
AQHOME_API char *AQH_MqttMessagePublish_ExtractTopic(const AQH_MESSAGE *msg);
|
||||
AQHOME_API char *AQH_MqttMessagePublish_ExtractValue(const AQH_MESSAGE *msg);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
93
aqhome/msg/mqtt/m_mqtt_suback.c
Normal file
93
aqhome/msg/mqtt/m_mqtt_suback.c
Normal file
@@ -0,0 +1,93 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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/msg/mqtt/m_mqtt_suback.h"
|
||||
#include "aqhome/msg/mqtt/m_mqtt.h"
|
||||
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementation
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQH_MESSAGE *AQH_MqttMessageSubAck_new(uint8_t flags, uint16_t packetId, uint8_t result)
|
||||
{
|
||||
AQH_MESSAGE *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=AQH_MqttMessage_new(AQH_MQTTMSG_MSGTYPE_SUBACK | flags,
|
||||
GWEN_Buffer_GetUsedBytes(buf),
|
||||
(const uint8_t*) GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_MqttMessageSubAck_GetPacketId(const AQH_MESSAGE *msg)
|
||||
{
|
||||
int idx;
|
||||
|
||||
idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
|
||||
if (idx>0) {
|
||||
int len;
|
||||
|
||||
len=(int)AQH_Message_GetUsedSize(msg)-idx;
|
||||
if (len>1) {
|
||||
if (AQH_MqttMessage_GetTypeAndFlags(msg) & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1)) {
|
||||
uint8_t *ptr;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg);
|
||||
return (ptr[0]<<8)+ptr[1];
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int AQH_MqttMessageSubAck_GetResultCode(const AQH_MESSAGE *msg)
|
||||
{
|
||||
int idx;
|
||||
|
||||
idx=AQH_MqttMessage_GetOffsetOfPayload(msg);
|
||||
if (idx>0) {
|
||||
int len;
|
||||
|
||||
if (AQH_MqttMessage_GetTypeAndFlags(msg) & (AQH_MQTTMSG_FLAGS_QOS2 | AQH_MQTTMSG_FLAGS_QOS1))
|
||||
idx+=2;
|
||||
len=(int)AQH_Message_GetUsedSize(msg)-idx;
|
||||
if (len>0) {
|
||||
uint8_t *ptr;
|
||||
|
||||
ptr=AQH_Message_GetMsgPointer(msg);
|
||||
return *ptr;
|
||||
}
|
||||
}
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
|
||||
|
||||
|
||||
27
aqhome/msg/mqtt/m_mqtt_suback.h
Normal file
27
aqhome/msg/mqtt/m_mqtt_suback.h
Normal file
@@ -0,0 +1,27 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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_M_MQTT_SUBACK_H
|
||||
#define AQH_M_MQTT_SUBACK_H
|
||||
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/ipc2/message.h>
|
||||
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
|
||||
AQHOME_API AQH_MESSAGE *AQH_MqttMessageSubAck_new(uint8_t flags, uint16_t packetId, uint8_t result);
|
||||
AQHOME_API int AQH_MqttMessageSubAck_GetResultCode(const AQH_MESSAGE *msg);
|
||||
AQHOME_API int AQH_MqttMessageSubAck_GetPacketId(const AQH_MESSAGE *msg);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
46
aqhome/msg/mqtt/m_mqtt_subscribe.c
Normal file
46
aqhome/msg/mqtt/m_mqtt_subscribe.c
Normal file
@@ -0,0 +1,46 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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/msg/mqtt/m_mqtt_subscribe.h"
|
||||
#include "aqhome/msg/mqtt/m_mqtt.h"
|
||||
|
||||
#include <gwenhywfar/text.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementation
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQH_MESSAGE *AQH_MqttMessageSubscribe_new(uint8_t flags, uint16_t packetId, const char *sTopic, uint8_t requestedQos)
|
||||
{
|
||||
AQH_MESSAGE *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_MqttMessage_AppendStringWithLenToBuffer(buf, sTopic);
|
||||
GWEN_Buffer_AppendByte(buf, requestedQos);
|
||||
|
||||
msg=AQH_MqttMessage_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE | flags,
|
||||
GWEN_Buffer_GetUsedBytes(buf),
|
||||
(const uint8_t*) GWEN_Buffer_GetStart(buf));
|
||||
GWEN_Buffer_free(buf);
|
||||
return msg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
25
aqhome/msg/mqtt/m_mqtt_subscribe.h
Normal file
25
aqhome/msg/mqtt/m_mqtt_subscribe.h
Normal file
@@ -0,0 +1,25 @@
|
||||
/****************************************************************************
|
||||
* This file is part of the project AqHome.
|
||||
* AqHome (c) by 2025 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_M_MQTT_SUBSCRIBE_H
|
||||
#define AQH_M_MQTT_SUBSCRIBE_H
|
||||
|
||||
|
||||
#include <aqhome/api.h>
|
||||
#include <aqhome/ipc2/message.h>
|
||||
|
||||
#include <gwenhywfar/buffer.h>
|
||||
|
||||
|
||||
|
||||
AQHOME_API AQH_MESSAGE *AQH_MqttMessageSubscribe_new(uint8_t flags, uint16_t packetId, const char *sTopic, uint8_t requestedQos);
|
||||
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user