NordicPlayground / nRF51-ble-bcast-mesh

Other
324 stars 121 forks source link

Is function timer_sch_schedule fit for "delay action" events? #149

Open bayou9 opened 7 years ago

bayou9 commented 7 years ago

I'm trying to push a trickle instance into the local data cache... AFTER a certain variable time T (for example, 200ms, 55 ms, 308 ms, etc )

Now clearly there are 2 ways to do it:

One is to write my own timer function that , after the desired delay T, triggers an interrupt, and in this interrupt a callback will be called upon to push my target data into the m_data_cache and then I simply forget about it because it will have a timeout (of course, provided that it hears an amount of "gossip" with the same handle for less than c times) and promptly be sent, which is exactly what I want, the minute difference of 5 or even 50 ms is of no concern to me.

But I'm rather reluctant, because I'm not sure if I would end up missing something and result in some unforeseen consequences, another reason is that the function "timer_sch_schedule" is readily available but never used, and based on its description, it is exactly what I want, and it was exactly what I went after

Problem is, it does not work. I entered error loop after either:

A. try to event_handler_push my event into the asynchronous event queue. B. finishing executing the radio_signal_callback.

A will not happen if I place a breakpoint right before it and F11 through everything, however that's when B happens, A will happen only when I let the program run on its own or press F10 instead of F11 after the breakpoint pause (right before event_handler_push(evt)), in which case I would be letting it run by itself too.

I suspect B isn't a real problem, just has something to do with time_slots.

And the code are as follows:

           `timer_event_t *p_my_setting_evt;
    timer_event_t my_setting_evt;
    p_my_setting_evt = &my_setting_evt;
    timestamp_t time_now;
    time_now_add = timer_now();
    p_my_setting_evt->timestamp = time_now + (my_offset);
    p_my_setting_evt->cb = rbc_mesh_my_value_set;
    p_my_setting_evt->interval = 0;
    p_my_setting_evt->p_next = NULL;

    timer_sch_schedule(p_my_setting_evt);`

What would be the probable cause(s)?

trond-snekvik commented 7 years ago

Sounds like the timer scheduler should fit your use-case quite well.

It is a finicky little module, that fails quite miserably unless used 100% correctly (which is why we didn't expose it completely in the API :)).

For it to function correctly, the user has to follow some rules:

Example usage:

static timer_event_t m_event;

void schedule_event()
{
    m_event.timestamp = timer_now() + DELAY;
    m_event.interval = 0;
    m_event.cb = timer_cb_function;
    timer_sch_schedule(&m_event);
}