1 /**
2  ****************************************************************************************
3  *
4  * @file prf_utils.h
5  *
6  * @brief  Header file - Profile Utilities
7  *
8  * Copyright (C) RivieraWaves 2009-2016
9  *
10  *
11  ****************************************************************************************
12  */
13 
14 
15 #ifndef _PRF_UTILS_H_
16 #define _PRF_UTILS_H_
17 
18 /**
19  ****************************************************************************************
20  * @addtogroup PRF_UTILS
21  * @ingroup PROFILE
22  *
23  * @brief Definitions of shared profiles functions that can be used by several profiles
24  *
25  * @{
26  ****************************************************************************************
27  */
28 
29 
30 /*
31  * INCLUDE FILES
32  ****************************************************************************************
33  */
34 
35 #include "rwip_config.h"
36 #if (BLE_SERVER_PRF || BLE_CLIENT_PRF)
37 #include "ke_msg.h"
38 #include "prf_types.h"
39 #include "gattc_task.h"
40 #include "gapc.h"
41 #include "gapc_task.h"
42 #include "attm.h"
43 #include "prf.h"
44 #endif /* (BLE_SERVER_PRF || BLE_CLIENT_PRF) */
45 
46 /*
47  * MACROS
48  ****************************************************************************************
49  */
50 
51 /**
52  ****************************************************************************************
53  * @brief Macro used to called the prf_client_get_env function.
54  *
55  * @param prf_id    Profile Task ID   (In Upper case, ex: HTPC, DISC, ...)
56  * @param type      Profile task type (In lower case, ex: htpc, disc, ...)
57  ****************************************************************************************
58  */
59 #define PRF_ENV_GET(prf_id, type) \
60         ((struct type ## _env_tag *)prf_env_get((TASK_ID_##prf_id)))
61 
62 
63 
64 
65 
66 /*
67  * FUNCTION DECLARATIONS
68  ****************************************************************************************
69  */
70 
71 #if (BLE_BATT_SERVER)
72 /**
73  ****************************************************************************************
74  * @brief Pack Characteristic Presentation Format descriptor value
75  ****************************************************************************************
76  */
77 void prf_pack_char_pres_fmt(uint8_t *packed_val, const struct prf_char_pres_fmt* char_pres_fmt);
78 #endif // (BLE_BATT_SERVER)
79 
80 #if (BLE_BATT_CLIENT)
81 /**
82  ****************************************************************************************
83  * @brief Unpack Characteristic Presentation Format descriptor value
84  ****************************************************************************************
85  */
86 void prf_unpack_char_pres_fmt(const uint8_t *packed_val, struct prf_char_pres_fmt* char_pres_fmt);
87 #endif // (BLE_BATT_CLIENT)
88 
89 #if (BLE_CLIENT_PRF)
90 /**
91  ****************************************************************************************
92  * @brief Request  peer device to read an attribute
93  *
94  * @param[in] prf_env Pointer to profile information
95  *
96  * @param conidx   Connection index
97  * @param shdl     Search Start Handle
98  * @param ehdl     Search End Handle
99  *
100  * @param valhdl   Value Handle
101  *
102  * @note: if attribute is invalid, nothing is registered
103  ****************************************************************************************
104  */
105 void prf_read_char_send(prf_env_t *prf_env, uint8_t conidx,
106                         uint16_t shdl, uint16_t ehdl, uint16_t valhdl);
107 
108 
109 /**
110  ****************************************************************************************
111  * @brief register attribute handle in GATT
112  *
113  * @param[in] prf_env Pointer to profile information
114  *
115  * @param conidx   Connection index
116  * @param svc   Service to register
117  *
118  * @note: if attribute is invalid, nothing is registered
119  ****************************************************************************************
120  */
121 void prf_register_atthdl2gatt(prf_env_t *prf_env, uint8_t conidx, struct prf_svc *svc);
122 
123 /**
124  ****************************************************************************************
125  * @brief Unregister attribute handle in GATT
126  *
127  * @param[in] prf_env Pointer to profile information
128  *
129  * @param conidx   Connection index
130  * @param svc   Service to register
131  *
132  * @note: if attribute is invalid, nothing is registered
133  ****************************************************************************************
134  */
135 void prf_unregister_atthdl2gatt(prf_env_t *prf_env, uint8_t conidx, struct prf_svc *svc);
136 
137 /**
138  ****************************************************************************************
139  * @brief Request service discovery on peer device.
140  *
141  * This request will be used to retrieve start and end handles of the service.
142  *
143  * @param[in] prf_env Pointer to profile information
144  *
145  * @param conidx   Connection index
146  * @param uuid     Service UUID
147  ****************************************************************************************
148  */
149 void prf_disc_svc_send(prf_env_t *prf_env,uint8_t conidx,  uint16_t uuid);
150 
151 
152 /**
153  ****************************************************************************************
154  * @brief Write peer characteristic using GATT.
155  *
156  * It will request write modification of peer handle
157  *
158  * @param[in] prf_env Pointer to profile information
159  * @param[in] conidx   Connection index
160  * @param[in] handle Peer handle to modify
161  * @param[in] value  New Peer handle value
162  * @param[in] length Value length
163  ****************************************************************************************
164  */
165 void prf_gatt_write(prf_env_t *prf_env, uint8_t conidx,
166                     uint16_t handle, uint8_t* value, uint16_t length, uint8_t operation);
167 
168 /**
169  ****************************************************************************************
170  * @brief Modify peer client configuration descriptor using GATT
171  *
172  * It will request write modification of peer client configuration descriptor handle
173  *
174  * @param[in] prf_env Pointer to profile information
175  *
176  * @param[in] conidx   Connection index
177  * @param[in] handle Peer client configuration descriptor handle to modify
178  *
179  * @param[in] ntf_ind_cfg  Indication/Notification configuration
180  ****************************************************************************************
181  */
182 void prf_gatt_write_ntf_ind(prf_env_t *prf_env, uint8_t conidx, uint16_t handle,
183         uint16_t ntf_ind_cfg);
184 
185 /**
186  ****************************************************************************************
187  * @brief Check service characteristic validity
188  *
189  * For each characteristic in service it verifies handles.
190  *
191  * If some handles are not present, it checks if they shall be present or they are optional.
192  *
193  * @param nb_chars   Number of Characteristics in the service
194  * @param chars      Characteristics values (char handles, val handles, properties)
195  * @param chars_req  Characteristics requirements.
196  *
197  * @return 0x1 if service is valid, 0x00 else.
198  ****************************************************************************************
199  */
200 uint8_t prf_check_svc_char_validity(uint8_t nb_chars,
201                                     const struct prf_char_inf* chars,
202                                     const struct prf_char_def* chars_req);
203 
204 /**
205  ****************************************************************************************
206  * @brief Check service characteristic validity
207  *
208  * For each characteristic in service it verifies handles.
209  *
210  * If some handles are not present, it checks if they shall be present or they are optional.
211  *
212  * @param nb_chars   Number of Characteristics in the service
213  * @param chars      Characteristics values (char handles, val handles, properties)
214  * @param chars_req  Characteristics requirements.
215  *
216  * @return 0x1 if service is valid, 0x00 else.
217  ****************************************************************************************
218  */
219 uint8_t prf_check_svc_char_uuid128_validity(uint8_t nb_chars,
220                                     const struct prf_char_inf* chars,
221                                     const struct prf_char_uuid128_def* chars_req);
222 
223 /**
224  ****************************************************************************************
225  * @brief Check service characteristic descriptors validity
226  *
227  * For each characteristic descriptors in service it verifies handles.
228  *
229  * If some handles are not present, according to characteristic properties it verify if
230  * descriptor is optional or not.
231  *
232  * @param descs_size Number of Characteristic descriptors in the service
233  * @param descs      Characteristic descriptors values (handles)
234  * @param descs_req  Characteristics descriptors requirements.
235  *
236  * @return 0x1 if service is valid, 0x00 else.
237  ****************************************************************************************
238  */
239 uint8_t prf_check_svc_char_desc_validity(uint8_t descs_size,
240                                         const struct prf_char_desc_inf* descs,
241                                         const struct prf_char_desc_def* descs_req,
242                                         const struct prf_char_inf* chars);
243 
244 /**
245  ****************************************************************************************
246  * @brief Check service characteristic descriptors validity
247  *
248  * For each characteristic descriptors in service it verifies handles.
249  *
250  * If some handles are not present, according to characteristic properties it verify if
251  * descriptor is optional or not.
252  *
253  * @param descs_size Number of Characteristic descriptors in the service
254  * @param descs      Characteristic descriptors values (handles)
255  * @param descs_req  Characteristics descriptors requirements.
256  *
257  * @return 0x1 if service is valid, 0x00 else.
258  ****************************************************************************************
259  */
260 uint8_t prf_check_svc_char_desc_uuid128_validity(uint8_t descs_size,
261                                         const struct prf_char_desc_inf* descs,
262                                         const struct prf_char_desc_uuid128_def* descs_req,
263                                         const struct prf_char_inf* chars);
264 
265 /**
266  ****************************************************************************************
267  * @brief Extract information of the service according to the service description
268  *
269  * @param param            Service information
270  * @param nb_chars         Length of provided arrays (chars and chars_req)
271  * @param chars_req        Characteristics requirements
272  * @param chars            Characteristics
273  * @param nb_descs         Length of provided arrays (descs and descs_req)
274  * @param descs_req        Descriptors requirements
275  * @param descs            Descriptors
276  ****************************************************************************************
277  */
278 void prf_extract_svc_info(const struct gattc_sdp_svc_ind* param,
279         uint8_t nb_chars, const struct prf_char_def* chars_req, struct prf_char_inf* chars,
280         uint8_t nb_descs, const struct prf_char_desc_def* descs_req, struct prf_char_desc_inf* descs);
281 
282 /**
283  ****************************************************************************************
284  * @brief Extract information of the service according to the service description
285  *
286  * @param param            Service information
287  * @param nb_chars         Length of provided arrays (chars and chars_req)
288  * @param chars_req        Characteristics requirements in UUID128 format
289  * @param chars            Characteristics
290  * @param nb_descs         Length of provided arrays (descs and descs_req)
291  * @param descs_req        Descriptors requirements in UUID128 format
292  * @param descs            Descriptors
293  ****************************************************************************************
294  */
295 void prf_extract_svc_uuid128_info(const struct gattc_sdp_svc_ind* param,
296         uint8_t nb_chars, const struct prf_char_uuid128_def* chars_uuid128_req, struct prf_char_inf* chars,
297         uint8_t nb_descs, const struct prf_char_desc_uuid128_def* descs_uuid128_req, struct prf_char_desc_inf* descs);
298 
299 #endif //(BLE_CLIENT_PRF)
300 
301 
302 #if (BLE_CLIENT_PRF || BLE_TIP_SERVER || BLE_AN_SERVER || BLE_PAS_SERVER)
303 
304 /**
305  ****************************************************************************************
306  * @brief The function is used to send information about peer attribute value
307  *
308  * @param[in] prf_env       Pointer to the profile environment variable
309  * @param[in] conidx        Connection index
310  * @param[in] msg_id        Profile message ID to trigger
311  * @param[in] status        Response status code
312  * @param[in] read_ind      GATT read message indication
313  ****************************************************************************************
314  */
315 void prf_client_att_info_rsp(prf_env_t *prf_env, uint8_t conidx, uint16_t msg_id,
316                              uint8_t status, struct gattc_read_ind const* read_ind);
317 
318 #endif //(BLE_CLIENT_PRF || BLE_TIP_SERVER || BLE_AN_SERVER || BLE_PAS_SERVER)
319 
320 #if (BLE_SERVER_PRF || BLE_CLIENT_PRF)
321 /**
322  ****************************************************************************************
323  * @brief Pack date time value
324  *
325  * @param[out] packed_date packed date time
326  * @param[in] date_time structure date time
327  *
328  * @return size of packed value
329  ****************************************************************************************
330  */
331 uint8_t prf_pack_date_time(uint8_t *packed_date, const struct prf_date_time* date_time);
332 
333 /**
334  ****************************************************************************************
335  * @brief Unpack date time value
336  *
337  * @param[in] packed_date packed date time
338  * @param[out] date_time structure date time
339  *
340  * @return size of packed value
341  ****************************************************************************************
342  */
343 uint8_t prf_unpack_date_time(uint8_t *packed_date, struct prf_date_time* date_time);
344 
345 #endif /* (BLE_SERVER_PRF || BLE_CLIENT_PRF) */
346 
347 /// @} prf_utils
348 
349 #endif /* _PRF_UTILS_H_ */
350