1 /*
2  * Copyright 2019 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #ifndef P2G4_FUNC_QUEUE_H
7 #define P2G4_FUNC_QUEUE_H
8 
9 #include "bs_types.h"
10 
11 #ifdef __cplusplus
12 extern "C"{
13 #endif
14 
15 typedef void (*queable_f)(uint dev_nbr);
16 
17 /**
18  * Functions (types of events) which can be queued
19  * Ordered by priority: last to first */
20 typedef enum {
21   State_None = 0,
22   Wait_Done,
23   Tx_End,
24   Tx_Packet_End,
25   Rx_CCA_meas,
26   RSSI_Meas,
27   Rx_Found,
28   Rx_Sync,
29   Rx_Header,
30   Rx_Payload,
31   Tx_Abort_Reeval,
32   Tx_Packet_Start,
33   Rx_Search_reeval,
34   Rx_Search_start,
35   Tx_Start, /* For a piggybacking/prelocked Rx to work without pretruncation, we need its Tx to start first */
36   N_funcs //Minor issue: one too many
37 } f_index_t;
38 //Note: We need to use these indexes, instead of just keeping the function pointers
39 //to be able to set the order between the functions
40 
41 /* Queue element time */
42 typedef struct {
43   bs_time_t time; /* Simualted time when the call should be done */
44   f_index_t f_index; /* Function type to be called */
45 } fq_element_t;
46 
47 /**
48  * @brief Initialize the function queue
49  *
50  * @param n_devs Number of devices we are connected to
51  */
52 void fq_init(uint32_t n_devs);
53 
54 /**
55  * Register which function will be called for a type of event
56  *
57  * @param index Type of event/function for which to register
58  * @param fptr Function pointer to call when that event is due
59  */
60 void fq_register_func(f_index_t index, queable_f fptr);
61 
62 /**
63  * Add (modify) an entry in the queue for a given device
64  *
65  * @param time When is the function meant to be run
66  * @parm index Which function to run
67  * @param dev_nbr For which device interface
68  */
69 void fq_add(bs_time_t time, f_index_t index, uint32_t dev_nbr);
70 
71 /**
72  * Get the simulated time, in microseconds, of the next scheduled function
73  */
74 bs_time_t fq_get_next_time();
75 
76 /**
77  * Call the next function in the queue
78  * Note: The function itself is left in the queue.
79  */
80 void fq_call_next();
81 
82 /**
83  * Find and update the next function which should be executed
84  */
85 void fq_find_next();
86 
87 /**
88  * Remove whichever entry may be queued for this interface
89  * (and find the next one)
90  *
91  * It is safe to call it on an interface which does not have anything queued
92  *
93  * Note that it is the responsibility of the user to either update an entry
94  * after it has triggered, or to remove it. Otherwise the same entry will
95  * stay on the top, being the next one all the time.
96  *
97  * @param dev_nbr Which device interface
98  */
99 void fq_remove(uint32_t dev_nbr);
100 
101 /**
102  * Free resources allocated by the function queue
103  *
104  * This must be called before exiting to clean up any memory allocated by the
105  * queue
106  */
107 void fq_free();
108 
109 #ifdef __cplusplus
110 }
111 #endif
112 
113 #endif
114