1 // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2 //
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 #include <stdlib.h>
16 #include <string.h>
17 #include "btc/btc_task.h"
18 #include "osi/thread.h"
19 #include "esp_log.h"
20 #include "bt_common.h"
21 #include "osi/allocator.h"
22 #include "btc/btc_alarm.h"
23 
24 #ifdef CONFIG_BT_BLUEDROID_ENABLED
25 #include "common/bt_target.h"
26 #include "btc/btc_main.h"
27 #include "btc/btc_manage.h"
28 #include "btc/btc_dev.h"
29 #include "btc_gatts.h"
30 #include "btc_gattc.h"
31 #include "btc_gatt_common.h"
32 #include "btc_gap_ble.h"
33 #include "btc_blufi_prf.h"
34 #include "blufi_int.h"
35 #include "btc/btc_dm.h"
36 #include "bta/bta_gatt_api.h"
37 #if CLASSIC_BT_INCLUDED
38 #include "btc/btc_profile_queue.h"
39 #if (BTC_GAP_BT_INCLUDED == TRUE)
40 #include "btc_gap_bt.h"
41 #endif /* BTC_GAP_BT_INCLUDED == TRUE */
42 #if BTC_AV_INCLUDED
43 #include "btc_av.h"
44 #include "btc_avrc.h"
45 #include "btc_av_co.h"
46 #endif /* #if BTC_AV_INCLUDED */
47 #if (BTC_SPP_INCLUDED == TRUE)
48 #include "btc_spp.h"
49 #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
50 #if BTC_HF_INCLUDED
51 #include "btc_hf_ag.h"
52 #endif/* #if BTC_HF_INCLUDED */
53 #if BTC_HF_CLIENT_INCLUDED
54 #include "btc_hf_client.h"
55 #endif  /* #if BTC_HF_CLIENT_INCLUDED */
56 #endif /* #if CLASSIC_BT_INCLUDED */
57 #endif
58 
59 #if CONFIG_BLE_MESH
60 #include "btc_ble_mesh_ble.h"
61 #include "btc_ble_mesh_prov.h"
62 #include "btc_ble_mesh_health_model.h"
63 #include "btc_ble_mesh_config_model.h"
64 #include "btc_ble_mesh_generic_model.h"
65 #include "btc_ble_mesh_lighting_model.h"
66 #include "btc_ble_mesh_sensor_model.h"
67 #include "btc_ble_mesh_time_scene_model.h"
68 #endif /* #if CONFIG_BLE_MESH */
69 
70 #define BTC_TASK_PINNED_TO_CORE         (TASK_PINNED_TO_CORE)
71 #define BTC_TASK_STACK_SIZE             (BT_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE)	//by menuconfig
72 #define BTC_TASK_NAME                   "BTC_TASK"
73 #define BTC_TASK_PRIO                   (BT_TASK_MAX_PRIORITIES - 6)
74 
75 osi_thread_t *btc_thread;
76 
77 static const btc_func_t profile_tab[BTC_PID_NUM] = {
78 #ifdef CONFIG_BT_BLUEDROID_ENABLED
79     [BTC_PID_MAIN_INIT]   = {btc_main_call_handler,       NULL                    },
80     [BTC_PID_DEV]         = {btc_dev_call_handler,        NULL                    },
81 #if (GATTS_INCLUDED == TRUE)
82     [BTC_PID_GATTS]       = {btc_gatts_call_handler,      btc_gatts_cb_handler    },
83 #endif  ///GATTS_INCLUDED == TRUE
84 #if (GATTC_INCLUDED == TRUE)
85     [BTC_PID_GATTC]       = {btc_gattc_call_handler,      btc_gattc_cb_handler    },
86 #endif  ///GATTC_INCLUDED == TRUE
87 #if (GATTS_INCLUDED == TRUE || GATTC_INCLUDED == TRUE)
88     [BTC_PID_GATT_COMMON] = {btc_gatt_com_call_handler,   NULL                    },
89 #endif //GATTC_INCLUDED == TRUE || GATTS_INCLUDED == TRUE
90 #if (BLE_INCLUDED == TRUE)
91     [BTC_PID_GAP_BLE]     = {btc_gap_ble_call_handler,    btc_gap_ble_cb_handler  },
92 #else
93     [BTC_PID_GAP_BLE]     = {NULL, NULL},
94 #endif  ///BLE_INCLUDED == TRUE
95     [BTC_PID_BLE_HID]     = {NULL, NULL},
96     [BTC_PID_SPPLIKE]     = {NULL, NULL},
97 #if (BLUFI_INCLUDED == TRUE)
98     [BTC_PID_BLUFI]       = {btc_blufi_call_handler,      btc_blufi_cb_handler    },
99 #endif  ///BLUFI_INCLUDED == TRUE
100     [BTC_PID_DM_SEC]      = {NULL,                        btc_dm_sec_cb_handler   },
101 #endif
102     [BTC_PID_ALARM]       = {btc_alarm_handler,           NULL                    },
103 #ifdef CONFIG_BT_BLUEDROID_ENABLED
104 #if CLASSIC_BT_INCLUDED
105 #if (BTC_GAP_BT_INCLUDED == TRUE)
106     [BTC_PID_GAP_BT]    = {btc_gap_bt_call_handler,     btc_gap_bt_cb_handler   },
107 #endif /* (BTC_GAP_BT_INCLUDED == TRUE) */
108     [BTC_PID_PRF_QUE]     = {btc_profile_queue_handler,   NULL                    },
109 #if BTC_AV_INCLUDED
110     [BTC_PID_A2DP]        = {btc_a2dp_call_handler,       btc_a2dp_cb_handler     },
111     [BTC_PID_AVRC_CT]     = {btc_avrc_ct_call_handler,    NULL                    },
112     [BTC_PID_AVRC_TG]     = {btc_avrc_tg_call_handler,    NULL                    },
113 #endif /* #if BTC_AV_INCLUDED */
114 #if (BTC_SPP_INCLUDED == TRUE)
115     [BTC_PID_SPP]         = {btc_spp_call_handler,        btc_spp_cb_handler      },
116 #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
117 #if BTC_HF_INCLUDED
118     [BTC_PID_HF]   = {btc_hf_call_handler,  btc_hf_cb_handler},
119 #endif  /* #if BTC_HF_INCLUDED */
120 #if BTC_HF_CLIENT_INCLUDED
121     [BTC_PID_HF_CLIENT]   = {btc_hf_client_call_handler,  btc_hf_client_cb_handler},
122 #endif  /* #if BTC_HF_CLIENT_INCLUDED */
123 #endif /* #if CLASSIC_BT_INCLUDED */
124 #endif
125 #if CONFIG_BLE_MESH
126     [BTC_PID_PROV]              = {btc_ble_mesh_prov_call_handler,              btc_ble_mesh_prov_cb_handler             },
127     [BTC_PID_MODEL]             = {btc_ble_mesh_model_call_handler,             btc_ble_mesh_model_cb_handler            },
128 #if CONFIG_BLE_MESH_HEALTH_CLI
129     [BTC_PID_HEALTH_CLIENT]     = {btc_ble_mesh_health_client_call_handler,     btc_ble_mesh_health_client_cb_handler    },
130 #endif /* CONFIG_BLE_MESH_HEALTH_CLI */
131 #if CONFIG_BLE_MESH_HEALTH_SRV
132     [BTC_PID_HEALTH_SERVER]     = {btc_ble_mesh_health_server_call_handler,     btc_ble_mesh_health_server_cb_handler    },
133 #endif /* CONFIG_BLE_MESH_HEALTH_SRV */
134 #if CONFIG_BLE_MESH_CFG_CLI
135     [BTC_PID_CONFIG_CLIENT]     = {btc_ble_mesh_config_client_call_handler,     btc_ble_mesh_config_client_cb_handler    },
136 #endif /* CONFIG_BLE_MESH_CFG_CLI */
137     [BTC_PID_CONFIG_SERVER]     = {NULL,                                        btc_ble_mesh_config_server_cb_handler    },
138 #if CONFIG_BLE_MESH_GENERIC_CLIENT
139     [BTC_PID_GENERIC_CLIENT]    = {btc_ble_mesh_generic_client_call_handler,    btc_ble_mesh_generic_client_cb_handler   },
140 #endif /* CONFIG_BLE_MESH_GENERIC_CLIENT */
141 #if CONFIG_BLE_MESH_LIGHTING_CLIENT
142     [BTC_PID_LIGHTING_CLIENT]   = {btc_ble_mesh_lighting_client_call_handler,   btc_ble_mesh_lighting_client_cb_handler  },
143 #endif /* CONFIG_BLE_MESH_LIGHTING_CLIENT */
144 #if CONFIG_BLE_MESH_SENSOR_CLI
145     [BTC_PID_SENSOR_CLIENT]     = {btc_ble_mesh_sensor_client_call_handler,     btc_ble_mesh_sensor_client_cb_handler    },
146 #endif /* CONFIG_BLE_MESH_SENSOR_CLI */
147 #if CONFIG_BLE_MESH_TIME_SCENE_CLIENT
148     [BTC_PID_TIME_SCENE_CLIENT] = {btc_ble_mesh_time_scene_client_call_handler, btc_ble_mesh_time_scene_client_cb_handler},
149 #endif /* CONFIG_BLE_MESH_TIME_SCENE_CLIENT */
150 #if CONFIG_BLE_MESH_GENERIC_SERVER
151     [BTC_PID_GENERIC_SERVER]    = {NULL,                                        btc_ble_mesh_generic_server_cb_handler   },
152 #endif /* CONFIG_BLE_MESH_GENERIC_SERVER */
153 #if CONFIG_BLE_MESH_LIGHTING_SERVER
154     [BTC_PID_LIGHTING_SERVER]   = {NULL,                                        btc_ble_mesh_lighting_server_cb_handler  },
155 #endif /* CONFIG_BLE_MESH_LIGHTING_SERVER */
156 #if CONFIG_BLE_MESH_SENSOR_SERVER
157     [BTC_PID_SENSOR_SERVER]     = {NULL,                                        btc_ble_mesh_sensor_server_cb_handler    },
158 #endif /* CONFIG_BLE_MESH_SENSOR_SERVER */
159 #if CONFIG_BLE_MESH_TIME_SCENE_SERVER
160     [BTC_PID_TIME_SCENE_SERVER] = {NULL,                                        btc_ble_mesh_time_scene_server_cb_handler},
161 #endif /* CONFIG_BLE_MESH_TIME_SCENE_SERVER */
162 #if CONFIG_BLE_MESH_BLE_COEX_SUPPORT
163     [BTC_PID_BLE_MESH_BLE_COEX] = {btc_ble_mesh_ble_call_handler,               btc_ble_mesh_ble_cb_handler              },
164 #endif /* CONFIG_BLE_MESH_BLE_COEX_SUPPORT */
165 #endif /* #if CONFIG_BLE_MESH */
166 };
167 
168 /*****************************************************************************
169 **
170 ** Function         btc_task
171 **
172 ** Description      Process profile Task Thread.
173 ******************************************************************************/
btc_thread_handler(void * arg)174 static void btc_thread_handler(void *arg)
175 {
176     btc_msg_t *msg = (btc_msg_t *)arg;
177 
178     BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, msg->arg);
179     switch (msg->sig) {
180     case BTC_SIG_API_CALL:
181         profile_tab[msg->pid].btc_call(msg);
182         break;
183     case BTC_SIG_API_CB:
184         profile_tab[msg->pid].btc_cb(msg);
185         break;
186     default:
187         break;
188     }
189 
190     if (msg->arg) {
191         osi_free(msg->arg);
192     }
193     osi_free(msg);
194 }
195 
btc_task_post(btc_msg_t * msg,uint32_t timeout)196 static bt_status_t btc_task_post(btc_msg_t *msg, uint32_t timeout)
197 {
198     btc_msg_t *lmsg;
199 
200     lmsg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t));
201     if (lmsg == NULL) {
202         return BT_STATUS_NOMEM;
203     }
204 
205     memcpy(lmsg, msg, sizeof(btc_msg_t));
206 
207     if (osi_thread_post(btc_thread, btc_thread_handler, lmsg, 0, timeout) == false) {
208         return BT_STATUS_BUSY;
209     }
210 
211     return BT_STATUS_SUCCESS;
212 }
213 
214 /**
215  * transfer an message to another module in the different task.
216  * @param  msg       message
217  * @param  arg       paramter
218  * @param  arg_len   length of paramter
219  * @param  copy_func deep copy function
220  * @return           BT_STATUS_SUCCESS: success
221  *                   others: fail
222  */
btc_transfer_context(btc_msg_t * msg,void * arg,int arg_len,btc_arg_deep_copy_t copy_func)223 bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg_deep_copy_t copy_func)
224 {
225     btc_msg_t lmsg;
226 
227     if (msg == NULL) {
228         return BT_STATUS_PARM_INVALID;
229     }
230 
231     BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, arg);
232 
233     memcpy(&lmsg, msg, sizeof(btc_msg_t));
234     if (arg) {
235         lmsg.arg = (void *)osi_malloc(arg_len);
236         if (lmsg.arg == NULL) {
237             return BT_STATUS_NOMEM;
238         }
239         memset(lmsg.arg, 0x00, arg_len);    //important, avoid arg which have no length
240         memcpy(lmsg.arg, arg, arg_len);
241         if (copy_func) {
242             copy_func(&lmsg, lmsg.arg, arg);
243         }
244     } else {
245         lmsg.arg = NULL;
246     }
247 
248     return btc_task_post(&lmsg, OSI_THREAD_MAX_TIMEOUT);
249 
250 }
251 
252 /**
253  * transfer an message to another module in tha same task.
254  * @param  msg       message
255  * @param  arg       paramter
256  * @return           BT_STATUS_SUCCESS: success
257  *                   others: fail
258  */
btc_inter_profile_call(btc_msg_t * msg,void * arg)259 bt_status_t btc_inter_profile_call(btc_msg_t *msg, void *arg)
260 {
261     if (msg == NULL) {
262         return BT_STATUS_PARM_INVALID;
263     }
264 
265     msg->arg = arg;
266     switch (msg->sig) {
267     case BTC_SIG_API_CALL:
268         profile_tab[msg->pid].btc_call(msg);
269         break;
270     case BTC_SIG_API_CB:
271         profile_tab[msg->pid].btc_cb(msg);
272         break;
273     default:
274         break;
275     }
276     return BT_STATUS_SUCCESS;
277 }
278 
279 
280 #if BTC_DYNAMIC_MEMORY
281 
btc_deinit_mem(void)282 static void btc_deinit_mem(void) {
283     if (btc_dm_cb_ptr) {
284         osi_free(btc_dm_cb_ptr);
285         btc_dm_cb_ptr = NULL;
286     }
287 
288     if (btc_profile_cb_tab) {
289         osi_free(btc_profile_cb_tab);
290         btc_profile_cb_tab = NULL;
291     }
292 
293 #if (BLE_INCLUDED == TRUE)
294     if (gl_bta_adv_data_ptr) {
295         osi_free(gl_bta_adv_data_ptr);
296         gl_bta_adv_data_ptr = NULL;
297     }
298 
299     if (gl_bta_scan_rsp_data_ptr) {
300         osi_free(gl_bta_scan_rsp_data_ptr);
301         gl_bta_scan_rsp_data_ptr = NULL;
302     }
303 #endif  ///BLE_INCLUDED == TRUE
304 
305 #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
306     if (btc_creat_tab_env_ptr) {
307         osi_free(btc_creat_tab_env_ptr);
308         btc_creat_tab_env_ptr = NULL;
309     }
310 #if (BLUFI_INCLUDED == TRUE)
311     if (blufi_env_ptr) {
312         osi_free(blufi_env_ptr);
313         blufi_env_ptr = NULL;
314     }
315 #endif
316 #endif
317 
318 #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
319     if (hf_client_local_param_ptr) {
320         osi_free(hf_client_local_param_ptr);
321         hf_client_local_param_ptr = NULL;
322     }
323 #endif
324 
325 #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
326     if (btc_rc_cb_ptr) {
327         osi_free(btc_rc_cb_ptr);
328         btc_rc_cb_ptr = NULL;
329     }
330     if (bta_av_co_cb_ptr) {
331         osi_free(bta_av_co_cb_ptr);
332         bta_av_co_cb_ptr = NULL;
333     }
334 #endif
335 }
336 
btc_init_mem(void)337 static bt_status_t btc_init_mem(void) {
338     if ((btc_dm_cb_ptr = (btc_dm_cb_t *)osi_malloc(sizeof(btc_dm_cb_t))) == NULL) {
339         goto error_exit;
340     }
341     memset((void *)btc_dm_cb_ptr, 0, sizeof(btc_dm_cb_t));
342 
343     if ((btc_profile_cb_tab = (void **)osi_malloc(sizeof(void *) * BTC_PID_NUM)) == NULL) {
344         goto error_exit;
345     }
346     memset((void *)btc_profile_cb_tab, 0, sizeof(void *) * BTC_PID_NUM);
347 
348 #if (BLE_INCLUDED == TRUE)
349     if ((gl_bta_adv_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
350         goto error_exit;
351     }
352     memset((void *)gl_bta_adv_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
353 
354     if ((gl_bta_scan_rsp_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
355         goto error_exit;
356     }
357     memset((void *)gl_bta_scan_rsp_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
358 #endif  ///BLE_INCLUDED == TRUE
359 
360 #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
361     if ((btc_creat_tab_env_ptr = (esp_btc_creat_tab_t *)osi_malloc(sizeof(esp_btc_creat_tab_t))) == NULL) {
362         goto error_exit;
363     }
364     memset((void *)btc_creat_tab_env_ptr, 0, sizeof(esp_btc_creat_tab_t));
365 #if (BLUFI_INCLUDED == TRUE)
366     if ((blufi_env_ptr = (tBLUFI_ENV *)osi_malloc(sizeof(tBLUFI_ENV))) == NULL) {
367         goto error_exit;
368     }
369     memset((void *)blufi_env_ptr, 0, sizeof(tBLUFI_ENV));
370 #endif
371 #endif
372 
373 #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
374     if ((hf_client_local_param_ptr = (hf_client_local_param_t *)osi_malloc(sizeof(hf_client_local_param_t))) == NULL) {
375         goto error_exit;
376     }
377     memset((void *)hf_client_local_param_ptr, 0, sizeof(hf_client_local_param_t));
378 #endif
379 
380 #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
381     if ((btc_rc_cb_ptr = (btc_rc_cb_t *)osi_malloc(sizeof(btc_rc_cb_t))) == NULL) {
382         goto error_exit;
383     }
384     memset((void *)btc_rc_cb_ptr, 0, sizeof(btc_rc_cb_t));
385     if ((bta_av_co_cb_ptr = (tBTA_AV_CO_CB *)osi_malloc(sizeof(tBTA_AV_CO_CB))) == NULL) {
386         goto error_exit;
387     }
388     memset((void *)bta_av_co_cb_ptr, 0, sizeof(tBTA_AV_CO_CB));
389 #endif
390 
391     return BT_STATUS_SUCCESS;
392 
393 error_exit:;
394     btc_deinit_mem();
395     return BT_STATUS_NOMEM;
396 }
397 #endif ///BTC_DYNAMIC_MEMORY
398 
btc_init(void)399 bt_status_t btc_init(void)
400 {
401     btc_thread = osi_thread_create(BTC_TASK_NAME, BTC_TASK_STACK_SIZE, BTC_TASK_PRIO, BTC_TASK_PINNED_TO_CORE, 2);
402     if (btc_thread == NULL) {
403         return BT_STATUS_NOMEM;
404     }
405 
406 #if BTC_DYNAMIC_MEMORY
407     if (btc_init_mem() != BT_STATUS_SUCCESS){
408         return BT_STATUS_NOMEM;
409     }
410 #endif
411 
412 #if (BLE_INCLUDED == TRUE)
413     btc_gap_callback_init();
414 #endif  ///BLE_INCLUDED == TRUE
415 
416 #if SCAN_QUEUE_CONGEST_CHECK
417     btc_adv_list_init();
418 #endif
419     /* TODO: initial the profile_tab */
420     return BT_STATUS_SUCCESS;
421 }
422 
btc_deinit(void)423 void btc_deinit(void)
424 {
425 #if BTC_DYNAMIC_MEMORY
426     btc_deinit_mem();
427 #endif
428 
429     osi_thread_free(btc_thread);
430     btc_thread = NULL;
431 
432 #if SCAN_QUEUE_CONGEST_CHECK
433     btc_adv_list_deinit();
434 #endif
435 }
436 
btc_check_queue_is_congest(void)437 bool btc_check_queue_is_congest(void)
438 {
439     if (osi_thread_queue_wait_size(btc_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {
440         return true;
441     }
442 
443     return false;
444 }
445 
get_btc_work_queue_size(void)446 int get_btc_work_queue_size(void)
447 {
448     return osi_thread_queue_wait_size(btc_thread, 0);
449 }
450