1 /**
2  ****************************************************************************************
3  *
4  * @file prf.h
5  *
6  * @brief Entry point of profile header file.
7  *
8  * Used to manage life cycle of profiles
9  *
10  * Copyright (C) RivieraWaves 2009-2016
11  *
12  ****************************************************************************************
13  */
14 
15 
16 #ifndef _PRF_H_
17 #define _PRF_H_
18 
19 /**
20  ****************************************************************************************
21  * @addtogroup PROFILE PROFILES
22  * @ingroup ROOT
23  * @brief Bluetooth Low Energy Host Profiles
24  *
25  * The PROFILE of the stack contains the profile layers (@ref PROX "PROXIMITY",
26  * @ref HTP "HTP",@ref FIND "FIND ME" @ref BPS "Blood Pressure").
27  ****************************************************************************************
28  */
29 
30 /**
31  ****************************************************************************************
32  * @addtogroup PRF
33  * @ingroup PROFILE
34  * @brief Definitions of Profile management API
35  *
36  * @{
37  ****************************************************************************************
38  */
39 
40 
41 /*
42  * INCLUDE FILES
43  ****************************************************************************************
44  */
45 #include "rwip_config.h"
46 
47 #if (BLE_PROFILES)
48 
49 #include "ke_task.h"
50 #include "gapm_task.h"
51 
52 /*
53  * DEFINES
54  ****************************************************************************************
55  */
56 
57 /**
58  * Profile task fields
59  *
60  *  15   14   13   12   11   10   9    8    7    6    5    4    3    2    1    0
61  * +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
62  * | MI |                               TASK Number                                |
63  * +----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
64  *
65  * Bit [0-14] : Task number value
66  * Bit [15]   : Task is multi-instantiated (Connection index is conveyed)
67  */
68 enum prf_perm_mask
69 {
70     /// Task number value
71     PERM_MASK_PRF_TASK      = 0x7FFF,
72     PERM_POS_PRF_TASK       = 0,
73     /// Task is multi-instantiated
74     PERM_MASK_PRF_MI        = 0x8000,
75     PERM_POS_PRF_MI         = 15,
76 };
77 /*
78  * TYPE DEFINITIONS
79  ****************************************************************************************
80  */
81 
82 
83 /// Profile Environment Data
84 typedef struct prf_env
85 {
86     /// Application Task Number - if MSB bit set, Multi-Instantiated task
87     ke_task_id_t app_task;
88     /// Profile Task  Number    - if MSB bit set, Multi-Instantiated task
89     ke_task_id_t prf_task;
90 } prf_env_t;
91 
92 
93 
94 /// Profile task environment variable definition to dynamically allocate a Task.
95 struct prf_task_env
96 {
97     /// Profile Task description
98     struct ke_task_desc desc;
99     /// pointer to the allocated memory used by profile during runtime.
100     prf_env_t*          env;
101     /// Profile Task Number
102     ke_task_id_t        task;
103     /// Profile Task Identifier
104     ke_task_id_t        id;
105 };
106 
107 /**
108  ****************************************************************************************
109  * @brief Initialization of the Profile module.
110  * This function performs all the initializations of the Profile module.
111  *  - Creation of database (if it's a service)
112  *  - Allocation of profile required memory
113  *  - Initialization of task descriptor to register application
114  *      - Task State array
115  *      - Number of tasks
116  *      - Default task handler
117  *
118  * @param[out]    env        Collector or Service allocated environment data.
119  * @param[in|out] start_hdl  Service start handle (0 - dynamically allocated), only applies for services.
120  * @param[in]     app_task   Application task number.
121  * @param[in]     sec_lvl    Security level (AUTH, EKS and MI field of @see enum attm_value_perm_mask)
122  * @param[in]     param      Configuration parameters of profile collector or service (32 bits aligned)
123  *
124  * @return status code to know if profile initialization succeed or not.
125  ****************************************************************************************
126  */
127 typedef uint8_t (*prf_init_fnct)    (struct prf_task_env* env, uint16_t* start_hdl, uint16_t app_task, uint8_t sec_lvl,  void* params);
128 
129 /**
130  ****************************************************************************************
131  * @brief Destruction of the Profile module - due to a reset for instance.
132  * This function clean-up allocated memory (attribute database is destroyed by another
133  * procedure)
134  *
135  * @param[in|out]    env        Collector or Service allocated environment data.
136  ****************************************************************************************
137  */
138 typedef void    (*prf_destroy_fnct) (struct prf_task_env* env);
139 
140 /**
141  ****************************************************************************************
142  * @brief Handles Connection creation
143  *
144  * @param[in|out]    env        Collector or Service allocated environment data.
145  * @param[in]        conidx     Connection index
146  ****************************************************************************************
147  */
148 typedef void    (*prf_create_fnct)  (struct prf_task_env* env, uint8_t conidx);
149 
150 /**
151  ****************************************************************************************
152  * @brief Handles Disconnection
153  *
154  * @param[in|out]    env        Collector or Service allocated environment data.
155  * @param[in]        conidx     Connection index
156  * @param[in]        reason     Detach reason
157  ****************************************************************************************
158  */
159 typedef void    (*prf_cleanup_fnct) (struct prf_task_env* env, uint8_t conidx, uint8_t reason);
160 
161 /// Profile task callbacks.
162 struct prf_task_cbs
163 {
164     /// Initialization callback
165     prf_init_fnct    init;
166     /// Destroy profile callback
167     prf_destroy_fnct destroy;
168     /// Connection callback
169     prf_create_fnct  create;
170     /// Disconnection callback
171     prf_cleanup_fnct cleanup;
172 };
173 
174 
175 /// Profile Manager environment structure
176 struct prf_env_tag
177 {
178     /// Array of profile tasks that can be managed by Profile manager.
179     struct prf_task_env prf[BLE_NB_PROFILES];
180 };
181 
182 /*
183  * MACROS
184  ****************************************************************************************
185  */
186 
187 
188 /*
189  * GLOBAL VARIABLE DECLARATIONS
190  ****************************************************************************************
191  */
192 extern struct prf_env_tag prf_env;
193 
194 /*
195  * FUNCTION DECLARATIONS
196  ****************************************************************************************
197  */
198 
199 /**
200  ****************************************************************************************
201  * @brief Perform Profile initialization
202  *
203  * @param[in] reset        Reset requested or basic initialization
204  ****************************************************************************************
205  */
206 void prf_init(bool reset);
207 
208 
209 /**
210  ****************************************************************************************
211  * @brief Create Profile (collector or service) task creation and initialize it.
212  *
213  * @param[in|out] params   Collector or Service parameter used for profile task creation
214  * @param[out]    prf_task Allocated Task number
215  *
216  * @return status of adding profile task
217  ****************************************************************************************
218  */
219 uint8_t prf_add_profile(struct gapm_profile_task_add_cmd * params, ke_task_id_t *prf_task);
220 
221 
222 /**
223  ****************************************************************************************
224  * @brief Link creation event, update profiles states.
225  *
226  * @param[in] conidx        connection index
227  *
228  ****************************************************************************************
229  */
230 void prf_create(uint8_t conidx);
231 
232 /**
233  ****************************************************************************************
234  * @brief Link disconnection event, clean-up profiles.
235  *
236  * @param[in] conidx        connection index
237  * @param[in] reason        detach reason
238  *
239  ****************************************************************************************
240  */
241 void prf_cleanup(uint8_t conidx, uint8_t reason);
242 
243 
244 
245 /**
246  ****************************************************************************************
247  * @brief Retrieve environment variable allocated for a profile
248  *
249  * @param[in] prf_id        Profile Task Identifier
250  *
251  * @return Environment variable allocated for a profile
252  ****************************************************************************************
253  */
254 prf_env_t* prf_env_get(uint16_t prf_id);
255 
256 
257 /**
258  ****************************************************************************************
259  * @brief Retrieve source profile task number value
260  *
261  * @param[in] env     Profile Environment
262  * @param[in] conidx  Connection index
263  *
264  * @return Source profile task number value
265  ****************************************************************************************
266  */
267 ke_task_id_t prf_src_task_get(prf_env_t* env, uint8_t conidx);
268 
269 /**
270  ****************************************************************************************
271  * @brief Retrieve destination application task number value
272  *
273  * @param[in] env     Profile Environment
274  * @param[in] conidx  Connection index
275  *
276  * @return Destination application task number value
277  ****************************************************************************************
278  */
279 ke_task_id_t prf_dst_task_get(prf_env_t* env, uint8_t conidx);
280 
281 
282 /**
283  ****************************************************************************************
284  * @brief Retrieve Task Identifier from Task number
285  * (automatically update index of task in returned task id)
286  *
287  * @param task Task number
288  * @return Task Identifier
289  ****************************************************************************************
290  */
291 ke_task_id_t prf_get_id_from_task(ke_msg_id_t task);
292 
293 /**
294  ****************************************************************************************
295  * @brief Retrieve Task Number from Task Identifier
296  * (automatically update index of task in returned task id)
297  *
298  * @param id Task Identifier
299  * @return Task Number
300  ****************************************************************************************
301  */
302 ke_task_id_t prf_get_task_from_id(ke_msg_id_t id);
303 
304 #endif // (BLE_PROFILES)
305 
306 /// @} PRF
307 
308 #endif /* _PRF_H_ */
309