156 lines
4.3 KiB
C
156 lines
4.3 KiB
C
/****************************************************************************
|
|
* 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/msgendpointlog_p.h"
|
|
|
|
#include "aqhome/msg_value.h"
|
|
#include "aqhome/msg_sendstats.h"
|
|
#include "aqhome/msg_ping.h"
|
|
#include "aqhome/msg_pong.h"
|
|
#include "aqhome/msg_needaddr.h"
|
|
#include "aqhome/msg_claimaddr.h"
|
|
#include "aqhome/msg_haveaddr.h"
|
|
#include "aqhome/msg_denyaddr.h"
|
|
|
|
#include <gwenhywfar/list.h>
|
|
#include <gwenhywfar/inherit.h>
|
|
#include <gwenhywfar/debug.h>
|
|
#include <gwenhywfar/gwentime.h>
|
|
|
|
|
|
|
|
GWEN_INHERIT(AQH_MSG_ENDPOINT, AQH_MSG_ENDPOINT_LOG)
|
|
|
|
|
|
static void GWENHYWFAR_CB _freeData(void *bp, void *p);
|
|
static void _run(AQH_MSG_ENDPOINT *ep);
|
|
static void _logMessage(AQH_MSG_ENDPOINT *ep, const AQH_MSG *msg);
|
|
static void _writeToLogFile(const char *filename, const char *txt);
|
|
|
|
|
|
|
|
|
|
|
|
AQH_MSG_ENDPOINT *AQH_MsgEndpointLog_new(const char *filename)
|
|
{
|
|
int fd;
|
|
AQH_MSG_ENDPOINT *ep;
|
|
AQH_MSG_ENDPOINT_LOG *xep;
|
|
|
|
ep=AQH_MsgEndpoint_new(-1, AQH_MSG_ENDPOINT_ENDPOINTGROUP_NET, "LOG");
|
|
GWEN_NEW_OBJECT(AQH_MSG_ENDPOINT_LOG, xep);
|
|
xep->filename=strdup(filename);
|
|
GWEN_INHERIT_SETDATA(AQH_MSG_ENDPOINT, AQH_MSG_ENDPOINT_LOG, ep, xep, _freeData);
|
|
AQH_MsgEndpoint_SetAcceptedEndpointGroups(ep, AQH_MSG_ENDPOINT_ENDPOINTGROUP_BUS);
|
|
AQH_MsgEndpoint_SetAcceptedMsgGroups(ep, AQH_MSG_ENDPOINT_MSGGROUP_ALL);
|
|
AQH_MsgEndpoint_AddFlags(ep, AQH_MSG_ENDPOINT_FLAGS_NOIO);
|
|
AQH_MsgEndpoint_SetRunFn(ep, _run);
|
|
|
|
return ep;
|
|
}
|
|
|
|
|
|
|
|
void _freeData(void *bp, void *p)
|
|
{
|
|
AQH_MSG_ENDPOINT_LOG *xep;
|
|
|
|
xep=(AQH_MSG_ENDPOINT_LOG*) p;
|
|
free(xep->filename);
|
|
GWEN_FREE_OBJECT(xep);
|
|
}
|
|
|
|
|
|
|
|
void _run(AQH_MSG_ENDPOINT *ep)
|
|
{
|
|
AQH_MSG_LIST *msgList;
|
|
|
|
msgList=AQH_MsgEndpoint_GetSendMessageList(ep);
|
|
if (msgList && AQH_Msg_List_GetCount(msgList)) {
|
|
AQH_MSG *msg;
|
|
|
|
msg=AQH_Msg_List_First(msgList);
|
|
while(msg) {
|
|
AQH_MSG *next;
|
|
|
|
next=AQH_Msg_List_Next(msg);
|
|
_logMessage(ep, msg);
|
|
AQH_Msg_free(msg);
|
|
msg=next;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
void _logMessage(AQH_MSG_ENDPOINT *ep, const AQH_MSG *msg)
|
|
{
|
|
AQH_MSG_ENDPOINT_LOG *xep;
|
|
const uint8_t *ptr;
|
|
uint8_t len;
|
|
uint8_t msgType;
|
|
int msgIsValid;
|
|
GWEN_BUFFER *dbuf;
|
|
GWEN_TIME *ti;
|
|
|
|
xep=GWEN_INHERIT_GETDATA(AQH_MSG_ENDPOINT, AQH_MSG_ENDPOINT_LOG, ep);
|
|
dbuf=GWEN_Buffer_new(0, 256, 0, 1);
|
|
ti=GWEN_CurrentTime();
|
|
GWEN_Time_toString(ti, "YYYY-MM-DD hh:mm:ss ", dbuf);
|
|
GWEN_Time_free(ti);
|
|
ti=NULL;
|
|
|
|
msgIsValid=(AQH_Msg_IsChecksumValid(msg) && AQH_Msg_IsMsgComplete(msg));
|
|
ptr=AQH_Msg_GetConstBuffer(msg);
|
|
len=AQH_Msg_GetBytesInBuffer(msg);
|
|
msgType=AQH_Msg_GetMsgType(msg);
|
|
|
|
switch(msgType) {
|
|
case AQH_MSG_TYPE_PING: AQH_MsgPing_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_PONG: AQH_MsgPong_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_COMSENDSTATS: AQH_MsgSendStats_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_TWIBUSMEMBER: AQH_Msg_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_DEBUG: AQH_Msg_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_VALUE: AQH_MsgValue_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_NEED_ADDRESS: AQH_MsgNeedAddr_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_CLAIM_ADDRESS: AQH_MsgClaimAddr_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_HAVE_ADDRESS: AQH_MsgHaveAddr_DumpToBuffer(msg, dbuf, "received"); break;
|
|
case AQH_MSG_TYPE_DENY_ADDRESS: AQH_MsgDenyAddr_DumpToBuffer(msg, dbuf, "received"); break;
|
|
default: AQH_MsgValue_DumpToBuffer(msg, dbuf, "received"); break;
|
|
}
|
|
_writeToLogFile(xep->filename, GWEN_Buffer_GetStart(dbuf));
|
|
GWEN_Buffer_free(dbuf);
|
|
}
|
|
|
|
|
|
|
|
void _writeToLogFile(const char *filename, const char *txt)
|
|
{
|
|
if (txt && *txt) {
|
|
FILE *f;
|
|
|
|
f=fopen(filename, "a+");
|
|
if (f) {
|
|
if (1!=fwrite(txt, strlen(txt), 1, f)) {
|
|
DBG_ERROR(AQH_LOGDOMAIN, "Error logging.");
|
|
}
|
|
fclose(f);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|