Started adding an event layer.

This commit is contained in:
Martin Preuss
2023-10-12 00:35:08 +02:00
parent 077b367299
commit 16e08d623d
11 changed files with 761 additions and 0 deletions

View File

@@ -69,6 +69,7 @@
mqtt mqtt
hexfile hexfile
data data
events
</subdirs> </subdirs>
@@ -79,6 +80,7 @@
aqhmqtt aqhmqtt
aqhhexfile aqhhexfile
aqhdata aqhdata
aqhevents
</useTargets> </useTargets>
<libraries> <libraries>

87
aqhome/events/0BUILD Normal file
View File

@@ -0,0 +1,87 @@
<?xml?>
<gwbuild>
<target type="ConvenienceLibrary" name="aqhevents" >
<includes type="c" >
$(gwenhywfar_cflags)
$(aqdatabase_cflags)
-I$(topsrcdir)
-I$(topbuilddir)
</includes>
<includes type="tm2" >
--include=$(builddir)
--include=$(srcdir)
--include=$(aqdatabase_AQDATABASE_TYPEMAKERDIR)/c
</includes>
<define name="BUILDING_AQHOME" />
<setVar name="local/cflags">$(visibility_cflags)</setVar>
<setVar name="tm2flags" >
--api=AQHOME_API
</setVar>
<setVar name="local/typefiles" >
</setVar>
<setVar name="local/built_sources" >
</setVar>
<setVar name="local/built_headers_pub">
</setVar>
<setVar name="local/built_headers_priv" >
</setVar>
<headers dist="false" install="$(pkgincludedir)/data" >
$(local/built_headers_pub)
</headers>
<headers dist="true" install="$(pkgincludedir)/data" >
eventsubscription.h
timer.h
eventhandler.h
</headers>
<headers dist="true" >
eventsubscription_p.h
timer_p.h
eventhandler_p.h
</headers>
<sources>
$(local/typefiles)
eventsubscription.c
timer.c
eventhandler.c
</sources>
<extradist>
</extradist>
<useTargets>
</useTargets>
<subdirs>
</subdirs>
</target>
</gwbuild>

View File

@@ -0,0 +1,138 @@
/****************************************************************************
* 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 "./eventhandler_p.h"
#include <gwenhywfar/debug.h>
GWEN_INHERIT_FUNCTIONS(AQH_EVENT_HANDLER)
GWEN_LIST_FUNCTIONS(AQH_EVENT_HANDLER, AQH_EventHandler)
AQH_EVENT_HANDLER *AQH_EventHandler_new()
{
AQH_EVENT_HANDLER *evh;
GWEN_NEW_OBJECT(AQH_EVENT_HANDLER, evh);
GWEN_INHERIT_INIT(AQH_EVENT_HANDLER, evh);
GWEN_LIST_INIT(AQH_EVENT_HANDLER, evh);
evh->timerList=AQH_EventTimer_List_new();
evh->subscriptionList=AQH_EventSubscription_List_new();
return evh;
}
void AQH_EventHandler_free(AQH_EVENT_HANDLER *evh)
{
if (evh) {
GWEN_LIST_FINI(AQH_EVENT_HANDLER, evh);
GWEN_INHERIT_FINI(AQH_EVENT_HANDLER, evh);
AQH_EventSubscription_List_free(evh->subscriptionList);
AQH_EventTimer_List_free(evh->timerList);
GWEN_FREE_OBJECT(evh);
}
}
AQH_EVENT_TIMER_LIST *AQH_EventHandler_GetTimerList(const AQH_EVENT_HANDLER *evh)
{
return evh?evh->timerList:NULL;
}
void AQH_EventHandler_AddTimer(AQH_EVENT_HANDLER *evh, AQH_EVENT_TIMER *evt)
{
if (evh && evt) {
AQH_EventTimer_SetId(evt, ++(evh->lastTimerId));
AQH_EventTimer_List_Add(evt, evh->timerList);
}
}
AQH_EVENT_SUBSCRIPTION_LIST *AQH_EventHandler_GetSubscriptionList(const AQH_EVENT_HANDLER *evh)
{
return evh?evh->subscriptionList:NULL;
}
void AQH_EventHandler_AddSubscription(AQH_EVENT_HANDLER *evh, AQH_EVENT_SUBSCRIPTION *evs)
{
if (evh && evs)
AQH_EventSubscription_List_Add(evs, evh->subscriptionList);
}
void AQH_EventHandler_HandleTimeTick(AQH_EVENT_HANDLER *evh, uint64_t t)
{
if (evh) {
AQH_EVENT_TIMER *evt;
evt=AQH_EventTimer_List_First(evh->timerList);
while(evt) {
AQH_EVENT_TIMER *evtNext;
evtNext=AQH_EventTimer_List_Next(evt);
if (AQH_EventTimer_CheckWhetherTickTriggers(evt, t)) {
AQH_EventHandler_HandleEvent(evh, AQH_EventTimer_GetEventType(evt), AQH_EventTimer_GetId(evt));
if (AQH_EventTimer_GetFlags(evt) & AQH_EVENT_TIMER_FLAGS_DELETE_IF_TRIGGERED) {
DBG_INFO(AQH_LOGDOMAIN, "Deleting timer after trigger");
AQH_EventTimer_List_Del(evt);
AQH_EventTimer_free(evt);
}
}
evt=evtNext;
}
}
}
void AQH_EventHandler_HandleEvent(AQH_EVENT_HANDLER *evh, int eventType, uint64_t objectId)
{
if (evh) {
AQH_EVENT_SUBSCRIPTION *evs;
evs=AQH_EventSubscription_List_First(evh->subscriptionList);
while(evs) {
if (AQH_EventSubscription_GetEventType(evs)==eventType) {
int objectIdFromSubscription;
objectIdFromSubscription=AQH_EventSubscription_GetObjectId(evs);
if (objectIdFromSubscription==0 || objectIdFromSubscription==objectId)
AQH_EventSubscription_CallHandler(evs, eventType, objectId);
}
evs=AQH_EventSubscription_List_Next(evs);
}
}
}

View File

@@ -0,0 +1,46 @@
/****************************************************************************
* 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.
****************************************************************************/
#ifndef AQH_EVENT_HANDLER_H
#define AQH_EVENT_HANDLER_H
#include "aqhome/api.h"
#include <gwenhywfar/inherit.h>
#include <gwenhywfar/list.h>
typedef struct AQH_EVENT_HANDLER AQH_EVENT_HANDLER;
GWEN_INHERIT_FUNCTION_LIB_DEFS(AQH_EVENT_HANDLER, AQHOME_API)
GWEN_LIST_FUNCTION_LIB_DEFS(AQH_EVENT_HANDLER, AQH_EventHandler, AQHOME_API)
#include "aqhome/events/timer.h"
#include "aqhome/events/eventsubscription.h"
AQHOME_API AQH_EVENT_HANDLER *AQH_EventHandler_new();
AQHOME_API void AQH_EventHandler_free(AQH_EVENT_HANDLER *evh);
AQHOME_API AQH_EVENT_TIMER_LIST *AQH_EventHandler_GetTimerList(const AQH_EVENT_HANDLER *evh);
AQHOME_API void AQH_EventHandler_AddTimer(AQH_EVENT_HANDLER *evh, AQH_EVENT_TIMER *evt);
AQHOME_API AQH_EVENT_SUBSCRIPTION_LIST *AQH_EventHandler_GetSubscriptionList(const AQH_EVENT_HANDLER *evh);
AQHOME_API void AQH_EventHandler_AddSubscription(AQH_EVENT_HANDLER *evh, AQH_EVENT_SUBSCRIPTION *evs);
AQHOME_API void AQH_EventHandler_HandleTimeTick(AQH_EVENT_HANDLER *evh, uint64_t t);
AQHOME_API void AQH_EventHandler_HandleEvent(AQH_EVENT_HANDLER *evh, int eventType, uint64_t objectId);
#endif

View File

@@ -0,0 +1,30 @@
/****************************************************************************
* 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.
****************************************************************************/
#ifndef AQH_EVENT_HANDLER_P_H
#define AQH_EVENT_HANDLER_P_H
#include "aqhome/events/eventhandler.h"
struct AQH_EVENT_HANDLER {
GWEN_INHERIT_ELEMENT(AQH_EVENT_HANDLER)
GWEN_LIST_ELEMENT(AQH_EVENT_HANDLER)
uint64_t lastTimerId;
AQH_EVENT_TIMER_LIST *timerList;
AQH_EVENT_SUBSCRIPTION_LIST *subscriptionList;
};
#endif

View File

@@ -0,0 +1,93 @@
/****************************************************************************
* 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 "./eventsubscription_p.h"
#include <gwenhywfar/debug.h>
GWEN_INHERIT_FUNCTIONS(AQH_EVENT_SUBSCRIPTION)
GWEN_LIST_FUNCTIONS(AQH_EVENT_SUBSCRIPTION, AQH_EventSubscription)
AQH_EVENT_SUBSCRIPTION *AQH_EventSubscription_new()
{
AQH_EVENT_SUBSCRIPTION *evs;
GWEN_NEW_OBJECT(AQH_EVENT_SUBSCRIPTION, evs);
GWEN_INHERIT_INIT(AQH_EVENT_SUBSCRIPTION, evs);
GWEN_LIST_INIT(AQH_EVENT_SUBSCRIPTION, evs);
return evs;
}
void AQH_EventSubscription_free(AQH_EVENT_SUBSCRIPTION *evs)
{
if (evs) {
GWEN_LIST_FINI(AQH_EVENT_SUBSCRIPTION, evs);
GWEN_INHERIT_FINI(AQH_EVENT_SUBSCRIPTION, evs);
GWEN_FREE_OBJECT(evs);
}
}
int AQH_EventSubscription_GetEventType(const AQH_EVENT_SUBSCRIPTION *evs)
{
return evs?evs->eventType:0;
}
void AQH_EventSubscription_SetEventType(AQH_EVENT_SUBSCRIPTION *evs, int i)
{
if (evs)
evs->eventType=i;
}
uint64_t AQH_EventSubscription_GetObjectId(const AQH_EVENT_SUBSCRIPTION *evs)
{
return evs?evs->objectId:0;
}
void AQH_EventSubscription_SetObjectId(AQH_EVENT_SUBSCRIPTION *evs, uint64_t i)
{
if (evs)
evs->objectId=i;
}
int AQH_EventSubscription_CallHandler(AQH_EVENT_SUBSCRIPTION *evs, int eventType, uint64_t objectId)
{
if (evs && evs->handlerFn)
return evs->handlerFn(evs, eventType, objectId);
return 0;
}

View File

@@ -0,0 +1,41 @@
/****************************************************************************
* 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.
****************************************************************************/
#ifndef AQH_EVENTSUBSCRIPTION_H
#define AQH_EVENTSUBSCRIPTION_H
#include "aqhome/api.h"
#include <gwenhywfar/inherit.h>
#include <gwenhywfar/list.h>
typedef struct AQH_EVENT_SUBSCRIPTION AQH_EVENT_SUBSCRIPTION;
GWEN_INHERIT_FUNCTION_LIB_DEFS(AQH_EVENT_SUBSCRIPTION, AQHOME_API)
GWEN_LIST_FUNCTION_LIB_DEFS(AQH_EVENT_SUBSCRIPTION, AQH_EventSubscription, AQHOME_API)
typedef int (*AQH_EVENT_SUBSCRIPTION_HANDLER_FN)(AQH_EVENT_SUBSCRIPTION *evs, int eventType, uint64_t objectId);
AQHOME_API AQH_EVENT_SUBSCRIPTION *AQH_EventSubscription_new();
AQHOME_API void AQH_EventSubscription_free(AQH_EVENT_SUBSCRIPTION *evs);
AQHOME_API int AQH_EventSubscription_GetEventType(const AQH_EVENT_SUBSCRIPTION *evs);
AQHOME_API void AQH_EventSubscription_SetEventType(AQH_EVENT_SUBSCRIPTION *evs, int i);
AQHOME_API uint64_t AQH_EventSubscription_GetObjectId(const AQH_EVENT_SUBSCRIPTION *evs);
AQHOME_API void AQH_EventSubscription_SetObjectId(AQH_EVENT_SUBSCRIPTION *evs, uint64_t i);
AQHOME_API int AQH_EventSubscription_CallHandler(AQH_EVENT_SUBSCRIPTION *evs, int eventType, uint64_t objectId);
#endif

View File

@@ -0,0 +1,31 @@
/****************************************************************************
* 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.
****************************************************************************/
#ifndef AQH_EVENTSUBSCRIPTION_P_H
#define AQH_EVENTSUBSCRIPTION_P_H
#include "aqhome/events/eventsubscription.h"
struct AQH_EVENT_SUBSCRIPTION {
GWEN_INHERIT_ELEMENT(AQH_EVENT_SUBSCRIPTION)
GWEN_LIST_ELEMENT(AQH_EVENT_SUBSCRIPTION)
int eventType;
uint64_t objectId;
AQH_EVENT_SUBSCRIPTION_HANDLER_FN handlerFn;
};
#endif

194
aqhome/events/timer.c Normal file
View File

@@ -0,0 +1,194 @@
/****************************************************************************
* 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 "./timer_p.h"
#include <gwenhywfar/debug.h>
GWEN_INHERIT_FUNCTIONS(AQH_EVENT_TIMER)
GWEN_LIST_FUNCTIONS(AQH_EVENT_TIMER, AQH_EventTimer)
AQH_EVENT_TIMER *AQH_EventTimer_new()
{
AQH_EVENT_TIMER *evt;
GWEN_NEW_OBJECT(AQH_EVENT_TIMER, evt);
GWEN_INHERIT_INIT(AQH_EVENT_TIMER, evt);
GWEN_LIST_INIT(AQH_EVENT_TIMER, evt);
return evt;
}
void AQH_EventTimer_free(AQH_EVENT_TIMER *evt)
{
if (evt) {
GWEN_LIST_FINI(AQH_EVENT_TIMER, evt);
GWEN_INHERIT_FINI(AQH_EVENT_TIMER, evt);
GWEN_FREE_OBJECT(evt);
}
}
uint32_t AQH_EventTimer_GetFlags(const AQH_EVENT_TIMER *evt)
{
return evt?evt->flags:0;
}
void AQH_EventTimer_SetFlags(AQH_EVENT_TIMER *evt, uint32_t f)
{
if (evt)
evt->flags=f;
}
void AQH_EventTimer_AddFlags(AQH_EVENT_TIMER *evt, uint32_t f)
{
if (evt)
evt->flags|=f;
}
void AQH_EventTimer_SubFlags(AQH_EVENT_TIMER *evt, uint32_t f)
{
if (evt)
evt->flags&=~f;
}
int AQH_EventTimer_GetTimerType(const AQH_EVENT_TIMER *evt)
{
return evt?evt->timerType:0;
}
void AQH_EventTimer_SetTimerType(AQH_EVENT_TIMER *evt, int i)
{
if (evt)
evt->timerType=i;
}
uint64_t AQH_EventTimer_GetId(const AQH_EVENT_TIMER *evt)
{
return evt?evt->id:0;
}
void AQH_EventTimer_SetId(AQH_EVENT_TIMER *evt, uint64_t i)
{
if (evt)
evt->id=i;
}
uint64_t AQH_EventTimer_GetCurrentValue(const AQH_EVENT_TIMER *evt)
{
return evt?evt->currentValue:0;
}
void AQH_EventTimer_SetCurrentValue(AQH_EVENT_TIMER *evt, uint64_t i)
{
if (evt)
evt->currentValue=i;
}
uint64_t AQH_EventTimer_GetReloadOrAlarmValue(const AQH_EVENT_TIMER *evt)
{
return evt?evt->reloadOrAlarmValue:0;
}
void AQH_EventTimer_SetReloadOrAlarmValue(AQH_EVENT_TIMER *evt, uint64_t i)
{
if (evt)
evt->reloadOrAlarmValue=i;
}
int AQH_EventTimer_GetEventType(const AQH_EVENT_TIMER *evt)
{
return evt?evt->eventType:0;
}
void AQH_EventTimer_SetEventType(AQH_EVENT_TIMER *evt, int i)
{
if (evt)
evt->eventType=i;
}
int AQH_EventTimer_CheckWhetherTickTriggers(AQH_EVENT_TIMER *evt, uint64_t t)
{
if (evt && evt->flags & AQH_EVENT_TIMER_FLAGS_RUNNING) {
if (evt->timerType==AQH_EventTimerType_CountDown) {
if (evt->currentValue>0) {
evt->currentValue--;
if (evt->currentValue==0) {
if (evt->flags & AQH_EVENT_TIMER_FLAGS_RESTART) {
DBG_INFO(AQH_LOGDOMAIN, "Timer %lu expired, restarting", (unsigned long int) (evt->id));
evt->currentValue=evt->reloadOrAlarmValue;
}
else {
DBG_INFO(AQH_LOGDOMAIN, "Timer %lu expired", (unsigned long int) (evt->id));
evt->flags&=~AQH_EVENT_TIMER_FLAGS_RUNNING;
}
return 1;
}
}
}
else if (evt->timerType==AQH_EventTimerType_Alarm) {
if (t>=evt->reloadOrAlarmValue) {
evt->flags&=~AQH_EVENT_TIMER_FLAGS_RUNNING;
return 1;
}
}
else {
DBG_INFO(AQH_LOGDOMAIN, "Unknown timer type %d", evt->timerType);
}
}
return 0;
}

65
aqhome/events/timer.h Normal file
View File

@@ -0,0 +1,65 @@
/****************************************************************************
* 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.
****************************************************************************/
#ifndef AQH_EVENT_TIMER_H
#define AQH_EVENT_TIMER_H
#include "aqhome/api.h"
#include <gwenhywfar/inherit.h>
#include <gwenhywfar/list.h>
enum {
AQH_EventTimerType_CountDown=0,
AQH_EventTimerType_Alarm
} AQH_EVENT_TIMER_TYPE;
#define AQH_EVENT_TIMER_FLAGS_RUNNING 0x0001
#define AQH_EVENT_TIMER_FLAGS_RESTART 0x0002
#define AQH_EVENT_TIMER_FLAGS_DELETE_IF_TRIGGERED 0x0004
typedef struct AQH_EVENT_TIMER AQH_EVENT_TIMER;
GWEN_INHERIT_FUNCTION_LIB_DEFS(AQH_EVENT_TIMER, AQHOME_API)
GWEN_LIST_FUNCTION_LIB_DEFS(AQH_EVENT_TIMER, AQH_EventTimer, AQHOME_API)
AQHOME_API AQH_EVENT_TIMER *AQH_EventTimer_new();
AQHOME_API void AQH_EventTimer_free(AQH_EVENT_TIMER *evt);
AQHOME_API uint32_t AQH_EventTimer_GetFlags(const AQH_EVENT_TIMER *evt);
AQHOME_API void AQH_EventTimer_SetFlags(AQH_EVENT_TIMER *evt, uint32_t f);
AQHOME_API void AQH_EventTimer_AddFlags(AQH_EVENT_TIMER *evt, uint32_t f);
AQHOME_API void AQH_EventTimer_SubFlags(AQH_EVENT_TIMER *evt, uint32_t f);
AQHOME_API int AQH_EventTimer_GetTimerType(const AQH_EVENT_TIMER *evt);
AQHOME_API void AQH_EventTimer_SetTimerType(AQH_EVENT_TIMER *evt, int i);
AQHOME_API uint64_t AQH_EventTimer_GetId(const AQH_EVENT_TIMER *evt);
AQHOME_API void AQH_EventTimer_SetId(AQH_EVENT_TIMER *evt, uint64_t i);
AQHOME_API uint64_t AQH_EventTimer_GetCurrentValue(const AQH_EVENT_TIMER *evt);
AQHOME_API void AQH_EventTimer_SetCurrentValue(AQH_EVENT_TIMER *evt, uint64_t i);
AQHOME_API uint64_t AQH_EventTimer_GetReloadOrAlarmValue(const AQH_EVENT_TIMER *evt);
AQHOME_API void AQH_EventTimer_SetReloadOrAlarmValue(AQH_EVENT_TIMER *evt, uint64_t i);
AQHOME_API int AQH_EventTimer_GetEventType(const AQH_EVENT_TIMER *evt);
AQHOME_API void AQH_EventTimer_SetEventType(AQH_EVENT_TIMER *evt, int i);
AQHOME_API int AQH_EventTimer_CheckWhetherTickTriggers(AQH_EVENT_TIMER *evt, uint64_t t);
#endif

34
aqhome/events/timer_p.h Normal file
View File

@@ -0,0 +1,34 @@
/****************************************************************************
* 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.
****************************************************************************/
#ifndef AQH_EVENT_TIMER_P_H
#define AQH_EVENT_TIMER_P_H
#include "aqhome/events/timer.h"
struct AQH_EVENT_TIMER {
GWEN_INHERIT_ELEMENT(AQH_EVENT_TIMER)
GWEN_LIST_ELEMENT(AQH_EVENT_TIMER)
uint32_t flags;
int timerType;
uint64_t id;
uint64_t currentValue;
uint64_t reloadOrAlarmValue;
int eventType;
};
#endif