aqhome: adapted server aqhome-mqttlog to events2 api.
This commit is contained in:
@@ -49,6 +49,7 @@
|
||||
msgwriter.h
|
||||
ipcmsgreader.h
|
||||
nodemsgreader.h
|
||||
mqttmsgreader.h
|
||||
ttyobject.h
|
||||
tcpd_object.h
|
||||
tcp_object.h
|
||||
@@ -58,6 +59,8 @@
|
||||
ipc_server.h
|
||||
ipc_client.h
|
||||
tty_endpoint.h
|
||||
mqtt_endpoint.h
|
||||
mqtt_client.h
|
||||
</headers>
|
||||
|
||||
|
||||
@@ -79,6 +82,7 @@
|
||||
msgwriter.c
|
||||
ipcmsgreader.c
|
||||
nodemsgreader.c
|
||||
mqttmsgreader.c
|
||||
ttyobject.c
|
||||
tcpd_object.c
|
||||
tcp_object.c
|
||||
@@ -89,6 +93,8 @@
|
||||
ipc_client.c
|
||||
ipc_endpoint.c
|
||||
tty_endpoint.c
|
||||
mqtt_endpoint.c
|
||||
mqtt_client.c
|
||||
</sources>
|
||||
|
||||
|
||||
|
||||
@@ -395,11 +395,17 @@ void AQH_Endpoint_AddMsgOut(AQH_OBJECT *o, AQH_MESSAGE *msg)
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_ENDPOINT, o);
|
||||
if (xo) {
|
||||
AQH_Message_List_Add(msg, xo->msgOutList);
|
||||
if (xo->msgWriter && AQH_Message_List_GetCount(xo->msgOutList)==1) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Enabling msgWriter, sending message");
|
||||
AQH_MsgWriter_SendMsg(xo->msgWriter, AQH_Message_GetMsgPointer(msg), AQH_Message_GetUsedSize(msg));
|
||||
AQH_Object_Enable(xo->msgWriter);
|
||||
if (AQH_Message_GetUsedSize(msg)<1) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Empty message, not sending");
|
||||
AQH_Message_free(msg);
|
||||
}
|
||||
else {
|
||||
AQH_Message_List_Add(msg, xo->msgOutList);
|
||||
if (xo->msgWriter && AQH_Message_List_GetCount(xo->msgOutList)==1) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Enabling msgWriter, sending message");
|
||||
AQH_MsgWriter_SendMsg(xo->msgWriter, AQH_Message_GetMsgPointer(msg), AQH_Message_GetUsedSize(msg));
|
||||
AQH_Object_Enable(xo->msgWriter);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
53
aqhome/ipc2/mqtt_client.c
Normal file
53
aqhome/ipc2/mqtt_client.c
Normal file
@@ -0,0 +1,53 @@
|
||||
/****************************************************************************
|
||||
* 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 "./mqtt_client.h"
|
||||
|
||||
#include <aqhome/ipc2/mqttmsgreader.h>
|
||||
#include <aqhome/ipc2/msgwriter.h>
|
||||
#include <aqhome/events2/fdobject.h>
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* code
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQH_OBJECT *AQH_MqttClientObject_new(AQH_EVENT_LOOP *eventLoop, int fd)
|
||||
{
|
||||
int fdCopy;
|
||||
AQH_OBJECT *fdReader;
|
||||
AQH_OBJECT *fdWriter;
|
||||
AQH_OBJECT *msgReader;
|
||||
AQH_OBJECT *msgWriter;
|
||||
AQH_OBJECT *endpoint;
|
||||
|
||||
fdCopy=dup(fd);
|
||||
|
||||
fdReader=AQH_FdObject_new(eventLoop, fd, AQH_FDOBJECT_FDMODE_READ);
|
||||
msgReader=AQH_MqttMsgReader_new(eventLoop, fdReader);
|
||||
AQH_Object_Enable(msgReader);
|
||||
|
||||
fdWriter=AQH_FdObject_new(eventLoop, fdCopy, AQH_FDOBJECT_FDMODE_WRITE);
|
||||
msgWriter=AQH_MsgWriter_new(eventLoop, fdWriter);
|
||||
|
||||
endpoint=AQH_Endpoint_new(eventLoop, msgReader, msgWriter);
|
||||
return endpoint;
|
||||
}
|
||||
|
||||
|
||||
|
||||
27
aqhome/ipc2/mqtt_client.h
Normal file
27
aqhome/ipc2/mqtt_client.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_MQTT_CLIENT_H
|
||||
#define AQH_MQTT_CLIENT_H
|
||||
|
||||
|
||||
#include <aqhome/ipc2/endpoint.h>
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param eventLoop pointer to eventLoop
|
||||
* @param fd connected non-blocking socket to work with (see @ref AQH_TcpObject_CreateConnectedSocket).
|
||||
*/
|
||||
AQHOME_API AQH_OBJECT *AQH_MqttClientObject_new(AQH_EVENT_LOOP *eventLoop, int fd);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
74
aqhome/ipc2/mqtt_endpoint.c
Normal file
74
aqhome/ipc2/mqtt_endpoint.c
Normal file
@@ -0,0 +1,74 @@
|
||||
/****************************************************************************
|
||||
* 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 "./mqtt_endpoint.h"
|
||||
#include "aqhome/msg/mqtt/m_mqtt.h"
|
||||
|
||||
#include <gwenhywfar/debug.h>
|
||||
|
||||
#include <time.h>
|
||||
|
||||
|
||||
|
||||
AQH_MESSAGE *AQH_MqttEndpoint_WaitForConnAckMsg(AQH_OBJECT *mqttEndpoint, int timeoutInSeconds)
|
||||
{
|
||||
return AQH_MqttEndpoint_WaitForMsg(mqttEndpoint, AQH_MQTTMSG_MSGTYPE_CONNACK, timeoutInSeconds);
|
||||
}
|
||||
|
||||
|
||||
|
||||
AQH_MESSAGE *AQH_MqttEndpoint_WaitForMsg(AQH_OBJECT *mqttEndpoint, uint8_t t, int timeoutInSeconds)
|
||||
{
|
||||
time_t startTime;
|
||||
|
||||
startTime=time(NULL);
|
||||
t&=0xf0;
|
||||
|
||||
for (;;) {
|
||||
AQH_MESSAGE_LIST *msgList;
|
||||
time_t now;
|
||||
|
||||
msgList=AQH_Endpoint_GetMsgInList(mqttEndpoint);
|
||||
if (msgList) {
|
||||
AQH_MESSAGE *msg;
|
||||
|
||||
msg=AQH_Message_List_First(msgList);
|
||||
while(msg) {
|
||||
uint8_t msgTypeAndFlags;
|
||||
|
||||
msgTypeAndFlags=AQH_MqttMessage_GetTypeAndFlags(msg);
|
||||
if ((msgTypeAndFlags & 0xf0) == t) {
|
||||
AQH_Message_List_Del(msg);
|
||||
return msg;
|
||||
}
|
||||
msg=AQH_Message_List_Next(msg);
|
||||
}
|
||||
}
|
||||
|
||||
now=time(NULL);
|
||||
if (now-startTime>timeoutInSeconds) {
|
||||
DBG_INFO(NULL, "Timeout");
|
||||
break;
|
||||
}
|
||||
|
||||
AQH_EventLoop_Run(AQH_Object_GetEventLoop(mqttEndpoint), 500);
|
||||
} /* for */
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
23
aqhome/ipc2/mqtt_endpoint.h
Normal file
23
aqhome/ipc2/mqtt_endpoint.h
Normal file
@@ -0,0 +1,23 @@
|
||||
/****************************************************************************
|
||||
* 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_MQTT_ENDPOINT_H
|
||||
#define AQH_MQTT_ENDPOINT_H
|
||||
|
||||
|
||||
#include <aqhome/ipc2/endpoint.h>
|
||||
|
||||
|
||||
|
||||
AQHOME_API AQH_MESSAGE *AQH_MqttEndpoint_WaitForConnAckMsg(AQH_OBJECT *mqttEndpoint, int timeoutInSeconds);
|
||||
AQHOME_API AQH_MESSAGE *AQH_MqttEndpoint_WaitForMsg(AQH_OBJECT *mqttEndpoint, uint8_t t, int timeoutInSeconds);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
182
aqhome/ipc2/mqttmsgreader.c
Normal file
182
aqhome/ipc2/mqttmsgreader.c
Normal file
@@ -0,0 +1,182 @@
|
||||
/****************************************************************************
|
||||
* 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 "./mqttmsgreader.h"
|
||||
#include "./msgreader_p.h"
|
||||
#include <aqhome/events2/fdobject.h>
|
||||
|
||||
#include <gwenhywfar/inherit.h>
|
||||
#include <gwenhywfar/debug.h>
|
||||
#include <gwenhywfar/endianfns.h>
|
||||
#include <gwenhywfar/text.h>
|
||||
|
||||
#include <sys/socket.h>
|
||||
|
||||
|
||||
#define AQH_MSG_READER_HEADER_SIZE 2
|
||||
#define AQH_MSG_READER_MINMSGSIZE 12
|
||||
#define AQH_MSG_READER_MAXMSGSIZE 10240
|
||||
|
||||
#define AQH_MSG_READER_FLAGS_READBODY 0x0001
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* forward declarations
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
static int _readMsg(AQH_OBJECT *o);
|
||||
static int _readHeaderFromRingbuffer(AQH_MSG_READER *xo);
|
||||
|
||||
|
||||
|
||||
/* ------------------------------------------------------------------------------------------------
|
||||
* implementation
|
||||
* ------------------------------------------------------------------------------------------------
|
||||
*/
|
||||
|
||||
AQH_OBJECT *AQH_MqttMsgReader_new(AQH_EVENT_LOOP *eventLoop, AQH_OBJECT *fdObject)
|
||||
{
|
||||
AQH_OBJECT *o;
|
||||
|
||||
o=AQH_MsgReader_new(eventLoop, fdObject);
|
||||
AQH_MsgReader_SetReadMsgFn(o, _readMsg);
|
||||
|
||||
return o;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _readMsg(AQH_OBJECT *o)
|
||||
{
|
||||
AQH_MSG_READER *xo;
|
||||
|
||||
xo=AQH_MsgReader_GetData(o);
|
||||
if (xo) {
|
||||
int rv;
|
||||
|
||||
if (!(xo->flags & AQH_MSG_READER_FLAGS_READBODY)) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Reading header");
|
||||
rv=_readHeaderFromRingbuffer(xo);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
}
|
||||
|
||||
if (xo->flags & AQH_MSG_READER_FLAGS_READBODY) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Reading body");
|
||||
/* reading remainder of msg directly into allocated buffer */
|
||||
rv=AQH_MsgReader_ReadRemainderFromRingbuffer(o);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
|
||||
return rv;
|
||||
}
|
||||
else if (rv==1) {
|
||||
int msgLen;
|
||||
uint8_t *msgPtr;
|
||||
|
||||
msgLen=xo->bytesReceived;
|
||||
msgPtr=xo->currentMsgBuf;
|
||||
|
||||
xo->bytesReceived=0;
|
||||
xo->bytesLeft=0;
|
||||
xo->currentMsgBuf=NULL;
|
||||
xo->flags&=~AQH_MSG_READER_FLAGS_READBODY;
|
||||
|
||||
DBG_ERROR(NULL, "Received message:");
|
||||
//GWEN_Text_LogString((const char*) msgPtr, msgLen, NULL, GWEN_LoggerLevel_Error);
|
||||
rv=AQH_Object_EmitSignal(o, AQH_MSG_READER_SIGNAL_MSGRECVD, msgLen, (void*) msgPtr);
|
||||
if (rv==0) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Received message ignored");
|
||||
}
|
||||
free(msgPtr);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int _readHeaderFromRingbuffer(AQH_MSG_READER *xo)
|
||||
{
|
||||
int remainingBytesInBuffer;
|
||||
uint32_t mqttPayloadLen=0;
|
||||
int idx=0;
|
||||
const uint8_t *ptr;
|
||||
uint8_t lenByte;
|
||||
int shift=0;
|
||||
int rv;
|
||||
|
||||
ptr=xo->headerBuffer;
|
||||
|
||||
remainingBytesInBuffer=xo->bytesReceived;
|
||||
if (remainingBytesInBuffer>AQH_MSG_READER_HEADERBUFFER_SIZE) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Error in message (msg size not determined within %d bytes)", remainingBytesInBuffer);
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
|
||||
/* read type and flags (first byte) */
|
||||
if (xo->bytesReceived<=idx) {
|
||||
rv=GWEN_RingBuffer_ReadByte(xo->ringBuffer);
|
||||
if (rv!=-1) {
|
||||
xo->headerBuffer[idx]=rv;
|
||||
xo->bytesReceived++;
|
||||
remainingBytesInBuffer++;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
idx++;
|
||||
|
||||
/* read address bytes */
|
||||
while(idx<6) { /* max 4 bytes size plus type/flags byte */
|
||||
if (xo->bytesReceived<=idx) {
|
||||
rv=GWEN_RingBuffer_ReadByte(xo->ringBuffer);
|
||||
if (rv!=-1) {
|
||||
xo->headerBuffer[idx]=rv;
|
||||
xo->bytesReceived++;
|
||||
remainingBytesInBuffer++;
|
||||
}
|
||||
else
|
||||
return 0;
|
||||
}
|
||||
lenByte=ptr[idx];
|
||||
mqttPayloadLen+=(lenByte & 0x7f)<<shift;
|
||||
if (!(lenByte & 0x80)) {
|
||||
uint32_t fullMsgSize;
|
||||
|
||||
/* last byte of size, finish */
|
||||
fullMsgSize=mqttPayloadLen+idx+1;
|
||||
xo->bytesLeft=(fullMsgSize-xo->bytesReceived);
|
||||
|
||||
xo->currentMsgBuf=(uint8_t*) malloc(fullMsgSize);
|
||||
memmove(xo->currentMsgBuf, xo->headerBuffer, xo->bytesReceived);
|
||||
xo->bytesLeft=fullMsgSize-xo->bytesReceived;
|
||||
xo->flags|=AQH_MSG_READER_FLAGS_READBODY;
|
||||
DBG_ERROR(AQH_LOGDOMAIN,
|
||||
"Got size: full size=%d, payload pos=%d, payload size=%d (%04x)",
|
||||
fullMsgSize, idx+1, mqttPayloadLen, mqttPayloadLen);
|
||||
return 1; /* size successfully determined */
|
||||
}
|
||||
shift+=7;
|
||||
idx++;
|
||||
}
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Bad MQTT message (could not determine message length)");
|
||||
return GWEN_ERROR_BAD_DATA;
|
||||
}
|
||||
|
||||
|
||||
|
||||
20
aqhome/ipc2/mqttmsgreader.h
Normal file
20
aqhome/ipc2/mqttmsgreader.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/****************************************************************************
|
||||
* 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_MQTTMSGREADER_H
|
||||
#define AQH_MQTTMSGREADER_H
|
||||
|
||||
#include <aqhome/events2/object.h>
|
||||
|
||||
|
||||
AQH_OBJECT *AQH_MqttMsgReader_new(AQH_EVENT_LOOP *eventLoop, AQH_OBJECT *fdObject);
|
||||
|
||||
|
||||
|
||||
#endif
|
||||
|
||||
@@ -23,7 +23,7 @@
|
||||
|
||||
#define AQH_MSGREADER_SKIPTIME_IN_MS 20
|
||||
|
||||
#define AQH_MSGREADER_FLAGS_SKIP 0x0001
|
||||
#define AQH_MSGREADER_FLAGS_SKIP 0x80000000
|
||||
|
||||
|
||||
|
||||
@@ -109,8 +109,10 @@ void AQH_MsgReader_SetFlags(AQH_OBJECT *o, uint32_t f)
|
||||
AQH_MSG_READER *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o);
|
||||
if (xo)
|
||||
if (xo) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Set flags: %08x", f);
|
||||
xo->flags=f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -120,8 +122,10 @@ void AQH_MsgReader_AddFlags(AQH_OBJECT *o, uint32_t f)
|
||||
AQH_MSG_READER *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o);
|
||||
if (xo)
|
||||
if (xo) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Adding flags: %08x", f);
|
||||
xo->flags|=f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -131,8 +135,10 @@ void AQH_MsgReader_SubFlags(AQH_OBJECT *o, uint32_t f)
|
||||
AQH_MSG_READER *xo;
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o);
|
||||
if (xo)
|
||||
if (xo) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Clearing flags: %08x", f);
|
||||
xo->flags&=~f;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -339,35 +345,45 @@ int AQH_MsgReader_ReadRemainderFromRingbuffer(AQH_OBJECT *o)
|
||||
|
||||
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o);
|
||||
if (xo) {
|
||||
uint32_t bytesInRingBuffer;
|
||||
uint32_t bytesToRead;
|
||||
int rv;
|
||||
if (xo->bytesLeft==0) {
|
||||
/* msg finished */
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Message complete");
|
||||
return 1;
|
||||
}
|
||||
else {
|
||||
uint32_t bytesInRingBuffer;
|
||||
uint32_t bytesToRead;
|
||||
int rv;
|
||||
|
||||
bytesInRingBuffer=GWEN_RingBuffer_GetUsedBytes(xo->ringBuffer);
|
||||
|
||||
/* still reading header */
|
||||
bytesToRead=xo->bytesLeft;
|
||||
if (bytesInRingBuffer<bytesToRead)
|
||||
bytesToRead=bytesInRingBuffer;
|
||||
if (bytesToRead) {
|
||||
uint32_t xferSize;
|
||||
|
||||
xferSize=bytesToRead;
|
||||
rv=GWEN_RingBuffer_ReadBytes(xo->ringBuffer, (char*) (xo->currentMsgBuf+xo->bytesReceived), &xferSize);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Ringbuffer empty");
|
||||
return 0;
|
||||
bytesInRingBuffer=GWEN_RingBuffer_GetUsedBytes(xo->ringBuffer);
|
||||
|
||||
/* still reading header */
|
||||
bytesToRead=xo->bytesLeft;
|
||||
if (bytesInRingBuffer<bytesToRead)
|
||||
bytesToRead=bytesInRingBuffer;
|
||||
if (bytesToRead) {
|
||||
uint32_t xferSize;
|
||||
|
||||
xferSize=bytesToRead;
|
||||
rv=GWEN_RingBuffer_ReadBytes(xo->ringBuffer, (char*) (xo->currentMsgBuf+xo->bytesReceived), &xferSize);
|
||||
if (rv<0) {
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Ringbuffer empty");
|
||||
return 0;
|
||||
}
|
||||
if (xferSize<bytesToRead) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Read fewer bytes than available?");
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
xo->bytesReceived+=xferSize;
|
||||
xo->bytesLeft-=xferSize;
|
||||
if (xo->bytesLeft==0) {
|
||||
/* msg finished */
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Message complete");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
if (xferSize<bytesToRead) {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Read fewer bytes than available?");
|
||||
return GWEN_ERROR_GENERIC;
|
||||
}
|
||||
xo->bytesReceived+=xferSize;
|
||||
xo->bytesLeft-=xferSize;
|
||||
if (xo->bytesLeft==0) {
|
||||
/* msg finished */
|
||||
DBG_INFO(AQH_LOGDOMAIN, "Message complete");
|
||||
return 1;
|
||||
else {
|
||||
DBG_ERROR(AQH_LOGDOMAIN, "Nothing to read??");
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
@@ -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