0

Is it possible to do what the code below does without any process? I need a timeout without surrounding it with Contiki process. Is this possible?

#include "sys/etimer.h"

PROCESS_THREAD(example_process, ev, data)
{
    static struct etimer et;
    PROCESS_BEGIN();

    /* Delay 1 second */
    etimer_set(&et, CLOCK_SECOND);

    while(1) {
        PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));

        /* Reset the etimer to trig again in 1 second */
        etimer_reset(&et);
        /* ... */
    }
    PROCESS_END();
}
kfx
  • 8,136
  • 3
  • 28
  • 52

1 Answers1

1

You can use callback timers:

struct ctimer my_timer;

static void
callback_function(void *data)
{
  ctimer_set(&my_timer, CLOCK_SECOND, callback_function, NULL);
}

To get the timer started the first time, call ctimer_set(&my_timer, CLOCK_SECOND, callback_function, NULL); from some initialization code. It does not have to be within a process handler function.

kfx
  • 8,136
  • 3
  • 28
  • 52
  • Thank you for the answer @kfx I'll +1 your answer. Also, I didn't know you can `ctimer_set` from within the function. Is that allowed or do we need to edit the answer? (I thought that only `ctimer_reset` or `ctimer_restart` are allowed). – Rahav Nov 22 '22 at 23:04
  • @Rahav Interesting, what makes you think it's not allowed? Did you see some comments that point to that or experienced problems with it? – kfx Nov 23 '22 at 07:32
  • No, I simply never saw it used this way. What I need to is delay and block in a regular C function (not a protothread) while running in Cooja. But for the life of me, all timers yield to the OS. – Rahav Nov 23 '22 at 20:48