Kévin Redon | 69b92d9 | 2019-01-24 16:39:20 +0100 | [diff] [blame] | 1 | /** |
| 2 | * \file |
| 3 | * |
| 4 | * \brief Events declaration. |
| 5 | * |
| 6 | * Copyright (c) 2015-2018 Microchip Technology Inc. and its subsidiaries. |
| 7 | * |
| 8 | * \asf_license_start |
| 9 | * |
| 10 | * \page License |
| 11 | * |
| 12 | * Subject to your compliance with these terms, you may use Microchip |
| 13 | * software and any derivatives exclusively with Microchip products. |
| 14 | * It is your responsibility to comply with third party license terms applicable |
| 15 | * to your use of third party software (including open source software) that |
| 16 | * may accompany Microchip software. |
| 17 | * |
| 18 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, |
| 19 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, |
| 20 | * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, |
| 21 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE |
| 22 | * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL |
| 23 | * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE |
| 24 | * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE |
| 25 | * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT |
| 26 | * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY |
| 27 | * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 28 | * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 29 | * |
| 30 | * \asf_license_stop |
| 31 | * |
| 32 | */ |
| 33 | |
| 34 | #ifndef _UTILS_EVENT_H_INCLUDED |
| 35 | #define _UTILS_EVENT_H_INCLUDED |
| 36 | |
| 37 | #ifdef __cplusplus |
| 38 | extern "C" { |
| 39 | #endif |
| 40 | |
| 41 | #include <utils.h> |
| 42 | #include <utils_list.h> |
| 43 | #include <events.h> |
| 44 | |
| 45 | /** |
| 46 | * \brief The maximum amount of events |
| 47 | */ |
| 48 | #define EVENT_MAX_AMOUNT 8 |
| 49 | |
| 50 | /** |
| 51 | * \brief The size of event mask used, it is EVENT_MAX_AMOUNT rounded up to the |
| 52 | * closest number divisible by 8. |
| 53 | */ |
| 54 | #define EVENT_MASK_SIZE (round_up(EVENT_MAX_AMOUNT, 8)) |
| 55 | |
| 56 | /** |
| 57 | * \brief The type of event ID. IDs should start with 0 and be in numerical order. |
| 58 | */ |
| 59 | typedef uint8_t event_id_t; |
| 60 | |
| 61 | /** |
| 62 | * \brief The type of returned parameter. This type is big enough to contain |
| 63 | * pointer to data on any platform. |
| 64 | */ |
| 65 | typedef uintptr_t event_data_t; |
| 66 | |
| 67 | /** |
| 68 | * \brief The type of returned parameter. This type is big enough to contain |
| 69 | * pointer to data on any platform. |
| 70 | */ |
| 71 | typedef void (*event_cb_t)(event_id_t id, event_data_t data); |
| 72 | |
| 73 | /** |
| 74 | * \brief Event structure |
| 75 | */ |
| 76 | struct event { |
| 77 | struct list_element elem; /*! The pointer to next event */ |
| 78 | uint8_t mask[EVENT_MASK_SIZE]; /*! Mask of event IDs callback is called for */ |
| 79 | event_cb_t cb; /*! Callback to be called when an event occurs */ |
| 80 | }; |
| 81 | |
| 82 | /** |
| 83 | * \brief Subscribe to event |
| 84 | * |
| 85 | * \param[in] event The pointer to event structure |
| 86 | * \param[in] id The event ID to subscribe to |
| 87 | * \param[in] cb The callback function to call when the given event occurs |
| 88 | * |
| 89 | * \return The status of subscription |
| 90 | */ |
| 91 | int32_t event_subscribe(struct event *const event, const event_id_t id, event_cb_t cb); |
| 92 | |
| 93 | /** |
| 94 | * \brief Remove event from subscription |
| 95 | * |
| 96 | * \param[in] event The pointer to event structure |
| 97 | * \param[in] id The event ID to remove subscription from |
| 98 | * |
| 99 | * \return The status of subscription removing |
| 100 | */ |
| 101 | int32_t event_unsubscribe(struct event *const event, const event_id_t id); |
| 102 | |
| 103 | /** |
| 104 | * \brief Post event |
| 105 | * |
| 106 | * \param[in] id The event ID to post |
| 107 | * \param[in] data The event data to be passed to event subscribers |
| 108 | */ |
| 109 | void event_post(const event_id_t id, const event_data_t data); |
| 110 | |
| 111 | #ifdef __cplusplus |
| 112 | } |
| 113 | #endif |
| 114 | |
| 115 | #endif /* _UTILS_EVENT_H_INCLUDED */ |