Generalized event subscription system?

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Generalized event subscription system?

Post by Korken »

Hi all user of ChibiOS! I know this is not quite support for ChibiOS (more towards usage), but I hope more than I can have use of this after we discuss this and I implement it. :)

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
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: Generalized event subscription system?

Post by Giovanni »

Hi,

Events are a complex topic, my question is: isn't chEvtRegister() a way to subscribe to an event source?

There are two entities:
1) Event Sources.
2) Threads (through Event Listener structures).

The "register" operation is meant to subscribe a thread on a public source.

If you want to hide the Event Source structures you could just export from each module an xxxSubscribe(&el, ...) function that would just call chEvtRegister() internally with an implicit parameter (the source).

Giovanni
User avatar
Korken
Posts: 270
Joined: Wed Apr 02, 2014 4:09 pm
Has thanked: 5 times
Been thanked: 6 times

Re: Generalized event subscription system?

Post by Korken »

Hi Giovanni,

Yes indeed, in the bottom of everything there will be chEvtRegister(), but there should be a "layer" above this to abstract.
So you should not need to know exactly which function a module has implemented to subscribe to the event source.

I want this to become a generic backbone of my system to facilitate the expansion and have a generic way to synchronize.


I have also had an idea that all listen for messages in a queue. And if the ID of the message in a queue equals what you want, then you get it.
So in order to "send" an event just equals to placing the correct ID in the queue.
However, I don't believe this will be as efficient as using events directly.
Post Reply