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