oic; use os_events for controlling when to run oc_etimer.
Project: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/commit/a400a8d9 Tree: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/tree/a400a8d9 Diff: http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/diff/a400a8d9 Branch: refs/heads/develop Commit: a400a8d973bcf8158d6e4552b4e14451696f3ea6 Parents: a0ba348 Author: Marko Kiiskila <ma...@runtime.io> Authored: Tue Nov 15 10:48:34 2016 -0800 Committer: Marko Kiiskila <ma...@runtime.io> Committed: Mon Nov 21 17:15:47 2016 -0800 ---------------------------------------------------------------------- net/oic/src/api/oc_ri.c | 4 +- net/oic/src/port/mynewt/adaptor.h | 17 +- net/oic/src/util/oc_etimer.c | 287 ++++++++++++++------------------- net/oic/src/util/oc_etimer.h | 77 +-------- 4 files changed, 139 insertions(+), 246 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a400a8d9/net/oic/src/api/oc_ri.c ---------------------------------------------------------------------- diff --git a/net/oic/src/api/oc_ri.c b/net/oic/src/api/oc_ri.c index 8de9c81..64dccb2 100644 --- a/net/oic/src/api/oc_ri.c +++ b/net/oic/src/api/oc_ri.c @@ -194,7 +194,7 @@ static void start_processes(void) { allocate_events(); - oc_process_start(&oc_etimer_process, NULL); + oc_etimer_init(); oc_process_start(&timed_callback_events, NULL); oc_process_start(&coap_engine, NULL); oc_process_start(&message_buffer_handler, NULL); @@ -209,7 +209,7 @@ start_processes(void) static void stop_processes(void) { - oc_process_exit(&oc_etimer_process); + oc_etimer_deinit(); oc_process_exit(&timed_callback_events); oc_process_exit(&coap_engine); http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a400a8d9/net/oic/src/port/mynewt/adaptor.h ---------------------------------------------------------------------- diff --git a/net/oic/src/port/mynewt/adaptor.h b/net/oic/src/port/mynewt/adaptor.h index 259691c..e9062e0 100644 --- a/net/oic/src/port/mynewt/adaptor.h +++ b/net/oic/src/port/mynewt/adaptor.h @@ -27,28 +27,29 @@ extern "C" { struct os_eventq *oc_evq_get(void); void oc_evq_set(struct os_eventq *evq); +struct oc_message_s; #if (MYNEWT_VAL(OC_TRANSPORT_IP) == 1) int oc_connectivity_init_ip(void); void oc_connectivity_shutdown_ip(void); -void oc_send_buffer_ip(oc_message_t *message); -void oc_send_buffer_ip_mcast(oc_message_t *message); -oc_message_t *oc_attempt_rx_ip(void); +void oc_send_buffer_ip(struct oc_message_s *message); +void oc_send_buffer_ip_mcast(struct oc_message_s *message); +struct oc_message_s *oc_attempt_rx_ip(void); #endif #if (MYNEWT_VAL(OC_TRANSPORT_GATT) == 1) int oc_connectivity_init_gatt(void); void oc_connectivity_shutdown_gatt(void); -void oc_send_buffer_gatt(oc_message_t *message); -void oc_send_buffer_gatt_mcast(oc_message_t *message); -oc_message_t *oc_attempt_rx_gatt(void); +void oc_send_buffer_gatt(struct oc_message_s *message); +void oc_send_buffer_gatt_mcast(struct oc_message_s *message); +struct oc_message_s *oc_attempt_rx_gatt(void); #endif #if (MYNEWT_VAL(OC_TRANSPORT_SERIAL) == 1) int oc_connectivity_init_serial(void); void oc_connectivity_shutdown_serial(void); -void oc_send_buffer_serial(oc_message_t *message); -oc_message_t *oc_attempt_rx_serial(void); +void oc_send_buffer_serial(struct oc_message_s *message); +struct oc_message_s *oc_attempt_rx_serial(void); #endif #ifdef __cplusplus http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a400a8d9/net/oic/src/util/oc_etimer.c ---------------------------------------------------------------------- diff --git a/net/oic/src/util/oc_etimer.c b/net/oic/src/util/oc_etimer.c index bf56f49..138d251 100644 --- a/net/oic/src/util/oc_etimer.c +++ b/net/oic/src/util/oc_etimer.c @@ -34,230 +34,191 @@ * */ +#include <os/os_eventq.h> + #include "oc_etimer.h" #include "oc_process.h" +#include "port/mynewt/adaptor.h" + +static void oc_etimer_process(struct os_event *); + +static struct os_event etimer_ev = { + .ev_cb = oc_etimer_process +}; static struct oc_etimer *timerlist; static oc_clock_time_t next_expiration; -OC_PROCESS(oc_etimer_process, "Event timer"); /*---------------------------------------------------------------------------*/ static void update_time(void) { - oc_clock_time_t tdist; - oc_clock_time_t now; - struct oc_etimer *t; - - if (timerlist == NULL) { - next_expiration = 0; - } else { - now = oc_clock_time(); - t = timerlist; - /* Must calculate distance to next time into account due to wraps */ - tdist = t->timer.start + t->timer.interval - now; - for (t = t->next; t != NULL; t = t->next) { - if (t->timer.start + t->timer.interval - now < tdist) { - tdist = t->timer.start + t->timer.interval - now; - } - } - next_expiration = now + tdist; - } -} -/*---------------------------------------------------------------------------*/ -OC_PROCESS_THREAD(oc_etimer_process, ev, data) -{ - struct oc_etimer *t, *u; - - OC_PROCESS_BEGIN(); - - timerlist = NULL; - - while (1) { - OC_PROCESS_YIELD(); - - if (ev == OC_PROCESS_EVENT_EXITED) { - struct oc_process *p = data; - - while (timerlist != NULL && timerlist->p == p) { - timerlist = timerlist->next; - } - - if (timerlist != NULL) { + oc_clock_time_t tdist; + oc_clock_time_t now; + struct oc_etimer *t; + + if (timerlist == NULL) { + next_expiration = 0; + } else { + now = oc_clock_time(); t = timerlist; - while (t->next != NULL) { - if (t->next->p == p) { - t->next = t->next->next; - } else - t = t->next; - } - } - continue; - } else if (ev != OC_PROCESS_EVENT_POLL) { - continue; - } - - again: - - u = NULL; - - for (t = timerlist; t != NULL; t = t->next) { - if (oc_timer_expired(&t->timer)) { - if (oc_process_post(t->p, OC_PROCESS_EVENT_TIMER, t) == - OC_PROCESS_ERR_OK) { - - /* Reset the process ID of the event timer, to signal that the - etimer has expired. This is later checked in the - oc_etimer_expired() function. */ - t->p = OC_PROCESS_NONE; - if (u != NULL) { - u->next = t->next; - } else { - timerlist = t->next; - } - t->next = NULL; - update_time(); - goto again; - } else { - oc_etimer_request_poll(); + /* Must calculate distance to next time into account due to wraps */ + tdist = t->timer.start + t->timer.interval - now; + for (t = t->next; t != NULL; t = t->next) { + if (t->timer.start + t->timer.interval - now < tdist) { + tdist = t->timer.start + t->timer.interval - now; + } } - } - u = t; + next_expiration = now + tdist; } - } - - OC_PROCESS_END(); } /*---------------------------------------------------------------------------*/ oc_clock_time_t oc_etimer_request_poll(void) { - oc_process_poll(&oc_etimer_process); - return oc_etimer_next_expiration_time(); + oc_etimer_sched(); + return oc_etimer_next_expiration_time(); } /*---------------------------------------------------------------------------*/ static void add_timer(struct oc_etimer *timer) { - struct oc_etimer *t; - - oc_etimer_request_poll(); - - if (timer->p != OC_PROCESS_NONE) { - for (t = timerlist; t != NULL; t = t->next) { - if (t == timer) { - /* Timer already on list, bail out. */ - timer->p = OC_PROCESS_CURRENT(); - update_time(); - return; - } + struct oc_etimer *t; + + oc_etimer_request_poll(); + + if (timer->p != OC_PROCESS_NONE) { + for (t = timerlist; t != NULL; t = t->next) { + if (t == timer) { + /* Timer already on list, bail out. */ + timer->p = OC_PROCESS_CURRENT(); + update_time(); + return; + } + } } - } - /* Timer not on list. */ - timer->p = OC_PROCESS_CURRENT(); - timer->next = timerlist; - timerlist = timer; + /* Timer not on list. */ + timer->p = OC_PROCESS_CURRENT(); + timer->next = timerlist; + timerlist = timer; - update_time(); + update_time(); } /*---------------------------------------------------------------------------*/ void oc_etimer_set(struct oc_etimer *et, oc_clock_time_t interval) { - oc_timer_set(&et->timer, interval); - add_timer(et); -} -/*---------------------------------------------------------------------------*/ -void -oc_etimer_reset_with_new_interval(struct oc_etimer *et, - oc_clock_time_t interval) -{ - oc_timer_reset(&et->timer); - et->timer.interval = interval; - add_timer(et); -} -/*---------------------------------------------------------------------------*/ -void -oc_etimer_reset(struct oc_etimer *et) -{ - oc_timer_reset(&et->timer); - add_timer(et); + oc_timer_set(&et->timer, interval); + add_timer(et); } /*---------------------------------------------------------------------------*/ void oc_etimer_restart(struct oc_etimer *et) { - oc_timer_restart(&et->timer); - add_timer(et); + oc_timer_restart(&et->timer); + add_timer(et); } /*---------------------------------------------------------------------------*/ -void -oc_etimer_adjust(struct oc_etimer *et, int timediff) +int +oc_etimer_expired(struct oc_etimer *et) { - et->timer.start += timediff; - update_time(); + return et->p == OC_PROCESS_NONE; } /*---------------------------------------------------------------------------*/ int -oc_etimer_expired(struct oc_etimer *et) +oc_etimer_pending(void) { - return et->p == OC_PROCESS_NONE; + return timerlist != NULL; } /*---------------------------------------------------------------------------*/ oc_clock_time_t -oc_etimer_expiration_time(struct oc_etimer *et) +oc_etimer_next_expiration_time(void) { - return et->timer.start + et->timer.interval; + return oc_etimer_pending() ? next_expiration : 0; } /*---------------------------------------------------------------------------*/ -oc_clock_time_t -oc_etimer_start_time(struct oc_etimer *et) +void +oc_etimer_stop(struct oc_etimer *et) { - return et->timer.start; + struct oc_etimer *t; + + /* First check if et is the first event timer on the list. */ + if (et == timerlist) { + timerlist = timerlist->next; + update_time(); + } else { + /* Else walk through the list and try to find the item before the + et timer. */ + for (t = timerlist; t != NULL && t->next != et; t = t->next) + ; + + if (t != NULL) { + /* We've found the item before the event timer that we are about + to remove. We point the items next pointer to the event after + the removed item. */ + t->next = et->next; + + update_time(); + } + } + + /* Remove the next pointer from the item to be removed. */ + et->next = NULL; + /* Set the timer as expired */ + et->p = OC_PROCESS_NONE; } -/*---------------------------------------------------------------------------*/ -int -oc_etimer_pending(void) + +void +oc_etimer_init(void) { - return timerlist != NULL; + timerlist = NULL; } -/*---------------------------------------------------------------------------*/ -oc_clock_time_t -oc_etimer_next_expiration_time(void) + +void +oc_etimer_deinit(void) { - return oc_etimer_pending() ? next_expiration : 0; + timerlist = NULL; } -/*---------------------------------------------------------------------------*/ + void -oc_etimer_stop(struct oc_etimer *et) +oc_etimer_sched(void) { - struct oc_etimer *t; + os_eventq_put(oc_evq_get(), &etimer_ev); +} - /* First check if et is the first event timer on the list. */ - if (et == timerlist) { - timerlist = timerlist->next; - update_time(); - } else { - /* Else walk through the list and try to find the item before the - et timer. */ - for (t = timerlist; t != NULL && t->next != et; t = t->next) - ; +static void +oc_etimer_process(struct os_event *ev) +{ + struct oc_etimer *t, *u; - if (t != NULL) { - /* We've found the item before the event timer that we are about - to remove. We point the items next pointer to the event after - the removed item. */ - t->next = et->next; +again: + u = NULL; - update_time(); + for (t = timerlist; t != NULL; t = t->next) { + if (oc_timer_expired(&t->timer)) { + if (oc_process_post(t->p, OC_PROCESS_EVENT_TIMER, t) == + OC_PROCESS_ERR_OK) { + + /* Reset the process ID of the event timer, to signal that the + etimer has expired. This is later checked in the + oc_etimer_expired() function. */ + t->p = OC_PROCESS_NONE; + if (u != NULL) { + u->next = t->next; + } else { + timerlist = t->next; + } + t->next = NULL; + update_time(); + goto again; + } else { + oc_etimer_request_poll(); + } + } + u = t; } - } - - /* Remove the next pointer from the item to be removed. */ - et->next = NULL; - /* Set the timer as expired */ - et->p = OC_PROCESS_NONE; } + /*---------------------------------------------------------------------------*/ /** @} */ http://git-wip-us.apache.org/repos/asf/incubator-mynewt-core/blob/a400a8d9/net/oic/src/util/oc_etimer.h ---------------------------------------------------------------------- diff --git a/net/oic/src/util/oc_etimer.h b/net/oic/src/util/oc_etimer.h index 703b557..4ef58e5 100644 --- a/net/oic/src/util/oc_etimer.h +++ b/net/oic/src/util/oc_etimer.h @@ -95,37 +95,6 @@ struct oc_etimer void oc_etimer_set(struct oc_etimer *et, oc_clock_time_t interval); /** - * \brief Reset an event timer with the same interval as was - * previously set. - * \param et A pointer to the event timer. - * - * This function resets the event timer with the same - * interval that was given to the event timer with the - * oc_etimer_set() function. The start point of the interval - * is the exact time that the event timer last - * expired. Therefore, this function will cause the timer - * to be stable over time, unlike the oc_etimer_restart() - * function. - * - * \sa oc_etimer_restart() - */ -void oc_etimer_reset(struct oc_etimer *et); - -/** - * \brief Reset an event timer with a new interval. - * \param et A pointer to the event timer. - * \param interval The interval before the timer expires. - * - * This function very similar to oc_etimer_reset. Opposed to - * oc_etimer_reset it is possible to change the timout. - * This allows accurate, non-periodic timers without drift. - * - * \sa oc_etimer_reset() - */ -void oc_etimer_reset_with_new_interval(struct oc_etimer *et, - oc_clock_time_t interval); - -/** * \brief Restart an event timer from the current point in time * \param et A pointer to the event timer. * @@ -143,47 +112,6 @@ void oc_etimer_reset_with_new_interval(struct oc_etimer *et, void oc_etimer_restart(struct oc_etimer *et); /** - * \brief Adjust the expiration time for an event timer - * \param et A pointer to the event timer. - * \param td The time difference to adjust the expiration time with. - * - * This function is used to adjust the time the event - * timer will expire. It can be used to synchronize - * periodic timers without the need to restart the timer - * or change the timer interval. - * - * \note This function should only be used for small - * adjustments. For large adjustments use oc_etimer_set() - * instead. - * - * \note A periodic timer will drift unless the - * oc_etimer_reset() function is used. - * - * \sa oc_etimer_set() - * \sa oc_etimer_reset() - */ -void oc_etimer_adjust(struct oc_etimer *et, int td); - -/** - * \brief Get the expiration time for the event timer. - * \param et A pointer to the event timer - * \return The expiration time for the event timer. - * - * This function returns the expiration time for an event timer. - */ -oc_clock_time_t oc_etimer_expiration_time(struct oc_etimer *et); - -/** - * \brief Get the start time for the event timer. - * \param et A pointer to the event timer - * \return The start time for the event timer. - * - * This function returns the start time (when the timer - * was last set) for an event timer. - */ -oc_clock_time_t oc_etimer_start_time(struct oc_etimer *et); - -/** * \brief Check if an event timer has expired. * \param et A pointer to the event timer * \return Non-zero if the timer has expired, zero otherwise. @@ -245,7 +173,10 @@ oc_clock_time_t oc_etimer_next_expiration_time(void); /** @} */ -OC_PROCESS_NAME(oc_etimer_process); +void oc_etimer_init(void); +void oc_etimer_deinit(void); +void oc_etimer_sched(void); + #ifdef __cplusplus } #endif