aqhome: adapted server aqhome-mqttlog to events2 api.

This commit is contained in:
Martin Preuss
2025-03-08 01:03:22 +01:00
parent 58c6d12e36
commit ca2103f7b3
38 changed files with 3300 additions and 205 deletions

View File

@@ -37,30 +37,23 @@
</setVar> </setVar>
<headers dist="true" > <headers dist="true" >
init.h
fini.h
loop.h
loop_ipc.h
loop_mqtt.h
aqhome_mqtt.h
aqhome_mqtt_p.h
xmlread.h xmlread.h
xmlwrite.h xmlwrite.h
c_setdata.h c_setdata.h
server.h
server_p.h
s_publish.h
</headers> </headers>
<sources> <sources>
$(local/typefiles) $(local/typefiles)
aqhome_mqtt.c aqhome_mqtt.c
init.c
fini.c
loop.c
loop_ipc.c
loop_mqtt.c
main.c main.c
xmlread.c xmlread.c
xmlwrite.c xmlwrite.c
c_setdata.c c_setdata.c
server.c
s_publish.c
</sources> </sources>
<useTargets> <useTargets>

View File

@@ -10,10 +10,7 @@
# include <config.h> # include <config.h>
#endif #endif
#include "./init.h" #include "./server.h"
#include "./fini.h"
#include "./loop.h"
#include "./loop_mqtt.h"
#include "aqhome/aqhome.h" #include "aqhome/aqhome.h"
@@ -46,8 +43,9 @@
#define I18N(msg) msg #define I18N(msg) msg
#define I18S(msg) msg #define I18S(msg) msg
#define AQHOME_MQTTLOG_PING_INTERVAL 120 #define CONNCHECK_INTERVAL_IN_SECS 10
#define AQHOME_MQTTLOG_SAVE_INTERVAL 60 #define PING_INTERVAL_IN_SECS 120
#define SAVE_INTERVAL_IN_SECS 60
#define FULL_DEBUG #define FULL_DEBUG
@@ -58,7 +56,8 @@
* ------------------------------------------------------------------------------------------------ * ------------------------------------------------------------------------------------------------
*/ */
static void _serve(AQHOME_MQTT *aqh); static void _runService(AQH_OBJECT *aqh, AQH_EVENT_LOOP *eventLoop);
static int _diffInSeconds(time_t t1, time_t t0);
#ifdef HAVE_SIGNAL_H #ifdef HAVE_SIGNAL_H
static int _setSignalHandlers(void); static int _setSignalHandlers(void);
@@ -85,11 +84,10 @@ static int stopService=0;
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
AQHOME_MQTT *aqh; AQH_EVENT_LOOP *eventLoop;
GWEN_DB_NODE *dbArgs; AQH_OBJECT *aqh;
int rv; int rv;
GWEN_GUI *gui; GWEN_GUI *gui;
const char *s;
rv=GWEN_Init(); rv=GWEN_Init();
if (rv) { if (rv) {
@@ -100,14 +98,25 @@ int main(int argc, char **argv)
GWEN_Logger_Open(0, "aqhome-mqttlog", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User); GWEN_Logger_Open(0, "aqhome-mqttlog", 0, GWEN_LoggerType_Console, GWEN_LoggerFacility_User);
GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning); GWEN_Logger_SetLevel(0, GWEN_LoggerLevel_Warning);
rv=_setSignalHandlers();
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
}
rv=AQH_Init(); rv=AQH_Init();
if (rv<0) { if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv); DBG_INFO(NULL, "here (%d)", rv);
return 2; return 2;
} }
aqh=AqHomeMqtt_new(); gui=GWEN_Gui_CGui_new();
rv=AqHomeMqtt_Init(aqh, argc, argv); GWEN_Gui_SetGui(gui);
eventLoop=AQH_EventLoop_new();
aqh=AQH_MqttLogServer_new(eventLoop);
rv=AQH_MqttLogServer_Init(aqh, argc, argv);
if (rv<0) { if (rv<0) {
if (rv==GWEN_ERROR_CLOSE) if (rv==GWEN_ERROR_CLOSE)
return 1; return 1;
@@ -115,18 +124,11 @@ int main(int argc, char **argv)
return 2; return 2;
} }
dbArgs=AqHomeMqtt_GetDbArgs(aqh); _runService(aqh, eventLoop);
gui=GWEN_Gui_CGui_new();
s=GWEN_DB_GetCharValue(dbArgs, "charset", 0, NULL);
if (s && *s)
GWEN_Gui_SetCharSet(gui, s);
GWEN_Gui_SetGui(gui);
_serve(aqh); AQH_MqttLogServer_Fini(aqh);
AQH_Object_free(aqh);
AqHomeMqtt_Fini(aqh);
AqHomeMqtt_free(aqh);
GWEN_Gui_SetGui(NULL); GWEN_Gui_SetGui(NULL);
GWEN_Gui_free(gui); GWEN_Gui_free(gui);
@@ -136,77 +138,65 @@ int main(int argc, char **argv)
void _serve(AQHOME_MQTT *aqh) void _runService(AQH_OBJECT *aqh, AQH_EVENT_LOOP *eventLoop)
{ {
int rv; time_t timeStart;
int timeout; int timeout;
time_t startTime; time_t timeLastConnCheck;
time_t lastPingSendTime; time_t timeLastSave;
time_t lastSaveTime; time_t timeLastPingSend;
GWEN_DB_NODE *dbArgs; int rv;
startTime=time(NULL); timeout=AQH_MqttLogServer_GetTimeout(aqh);
lastSaveTime=time(NULL); timeStart=time(NULL);
timeLastConnCheck=time(NULL);
dbArgs=AqHomeMqtt_GetDbArgs(aqh); timeLastSave=time(NULL);
timeLastPingSend=time(NULL);
rv=_setSignalHandlers();
if (rv<0) {
DBG_ERROR(NULL, "Error setting signal handlers (%d)", rv);
return;
}
timeout=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 0);
lastPingSendTime=time(NULL);
while(!stopService) { while(!stopService) {
DBG_DEBUG(NULL, "Next loop");
AqHomeMqttLog_Loop(aqh, 2000);
if (timeout) {
time_t now; time_t now;
now=time(NULL); AQH_EventLoop_Run(eventLoop, 2000);
if ((now-startTime)>timeout) { AQH_MqttLogServer_HandleMqttMsgs(aqh);
DBG_ERROR(NULL, "Timeout, stopping service"); AQH_MqttLogServer_HandleBrokerMsgs(aqh);
break;
}
}
if (1){
time_t now;
now=time(NULL); now=time(NULL);
if (now-lastPingSendTime>AQHOME_MQTTLOG_PING_INTERVAL) {
rv=AqHomeMqttLog_SendPing(aqh); if (_diffInSeconds(now, timeLastConnCheck)>CONNCHECK_INTERVAL_IN_SECS) {
DBG_ERROR(NULL, "Check connections");
AQH_MqttLogServer_CheckBrokerConnection(aqh);
AQH_MqttLogServer_CheckMqttConnection(aqh);
timeLastConnCheck=now;
}
if (_diffInSeconds(now, timeLastPingSend)>PING_INTERVAL_IN_SECS) {
rv=AQH_MqttLogServer_SendPing(aqh);
if (rv<0) { if (rv<0) {
DBG_INFO(NULL, "Error sending PING"); DBG_INFO(NULL, "Error sending PING");
} }
lastPingSendTime=time(NULL); timeLastPingSend=time(NULL);
}
} }
if (1){ if (_diffInSeconds(now, timeLastSave)>SAVE_INTERVAL_IN_SECS) {
time_t now;
now=time(NULL);
if (now-lastSaveTime>AQHOME_MQTTLOG_SAVE_INTERVAL) {
DBG_ERROR(NULL, "Writing device files"); DBG_ERROR(NULL, "Writing device files");
rv=AqHomeMqtt_SaveRuntimeDeviceFiles(aqh); rv=AQH_MqttLogServer_SaveRuntimeDeviceFiles(aqh);
if (rv<0) { if (rv<0) {
DBG_INFO(NULL, "Error writing runtime data"); DBG_INFO(NULL, "Error writing device file");
}
lastSaveTime=time(NULL);
} }
timeLastSave=time(NULL);
} }
if (timeout && (_diffInSeconds(now, timeStart)>timeout)) {
DBG_INFO(NULL, "Timeout");
break;
}
} /* while */ } /* while */
rv=AqHomeMqtt_SaveRuntimeDeviceFiles(aqh);
rv=AQH_MqttLogServer_SaveRuntimeDeviceFiles(aqh);
if (rv<0) { if (rv<0) {
DBG_INFO(NULL, "Error writing runtime data"); DBG_INFO(NULL, "Error writing runtime data");
} }
} }
@@ -277,3 +267,8 @@ void _signalHandler(int s)
int _diffInSeconds(time_t t1, time_t t0)
{
return t1-t0;
}

View File

@@ -0,0 +1,436 @@
/****************************************************************************
* 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 "./s_publish.h"
#include "./server_p.h"
#include <aqhome/aqhome.h>
#include <aqhome/msg/ipc/data/m_ipcd.h>
#include <aqhome/msg/ipc/data/m_ipcd_multidata.h>
#include <aqhome/msg/ipc/data/m_ipcd_values.h>
#include <aqhome/ipc2/endpoint.h>
#include <aqhome/msg/mqtt/m_mqtt_publish.h>
#include <gwenhywfar/json_read.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/debug.h>
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static int _handlePublish(AQH_OBJECT *o, const char *rcvdTopic, const char *rcvdValue);
static void _handleNumTopic(AQH_MQTTLOG_SERVER *xo, AQHMQTT_DEVICE *device,
AQHMQTT_TOPIC *topic, const char *rcvdValue);
static void _handleJsonTopic(AQH_MQTTLOG_SERVER *xo, AQHMQTT_DEVICE *device,
AQHMQTT_TOPIC *topic, const char *rcvdValue);
static void _sendMessage(AQH_MQTTLOG_SERVER *xo, const AQHMQTT_DEVICE *device, const AQHMQTT_VALUE *value, const char *rcvdValue);
static void _announceDeviceToBroker(AQH_MQTTLOG_SERVER *xo, const AQHMQTT_DEVICE *device);
static void _sendAnnounceValueMessage(AQH_MQTTLOG_SERVER *xo, const AQHMQTT_DEVICE *device, const AQHMQTT_VALUE *value);
static AQH_VALUE *_mkMessageValue(const AQHMQTT_DEVICE *device, const AQHMQTT_VALUE *value);
static int _mqttValueTypeMessageValueType(int t);
static int _registerNewDeviceForTopic(AQH_MQTTLOG_SERVER *xo, const char *rcvdTopic);
static AQHMQTT_TOPIC *_findMaskMatchingTopic(AQHMQTT_TOPIC_LIST *topicList, const char *rcvdTopic, int dir);
static AQHMQTT_TOPIC *_findTopicMatchingTopic(AQHMQTT_TOPIC_LIST *topicList, const char *rcvdTopic, int dir);
static GWEN_BUFFER *_extractDeviceId(const AQHMQTT_TOPIC *topic, const char *rcvdTopic);
/* ------------------------------------------------------------------------------------------------
* code
* ------------------------------------------------------------------------------------------------
*/
void AQH_MqttLogServer_HandlePublishMsg(AQH_OBJECT *o, GWEN_UNUSED AQH_OBJECT *ep, const AQH_MESSAGE *msg)
{
if (o && msg) {
AQH_MQTTLOG_SERVER *xo;
xo=AQH_MqttLogServer_GetServerData(o);
if (xo && xo->registeredDeviceList) {
char *topic;
char *value;
topic=AQH_MqttMessagePublish_ExtractTopic(msg);
value=AQH_MqttMessagePublish_ExtractValue(msg);
if (topic && value) {
int rv;
rv=_handlePublish(o, topic, value);
if (rv!=1) {
DBG_INFO(NULL, "New topic \"%s\", trying to register", topic);
rv=_registerNewDeviceForTopic(xo, topic);
if (rv==1) {
rv=_handlePublish(o, topic, value);
if (rv!=1) {
DBG_ERROR(NULL, "Topic \"%s\" still not handled, SNH!", topic);
}
}
}
}
else {
DBG_ERROR(NULL, "Either topic or value missing in PUBLISH msg");
}
free(value);
free(topic);
}
}
}
int _handlePublish(AQH_OBJECT *o, const char *rcvdTopic, const char *rcvdValue)
{
if (o && rcvdTopic && *rcvdTopic) {
AQH_MQTTLOG_SERVER *xo;
xo=AQH_MqttLogServer_GetServerData(o);
if (xo && xo->registeredDeviceList) {
AQHMQTT_DEVICE *device;
device=AQHMQTT_Device_List_First(xo->registeredDeviceList);
while(device) {
AQHMQTT_TOPIC_LIST *topicList;
const char *sDeviceName;
const char *sDeviceId;
sDeviceName=AQHMQTT_Device_GetName(device);
sDeviceId=AQHMQTT_Device_GetId(device);
topicList=AQHMQTT_Device_GetTopicList(device);
if (topicList) {
AQHMQTT_TOPIC *topic;
topic=_findTopicMatchingTopic(topicList, rcvdTopic, AQHMQTT_TopicDir_In);
#if 0
if (topic==NULL) {
topic=_findMaskMatchingTopic(topicList, rcvdTopic, AQHMQTT_TopicDir_In);
if (topic)
AQHMQTT_Topic_SetTopic(topic, rcvdTopic);
}
#endif
if (topic) {
DBG_INFO(AQH_LOGDOMAIN,
"Handling topic \"%s\" for device type %s (id: %s)",
rcvdTopic,
sDeviceName, sDeviceId?sDeviceId:"<no id>");
if (AQHMQTT_Topic_GetTopicType(topic)==AQHMQTT_TopicType_Json)
_handleJsonTopic(xo, device, topic, rcvdValue);
else
_handleNumTopic(xo, device, topic, rcvdValue);
return 1;
}
}
device=AQHMQTT_Device_List_Next(device);
}
DBG_INFO(AQH_LOGDOMAIN, "ignoring topic \"%s\"", rcvdTopic);
}
}
return 0;
}
void _handleNumTopic(AQH_MQTTLOG_SERVER *xo, AQHMQTT_DEVICE *device,
AQHMQTT_TOPIC *topic, const char *rcvdValue)
{
AQHMQTT_VALUE_LIST *valueList;
valueList=AQHMQTT_Topic_GetValueList(topic);
if (valueList)
_sendMessage(xo, device, AQHMQTT_Value_List_First(valueList), rcvdValue);
else {
DBG_INFO(NULL, "No value list in device \"%s\"", AQHMQTT_Device_GetId(device));
}
}
void _handleJsonTopic(AQH_MQTTLOG_SERVER *xo, AQHMQTT_DEVICE *device,
AQHMQTT_TOPIC *topic, const char *rcvdValue)
{
GWEN_JSON_ELEM *jeRoot;
jeRoot=GWEN_JsonElement_fromString(rcvdValue);
if (jeRoot==NULL) {
DBG_INFO(NULL, "Could not parse JSON value: %s", rcvdValue);
}
else {
AQHMQTT_VALUE_LIST *valueList;
valueList=AQHMQTT_Topic_GetValueList(topic);
if (valueList) {
AQHMQTT_VALUE *value;
value=AQHMQTT_Value_List_First(valueList);
while(value) {
const char *path;
path=AQHMQTT_Value_GetPath(value);
if (path && *path) {
GWEN_JSON_ELEM *je;
je=GWEN_JsonElement_GetElementByPath(jeRoot, path, 0);
if (je) {
const char *s;
s=GWEN_JsonElement_GetData(je);
if (s && *s)
_sendMessage(xo, device, value, s);
}
}
value=AQHMQTT_Value_List_Next(value);
} /* while */
}
GWEN_JsonElement_free(jeRoot);
}
}
void _sendMessage(AQH_MQTTLOG_SERVER *xo, const AQHMQTT_DEVICE *device, const AQHMQTT_VALUE *value, const char *rcvdValue)
{
int rv;
double f;
const char *deviceName;
deviceName=AQHMQTT_Device_GetId(device);
rv=GWEN_Text_StringToDouble(rcvdValue, &f);
if (rv<0) {
DBG_ERROR(NULL, "Invalid value received from MQTT server (%s)", rcvdValue?rcvdValue:"<empty>");
}
else {
AQH_MESSAGE *pubMsg;
uint64_t now;
AQH_VALUE *msgValue;
now=(uint64_t) time(NULL);
msgValue=_mkMessageValue(device, value);
pubMsg=AQH_IpcdMessageMultiData_newForOne(AQH_MSGTYPE_IPC_DATA_UPDATEDATA,
AQH_Endpoint_GetNextMessageId(xo->brokerEndpoint), 0,
msgValue, now, f);
DBG_INFO(AQH_LOGDOMAIN, "BROKER UPDATE_DATA %s/%s: %f",
deviceName?deviceName:"<no device name>",
AQH_Value_GetName(msgValue), f);
AQH_Endpoint_AddMsgOut(xo->brokerEndpoint, pubMsg);
AQH_Value_free(msgValue);
}
}
void _announceDeviceToBroker(AQH_MQTTLOG_SERVER *xo, const AQHMQTT_DEVICE *device)
{
AQHMQTT_TOPIC_LIST *topicList;
topicList=AQHMQTT_Device_GetTopicList(device);
if (topicList) {
AQHMQTT_TOPIC *topic;
topic=AQHMQTT_Topic_List_First(topicList);
while(topic) {
AQHMQTT_VALUE_LIST *valueList;
valueList=AQHMQTT_Topic_GetValueList(topic);
if (valueList) {
const AQHMQTT_VALUE *value;
value=AQHMQTT_Value_List_First(valueList);
while(value) {
_sendAnnounceValueMessage(xo, device, value);
value=AQHMQTT_Value_List_Next(value);
}
}
topic=AQHMQTT_Topic_List_Next(topic);
}
}
}
void _sendAnnounceValueMessage(AQH_MQTTLOG_SERVER *xo, const AQHMQTT_DEVICE *device, const AQHMQTT_VALUE *value)
{
AQH_MESSAGE *pubMsg;
AQH_VALUE *msgValue;
msgValue=_mkMessageValue(device, value);
pubMsg=AQH_IpcdMessageValues_newForOne(AQH_MSGTYPE_IPC_DATA_ANNOUNCEVALUE,
AQH_Endpoint_GetNextMessageId(xo->brokerEndpoint), 0,
0, msgValue);
if (pubMsg) {
DBG_INFO(AQH_LOGDOMAIN, "BROKER ANNOUNCE_VALUE %s", AQH_Value_GetName(msgValue));
AQH_Endpoint_AddMsgOut(xo->brokerEndpoint, pubMsg);
}
AQH_Value_free(msgValue);
}
AQH_VALUE *_mkMessageValue(const AQHMQTT_DEVICE *device, const AQHMQTT_VALUE *value)
{
AQH_VALUE *msgValue;
msgValue=AQH_Value_new();
AQH_Value_SetDeviceName(msgValue, AQHMQTT_Device_GetId(device));
AQH_Value_SetName(msgValue, AQHMQTT_Value_GetName(value));
AQH_Value_SetValueUnits(msgValue, AQHMQTT_Value_GetValueUnits(value));
AQH_Value_SetValueType(msgValue, _mqttValueTypeMessageValueType(AQHMQTT_Value_GetValueType(value)));
return msgValue;
}
int _mqttValueTypeMessageValueType(int t)
{
switch(t){
case AQHMQTT_ValueType_Sensor: return AQH_ValueType_Sensor;
case AQHMQTT_ValueType_Actor: return AQH_ValueType_Actor;
default: break;
}
DBG_ERROR(AQH_LOGDOMAIN, "Invalid mqtt value type %d", t);
return AQH_ValueType_Sensor;
}
int _registerNewDeviceForTopic(AQH_MQTTLOG_SERVER *xo, const char *rcvdTopic)
{
if (rcvdTopic && *rcvdTopic) {
if (xo->availableDeviceList) {
AQHMQTT_DEVICE *device;
device=AQHMQTT_Device_List_First(xo->availableDeviceList);
while(device) {
AQHMQTT_TOPIC_LIST *topicList;
topicList=AQHMQTT_Device_GetTopicList(device);
if (topicList) {
AQHMQTT_TOPIC *topic;
topic=_findMaskMatchingTopic(topicList, rcvdTopic, AQHMQTT_TopicDir_In);
if (topic) {
GWEN_BUFFER *buf;
buf=_extractDeviceId(topic, rcvdTopic);
if (buf) {
AQHMQTT_DEVICE *newDevice;
newDevice=AQHMQTT_Device_dup(device);
AQHMQTT_Device_SetId(newDevice, GWEN_Buffer_GetStart(buf));
topic=_findMaskMatchingTopic(AQHMQTT_Device_GetTopicList(newDevice), rcvdTopic, AQHMQTT_TopicDir_In);
AQHMQTT_Topic_SetTopic(topic, rcvdTopic);
if (xo->registeredDeviceList==NULL)
xo->registeredDeviceList=AQHMQTT_Device_List_new();
DBG_ERROR(NULL, "Registered device \"%s\" (%s)", AQHMQTT_Device_GetId(newDevice), AQHMQTT_Device_GetName(newDevice));
AQHMQTT_Device_List_Add(newDevice, xo->registeredDeviceList);
_announceDeviceToBroker(xo, newDevice);
GWEN_Buffer_free(buf);
return 1;
}
}
}
device=AQHMQTT_Device_List_Next(device);
}
}
DBG_INFO(AQH_LOGDOMAIN, "ignoring topic \"%s\"", rcvdTopic);
}
return 0;
}
AQHMQTT_TOPIC *_findMaskMatchingTopic(AQHMQTT_TOPIC_LIST *topicList, const char *rcvdTopic, int dir)
{
if (topicList) {
AQHMQTT_TOPIC *topic;
topic=AQHMQTT_Topic_List_First(topicList);
while(topic) {
if (AQHMQTT_Topic_GetDirection(topic)==dir) {
const char *sMask;
sMask=AQHMQTT_Topic_GetMask(topic);
if (sMask && *sMask && GWEN_Text_ComparePattern(rcvdTopic, sMask, 0)!=-1) {
/* found a matching topic */
return topic;
}
}
topic=AQHMQTT_Topic_List_Next(topic);
}
}
return NULL;
}
AQHMQTT_TOPIC *_findTopicMatchingTopic(AQHMQTT_TOPIC_LIST *topicList, const char *rcvdTopic, int dir)
{
if (topicList) {
AQHMQTT_TOPIC *topic;
topic=AQHMQTT_Topic_List_First(topicList);
while(topic) {
if (AQHMQTT_Topic_GetDirection(topic)==dir) {
const char *sTopic;
sTopic=AQHMQTT_Topic_GetTopic(topic);
if (sTopic && *sTopic && strcasecmp(rcvdTopic, sTopic)==0) {
return topic;
}
}
topic=AQHMQTT_Topic_List_Next(topic);
}
}
return NULL;
}
GWEN_BUFFER *_extractDeviceId(const AQHMQTT_TOPIC *topic, const char *rcvdTopic)
{
const char *sBefore;
const char *sAfter;
sBefore=AQHMQTT_Topic_GetBeforeId(topic);
sAfter=AQHMQTT_Topic_GetAfterId(topic);
if (sBefore && *sBefore && sAfter && *sAfter) {
GWEN_BUFFER *buf;
int rv;
buf=GWEN_Buffer_new(0, 256, 0, 1);
GWEN_Buffer_AppendString(buf, rcvdTopic);
rv=GWEN_Buffer_KeepTextBetweenStrings(buf, sBefore, sAfter, 1);
if (rv<0) {
DBG_INFO(NULL, "Could not extract id from [%s] (beforeId=%s, afterId=%s)", rcvdTopic, sBefore, sAfter);
GWEN_Buffer_free(buf);
return NULL;
}
return buf;
}
return NULL;
}

View 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 AQHOMEMQTT_S_PUBLISH_H
#define AQHOMEMQTT_S_PUBLISH_H
#include "./aqhome_mqtt.h"
#include <aqhome/events2/object.h>
#include <aqhome/ipc2/message.h>
void AQH_MqttLogServer_HandlePublishMsg(AQH_OBJECT *o, AQH_OBJECT *ep, const AQH_MESSAGE *msg);
#endif

1127
apps/aqhome-mqttlog/server.c Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,75 @@
/****************************************************************************
* 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 SERVER_H
#define SERVER_H
#include "aqhome-mqttlog/types/device.h"
#include "aqhome/events2/object.h"
#define AQH_ENDPOINT_PERMS_LISTVALUES 0x0001
#define AQH_ENDPOINT_PERMS_READVALUE 0x0002
#define AQH_ENDPOINT_PERMS_ADDVALUE 0x0004
#define AQH_ENDPOINT_PERMS_LISTDATA 0x0010
#define AQH_ENDPOINT_PERMS_READDATA 0x0020
#define AQH_ENDPOINT_PERMS_ADDDATA 0x0040
#define AQH_ENDPOINT_PERMS_SETDATA 0x0080
#define AQH_ENDPOINT_PERMS_LISTDEVICES 0x0100
#define AQH_ENDPOINT_PERMS_READDEVICE 0x0200
#define AQH_ENDPOINT_PERMS_ADDDEVICE 0x0400
#define AQH_ENDPOINT_PERMS_MODDEVICE 0x0800
AQH_OBJECT *AQH_MqttLogServer_new(AQH_EVENT_LOOP *eventLoop);
int AQH_MqttLogServer_Init(AQH_OBJECT *o, int argc, char **argv);
void AQH_MqttLogServer_Fini(AQH_OBJECT *o);
void AQH_MqttLogServer_ReloadDeviceFiles(AQH_OBJECT *o);
void AQH_MqttLogServer_LoadRuntimeDeviceFiles(AQH_OBJECT *o);
int AQH_MqttLogServer_SaveRuntimeDeviceFiles(AQH_OBJECT *o);
/* loop functions */
void AQH_MqttLogServer_HandleBrokerMsgs(AQH_OBJECT *o);
void AQH_MqttLogServer_HandleMqttMsgs(AQH_OBJECT *o);
void AQH_MqttLogServer_CheckBrokerConnection(AQH_OBJECT *o);
void AQH_MqttLogServer_CheckMqttConnection(AQH_OBJECT *o);
int AQH_MqttLogServer_SendPing(AQH_OBJECT *o);
/* getters and setters */
int AQH_MqttLogServer_GetTimeout(const AQH_OBJECT *o);
void AQH_MqttLogServer_SetPidFile(AQH_OBJECT *o, const char *s);
void AQH_MqttLogServer_SetDeviceFile(AQH_OBJECT *o, const char *s);
/* device management */
AQHMQTT_DEVICE_LIST *AQH_MqttLogServer_GetAvailableDeviceList(const AQH_OBJECT *o);
void AQH_MqttLogServer_SetAvailableDeviceList(AQH_OBJECT *o, AQHMQTT_DEVICE_LIST *dl);
void AQH_MqttLogServer_SetRegisteredDeviceList(AQH_OBJECT *o, AQHMQTT_DEVICE_LIST *dl);
AQHMQTT_DEVICE *AQH_MqttLogServer_FindRegisteredDevice(AQH_OBJECT *o, const char *wantedDeviceId);
void AQH_MqttLogServer_DumpRegisteredDevices(const AQH_OBJECT *o);
#endif

View File

@@ -0,0 +1,68 @@
/****************************************************************************
* 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 SERVER_P_H
#define SERVER_P_H
#include "./server.h"
#include "aqhome-nodes/types/device.h"
#include "aqhome/nodes/nodedb.h"
#include "aqhome/ipc2/msgrequest.h"
#include <termios.h>
/* default values */
#define AQHOME_MQTT_DEFAULT_PIDFILE "/var/run/aqhome-mqtt.pid"
#define AQHOME_MQTT_DEFAULT_DATADIR "/var/lib/aqhome-mqtt"
#define AQHOME_MQTT_DEFAULT_DEVICEFILE "mqttlog/registereddevices.xml"
#define AQHOME_MQTT_DEFAULT_BROKER_PORT 1899
#define AQHOME_MQTT_DEFAULT_BROKER_CLIENTID "mqtt"
typedef struct AQH_MQTTLOG_SERVER AQH_MQTTLOG_SERVER;
struct AQH_MQTTLOG_SERVER {
AQH_OBJECT *mqttEndpoint;
AQH_OBJECT *brokerEndpoint;
GWEN_DB_NODE *dbArgs;
AQHMQTT_DEVICE_LIST *availableDeviceList;
AQHMQTT_DEVICE_LIST *registeredDeviceList;
char *deviceFile;
char *pidFile;
int timeout; /* timeout for run e.g. inside valgrind */
char *mqttAddress;
int mqttPort;
char *mqttClientId;
int mqttKeepAlive;
char *brokerAddress;
int brokerPort;
char *brokerClientId;
time_t timestampMqttDown;
time_t timestampBrokerDown;
int timeoutInSeconds;
};
AQH_MQTTLOG_SERVER *AQH_MqttLogServer_GetServerData(const AQH_OBJECT *o);
#endif

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* This file is part of the project AqHome. * This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved. * AqHome (c) by 2025 Martin Preuss, all rights reserved.
* *
* The license for this file can be found in the file COPYING which you * The license for this file can be found in the file COPYING which you
* should have received along with this file. * should have received along with this file.
@@ -11,7 +11,7 @@
#endif #endif
#include "./xmlread.h" #include "./xmlread.h"
#include "./aqhome_mqtt_p.h" #include "./server_p.h"
#include "aqhome-mqttlog/types/topic.h" #include "aqhome-mqttlog/types/topic.h"
#include "aqhome-mqttlog/types/value.h" #include "aqhome-mqttlog/types/value.h"
#include "aqhome-mqttlog/types/translation.h" #include "aqhome-mqttlog/types/translation.h"
@@ -41,16 +41,16 @@
* ------------------------------------------------------------------------------------------------ * ------------------------------------------------------------------------------------------------
*/ */
static AQHMQTT_DEVICE_LIST *_readDeviceFiles(AQHOME_MQTT *aqh, const GWEN_STRINGLIST *sl); static AQHMQTT_DEVICE_LIST *_readDeviceFiles(const GWEN_STRINGLIST *sl);
static int _readDeviceFileToList(AQHOME_MQTT *aqh, const char *sFilename, AQHMQTT_DEVICE_LIST *deviceList); static int _readDeviceFileToList(const char *sFilename, AQHMQTT_DEVICE_LIST *deviceList);
static int _readXmlDevices(AQHOME_MQTT *aqh, GWEN_XMLNODE *deviceListNode, AQHMQTT_DEVICE_LIST *deviceList); static int _readXmlDevices(GWEN_XMLNODE *deviceListNode, AQHMQTT_DEVICE_LIST *deviceList);
static AQHMQTT_DEVICE *_readXmlDevice(AQHOME_MQTT *aqh, GWEN_XMLNODE *deviceNode); static AQHMQTT_DEVICE *_readXmlDevice(GWEN_XMLNODE *deviceNode);
static AQHMQTT_TOPIC_LIST *_readXmlTopicList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode); static AQHMQTT_TOPIC_LIST *_readXmlTopicList(GWEN_XMLNODE *parentNode);
static AQHMQTT_TOPIC *_readXmlTopic(AQHOME_MQTT *aqh, GWEN_XMLNODE *topicNode); static AQHMQTT_TOPIC *_readXmlTopic(GWEN_XMLNODE *topicNode);
static AQHMQTT_VALUE_LIST *_readXmlValueList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode); static AQHMQTT_VALUE_LIST *_readXmlValueList(GWEN_XMLNODE *parentNode);
static AQHMQTT_VALUE *_readXmlValue(AQHOME_MQTT *aqh, GWEN_XMLNODE *valueNode); static AQHMQTT_VALUE *_readXmlValue(GWEN_XMLNODE *valueNode);
static AQHMQTT_TRANSLATION_LIST *_readXmlTranslationList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode); static AQHMQTT_TRANSLATION_LIST *_readXmlTranslationList(GWEN_XMLNODE *parentNode);
static AQHMQTT_TRANSLATION *_readXmlTranslation(AQHOME_MQTT *aqh, GWEN_XMLNODE *translationNode); static AQHMQTT_TRANSLATION *_readXmlTranslation(GWEN_XMLNODE *translationNode);
@@ -60,8 +60,13 @@ static AQHMQTT_TRANSLATION *_readXmlTranslation(AQHOME_MQTT *aqh, GWEN_XMLNODE *
* ------------------------------------------------------------------------------------------------ * ------------------------------------------------------------------------------------------------
*/ */
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDeviceFile(AQHOME_MQTT *aqh, const char *sFilename) AQHMQTT_DEVICE_LIST *AQH_MqttLogServer_ReadDeviceFile(AQH_OBJECT *o, const char *sFilename)
{ {
if (o) {
AQH_MQTTLOG_SERVER *xo;
xo=AQH_MqttLogServer_GetServerData(o);
if (xo) {
int rv; int rv;
rv=GWEN_Directory_GetPath(sFilename, GWEN_PATH_FLAGS_CHECKROOT | GWEN_PATH_FLAGS_PATHMUSTEXIST | GWEN_PATH_FLAGS_VARIABLE); rv=GWEN_Directory_GetPath(sFilename, GWEN_PATH_FLAGS_CHECKROOT | GWEN_PATH_FLAGS_PATHMUSTEXIST | GWEN_PATH_FLAGS_VARIABLE);
@@ -73,7 +78,7 @@ AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDeviceFile(AQHOME_MQTT *aqh, const char *
AQHMQTT_DEVICE_LIST *deviceList; AQHMQTT_DEVICE_LIST *deviceList;
deviceList=AQHMQTT_Device_List_new(); deviceList=AQHMQTT_Device_List_new();
rv=_readDeviceFileToList(aqh, sFilename, deviceList); rv=_readDeviceFileToList(sFilename, deviceList);
if (rv<0) { if (rv<0) {
DBG_ERROR(NULL, "File \"%s\" not found", sFilename); DBG_ERROR(NULL, "File \"%s\" not found", sFilename);
AQHMQTT_Device_List_free(deviceList); AQHMQTT_Device_List_free(deviceList);
@@ -86,19 +91,27 @@ AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDeviceFile(AQHOME_MQTT *aqh, const char *
} }
return deviceList; return deviceList;
} }
}
}
return NULL;
} }
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDataDeviceFiles(AQHOME_MQTT *aqh) AQHMQTT_DEVICE_LIST *AQH_MqttLogServer_ReadDataDeviceFiles(AQH_OBJECT *o)
{ {
if (o) {
AQH_MQTTLOG_SERVER *xo;
xo=AQH_MqttLogServer_GetServerData(o);
if (xo) {
GWEN_STRINGLIST *sl; GWEN_STRINGLIST *sl;
sl=AQH_GetListOfMatchingDataFiles("aqhome/devices/mqtt", "*.xml"); sl=AQH_GetListOfMatchingDataFiles("aqhome/devices/mqtt", "*.xml");
if (sl) { if (sl) {
AQHMQTT_DEVICE_LIST *deviceList; AQHMQTT_DEVICE_LIST *deviceList;
deviceList=_readDeviceFiles(aqh, sl); deviceList=_readDeviceFiles(sl);
GWEN_StringList_free(sl); GWEN_StringList_free(sl);
if (deviceList==NULL) { if (deviceList==NULL) {
DBG_INFO(NULL, "Error reading data device files"); DBG_INFO(NULL, "Error reading data device files");
@@ -108,13 +121,15 @@ AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDataDeviceFiles(AQHOME_MQTT *aqh)
} }
else { else {
DBG_ERROR(NULL, "No data device files"); DBG_ERROR(NULL, "No data device files");
return NULL;
} }
}
}
return NULL;
} }
AQHMQTT_DEVICE_LIST *_readDeviceFiles(AQHOME_MQTT *aqh, const GWEN_STRINGLIST *sl) AQHMQTT_DEVICE_LIST *_readDeviceFiles(const GWEN_STRINGLIST *sl)
{ {
GWEN_STRINGLISTENTRY *se; GWEN_STRINGLISTENTRY *se;
AQHMQTT_DEVICE_LIST *deviceList; AQHMQTT_DEVICE_LIST *deviceList;
@@ -129,7 +144,7 @@ AQHMQTT_DEVICE_LIST *_readDeviceFiles(AQHOME_MQTT *aqh, const GWEN_STRINGLIST *s
int rv; int rv;
DBG_INFO(NULL, "Reading device file \"%s\"", s); DBG_INFO(NULL, "Reading device file \"%s\"", s);
rv=_readDeviceFileToList(aqh, s, deviceList); rv=_readDeviceFileToList(s, deviceList);
if (rv<0 && rv!=GWEN_ERROR_NO_DATA) { if (rv<0 && rv!=GWEN_ERROR_NO_DATA) {
DBG_WARN(NULL, "Error reading device file \"%s\" (%d), ignoring", s, rv); DBG_WARN(NULL, "Error reading device file \"%s\" (%d), ignoring", s, rv);
} }
@@ -147,7 +162,7 @@ AQHMQTT_DEVICE_LIST *_readDeviceFiles(AQHOME_MQTT *aqh, const GWEN_STRINGLIST *s
int _readDeviceFileToList(AQHOME_MQTT *aqh, const char *sFilename, AQHMQTT_DEVICE_LIST *deviceList) int _readDeviceFileToList(const char *sFilename, AQHMQTT_DEVICE_LIST *deviceList)
{ {
GWEN_XMLNODE *rootNode; GWEN_XMLNODE *rootNode;
GWEN_XMLNODE *deviceListNode; GWEN_XMLNODE *deviceListNode;
@@ -165,7 +180,7 @@ int _readDeviceFileToList(AQHOME_MQTT *aqh, const char *sFilename, AQHMQTT_DEVIC
if (deviceListNode==NULL) if (deviceListNode==NULL)
deviceListNode=rootNode; deviceListNode=rootNode;
rv=_readXmlDevices(aqh, deviceListNode, deviceList); rv=_readXmlDevices(deviceListNode, deviceList);
if (rv<0 && rv!=GWEN_ERROR_NO_DATA) { if (rv<0 && rv!=GWEN_ERROR_NO_DATA) {
DBG_ERROR(AQH_LOGDOMAIN, "Error reading devices from file \"%s\" (%d)", sFilename, rv); DBG_ERROR(AQH_LOGDOMAIN, "Error reading devices from file \"%s\" (%d)", sFilename, rv);
GWEN_XMLNode_free(rootNode); GWEN_XMLNode_free(rootNode);
@@ -179,7 +194,7 @@ int _readDeviceFileToList(AQHOME_MQTT *aqh, const char *sFilename, AQHMQTT_DEVIC
int _readXmlDevices(AQHOME_MQTT *aqh, GWEN_XMLNODE *deviceListNode, AQHMQTT_DEVICE_LIST *deviceList) int _readXmlDevices(GWEN_XMLNODE *deviceListNode, AQHMQTT_DEVICE_LIST *deviceList)
{ {
GWEN_XMLNODE *deviceNode; GWEN_XMLNODE *deviceNode;
@@ -192,7 +207,7 @@ int _readXmlDevices(AQHOME_MQTT *aqh, GWEN_XMLNODE *deviceListNode, AQHMQTT_DEVI
if (driverName && *driverName && strcasecmp(driverName, "mqtt")==0) { if (driverName && *driverName && strcasecmp(driverName, "mqtt")==0) {
AQHMQTT_DEVICE *device; AQHMQTT_DEVICE *device;
device=_readXmlDevice(aqh, deviceNode); device=_readXmlDevice(deviceNode);
if (device==NULL) { if (device==NULL) {
DBG_INFO(NULL, "Error reading device from XML"); DBG_INFO(NULL, "Error reading device from XML");
return GWEN_ERROR_BAD_DATA; return GWEN_ERROR_BAD_DATA;
@@ -227,7 +242,7 @@ int _readXmlDevices(AQHOME_MQTT *aqh, GWEN_XMLNODE *deviceListNode, AQHMQTT_DEVI
AQHMQTT_DEVICE *_readXmlDevice(AQHOME_MQTT *aqh, GWEN_XMLNODE *deviceNode) AQHMQTT_DEVICE *_readXmlDevice(GWEN_XMLNODE *deviceNode)
{ {
AQHMQTT_DEVICE *device; AQHMQTT_DEVICE *device;
GWEN_XMLNODE *topicsNode; GWEN_XMLNODE *topicsNode;
@@ -241,7 +256,7 @@ AQHMQTT_DEVICE *_readXmlDevice(AQHOME_MQTT *aqh, GWEN_XMLNODE *deviceNode)
if (topicsNode) { if (topicsNode) {
AQHMQTT_TOPIC_LIST *topicList; AQHMQTT_TOPIC_LIST *topicList;
topicList=_readXmlTopicList(aqh, topicsNode); topicList=_readXmlTopicList(topicsNode);
if (topicList) if (topicList)
AQHMQTT_Device_SetTopicList(device, topicList); AQHMQTT_Device_SetTopicList(device, topicList);
else { else {
@@ -261,7 +276,7 @@ AQHMQTT_DEVICE *_readXmlDevice(AQHOME_MQTT *aqh, GWEN_XMLNODE *deviceNode)
AQHMQTT_TOPIC_LIST *_readXmlTopicList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode) AQHMQTT_TOPIC_LIST *_readXmlTopicList(GWEN_XMLNODE *parentNode)
{ {
AQHMQTT_TOPIC_LIST *topicList; AQHMQTT_TOPIC_LIST *topicList;
GWEN_XMLNODE *topicNode; GWEN_XMLNODE *topicNode;
@@ -269,7 +284,7 @@ AQHMQTT_TOPIC_LIST *_readXmlTopicList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode
topicList=AQHMQTT_Topic_List_new(); topicList=AQHMQTT_Topic_List_new();
topicNode=GWEN_XMLNode_FindFirstTag(parentNode, "mqtttopic", NULL, NULL); topicNode=GWEN_XMLNode_FindFirstTag(parentNode, "mqtttopic", NULL, NULL);
while(topicNode) { while(topicNode) {
AQHMQTT_TOPIC *topic=_readXmlTopic(aqh, topicNode); AQHMQTT_TOPIC *topic=_readXmlTopic(topicNode);
if (topic) if (topic)
AQHMQTT_Topic_List_Add(topic, topicList); AQHMQTT_Topic_List_Add(topic, topicList);
else { else {
@@ -289,7 +304,7 @@ AQHMQTT_TOPIC_LIST *_readXmlTopicList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode
AQHMQTT_TOPIC *_readXmlTopic(AQHOME_MQTT *aqh, GWEN_XMLNODE *topicNode) AQHMQTT_TOPIC *_readXmlTopic(GWEN_XMLNODE *topicNode)
{ {
AQHMQTT_TOPIC *topic; AQHMQTT_TOPIC *topic;
GWEN_XMLNODE *valuesNode; GWEN_XMLNODE *valuesNode;
@@ -325,7 +340,7 @@ AQHMQTT_TOPIC *_readXmlTopic(AQHOME_MQTT *aqh, GWEN_XMLNODE *topicNode)
if (valuesNode) { if (valuesNode) {
AQHMQTT_VALUE_LIST *valueList; AQHMQTT_VALUE_LIST *valueList;
valueList=_readXmlValueList(aqh, valuesNode); valueList=_readXmlValueList(valuesNode);
if (valueList) if (valueList)
AQHMQTT_Topic_SetValueList(topic, valueList); AQHMQTT_Topic_SetValueList(topic, valueList);
else { else {
@@ -345,7 +360,7 @@ AQHMQTT_TOPIC *_readXmlTopic(AQHOME_MQTT *aqh, GWEN_XMLNODE *topicNode)
AQHMQTT_VALUE_LIST *_readXmlValueList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode) AQHMQTT_VALUE_LIST *_readXmlValueList(GWEN_XMLNODE *parentNode)
{ {
AQHMQTT_VALUE_LIST *valueList; AQHMQTT_VALUE_LIST *valueList;
GWEN_XMLNODE *valueNode; GWEN_XMLNODE *valueNode;
@@ -353,7 +368,7 @@ AQHMQTT_VALUE_LIST *_readXmlValueList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode
valueList=AQHMQTT_Value_List_new(); valueList=AQHMQTT_Value_List_new();
valueNode=GWEN_XMLNode_FindFirstTag(parentNode, "value", NULL, NULL); valueNode=GWEN_XMLNode_FindFirstTag(parentNode, "value", NULL, NULL);
while(valueNode) { while(valueNode) {
AQHMQTT_VALUE *value=_readXmlValue(aqh, valueNode); AQHMQTT_VALUE *value=_readXmlValue(valueNode);
if (value) if (value)
AQHMQTT_Value_List_Add(value, valueList); AQHMQTT_Value_List_Add(value, valueList);
else { else {
@@ -373,7 +388,7 @@ AQHMQTT_VALUE_LIST *_readXmlValueList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode
AQHMQTT_VALUE *_readXmlValue(AQHOME_MQTT *aqh, GWEN_XMLNODE *valueNode) AQHMQTT_VALUE *_readXmlValue(GWEN_XMLNODE *valueNode)
{ {
AQHMQTT_VALUE *value; AQHMQTT_VALUE *value;
GWEN_XMLNODE *translationNode; GWEN_XMLNODE *translationNode;
@@ -398,7 +413,7 @@ AQHMQTT_VALUE *_readXmlValue(AQHOME_MQTT *aqh, GWEN_XMLNODE *valueNode)
if (translationNode) { if (translationNode) {
AQHMQTT_TRANSLATION_LIST *translationList; AQHMQTT_TRANSLATION_LIST *translationList;
translationList=_readXmlTranslationList(aqh, translationNode); translationList=_readXmlTranslationList(translationNode);
if (translationList) { if (translationList) {
DBG_INFO(NULL, "Translations read"); DBG_INFO(NULL, "Translations read");
AQHMQTT_Value_SetTranslationList(value, translationList); AQHMQTT_Value_SetTranslationList(value, translationList);
@@ -413,7 +428,7 @@ AQHMQTT_VALUE *_readXmlValue(AQHOME_MQTT *aqh, GWEN_XMLNODE *valueNode)
AQHMQTT_TRANSLATION_LIST *_readXmlTranslationList(AQHOME_MQTT *aqh, GWEN_XMLNODE *parentNode) AQHMQTT_TRANSLATION_LIST *_readXmlTranslationList(GWEN_XMLNODE *parentNode)
{ {
AQHMQTT_TRANSLATION_LIST *translationList; AQHMQTT_TRANSLATION_LIST *translationList;
GWEN_XMLNODE *translationNode; GWEN_XMLNODE *translationNode;
@@ -421,7 +436,7 @@ AQHMQTT_TRANSLATION_LIST *_readXmlTranslationList(AQHOME_MQTT *aqh, GWEN_XMLNODE
translationList=AQHMQTT_Translation_List_new(); translationList=AQHMQTT_Translation_List_new();
translationNode=GWEN_XMLNode_FindFirstTag(parentNode, "translation", NULL, NULL); translationNode=GWEN_XMLNode_FindFirstTag(parentNode, "translation", NULL, NULL);
while(translationNode) { while(translationNode) {
AQHMQTT_TRANSLATION *translation=_readXmlTranslation(aqh, translationNode); AQHMQTT_TRANSLATION *translation=_readXmlTranslation(translationNode);
if (translation) if (translation)
AQHMQTT_Translation_List_Add(translation, translationList); AQHMQTT_Translation_List_Add(translation, translationList);
else { else {
@@ -441,7 +456,7 @@ AQHMQTT_TRANSLATION_LIST *_readXmlTranslationList(AQHOME_MQTT *aqh, GWEN_XMLNODE
AQHMQTT_TRANSLATION *_readXmlTranslation(AQHOME_MQTT *aqh, GWEN_XMLNODE *translationNode) AQHMQTT_TRANSLATION *_readXmlTranslation(GWEN_XMLNODE *translationNode)
{ {
const char *sAqhValue; const char *sAqhValue;
const char *sDriverValue; const char *sDriverValue;

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* This file is part of the project AqHome. * This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved. * AqHome (c) by 2025 Martin Preuss, all rights reserved.
* *
* The license for this file can be found in the file COPYING which you * The license for this file can be found in the file COPYING which you
* should have received along with this file. * should have received along with this file.
@@ -9,14 +9,14 @@
#ifndef AQHOME_MQTTLOG_XMLREAD_H #ifndef AQHOME_MQTTLOG_XMLREAD_H
#define AQHOME_MQTTLOG_XMLREAD_H #define AQHOME_MQTTLOG_XMLREAD_H
#include "aqhome-mqttlog/server.h"
#include "aqhome-mqttlog/aqhome_mqtt.h" #include "aqhome-mqttlog/aqhome_mqtt.h"
#include "aqhome-mqttlog/types/device.h" #include "aqhome-mqttlog/types/device.h"
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDataDeviceFiles(AQHOME_MQTT *aqh); AQHMQTT_DEVICE_LIST *AQH_MqttLogServer_ReadDataDeviceFiles(AQH_OBJECT *o);
AQHMQTT_DEVICE_LIST *AqHomeMqttLog_ReadDeviceFile(AQHOME_MQTT *aqh, const char *sFilename); AQHMQTT_DEVICE_LIST *AQH_MqttLogServer_ReadDeviceFile(AQH_OBJECT *o, const char *sFilename);

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* This file is part of the project AqHome. * This file is part of the project AqHome.
* AqHome (c) by 2024 Martin Preuss, all rights reserved. * AqHome (c) by 2025 Martin Preuss, all rights reserved.
* *
* The license for this file can be found in the file COPYING which you * The license for this file can be found in the file COPYING which you
* should have received along with this file. * should have received along with this file.
@@ -54,7 +54,7 @@ static void _setXmlCharValueIfNotNull(GWEN_XMLNODE *n, const char *name, const c
int AqHomeMqttLog_WriteDevicesFile(AQHOME_MQTT *aqh, const AQHMQTT_DEVICE_LIST *deviceList, const char *sFilename) int AQH_MqttLogServer_WriteDevicesFile(const AQHMQTT_DEVICE_LIST *deviceList, const char *sFilename)
{ {
int rv; int rv;

View File

@@ -1,6 +1,6 @@
/**************************************************************************** /****************************************************************************
* This file is part of the project AqHome. * This file is part of the project AqHome.
* AqHome (c) by 2024 Martin Preuss, all rights reserved. * AqHome (c) by 2025 Martin Preuss, all rights reserved.
* *
* The license for this file can be found in the file COPYING which you * The license for this file can be found in the file COPYING which you
* should have received along with this file. * should have received along with this file.
@@ -9,13 +9,13 @@
#ifndef AQHOME_MQTTLOG_XMLWRITE_H #ifndef AQHOME_MQTTLOG_XMLWRITE_H
#define AQHOME_MQTTLOG_XMLWRITE_H #define AQHOME_MQTTLOG_XMLWRITE_H
#include "aqhome-mqttlog/server.h"
#include "aqhome-mqttlog/aqhome_mqtt.h" #include "aqhome-mqttlog/aqhome_mqtt.h"
#include "aqhome-mqttlog/types/device.h" #include "aqhome-mqttlog/types/device.h"
int AqHomeMqttLog_WriteDevicesFile(AQHOME_MQTT *aqh, const AQHMQTT_DEVICE_LIST *deviceList, const char *sFilename); int AQH_MqttLogServer_WriteDevicesFile(const AQHMQTT_DEVICE_LIST *deviceList, const char *sFilename);

View File

@@ -107,6 +107,8 @@ int main(int argc, char **argv)
aqh=AQH_NodeServer_new(eventLoop); aqh=AQH_NodeServer_new(eventLoop);
rv=AQH_NodeServer_Init(aqh, argc, argv); rv=AQH_NodeServer_Init(aqh, argc, argv);
if (rv<0) { if (rv<0) {
if (rv==GWEN_ERROR_CLOSE)
return 1;
DBG_INFO(NULL, "here (%d)", rv); DBG_INFO(NULL, "here (%d)", rv);
return 2; return 2;
} }

View File

@@ -592,13 +592,13 @@ int _exchangeConnect(AQH_OBJECT *o, AQH_NODE_SERVER *xo, uint32_t flags)
AQH_MESSAGE *msgOut; AQH_MESSAGE *msgOut;
uint32_t msgId; uint32_t msgId;
msgId=AQH_Endpoint_GetNextMessageId(xo->ipcEndpoint); msgId=AQH_Endpoint_GetNextMessageId(xo->brokerEndpoint);
msgOut=AQH_IpcMessageConnect_new(xo->protoId, xo->protoVer, msgOut=AQH_IpcMessageConnect_new(xo->protoId, xo->protoVer,
AQH_MSGTYPE_IPC_CONNECT_REQ, AQH_MSGTYPE_IPC_CONNECT_REQ,
msgId, 0, msgId, 0,
xo->brokerClientId, NULL, NULL, flags); xo->brokerClientId, NULL, NULL, flags);
AQH_Endpoint_AddMsgOut(xo->ipcEndpoint, msgOut); AQH_Endpoint_AddMsgOut(xo->brokerEndpoint, msgOut);
return AQH_IpcEndpoint_WaitForResultMsg(xo->ipcEndpoint, return AQH_IpcEndpoint_WaitForResultMsg(xo->brokerEndpoint,
xo->protoId, xo->protoVer, AQH_MSGTYPE_IPC_RESULT, xo->protoId, xo->protoVer, AQH_MSGTYPE_IPC_RESULT,
msgId, xo->timeoutInSeconds); msgId, xo->timeoutInSeconds);
} }
@@ -1206,7 +1206,7 @@ int _handleTtyDown(AQH_NODE_SERVER *xo)
{ {
if (xo->ttyEndpoint) { if (xo->ttyEndpoint) {
DBG_ERROR(NULL, "TTY closed"); DBG_ERROR(NULL, "TTY closed");
AQH_Object_AddFlags(xo->brokerEndpoint, AQH_OBJECT_FLAGS_DELETE); AQH_Object_AddFlags(xo->ttyEndpoint, AQH_OBJECT_FLAGS_DELETE);
xo->timestampTtyDown=time(NULL); xo->timestampTtyDown=time(NULL);
} }
return 1; return 1;

View File

@@ -4,5 +4,5 @@ export AQHOME_LOGLEVEL=info
export LD_LIBRARY_PATH="0-build/aqhome/:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH="0-build/aqhome/:$LD_LIBRARY_PATH"
# 0-build/apps/aqhome-mqttlog/aqhome-mqttlog -ma 192.168.117.192 -mp 1883 -W /tmp/aqhome/mqttlog -i apps/aqhome-mqttlog/mqttlog.conf --mqttclientid=AQHOMEMQTTLOGTEST $* # 0-build/apps/aqhome-mqttlog/aqhome-mqttlog -ma 192.168.117.192 -mp 1883 -W /tmp/aqhome/mqttlog -i apps/aqhome-mqttlog/mqttlog.conf --mqttclientid=AQHOMEMQTTLOGTEST $*
0-build/apps/aqhome-mqttlog/aqhome-mqttlog --mqttclientid=AQHOMEMQTTLOGTEST -p ./aqhome-mqtt.pid -d ./aqhome-mqtt.devices "$@" 0-build/apps/aqhome-mqttlog/aqhome-mqttlog --mqttclientid=MQTTLOGTEST2 -p ./aqhome-mqtt.pid -d ./aqhome-mqtt.devices "$@"

View File

@@ -49,6 +49,7 @@
msgwriter.h msgwriter.h
ipcmsgreader.h ipcmsgreader.h
nodemsgreader.h nodemsgreader.h
mqttmsgreader.h
ttyobject.h ttyobject.h
tcpd_object.h tcpd_object.h
tcp_object.h tcp_object.h
@@ -58,6 +59,8 @@
ipc_server.h ipc_server.h
ipc_client.h ipc_client.h
tty_endpoint.h tty_endpoint.h
mqtt_endpoint.h
mqtt_client.h
</headers> </headers>
@@ -79,6 +82,7 @@
msgwriter.c msgwriter.c
ipcmsgreader.c ipcmsgreader.c
nodemsgreader.c nodemsgreader.c
mqttmsgreader.c
ttyobject.c ttyobject.c
tcpd_object.c tcpd_object.c
tcp_object.c tcp_object.c
@@ -89,6 +93,8 @@
ipc_client.c ipc_client.c
ipc_endpoint.c ipc_endpoint.c
tty_endpoint.c tty_endpoint.c
mqtt_endpoint.c
mqtt_client.c
</sources> </sources>

View File

@@ -395,6 +395,11 @@ void AQH_Endpoint_AddMsgOut(AQH_OBJECT *o, AQH_MESSAGE *msg)
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_ENDPOINT, o); xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_ENDPOINT, o);
if (xo) { if (xo) {
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); AQH_Message_List_Add(msg, xo->msgOutList);
if (xo->msgWriter && AQH_Message_List_GetCount(xo->msgOutList)==1) { if (xo->msgWriter && AQH_Message_List_GetCount(xo->msgOutList)==1) {
DBG_INFO(AQH_LOGDOMAIN, "Enabling msgWriter, sending message"); DBG_INFO(AQH_LOGDOMAIN, "Enabling msgWriter, sending message");
@@ -403,6 +408,7 @@ void AQH_Endpoint_AddMsgOut(AQH_OBJECT *o, AQH_MESSAGE *msg)
} }
} }
} }
}
} }

53
aqhome/ipc2/mqtt_client.c Normal file
View 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
View 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

View 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;
}

View 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
View 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;
}

View 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

View File

@@ -23,7 +23,7 @@
#define AQH_MSGREADER_SKIPTIME_IN_MS 20 #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; AQH_MSG_READER *xo;
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o); 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; xo->flags=f;
}
} }
@@ -120,8 +122,10 @@ void AQH_MsgReader_AddFlags(AQH_OBJECT *o, uint32_t f)
AQH_MSG_READER *xo; AQH_MSG_READER *xo;
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o); 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; xo->flags|=f;
}
} }
@@ -131,8 +135,10 @@ void AQH_MsgReader_SubFlags(AQH_OBJECT *o, uint32_t f)
AQH_MSG_READER *xo; AQH_MSG_READER *xo;
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o); 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; xo->flags&=~f;
}
} }
@@ -339,6 +345,12 @@ int AQH_MsgReader_ReadRemainderFromRingbuffer(AQH_OBJECT *o)
xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o); xo=GWEN_INHERIT_GETDATA(AQH_OBJECT, AQH_MSG_READER, o);
if (xo) { if (xo) {
if (xo->bytesLeft==0) {
/* msg finished */
DBG_INFO(AQH_LOGDOMAIN, "Message complete");
return 1;
}
else {
uint32_t bytesInRingBuffer; uint32_t bytesInRingBuffer;
uint32_t bytesToRead; uint32_t bytesToRead;
int rv; int rv;
@@ -370,6 +382,10 @@ int AQH_MsgReader_ReadRemainderFromRingbuffer(AQH_OBJECT *o)
return 1; return 1;
} }
} }
else {
DBG_ERROR(AQH_LOGDOMAIN, "Nothing to read??");
}
}
return 0; return 0;
} }
else { else {

View File

@@ -110,11 +110,13 @@
<useTargets> <useTargets>
aqhmsg_node aqhmsg_node
aqhmsg_ipc aqhmsg_ipc
aqhmsg_mqtt
</useTargets> </useTargets>
<subdirs> <subdirs>
node node
ipc ipc
mqtt
</subdirs> </subdirs>

88
aqhome/msg/mqtt/0BUILD Normal file
View 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
View 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
View 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

View 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;
}

View 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

View 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;
}

View 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

View 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;
}

View 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

View 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;
}

View 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

View 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;
}

View 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

View File

@@ -1,3 +1,3 @@
mosquitto_sub -h 192.168.117.192 -t '#' -v mosquitto_sub -h 192.168.117.194 -t '#' -v