adapted to latest changes in gwen (msgio API v2 becomes v1).

This commit is contained in:
Martin Preuss
2023-07-12 19:30:53 +02:00
parent 7a4edb6854
commit 0fd58567fe
37 changed files with 956 additions and 956 deletions

View File

@@ -45,7 +45,7 @@
<headers dist="true" install="$(pkgincludedir)/ipc" >
endpoint2_ipc.h
endpoint_ipc.h
msg_ipc.h
msg_ipc_forward.h
msg_ipc_value.h
@@ -58,14 +58,14 @@
<headers dist="true" >
endpoint2_ipc_p.h
endpoint_ipc_p.h
</headers>
<sources>
$(local/typefiles)
endpoint2_ipc.c
endpoint_ipc.c
msg_ipc.c
msg_ipc_forward.c
msg_ipc_value.c

View File

@@ -1,139 +0,0 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "aqhome/ipc/endpoint2_ipc_p.h"
#include <gwenhywfar/endpoint2_ipc.h>
#include <gwenhywfar/debug.h>
#define AQH_MSG_ENDPOINT2_IPC_NAME "ipc"
GWEN_INHERIT(GWEN_MSG_ENDPOINT, AQH_ENDPOINT2_IPC)
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
void AQH_IpcEndpoint2_Extend(GWEN_MSG_ENDPOINT2 *ep)
{
AQH_ENDPOINT2_IPC *xep;
GWEN_NEW_OBJECT(AQH_ENDPOINT2_IPC, xep);
GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_IPC, ep, xep, _freeData);
}
void _freeData(void *bp, void *p)
{
AQH_ENDPOINT2_IPC *xep;
xep=(AQH_ENDPOINT2_IPC*) p;
GWEN_FREE_OBJECT(xep);
}
uint32_t AQH_IpcEndpoint2_GetAcceptedMsgGroups(const GWEN_MSG_ENDPOINT2 *ep)
{
if (ep) {
AQH_ENDPOINT2_IPC *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_IPC, ep);
if (xep) {
return xep->acceptedMsgGroups;
}
}
return 0;
}
void AQH_IpcEndpoint2_SetAcceptedMsgGroups(GWEN_MSG_ENDPOINT2 *ep, uint32_t i)
{
if (ep) {
AQH_ENDPOINT2_IPC *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_IPC, ep);
if (xep)
xep->acceptedMsgGroups=i;
}
}
void AQH_IpcEndpoint2_AddAcceptedMsgGroups(GWEN_MSG_ENDPOINT2 *ep, uint32_t i)
{
if (ep) {
AQH_ENDPOINT2_IPC *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_IPC, ep);
if (xep)
xep->acceptedMsgGroups|=i;
}
}
void AQH_IpcEndpoint2_SubAcceptedMsgGroups(GWEN_MSG_ENDPOINT2 *ep, uint32_t i)
{
if (ep) {
AQH_ENDPOINT2_IPC *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_IPC, ep);
if (xep)
xep->acceptedMsgGroups&=~i;
}
}
GWEN_MSG_ENDPOINT2 *AQH_IpcEndpoint2_CreateIpcTcpClient(const char *host, int port, const char *name, int groupId)
{
GWEN_MSG_ENDPOINT2 *ep;
ep=GWEN_IpcEndpoint2_CreateIpcTcpClient(host, port, name?name:AQH_MSG_ENDPOINT2_IPC_NAME, groupId);
AQH_IpcEndpoint2_Extend(ep);
return ep;
}
GWEN_MSG_ENDPOINT2 *AQH_IpcEndpoint2_CreateIpcTcpServiceForSocket(GWEN_SOCKET *sk, const char *name, int groupId)
{
GWEN_MSG_ENDPOINT2 *ep;
ep=GWEN_IpcEndpoint2_CreateIpcTcpServiceForSocket(sk, name?name:AQH_MSG_ENDPOINT2_IPC_NAME, groupId);
AQH_IpcEndpoint2_Extend(ep);
return ep;
}

View File

@@ -1,31 +0,0 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQH_ENDPOINT2_IPC_H
#define AQH_ENDPOINT2_IPC_H
#include <aqhome/api.h>
#include <gwenhywfar/endpoint2.h>
AQHOME_API void AQH_IpcEndpoint2_Extend(GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API GWEN_MSG_ENDPOINT2 *AQH_IpcEndpoint2_CreateIpcTcpClient(const char *host, int port, const char *name, int groupId);
AQHOME_API GWEN_MSG_ENDPOINT2 *AQH_IpcEndpoint2_CreateIpcTcpServiceForSocket(GWEN_SOCKET *sk, const char *name, int groupId);
AQHOME_API uint32_t AQH_IpcEndpoint2_GetAcceptedMsgGroups(const GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API void AQH_IpcEndpoint2_SetAcceptedMsgGroups(GWEN_MSG_ENDPOINT2 *ep, uint32_t i);
AQHOME_API void AQH_IpcEndpoint2_AddAcceptedMsgGroups(GWEN_MSG_ENDPOINT2 *ep, uint32_t i);
AQHOME_API void AQH_IpcEndpoint2_SubAcceptedMsgGroups(GWEN_MSG_ENDPOINT2 *ep, uint32_t i);
#endif

139
aqhome/ipc/endpoint_ipc.c Normal file
View File

@@ -0,0 +1,139 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "aqhome/ipc/endpoint_ipc_p.h"
#include <gwenhywfar/endpoint_ipc.h>
#include <gwenhywfar/debug.h>
#define AQH_MSG_ENDPOINT_IPC_NAME "ipc"
GWEN_INHERIT(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC)
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
void AQH_IpcEndpoint_Extend(GWEN_MSG_ENDPOINT *ep)
{
AQH_ENDPOINT_IPC *xep;
GWEN_NEW_OBJECT(AQH_ENDPOINT_IPC, xep);
GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep, xep, _freeData);
}
void _freeData(void *bp, void *p)
{
AQH_ENDPOINT_IPC *xep;
xep=(AQH_ENDPOINT_IPC*) p;
GWEN_FREE_OBJECT(xep);
}
uint32_t AQH_IpcEndpoint_GetAcceptedMsgGroups(const GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
AQH_ENDPOINT_IPC *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
if (xep) {
return xep->acceptedMsgGroups;
}
}
return 0;
}
void AQH_IpcEndpoint_SetAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i)
{
if (ep) {
AQH_ENDPOINT_IPC *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
if (xep)
xep->acceptedMsgGroups=i;
}
}
void AQH_IpcEndpoint_AddAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i)
{
if (ep) {
AQH_ENDPOINT_IPC *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
if (xep)
xep->acceptedMsgGroups|=i;
}
}
void AQH_IpcEndpoint_SubAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i)
{
if (ep) {
AQH_ENDPOINT_IPC *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_IPC, ep);
if (xep)
xep->acceptedMsgGroups&=~i;
}
}
GWEN_MSG_ENDPOINT *AQH_IpcEndpoint_CreateIpcTcpClient(const char *host, int port, const char *name, int groupId)
{
GWEN_MSG_ENDPOINT *ep;
ep=GWEN_IpcEndpoint_CreateIpcTcpClient(host, port, name?name:AQH_MSG_ENDPOINT_IPC_NAME, groupId);
AQH_IpcEndpoint_Extend(ep);
return ep;
}
GWEN_MSG_ENDPOINT *AQH_IpcEndpoint_CreateIpcTcpServiceForSocket(GWEN_SOCKET *sk, const char *name, int groupId)
{
GWEN_MSG_ENDPOINT *ep;
ep=GWEN_IpcEndpoint_CreateIpcTcpServiceForSocket(sk, name?name:AQH_MSG_ENDPOINT_IPC_NAME, groupId);
AQH_IpcEndpoint_Extend(ep);
return ep;
}

31
aqhome/ipc/endpoint_ipc.h Normal file
View File

@@ -0,0 +1,31 @@
/****************************************************************************
* This file is part of the project AqHome.
* AqHome (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifndef AQH_ENDPOINT_IPC_H
#define AQH_ENDPOINT_IPC_H
#include <aqhome/api.h>
#include <gwenhywfar/endpoint.h>
AQHOME_API void AQH_IpcEndpoint_Extend(GWEN_MSG_ENDPOINT *ep);
AQHOME_API GWEN_MSG_ENDPOINT *AQH_IpcEndpoint_CreateIpcTcpClient(const char *host, int port, const char *name, int groupId);
AQHOME_API GWEN_MSG_ENDPOINT *AQH_IpcEndpoint_CreateIpcTcpServiceForSocket(GWEN_SOCKET *sk, const char *name, int groupId);
AQHOME_API uint32_t AQH_IpcEndpoint_GetAcceptedMsgGroups(const GWEN_MSG_ENDPOINT *ep);
AQHOME_API void AQH_IpcEndpoint_SetAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i);
AQHOME_API void AQH_IpcEndpoint_AddAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i);
AQHOME_API void AQH_IpcEndpoint_SubAcceptedMsgGroups(GWEN_MSG_ENDPOINT *ep, uint32_t i);
#endif

View File

@@ -6,19 +6,19 @@
* should have received along with this file.
****************************************************************************/
#ifndef AQH_ENDPOINT2_IPC_P_H
#define AQH_ENDPOINT2_IPC_P_H
#ifndef AQH_ENDPOINT_IPC_P_H
#define AQH_ENDPOINT_IPC_P_H
#include <aqhome/api.h>
#include <gwenhywfar/endpoint2.h>
#include <gwenhywfar/endpoint.h>
#include "aqhome/ipc/endpoint2_ipc.h"
#include "aqhome/ipc/endpoint_ipc.h"
typedef struct AQH_ENDPOINT2_IPC AQH_ENDPOINT2_IPC;
struct AQH_ENDPOINT2_IPC {
typedef struct AQH_ENDPOINT_IPC AQH_ENDPOINT_IPC;
struct AQH_ENDPOINT_IPC {
uint32_t acceptedMsgGroups;
};

View File

@@ -7,7 +7,7 @@
#include "aqhome/ipc/msg_ipc_ping.h"
#include "aqhome/ipc/msg_ipc_forward.h"
#include "aqhome/ipc/msg_ipc_setaccmsggrps.h"
#include "aqhome/mqtt/endpoint2_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"
@@ -28,8 +28,8 @@
#include <time.h>
static int _mqttConnect2(GWEN_MSG_ENDPOINT2 *epClient);
static GWEN_MSG *_awaitPacket2(GWEN_MSG_ENDPOINT2 *epClient, uint8_t expectedPacketType);
static int _mqttConnect2(GWEN_MSG_ENDPOINT *epClient);
static GWEN_MSG *_awaitPacket2(GWEN_MSG_ENDPOINT *epClient, uint8_t expectedPacketType);
@@ -104,18 +104,18 @@ GWEN_MSG *createPingMsg(uint8_t destAddr, uint8_t srcAddr)
int testMqttConnection2()
{
GWEN_MSG_ENDPOINT2 *epClient;
GWEN_MSG_ENDPOINT *epClient;
int loop;
AQH_Init();
epClient=AQH_MqttClientEndpoint2_new("TESTCLIENT1234", "127.0.0.1", 1883, NULL, 1);
epClient=AQH_MqttClientEndpoint_new("TESTCLIENT1234", "127.0.0.1", 1883, NULL, 1);
for (loop=0;; loop++) {
DBG_INFO(GWEN_LOGDOMAIN, "Loop %d:", loop);
GWEN_MsgEndpoint2_IoLoop(epClient, 2000); /* 2000 ms */
GWEN_MsgEndpoint_IoLoop(epClient, 2000); /* 2000 ms */
if (GWEN_MsgEndpoint2_GetState(epClient)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
if (GWEN_MsgEndpoint_GetState(epClient)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
DBG_INFO(AQH_LOGDOMAIN, "Connected.");
break;
}
@@ -127,7 +127,7 @@ int testMqttConnection2()
int testMqttSubscribe2(int argc, char **argv)
{
GWEN_MSG_ENDPOINT2 *epClient;
GWEN_MSG_ENDPOINT *epClient;
int rv;
GWEN_MSG *msgOut;
GWEN_MSG *msgIn;
@@ -140,14 +140,14 @@ int testMqttSubscribe2(int argc, char **argv)
host=argv[1];
DBG_ERROR(AQH_LOGDOMAIN, "Connecting to %s (%s)", host, argv[1]);
epClient=AQH_MqttClientEndpoint2_new("TESTCLIENT1234", host, 1883, NULL, 1);
epClient=AQH_MqttClientEndpoint_new("TESTCLIENT1234", host, 1883, NULL, 1);
rv=_mqttConnect2(epClient);
if (rv<0) {
DBG_INFO(AQH_LOGDOMAIN, "here (%d)", rv);
return 2;
}
pckId=AQH_MqttClientEndpoint2_GetNextPacketId(epClient);
pckId=AQH_MqttClientEndpoint_GetNextPacketId(epClient);
//msgOut=AQH_PublishMqttMsg_new(AQH_MQTTMSG_FLAGS_QOS1, 1, "test/subject1", (const uint8_t*) "29.9", 4);
//msgOut=GWEN_SubscribeMqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE, pckId, "aqhome/#", 0);
msgOut=GWEN_SubscribeMqttMsg_new(AQH_MQTTMSG_MSGTYPE_SUBSCRIBE, pckId, "#", 0);
@@ -155,7 +155,7 @@ int testMqttSubscribe2(int argc, char **argv)
DBG_ERROR(NULL, "Error creating message");
return 2;
}
GWEN_MsgEndpoint2_AddSendMessage(epClient, msgOut);
GWEN_MsgEndpoint_AddSendMessage(epClient, msgOut);
msgIn=_awaitPacket2(epClient, AQH_MQTTMSG_MSGTYPE_SUBACK);
if (msgIn) {
@@ -171,8 +171,8 @@ int testMqttSubscribe2(int argc, char **argv)
for (;;) {
GWEN_MSG *msg;
GWEN_MsgEndpoint2_IoLoop(epClient, 2000); /* 2000 ms */
msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(epClient);
GWEN_MsgEndpoint_IoLoop(epClient, 2000); /* 2000 ms */
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epClient);
if (msg) {
if ((AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0)==(AQH_MQTTMSG_MSGTYPE_PUBLISH & 0xf0)) {
GWEN_BUFFER *buf;
@@ -196,19 +196,19 @@ int testMqttSubscribe2(int argc, char **argv)
int _mqttConnect2(GWEN_MSG_ENDPOINT2 *epClient)
int _mqttConnect2(GWEN_MSG_ENDPOINT *epClient)
{
int loop;
for (loop=0;; loop++) {
DBG_INFO(GWEN_LOGDOMAIN, "Loop %d:", loop);
GWEN_MsgEndpoint2_IoLoop(epClient, 2000); /* 2000 ms */
GWEN_MsgEndpoint_IoLoop(epClient, 2000); /* 2000 ms */
if (GWEN_MsgEndpoint2_GetState(epClient)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
if (GWEN_MsgEndpoint_GetState(epClient)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
DBG_INFO(AQH_LOGDOMAIN, "Connected.");
break;
}
else if (GWEN_MsgEndpoint2_GetState(epClient)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
else if (GWEN_MsgEndpoint_GetState(epClient)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_INFO(AQH_LOGDOMAIN, "Disconnected.");
return GWEN_ERROR_IO;
}
@@ -219,15 +219,15 @@ int _mqttConnect2(GWEN_MSG_ENDPOINT2 *epClient)
GWEN_MSG *_awaitPacket2(GWEN_MSG_ENDPOINT2 *epClient, uint8_t expectedPacketType)
GWEN_MSG *_awaitPacket2(GWEN_MSG_ENDPOINT *epClient, uint8_t expectedPacketType)
{
fprintf(stdout, "Waiting for response\n");
for (;;) {
GWEN_MSG *msg;
DBG_DEBUG(AQH_LOGDOMAIN, "Next loop");
GWEN_MsgEndpoint2_IoLoop(epClient, 2000); /* 2000 ms */
msg=GWEN_MsgEndpoint2_TakeFirstReceivedMessage(epClient);
GWEN_MsgEndpoint_IoLoop(epClient, 2000); /* 2000 ms */
msg=GWEN_MsgEndpoint_TakeFirstReceivedMessage(epClient);
if (msg) {
if ((AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0)==(expectedPacketType & 0xf0)) {
return msg;

View File

@@ -45,8 +45,8 @@
<headers dist="true" install="$(pkgincludedir)/ipc" >
endpoint2_mqtt.h
endpoint2_mqttc.h
endpoint_mqtt.h
endpoint_mqttc.h
msg_mqtt.h
msg_mqtt_connect.h
msg_mqtt_connack.h
@@ -58,15 +58,15 @@
<headers dist="true" >
endpoint2_mqtt_p.h
endpoint_mqtt_p.h
</headers>
<sources>
$(local/typefiles)
endpoint2_mqtt.c
endpoint2_mqttc.c
endpoint_mqtt.c
endpoint_mqttc.c
msg_mqtt.c
msg_mqtt_connect.c
msg_mqtt_connack.c

View File

@@ -1,392 +0,0 @@
/****************************************************************************
* This file is part of the project Gwenhywfar.
* Gwenhywfar (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "aqhome/mqtt/endpoint2_mqttc.h"
#include "aqhome/mqtt/endpoint2_mqtt.h"
#include "aqhome/mqtt/msg_mqtt_connect.h"
#include "aqhome/mqtt/msg_mqtt_connack.h"
#include "aqhome/mqtt/msg_mqtt_publish.h"
#include <gwenhywfar/endpoint2_tcpc.h>
#include <gwenhywfar/endpoint2_msgio.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/inherit.h>
#define AQH_ENDPOINT2_MQTT_NAME "mqtt-client"
#define AQH_ENDPOINT2_MQTTC_RECONNECT_TIME 5
#define AQH_ENDPOINT2_MQTTC_CONNECT_TIMEOUT 10
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void _addSockets(GWEN_MSG_ENDPOINT2 *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_UNUSED GWEN_SOCKETSET *xSet);
static void _checkSockets(GWEN_MSG_ENDPOINT2 *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static int _startConnect(GWEN_MSG_ENDPOINT2 *ep);
static void _moveMessagesBetweenLists(GWEN_MSG_LIST *srcList, GWEN_MSG_LIST *dstList);
static void _addSocketsWhenUnconnected(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static void _addSocketsWhenConnecting(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static void _addSocketsWhenConnected(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static void _checkSocketsWhenConnecting(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static void _checkSocketsWhenConnected(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
GWEN_MSG_ENDPOINT2 *AQH_MqttClientEndpoint2_new(const char *clientId,
const char *host, int port,
const char *name, int groupId)
{
GWEN_MSG_ENDPOINT2 *ep;
GWEN_MSG_ENDPOINT2 *epChild;
ep=GWEN_MsgEndpoint2_new(name?name:AQH_ENDPOINT2_MQTT_NAME, groupId);
GWEN_MsgEndpoint2_SetAddSocketsFn(ep, _addSockets);
GWEN_MsgEndpoint2_SetCheckSocketsFn(ep, _checkSockets);
epChild=GWEN_TcpcEndpoint2_new(host, port, NULL, groupId);
GWEN_MsgIoEndpoint2_Extend(epChild);
AQH_MqttEndpoint2_Extend(epChild);
AQH_MqttEndpoint2_SetClientId(epChild, clientId);
GWEN_MsgEndpoint2_Tree2_AddChild(ep, epChild);
return ep;
}
int AQH_MqttClientEndpoint2_StartConnect(GWEN_MSG_ENDPOINT2 *ep)
{
if (ep) {
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
int rv;
/* connect, set state */
rv=_startConnect(ep);
if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) {
DBG_INFO(AQH_LOGDOMAIN, "Endpoint %s: Error connecting (%d)", GWEN_MsgEndpoint2_GetName(ep), rv);
GWEN_MsgEndpoint2_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTING);
}
else {
DBG_INFO(AQH_LOGDOMAIN, "Endpoint %s: Connecting.", GWEN_MsgEndpoint2_GetName(ep));
GWEN_MsgEndpoint2_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTING);
}
return rv;
}
else {
DBG_ERROR(AQH_LOGDOMAIN, "Endpoint %s: Not unconnected", GWEN_MsgEndpoint2_GetName(ep));
}
}
else {
DBG_ERROR(GWEN_LOGDOMAIN, "No endpoint");
}
return GWEN_ERROR_GENERIC;
}
uint16_t AQH_MqttClientEndpoint2_GetKeepAliveTime(const GWEN_MSG_ENDPOINT2 *ep)
{
if (ep) {
GWEN_MSG_ENDPOINT2 *epChild;
epChild=GWEN_MsgEndpoint2_Tree2_GetFirstChild(ep);
if (epChild)
return AQH_MqttEndpoint2_GetKeepAliveTime(epChild);
}
return 0;
}
void AQH_MqttClientEndpoint2_SetKeepAliveTime(GWEN_MSG_ENDPOINT2 *ep, uint16_t i)
{
if (ep) {
GWEN_MSG_ENDPOINT2 *epChild;
epChild=GWEN_MsgEndpoint2_Tree2_GetFirstChild(ep);
if (epChild) {
AQH_MqttEndpoint2_SetKeepAliveTime(epChild, i);
}
}
}
uint16_t AQH_MqttClientEndpoint2_GetNextPacketId(const GWEN_MSG_ENDPOINT2 *ep)
{
if (ep) {
GWEN_MSG_ENDPOINT2 *epChild;
epChild=GWEN_MsgEndpoint2_Tree2_GetFirstChild(ep);
if (epChild) {
return AQH_MqttEndpoint2_GetNextPacketId(epChild);
}
}
return 0;
}
const char *AQH_MqttClientEndpoint2_GetTopicPrefix(const GWEN_MSG_ENDPOINT2 *ep)
{
if (ep) {
GWEN_MSG_ENDPOINT2 *epChild;
epChild=GWEN_MsgEndpoint2_Tree2_GetFirstChild(ep);
if (epChild)
return AQH_MqttEndpoint2_GetTopicPrefix(epChild);
}
return NULL;
}
void AQH_MqttClientEndpoint2_SetTopicPrefix(GWEN_MSG_ENDPOINT2 *ep, const char *s)
{
if (ep) {
GWEN_MSG_ENDPOINT2 *epChild;
epChild=GWEN_MsgEndpoint2_Tree2_GetFirstChild(ep);
if (epChild)
AQH_MqttEndpoint2_SetTopicPrefix(epChild, s);
}
}
void _addSockets(GWEN_MSG_ENDPOINT2 *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
if (ep) {
GWEN_MSG_ENDPOINT2 *epChild;
epChild=GWEN_MsgEndpoint2_Tree2_GetFirstChild(ep);
if (epChild) {
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED)
_addSocketsWhenUnconnected(ep, epChild, readSet, writeSet, xSet);
else {
if (GWEN_MsgEndpoint2_GetState(epChild)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_ERROR(AQH_LOGDOMAIN, "Error on tcp layer, disconnecting");
GWEN_MsgEndpoint2_Disconnect(epChild);
GWEN_MsgEndpoint2_Disconnect(ep);
}
else {
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTING)
_addSocketsWhenConnecting(ep, epChild, readSet, writeSet, xSet);
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTED)
_addSocketsWhenConnected(ep, epChild, readSet, writeSet, xSet);
}
}
} /* if (epChild) */
} /* if (ep) */
}
void _checkSockets(GWEN_MSG_ENDPOINT2 *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
DBG_DEBUG(AQH_LOGDOMAIN, "Checking sockets in state %d", GWEN_MsgEndpoint2_GetState(ep));
if (ep) {
GWEN_MSG_ENDPOINT2 *epChild;
epChild=GWEN_MsgEndpoint2_Tree2_GetFirstChild(ep);
if (epChild) {
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
/* nothing to do here */
} /* if GWEN_MSG_ENDPOINT_STATE_UNCONNECTED */
else {
if (GWEN_MsgEndpoint2_GetState(epChild)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_ERROR(AQH_LOGDOMAIN, "Error on tcp layer, disconnecting");
GWEN_MsgEndpoint2_Disconnect(epChild);
GWEN_MsgEndpoint2_Disconnect(ep);
}
else {
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTING)
_checkSocketsWhenConnecting(ep, epChild, readSet, writeSet, xSet);
else if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTED)
_checkSocketsWhenConnected(ep, epChild, readSet, writeSet, xSet);
}
}
}
}
}
void _addSocketsWhenUnconnected(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
time_t now;
now=time(NULL);
if ((now-GWEN_MsgEndpoint2_GetTimeOfLastStateChange(ep))>=AQH_ENDPOINT2_MQTTC_RECONNECT_TIME) {
int rv;
/* (re)connect, set state */
DBG_INFO(AQH_LOGDOMAIN, "Starting to (re-)connect");
rv=AQH_MqttClientEndpoint2_StartConnect(ep);
if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) {
DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
}
}
}
void _addSocketsWhenConnecting(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
time_t now;
now=time(NULL);
if ((now-GWEN_MsgEndpoint2_GetTimeOfLastStateChange(ep))>=AQH_ENDPOINT2_MQTTC_CONNECT_TIMEOUT ||
GWEN_MsgEndpoint2_GetState(epChild)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_ERROR(AQH_LOGDOMAIN, "Timeout on connect");
GWEN_MsgEndpoint2_Disconnect(epChild);
GWEN_MsgEndpoint2_Disconnect(ep);
}
else
GWEN_MsgEndpoint2_AddSockets(epChild, readSet, writeSet, xSet);
}
void _addSocketsWhenConnected(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
if (GWEN_MsgEndpoint2_GetState(epChild)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_ERROR(AQH_LOGDOMAIN, "Error on tcp layer, disconnecting");
GWEN_MsgEndpoint2_Disconnect(epChild);
GWEN_MsgEndpoint2_Disconnect(ep);
}
else {
/* move to-send messages to child */
_moveMessagesBetweenLists(GWEN_MsgEndpoint2_GetSendMessageList(ep), GWEN_MsgEndpoint2_GetSendMessageList(epChild));
GWEN_MsgEndpoint2_AddSockets(epChild, readSet, writeSet, xSet);
}
}
void _checkSocketsWhenConnecting(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
GWEN_MSG *msg;
GWEN_MsgEndpoint2_CheckSockets(epChild, readSet, writeSet, xSet); /* let base layer work */
msg=GWEN_MsgEndpoint2_GetFirstReceivedMessage(epChild);
while(msg) {
GWEN_MSG *msgNext;
uint8_t msgType;
msgNext=GWEN_Msg_List_Next(msg);
msgType=AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0;
if (msgType==AQH_MQTTMSG_MSGTYPE_CONNACK) {
int code;
GWEN_Msg_List_Del(msg); /* remove from list */
code=AQH_ConnAckMqttMsg_GetResultCode(msg);
if (code==AQH_MQTTMSG_CONNACK_RESULT_ACCEPTED) {
DBG_INFO(AQH_LOGDOMAIN, "Positive CONNACK response, connected");
GWEN_MsgEndpoint2_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTED);
}
else {
DBG_ERROR(AQH_LOGDOMAIN, "Negative CONNACK response (%d)", code);
GWEN_MsgEndpoint2_Disconnect(epChild);
GWEN_MsgEndpoint2_Disconnect(ep);
}
GWEN_Msg_free(msg);
break;
}
else {
DBG_ERROR(AQH_LOGDOMAIN, "Ignoring response (%d)", msgType);
}
msg=msgNext;
} /* while */
}
void _checkSocketsWhenConnected(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG_ENDPOINT2 *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
_moveMessagesBetweenLists(GWEN_MsgEndpoint2_GetSendMessageList(ep), GWEN_MsgEndpoint2_GetSendMessageList(epChild));
GWEN_MsgEndpoint2_CheckSockets(epChild, readSet, writeSet, xSet);
_moveMessagesBetweenLists(GWEN_MsgEndpoint2_GetReceivedMessageList(epChild), GWEN_MsgEndpoint2_GetReceivedMessageList(ep));
}
int _startConnect(GWEN_MSG_ENDPOINT2 *ep)
{
GWEN_MSG_ENDPOINT2 *epChild;
epChild=GWEN_MsgEndpoint2_Tree2_GetFirstChild(ep);
if (epChild) {
int rv;
GWEN_MSG *msg;
rv=GWEN_TcpcEndpoint2_StartConnect(epChild);
if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) {
DBG_INFO(AQH_LOGDOMAIN, "Error starting to connect child layer (%d)", rv);
return rv;
}
msg=AQH_MqttEndpoint2_CreateMsgConnect(epChild);
if (msg) {
GWEN_MsgEndpoint2_AddSendMessage(epChild, msg);
GWEN_MsgEndpoint2_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTING);
return rv; /* result from GWEN_TcpcEndpoint2_StartConnect() above */
}
}
return GWEN_ERROR_GENERIC;
}
void _moveMessagesBetweenLists(GWEN_MSG_LIST *srcList, GWEN_MSG_LIST *dstList)
{
GWEN_MSG *msg;
while( (msg=GWEN_Msg_List_First(srcList)) ) {
GWEN_Msg_List_Del(msg);
GWEN_Msg_List_Add(msg, dstList);
}
}

View File

@@ -10,21 +10,21 @@
# include <config.h>
#endif
#include "aqhome/mqtt/endpoint2_mqtt_p.h"
#include "aqhome/mqtt/endpoint_mqtt_p.h"
#include "aqhome/mqtt/msg_mqtt_connect.h"
#include "aqhome/mqtt/msg_mqtt_publish.h"
#include <gwenhywfar/endpoint2_msgio.h>
#include <gwenhywfar/endpoint_msgio.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/inherit.h>
#define AQH_ENDPOINT2_MQTT_DEFAULT_KEEPALIVE 600
#define AQH_ENDPOINT_MQTT_DEFAULT_KEEPALIVE 600
GWEN_INHERIT(GWEN_MSG_ENDPOINT, AQH_ENDPOINT2_MQTT)
GWEN_INHERIT(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT)
@@ -35,7 +35,7 @@ GWEN_INHERIT(GWEN_MSG_ENDPOINT, AQH_ENDPOINT2_MQTT)
*/
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
static int _getBytesNeededForMessage(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg);
static int _getBytesNeededForMessage(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg);
static int _calcAndSetPayloadSizeAndOffset(GWEN_MSG *msg);
@@ -46,16 +46,16 @@ static int _calcAndSetPayloadSizeAndOffset(GWEN_MSG *msg);
*/
void AQH_MqttEndpoint2_Extend(GWEN_MSG_ENDPOINT2 *ep)
void AQH_MqttEndpoint_Extend(GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
GWEN_NEW_OBJECT(AQH_ENDPOINT2_MQTT, xep);
GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep, xep, _freeData);
xep->keepAliveTime=AQH_ENDPOINT2_MQTT_DEFAULT_KEEPALIVE;
GWEN_NEW_OBJECT(AQH_ENDPOINT_MQTT, xep);
GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep, xep, _freeData);
xep->keepAliveTime=AQH_ENDPOINT_MQTT_DEFAULT_KEEPALIVE;
GWEN_MsgIoEndpoint2_SetGetNeededBytesFn(ep, _getBytesNeededForMessage);
GWEN_MsgIoEndpoint_SetGetNeededBytesFn(ep, _getBytesNeededForMessage);
}
}
@@ -63,9 +63,9 @@ void AQH_MqttEndpoint2_Extend(GWEN_MSG_ENDPOINT2 *ep)
void GWENHYWFAR_CB _freeData(GWEN_UNUSED void *bp, void *p)
{
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=(AQH_ENDPOINT2_MQTT*) p;
xep=(AQH_ENDPOINT_MQTT*) p;
free(xep->clientId);
free(xep->topicPrefix);
GWEN_FREE_OBJECT(xep);
@@ -73,12 +73,12 @@ void GWENHYWFAR_CB _freeData(GWEN_UNUSED void *bp, void *p)
const char *AQH_MqttEndpoint2_GetClientId(const GWEN_MSG_ENDPOINT2 *ep)
const char *AQH_MqttEndpoint_GetClientId(const GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
return xep->clientId;
}
@@ -88,12 +88,12 @@ const char *AQH_MqttEndpoint2_GetClientId(const GWEN_MSG_ENDPOINT2 *ep)
void AQH_MqttEndpoint2_SetClientId(GWEN_MSG_ENDPOINT2 *ep, const char *s)
void AQH_MqttEndpoint_SetClientId(GWEN_MSG_ENDPOINT *ep, const char *s)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
free(xep->clientId);
xep->clientId=s?strdup(s):NULL;
@@ -103,12 +103,12 @@ void AQH_MqttEndpoint2_SetClientId(GWEN_MSG_ENDPOINT2 *ep, const char *s)
const char *AQH_MqttEndpoint2_GetTopicPrefix(const GWEN_MSG_ENDPOINT2 *ep)
const char *AQH_MqttEndpoint_GetTopicPrefix(const GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
return xep->topicPrefix;
}
@@ -118,12 +118,12 @@ const char *AQH_MqttEndpoint2_GetTopicPrefix(const GWEN_MSG_ENDPOINT2 *ep)
void AQH_MqttEndpoint2_SetTopicPrefix(GWEN_MSG_ENDPOINT2 *ep, const char *s)
void AQH_MqttEndpoint_SetTopicPrefix(GWEN_MSG_ENDPOINT *ep, const char *s)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
free(xep->topicPrefix);
xep->topicPrefix=s?strdup(s):NULL;
@@ -133,12 +133,12 @@ void AQH_MqttEndpoint2_SetTopicPrefix(GWEN_MSG_ENDPOINT2 *ep, const char *s)
uint16_t AQH_MqttEndpoint2_GetNextPacketId(const GWEN_MSG_ENDPOINT2 *ep)
uint16_t AQH_MqttEndpoint_GetNextPacketId(const GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
return ++(xep->lastPacketId);
}
@@ -148,12 +148,12 @@ uint16_t AQH_MqttEndpoint2_GetNextPacketId(const GWEN_MSG_ENDPOINT2 *ep)
uint16_t AQH_MqttEndpoint2_GetKeepAliveTime(const GWEN_MSG_ENDPOINT2 *ep)
uint16_t AQH_MqttEndpoint_GetKeepAliveTime(const GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
return xep->keepAliveTime;
}
@@ -163,12 +163,12 @@ uint16_t AQH_MqttEndpoint2_GetKeepAliveTime(const GWEN_MSG_ENDPOINT2 *ep)
void AQH_MqttEndpoint2_SetKeepAliveTime(GWEN_MSG_ENDPOINT2 *ep, uint16_t i)
void AQH_MqttEndpoint_SetKeepAliveTime(GWEN_MSG_ENDPOINT *ep, uint16_t i)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
xep->keepAliveTime=i;
}
@@ -177,7 +177,7 @@ void AQH_MqttEndpoint2_SetKeepAliveTime(GWEN_MSG_ENDPOINT2 *ep, uint16_t i)
int _getBytesNeededForMessage(GWEN_UNUSED GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg)
int _getBytesNeededForMessage(GWEN_UNUSED GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg)
{
uint32_t bytesInMsg;
@@ -239,13 +239,13 @@ int _calcAndSetPayloadSizeAndOffset(GWEN_MSG *msg)
GWEN_MSG *AQH_MqttEndpoint2_CreateMsgConnect(GWEN_MSG_ENDPOINT2 *ep)
GWEN_MSG *AQH_MqttEndpoint_CreateMsgConnect(GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
DBG_INFO(AQH_LOGDOMAIN, "Sending connect msg");
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
return GWEN_ConnectMqttMsg_new("MQTT", 4, 0, xep->keepAliveTime, xep->clientId, NULL, NULL);
}
@@ -255,38 +255,38 @@ GWEN_MSG *AQH_MqttEndpoint2_CreateMsgConnect(GWEN_MSG_ENDPOINT2 *ep)
GWEN_MSG *AQH_MqttEndpoint2_CreateMsgPublishDouble(GWEN_MSG_ENDPOINT2 *ep, uint32_t uid, int valueId, const char *valuePath, double v)
GWEN_MSG *AQH_MqttEndpoint_CreateMsgPublishDouble(GWEN_MSG_ENDPOINT *ep, 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;
return AQH_MqttEndpoint2_CreateMsgPublishString(ep, uid, valueId, valuePath, numBuf);
return AQH_MqttEndpoint_CreateMsgPublishString(ep, uid, valueId, valuePath, numBuf);
}
GWEN_MSG *AQH_MqttEndpoint2_CreateMsgPublishInt(GWEN_MSG_ENDPOINT2 *ep, uint32_t uid, int valueId, const char *valuePath, int v)
GWEN_MSG *AQH_MqttEndpoint_CreateMsgPublishInt(GWEN_MSG_ENDPOINT *ep, 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;
return AQH_MqttEndpoint2_CreateMsgPublishString(ep, uid, valueId, valuePath, numBuf);
return AQH_MqttEndpoint_CreateMsgPublishString(ep, uid, valueId, valuePath, numBuf);
}
GWEN_MSG *AQH_MqttEndpoint2_CreateMsgPublishString(GWEN_MSG_ENDPOINT2 *ep,
GWEN_MSG *AQH_MqttEndpoint_CreateMsgPublishString(GWEN_MSG_ENDPOINT *ep,
uint32_t uid,
int valueId,
const char *valuePath,
const char *v)
{
if (ep) {
AQH_ENDPOINT2_MQTT *xep;
AQH_ENDPOINT_MQTT *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_ENDPOINT2_MQTT, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_ENDPOINT_MQTT, ep);
if (xep) {
GWEN_BUFFER *bufTopic;
GWEN_MSG *pubMsg;

View File

@@ -6,13 +6,13 @@
* should have received along with this file.
****************************************************************************/
#ifndef AQH_ENDPOINT2_MQTT_H
#define AQH_ENDPOINT2_MQTT_H
#ifndef AQH_ENDPOINT_MQTT_H
#define AQH_ENDPOINT_MQTT_H
#include <aqhome/api.h>
#include <gwenhywfar/endpoint2.h>
#include <gwenhywfar/endpoint.h>
#ifdef __cplusplus
@@ -21,37 +21,37 @@ extern "C" {
/**
* Extends the given endpoint to support MQTT messages. It expects the function GWEN_MsgIoEndpoint2_Extend() to have been called
* Extends the given endpoint to support MQTT messages. It expects the function GWEN_MsgIoEndpoint_Extend() to have been called
* beforehand.
*/
AQHOME_API void AQH_MqttEndpoint2_Extend(GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API void AQH_MqttEndpoint_Extend(GWEN_MSG_ENDPOINT *ep);
AQHOME_API const char *AQH_MqttEndpoint2_GetClientId(const GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API void AQH_MqttEndpoint2_SetClientId(GWEN_MSG_ENDPOINT2 *ep, const char *s);
AQHOME_API const char *AQH_MqttEndpoint_GetClientId(const GWEN_MSG_ENDPOINT *ep);
AQHOME_API void AQH_MqttEndpoint_SetClientId(GWEN_MSG_ENDPOINT *ep, const char *s);
AQHOME_API const char *AQH_MqttEndpoint2_GetTopicPrefix(const GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API void AQH_MqttEndpoint2_SetTopicPrefix(GWEN_MSG_ENDPOINT2 *ep, const char *s);
AQHOME_API const char *AQH_MqttEndpoint_GetTopicPrefix(const GWEN_MSG_ENDPOINT *ep);
AQHOME_API void AQH_MqttEndpoint_SetTopicPrefix(GWEN_MSG_ENDPOINT *ep, const char *s);
AQHOME_API uint16_t AQH_MqttEndpoint2_GetNextPacketId(const GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API uint16_t AQH_MqttEndpoint_GetNextPacketId(const GWEN_MSG_ENDPOINT *ep);
AQHOME_API uint16_t AQH_MqttEndpoint2_GetKeepAliveTime(const GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API void AQH_MqttEndpoint2_SetKeepAliveTime(GWEN_MSG_ENDPOINT2 *ep, uint16_t i);
AQHOME_API uint16_t AQH_MqttEndpoint_GetKeepAliveTime(const GWEN_MSG_ENDPOINT *ep);
AQHOME_API void AQH_MqttEndpoint_SetKeepAliveTime(GWEN_MSG_ENDPOINT *ep, uint16_t i);
AQHOME_API GWEN_MSG *AQH_MqttEndpoint2_CreateMsgConnect(GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API GWEN_MSG *AQH_MqttEndpoint2_CreateMsgPublishString(GWEN_MSG_ENDPOINT2 *ep,
AQHOME_API GWEN_MSG *AQH_MqttEndpoint_CreateMsgConnect(GWEN_MSG_ENDPOINT *ep);
AQHOME_API GWEN_MSG *AQH_MqttEndpoint_CreateMsgPublishString(GWEN_MSG_ENDPOINT *ep,
uint32_t uid,
int valueId,
const char *valuePath,
const char *v);
AQHOME_API GWEN_MSG *AQH_MqttEndpoint2_CreateMsgPublishDouble(GWEN_MSG_ENDPOINT2 *ep,
AQHOME_API GWEN_MSG *AQH_MqttEndpoint_CreateMsgPublishDouble(GWEN_MSG_ENDPOINT *ep,
uint32_t uid,
int valueId,
const char *valuePath,
double v);
AQHOME_API GWEN_MSG *AQH_MqttEndpoint2_CreateMsgPublishInt(GWEN_MSG_ENDPOINT2 *ep,
AQHOME_API GWEN_MSG *AQH_MqttEndpoint_CreateMsgPublishInt(GWEN_MSG_ENDPOINT *ep,
uint32_t uid,
int valueId,
const char *valuePath,

View File

@@ -6,16 +6,16 @@
* should have received along with this file.
****************************************************************************/
#ifndef AQH_ENDPOINT2_MQTT_P_H
#define AQH_ENDPOINT2_MQTT_P_H
#ifndef AQH_ENDPOINT_MQTT_P_H
#define AQH_ENDPOINT_MQTT_P_H
#include "aqhome/mqtt/endpoint2_mqtt.h"
#include "aqhome/mqtt/endpoint_mqtt.h"
typedef struct AQH_ENDPOINT2_MQTT AQH_ENDPOINT2_MQTT;
struct AQH_ENDPOINT2_MQTT {
typedef struct AQH_ENDPOINT_MQTT AQH_ENDPOINT_MQTT;
struct AQH_ENDPOINT_MQTT {
char *clientId;
char *topicPrefix;
uint16_t lastPacketId;

View File

@@ -0,0 +1,392 @@
/****************************************************************************
* This file is part of the project Gwenhywfar.
* Gwenhywfar (c) by 2023 Martin Preuss, all rights reserved.
*
* The license for this file can be found in the file COPYING which you
* should have received along with this file.
****************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "aqhome/mqtt/endpoint_mqttc.h"
#include "aqhome/mqtt/endpoint_mqtt.h"
#include "aqhome/mqtt/msg_mqtt_connect.h"
#include "aqhome/mqtt/msg_mqtt_connack.h"
#include "aqhome/mqtt/msg_mqtt_publish.h"
#include <gwenhywfar/endpoint_tcpc.h>
#include <gwenhywfar/endpoint_msgio.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/inherit.h>
#define AQH_ENDPOINT2_MQTT_NAME "mqtt-client"
#define AQH_ENDPOINT2_MQTTC_RECONNECT_TIME 5
#define AQH_ENDPOINT2_MQTTC_CONNECT_TIMEOUT 10
/* ------------------------------------------------------------------------------------------------
* forward declarations
* ------------------------------------------------------------------------------------------------
*/
static void _addSockets(GWEN_MSG_ENDPOINT *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_UNUSED GWEN_SOCKETSET *xSet);
static void _checkSockets(GWEN_MSG_ENDPOINT *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static int _startConnect(GWEN_MSG_ENDPOINT *ep);
static void _moveMessagesBetweenLists(GWEN_MSG_LIST *srcList, GWEN_MSG_LIST *dstList);
static void _addSocketsWhenUnconnected(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static void _addSocketsWhenConnecting(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static void _addSocketsWhenConnected(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static void _checkSocketsWhenConnecting(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
static void _checkSocketsWhenConnected(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
/* ------------------------------------------------------------------------------------------------
* implementations
* ------------------------------------------------------------------------------------------------
*/
GWEN_MSG_ENDPOINT *AQH_MqttClientEndpoint_new(const char *clientId,
const char *host, int port,
const char *name, int groupId)
{
GWEN_MSG_ENDPOINT *ep;
GWEN_MSG_ENDPOINT *epChild;
ep=GWEN_MsgEndpoint_new(name?name:AQH_ENDPOINT2_MQTT_NAME, groupId);
GWEN_MsgEndpoint_SetAddSocketsFn(ep, _addSockets);
GWEN_MsgEndpoint_SetCheckSocketsFn(ep, _checkSockets);
epChild=GWEN_TcpcEndpoint_new(host, port, NULL, groupId);
GWEN_MsgIoEndpoint_Extend(epChild);
AQH_MqttEndpoint_Extend(epChild);
AQH_MqttEndpoint_SetClientId(epChild, clientId);
GWEN_MsgEndpoint_Tree2_AddChild(ep, epChild);
return ep;
}
int AQH_MqttClientEndpoint_StartConnect(GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
int rv;
/* connect, set state */
rv=_startConnect(ep);
if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) {
DBG_INFO(AQH_LOGDOMAIN, "Endpoint %s: Error connecting (%d)", GWEN_MsgEndpoint_GetName(ep), rv);
GWEN_MsgEndpoint_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTING);
}
else {
DBG_INFO(AQH_LOGDOMAIN, "Endpoint %s: Connecting.", GWEN_MsgEndpoint_GetName(ep));
GWEN_MsgEndpoint_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTING);
}
return rv;
}
else {
DBG_ERROR(AQH_LOGDOMAIN, "Endpoint %s: Not unconnected", GWEN_MsgEndpoint_GetName(ep));
}
}
else {
DBG_ERROR(GWEN_LOGDOMAIN, "No endpoint");
}
return GWEN_ERROR_GENERIC;
}
uint16_t AQH_MqttClientEndpoint_GetKeepAliveTime(const GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
GWEN_MSG_ENDPOINT *epChild;
epChild=GWEN_MsgEndpoint_Tree2_GetFirstChild(ep);
if (epChild)
return AQH_MqttEndpoint_GetKeepAliveTime(epChild);
}
return 0;
}
void AQH_MqttClientEndpoint_SetKeepAliveTime(GWEN_MSG_ENDPOINT *ep, uint16_t i)
{
if (ep) {
GWEN_MSG_ENDPOINT *epChild;
epChild=GWEN_MsgEndpoint_Tree2_GetFirstChild(ep);
if (epChild) {
AQH_MqttEndpoint_SetKeepAliveTime(epChild, i);
}
}
}
uint16_t AQH_MqttClientEndpoint_GetNextPacketId(const GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
GWEN_MSG_ENDPOINT *epChild;
epChild=GWEN_MsgEndpoint_Tree2_GetFirstChild(ep);
if (epChild) {
return AQH_MqttEndpoint_GetNextPacketId(epChild);
}
}
return 0;
}
const char *AQH_MqttClientEndpoint_GetTopicPrefix(const GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
GWEN_MSG_ENDPOINT *epChild;
epChild=GWEN_MsgEndpoint_Tree2_GetFirstChild(ep);
if (epChild)
return AQH_MqttEndpoint_GetTopicPrefix(epChild);
}
return NULL;
}
void AQH_MqttClientEndpoint_SetTopicPrefix(GWEN_MSG_ENDPOINT *ep, const char *s)
{
if (ep) {
GWEN_MSG_ENDPOINT *epChild;
epChild=GWEN_MsgEndpoint_Tree2_GetFirstChild(ep);
if (epChild)
AQH_MqttEndpoint_SetTopicPrefix(epChild, s);
}
}
void _addSockets(GWEN_MSG_ENDPOINT *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
if (ep) {
GWEN_MSG_ENDPOINT *epChild;
epChild=GWEN_MsgEndpoint_Tree2_GetFirstChild(ep);
if (epChild) {
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED)
_addSocketsWhenUnconnected(ep, epChild, readSet, writeSet, xSet);
else {
if (GWEN_MsgEndpoint_GetState(epChild)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_ERROR(AQH_LOGDOMAIN, "Error on tcp layer, disconnecting");
GWEN_MsgEndpoint_Disconnect(epChild);
GWEN_MsgEndpoint_Disconnect(ep);
}
else {
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTING)
_addSocketsWhenConnecting(ep, epChild, readSet, writeSet, xSet);
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTED)
_addSocketsWhenConnected(ep, epChild, readSet, writeSet, xSet);
}
}
} /* if (epChild) */
} /* if (ep) */
}
void _checkSockets(GWEN_MSG_ENDPOINT *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
DBG_DEBUG(AQH_LOGDOMAIN, "Checking sockets in state %d", GWEN_MsgEndpoint_GetState(ep));
if (ep) {
GWEN_MSG_ENDPOINT *epChild;
epChild=GWEN_MsgEndpoint_Tree2_GetFirstChild(ep);
if (epChild) {
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
/* nothing to do here */
} /* if GWEN_MSG_ENDPOINT_STATE_UNCONNECTED */
else {
if (GWEN_MsgEndpoint_GetState(epChild)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_ERROR(AQH_LOGDOMAIN, "Error on tcp layer, disconnecting");
GWEN_MsgEndpoint_Disconnect(epChild);
GWEN_MsgEndpoint_Disconnect(ep);
}
else {
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTING)
_checkSocketsWhenConnecting(ep, epChild, readSet, writeSet, xSet);
else if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTED)
_checkSocketsWhenConnected(ep, epChild, readSet, writeSet, xSet);
}
}
}
}
}
void _addSocketsWhenUnconnected(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
time_t now;
now=time(NULL);
if ((now-GWEN_MsgEndpoint_GetTimeOfLastStateChange(ep))>=AQH_ENDPOINT2_MQTTC_RECONNECT_TIME) {
int rv;
/* (re)connect, set state */
DBG_INFO(AQH_LOGDOMAIN, "Starting to (re-)connect");
rv=AQH_MqttClientEndpoint_StartConnect(ep);
if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) {
DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
}
}
}
void _addSocketsWhenConnecting(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
time_t now;
now=time(NULL);
if ((now-GWEN_MsgEndpoint_GetTimeOfLastStateChange(ep))>=AQH_ENDPOINT2_MQTTC_CONNECT_TIMEOUT ||
GWEN_MsgEndpoint_GetState(epChild)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_ERROR(AQH_LOGDOMAIN, "Timeout on connect");
GWEN_MsgEndpoint_Disconnect(epChild);
GWEN_MsgEndpoint_Disconnect(ep);
}
else
GWEN_MsgEndpoint_AddSockets(epChild, readSet, writeSet, xSet);
}
void _addSocketsWhenConnected(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
if (GWEN_MsgEndpoint_GetState(epChild)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
DBG_ERROR(AQH_LOGDOMAIN, "Error on tcp layer, disconnecting");
GWEN_MsgEndpoint_Disconnect(epChild);
GWEN_MsgEndpoint_Disconnect(ep);
}
else {
/* move to-send messages to child */
_moveMessagesBetweenLists(GWEN_MsgEndpoint_GetSendMessageList(ep), GWEN_MsgEndpoint_GetSendMessageList(epChild));
GWEN_MsgEndpoint_AddSockets(epChild, readSet, writeSet, xSet);
}
}
void _checkSocketsWhenConnecting(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
GWEN_MSG *msg;
GWEN_MsgEndpoint_CheckSockets(epChild, readSet, writeSet, xSet); /* let base layer work */
msg=GWEN_MsgEndpoint_GetFirstReceivedMessage(epChild);
while(msg) {
GWEN_MSG *msgNext;
uint8_t msgType;
msgNext=GWEN_Msg_List_Next(msg);
msgType=AQH_MqttMsg_GetMsgTypeAndFlags(msg) & 0xf0;
if (msgType==AQH_MQTTMSG_MSGTYPE_CONNACK) {
int code;
GWEN_Msg_List_Del(msg); /* remove from list */
code=AQH_ConnAckMqttMsg_GetResultCode(msg);
if (code==AQH_MQTTMSG_CONNACK_RESULT_ACCEPTED) {
DBG_INFO(AQH_LOGDOMAIN, "Positive CONNACK response, connected");
GWEN_MsgEndpoint_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTED);
}
else {
DBG_ERROR(AQH_LOGDOMAIN, "Negative CONNACK response (%d)", code);
GWEN_MsgEndpoint_Disconnect(epChild);
GWEN_MsgEndpoint_Disconnect(ep);
}
GWEN_Msg_free(msg);
break;
}
else {
DBG_ERROR(AQH_LOGDOMAIN, "Ignoring response (%d)", msgType);
}
msg=msgNext;
} /* while */
}
void _checkSocketsWhenConnected(GWEN_MSG_ENDPOINT *ep, GWEN_MSG_ENDPOINT *epChild,
GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet)
{
_moveMessagesBetweenLists(GWEN_MsgEndpoint_GetSendMessageList(ep), GWEN_MsgEndpoint_GetSendMessageList(epChild));
GWEN_MsgEndpoint_CheckSockets(epChild, readSet, writeSet, xSet);
_moveMessagesBetweenLists(GWEN_MsgEndpoint_GetReceivedMessageList(epChild), GWEN_MsgEndpoint_GetReceivedMessageList(ep));
}
int _startConnect(GWEN_MSG_ENDPOINT *ep)
{
GWEN_MSG_ENDPOINT *epChild;
epChild=GWEN_MsgEndpoint_Tree2_GetFirstChild(ep);
if (epChild) {
int rv;
GWEN_MSG *msg;
rv=GWEN_TcpcEndpoint_StartConnect(epChild);
if (rv<0 && rv!=GWEN_ERROR_IN_PROGRESS) {
DBG_INFO(AQH_LOGDOMAIN, "Error starting to connect child layer (%d)", rv);
return rv;
}
msg=AQH_MqttEndpoint_CreateMsgConnect(epChild);
if (msg) {
GWEN_MsgEndpoint_AddSendMessage(epChild, msg);
GWEN_MsgEndpoint_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTING);
return rv; /* result from GWEN_TcpcEndpoint_StartConnect() above */
}
}
return GWEN_ERROR_GENERIC;
}
void _moveMessagesBetweenLists(GWEN_MSG_LIST *srcList, GWEN_MSG_LIST *dstList)
{
GWEN_MSG *msg;
while( (msg=GWEN_Msg_List_First(srcList)) ) {
GWEN_Msg_List_Del(msg);
GWEN_Msg_List_Add(msg, dstList);
}
}

View File

@@ -12,7 +12,7 @@
#include <aqhome/api.h>
#include <gwenhywfar/endpoint2.h>
#include <gwenhywfar/endpoint.h>
#ifdef __cplusplus
@@ -22,20 +22,20 @@ extern "C" {
/**
*/
AQHOME_API GWEN_MSG_ENDPOINT2 *AQH_MqttClientEndpoint2_new(const char *clientId,
AQHOME_API GWEN_MSG_ENDPOINT *AQH_MqttClientEndpoint_new(const char *clientId,
const char *host, int port,
const char *name, int groupId);
AQHOME_API uint16_t AQH_MqttClientEndpoint2_GetKeepAliveTime(const GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API void AQH_MqttClientEndpoint2_SetKeepAliveTime(GWEN_MSG_ENDPOINT2 *ep, uint16_t i);
AQHOME_API uint16_t AQH_MqttClientEndpoint_GetKeepAliveTime(const GWEN_MSG_ENDPOINT *ep);
AQHOME_API void AQH_MqttClientEndpoint_SetKeepAliveTime(GWEN_MSG_ENDPOINT *ep, uint16_t i);
AQHOME_API uint16_t AQH_MqttClientEndpoint2_GetNextPacketId(const GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API uint16_t AQH_MqttClientEndpoint_GetNextPacketId(const GWEN_MSG_ENDPOINT *ep);
AQHOME_API const char *AQH_MqttClientEndpoint2_GetTopicPrefix(const GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API void AQH_MqttClientEndpoint2_SetTopicPrefix(GWEN_MSG_ENDPOINT2 *ep, const char *s);
AQHOME_API const char *AQH_MqttClientEndpoint_GetTopicPrefix(const GWEN_MSG_ENDPOINT *ep);
AQHOME_API void AQH_MqttClientEndpoint_SetTopicPrefix(GWEN_MSG_ENDPOINT *ep, const char *s);
AQHOME_API int AQH_MqttClientEndpoint2_StartConnect(GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API int AQH_MqttClientEndpoint_StartConnect(GWEN_MSG_ENDPOINT *ep);

View File

@@ -45,7 +45,7 @@
<headers dist="true" install="$(pkgincludedir)/msg" >
endpoint2_tty.h
endpoint_tty.h
msg_node.h
msg_ping.h
msg_pong.h
@@ -70,14 +70,14 @@
<headers dist="true" >
endpoint2_tty_p.h
endpoint_tty_p.h
</headers>
<sources>
$(local/typefiles)
endpoint2_tty.c
endpoint_tty.c
msg_node.c
msg_ping.c
msg_pong.c

View File

@@ -11,14 +11,14 @@
#endif
#include "aqhome/msg/endpoint2_tty_p.h"
#include "aqhome/msg/endpoint_tty_p.h"
#include "aqhome/msg/msg_node.h"
#include <gwenhywfar/inherit.h>
#include <gwenhywfar/debug.h>
#include <gwenhywfar/text.h>
#include <gwenhywfar/endpoint2_msgio.h>
#include <gwenhywfar/endpoint_msgio.h>
#include <fcntl.h>
#include <termios.h>
@@ -28,13 +28,13 @@
#define AQH_MSG_ENDPOINT2_TTY_BAUDRATE B19200
#define AQH_MSG_ENDPOINT2_TTY_BYTE_MICROSECS 520
#define GWEN_ENDPOINT2_TTY_RECONNECT_TIME 10
#define AQH_MSG_ENDPOINT_TTY_BAUDRATE B19200
#define AQH_MSG_ENDPOINT_TTY_BYTE_MICROSECS 520
#define GWEN_ENDPOINT_TTY_RECONNECT_TIME 10
GWEN_INHERIT(GWEN_MSG_ENDPOINT2, AQH_MSG_ENDPOINT2_TTY)
GWEN_INHERIT(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY)
@@ -46,20 +46,20 @@ GWEN_INHERIT(GWEN_MSG_ENDPOINT2, AQH_MSG_ENDPOINT2_TTY)
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
/* virtual fn for GWEN_MSG_ENDPOINT2 */
static void _addSockets(GWEN_MSG_ENDPOINT2 *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
/* virtual fn for GWEN_MSG_ENDPOINT */
static void _addSockets(GWEN_MSG_ENDPOINT *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_SOCKETSET *xSet);
/* virtual fns for GWEN_MSG_ENDPOINT2_MSGIO */
static int _getBytesNeededForMessage(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg);
static int _startMsg(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg);
static void _endMsg(GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg);
/* virtual fns for GWEN_MSG_ENDPOINT_MSGIO */
static int _getBytesNeededForMessage(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg);
static int _startMsg(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg);
static void _endMsg(GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg);
/* private fns */
static int _getSocketFd(GWEN_MSG_ENDPOINT2 *ep);
static int _openDevice(GWEN_MSG_ENDPOINT2 *ep);
static int _attnLow(GWEN_MSG_ENDPOINT2 *ep);
static int _attnHigh(GWEN_MSG_ENDPOINT2 *ep);
static int _isAttnLow(GWEN_MSG_ENDPOINT2 *ep);
static int _getSocketFd(GWEN_MSG_ENDPOINT *ep);
static int _openDevice(GWEN_MSG_ENDPOINT *ep);
static int _attnLow(GWEN_MSG_ENDPOINT *ep);
static int _attnHigh(GWEN_MSG_ENDPOINT *ep);
static int _isAttnLow(GWEN_MSG_ENDPOINT *ep);
@@ -69,26 +69,26 @@ static int _isAttnLow(GWEN_MSG_ENDPOINT2 *ep);
*/
GWEN_MSG_ENDPOINT2 *AQH_TtyEndpoint2_new(const char *devicePath, int groupId)
GWEN_MSG_ENDPOINT *AQH_TtyEndpoint_new(const char *devicePath, int groupId)
{
GWEN_MSG_ENDPOINT2 *ep;
AQH_MSG_ENDPOINT2_TTY *xep;
GWEN_MSG_ENDPOINT *ep;
AQH_MSG_ENDPOINT_TTY *xep;
ep=GWEN_MsgEndpoint2_new(AQH_MSG_ENDPOINT2_TTY_NAME, groupId);
GWEN_MsgEndpoint2_SetDefaultMessageSize(ep, AQH_MAXMSGSIZE);
ep=GWEN_MsgEndpoint_new(AQH_MSG_ENDPOINT_TTY_NAME, groupId);
GWEN_MsgEndpoint_SetDefaultMessageSize(ep, AQH_MAXMSGSIZE);
GWEN_NEW_OBJECT(AQH_MSG_ENDPOINT2_TTY, xep);
GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT2, AQH_MSG_ENDPOINT2_TTY, ep, xep, _freeData);
GWEN_NEW_OBJECT(AQH_MSG_ENDPOINT_TTY, xep);
GWEN_INHERIT_SETDATA(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY, ep, xep, _freeData);
xep->deviceName=strdup(devicePath);
GWEN_MsgEndpoint2_SetAddSocketsFn(ep, _addSockets);
GWEN_MsgEndpoint_SetAddSocketsFn(ep, _addSockets);
/* extend with msg handling code */
GWEN_MsgIoEndpoint2_Extend(ep);
GWEN_MsgIoEndpoint2_SetGetNeededBytesFn(ep, _getBytesNeededForMessage);
GWEN_MsgIoEndpoint2_SetSendMsgStartFn(ep, _startMsg);
GWEN_MsgIoEndpoint2_SetSendMsgFinishFn(ep, _endMsg);
GWEN_MsgIoEndpoint_Extend(ep);
GWEN_MsgIoEndpoint_SetGetNeededBytesFn(ep, _getBytesNeededForMessage);
GWEN_MsgIoEndpoint_SetSendMsgStartFn(ep, _startMsg);
GWEN_MsgIoEndpoint_SetSendMsgFinishFn(ep, _endMsg);
return ep;
}
@@ -97,48 +97,48 @@ GWEN_MSG_ENDPOINT2 *AQH_TtyEndpoint2_new(const char *devicePath, int groupId)
void _freeData(void *bp, void *p)
{
AQH_MSG_ENDPOINT2_TTY *xep;
AQH_MSG_ENDPOINT_TTY *xep;
xep=(AQH_MSG_ENDPOINT2_TTY*) p;
xep=(AQH_MSG_ENDPOINT_TTY*) p;
free(xep->deviceName);
GWEN_FREE_OBJECT(xep);
}
void _addSockets(GWEN_MSG_ENDPOINT2 *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_UNUSED GWEN_SOCKETSET *xSet)
void _addSockets(GWEN_MSG_ENDPOINT *ep, GWEN_SOCKETSET *readSet, GWEN_SOCKETSET *writeSet, GWEN_UNUSED GWEN_SOCKETSET *xSet)
{
if (ep) {
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
time_t now;
now=time(NULL);
if ((now-GWEN_MsgEndpoint2_GetTimeOfLastStateChange(ep))>=GWEN_ENDPOINT2_TTY_RECONNECT_TIME) {
if ((now-GWEN_MsgEndpoint_GetTimeOfLastStateChange(ep))>=GWEN_ENDPOINT_TTY_RECONNECT_TIME) {
int rv;
/* (re)connect, set state */
DBG_INFO(AQH_LOGDOMAIN, "Starting to (re-)connect");
rv=GWEN_TtyEndpoint2_Connect(ep);
rv=AQH_TtyEndpoint_Connect(ep);
if (rv<0) {
DBG_INFO(GWEN_LOGDOMAIN, "here (%d)", rv);
}
}
}
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
GWEN_SocketSet_AddSocket(readSet, GWEN_MsgEndpoint2_GetSocket(ep));
if (GWEN_MsgEndpoint2_HaveMessageToSend(ep))
GWEN_SocketSet_AddSocket(writeSet, GWEN_MsgEndpoint2_GetSocket(ep));
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_CONNECTED) {
GWEN_SocketSet_AddSocket(readSet, GWEN_MsgEndpoint_GetSocket(ep));
if (GWEN_MsgEndpoint_HaveMessageToSend(ep))
GWEN_SocketSet_AddSocket(writeSet, GWEN_MsgEndpoint_GetSocket(ep));
}
} /* if (ep) */
}
int GWEN_TtyEndpoint2_Connect(GWEN_MSG_ENDPOINT2 *ep)
int AQH_TtyEndpoint_Connect(GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
if (GWEN_MsgEndpoint2_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
if (GWEN_MsgEndpoint_GetState(ep)==GWEN_MSG_ENDPOINT_STATE_UNCONNECTED) {
int fd;
DBG_INFO(AQH_LOGDOMAIN, "Connecting TTY device");
@@ -151,15 +151,15 @@ int GWEN_TtyEndpoint2_Connect(GWEN_MSG_ENDPOINT2 *ep)
GWEN_SOCKET *sk;
sk=GWEN_Socket_fromFile(fd);
GWEN_MsgEndpoint2_SetSocket(ep, sk);
GWEN_MsgEndpoint2_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTED);
GWEN_MsgEndpoint2_DiscardInput(ep);
GWEN_MsgEndpoint_SetSocket(ep, sk);
GWEN_MsgEndpoint_SetState(ep, GWEN_MSG_ENDPOINT_STATE_CONNECTED);
GWEN_MsgEndpoint_DiscardInput(ep);
_attnHigh(ep);
return 0;
}
}
else {
DBG_ERROR(AQH_LOGDOMAIN, "Endpoint %s: Not unconnected", GWEN_MsgEndpoint2_GetName(ep));
DBG_ERROR(AQH_LOGDOMAIN, "Endpoint %s: Not unconnected", GWEN_MsgEndpoint_GetName(ep));
return GWEN_ERROR_INVALID;
}
}
@@ -168,12 +168,12 @@ int GWEN_TtyEndpoint2_Connect(GWEN_MSG_ENDPOINT2 *ep)
int _getSocketFd(GWEN_MSG_ENDPOINT2 *ep)
int _getSocketFd(GWEN_MSG_ENDPOINT *ep)
{
if (ep) {
GWEN_SOCKET *sk;
sk=GWEN_MsgEndpoint2_GetSocket(ep);
sk=GWEN_MsgEndpoint_GetSocket(ep);
if (sk) {
return GWEN_Socket_GetSocketInt(sk);
}
@@ -183,14 +183,14 @@ int _getSocketFd(GWEN_MSG_ENDPOINT2 *ep)
int _openDevice(GWEN_MSG_ENDPOINT2 *ep)
int _openDevice(GWEN_MSG_ENDPOINT *ep)
{
AQH_MSG_ENDPOINT2_TTY *xep;
AQH_MSG_ENDPOINT_TTY *xep;
int fd;
struct termios options;
int rv;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_MSG_ENDPOINT2_TTY, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY, ep);
assert(xep);
DBG_INFO(AQH_LOGDOMAIN, "Opening device %s", xep->deviceName);
fd=open(xep->deviceName, O_NOCTTY | O_NDELAY | O_RDWR);
@@ -214,12 +214,12 @@ int _openDevice(GWEN_MSG_ENDPOINT2 *ep)
options.c_cc[VTIME]=0; /* read timeout in deciseconds */
options.c_cc[VMIN]=0; /* no minimum number of receive bytes */
rv=cfsetispeed(&options, AQH_MSG_ENDPOINT2_TTY_BAUDRATE);
rv=cfsetispeed(&options, AQH_MSG_ENDPOINT_TTY_BAUDRATE);
if (rv<0) {
DBG_ERROR(AQH_LOGDOMAIN, "Error on cfsetispeed(%s): %s (%d)", xep->deviceName, strerror(errno), errno);
return GWEN_ERROR_IO;
}
rv=cfsetospeed(&options, AQH_MSG_ENDPOINT2_TTY_BAUDRATE);
rv=cfsetospeed(&options, AQH_MSG_ENDPOINT_TTY_BAUDRATE);
if (rv<0) {
DBG_ERROR(AQH_LOGDOMAIN, "Error on cfsetospeed(%s): %s (%d)", xep->deviceName, strerror(errno), errno);
return GWEN_ERROR_IO;
@@ -242,12 +242,12 @@ int _openDevice(GWEN_MSG_ENDPOINT2 *ep)
int _startMsg(GWEN_MSG_ENDPOINT2 *ep, GWEN_UNUSED GWEN_MSG *msg)
int _startMsg(GWEN_MSG_ENDPOINT *ep, GWEN_UNUSED GWEN_MSG *msg)
{
if (ep) {
AQH_MSG_ENDPOINT2_TTY *xep;
AQH_MSG_ENDPOINT_TTY *xep;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_MSG_ENDPOINT2_TTY, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY, ep);
assert(xep);
if (xep->intendedAttnState==1) {
int rv;
@@ -259,7 +259,7 @@ int _startMsg(GWEN_MSG_ENDPOINT2 *ep, GWEN_UNUSED GWEN_MSG *msg)
}
if (rv>0) {
DBG_INFO(AQH_LOGDOMAIN, "Line busy");
usleep(AQH_MSG_ENDPOINT2_TTY_BYTE_MICROSECS);
usleep(AQH_MSG_ENDPOINT_TTY_BYTE_MICROSECS);
return GWEN_ERROR_TIMEOUT;
}
@@ -268,7 +268,7 @@ int _startMsg(GWEN_MSG_ENDPOINT2 *ep, GWEN_UNUSED GWEN_MSG *msg)
DBG_ERROR(AQH_LOGDOMAIN, "here (%d)", rv);
return rv;
}
usleep(AQH_MSG_ENDPOINT2_TTY_BYTE_MICROSECS/5);
usleep(AQH_MSG_ENDPOINT_TTY_BYTE_MICROSECS/5);
}
}
return 0;
@@ -276,7 +276,7 @@ int _startMsg(GWEN_MSG_ENDPOINT2 *ep, GWEN_UNUSED GWEN_MSG *msg)
void _endMsg(GWEN_MSG_ENDPOINT2 *ep, GWEN_UNUSED GWEN_MSG *msg)
void _endMsg(GWEN_MSG_ENDPOINT *ep, GWEN_UNUSED GWEN_MSG *msg)
{
/* TODO: flush before releasing ATTN */
_attnHigh(ep);
@@ -284,7 +284,7 @@ void _endMsg(GWEN_MSG_ENDPOINT2 *ep, GWEN_UNUSED GWEN_MSG *msg)
int _getBytesNeededForMessage(GWEN_UNUSED GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg)
int _getBytesNeededForMessage(GWEN_UNUSED GWEN_MSG_ENDPOINT *ep, GWEN_MSG *msg)
{
uint32_t bytesInMsg;
@@ -309,14 +309,14 @@ int _getBytesNeededForMessage(GWEN_UNUSED GWEN_MSG_ENDPOINT2 *ep, GWEN_MSG *msg)
int _attnLow(GWEN_MSG_ENDPOINT2 *ep)
int _attnLow(GWEN_MSG_ENDPOINT *ep)
{
AQH_MSG_ENDPOINT2_TTY *xep;
AQH_MSG_ENDPOINT_TTY *xep;
int status;
int rv;
int fd;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_MSG_ENDPOINT2_TTY, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY, ep);
assert(xep);
fd=_getSocketFd(ep);
if (fd<0) {
@@ -340,14 +340,14 @@ int _attnLow(GWEN_MSG_ENDPOINT2 *ep)
int _attnHigh(GWEN_MSG_ENDPOINT2 *ep)
int _attnHigh(GWEN_MSG_ENDPOINT *ep)
{
AQH_MSG_ENDPOINT2_TTY *xep;
AQH_MSG_ENDPOINT_TTY *xep;
int status;
int rv;
int fd;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_MSG_ENDPOINT2_TTY, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY, ep);
assert(xep);
fd=_getSocketFd(ep);
if (fd<0) {
@@ -373,14 +373,14 @@ int _attnHigh(GWEN_MSG_ENDPOINT2 *ep)
int _isAttnLow(GWEN_MSG_ENDPOINT2 *ep)
int _isAttnLow(GWEN_MSG_ENDPOINT *ep)
{
AQH_MSG_ENDPOINT2_TTY *xep;
AQH_MSG_ENDPOINT_TTY *xep;
int status;
int rv;
int fd;
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT2, AQH_MSG_ENDPOINT2_TTY, ep);
xep=GWEN_INHERIT_GETDATA(GWEN_MSG_ENDPOINT, AQH_MSG_ENDPOINT_TTY, ep);
assert(xep);
fd=_getSocketFd(ep);
if (fd<0) {

View File

@@ -6,21 +6,21 @@
* should have received along with this file.
****************************************************************************/
#ifndef AQH_MSGENDPOINT2_TTY_H
#define AQH_MSGENDPOINT2_TTY_H
#ifndef AQH_MSGENDPOINT_TTY_H
#define AQH_MSGENDPOINT_TTY_H
#include <aqhome/api.h>
#include <gwenhywfar/endpoint2.h>
#include <gwenhywfar/endpoint.h>
#define AQH_MSG_ENDPOINT2_TTY_NAME "tty"
#define AQH_MSG_ENDPOINT_TTY_NAME "tty"
AQHOME_API GWEN_MSG_ENDPOINT2 *AQH_TtyEndpoint2_new(const char *devicePath, int groupId);
AQHOME_API GWEN_MSG_ENDPOINT *AQH_TtyEndpoint_new(const char *devicePath, int groupId);
AQHOME_API int GWEN_TtyEndpoint2_Connect(GWEN_MSG_ENDPOINT2 *ep);
AQHOME_API int AQH_TtyEndpoint_Connect(GWEN_MSG_ENDPOINT *ep);
#endif

View File

@@ -6,22 +6,22 @@
* should have received along with this file.
****************************************************************************/
#ifndef AQH_MSGENDPOINT2_TTY_P_H
#define AQH_MSGENDPOINT2_TTY_P_H
#ifndef AQH_MSGENDPOINT_TTY_P_H
#define AQH_MSGENDPOINT_TTY_P_H
#include <aqhome/api.h>
#include "aqhome/msg/endpoint2_tty.h"
#include "aqhome/msg/endpoint_tty.h"
#include <termios.h>
typedef struct AQH_MSG_ENDPOINT2_TTY AQH_MSG_ENDPOINT2_TTY;
struct AQH_MSG_ENDPOINT2_TTY {
typedef struct AQH_MSG_ENDPOINT_TTY AQH_MSG_ENDPOINT_TTY;
struct AQH_MSG_ENDPOINT_TTY {
char *deviceName;
struct termios previousOptions;
int intendedAttnState;