|
| 1 | +/* |
| 2 | + * Copyright (c) 2014-2015 ARM Limited. All rights reserved. |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Licensed under the Apache License, Version 2.0 (the License); you may |
| 5 | + * not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#ifndef EVENTOS_EVENT_H_ |
| 17 | +#define EVENTOS_EVENT_H_ |
| 18 | +#ifdef __cplusplus |
| 19 | +extern "C" { |
| 20 | +#endif |
| 21 | + |
| 22 | +#include "ns_types.h" |
| 23 | +#include "ns_list.h" |
| 24 | + |
| 25 | +/** |
| 26 | + * \enum arm_library_event_priority_e |
| 27 | + * \brief Event Priority level. |
| 28 | + */ |
| 29 | +typedef enum arm_library_event_priority_e { |
| 30 | + ARM_LIB_HIGH_PRIORITY_EVENT = 0, /**< High Priority Event (Function CB) */ |
| 31 | + ARM_LIB_MED_PRIORITY_EVENT = 1, /**< Medium Priority (Timer) */ |
| 32 | + ARM_LIB_LOW_PRIORITY_EVENT = 2, /*!*< Normal Event and ECC / Security */ |
| 33 | +} arm_library_event_priority_e; |
| 34 | + |
| 35 | +/** |
| 36 | + * \struct arm_event_s |
| 37 | + * \brief Event structure. |
| 38 | + */ |
| 39 | +typedef struct arm_event_s { |
| 40 | + int8_t receiver; /**< Event handler Tasklet ID */ |
| 41 | + int8_t sender; /**< Event sender Tasklet ID */ |
| 42 | + uint8_t event_type; /**< This will be typecast arm_library_event_type_e, arm_internal_event_type_e or application specific define */ |
| 43 | + uint8_t event_id; /**< Timer ID, NWK interface ID or application specific ID */ |
| 44 | + void *data_ptr; /**< Application could share data pointer tasklet to tasklet */ |
| 45 | + arm_library_event_priority_e priority; |
| 46 | + uintptr_t event_data; |
| 47 | +} arm_event_t; |
| 48 | + |
| 49 | +/* Backwards compatibility */ |
| 50 | +typedef arm_event_t arm_event_s; |
| 51 | + |
| 52 | +/** |
| 53 | + * \struct arm_event_storage |
| 54 | + * \brief Event structure storage, including list link. |
| 55 | +
|
| 56 | +@startuml |
| 57 | +
|
| 58 | +partition "Event loop" { |
| 59 | +(*) -->[event created] "UNQUEUED" |
| 60 | +"UNQUEUED" -->[event_core_write()] "QUEUED" |
| 61 | +"QUEUED" -->[event_core_read()] "RUNNING" |
| 62 | +"RUNNING" ->[event_core_free_push()] "UNQUEUED" |
| 63 | +} |
| 64 | +
|
| 65 | +partition "system_timer.c" { |
| 66 | + "UNQUEUED:timer" -->[eventOS_event_send_timer_allocated()] "QUEUED" |
| 67 | +} |
| 68 | +@enduml |
| 69 | +
|
| 70 | + */ |
| 71 | +typedef struct arm_event_storage { |
| 72 | + arm_event_s data; |
| 73 | + enum { |
| 74 | + ARM_LIB_EVENT_STARTUP_POOL, |
| 75 | + ARM_LIB_EVENT_DYNAMIC, |
| 76 | + ARM_LIB_EVENT_USER, |
| 77 | + ARM_LIB_EVENT_TIMER, |
| 78 | + } allocator; |
| 79 | + enum { |
| 80 | + ARM_LIB_EVENT_UNQUEUED, |
| 81 | + ARM_LIB_EVENT_QUEUED, |
| 82 | + ARM_LIB_EVENT_RUNNING, |
| 83 | + } state; |
| 84 | + ns_list_link_t link; |
| 85 | +} arm_event_storage_t; |
| 86 | + |
| 87 | +/** |
| 88 | + * \brief Send event to event scheduler. |
| 89 | + * |
| 90 | + * \param event pointer to pushed event. |
| 91 | + * |
| 92 | + * Event data is copied by the call, and this copy persists until the |
| 93 | + * recipient's callback function returns. The callback function is passed |
| 94 | + * a pointer to a copy of the data, not the original pointer. |
| 95 | + * |
| 96 | + * \return 0 Event push OK |
| 97 | + * \return -1 Memory allocation Fail |
| 98 | + */ |
| 99 | +extern int8_t eventOS_event_send(const arm_event_t *event); |
| 100 | + |
| 101 | +/* Alternate names for timer function from eventOS_event_timer.h; |
| 102 | + * implementations may one day merge */ |
| 103 | +#define eventOS_event_send_at(event, at) eventOS_event_timer_request_at(event, at) |
| 104 | +#define eventOS_event_send_in(event, in) eventOS_event_timer_request_in(event, in) |
| 105 | +#define eventOS_event_send_after(event, after) eventOS_event_timer_request_after(event, after) |
| 106 | +#define eventOS_event_send_every(event, every) eventOS_event_timer_request_every(event, every) |
| 107 | + |
| 108 | +/** |
| 109 | + * \brief Send user-allocated event to event scheduler. |
| 110 | + * |
| 111 | + * \param event pointer to pushed event storage. |
| 112 | + * |
| 113 | + * The event structure is not copied by the call, the event system takes |
| 114 | + * ownership and it is threaded directly into the event queue. This avoids the |
| 115 | + * possibility of event sending failing due to memory exhaustion. |
| 116 | + * |
| 117 | + * event->data must be filled in on entry - the rest of the structure (link and |
| 118 | + * allocator) need not be. |
| 119 | + * |
| 120 | + * The structure must remain valid until the recipient is called - the |
| 121 | + * event system passes ownership to the receiving event handler, who may then |
| 122 | + * invalidate it, or send it again. |
| 123 | + * |
| 124 | + * The recipient receives a pointer to the arm_event_t data member of the |
| 125 | + * event - it can use NS_CONTAINER_OF() to get a pointer to the original |
| 126 | + * event passed to this call, or to its outer container. |
| 127 | + * |
| 128 | + * It is a program error to send a user-allocated event to a non-existent task. |
| 129 | + */ |
| 130 | +extern void eventOS_event_send_user_allocated(arm_event_storage_t *event); |
| 131 | + |
| 132 | +/** |
| 133 | + * \brief Event handler callback register |
| 134 | + * |
| 135 | + * Function will register and allocate unique event id handler |
| 136 | + * |
| 137 | + * \param handler_func_ptr function pointer for event handler |
| 138 | + * \param init_event_type generated event type for init purpose |
| 139 | + * |
| 140 | + * \return >= 0 Unique event ID for this handler |
| 141 | + * \return < 0 Register fail |
| 142 | + * |
| 143 | + * */ |
| 144 | +extern int8_t eventOS_event_handler_create(void (*handler_func_ptr)(arm_event_t *), uint8_t init_event_type); |
| 145 | + |
| 146 | +/** |
| 147 | + * Cancel an event. |
| 148 | + * |
| 149 | + * Queued events are removed from the event-loop queue and/or the timer queue. |
| 150 | + * |
| 151 | + * Passing a NULL pointer is allowed, and does nothing. |
| 152 | + * |
| 153 | + * Event pointers are valid from the time they are queued until the event |
| 154 | + * has finished running or is cancelled. |
| 155 | + * |
| 156 | + * Cancelling a currently-running event is only useful to stop scheduling |
| 157 | + * it if it is on a periodic timer; it has no other effect. |
| 158 | + * |
| 159 | + * Cancelling an already-cancelled or already-run single-shot event |
| 160 | + * is undefined behaviour. |
| 161 | + * |
| 162 | + * \param event Pointer to event handle or NULL. |
| 163 | + */ |
| 164 | +extern void eventOS_cancel(arm_event_storage_t *event); |
| 165 | + |
| 166 | +#ifdef __cplusplus |
| 167 | +} |
| 168 | +#endif |
| 169 | +#endif /* EVENTOS_EVENT_H_ */ |
0 commit comments