aqhome: completed adapting to msgio2 interface.

This commit is contained in:
Martin Preuss
2023-07-12 13:33:04 +02:00
parent 39987b31c7
commit 08c3875a26
66 changed files with 1765 additions and 3914 deletions

View File

@@ -37,12 +37,15 @@
</setVar>
<headers dist="true" >
mqtt.h
messages.h
</headers>
<sources>
$(local/typefiles)
main.c
mqtt.c
messages.c
</sources>
<useTargets>

View File

@@ -11,44 +11,25 @@
#endif
#include "./item.h"
#include "./mqtt.h"
#include "./messages.h"
#include <aqhome/api.h>
#include <aqhome/aqhome.h>
#include <aqhome/msgmanager.h>
#include <aqhome/msg/endpoint_log.h>
#include <aqhome/mqtt/endpoint_mqttc.h>
#include "aqhome/mqtt/endpoint_mqttc.h"
#include <aqhome/mqtt/msg_mqtt_connect.h>
#include <aqhome/mqtt/msg_mqtt_connack.h>
#include <aqhome/mqtt/msg_mqtt_publish.h>
#include <aqhome/mqtt/msg_mqtt_subscribe.h>
#include <aqhome/mqtt/msg_mqtt_suback.h>
#include "aqhome/aqhome.h"
#include "aqhome/mqtt/msg_mqtt.h"
#include "aqhome/mqtt/msg_mqtt_publish.h"
#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/endpoint_tcpc.h>
#include <gwenhywfar/endpoint_connectable.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/logger.h>
#include <gwenhywfar/db.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/cgui.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/json_read.h>
#include <gwenhywfar/directory.h>
#ifdef HAVE_SIGNAL_H
# include <signal.h>
#endif
#ifdef HAVE_SYS_TYPES_H
# include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
# include <sys/stat.h>
#endif
#include <unistd.h>
#include <stdio.h>
#include <fcntl.h>
@@ -57,31 +38,27 @@
#include <time.h>
/* ------------------------------------------------------------------------------------------------
* defines
* ------------------------------------------------------------------------------------------------
*/
#define I18N(msg) msg
#define I18S(msg) msg
//#define FULL_DEBUG
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static int _serve(GWEN_DB_NODE *dbArgs);
static GWEN_MSG_ENDPOINT *_createMqttEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs);
static int _readArgs(int argc, char **argv, GWEN_DB_NODE *dbArgs);
static int _createPidFile(const char *pidFilename);
static ITEM_LIST *_readItems(GWEN_DB_NODE *dbArgs);
static const ITEM *_getItemForTopic(const ITEM_LIST *itemList, const char *topic);
int _subscribe(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, const char *topicFilter);
int _mqttConnect(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp);
GWEN_MSG *_awaitPacket(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, uint8_t expectedPacketType);
void _handlePublishMsg(const char *baseFolder, const ITEM_LIST *itemList, const GWEN_MSG *msg);
void _handlePublish(const char *baseFolder, const ITEM_LIST *itemList, const char *topic, const char *value);
void _handleRawMsgForItem(const char *baseFolder, const ITEM *item, const char *value);
void _handleJsonMsgForItem(const char *baseFolder, const ITEM *item, const char *value);
void _handleJsonItemVar(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, GWEN_JSON_ELEM *jeRoot);
void _writeValueAccordingToItem(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, const char *value);
void _writeToFile(const char *filename, const char *txt);
#ifdef HAVE_SIGNAL_H
static int _setSignalHandlers(void);
@@ -90,10 +67,21 @@ static void _signalHandler(int s);
static struct sigaction saINT,saTERM, saHUP, saTSTP, saCONT;
#endif
/* ------------------------------------------------------------------------------------------------
* static vars
* ------------------------------------------------------------------------------------------------
*/
static int stopService=0;
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
int main(int argc, char **argv)
{
@@ -153,8 +141,7 @@ int main(int argc, char **argv)
int _serve(GWEN_DB_NODE *dbArgs)
{
const char *pidFile;
GWEN_MSG_ENDPOINT_MGR *emgr;
GWEN_MSG_ENDPOINT *epTcp;
GWEN_MSG_ENDPOINT2 *epTcp;
ITEM_LIST *itemList;
int rv;
int timeout;
@@ -163,7 +150,7 @@ int _serve(GWEN_DB_NODE *dbArgs)
startTime=time(NULL);
itemList=_readItems(dbArgs);
itemList=AqHomeMqttLog_ReadItems(dbArgs);
if (itemList==NULL) {
DBG_ERROR(NULL, "No items to listen to, aborting.");
return GWEN_ERROR_GENERIC;
@@ -186,39 +173,39 @@ int _serve(GWEN_DB_NODE *dbArgs)
}
}
emgr=GWEN_MsgEndpointMgr_new();
epTcp=_createMqttEndpoint(emgr, dbArgs);
epTcp=AqHomeMqttLog_CreateMqttEndpoint(dbArgs);
if (epTcp==NULL) {
DBG_INFO(NULL, "here");
GWEN_MsgEndpointMgr_free(emgr);
Item_List_free(itemList);
return GWEN_ERROR_GENERIC;
}
rv=_mqttConnect(emgr, epTcp);
rv=AqHomeMqttLog_MqttConnect(epTcp);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
Item_List_free(itemList);
return rv;
}
rv=_subscribe(emgr, epTcp, "#");
rv=AqHomeMqttLog_Subscribe(epTcp, "#");
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
Item_List_free(itemList);
return rv;
}
while(!stopService) {
DBG_DEBUG(NULL, "Next loop");
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
if (GWEN_ConnectableMsgEndpoint_GetState(epTcp)<GWEN_ENDPOINT_MQTTC_STATE_ESTABLISHED) {
GWEN_MsgEndpoint2_IoLoop(epTcp, 2000); /* 2000 ms */
if (GWEN_MsgEndpoint2_GetState(epTcp)!=GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
DBG_INFO(NULL, "Not connected...");
}
else {
GWEN_MSG *msg;
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(epTcp);
if (msg) {
#ifdef FULL_DEBUG
DBG_ERROR(NULL, "Received this message:");
@@ -234,7 +221,7 @@ int _serve(GWEN_DB_NODE *dbArgs)
fprintf(stdout, "%s\n", GWEN_Buffer_GetStart(buf));
GWEN_Buffer_free(buf);
#endif
_handlePublishMsg(baseFolder, itemList, msg);
AqHomeMqttLog_HandlePublishMsg(baseFolder, itemList, msg);
}
else {
#ifdef FULL_DEBUG
@@ -259,278 +246,13 @@ int _serve(GWEN_DB_NODE *dbArgs)
if (pidFile && *pidFile)
remove(pidFile);
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
Item_List_free(itemList);
return 0;
}
void _handlePublishMsg(const char *baseFolder, const ITEM_LIST *itemList, const GWEN_MSG *msg)
{
char *topic;
char *value;
topic=AQH_PublishMqttMsg_ExtractTopic(msg);
value=AQH_PublishMqttMsg_ExtractValue(msg);
if (topic && value)
_handlePublish(baseFolder, itemList, topic, value);
else {
DBG_ERROR(NULL, "Either topic or value missing in PUBLISH msg");
}
free(value);
free(topic);
}
void _handlePublish(const char *baseFolder, const ITEM_LIST *itemList, const char *topic, const char *value)
{
const ITEM *item;
item=_getItemForTopic(itemList, topic);
if (item) {
const char *t;
DBG_INFO(NULL, "HANDLING topic \"%s\"", topic);
t=Item_GetDataType(item);
if (t && strcasecmp(t, "json")==0)
_handleJsonMsgForItem(baseFolder, item, value);
else
_handleRawMsgForItem(baseFolder, item, value);
}
else {
DBG_INFO(NULL, "ignoring topic \"%s\"", topic);
}
}
void _handleJsonMsgForItem(const char *baseFolder, const ITEM *item, const char *value)
{
GWEN_JSON_ELEM *jeRoot;
jeRoot=GWEN_JsonElement_fromString(value);
if (jeRoot==NULL) {
DBG_INFO(NULL, "Could not parse JSON value: %s", value);
}
else {
const ITEMVAR_LIST *itemVarList;
itemVarList=Item_GetVarList(item);
if (itemVarList) {
ITEMVAR *itemVar;
itemVar=ItemVar_List_First(itemVarList);
while(itemVar) {
_handleJsonItemVar(baseFolder, item, itemVar, jeRoot);
itemVar=ItemVar_List_Next(itemVar);
}
}
GWEN_JsonElement_free(jeRoot);
}
}
void _handleJsonItemVar(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, GWEN_JSON_ELEM *jeRoot)
{
const char *path;
path=ItemVar_GetPath(itemVar);
if (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)
_writeValueAccordingToItem(baseFolder, item, itemVar, s);
else {
DBG_ERROR(NULL, "Path \"%s\" in JSON data contains no data", path);
}
}
else {
DBG_ERROR(NULL, "Path \"%s\" not found in JSON data", path);
}
}
}
void _handleRawMsgForItem(const char *baseFolder, const ITEM *item, const char *value)
{
const ITEMVAR_LIST *itemVarList;
itemVarList=Item_GetVarList(item);
if (itemVarList) {
ITEMVAR *itemVar;
itemVar=ItemVar_List_First(itemVarList);
while(itemVar) {
_writeValueAccordingToItem(baseFolder, item, itemVar, value);
itemVar=ItemVar_List_Next(itemVar);
}
}
}
void _writeValueAccordingToItem(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, const char *value)
{
const char *id;
const char *name;
id=Item_GetId(item);
name=ItemVar_GetName(itemVar);
if (id && *id && name && *name) {
GWEN_BUFFER *fnameBuf;
fnameBuf=GWEN_Buffer_new(0, 256, 0, 1);
GWEN_Buffer_AppendString(fnameBuf, baseFolder);
GWEN_Buffer_AppendString(fnameBuf, GWEN_DIR_SEPARATOR_S);
GWEN_Buffer_AppendArgs(fnameBuf, "%s_%s", id, name);
_writeToFile(GWEN_Buffer_GetStart(fnameBuf), value);
GWEN_Buffer_free(fnameBuf);
}
else {
DBG_ERROR(NULL, "Either id or name missing in item list file");
}
}
void _writeToFile(const char *filename, const char *txt)
{
if (txt && *txt) {
GWEN_BUFFER *tmpNameBuf;
int rv;
tmpNameBuf=GWEN_Buffer_new(0, 256, 0, 1);
GWEN_Buffer_AppendString(tmpNameBuf, filename);
GWEN_Buffer_AppendString(tmpNameBuf, ".tmp");
rv=GWEN_Directory_GetPath(GWEN_Buffer_GetStart(tmpNameBuf), GWEN_PATH_FLAGS_VARIABLE);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "Error getting path for %s (%d)", GWEN_Buffer_GetStart(tmpNameBuf), rv);
}
else {
FILE *f;
f=fopen(GWEN_Buffer_GetStart(tmpNameBuf), "w");
if (f) {
if (1!=fwrite(txt, strlen(txt), 1, f)) {
DBG_ERROR(AQH_LOGDOMAIN, "Error writing.");
fclose(f);
}
else {
fclose(f);
rename(GWEN_Buffer_GetStart(tmpNameBuf), filename);
}
}
}
GWEN_Buffer_free(tmpNameBuf);
}
}
const ITEM *_getItemForTopic(const ITEM_LIST *itemList, const char *topic)
{
const ITEM *item;
item=Item_List_First(itemList);
while(item) {
const char *s;
s=Item_GetTopic(item);
if (s && GWEN_Text_ComparePattern(topic, s, 0)!=-1)
return item;
item=Item_List_Next(item);
}
return NULL;
}
int _subscribe(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, const char *topicFilter)
{
uint16_t pckId;
GWEN_MSG *msgOut;
GWEN_MSG *msgIn;
DBG_INFO(NULL, "Sending SUBSCRIBE %s", topicFilter);
pckId=AQH_MqttClientEndpoint_GetNextPacketId(epTcp);
msgOut=GWEN_SubscribeMqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE, pckId, topicFilter, 0);
if (msgOut==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_INTERNAL;
}
DBG_ERROR(NULL, "Sending this message:");
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msgOut), GWEN_Msg_GetBytesInBuffer(msgOut), 2);
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
DBG_INFO(NULL, "Waiting for response");
msgIn=_awaitPacket(emgr, epTcp, AQH_MQTTMSG_MSGTYPE_SUBACK);
if (msgIn) {
GWEN_BUFFER *buf;
buf=GWEN_Buffer_new(0, 256, 0, 1);
AQH_SubAckMqttMsg_DumpToBuffer(msgIn, buf, "received");
DBG_INFO(NULL, "%s", GWEN_Buffer_GetStart(buf));
GWEN_Buffer_free(buf);
GWEN_Msg_free(msgIn);
}
return 0;
}
GWEN_MSG_ENDPOINT *_createMqttEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs)
{
const char *mqttAddress;
int mqttPort;
const char *mqttClientId;
const char *mqttTopicPrefix;
int mqttKeepAlive;
mqttAddress=GWEN_DB_GetCharValue(dbArgs, "mqttAddress", 0, NULL);
mqttPort=GWEN_DB_GetIntValue(dbArgs, "mqttPort", 0, 1883);
mqttClientId=GWEN_DB_GetCharValue(dbArgs, "mqttClientId", 0, "aqhome-mqttlog");
mqttTopicPrefix=GWEN_DB_GetCharValue(dbArgs, "mqttTopicPrefix", 0, "aqhome/sensors");
mqttKeepAlive=GWEN_DB_GetIntValue(dbArgs, "mqttKeepAlive", 0, 600);
if (mqttAddress && *mqttAddress && mqttPort) {
GWEN_MSG_ENDPOINT *epMqtt;
DBG_INFO(AQH_LOGDOMAIN, "Connecting to %s (port %d)", mqttAddress, mqttPort);
epMqtt=AQH_MqttClientEndpoint_new(mqttAddress, mqttPort, NULL, AQH_MSGMGR_ENDPOINTGROUP_MQTT);
if (epMqtt==NULL) {
DBG_ERROR(AQH_LOGDOMAIN, "Error creating endpoint TCP");
return NULL;
}
GWEN_MsgEndpoint_SetAcceptedGroupIds(epMqtt, AQH_MSGMGR_ENDPOINTGROUP_NODE | AQH_MSGMGR_ENDPOINTGROUP_MQTT);
if (mqttClientId && *mqttClientId)
AQH_MqttClientEndpoint_SetClientId(epMqtt, mqttClientId);
if (mqttTopicPrefix && *mqttTopicPrefix)
AQH_MqttClientEndpoint_SetTopicPrefix(epMqtt, mqttTopicPrefix);
AQH_MqttClientEndpoint_SetKeepAliveTime(epMqtt, mqttKeepAlive);
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epMqtt);
return epMqtt;
}
return NULL;
}
int _setSignalHandlers(void)
{
#ifdef HAVE_SIGNAL_H
@@ -633,90 +355,6 @@ int _createPidFile(const char *pidFilename)
int _mqttConnect(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp)
{
while(GWEN_ConnectableMsgEndpoint_GetState(epTcp)<GWEN_ENDPOINT_MQTTC_STATE_ESTABLISHED) {
DBG_DEBUG(AQH_LOGDOMAIN, "Next loop");
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
}
return 0;
}
GWEN_MSG *_awaitPacket(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, uint8_t expectedPacketType)
{
while(!stopService) {
GWEN_MSG *msg;
DBG_DEBUG(AQH_LOGDOMAIN, "Next loop");
//GWEN_MsgManager_LoopOnce(emgr);
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
if (msg) {
if ((AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0)==(expectedPacketType & 0xf0)) {
return msg;
}
else {
DBG_ERROR(NULL, "Received this message:");
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), 2);
}
}
} /* for */
return NULL;
}
ITEM_LIST *_readItems(GWEN_DB_NODE *dbArgs)
{
const char *itemFile;
itemFile=GWEN_DB_GetCharValue(dbArgs, "itemfile", 0, NULL);
if (itemFile && *itemFile) {
ITEM_LIST *itemList;
GWEN_DB_NODE *dbItemList;
GWEN_DB_NODE *dbItem;
int rv;
dbItemList=GWEN_DB_Group_new("items");
rv=GWEN_DB_ReadFile(dbItemList, itemFile,
GWEN_DB_FLAGS_DEFAULT |
GWEN_PATH_FLAGS_CREATE_GROUP |
GWEN_DB_FLAGS_ALLOW_EMPTY_STREAM);
if (rv<0) {
DBG_ERROR(NULL, "Error reading item file \"%s\" (%d)", itemFile, rv);
GWEN_DB_Group_free(dbItemList);
return NULL;
}
itemList=Item_List_new();
dbItem=GWEN_DB_FindFirstGroup(dbItemList, "item");
while(dbItem) {
ITEM *item;
item=Item_fromDb(dbItem);
Item_List_Add(item, itemList);
dbItem=GWEN_DB_FindNextGroup(dbItem, "item");
}
GWEN_DB_Group_free(dbItemList);
if (Item_List_GetCount(itemList)==0) {
DBG_INFO(NULL, "No items in file");
Item_List_free(itemList);
return NULL;
}
return itemList;
}
return NULL;
}
int _readArgs(int argc, char **argv, GWEN_DB_NODE *dbArgs)
{
int rv;

View File

@@ -0,0 +1,274 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "./messages.h"
#include "aqhome/mqtt/msg_mqtt_publish.h"
#include <gwenhywfar/json_read.h>
#include <gwenhywfar/db.h>
#include <gwenhywfar/directory.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/debug.h>
static const ITEM *_getItemForTopic(const ITEM_LIST *itemList, const char *topic);
void _handlePublishMsg(const char *baseFolder, const ITEM_LIST *itemList, const GWEN_MSG *msg);
void _handlePublish(const char *baseFolder, const ITEM_LIST *itemList, const char *topic, const char *value);
void _handleRawMsgForItem(const char *baseFolder, const ITEM *item, const char *value);
void _handleJsonMsgForItem(const char *baseFolder, const ITEM *item, const char *value);
void _handleJsonItemVar(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, GWEN_JSON_ELEM *jeRoot);
void _writeValueAccordingToItem(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, const char *value);
void _writeToFile(const char *filename, const char *txt);
ITEM_LIST *AqHomeMqttLog_ReadItems(GWEN_DB_NODE *dbArgs)
{
const char *itemFile;
itemFile=GWEN_DB_GetCharValue(dbArgs, "itemfile", 0, NULL);
if (itemFile && *itemFile) {
ITEM_LIST *itemList;
GWEN_DB_NODE *dbItemList;
GWEN_DB_NODE *dbItem;
int rv;
dbItemList=GWEN_DB_Group_new("items");
rv=GWEN_DB_ReadFile(dbItemList, itemFile,
GWEN_DB_FLAGS_DEFAULT |
GWEN_PATH_FLAGS_CREATE_GROUP |
GWEN_DB_FLAGS_ALLOW_EMPTY_STREAM);
if (rv<0) {
DBG_ERROR(NULL, "Error reading item file \"%s\" (%d)", itemFile, rv);
GWEN_DB_Group_free(dbItemList);
return NULL;
}
itemList=Item_List_new();
dbItem=GWEN_DB_FindFirstGroup(dbItemList, "item");
while(dbItem) {
ITEM *item;
item=Item_fromDb(dbItem);
Item_List_Add(item, itemList);
dbItem=GWEN_DB_FindNextGroup(dbItem, "item");
}
GWEN_DB_Group_free(dbItemList);
if (Item_List_GetCount(itemList)==0) {
DBG_INFO(NULL, "No items in file");
Item_List_free(itemList);
return NULL;
}
return itemList;
}
return NULL;
}
void AqHomeMqttLog_HandlePublishMsg(const char *baseFolder, const ITEM_LIST *itemList, const GWEN_MSG *msg)
{
char *topic;
char *value;
topic=AQH_PublishMqttMsg_ExtractTopic(msg);
value=AQH_PublishMqttMsg_ExtractValue(msg);
if (topic && value)
_handlePublish(baseFolder, itemList, topic, value);
else {
DBG_ERROR(NULL, "Either topic or value missing in PUBLISH msg");
}
free(value);
free(topic);
}
void _handlePublish(const char *baseFolder, const ITEM_LIST *itemList, const char *topic, const char *value)
{
const ITEM *item;
item=_getItemForTopic(itemList, topic);
if (item) {
const char *t;
DBG_INFO(NULL, "HANDLING topic \"%s\"", topic);
t=Item_GetDataType(item);
if (t && strcasecmp(t, "json")==0)
_handleJsonMsgForItem(baseFolder, item, value);
else
_handleRawMsgForItem(baseFolder, item, value);
}
else {
DBG_INFO(NULL, "ignoring topic \"%s\"", topic);
}
}
void _handleJsonMsgForItem(const char *baseFolder, const ITEM *item, const char *value)
{
GWEN_JSON_ELEM *jeRoot;
jeRoot=GWEN_JsonElement_fromString(value);
if (jeRoot==NULL) {
DBG_INFO(NULL, "Could not parse JSON value: %s", value);
}
else {
const ITEMVAR_LIST *itemVarList;
itemVarList=Item_GetVarList(item);
if (itemVarList) {
ITEMVAR *itemVar;
itemVar=ItemVar_List_First(itemVarList);
while(itemVar) {
_handleJsonItemVar(baseFolder, item, itemVar, jeRoot);
itemVar=ItemVar_List_Next(itemVar);
}
}
GWEN_JsonElement_free(jeRoot);
}
}
void _handleJsonItemVar(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, GWEN_JSON_ELEM *jeRoot)
{
const char *path;
path=ItemVar_GetPath(itemVar);
if (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)
_writeValueAccordingToItem(baseFolder, item, itemVar, s);
else {
DBG_ERROR(NULL, "Path \"%s\" in JSON data contains no data", path);
}
}
else {
DBG_ERROR(NULL, "Path \"%s\" not found in JSON data", path);
}
}
}
void _handleRawMsgForItem(const char *baseFolder, const ITEM *item, const char *value)
{
const ITEMVAR_LIST *itemVarList;
itemVarList=Item_GetVarList(item);
if (itemVarList) {
ITEMVAR *itemVar;
itemVar=ItemVar_List_First(itemVarList);
while(itemVar) {
_writeValueAccordingToItem(baseFolder, item, itemVar, value);
itemVar=ItemVar_List_Next(itemVar);
}
}
}
void _writeValueAccordingToItem(const char *baseFolder, const ITEM *item, const ITEMVAR *itemVar, const char *value)
{
const char *id;
const char *name;
id=Item_GetId(item);
name=ItemVar_GetName(itemVar);
if (id && *id && name && *name) {
GWEN_BUFFER *fnameBuf;
fnameBuf=GWEN_Buffer_new(0, 256, 0, 1);
GWEN_Buffer_AppendString(fnameBuf, baseFolder);
GWEN_Buffer_AppendString(fnameBuf, GWEN_DIR_SEPARATOR_S);
GWEN_Buffer_AppendArgs(fnameBuf, "%s_%s", id, name);
_writeToFile(GWEN_Buffer_GetStart(fnameBuf), value);
GWEN_Buffer_free(fnameBuf);
}
else {
DBG_ERROR(NULL, "Either id or name missing in item list file");
}
}
void _writeToFile(const char *filename, const char *txt)
{
if (txt && *txt) {
GWEN_BUFFER *tmpNameBuf;
int rv;
tmpNameBuf=GWEN_Buffer_new(0, 256, 0, 1);
GWEN_Buffer_AppendString(tmpNameBuf, filename);
GWEN_Buffer_AppendString(tmpNameBuf, ".tmp");
rv=GWEN_Directory_GetPath(GWEN_Buffer_GetStart(tmpNameBuf), GWEN_PATH_FLAGS_VARIABLE);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "Error getting path for %s (%d)", GWEN_Buffer_GetStart(tmpNameBuf), rv);
}
else {
FILE *f;
f=fopen(GWEN_Buffer_GetStart(tmpNameBuf), "w");
if (f) {
if (1!=fwrite(txt, strlen(txt), 1, f)) {
DBG_ERROR(AQH_LOGDOMAIN, "Error writing.");
fclose(f);
}
else {
fclose(f);
rename(GWEN_Buffer_GetStart(tmpNameBuf), filename);
}
}
}
GWEN_Buffer_free(tmpNameBuf);
}
}
const ITEM *_getItemForTopic(const ITEM_LIST *itemList, const char *topic)
{
const ITEM *item;
item=Item_List_First(itemList);
while(item) {
const char *s;
s=Item_GetTopic(item);
if (s && GWEN_Text_ComparePattern(topic, s, 0)!=-1)
return item;
item=Item_List_Next(item);
}
return NULL;
}

View File

@@ -0,0 +1,25 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQHOME_TOOL_MESSAGES_H
#define AQHOME_TOOL_MESSAGES_H
#include "./item.h"
#include <gwenhywfar/msg.h>
ITEM_LIST *AqHomeMqttLog_ReadItems(GWEN_DB_NODE *dbArgs);
void AqHomeMqttLog_HandlePublishMsg(const char *baseFolder, const ITEM_LIST *itemList, const GWEN_MSG *msg);
#endif

179
apps/aqhome-mqttlog/mqtt.c Normal file
View File

@@ -0,0 +1,179 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "./mqtt.h"
#include <aqhome/api.h>
#include <aqhome/aqhome.h>
#include "aqhome/mqtt/endpoint2_mqttc.h"
#include <aqhome/mqtt/msg_mqtt_connect.h>
#include <aqhome/mqtt/msg_mqtt_connack.h>
#include <aqhome/mqtt/msg_mqtt_publish.h>
#include <aqhome/mqtt/msg_mqtt_subscribe.h>
#include <aqhome/mqtt/msg_mqtt_suback.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <time.h>
#include <unistd.h>
/* ------------------------------------------------------------------------------------------------
* defines
* ------------------------------------------------------------------------------------------------
*/
#define AQHOME_MQTTLOG_DEFAULT_CMDTIMEOUT 10000
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static GWEN_MSG *_awaitPacket(GWEN_MSG_ENDPOINT2 *epTcp, uint8_t expectedPacketType, int timeoutInSeconds);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
GWEN_MSG_ENDPOINT2 *AqHomeMqttLog_CreateMqttEndpoint(GWEN_DB_NODE *dbArgs)
{
const char *mqttAddress;
int mqttPort;
const char *mqttClientId;
const char *mqttTopicPrefix;
int mqttKeepAlive;
mqttAddress=GWEN_DB_GetCharValue(dbArgs, "mqttAddress", 0, NULL);
mqttPort=GWEN_DB_GetIntValue(dbArgs, "mqttPort", 0, 1883);
mqttClientId=GWEN_DB_GetCharValue(dbArgs, "mqttClientId", 0, "aqhome-mqttlog");
mqttTopicPrefix=GWEN_DB_GetCharValue(dbArgs, "mqttTopicPrefix", 0, "aqhome/sensors");
mqttKeepAlive=GWEN_DB_GetIntValue(dbArgs, "mqttKeepAlive", 0, 600);
if (mqttAddress && *mqttAddress && mqttPort) {
GWEN_MSG_ENDPOINT2 *epMqtt;
DBG_INFO(AQH_LOGDOMAIN, "Connecting to %s (port %d)", mqttAddress, mqttPort);
epMqtt=AQH_MqttClientEndpoint2_new(mqttClientId, mqttAddress, mqttPort, NULL, 0);
if (epMqtt==NULL) {
DBG_ERROR(AQH_LOGDOMAIN, "Error creating endpoint TCP");
return NULL;
}
if (mqttTopicPrefix && *mqttTopicPrefix)
AQH_MqttClientEndpoint2_SetTopicPrefix(epMqtt, mqttTopicPrefix);
AQH_MqttClientEndpoint2_SetKeepAliveTime(epMqtt, mqttKeepAlive);
return epMqtt;
}
return NULL;
}
int AqHomeMqttLog_MqttConnect(GWEN_MSG_ENDPOINT2 *epTcp)
{
if (GWEN_MsgEndpoint2_GetState(epTcp)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
int rv;
rv=AQH_MqttClientEndpoint2_StartConnect(epTcp);
if (rv<0) {
DBG_ERROR(NULL, "Error starting to connect (%d)", rv);
return rv;
}
}
while(GWEN_MsgEndpoint2_GetState(epTcp)!=GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
DBG_DEBUG(NULL, "Next loop");
GWEN_MsgEndpoint2_IoLoop(epTcp, 2000); /* 2000 ms */
}
return 0;
}
int AqHomeMqttLog_Subscribe(GWEN_MSG_ENDPOINT2 *epTcp, const char *topicFilter)
{
uint16_t pckId;
GWEN_MSG *msgOut;
GWEN_MSG *msgIn;
DBG_INFO(NULL, "Sending SUBSCRIBE %s", topicFilter);
pckId=AQH_MqttClientEndpoint2_GetNextPacketId(epTcp);
msgOut=GWEN_SubscribeMqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE, pckId, topicFilter, 0);
if (msgOut==NULL) {
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_INTERNAL;
}
DBG_ERROR(NULL, "Sending this message:");
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msgOut), GWEN_Msg_GetBytesInBuffer(msgOut), 2);
GWEN_MsgEndpoint2_AddSendMessage(epTcp, msgOut);
DBG_INFO(NULL, "Waiting for response");
msgIn=_awaitPacket(epTcp, AQH_MQTTMSG_MSGTYPE_SUBACK, AQHOME_MQTTLOG_DEFAULT_CMDTIMEOUT);
if (msgIn) {
GWEN_BUFFER *buf;
buf=GWEN_Buffer_new(0, 256, 0, 1);
AQH_SubAckMqttMsg_DumpToBuffer(msgIn, buf, "received");
DBG_INFO(NULL, "%s", GWEN_Buffer_GetStart(buf));
GWEN_Buffer_free(buf);
GWEN_Msg_free(msgIn);
}
return 0;
}
GWEN_MSG *_awaitPacket(GWEN_MSG_ENDPOINT2 *epTcp, uint8_t expectedPacketType, int timeoutInSeconds)
{
time_t startTime;
startTime=time(NULL);
for (;;) {
GWEN_MSG *msg;
time_t now;
GWEN_MsgEndpoint2_IoLoop(epTcp, 2000); /* 2000 ms */
msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(epTcp);
if (msg) {
if ((AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0)==(expectedPacketType & 0xf0)) {
return msg;
}
else {
DBG_ERROR(NULL, "Received this message:");
GWEN_Text_DumpString((const char*) GWEN_Msg_GetConstBuffer(msg), GWEN_Msg_GetBytesInBuffer(msg), 2);
}
GWEN_Msg_free(msg);
}
now=time(NULL);
if (now-startTime>timeoutInSeconds) {
DBG_INFO(NULL, "Timeout");
break;
}
}
return NULL;
}

View File

@@ -0,0 +1,28 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQHOME_TOOL_MQTT_H
#define AQHOME_TOOL_MQTT_H
#include "aqhome/mqtt/endpoint2_mqttc.h"
#include <gwenhywfar/db.h>
GWEN_MSG_ENDPOINT2 *AqHomeMqttLog_CreateMqttEndpoint(GWEN_DB_NODE *dbArgs);
int AqHomeMqttLog_MqttConnect(GWEN_MSG_ENDPOINT2 *epTcp);
int AqHomeMqttLog_Subscribe(GWEN_MSG_ENDPOINT2 *epTcp, const char *topicFilter);
#endif

View File

@@ -13,9 +13,9 @@
#include "./flash.h"
#include "./utils.h"
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/ipc/msg_ipc_setaccmsggrps.h"
#include "aqhome/ipc/msg_ipc_forward.h"
#include "aqhome/ipc/endpoint_ipc_tcpc.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_flashready.h"
#include "aqhome/msg/msg_flashstart.h"
@@ -29,7 +29,6 @@
#include <gwenhywfar/args.h>
#include <gwenhywfar/i18n.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint_tcpc.h>
#include <gwenhywfar/text.h>
#include <time.h>
@@ -48,30 +47,28 @@ static int _doFlash(GWEN_DB_NODE *dbArgs);
static AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename);
static int _rebootNode(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds);
static int _performFlashProcedure(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_MSG_ENDPOINT *epTcp,
static int _rebootNode(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid, int timeoutInSeconds);
static int _performFlashProcedure(GWEN_MSG_ENDPOINT2 *epTcp,
unsigned int uid,
const AQH_FLASHRECORD_LIST *flashRecordList,
int pageSize,
int timeoutInSeconds);
static int _flashStart(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds);
static int _flashStart(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid, int timeoutInSeconds);
static GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
static GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT2 *epTcp,
unsigned int uid, int timeoutInSeconds);
static int _sendRebootRequest(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid);
static int _sendFlashStart(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid);
static int _waitForFlashResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
static int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
static int _sendFlashRecord(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_MSG_ENDPOINT *epTcp,
static int _sendRebootRequest(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid);
static int _sendFlashStart(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid);
static int _waitForFlashResponseMessage(GWEN_MSG_ENDPOINT2 *epTcp, int timeoutInSeconds);
static int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT2 *epTcp, int timeoutInSeconds);
static int _sendFlashRecord(GWEN_MSG_ENDPOINT2 *epTcp,
const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize,
int timeoutInSeconds);
static int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason);
static int _sendFlashEnd(GWEN_MSG_ENDPOINT2 *epTcp, int reason);
@@ -188,8 +185,7 @@ int AQH_Tool_Flash(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
int _doFlash(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT_MGR *emgr;
GWEN_MSG_ENDPOINT *epTcp;
GWEN_MSG_ENDPOINT2 *epTcp;
int rv;
int timeoutInSeconds;
int doReboot;
@@ -216,28 +212,26 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
}
/* setup client connection */
emgr=GWEN_MsgEndpointMgr_new();
epTcp=Utils_SetupIpcEndpoint(emgr, dbArgs);
epTcp=Utils_SetupIpcEndpoint(dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 3;
}
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
/* declare accepted message type groups */
rv=Utils_SendAcceptedMsgGroups(epTcp, AQH_MSG_TYPEGROUP_FLASH);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpoint2_free(epTcp);
return 3;
}
if (doReboot) {
fprintf(stdout, "Sending REBOOT request\n");
rv=_rebootNode(emgr, epTcp, uid, timeoutInSeconds);
rv=_rebootNode(epTcp, uid, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
return 3;
}
fprintf(stdout, "Reboot in progress\n");
@@ -245,10 +239,10 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
/* wait for FLASH_READY message */
fprintf(stdout, "Waiting for node to become ready for flashing\n");
msg=_waitForFlashReadyMessageForUid(emgr, epTcp, uid, timeoutInSeconds);
msg=_waitForFlashReadyMessageForUid(epTcp, uid, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No FLASH_READY message received.");
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
return 3;
}
DBG_INFO(NULL, "FLASH_READY message received");
@@ -257,18 +251,18 @@ int _doFlash(GWEN_DB_NODE *dbArgs)
GWEN_Msg_free(msg);
/* perform flash */
rv=_performFlashProcedure(emgr, epTcp, uid, flashRecordList, pageSize, timeoutInSeconds);
rv=_performFlashProcedure(epTcp, uid, flashRecordList, pageSize, timeoutInSeconds);
if (rv<0) {
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
AQH_FlashRecord_List_free(flashRecordList);
return 4;
}
}
AQH_FlashRecord_List_free(flashRecordList);
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
return 0;
}
@@ -299,7 +293,7 @@ AQH_FLASHRECORD_LIST *_readHexfileIntoFlashRecordList(const char *hexFilename)
int _rebootNode(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds)
int _rebootNode(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid, int timeoutInSeconds)
{
int rv;
@@ -311,7 +305,7 @@ int _rebootNode(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned
return rv;
}
rv=_waitForRebootResponseMessage(emgr, epTcp, timeoutInSeconds);
rv=_waitForRebootResponseMessage(epTcp, timeoutInSeconds);
if (rv!=0) {
DBG_INFO(NULL, "Bad or no reboot response received (%d).", rv);
return rv;
@@ -322,8 +316,7 @@ int _rebootNode(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned
int _performFlashProcedure(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_MSG_ENDPOINT *epTcp,
int _performFlashProcedure(GWEN_MSG_ENDPOINT2 *epTcp,
unsigned int uid,
const AQH_FLASHRECORD_LIST *flashRecordList,
int pageSize,
@@ -333,7 +326,7 @@ int _performFlashProcedure(GWEN_MSG_ENDPOINT_MGR *emgr,
const AQH_FLASHRECORD *flashRecord;
fprintf(stdout, "Sending FLASH_START\n");
rv=_flashStart(emgr, epTcp, uid, timeoutInSeconds);
rv=_flashStart(epTcp, uid, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
@@ -343,7 +336,7 @@ int _performFlashProcedure(GWEN_MSG_ENDPOINT_MGR *emgr,
flashRecord=AQH_FlashRecord_List_First(flashRecordList);
while(flashRecord) {
DBG_ERROR(NULL, "Sending flash record at %08x", AQH_FlashRecord_GetAddress(flashRecord));
rv=_sendFlashRecord(emgr, epTcp, flashRecord, pageSize, timeoutInSeconds);
rv=_sendFlashRecord(epTcp, flashRecord, pageSize, timeoutInSeconds);
if (rv!=0) {
DBG_ERROR(NULL, "Error sending flash data (%d)", rv);
return rv;
@@ -357,7 +350,7 @@ int _performFlashProcedure(GWEN_MSG_ENDPOINT_MGR *emgr,
return rv;
}
rv=Utils_FlushOutMessageQueue(emgr, epTcp, timeoutInSeconds);
rv=Utils_FlushOutMessageQueue(epTcp, timeoutInSeconds);
if (rv<0) {
DBG_INFO(NULL, "here (%d)", rv);
return rv;
@@ -368,7 +361,7 @@ int _performFlashProcedure(GWEN_MSG_ENDPOINT_MGR *emgr,
int _flashStart(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned int uid, int timeoutInSeconds)
int _flashStart(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid, int timeoutInSeconds)
{
int rv;
int i;
@@ -385,7 +378,7 @@ int _flashStart(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned
}
/* wait for response */
rv=_waitForFlashResponseMessage(emgr, epTcp, timeoutInSeconds);
rv=_waitForFlashResponseMessage(epTcp, timeoutInSeconds);
if (rv!=0) {
DBG_INFO(NULL, "Bad or no response received (%d).", rv);
return rv;
@@ -406,8 +399,7 @@ int _flashStart(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, unsigned
GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
unsigned int uid, int timeoutInSeconds)
GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid, int timeoutInSeconds)
{
int i;
@@ -415,7 +407,7 @@ GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_
GWEN_MSG *msg;
unsigned int receivedUid;
msg=Utils_WaitForSpecificNodeMessage(emgr, epTcp, AQH_MSG_TYPE_FLASH_READY, 0, timeoutInSeconds);
msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_FLASH_READY, 0, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No FLASH_READY message received.");
return NULL;
@@ -436,11 +428,11 @@ GWEN_MSG *_waitForFlashReadyMessageForUid(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_
int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT2 *epTcp, int timeoutInSeconds)
{
GWEN_MSG *msg;
msg=Utils_WaitForSpecificNodeMessage(emgr, epTcp, AQH_MSG_TYPE_REBOOT_RSP, 0, timeoutInSeconds);
msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_REBOOT_RSP, 0, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No REBOOT_RSP message received.");
return GWEN_ERROR_IO;
@@ -451,12 +443,12 @@ int _waitForRebootResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT
int _waitForFlashResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
int _waitForFlashResponseMessage(GWEN_MSG_ENDPOINT2 *epTcp, int timeoutInSeconds)
{
GWEN_MSG *msg;
int responseCode;
msg=Utils_WaitForSpecificNodeMessage(emgr, epTcp, AQH_MSG_TYPE_FLASH_RSP, 0, timeoutInSeconds);
msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_FLASH_RSP, 0, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No FLASH_RSP message received.");
return GWEN_ERROR_IO;
@@ -468,7 +460,7 @@ int _waitForFlashResponseMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT
int _sendRebootRequest(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid)
int _sendRebootRequest(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid)
{
GWEN_MSG *msgNode;
GWEN_MSG *msgOut;
@@ -480,14 +472,14 @@ int _sendRebootRequest(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid)
}
msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_FORWARD, GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode));
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_MsgEndpoint2_AddSendMessage(epTcp, msgOut);
GWEN_Msg_free(msgNode);
return 0;
}
int _sendFlashStart(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid)
int _sendFlashStart(GWEN_MSG_ENDPOINT2 *epTcp, unsigned int uid)
{
GWEN_MSG *msgNode;
GWEN_MSG *msgOut;
@@ -499,14 +491,14 @@ int _sendFlashStart(GWEN_MSG_ENDPOINT *epTcp, unsigned int uid)
}
msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_FORWARD, GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode));
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_MsgEndpoint2_AddSendMessage(epTcp, msgOut);
GWEN_Msg_free(msgNode);
return 0;
}
int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason)
int _sendFlashEnd(GWEN_MSG_ENDPOINT2 *epTcp, int reason)
{
GWEN_MSG *msgNode;
GWEN_MSG *msgOut;
@@ -518,15 +510,14 @@ int _sendFlashEnd(GWEN_MSG_ENDPOINT *epTcp, int reason)
}
msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_FORWARD, GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode));
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_MsgEndpoint2_AddSendMessage(epTcp, msgOut);
GWEN_Msg_free(msgNode);
return 0;
}
int _sendFlashRecord(GWEN_MSG_ENDPOINT_MGR *emgr,
GWEN_MSG_ENDPOINT *epTcp,
int _sendFlashRecord(GWEN_MSG_ENDPOINT2 *epTcp,
const AQH_FLASHRECORD *flashRecord,
uint16_t pageSize,
int timeoutInSeconds)
@@ -565,10 +556,10 @@ int _sendFlashRecord(GWEN_MSG_ENDPOINT_MGR *emgr,
}
msgOut=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_FORWARD, GWEN_Msg_GetConstBuffer(msgNode), GWEN_Msg_GetBytesInBuffer(msgNode));
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_MsgEndpoint2_AddSendMessage(epTcp, msgOut);
GWEN_Msg_free(msgNode);
rv=_waitForFlashResponseMessage(emgr, epTcp, timeoutInSeconds);
rv=_waitForFlashResponseMessage(epTcp, timeoutInSeconds);
if (rv==0)
break;
else {

View File

@@ -13,10 +13,10 @@
#include "./getdevices.h"
#include "./utils.h"
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/ipc/msg_ipc_getdevices_req.h"
#include "aqhome/ipc/msg_ipc_getdevices_rsp.h"
#include "aqhome/ipc/msg_ipc_error.h"
#include "aqhome/ipc/endpoint_ipc_tcpc.h"
#include "aqhome/msg/msg_node.h"
#include <gwenhywfar/args.h>
@@ -35,7 +35,7 @@
static int _doGetDevices(GWEN_DB_NODE *dbArgs);
static int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp);
static int _sendGetDevices(GWEN_MSG_ENDPOINT2 *epTcp);
@@ -119,19 +119,15 @@ int AQH_Tool_GetDevices(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
int _doGetDevices(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT_MGR *emgr;
GWEN_MSG_ENDPOINT *epTcp;
GWEN_MSG_ENDPOINT2 *epTcp;
int rv;
int timeoutInSeconds;
emgr=GWEN_MsgEndpointMgr_new();
epTcp=Utils_SetupIpcEndpoint(emgr, dbArgs);
epTcp=Utils_SetupIpcEndpoint(dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 2;
}
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
@@ -151,10 +147,10 @@ int _doGetDevices(GWEN_DB_NODE *dbArgs)
GWEN_MSG *msg;
uint16_t code;
msg=Utils_WaitForSpecificIpcMessage(emgr, epTcp, AQH_MSGTYPE_IPC_GETDEVICES_RSP, timeoutInSeconds);
msg=Utils_WaitForSpecificIpcMessage(epTcp, AQH_MSGTYPE_IPC_GETDEVICES_RSP, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No GET_DEVICE response received.");
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
return 2;
}
code=GWEN_IpcMsg_GetCode(msg);
@@ -201,13 +197,13 @@ int _doGetDevices(GWEN_DB_NODE *dbArgs)
}
}
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
return 0;
}
int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp)
int _sendGetDevices(GWEN_MSG_ENDPOINT2 *epTcp)
{
GWEN_MSG *msgOut;
@@ -216,7 +212,7 @@ int _sendGetDevices(GWEN_MSG_ENDPOINT *epTcp)
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_MsgEndpoint2_AddSendMessage(epTcp, msgOut);
return 0;
}

View File

@@ -16,7 +16,7 @@
#include "aqhome/ipc/msg_ipc_setaccmsggrps.h"
#include "aqhome/ipc/msg_ipc_ping.h"
#include "aqhome/ipc/msg_ipc_forward.h"
#include "aqhome/ipc/endpoint_ipc_tcpc.h"
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/msg/msg_node.h"
#include <gwenhywfar/args.h>
@@ -35,7 +35,7 @@
static int _doPing(GWEN_DB_NODE *dbArgs);
static int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr);
static int _sendPing(GWEN_MSG_ENDPOINT2 *epTcp, int nodeAddr);
@@ -130,21 +130,17 @@ int AQH_Tool_Ping(GWEN_DB_NODE *dbGlobalArgs, int argc, char **argv)
int _doPing(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT_MGR *emgr;
GWEN_MSG_ENDPOINT *epTcp;
GWEN_MSG_ENDPOINT2 *epTcp;
int rv;
int nodeAddr;
int timeoutInSeconds;
GWEN_MSG *msg;
emgr=GWEN_MsgEndpointMgr_new();
epTcp=Utils_SetupIpcEndpoint(emgr, dbArgs);
epTcp=Utils_SetupIpcEndpoint(dbArgs);
if (epTcp==NULL) {
DBG_ERROR(NULL, "ERROR creating TCP connection");
return 2;
}
GWEN_MsgEndpointMgr_AddEndpoint(emgr, epTcp);
nodeAddr=GWEN_DB_GetIntValue(dbArgs, "nodeAddr", 0, 0);
timeoutInSeconds=GWEN_DB_GetIntValue(dbArgs, "timeout", 0, 5);
@@ -163,21 +159,20 @@ int _doPing(GWEN_DB_NODE *dbArgs)
return 2;
}
msg=Utils_WaitForSpecificNodeMessage(emgr, epTcp, AQH_MSG_TYPE_PONG, nodeAddr, timeoutInSeconds);
msg=Utils_WaitForSpecificNodeMessage(epTcp, AQH_MSG_TYPE_PONG, nodeAddr, timeoutInSeconds);
if (msg==NULL) {
DBG_INFO(NULL, "No PONG response received.");
GWEN_MsgEndpointMgr_free(emgr);
return 2;
}
fprintf(stdout, "PONG response received\n");
GWEN_MsgEndpointMgr_free(emgr);
GWEN_MsgEndpoint2_free(epTcp);
return 0;
}
int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr)
int _sendPing(GWEN_MSG_ENDPOINT2 *epTcp, int nodeAddr)
{
GWEN_MSG *msgOut;
@@ -186,7 +181,7 @@ int _sendPing(GWEN_MSG_ENDPOINT *epTcp, int nodeAddr)
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_MsgEndpoint2_AddSendMessage(epTcp, msgOut);
return 0;
}

View File

@@ -13,10 +13,11 @@
#include "./utils.h"
#include "aqhome/ipc/endpoint_ipc_tcpc.h"
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/ipc/msg_ipc_setaccmsggrps.h"
#include "aqhome/ipc/msg_ipc_forward.h"
#include <gwenhywfar/endpoint2_tcpc.h>
#include <gwenhywfar/debug.h>
#include <time.h>
@@ -25,28 +26,35 @@
GWEN_MSG_ENDPOINT *Utils_SetupIpcEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs)
GWEN_MSG_ENDPOINT2 *Utils_SetupIpcEndpoint(GWEN_DB_NODE *dbArgs)
{
GWEN_MSG_ENDPOINT *epTcp;
GWEN_MSG_ENDPOINT2 *epTcp;
const char *tcpAddress;
int tcpPort;
int rv;
tcpAddress=GWEN_DB_GetCharValue(dbArgs, "tcpAddress", 0, "127.0.0.1");
tcpPort=GWEN_DB_GetIntValue(dbArgs, "tcpPort", 0, 45454);
DBG_INFO(NULL, "Setup tcp client endpoint to %s:%d", tcpAddress, tcpPort);
epTcp=AQH_IpcTcpClientEndpoint_new(tcpAddress, tcpPort, "aqhome-tool-IPC", 0);
epTcp=AQH_IpcEndpoint2_CreateIpcTcpClient(tcpAddress, tcpPort, "aqhome-tool-IPC", 0);
if (epTcp==NULL) {
DBG_ERROR(NULL, "Error creating endpoint TCPc");
return NULL;
}
rv=GWEN_TcpcEndpoint2_StartConnect(epTcp);
if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) {
DBG_ERROR(NULL, "Error connecting (%d)", rv);
GWEN_MsgEndpoint2_free(epTcp);
return NULL;
}
return epTcp;
}
GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT2 *epTcp,
int msgCode,
int nodeAddr,
int timeoutInSeconds)
@@ -59,9 +67,8 @@ GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG
GWEN_MSG *msg;
time_t now;
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
GWEN_MsgEndpoint2_IoLoop(epTcp, 2000); /* 2000 ms */
msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(epTcp);
if (msg) {
if (GWEN_IpcMsg_GetCode(msg)==AQH_MSGTYPE_IPC_FORWARD) {
GWEN_MSG *nodeMsg;
@@ -79,6 +86,9 @@ GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG
}
}
}
else {
DBG_INFO(NULL, "Received IPC message %d, ignoring", GWEN_IpcMsg_GetCode(msg));
}
GWEN_Msg_free(msg);
}
now=time(NULL);
@@ -93,7 +103,7 @@ GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG
GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT2 *epTcp,
int msgCode,
int timeoutInSeconds)
{
@@ -105,9 +115,8 @@ GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_
GWEN_MSG *msg;
time_t now;
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epTcp);
GWEN_MsgEndpoint2_IoLoop(epTcp, 2000); /* 2000 ms */
msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(epTcp);
if (msg) {
uint16_t code;
@@ -134,17 +143,16 @@ GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_
int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds)
int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT2 *epTcp, int timeoutInSeconds)
{
time_t startTime;
startTime=time(NULL);
while(GWEN_MsgEndpoint_HaveMessageToSend(epTcp)) {
while(GWEN_MsgEndpoint2_HaveMessageToSend(epTcp)) {
time_t now;
GWEN_MsgEndpointMgr_RunAllEndpoints(emgr);
GWEN_MsgEndpointMgr_IoLoopOnce(emgr);
GWEN_MsgEndpoint2_IoLoop(epTcp, 2000); /* 2000 ms */
now=time(NULL);
if (now-startTime>timeoutInSeconds) {
DBG_INFO(NULL, "Timeout");
@@ -157,7 +165,7 @@ int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *e
int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp, uint32_t groups)
int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT2 *epTcp, uint32_t groups)
{
GWEN_MSG *msgOut;
@@ -166,7 +174,7 @@ int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp, uint32_t groups)
DBG_ERROR(NULL, "Error creating message");
return GWEN_ERROR_GENERIC;
}
GWEN_MsgEndpoint_AddSendMessage(epTcp, msgOut);
GWEN_MsgEndpoint2_AddSendMessage(epTcp, msgOut);
return 0;
}

View File

@@ -11,24 +11,24 @@
#include <gwenhywfar/db.h>
#include <gwenhywfar/endpointmgr.h>
#include <gwenhywfar/endpoint2.h>
GWEN_MSG_ENDPOINT *Utils_SetupIpcEndpoint(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_DB_NODE *dbArgs);
GWEN_MSG_ENDPOINT2 *Utils_SetupIpcEndpoint(GWEN_DB_NODE *dbArgs);
GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
GWEN_MSG *Utils_WaitForSpecificNodeMessage(GWEN_MSG_ENDPOINT2 *epTcp,
int msgCode,
int nodeAddr,
int timeoutInSeconds);
GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp,
GWEN_MSG *Utils_WaitForSpecificIpcMessage(GWEN_MSG_ENDPOINT2 *epTcp,
int msgCode,
int timeoutInSeconds);
int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT_MGR *emgr, GWEN_MSG_ENDPOINT *epTcp, int timeoutInSeconds);
int Utils_FlushOutMessageQueue(GWEN_MSG_ENDPOINT2 *epTcp, int timeoutInSeconds);
int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT *epTcp, uint32_t groups);
int Utils_SendAcceptedMsgGroups(GWEN_MSG_ENDPOINT2 *epTcp, uint32_t groups);
#endif

View File

@@ -38,6 +38,10 @@
init.h
fini.h
loop.h
loop_tty.h
loop_tty_ipc.h
loop_tty_mqtt.h
loop_ipc.h
db.h
tty_log.h
tty_write.h
@@ -50,6 +54,10 @@
init.c
fini.c
loop.c
loop_tty.c
loop_tty_ipc.c
loop_tty_mqtt.c
loop_ipc.c
db.c
tty_log.c
tty_write.c

View File

@@ -74,6 +74,7 @@ void AqHomed_free(AQHOMED *aqh)
free(aqh->writeFolder);
free(aqh->pidFile);
free(aqh->dbFile);
free(aqh->mqttTopicPrefix);
GWEN_FREE_OBJECT(aqh);
}
@@ -177,7 +178,20 @@ void AqHomed_SetDbFile(AQHOMED *aqh, const char *s)
const char *AqHomed_GetMqttTopicPrefix(const AQHOMED *aqh)
{
return aqh?aqh->mqttTopicPrefix:NULL;
}
void AqHomed_SetMqttTopicPrefix(AQHOMED *aqh, const char *s)
{
if (aqh) {
free(aqh->mqttTopicPrefix);
aqh->mqttTopicPrefix=s?strdup(s):NULL;
}
}

View File

@@ -42,6 +42,8 @@ void AqHomed_SetPidFile(AQHOMED *aqh, const char *s);
const char *AqHomed_GetDbFile(const AQHOMED *aqh);
void AqHomed_SetDbFile(AQHOMED *aqh, const char *s);
const char *AqHomed_GetMqttTopicPrefix(const AQHOMED *aqh);
void AqHomed_SetMqttTopicPrefix(AQHOMED *aqh, const char *s);
#endif

View File

@@ -44,6 +44,7 @@ struct AQHOMED {
char *pidFile;
int nodeAddress;
char *mqttTopicPrefix;
};
#endif

View File

@@ -185,6 +185,7 @@ void _setupMqtt(AQHOMED *aqh, GWEN_DB_NODE *dbArgs)
int rv;
ep=AQH_MqttClientEndpoint2_new(mqttClientId, mqttAddress, mqttPort, NULL, AQHOME_ENDPOINTGROUP_MQTT);
AqHomed_SetMqttTopicPrefix(aqh, mqttTopicPrefix);
AQH_MqttClientEndpoint2_SetTopicPrefix(ep, mqttTopicPrefix);
AQH_MqttClientEndpoint2_SetKeepAliveTime(ep, mqttKeepAlive);

23
apps/aqhomed/ipc.h Normal file
View File

@@ -0,0 +1,23 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQHOMED_LOOP_IPC_H
#define AQHOMED_LOOP_IPC_H
#include "./aqhomed.h"
void AqHomed_ForwardTtyMsgToIpcClients(AQHOMED *aqh, const GWEN_MSG *msg);
#endif

View File

@@ -12,6 +12,8 @@
#include "./loop.h"
#include "./loop_tty.h"
#include "./loop_ipc.h"
#include "./aqhomed_p.h"
#include "./tty_log.h"
#include "./tty_write.h"
@@ -47,11 +49,6 @@
* ------------------------------------------------------------------------------------------------
*/
static void _readTtyMessages(AQHOMED *aqh);
static void _handleTtyMsg(AQHOMED *aqh, const GWEN_MSG *msg);
static void _forwardTtyMsgToIpcClients(AQHOMED *aqh, const GWEN_MSG *msg);
static void _forwardValue2MsgToIpc(GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *nodeMsg);
static void _forwardAnyMsgToIpc(GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *nodeMsg);
@@ -64,7 +61,8 @@ void AqHomed_Loop(AQHOMED *aqh, int timeoutInMsecs)
{
if (aqh) {
GWEN_MsgEndpoint2_ChildrenIoLoop(aqh->rootEndpoint, timeoutInMsecs);
_readTtyMessages(aqh);
AqHomed_ReadAndHandleTtyMessages(aqh);
AqHomed_ReadAndHandleIpcMessages(aqh);
if (AQH_NodeDb_IsModified(aqh->nodeDb)) {
if (aqh->dbFile) {
@@ -81,88 +79,3 @@ void AqHomed_Loop(AQHOMED *aqh, int timeoutInMsecs)
void _readTtyMessages(AQHOMED *aqh)
{
GWEN_MSG *msg;
while( (msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(aqh->ttyEndpoint)) ) {
_handleTtyMsg(aqh, msg);
GWEN_Msg_free(msg);
}
}
void _handleTtyMsg(AQHOMED *aqh, const GWEN_MSG *msg)
{
if (aqh->logFile)
AqHomed_LogTtyMsg(aqh, msg);
if (aqh->writeFolder)
AqHomed_WriteTtyMsg(aqh, msg);
if (aqh->nodeDb)
AqHomed_NodeMsgToDb(aqh, msg);
if (aqh->ipcdEndpoint)
_forwardTtyMsgToIpcClients(aqh, msg);
}
void _forwardTtyMsgToIpcClients(AQHOMED *aqh, const GWEN_MSG *msg)
{
uint32_t msgGroup;
msgGroup=AQH_NodeMsg_GetMsgGroup(AQH_NodeMsg_GetMsgType(msg));
if (msgGroup) {
GWEN_MSG_ENDPOINT2 *ep;
ep=GWEN_MsgEndpoint2_Tree2_GetFirstChild(aqh->ipcdEndpoint);
while(ep) {
if (msgGroup & AQH_IpcEndpoint2_GetAcceptedMsgGroups(ep)) {
DBG_INFO(NULL, "Endpoint accepts msg group %d", msgGroup);
switch(AQH_NodeMsg_GetMsgType(msg)) {
case AQH_MSG_TYPE_VALUE2:
_forwardValue2MsgToIpc(ep, msg);
break;
default:
_forwardAnyMsgToIpc(ep, msg);
break;
}
}
ep=GWEN_MsgEndpoint2_Tree2_GetNext(ep);
}
}
else {
DBG_ERROR(NULL, "Message type %d not in any message group, ignoring message", AQH_NodeMsg_GetMsgType(msg));
}
}
void _forwardValue2MsgToIpc(GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *nodeMsg)
{
GWEN_MSG *ipcMsg;
ipcMsg=AQH_ValueIpcMsg_new(AQH_MSGTYPE_IPC_VALUE,
AQH_Value2Msg_GetUid(nodeMsg),
AQH_Value2Msg_GetValueId(nodeMsg),
AQH_Value2Msg_GetValueType(nodeMsg),
AQH_Value2Msg_GetValueNom(nodeMsg),
AQH_Value2Msg_GetValueDenom(nodeMsg));
GWEN_MsgEndpoint2_AddSendMessage(ep, ipcMsg);
}
void _forwardAnyMsgToIpc(GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *nodeMsg)
{
GWEN_MSG *ipcMsg;
ipcMsg=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_FORWARD, GWEN_Msg_GetConstBuffer(nodeMsg), GWEN_Msg_GetBytesInBuffer(nodeMsg));
GWEN_MsgEndpoint2_AddSendMessage(ep, ipcMsg);
}

188
apps/aqhomed/loop_ipc.c Normal file
View File

@@ -0,0 +1,188 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "./loop_ipc.h"
#include "./aqhomed_p.h"
#include "./tty_log.h"
#include "./tty_write.h"
#include "./db.h"
#include "aqhome/msg/endpoint2_tty.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/msg/msg_ping.h"
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/ipc/msg_ipc.h"
#include "aqhome/ipc/msg_ipc_forward.h"
#include "aqhome/ipc/msg_ipc_value.h"
#include "aqhome/ipc/msg_ipc_ping.h"
#include "aqhome/ipc/msg_ipc_setaccmsggrps.h"
#include "aqhome/ipc/msg_ipc_getdevices_rsp.h"
#include "aqhome/ipc/msg_ipc_error.h"
#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint2_tcpd.h>
/* ------------------------------------------------------------------------------------------------
* defines
* ------------------------------------------------------------------------------------------------
*/
#define I18N(msg) msg
#define I18S(msg) msg
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void _handleIpcEndpoint(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep);
static void _handleIpcMsg(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg);
void _handleIpcMsgPing(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg);
void _handleIpcMsgSetAccMsgGrps(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg);
void _handleIpcMsgForward(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg);
void _handleIpcMsgGetDevicesReq(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
void AqHomed_ReadAndHandleIpcMessages(AQHOMED *aqh)
{
if (aqh->ipcdEndpoint) {
GWEN_MSG_ENDPOINT2 *ep;
ep=GWEN_MsgEndpoint2_Tree2_GetFirstChild(aqh->ipcdEndpoint);
while(ep) {
_handleIpcEndpoint(aqh, ep);
ep=GWEN_MsgEndpoint2_Tree2_GetNext(ep);
}
}
}
void _handleIpcEndpoint(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep)
{
GWEN_MSG *msg;
while( (msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(ep)) ) {
_handleIpcMsg(aqh, ep, msg);
GWEN_Msg_free(msg);
}
}
void _handleIpcMsg(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg)
{
uint16_t code;
/* exec IPC message */
code=GWEN_IpcMsg_GetCode(msg);
DBG_ERROR(AQH_LOGDOMAIN, "Received IPC packet");
switch(code) {
case AQH_MSGTYPE_IPC_PING: _handleIpcMsgPing(aqh, ep, msg); break;
case AQH_MSGTYPE_IPC_SETACCMSGGRPS: _handleIpcMsgSetAccMsgGrps(aqh, ep, msg); break;
case AQH_MSGTYPE_IPC_FORWARD: _handleIpcMsgForward(aqh, ep, msg); break;
case AQH_MSGTYPE_IPC_GETDEVICES_REQ: _handleIpcMsgGetDevicesReq(aqh, ep, msg); break;
default: break;
}
}
void _handleIpcMsgPing(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg)
{
if (aqh->ttyEndpoint && GWEN_MsgEndpoint2_GetState(aqh->ttyEndpoint)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
GWEN_MSG *msgOut;
DBG_ERROR(AQH_LOGDOMAIN, "Received IPC PING message");
msgOut=AQH_PingMsg_new(aqh->nodeAddress, AQH_PingIpcMsg_GetDestAddr(msg), AQH_MSG_TYPE_PING);
GWEN_MsgEndpoint2_AddSendMessage(aqh->ttyEndpoint, msgOut);
}
}
void _handleIpcMsgSetAccMsgGrps(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg)
{
uint32_t groups;
DBG_ERROR(AQH_LOGDOMAIN, "Received IPC SET_ACCEPTED_MSG_GROUPS message");
groups=AQH_SetAcceptedMsgGroupsIpcMsg_GetMsgGroups(msg);
AQH_IpcEndpoint2_SetAcceptedMsgGroups(ep, groups);
// TODO: send response?
}
void _handleIpcMsgForward(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg)
{
if (aqh->ttyEndpoint && GWEN_MsgEndpoint2_GetState(aqh->ttyEndpoint)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
GWEN_MSG *msgOut;
DBG_ERROR(AQH_LOGDOMAIN, "Received IPC FORWARD message");
msgOut=AQH_ForwardIpcMsg_GetCopyOfNodeMsg(msg);
if (msgOut)
GWEN_MsgEndpoint2_AddSendMessage(aqh->ttyEndpoint, msgOut);
}
}
void _handleIpcMsgGetDevicesReq(AQHOMED *aqh, GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *msg)
{
AQH_NODE_INFO_LIST *nodeInfoList;
DBG_ERROR(AQH_LOGDOMAIN, "Received IPC GetDevicesRequest message");
nodeInfoList=AQH_NodeDb_GetAllNodeInfos(aqh->nodeDb);
if (nodeInfoList && AQH_NodeInfo_List_GetCount(nodeInfoList)) {
const AQH_NODE_INFO *ni;
ni=AQH_NodeInfo_List_First(nodeInfoList);
while(ni) {
const AQH_NODE_INFO *niNext;
GWEN_MSG *msgOut;
niNext=AQH_NodeInfo_List_Next(ni);
DBG_INFO(AQH_LOGDOMAIN, "Sending response for node %02x (%08x)", AQH_NodeInfo_GetBusAddress(ni), AQH_NodeInfo_GetUid(ni));
msgOut=AQH_GetDevicesResponseIpcMsg_new(AQH_MSGTYPE_IPC_GETDEVICES_RSP, niNext?0:AQH_MSGIPC_GETDEVICES_RSP_FLAGS_LAST, ni);
GWEN_MsgEndpoint2_AddSendMessage(ep, msgOut);
ni=niNext;
}
}
else {
GWEN_MSG *msgOut;
DBG_INFO(AQH_LOGDOMAIN, "No nodes");
msgOut=AQH_ErrorIpcMsg_new(AQH_MSGTYPE_IPC_ERROR, AQH_MSG_IPC_ERROR_NODATA);
GWEN_MsgEndpoint2_AddSendMessage(ep, msgOut);
}
}

23
apps/aqhomed/loop_ipc.h Normal file
View File

@@ -0,0 +1,23 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQHOMED_LOOP_IPC_H
#define AQHOMED_LOOP_IPC_H
#include "./aqhomed.h"
void AqHomed_ReadAndHandleIpcMessages(AQHOMED *aqh);
#endif

89
apps/aqhomed/loop_tty.c Normal file
View File

@@ -0,0 +1,89 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "./loop_tty.h"
#include "./loop_tty_ipc.h"
#include "./loop_tty_mqtt.h"
#include "./aqhomed_p.h"
#include "./tty_log.h"
#include "./tty_write.h"
#include "./db.h"
#include "aqhome/msg/endpoint2_tty.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/ipc/msg_ipc_forward.h"
#include "aqhome/ipc/msg_ipc_value.h"
#include "aqhome/mqtt/endpoint2_mqttc.h"
#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint2_tcpd.h>
/* ------------------------------------------------------------------------------------------------
* defines
* ------------------------------------------------------------------------------------------------
*/
#define I18N(msg) msg
#define I18S(msg) msg
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void _handleTtyMsg(AQHOMED *aqh, const GWEN_MSG *msg);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
void AqHomed_ReadAndHandleTtyMessages(AQHOMED *aqh)
{
GWEN_MSG *msg;
while( (msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(aqh->ttyEndpoint)) ) {
_handleTtyMsg(aqh, msg);
GWEN_Msg_free(msg);
}
}
void _handleTtyMsg(AQHOMED *aqh, const GWEN_MSG *msg)
{
if (aqh->logFile)
AqHomed_LogTtyMsg(aqh, msg);
if (aqh->writeFolder)
AqHomed_WriteTtyMsg(aqh, msg);
if (aqh->nodeDb)
AqHomed_NodeMsgToDb(aqh, msg);
if (aqh->ipcdEndpoint)
AqHomed_ForwardTtyMsgToIpcClients(aqh, msg);
if (aqh->mqttEndpoint)
AqHomed_ForwardTtyMsgToMqttServer(aqh, msg);
}

23
apps/aqhomed/loop_tty.h Normal file
View File

@@ -0,0 +1,23 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQHOMED_LOOP_TTY_H
#define AQHOMED_LOOP_TTY_H
#include "./aqhomed.h"
void AqHomed_ReadAndHandleTtyMessages(AQHOMED *aqh);
#endif

118
apps/aqhomed/loop_tty_ipc.c Normal file
View File

@@ -0,0 +1,118 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "./loop_tty_ipc.h"
#include "./aqhomed_p.h"
#include "./tty_log.h"
#include "./tty_write.h"
#include "./db.h"
#include "aqhome/msg/endpoint2_tty.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/ipc/msg_ipc_forward.h"
#include "aqhome/ipc/msg_ipc_value.h"
#include "aqhome/mqtt/endpoint2_mqttc.h"
#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint2_tcpd.h>
/* ------------------------------------------------------------------------------------------------
* defines
* ------------------------------------------------------------------------------------------------
*/
#define I18N(msg) msg
#define I18S(msg) msg
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void _forwardValue2MsgToIpc(GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *nodeMsg);
static void _forwardAnyMsgToIpc(GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *nodeMsg);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
void AqHomed_ForwardTtyMsgToIpcClients(AQHOMED *aqh, const GWEN_MSG *msg)
{
uint32_t msgGroup;
msgGroup=AQH_NodeMsg_GetMsgGroup(AQH_NodeMsg_GetMsgType(msg));
if (msgGroup) {
GWEN_MSG_ENDPOINT2 *ep;
ep=GWEN_MsgEndpoint2_Tree2_GetFirstChild(aqh->ipcdEndpoint);
while(ep) {
if (msgGroup & AQH_IpcEndpoint2_GetAcceptedMsgGroups(ep)) {
DBG_INFO(NULL, "Endpoint accepts msg group %d", msgGroup);
switch(AQH_NodeMsg_GetMsgType(msg)) {
case AQH_MSG_TYPE_VALUE2:
_forwardValue2MsgToIpc(ep, msg);
break;
default:
_forwardAnyMsgToIpc(ep, msg);
break;
}
}
ep=GWEN_MsgEndpoint2_Tree2_GetNext(ep);
}
}
else {
DBG_ERROR(NULL, "Message type %d not in any message group, ignoring message", AQH_NodeMsg_GetMsgType(msg));
}
}
void _forwardValue2MsgToIpc(GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *nodeMsg)
{
GWEN_MSG *ipcMsg;
ipcMsg=AQH_ValueIpcMsg_new(AQH_MSGTYPE_IPC_VALUE,
AQH_Value2Msg_GetUid(nodeMsg),
AQH_Value2Msg_GetValueId(nodeMsg),
AQH_Value2Msg_GetValueType(nodeMsg),
AQH_Value2Msg_GetValueNom(nodeMsg),
AQH_Value2Msg_GetValueDenom(nodeMsg));
GWEN_MsgEndpoint2_AddSendMessage(ep, ipcMsg);
}
void _forwardAnyMsgToIpc(GWEN_MSG_ENDPOINT2 *ep, const GWEN_MSG *nodeMsg)
{
GWEN_MSG *ipcMsg;
ipcMsg=AQH_ForwardIpcMsg_new(AQH_MSGTYPE_IPC_FORWARD, GWEN_Msg_GetConstBuffer(nodeMsg), GWEN_Msg_GetBytesInBuffer(nodeMsg));
GWEN_MsgEndpoint2_AddSendMessage(ep, ipcMsg);
}

View File

@@ -0,0 +1,23 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQHOMED_LOOP_TTY_IPC_H
#define AQHOMED_LOOP_TTY_IPC_H
#include "./aqhomed.h"
void AqHomed_ForwardTtyMsgToIpcClients(AQHOMED *aqh, const GWEN_MSG *msg);
#endif

View File

@@ -0,0 +1,215 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "./loop_tty_mqtt.h"
#include "./aqhomed_p.h"
#include "./tty_log.h"
#include "./tty_write.h"
#include "./db.h"
#include "aqhome/msg/endpoint2_tty.h"
#include "aqhome/msg/msg_node.h"
#include "aqhome/msg/msg_value2.h"
#include "aqhome/msg/msg_sendstats.h"
#include "aqhome/msg/msg_recvstats.h"
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/ipc/msg_ipc_forward.h"
#include "aqhome/ipc/msg_ipc_value.h"
#include "aqhome/mqtt/endpoint2_mqttc.h"
#include "aqhome/mqtt/msg_mqtt_publish.h"
#include <gwenhywfar/gwenhywfar.h>
#include <gwenhywfar/args.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/endpoint2_tcpd.h>
/* ------------------------------------------------------------------------------------------------
* defines
* ------------------------------------------------------------------------------------------------
*/
#define I18N(msg) msg
#define I18S(msg) msg
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void _processValue2Message(AQHOMED *aqh, const GWEN_MSG *nodeMsg);
static void _processSendStatsMessage(AQHOMED *aqh, const GWEN_MSG *nodeMsg);
static void _processRecvStatsMessage(AQHOMED *aqh, const GWEN_MSG *nodeMsg);
static void _publishDouble(AQHOMED *aqh, uint32_t uid, int valueId, const char *valuePath, double v);
static void _publishInt(AQHOMED *aqh, uint32_t uid, int valueId, const char *valuePath, int v);
static void _publishString(AQHOMED *aqh, uint32_t uid, int valueId, const char *valuePath, const char *v);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
void AqHomed_ForwardTtyMsgToMqttServer(AQHOMED *aqh, const GWEN_MSG *nodeMsg)
{
if (GWEN_MsgEndpoint2_GetState(aqh->mqttEndpoint)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
DBG_DEBUG(AQH_LOGDOMAIN, "Processing output message");
switch(AQH_NodeMsg_GetMsgType(nodeMsg)) {
case AQH_MSG_TYPE_VALUE2:
_processValue2Message(aqh, nodeMsg);
break;
case AQH_MSG_TYPE_COMSENDSTATS:
_processSendStatsMessage(aqh, nodeMsg);
break;
case AQH_MSG_TYPE_COMRECVSTATS:
_processRecvStatsMessage(aqh, nodeMsg);
break;
default:
break;
}
}
}
void _processValue2Message(AQHOMED *aqh, const GWEN_MSG *nodeMsg)
{
if (AQH_Value2Msg_GetValueType(nodeMsg)==AQH_MSG_VALUE2_TYPE_DOOR)
_publishString(aqh,
AQH_Value2Msg_GetUid(nodeMsg),
AQH_Value2Msg_GetValueId(nodeMsg),
AQH_Value2Msg_GetValueTypeName(nodeMsg),
AQH_Value2Msg_GetValueAsWindowStateString(nodeMsg));
else
_publishDouble(aqh,
AQH_Value2Msg_GetUid(nodeMsg),
AQH_Value2Msg_GetValueId(nodeMsg),
AQH_Value2Msg_GetValueTypeName(nodeMsg),
AQH_Value2Msg_GetValue(nodeMsg));
}
void _processSendStatsMessage(AQHOMED *aqh, const GWEN_MSG *nodeMsg)
{
uint16_t packetsOutInt;
packetsOutInt=AQH_SendStatsMsg_GetPacketsOut(nodeMsg);
if (packetsOutInt) {
double packetsOut;
double collisions;
double busy;
double collisionsPercentage=0.0;
double busyPercentage=0.0;
packetsOut=(double) packetsOutInt;
collisions=(double)AQH_SendStatsMsg_GetCollisions(nodeMsg);
busy=(double)AQH_SendStatsMsg_GetBusyErrors(nodeMsg);
collisionsPercentage=collisions*100.0/packetsOut;
busyPercentage=busy*100.0/packetsOut;
_publishInt(aqh, AQH_SendStatsMsg_GetUid(nodeMsg), 0, "net/packetsOut", packetsOutInt);
_publishInt(aqh, AQH_SendStatsMsg_GetUid(nodeMsg), 0, "net/collisions", (int) AQH_SendStatsMsg_GetCollisions(nodeMsg));
_publishDouble(aqh, AQH_SendStatsMsg_GetUid(nodeMsg), 0, "net/collisionsPercent", collisionsPercentage);
_publishDouble(aqh, AQH_SendStatsMsg_GetUid(nodeMsg), 0, "net/busyPercent", busyPercentage);
}
}
void _processRecvStatsMessage(AQHOMED *aqh, const GWEN_MSG *nodeMsg)
{
uint16_t packetsInInt;
packetsInInt=AQH_RecvStatsMsg_GetPacketsIn(nodeMsg);
if (packetsInInt) {
double packetsIn;
double crcErrors;
double ioErrors;
double crcErrorsPercentage=0.0;
double ioErrorsPercentage=0.0;
packetsIn=(double) packetsInInt;
crcErrors=(double)AQH_RecvStatsMsg_GetCrcErrors(nodeMsg);
ioErrors=(double)AQH_RecvStatsMsg_GetIoErrors(nodeMsg);
crcErrorsPercentage=crcErrors*100.0/packetsIn;
ioErrorsPercentage=ioErrors*100.0/packetsIn;
_publishInt(aqh, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/packetsIn", packetsInInt);
_publishInt(aqh, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/crcerrors", (int) AQH_RecvStatsMsg_GetCrcErrors(nodeMsg));
_publishInt(aqh, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/ioerrors", (int) AQH_RecvStatsMsg_GetIoErrors(nodeMsg));
_publishDouble(aqh, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/crcerrorsPercent", crcErrorsPercentage);
_publishDouble(aqh, AQH_RecvStatsMsg_GetUid(nodeMsg), 0, "net/ioerrorsPercent", ioErrorsPercentage);
}
}
void _publishDouble(AQHOMED *aqh, uint32_t uid, int valueId, const char *valuePath, double v)
{
char numBuf[16];
snprintf(numBuf, sizeof(numBuf)-1, "%f", v);
numBuf[sizeof(numBuf)-1]=0;
_publishString(aqh, uid, valueId, valuePath, numBuf);
}
void _publishInt(AQHOMED *aqh, uint32_t uid, int valueId, const char *valuePath, int v)
{
char numBuf[16];
snprintf(numBuf, sizeof(numBuf)-1, "%d", v);
numBuf[sizeof(numBuf)-1]=0;
_publishString(aqh, uid, valueId, valuePath, numBuf);
}
void _publishString(AQHOMED *aqh, uint32_t uid, int valueId, const char *valuePath, const char *v)
{
GWEN_BUFFER *bufTopic;
GWEN_MSG *pubMsg;
bufTopic=GWEN_Buffer_new(0, 64, 0, 1);
if (valueId>0)
GWEN_Buffer_AppendArgs(bufTopic, "%s/%08x/%d/%s",
aqh->mqttTopicPrefix,
uid,
valueId,
valuePath);
else
GWEN_Buffer_AppendArgs(bufTopic, "%s/%08x/%s",
aqh->mqttTopicPrefix,
uid,
valuePath);
pubMsg=AQH_PublishMqttMsg_new(0, 0, GWEN_Buffer_GetStart(bufTopic), (const uint8_t*) v, strlen(v));
if (pubMsg) {
DBG_INFO(AQH_LOGDOMAIN, "MQTT PUBLISH %s: %s", GWEN_Buffer_GetStart(bufTopic), v);
GWEN_MsgEndpoint2_AddSendMessage(aqh->mqttEndpoint, pubMsg);
}
GWEN_Buffer_free(bufTopic);
}

View File

@@ -0,0 +1,23 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQHOMED_LOOP_TTY_MQTT_H
#define AQHOMED_LOOP_TTY_MQTT_H
#include "./aqhomed.h"
void AqHomed_ForwardTtyMsgToMqttServer(AQHOMED *aqh, const GWEN_MSG *msg);
#endif