1 /*$Id: //dwh/bluetooth/DWC_ble154combo/firmware/rel/1.32a-lca02/firmware/public_inc/event_manager.h#2 $*/
2 /**
3  ********************************************************************************
4  * @file    event_manager.h
5  * @brief   This file contains all the functions prototypes for the event_manager.c.
6  ******************************************************************************
7  * @copy
8  * This Synopsys DWC Bluetooth Low Energy Combo Link Layer/MAC software and
9  * associated documentation ( hereinafter the "Software") is an unsupported
10  * proprietary work of Synopsys, Inc. unless otherwise expressly agreed to in
11  * writing between Synopsys and you. The Software IS NOT an item of Licensed
12  * Software or a Licensed Product under any End User Software License Agreement
13  * or Agreement for Licensed Products with Synopsys or any supplement thereto.
14  * Synopsys is a registered trademark of Synopsys, Inc. Other names included in
15  * the SOFTWARE may be the trademarks of their respective owners.
16  *
17  * Synopsys MIT License:
18  * Copyright (c) 2020-Present Synopsys, Inc
19  *
20  * Permission is hereby granted, free of charge, to any person obtaining a copy of
21  * the Software), to deal in the Software without restriction, including without
22  * limitation the rights to use, copy, modify, merge, publish, distribute,
23  * sublicense, and/or sell copies of the Software, and to permit persons to whom
24  * the Software is furnished to do so, subject to the following conditions:
25  *
26  * The above copyright notice and this permission notice shall be included in all
27  * copies or substantial portions of the Software.
28  *
29  * THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30  * IMPLIED, INCLUDING, BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31  * FITNESS FOR A PARTICULAR PURPOSE, AND NONINFRINGEMENT. IN NO EVENT SHALL THE
32  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
33  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT, OR OTHERWISE ARISING FROM,
34  * OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  * */
37 /* Define to prevent recursive inclusion -------------------------------------*/
38 #ifndef INCLUDE_EVENT_MANAGER_H_
39 #define INCLUDE_EVENT_MANAGER_H_
40 #include <stdint.h>
41 #include "ll_fw_config.h"
42 #include "common_types.h"
43 
44 /* Defination ----------------------------------------------------------------*/
45 /*Event manager events*/
46 #define EVENT_PENDING		1
47 #define HANDLE_IS_FULL		77
48 #define NO_EVENT_PENDING	0
49 
50 
51 #define EVENT_NOT_BUSY		0
52 #define EVENT_BUSY			1
53 
54 #if (NUM_OF_CTSM_EMNGR_HNDLS > 3)
55 #error "NUM_OF_CTSM_EMNGR_HNDLS shouldn't exceed 3 !"
56 #endif
57 
58 
59 
60 typedef uint8_t (*conditional_cbk)(void * em_data , void * caller_data);
61 
62 
63 
64 /**
65  * @brief Enum event manager handler types.
66  */
67 	/* Event Manager IDs */
68 typedef enum {
69 #if SUPPORT_BLE
70 	LLHWC_EVENT,
71 	PRDC_CLBR_EVENT,
72 #if ((SUPPORT_MASTER_CONNECTION) || (SUPPORT_SLAVE_CONNECTION))
73 	CONN_EVENT, /*connection event handler ID*/
74 	CONN_PARAM_UPDATE_EVENT, /* handler for connection parameter update initiated by link layer */
75 #if ((SUPPORT_CONNECTED_ISOCHRONOUS) && (SUPPORT_MASTER_CONNECTION))
76 	CONN_DATA_LEN_UPDATE_EVENT, /* handler for connection data length update initiated by link layer */
77 #endif /*SUPPORT_CONNECTED_ISOCHRONOUS && SUPPORT_MASTER_CONNECTION*/
78 #if(SUPPORT_CONNECTED_ISOCHRONOUS &&( SUPPORT_MASTER_CONNECTION || SUPPORT_SLAVE_CONNECTION))
79 	CIS_EVENT,
80 #endif /*SUPPORT_CONNECTED_ISOCHRONOUS*/
81 #endif /* SUPPORT_MASTER_CONNECTION) || (SUPPORT_SLAVE_CONNECTION */
82 #if(SUPPORT_BRD_ISOCHRONOUS || SUPPORT_SYNC_ISOCHRONOUS)
83 	BIS_TERM_EVENT,
84 #if(SUPPORT_SYNC_ISOCHRONOUS)
85 	BIS_SYNC_TIMEOUT_EVENT,
86 #endif /* SUPPORT_SYNC_ISOCHRONOUS */
87 #endif /* SUPPORT_BRD_ISOCHRONOUS || SUPPORT_SYNC_ISOCHRONOUS */
88 #endif /*SUPPORT_BLE*/
89 #if SUPPORT_BLE
90 	HCI_HANDLER, /* handler for the HCI events; handling events from Host to HCI*/
91 #endif /*SUPPORT_BLE*/
92 #if SUPPORT_BLE
93 #if SUPPORT_LE_EXTENDED_ADVERTISING
94 	ADV_TIMEOUT_EVENT, 	/*handler for advertising extended timeout feature */
95 	SCN_DURATION_EVENT,	/*handler for extended scanning duration */
96 	SCN_PERIOD_EVENT, 	/*handler for extended scanning period */
97 #if SUPPORT_LE_PERIODIC_ADVERTISING
98 	PRDC_SCAN_TIMEOUT_EVENT, /*handler for periodic scan sync timeout */
99 	PRDC_SCAN_CANCEL_EVENT,
100 #endif /* SUPPORT_LE_PERIODIC_ADVERTISING */
101 #if SUPPORT_LE_PAWR_ADVERTISER_ROLE
102 	PAWR_SEND_FRST_REQ,
103 #endif /* SUPPORT_LE_PAWR_ADVERTISER_ROLE */
104 #endif /* SUPPORT_LE_EXTENDED_ADVERTISING */
105 #endif /*SUPPORT_BLE*/
106 #if SUPPORT_COEXISTENCE
107 	COEX_TIMER_EVENT,
108 #endif
109 #if SUPPORT_MAC
110 	RADIO_MAC_TX_DONE_EVENT,
111 	RAL_SM_DONE_EVENT,
112 	MAC_SM_DONE_EVENT,
113 	ED_TMR_EVENT,
114 #endif /*SUPPORT_MAC*/
115 #if ((SUPPORT_BLE)||(SUPPORT_MAC_HCI_UART)||(SUPPORT_ANT_HCI_UART) || (SUPPORT_AUG_MAC_HCI_UART))
116 	HCI_TRANSPORT_HANDLER, /* handler for the HCI transport events; handling events from HCI to Host */
117 #endif /*SUPPORT_BLE*/
118 #if (SUPPORT_HCI_EVENT_ONLY)
119 	GENERIC_EVENT,
120 #if SUPPORT_SYNC_ISOCHRONOUS || SUPPORT_CONNECTED_ISOCHRONOUS
121 	ISO_DATA_EVENT,
122 #endif /* SUPPORT_SYNC_ISOCHRONOUS || SUPPORT_CONNECTED_ISOCHRONOUS */
123 #if SUPPORT_MASTER_CONNECTION || SUPPORT_SLAVE_CONNECTION
124 	ACL_DATA_EVENT,
125 #endif /* SUPPORT_MASTER_CONNECTION || SUPPORT_SLAVE_CONNECTION */
126 #if SUPPORT_EXPLCT_OBSERVER_ROLE || SUPPORT_MASTER_CONNECTION || SUPPORT_SYNC_ISOCHRONOUS  || (SUPPORT_AOA_AOD && SUPPORT_SLAVE_CONNECTION)
127 	ADV_REPORT_EVENT,
128 #endif /* SUPPORT_EXPLCT_OBSERVER_ROLE || SUPPORT_MASTER_CONNECTION || SUPPORT_SYNC_ISOCHRONOUS  || (SUPPORT_AOA_AOD && SUPPORT_SLAVE_CONNECTION) */
129 #if (SUPPORT_CONNECTED_ISOCHRONOUS && (SUPPORT_MASTER_CONNECTION || SUPPORT_SLAVE_CONNECTION) || (SUPPORT_BRD_ISOCHRONOUS) || (SUPPORT_SYNC_ISOCHRONOUS))
130 	HCI_SYNC_EVENT,
131 #endif /* SYNC_EVENT */
132 #if END_OF_RADIO_ACTIVITY_REPORTING
133 	HCI_RADIO_ACTIVITY_EVENT,
134 #endif /* END_OF_RADIO_ACTIVITY_REPORTING */
135 #endif /* SUPPORT_HCI_EVENT_ONLY */
136 #if ((SUPPORT_MAC) && (MAC_LAYER_BUILD))
137 	MLME_TIMER_EVENT,
138 	DIRECT_DATA_TX_EVENT,
139 	INDIRECT_DATA_TIMEOUT_EVENT,
140 #if SUPPORT_MAC_HCI_UART
141 	MAC_HCI_HANDLER,
142 #endif /* SUPPORT_MAC_HCI_UART */
143 #endif/*((SUPPORT_MAC) && (MAC_LAYER_BUILD)) */
144 #if ((!SUPPORT_COEXISTENCE) &&  (SUPPORT_OPENTHREAD_1_2 && CONFIG_MAC_CSL_RECEIVER_ENABLE))
145 	CSL_RCV_TMR_EVENT,
146 #endif /*((!SUPPORT_COEXISTENCE) &&  (SUPPORT_OPENTHREAD_1_2 && CONFIG_MAC_CSL_RECEIVER_ENABLE))*/
147 #if(!SUPPORT_COEXISTENCE && DEFAULT_PHY_CALIBRATION_PERIOD && SUPPORT_MAC)
148 	PRDC_CLBR_TMR_EVENT,
149 #endif
150 #if SUPPORT_AUG_MAC_HCI_UART
151 	AUG_MAC_HCI_HANDLER,
152 #endif/*SUPPORT_AUG_MAC_HCI_UART */
153 #if SUPPORT_ANT
154 	ANT_RADIO_CMPLT_EVENT,
155 	ANT_STACK_TASK_EVENT,
156 #if SUPPORT_ANT_HCI_UART
157 	ANT_HCI_HANDLER,
158 #endif /*SUPPORT_ANT_TESTING */
159 #endif  /* SUPPORT_ANT */
160 #if (NUM_OF_CTSM_EMNGR_HNDLS >= 1)
161 	CUSTOM_HANDLE_1,
162 #endif
163 #if (NUM_OF_CTSM_EMNGR_HNDLS >= 2)
164 	CUSTOM_HANDLE_2,
165 #endif
166 #if (NUM_OF_CTSM_EMNGR_HNDLS >= 3)
167 	CUSTOM_HANDLE_3,
168 #endif
169 	MAX_EM_HANDLE
170 } handler_t;
171 /* Exported functions ------------------------------------------------------- */
172 
173 
174 
175 /**
176  * @brief  Used to initialize the event manager component.
177  *
178  * @param  None.
179  *
180  * @retval None.
181  */
182 void emngr_init(void);
183 
184 /**
185  * @brief  Used to reset the event manager structure.
186  *
187  * @param  None.
188  *
189  * @retval Status : 0: SUCCESS. Otherwise: FAILED.
190  */
191 int emngr_reset(void);
192 
193 /** @ingroup event_manager_functions
194  * @{
195  */
196 /**
197  * @brief  Used to initialize a certain handle in the event manager.
198  *
199  * @param  id  : [in] ID of the handle to be initialized. [Range: 0 to 255].
200  * @param  max : [in] Maximum number of events that can be added to this handle. [Range: 0 to 255].
201  * @param  call_back_fun : [in] Pointer to the function that will process the events of that handle.
202  *
203  * @retval Status : 0: SUCCESS. Otherwise: FAILED.
204  */
205 int emngr_handle_init(unsigned char id, unsigned char max,
206 	void (*call_back_fun)(void *));
207 
208 /**
209  * @brief  Used to remove a certain handle from the event manager.
210  *
211  * @param  id : [in] ID of the handle to be removed. [Range: 0 to 255].
212  *
213  * @retval Status : 0: SUCCESS. Otherwise: FAILED.
214  */
215 int emngr_handle_remove(
216 	unsigned char id);
217 
218 /**
219  * @brief  Used to post an event to a certain event manager handle that is identified by the handle ID.
220  *
221  * @param  id 		: [in] ID of the handle to which the event will be posted. [Range: 0 to 255].
222  * @param  event 	: [in] Void pointer pointing to the data related to the posted event.
223  *
224  * @retval Status : 0: SUCCESS. Otherwise: FAILED.
225  */
226 int emngr_post_event(unsigned char id, void *event);
227 
228 /**
229  * @brief  Used to post an event to a certain event manager handle that is identified by the handle ID.
230  * 		   This Event will be handled first
231  *
232  * @param  id 		: [in] ID of the handle to which the event will be posted. [Range: 0 to 255].
233  * @param  event 	: [in] Void pointer pointing to the data related to the posted event.
234  *
235  * @retval Status : 0: SUCCESS. Otherwise: FAILED.
236  */
237 int emngr_post_event_first(unsigned char id ,void *event);
238 
239 /**
240  * @brief  Used to get an event from a certain event manager handle that is identified by the handle ID.
241  *
242  * @param  id : [in] ID of the handle whose events are to be gotten. [Range: 0 to 255].
243  *
244  * @retval event handle : Void pointer pointing to the returned event.
245  */
246 void * emngr_get_event(unsigned char id);
247 
248 /**
249  * @brief  Used to process an event from a a certain event manager handle that is identified by the handle ID, by first getting the event (in case the handle contains any) then processing it through the associated callback function.
250  *
251  * @param  id : [in] ID of the handle whose events are to be processed. [Range: 0 to 255].
252  *
253  * @retval Status : 0: SUCCESS. Otherwise: FAILED.
254  */
255 int emngr_process_event(unsigned char id);
256 
257 /**
258  * @brief  Used to return the number of events in the event manager.
259  *
260  * @param  None.
261  *
262  * @retval Events Number.
263  */
264 int emngr_get_total_events(void);
265 
266 /**
267  * @brief  Used to return the number of events in a certain handle that is identified by the handle ID.
268  *
269  * @param  id : [in] ID of the handle whose events number is to be returned. [Range: 0 to 255].
270  *
271  * @retval Events Number per handle.
272  */
273 int emngr_get_handle_events(unsigned char id);
274 
275 /**
276  * @brief  Used to set a flag "busy_flag" to determine whether a certain handle is busy processing an event.
277  *
278  * @param  id 			: [in] ID of the handle whose "busy_flag" parameter value is to be set. [Range: 0 to 255].
279  * @param  busy_flag  	: [in] Value to be set to the "busy_flag" parameter. 0: EVENT_NOT_BUSY. 1: EVENT_BUSY.
280  *
281  * @retval None.
282  */
283 void emngr_set_event_handle_busy(unsigned char id, unsigned char busy_flag);
284 
285 /**
286  * @brief  Used to return the state of the event "busy_flag", which indicates whether the events of a certain handle, identified by the handle ID, are being currently processed.
287  *
288  * @param  id : [in] ID of the handle whose events are being currently processed. [Range: 0 to 255].
289  *
290  * @retval busy_flag state : 0: EVENT_NOT_BUSY. 1: EVENT_BUSY.
291  */
292 int emngr_is_event_busy(
293 		unsigned char id);
294 
295 /**
296  * @brief  Used to loop through all the registered handles in the event manager and process their events, if any exists.
297  *
298  * @param None.
299  *
300  * @retval None.
301  */
302 void emngr_handle_all_events(void);
303 
304 /**
305  * @brief  Used to process an event of a certain event manager handle that is identified by the handle ID.
306  *
307  * @param  id : [in] ID of the handle whose event is to be processed. [Range: 0 to 255].
308  *
309  * @retval None.
310  */
311 void emngr_handle_event(handler_t id);
312 
313 /**
314  * @brief  Used to return a pointer to the first event in a certain event manager handle, identified by the handle ID, without dequeuing that event.
315  *
316  * @param  id : [in] ID of the handle whose first event is to be returned without being removed from the handle queue. [Range: 0 to 255].
317  *
318  * @retval : void Pointer to the first event of the specified handle.
319  */
320 void * emngr_peak_frst_event(unsigned char id);
321 
322 /**
323  * @brief  Used to delete an event from  a certain event manager handle, identified by the handle ID.
324  *
325  * @param  id 	: [in] ID of the handle whose one of its events is to be deleted. [Range: 0 to 255].
326  * @param  data : [in] Pointer to the event data to be used for searching for the event to be deleted out of all the events in the specified handle.
327  *
328  * @retval handle : Void pointer to the event data of the event node that is being deleted.
329  */
330 void * emngr_del_event(unsigned char id, void *data);
331 
332 
333 /* @breif Used to remove a certain event within a queue that satisfies the condition set by cbk
334  * 		  the cbk can delete priv data if it has allocated data.
335  * @param id: [In] ID of the queue to search for
336  * @param only_one_event: [In] if False delete all events that satisfies the condition
337  * @param conitional_data: [In] Optional pointer to pass to cbk along with priv data
338  * @param cbk: [In] A callback function that returns True if the event priv data satisfies a condition
339  * @retval True if any event was found and removed
340  * */
341 uint8_t emngr_remove_conditional_event( uint8_t id, uint8_t only_one_event,
342 		void* conditional_data,  conditional_cbk cbk);
343 
344 
345 /*
346  * @brief Check if MCU can sleep in the case that all events in the event manager are busy and
347  * 		  of type that allows sleep while busy.
348  * @retval True if MCU can sleep.
349  * */
350 uint8_t emngr_can_mcu_sleep(void);
351 
352 /* @breif Used to process a certain event within a queue that satisfies the condition set by cbk
353  * @param id: [In] ID of the queue to search for
354  * @param only_one_event: [In] if False process all events that satisfies the condition
355  * @param conitional_data: [In] Optional pointer to pass to cbk along with priv data
356  * @param cbk: [In] A callback function that returns True if the event priv data satisfies a condition
357  * */
358 uint8_t emngr_process_conditional_event( uint8_t id, uint8_t only_one_event,
359 		void* conditional_data,  conditional_cbk cbk);
360 
361 /**
362  * @}
363  */
364 
365 #endif /* INCLUDE_EVENT_MANAGER_H_ */
366