• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <common/bk_err.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 /**
24  * @brief BK EVENT API
25  * @addtogroup bk_api_event BK EVENT API
26  * @{
27  */
28 
29 /**
30  * @brief BK EVENT typedefs
31  * @defgroup bk_api_event_typedef
32  * @ingroup bk_api_event
33  * @{
34  */
35 
36 typedef enum {
37 	EVENT_MOD_WIFI_INTERNAL,   /**< WiFi internal event */
38 	EVENT_MOD_WIFI,            /**< WiFi public event */
39 	EVENT_MOD_NETIF,           /**< Netif event */
40 	EVENT_MOD_COUNT,           /**< Event module count */
41 } event_module_t;
42 
43 #define BK_ERR_EVENT_MOD            (BK_ERR_EVENT_BASE - 1) /**< Event module is not supported */
44 #define BK_ERR_EVENT_ID             (BK_ERR_EVENT_BASE - 2) /**< Event ID is not supported */
45 #define BK_ERR_EVENT_MOD_OR_ID      (BK_ERR_EVENT_BASE - 3) /**< Event ID or Module is invalid */
46 #define BK_ERR_EVENT_CB_EXIST       (BK_ERR_EVENT_BASE - 4) /**< Event cb already registered */
47 #define BK_ERR_EVENT_NO_CB          (BK_ERR_EVENT_BASE - 5) /**< No event cb is registered */
48 #define BK_ERR_EVENT_CREATE_QUEUE   (BK_ERR_EVENT_BASE - 6) /**< Failed to create event message queue */
49 #define BK_ERR_EVENT_CREATE_MUTEX   (BK_ERR_EVENT_BASE - 7) /**< Failed to create event mutex */
50 #define BK_ERR_EVENT_CREATE_TASK    (BK_ERR_EVENT_BASE - 8) /**< Failed to create event task */
51 #define BK_ERR_EVENT_NULL_MSG       (BK_ERR_EVENT_BASE - 9) /**< Event message is NULL */
52 #define BK_ERR_EVENT_INIT_SEM       (BK_ERR_EVENT_BASE - 10) /**< Failed to init RTOS semaphore */
53 #define BK_ERR_EVENT_NOT_INIT       (BK_ERR_EVENT_BASE - 11) /**< Event module not init */
54 #define BK_ERR_EVENT_POST_QUEUE     (BK_ERR_EVENT_BASE - 12) /**< Failed to post event request to event queue */
55 #define BK_ERR_EVENT_UNKNOWN_MSG    (BK_ERR_EVENT_BASE - 13) /**< Not supported event message */
56 
57 #define EVENT_ID_ALL            (-1) /**< All events in a event module */
58 
59 /**
60  * @}
61  */
62 
63 /**
64  * @}
65  */
66 
67 /**
68  * @brief BK EVENT API
69  * @addtogroup bk_api_event BK EVENT API
70  * @{
71  */
72 
73 /**
74  * @brief Event callback
75  *
76  * The event callback get called when related event dispatched.
77  */
78 typedef bk_err_t (*event_cb_t)(void *arg, event_module_t event_module,
79 		int event_id, void *event_data);
80 
81 /**
82  * @brief Register event callback
83  *
84  * @param event_module_id the module ID of the event to register the callback for
85  * @param event_id the id of the event to register the callback for
86  * @param event_cb the callback which gets called when the event is dispatched
87  * @param event_cb_arg data, aside from event data, that is passed to the handler when it is called
88  *
89  * @return
90  *  - BK_OK: succeed
91  *  - BK_ERR_EVENT_NOT_INIT: the event module is not initialized
92  *  - BK_ERR_EVENT_MOD_OR_ID: the event module ID or event ID is invalid
93  *  - BK_ERR_EVENT_NO_MEM: out of memory
94  *  - BK_ERR_EVENT_INIT_SEM: failed to init the internal event semaphore
95  *  - BK_ERR_EVENT_POST_QUEUE: failed to post the event to event queue
96  *  - BK_ERR_EVENT_CB_EXIST: the event callback already unregistered
97  *  - Others: Fail
98  */
99 bk_err_t bk_event_register_cb(event_module_t event_module_id, int event_id,
100 		event_cb_t event_cb, void *event_cb_arg);
101 
102 /**
103  * @brief Unregister a callback
104  *
105  * This function can be used to unregister a callback so that it no longer gets called
106  * during dispatch.
107  *
108  * @param event_module_id the ID of the module with which to unregister the callback
109  * @param event_id the ID of the event with which to unregister the callback
110  * @param event_cb the callback to be unregistered
111  *
112  * @return
113  *  - BK_OK: Success
114  *  - BK_ERR_EVENT_NOT_INIT: the event module is not initialized
115  *  - BK_ERR_EVENT_MOD_OR_ID: the event module ID or event ID is invalid
116  *  - BK_ERR_EVENT_NO_MEM: out of memory
117  *  - BK_ERR_EVENT_INIT_SEM: failed to init the internal event semaphore
118  *  - BK_ERR_EVENT_POST_QUEUE: failed to post the event to event queue
119  *  - BK_ERR_EVENT_NO_CB: the event callback already unregistered
120  *  - Others: Fail
121  */
122 bk_err_t bk_event_unregister_cb(event_module_t event_module_id, int event_id,
123 		event_cb_t event_cb);
124 
125 /**
126  * @brief Posts an event to the event task.
127  *
128  * @param event_module_id The ID of the module that generates the event
129  * @param event_id the event id that identifies the event
130  * @param event_data the data, specific to the event occurence, that gets passed to the callback
131  * @param event_data_size the size of the event data
132  * @param timeout number of milliseconds to block on a full event queue
133  *
134  * @return
135  *  - BK_OK: Success
136  *  - BK_ERR_EVENT_NOT_INIT: the event module is not initialized
137  *  - BK_ERR_EVENT_MOD_OR_ID: the event module ID or event ID is not invalid
138  *  - BK_ERR_EVENT_NO_MEM: out of memory
139  *  - BK_ERR_EVENT_INIT_SEM: failed to init the internal event semaphore
140  *  - BK_ERR_EVENT_POST_QUEUE: failed to post the event to event queue
141  *  - Others: Fail
142  */
143 bk_err_t bk_event_post(event_module_t event_module_id, int event_id,
144 		void* event_data, size_t event_data_size, uint32_t timeout);
145 
146 /**
147  * @brief Init the event module
148  *
149  * This API initializes the event queue and create the event task.
150  *
151  * This API should be called before any event API is called, generally it should
152  * be called before WiFi/BLE is initialized.
153  *
154  * @return
155  *  - BK_OK: succeed
156  *  - BK_ERR_EVENT_CREATE_QUEUE: failed to create event queue
157  *  - BK_ERR_EVENT_CREATE_TASK: failed to create event task
158  *  - Others: Fail
159  */
160 bk_err_t bk_event_init(void);
161 
162 /**
163  * @brief Deinit the event module
164  *
165  * This API destroy the event queue and delete the event task.
166  *
167  * @return
168  *  - BK_OK: succeed
169  *  - BK_ERR_NO_MEM: out of memory
170  *  - Others: Fail
171  */
172 bk_err_t bk_event_deinit(void);
173 
174 /**
175  * @brief Dump all registered event callback
176  *
177  * @attention This API is for debug only
178  *
179  * @return
180  *  - BK_OK: succeed
181  *  - BK_ERR_NO_MEM: out of memory
182  *  - Others: Fail
183  */
184 bk_err_t bk_event_dump(void);
185 
186 /**
187  * @}
188  */
189 
190 typedef bk_err_t (event_callback_t)(void *arg, event_module_t event_module,
191 		int event_id, void *event_data);
192 
193 #ifdef __cplusplus
194 }
195 #endif
196