I have come to a point in a project where hy current style of managing Even Sources/Listeners with associated data just does not hold due to size/complexity of the system.
It requires that you must have a very good knowledge of the system to use/implement it and I want to make it easier and more generic so it becomes a natural part of the system and not design for each implementation.
How I currently do it is that I write my code in generic modules for my system and if any module has an event then here is a function to get the event source to register to it.
Example:
Code: Select all
EVENTSOURCE_DECL(new_vicon_data_es);
/**
* @brief Initializes the New Vicon Data events.
*/
void vInitializeViconSupport()
{
/* Initialize New Vicon Data event source */
osalEventObjectInit(&new_vicon_data_es);
}
/**
* @brief Broadcast the New Vicon Data events.
*/
void vBroadcastNewViconDataAvailable()
{
}
/**
* @brief Returns the pointer to the New Vicon Data event source.
*
* @return Pointer to the New Vicon Data event source.
*/
event_source_t *ptrGetViconDataEventSource(void)
{
return &new_vicon_data_es;
}
/*
... More code ....
*/This works for my everyday system but the solution requires a lot of manual coding and errors can easily creep in.
So I believe it is time to create a generic way of handling events, so an event source is somehow "registered" to the pool of events and then the user application queries the pool to "subscribe" to events.
The thing is that I am unsure of how I should do this, for example, there is usually data associated to the events so there should be a way to include references to the data and so on.
How would you do this? Are there any good generic ways to do this?
Any ideas and suggestions are welcome!
Also, I am not looking for code - I am very much interested in implementing this for my own learning!
Maybe the usage (for a subscriber) could look like this:
Code: Select all
my_type_t *pointer_to_data;
event_source_t *subscription_es;
void ModuleInit()
{
/* Return true if the event signature exist */
if (!SubscribeToEvent(MY_EVENT_SIGNATURE, subscription_es, pointer_to_data))
{
/* Handle horrible reality... */
}
chThdCreateStatic(waThreadForModule,
sizeof(waThreadForModule),
SOME_PRIO,
ThreadForModule,
NULL);
}
static THD_FUNCTION(ThreadForModule, arg)
{
event_listerner_t *my_el;
chEvtRegisterMask(subscription_es,
&my_el,
MY_EVENTMASK);
while (1)
{
chEvtWaitOne(MY_EVENTMASK);
/* Do work with the data from pointer_to_data */
}
}And registering an event source:
Code: Select all
EVENTSOURCE_DECL(new_data_es);
my_type_t data_holder;
void InitProducerModule()
{
EventSubscription_Register(MY_EVENT_SIGNATURE, &new_data_es, &data_holder);
}
void TerminateProducerModule()
{
EventSubscription_Revoke(&new_data_es);
}
/* Code to produce data and events ..... */Thank you for you time!
Best Regards
Emil