leo hendrickson / Mbed OS example-Ethernet-mbed-Cloud-connect
Committer:
leothedragon
Date:
Tue May 04 08:55:12 2021 +0000
Revision:
0:8f0bb79ddd48
nmn

Who changed what in which revision?

UserRevisionLine numberNew contents of line
leothedragon 0:8f0bb79ddd48 1 /*
leothedragon 0:8f0bb79ddd48 2 * Copyright (c) 2014-2015 ARM Limited. All rights reserved.
leothedragon 0:8f0bb79ddd48 3 * SPDX-License-Identifier: Apache-2.0
leothedragon 0:8f0bb79ddd48 4 * Licensed under the Apache License, Version 2.0 (the License); you may
leothedragon 0:8f0bb79ddd48 5 * not use this file except in compliance with the License.
leothedragon 0:8f0bb79ddd48 6 * You may obtain a copy of the License at
leothedragon 0:8f0bb79ddd48 7 *
leothedragon 0:8f0bb79ddd48 8 * http://www.apache.org/licenses/LICENSE-2.0
leothedragon 0:8f0bb79ddd48 9 *
leothedragon 0:8f0bb79ddd48 10 * Unless required by applicable law or agreed to in writing, software
leothedragon 0:8f0bb79ddd48 11 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
leothedragon 0:8f0bb79ddd48 12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
leothedragon 0:8f0bb79ddd48 13 * See the License for the specific language governing permissions and
leothedragon 0:8f0bb79ddd48 14 * limitations under the License.
leothedragon 0:8f0bb79ddd48 15 */
leothedragon 0:8f0bb79ddd48 16 #include "eventOS_event.h"
leothedragon 0:8f0bb79ddd48 17 #include "eventOS_event_timer.h"
leothedragon 0:8f0bb79ddd48 18 #include "nsdynmemLIB.h"
leothedragon 0:8f0bb79ddd48 19 #include "ns_list.h"
leothedragon 0:8f0bb79ddd48 20 #include "timer_sys.h"
leothedragon 0:8f0bb79ddd48 21
leothedragon 0:8f0bb79ddd48 22 #define STARTUP_EVENT 0
leothedragon 0:8f0bb79ddd48 23 #define TIMER_EVENT 1
leothedragon 0:8f0bb79ddd48 24
leothedragon 0:8f0bb79ddd48 25 // Timeout structure, already typedefed to timeout_t
leothedragon 0:8f0bb79ddd48 26 struct timeout_entry_t {
leothedragon 0:8f0bb79ddd48 27 void (*callback)(void *);
leothedragon 0:8f0bb79ddd48 28 void *arg;
leothedragon 0:8f0bb79ddd48 29 arm_event_storage_t *event;
leothedragon 0:8f0bb79ddd48 30 };
leothedragon 0:8f0bb79ddd48 31
leothedragon 0:8f0bb79ddd48 32 static int8_t timeout_tasklet_id = -1;
leothedragon 0:8f0bb79ddd48 33
leothedragon 0:8f0bb79ddd48 34 static void timeout_tasklet(arm_event_s *event)
leothedragon 0:8f0bb79ddd48 35 {
leothedragon 0:8f0bb79ddd48 36 if (TIMER_EVENT != event->event_type) {
leothedragon 0:8f0bb79ddd48 37 return;
leothedragon 0:8f0bb79ddd48 38 }
leothedragon 0:8f0bb79ddd48 39
leothedragon 0:8f0bb79ddd48 40 timeout_t *t = event->data_ptr;
leothedragon 0:8f0bb79ddd48 41 arm_event_storage_t *storage = t->event;
leothedragon 0:8f0bb79ddd48 42 sys_timer_struct_s *timer = NS_CONTAINER_OF(storage, sys_timer_struct_s, event);
leothedragon 0:8f0bb79ddd48 43
leothedragon 0:8f0bb79ddd48 44 t->callback(t->arg);
leothedragon 0:8f0bb79ddd48 45
leothedragon 0:8f0bb79ddd48 46
leothedragon 0:8f0bb79ddd48 47 // Check if this was periodic timer
leothedragon 0:8f0bb79ddd48 48 if (timer->period == 0) {
leothedragon 0:8f0bb79ddd48 49 ns_dyn_mem_free(event->data_ptr);
leothedragon 0:8f0bb79ddd48 50 }
leothedragon 0:8f0bb79ddd48 51 }
leothedragon 0:8f0bb79ddd48 52
leothedragon 0:8f0bb79ddd48 53 static timeout_t *eventOS_timeout_at_(void (*callback)(void *), void *arg, uint32_t at, uint32_t period)
leothedragon 0:8f0bb79ddd48 54 {
leothedragon 0:8f0bb79ddd48 55 arm_event_storage_t *storage;
leothedragon 0:8f0bb79ddd48 56
leothedragon 0:8f0bb79ddd48 57 timeout_t *timeout = ns_dyn_mem_alloc(sizeof(timeout_t));
leothedragon 0:8f0bb79ddd48 58 if (!timeout) {
leothedragon 0:8f0bb79ddd48 59 return NULL;
leothedragon 0:8f0bb79ddd48 60 }
leothedragon 0:8f0bb79ddd48 61 timeout->callback = callback;
leothedragon 0:8f0bb79ddd48 62 timeout->arg = arg;
leothedragon 0:8f0bb79ddd48 63
leothedragon 0:8f0bb79ddd48 64 // Start timeout taskled if it is not running
leothedragon 0:8f0bb79ddd48 65 if (-1 == timeout_tasklet_id) {
leothedragon 0:8f0bb79ddd48 66 timeout_tasklet_id = eventOS_event_handler_create(timeout_tasklet, STARTUP_EVENT);
leothedragon 0:8f0bb79ddd48 67 if (timeout_tasklet_id < 0) {
leothedragon 0:8f0bb79ddd48 68 timeout_tasklet_id = -1;
leothedragon 0:8f0bb79ddd48 69 goto FAIL;
leothedragon 0:8f0bb79ddd48 70 }
leothedragon 0:8f0bb79ddd48 71 }
leothedragon 0:8f0bb79ddd48 72
leothedragon 0:8f0bb79ddd48 73 arm_event_t event = {
leothedragon 0:8f0bb79ddd48 74 .receiver = timeout_tasklet_id,
leothedragon 0:8f0bb79ddd48 75 .sender = timeout_tasklet_id,
leothedragon 0:8f0bb79ddd48 76 .event_type = TIMER_EVENT,
leothedragon 0:8f0bb79ddd48 77 .event_id = TIMER_EVENT,
leothedragon 0:8f0bb79ddd48 78 .data_ptr = timeout
leothedragon 0:8f0bb79ddd48 79 };
leothedragon 0:8f0bb79ddd48 80
leothedragon 0:8f0bb79ddd48 81 if (period)
leothedragon 0:8f0bb79ddd48 82 storage = eventOS_event_timer_request_every(&event, period);
leothedragon 0:8f0bb79ddd48 83 else
leothedragon 0:8f0bb79ddd48 84 storage = eventOS_event_timer_request_at(&event, at);
leothedragon 0:8f0bb79ddd48 85
leothedragon 0:8f0bb79ddd48 86 timeout->event = storage;
leothedragon 0:8f0bb79ddd48 87 if (storage)
leothedragon 0:8f0bb79ddd48 88 return timeout;
leothedragon 0:8f0bb79ddd48 89 FAIL:
leothedragon 0:8f0bb79ddd48 90 ns_dyn_mem_free(timeout);
leothedragon 0:8f0bb79ddd48 91 return NULL;
leothedragon 0:8f0bb79ddd48 92 }
leothedragon 0:8f0bb79ddd48 93
leothedragon 0:8f0bb79ddd48 94 timeout_t *eventOS_timeout_ms(void (*callback)(void *), uint32_t ms, void *arg)
leothedragon 0:8f0bb79ddd48 95 {
leothedragon 0:8f0bb79ddd48 96 return eventOS_timeout_at_(callback, arg, eventOS_event_timer_ms_to_ticks(ms)+eventOS_event_timer_ticks(), 0);
leothedragon 0:8f0bb79ddd48 97 }
leothedragon 0:8f0bb79ddd48 98
leothedragon 0:8f0bb79ddd48 99 timeout_t *eventOS_timeout_every_ms(void (*callback)(void *), uint32_t every, void *arg)
leothedragon 0:8f0bb79ddd48 100 {
leothedragon 0:8f0bb79ddd48 101 return eventOS_timeout_at_(callback, arg, 0, eventOS_event_timer_ms_to_ticks(every));
leothedragon 0:8f0bb79ddd48 102 }
leothedragon 0:8f0bb79ddd48 103
leothedragon 0:8f0bb79ddd48 104 void eventOS_timeout_cancel(timeout_t *t)
leothedragon 0:8f0bb79ddd48 105 {
leothedragon 0:8f0bb79ddd48 106 if (!t)
leothedragon 0:8f0bb79ddd48 107 return;
leothedragon 0:8f0bb79ddd48 108
leothedragon 0:8f0bb79ddd48 109 eventOS_cancel(t->event);
leothedragon 0:8f0bb79ddd48 110
leothedragon 0:8f0bb79ddd48 111 // Defer the freeing until returning from the callback
leothedragon 0:8f0bb79ddd48 112 if (t->event->state != ARM_LIB_EVENT_RUNNING) {
leothedragon 0:8f0bb79ddd48 113 ns_dyn_mem_free(t);
leothedragon 0:8f0bb79ddd48 114 }
leothedragon 0:8f0bb79ddd48 115 }