]> granicus.if.org Git - esp-idf/blob - components/bt/common/btc/core/btc_task.c
Merge branch 'feature/hf_ag' into 'master'
[esp-idf] / components / bt / common / btc / core / btc_task.c
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_prov.h"
61 #include "btc_ble_mesh_health_model.h"
62 #include "btc_ble_mesh_config_model.h"
63 #include "btc_ble_mesh_generic_model.h"
64 #include "btc_ble_mesh_lighting_model.h"
65 #include "btc_ble_mesh_sensor_model.h"
66 #include "btc_ble_mesh_time_scene_model.h"
67 #endif /* #if CONFIG_BLE_MESH */
68
69 #define BTC_TASK_PINNED_TO_CORE         (TASK_PINNED_TO_CORE)
70 #define BTC_TASK_STACK_SIZE             (BT_BTC_TASK_STACK_SIZE + BT_TASK_EXTRA_STACK_SIZE)     //by menuconfig
71 #define BTC_TASK_NAME                   "btcT"
72 #define BTC_TASK_PRIO                   (BT_TASK_MAX_PRIORITIES - 6)
73
74 osi_thread_t *btc_thread;
75
76 static const btc_func_t profile_tab[BTC_PID_NUM] = {
77 #ifdef CONFIG_BT_BLUEDROID_ENABLED
78     [BTC_PID_MAIN_INIT]   = {btc_main_call_handler,       NULL                    },
79     [BTC_PID_DEV]         = {btc_dev_call_handler,        NULL                    },
80 #if (GATTS_INCLUDED == TRUE)
81     [BTC_PID_GATTS]       = {btc_gatts_call_handler,      btc_gatts_cb_handler    },
82 #endif  ///GATTS_INCLUDED == TRUE
83 #if (GATTC_INCLUDED == TRUE)
84     [BTC_PID_GATTC]       = {btc_gattc_call_handler,      btc_gattc_cb_handler    },
85 #endif  ///GATTC_INCLUDED == TRUE
86 #if (GATTS_INCLUDED == TRUE || GATTC_INCLUDED == TRUE)
87     [BTC_PID_GATT_COMMON] = {btc_gatt_com_call_handler,   NULL                    },
88 #endif //GATTC_INCLUDED == TRUE || GATTS_INCLUDED == TRUE
89 #if (BLE_INCLUDED == TRUE)
90     [BTC_PID_GAP_BLE]     = {btc_gap_ble_call_handler,    btc_gap_ble_cb_handler  },
91 #else
92     [BTC_PID_GAP_BLE]     = {NULL, NULL},
93 #endif  ///BLE_INCLUDED == TRUE
94     [BTC_PID_BLE_HID]     = {NULL, NULL},
95     [BTC_PID_SPPLIKE]     = {NULL, NULL},
96 #if (GATTS_INCLUDED == TRUE)
97     [BTC_PID_BLUFI]       = {btc_blufi_call_handler,      btc_blufi_cb_handler    },
98 #endif  ///GATTS_INCLUDED == TRUE
99     [BTC_PID_DM_SEC]      = {NULL,                        btc_dm_sec_cb_handler   },
100 #endif
101     [BTC_PID_ALARM]       = {btc_alarm_handler,           NULL                    },
102 #ifdef CONFIG_BT_BLUEDROID_ENABLED
103 #if CLASSIC_BT_INCLUDED
104 #if (BTC_GAP_BT_INCLUDED == TRUE)
105     [BTC_PID_GAP_BT]    = {btc_gap_bt_call_handler,     btc_gap_bt_cb_handler   },
106 #endif /* (BTC_GAP_BT_INCLUDED == TRUE) */
107     [BTC_PID_PRF_QUE]     = {btc_profile_queue_handler,   NULL                    },
108 #if BTC_AV_INCLUDED
109     [BTC_PID_A2DP]        = {btc_a2dp_call_handler,       btc_a2dp_cb_handler     },
110     [BTC_PID_AVRC_CT]     = {btc_avrc_ct_call_handler,    NULL                    },
111     [BTC_PID_AVRC_TG]     = {btc_avrc_tg_call_handler,    NULL                    },
112 #endif /* #if BTC_AV_INCLUDED */
113 #if (BTC_SPP_INCLUDED == TRUE)
114     [BTC_PID_SPP]         = {btc_spp_call_handler,        btc_spp_cb_handler      },
115 #endif /* #if (BTC_SPP_INCLUDED == TRUE) */
116 #if BTC_HF_INCLUDED
117     [BTC_PID_HF]   = {btc_hf_call_handler,  btc_hf_cb_handler},
118 #endif  /* #if BTC_HF_INCLUDED */
119 #if BTC_HF_CLIENT_INCLUDED
120     [BTC_PID_HF_CLIENT]   = {btc_hf_client_call_handler,  btc_hf_client_cb_handler},
121 #endif  /* #if BTC_HF_CLIENT_INCLUDED */
122 #endif /* #if CLASSIC_BT_INCLUDED */
123 #endif
124 #if CONFIG_BLE_MESH
125     [BTC_PID_PROV]              = {btc_ble_mesh_prov_call_handler,              btc_ble_mesh_prov_cb_handler             },
126     [BTC_PID_MODEL]             = {btc_ble_mesh_model_call_handler,             btc_ble_mesh_model_cb_handler            },
127     [BTC_PID_HEALTH_CLIENT]     = {btc_ble_mesh_health_client_call_handler,     btc_ble_mesh_health_client_cb_handler    },
128     [BTC_PID_HEALTH_SERVER]     = {btc_ble_mesh_health_server_call_handler,     btc_ble_mesh_health_server_cb_handler    },
129     [BTC_PID_CONFIG_CLIENT]     = {btc_ble_mesh_config_client_call_handler,     btc_ble_mesh_config_client_cb_handler    },
130     [BTC_PID_CONFIG_SERVER]     = {NULL,                                        btc_ble_mesh_config_server_cb_handler    },
131     [BTC_PID_GENERIC_CLIENT]    = {btc_ble_mesh_generic_client_call_handler,    btc_ble_mesh_generic_client_cb_handler   },
132     [BTC_PID_LIGHTING_CLIENT]   = {btc_ble_mesh_lighting_client_call_handler,   btc_ble_mesh_lighting_client_cb_handler  },
133     [BTC_PID_SENSOR_CLIENT]     = {btc_ble_mesh_sensor_client_call_handler,     btc_ble_mesh_sensor_client_cb_handler    },
134     [BTC_PID_TIME_SCENE_CLIENT] = {btc_ble_mesh_time_scene_client_call_handler, btc_ble_mesh_time_scene_client_cb_handler},
135 #endif /* #if CONFIG_BLE_MESH */
136 };
137
138 /*****************************************************************************
139 **
140 ** Function         btc_task
141 **
142 ** Description      Process profile Task Thread.
143 ******************************************************************************/
144 static void btc_thread_handler(void *arg)
145 {
146     btc_msg_t *msg = (btc_msg_t *)arg;
147
148     BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, msg->arg);
149     switch (msg->sig) {
150     case BTC_SIG_API_CALL:
151         profile_tab[msg->pid].btc_call(msg);
152         break;
153     case BTC_SIG_API_CB:
154         profile_tab[msg->pid].btc_cb(msg);
155         break;
156     default:
157         break;
158     }
159
160     if (msg->arg) {
161         osi_free(msg->arg);
162     }
163     osi_free(msg);
164 }
165
166 static bt_status_t btc_task_post(btc_msg_t *msg, uint32_t timeout)
167 {
168     btc_msg_t *lmsg;
169
170     lmsg = (btc_msg_t *)osi_malloc(sizeof(btc_msg_t));
171     if (lmsg == NULL) {
172         return BT_STATUS_NOMEM;
173     }
174
175     memcpy(lmsg, msg, sizeof(btc_msg_t));
176
177     if (osi_thread_post(btc_thread, btc_thread_handler, lmsg, 0, timeout) == false) {
178         return BT_STATUS_BUSY;
179     }
180
181     return BT_STATUS_SUCCESS;
182 }
183
184 bt_status_t btc_transfer_context(btc_msg_t *msg, void *arg, int arg_len, btc_arg_deep_copy_t copy_func)
185 {
186     btc_msg_t lmsg;
187
188     if (msg == NULL) {
189         return BT_STATUS_PARM_INVALID;
190     }
191
192     BTC_TRACE_DEBUG("%s msg %u %u %u %p\n", __func__, msg->sig, msg->pid, msg->act, arg);
193
194     memcpy(&lmsg, msg, sizeof(btc_msg_t));
195     if (arg) {
196         lmsg.arg = (void *)osi_malloc(arg_len);
197         if (lmsg.arg == NULL) {
198             return BT_STATUS_NOMEM;
199         }
200         memset(lmsg.arg, 0x00, arg_len);    //important, avoid arg which have no length
201         memcpy(lmsg.arg, arg, arg_len);
202         if (copy_func) {
203             copy_func(&lmsg, lmsg.arg, arg);
204         }
205     } else {
206         lmsg.arg = NULL;
207     }
208
209     return btc_task_post(&lmsg, OSI_THREAD_MAX_TIMEOUT);
210
211 }
212
213 #if BTC_DYNAMIC_MEMORY
214
215 static void btc_deinit_mem(void) {
216     if (btc_dm_cb_ptr) {
217         osi_free(btc_dm_cb_ptr);
218         btc_dm_cb_ptr = NULL;
219     }
220
221     if (btc_profile_cb_tab) {
222         osi_free(btc_profile_cb_tab);
223         btc_profile_cb_tab = NULL;
224     }
225
226 #if (BLE_INCLUDED == TRUE)
227     if (gl_bta_adv_data_ptr) {
228         osi_free(gl_bta_adv_data_ptr);
229         gl_bta_adv_data_ptr = NULL;
230     }
231
232     if (gl_bta_scan_rsp_data_ptr) {
233         osi_free(gl_bta_scan_rsp_data_ptr);
234         gl_bta_scan_rsp_data_ptr = NULL;
235     }
236 #endif  ///BLE_INCLUDED == TRUE
237
238 #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
239     if (btc_creat_tab_env_ptr) {
240         osi_free(btc_creat_tab_env_ptr);
241         btc_creat_tab_env_ptr = NULL;
242     }
243
244     if (blufi_env_ptr) {
245         osi_free(blufi_env_ptr);
246         blufi_env_ptr = NULL;
247     }
248 #endif
249
250 #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
251     if (hf_client_local_param_ptr) {
252         osi_free(hf_client_local_param_ptr);
253         hf_client_local_param_ptr = NULL;
254     }
255 #endif
256
257 #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
258     if (btc_rc_cb_ptr) {
259         osi_free(btc_rc_cb_ptr);
260         btc_rc_cb_ptr = NULL;
261     }
262     if (bta_av_co_cb_ptr) {
263         osi_free(bta_av_co_cb_ptr);
264         bta_av_co_cb_ptr = NULL;
265     }
266 #endif
267 }
268
269 static bt_status_t btc_init_mem(void) {
270     if ((btc_dm_cb_ptr = (btc_dm_cb_t *)osi_malloc(sizeof(btc_dm_cb_t))) == NULL) {
271         goto error_exit;
272     }
273     memset((void *)btc_dm_cb_ptr, 0, sizeof(btc_dm_cb_t));
274
275     if ((btc_profile_cb_tab = (void **)osi_malloc(sizeof(void *) * BTC_PID_NUM)) == NULL) {
276         goto error_exit;
277     }
278     memset((void *)btc_profile_cb_tab, 0, sizeof(void *) * BTC_PID_NUM);
279
280 #if (BLE_INCLUDED == TRUE)
281     if ((gl_bta_adv_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
282         goto error_exit;
283     }
284     memset((void *)gl_bta_adv_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
285
286     if ((gl_bta_scan_rsp_data_ptr = (tBTA_BLE_ADV_DATA *)osi_malloc(sizeof(tBTA_BLE_ADV_DATA))) == NULL) {
287         goto error_exit;
288     }
289     memset((void *)gl_bta_scan_rsp_data_ptr, 0, sizeof(tBTA_BLE_ADV_DATA));
290 #endif  ///BLE_INCLUDED == TRUE
291
292 #if GATTS_INCLUDED == TRUE && GATT_DYNAMIC_MEMORY == TRUE
293     if ((btc_creat_tab_env_ptr = (esp_btc_creat_tab_t *)osi_malloc(sizeof(esp_btc_creat_tab_t))) == NULL) {
294         goto error_exit;
295     }
296     memset((void *)btc_creat_tab_env_ptr, 0, sizeof(esp_btc_creat_tab_t));
297
298     if ((blufi_env_ptr = (tBLUFI_ENV *)osi_malloc(sizeof(tBLUFI_ENV))) == NULL) {
299         goto error_exit;
300     }
301     memset((void *)blufi_env_ptr, 0, sizeof(tBLUFI_ENV));
302 #endif
303
304 #if BTC_HF_CLIENT_INCLUDED == TRUE && HFP_DYNAMIC_MEMORY == TRUE
305     if ((hf_client_local_param_ptr = (hf_client_local_param_t *)osi_malloc(sizeof(hf_client_local_param_t))) == NULL) {
306         goto error_exit;
307     }
308     memset((void *)hf_client_local_param_ptr, 0, sizeof(hf_client_local_param_t));
309 #endif
310
311 #if BTC_AV_INCLUDED == TRUE && AVRC_DYNAMIC_MEMORY == TRUE
312     if ((btc_rc_cb_ptr = (btc_rc_cb_t *)osi_malloc(sizeof(btc_rc_cb_t))) == NULL) {
313         goto error_exit;
314     }
315     memset((void *)btc_rc_cb_ptr, 0, sizeof(btc_rc_cb_t));
316     if ((bta_av_co_cb_ptr = (tBTA_AV_CO_CB *)osi_malloc(sizeof(tBTA_AV_CO_CB))) == NULL) {
317         goto error_exit;
318     }
319     memset((void *)bta_av_co_cb_ptr, 0, sizeof(tBTA_AV_CO_CB));
320 #endif
321
322     return BT_STATUS_SUCCESS;
323
324 error_exit:;
325     btc_deinit_mem();
326     return BT_STATUS_NOMEM;
327 }
328 #endif ///BTC_DYNAMIC_MEMORY
329
330 int btc_init(void)
331 {
332     btc_thread = osi_thread_create("BTC_TASK", BTC_TASK_STACK_SIZE, BTC_TASK_PRIO, BTC_TASK_PINNED_TO_CORE, 3);
333     if (btc_thread == NULL) {
334         return BT_STATUS_NOMEM;
335     }
336
337 #if BTC_DYNAMIC_MEMORY
338     if (btc_init_mem() != BT_STATUS_SUCCESS){
339         return BT_STATUS_NOMEM;
340     }
341 #endif
342
343 #if (BLE_INCLUDED == TRUE)
344     btc_gap_callback_init();
345 #endif  ///BLE_INCLUDED == TRUE
346
347 #if SCAN_QUEUE_CONGEST_CHECK
348     btc_adv_list_init();
349 #endif
350     /* TODO: initial the profile_tab */
351     return BT_STATUS_SUCCESS;
352 }
353
354 void btc_deinit(void)
355 {
356 #if BTC_DYNAMIC_MEMORY
357     btc_deinit_mem();
358 #endif
359
360     osi_thread_free(btc_thread);
361     btc_thread = NULL;
362
363 #if SCAN_QUEUE_CONGEST_CHECK
364     btc_adv_list_deinit();
365 #endif
366 }
367
368 bool btc_check_queue_is_congest(void)
369 {
370     if (osi_thread_queue_wait_size(btc_thread, 0) >= BT_QUEUE_CONGEST_SIZE) {
371         return true;
372     }
373
374     return false;
375 }
376