bte_init.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503
  1. /******************************************************************************
  2. *
  3. * Copyright (C) 2000-2012 Broadcom Corporation
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at:
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. ******************************************************************************/
  18. /******************************************************************************
  19. *
  20. * This module contains the routines that initialize the stack components.
  21. * It must be called before the BTU task is started.
  22. *
  23. ******************************************************************************/
  24. #include "common/bt_target.h"
  25. #include <string.h>
  26. /* Stack Configuation Related Init Definaton
  27. * TODO: Now Just Unmask these defination until stack layer is OK
  28. */
  29. #ifndef BTA_INCLUDED
  30. #define BTA_INCLUDED FALSE
  31. #endif
  32. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  33. // Include initialization functions definitions
  34. #include "stack/port_api.h"
  35. #endif
  36. #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
  37. #include "bnep_api.h"
  38. #endif
  39. #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
  40. #include "stack/gap_api.h"
  41. #endif
  42. #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
  43. #include "pan_api.h"
  44. #endif
  45. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  46. #include "stack/hidh_api.h"
  47. #endif
  48. #if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE)
  49. #include "stack/hidd_api.h"
  50. #endif
  51. #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
  52. #include "stack/avrc_api.h"
  53. #endif
  54. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  55. #include "stack/a2d_api.h"
  56. #endif
  57. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
  58. #include "avdt_int.h"
  59. #endif
  60. #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE)
  61. #include "avct_int.h"
  62. #endif
  63. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  64. #include "stack/hidh_api.h"
  65. #endif
  66. #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
  67. #include "mca_api.h"
  68. #endif
  69. #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
  70. #include "stack/gatt_api.h"
  71. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  72. #include "stack/smp_api.h"
  73. #endif
  74. #endif
  75. //BTA Modules
  76. #if BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
  77. #include "bta/bta_api.h"
  78. #include "bta/bta_sys.h"
  79. #include "osi/allocator.h"
  80. #if BTA_HF_INCLUDED == TRUE
  81. #include "bta_hf_client_int.h"
  82. #endif
  83. #if BTA_AG_INCLUDED == TRUE
  84. #include "bta_ag_int.h"
  85. #endif
  86. #if BTA_SDP_INCLUDED == TRUE
  87. #include "bta_sdp_int.h"
  88. #endif
  89. #if BTA_HS_INCLUDED == TRUE
  90. #include "bta_hs_int.h"
  91. #endif
  92. #include "bta_dm_int.h"
  93. #if BTA_AR_INCLUDED==TRUE
  94. #include "bta_ar_int.h"
  95. #endif
  96. #if BTA_AV_INCLUDED==TRUE
  97. #include "bta_av_int.h"
  98. #endif
  99. #if BTA_HH_INCLUDED==TRUE
  100. #include "bta_hh_int.h"
  101. #endif
  102. #if BTA_HD_INCLUDED==TRUE
  103. #include "bta_hd_int.h"
  104. #endif
  105. #if BTA_JV_INCLUDED==TRUE
  106. #include "bta_jv_int.h"
  107. #endif
  108. #if BTA_HL_INCLUDED == TRUE
  109. #include "bta_hl_int.h"
  110. #endif
  111. #if BTA_GATT_INCLUDED == TRUE
  112. #include "bta_gattc_int.h"
  113. #include "bta_gatts_int.h"
  114. #endif
  115. #if BTA_PAN_INCLUDED==TRUE
  116. #include "bta_pan_int.h"
  117. #endif
  118. #include "bta_sys_int.h"
  119. // control block for patch ram downloading
  120. //#include "bta_prm_int.h"
  121. #endif // BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
  122. /*****************************************************************************
  123. ** F U N C T I O N S *
  124. ******************************************************************************/
  125. /*****************************************************************************
  126. **
  127. ** Function BTE_DeinitStack
  128. **
  129. ** Description Deinitialize control block memory for each component.
  130. **
  131. ** Note: This API must be called
  132. ** after freeing the BTU Task.
  133. **
  134. ** Returns void
  135. **
  136. ******************************************************************************/
  137. void BTE_DeinitStack(void)
  138. {
  139. //BTA Modules
  140. #if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
  141. #if GATTS_INCLUDED == TRUE
  142. if (bta_gatts_cb_ptr){
  143. osi_free(bta_gatts_cb_ptr);
  144. bta_gatts_cb_ptr = NULL;
  145. }
  146. #endif
  147. #if GATTC_INCLUDED==TRUE
  148. if (bta_gattc_cb_ptr){
  149. osi_free(bta_gattc_cb_ptr);
  150. bta_gattc_cb_ptr = NULL;
  151. }
  152. #endif
  153. #if BTA_HD_INCLUDED==TRUE
  154. if (bta_hd_cb_ptr){
  155. osi_free(bta_hd_cb_ptr);
  156. bta_hd_cb_ptr = NULL;
  157. }
  158. #endif
  159. #if BTA_HH_INCLUDED==TRUE
  160. if (bta_hh_cb_ptr){
  161. osi_free(bta_hh_cb_ptr);
  162. bta_hh_cb_ptr = NULL;
  163. }
  164. #endif
  165. #if BTA_AV_INCLUDED==TRUE
  166. if (bta_av_cb_ptr){
  167. osi_free(bta_av_cb_ptr);
  168. bta_av_cb_ptr = NULL;
  169. }
  170. if (bta_av_sbc_ups_cb_ptr){
  171. osi_free(bta_av_sbc_ups_cb_ptr);
  172. bta_av_sbc_ups_cb_ptr = NULL;
  173. }
  174. #endif
  175. #if BTA_AR_INCLUDED==TRUE
  176. if (bta_ar_cb_ptr){
  177. osi_free(bta_ar_cb_ptr);
  178. bta_ar_cb_ptr = NULL;
  179. }
  180. #endif
  181. #if SDP_INCLUDED == TRUE
  182. if (g_disc_raw_data_buf){
  183. osi_free(g_disc_raw_data_buf);
  184. g_disc_raw_data_buf = NULL;
  185. }
  186. #endif
  187. #if BTA_SDP_INCLUDED == TRUE
  188. if (bta_sdp_cb_ptr){
  189. osi_free(bta_sdp_cb_ptr);
  190. bta_sdp_cb_ptr = NULL;
  191. }
  192. #endif
  193. #if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
  194. if (bta_jv_cb_ptr){
  195. osi_free(bta_jv_cb_ptr);
  196. bta_jv_cb_ptr = NULL;
  197. }
  198. #endif //JV
  199. #if (defined BTA_HF_INCLUDED && BTA_HF_INCLUDED == TRUE)
  200. if (bta_hf_client_cb_ptr){
  201. osi_free(bta_hf_client_cb_ptr);
  202. bta_hf_client_cb_ptr = NULL;
  203. }
  204. #endif
  205. #if (defined BTA_AG_INCLUDED && BTA_AG_INCLUDED == TRUE)
  206. if (bta_ag_cb_ptr){
  207. osi_free(bta_ag_cb_ptr);
  208. bta_ag_cb_ptr = NULL;
  209. }
  210. #endif
  211. if (bta_dm_conn_srvcs_ptr){
  212. osi_free(bta_dm_conn_srvcs_ptr);
  213. bta_dm_conn_srvcs_ptr = NULL;
  214. }
  215. if (bta_dm_di_cb_ptr){
  216. osi_free(bta_dm_di_cb_ptr);
  217. bta_dm_di_cb_ptr = NULL;
  218. }
  219. if (bta_dm_search_cb_ptr){
  220. osi_free(bta_dm_search_cb_ptr);
  221. bta_dm_search_cb_ptr = NULL;
  222. }
  223. if (bta_dm_cb_ptr){
  224. osi_free(bta_dm_cb_ptr);
  225. bta_dm_cb_ptr = NULL;
  226. }
  227. if (bta_sys_cb_ptr){
  228. osi_free(bta_sys_cb_ptr);
  229. bta_sys_cb_ptr = NULL;
  230. }
  231. #endif // BTA_INCLUDED == TRUE
  232. #if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE)
  233. HID_DevDeinit();
  234. #endif
  235. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  236. HID_HostDeinit();
  237. #endif
  238. #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
  239. GAP_Deinit();
  240. #endif
  241. #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE && AVCT_DYNAMIC_MEMORY == TRUE)
  242. if (avct_cb_ptr){
  243. osi_free(avct_cb_ptr);
  244. avct_cb_ptr = NULL;
  245. }
  246. #endif
  247. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE && AVDT_DYNAMIC_MEMORY == TRUE)
  248. if (avdt_cb_ptr){
  249. osi_free(avdt_cb_ptr);
  250. avdt_cb_ptr = NULL;
  251. }
  252. #endif
  253. #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
  254. AVRC_Deinit();
  255. #endif
  256. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  257. A2D_Deinit();
  258. #endif
  259. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  260. RFCOMM_Deinit();
  261. #endif
  262. }
  263. /*****************************************************************************
  264. **
  265. ** Function BTE_InitStack
  266. **
  267. ** Description Initialize control block memory for each component.
  268. **
  269. ** Note: The core stack components must be called
  270. ** before creating the BTU Task. The rest of the
  271. ** components can be initialized at a later time if desired
  272. ** as long as the component's init function is called
  273. ** before accessing any of its functions.
  274. **
  275. ** Returns status
  276. **
  277. ******************************************************************************/
  278. bt_status_t BTE_InitStack(void)
  279. {
  280. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  281. //Initialize the optional stack components
  282. if (RFCOMM_Init() != BT_STATUS_SUCCESS) {
  283. goto error_exit;
  284. }
  285. #endif
  286. //BNEP and its profiles
  287. #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
  288. BNEP_Init();
  289. #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
  290. PAN_Init();
  291. #endif // PAN
  292. #endif // BNEP Included
  293. //AVDT and its profiles
  294. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  295. if (A2D_Init() != BT_STATUS_SUCCESS) {
  296. goto error_exit;
  297. }
  298. #endif // AADP
  299. #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
  300. if (AVRC_Init() != BT_STATUS_SUCCESS) {
  301. goto error_exit;
  302. }
  303. #endif
  304. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE && AVDT_DYNAMIC_MEMORY == TRUE)
  305. if ((avdt_cb_ptr = (tAVDT_CB *)osi_malloc(sizeof(tAVDT_CB))) == NULL) {
  306. goto error_exit;
  307. }
  308. memset((void *)avdt_cb_ptr, 0, sizeof(tAVDT_CB));
  309. #endif
  310. #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE && AVCT_DYNAMIC_MEMORY == TRUE)
  311. if ((avct_cb_ptr = (tAVCT_CB *)osi_malloc(sizeof(tAVCT_CB))) == NULL) {
  312. goto error_exit;
  313. }
  314. memset((void *)avct_cb_ptr, 0, sizeof(tAVCT_CB));
  315. #endif
  316. #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
  317. if (GAP_Init() != BT_STATUS_SUCCESS) {
  318. goto error_exit;
  319. }
  320. #endif
  321. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  322. if (HID_HostInit() != HID_SUCCESS) {
  323. goto error_exit;
  324. }
  325. #endif
  326. #if (defined(HID_DEV_INCLUDED) && HID_DEV_INCLUDED == TRUE)
  327. if (HID_DevInit() != HID_SUCCESS) {
  328. goto error_exit;
  329. }
  330. #endif
  331. #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
  332. MCA_Init();
  333. #endif
  334. //BTA Modules
  335. #if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
  336. if ((bta_sys_cb_ptr = (tBTA_SYS_CB *)osi_malloc(sizeof(tBTA_SYS_CB))) == NULL) {
  337. goto error_exit;
  338. }
  339. if ((bta_dm_cb_ptr = (tBTA_DM_CB *)osi_malloc(sizeof(tBTA_DM_CB))) == NULL) {
  340. goto error_exit;
  341. }
  342. if ((bta_dm_search_cb_ptr = (tBTA_DM_SEARCH_CB *)osi_malloc(sizeof(tBTA_DM_SEARCH_CB))) == NULL) {
  343. goto error_exit;
  344. }
  345. if ((bta_dm_di_cb_ptr = (tBTA_DM_DI_CB *)osi_malloc(sizeof(tBTA_DM_DI_CB))) == NULL) {
  346. goto error_exit;
  347. }
  348. if ((bta_dm_conn_srvcs_ptr = (tBTA_DM_CONNECTED_SRVCS *)osi_malloc(sizeof(tBTA_DM_CONNECTED_SRVCS))) == NULL) {
  349. goto error_exit;
  350. }
  351. memset((void *)bta_sys_cb_ptr, 0, sizeof(tBTA_SYS_CB));
  352. memset((void *)bta_dm_cb_ptr, 0, sizeof(tBTA_DM_CB));
  353. memset((void *)bta_dm_search_cb_ptr, 0, sizeof(tBTA_DM_SEARCH_CB));
  354. memset((void *)bta_dm_di_cb_ptr, 0, sizeof(tBTA_DM_DI_CB));
  355. memset((void *)bta_dm_conn_srvcs_ptr, 0, sizeof(tBTA_DM_CONNECTED_SRVCS));
  356. //memset((void *)bta_prm_cb_ptr, 0, sizeof(tBTA_PRM_CB));
  357. #if (defined BTA_HF_INCLUDED && BTA_HF_INCLUDED == TRUE)
  358. if ((bta_hf_client_cb_ptr = (tBTA_HF_CLIENT_CB *)osi_malloc(sizeof(tBTA_HF_CLIENT_CB))) == NULL) {
  359. goto error_exit;
  360. }
  361. memset((void *)bta_hf_client_cb_ptr, 0, sizeof(tBTA_HF_CLIENT_CB));
  362. #endif
  363. #if (defined BTA_AG_INCLUDED && BTA_AG_INCLUDED == TRUE)
  364. if ((bta_ag_cb_ptr = (tBTA_AG_CB *)osi_malloc(sizeof(tBTA_AG_CB))) == NULL) {
  365. goto error_exit;
  366. }
  367. memset((void *)bta_ag_cb_ptr, 0, sizeof(tBTA_AG_CB));
  368. #endif
  369. #if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
  370. if ((bta_jv_cb_ptr = (tBTA_JV_CB *)osi_malloc(sizeof(tBTA_JV_CB))) == NULL) {
  371. goto error_exit;
  372. }
  373. memset((void *)bta_jv_cb_ptr, 0, sizeof(tBTA_JV_CB));
  374. #endif //JV
  375. #if BTA_HS_INCLUDED == TRUE
  376. memset((void *)bta_hs_cb_ptr, 0, sizeof(tBTA_HS_CB));
  377. #endif
  378. #if BTA_SDP_INCLUDED == TRUE
  379. if ((bta_sdp_cb_ptr = (tBTA_SDP_CB *)osi_malloc(sizeof(tBTA_SDP_CB))) == NULL) {
  380. goto error_exit;
  381. }
  382. memset((void *)bta_sdp_cb_ptr, 0, sizeof(tBTA_SDP_CB));
  383. #endif
  384. #if SDP_INCLUDED == TRUE
  385. if ((g_disc_raw_data_buf = (UINT8 *)osi_malloc(MAX_DISC_RAW_DATA_BUF)) == NULL) {
  386. goto error_exit;
  387. }
  388. memset((void *)g_disc_raw_data_buf, 0, MAX_DISC_RAW_DATA_BUF);
  389. #endif
  390. #if BTA_AR_INCLUDED==TRUE
  391. if ((bta_ar_cb_ptr = (tBTA_AR_CB *)osi_malloc(sizeof(tBTA_AR_CB))) == NULL) {
  392. goto error_exit;
  393. }
  394. memset((void *)bta_ar_cb_ptr, 0, sizeof(tBTA_AR_CB));
  395. #endif
  396. #if BTA_AV_INCLUDED==TRUE
  397. if ((bta_av_cb_ptr = (tBTA_AV_CB *)osi_malloc(sizeof(tBTA_AV_CB))) == NULL) {
  398. goto error_exit;
  399. }
  400. memset((void *)bta_av_cb_ptr, 0, sizeof(tBTA_AV_CB));
  401. if ((bta_av_sbc_ups_cb_ptr = (tBTA_AV_SBC_UPS_CB *)osi_malloc(sizeof(tBTA_AV_SBC_UPS_CB))) == NULL) {
  402. goto error_exit;
  403. }
  404. memset((void *)bta_av_sbc_ups_cb_ptr, 0, sizeof(tBTA_AV_SBC_UPS_CB));
  405. #endif
  406. #if BTA_HH_INCLUDED==TRUE
  407. if ((bta_hh_cb_ptr = (tBTA_HH_CB *)osi_malloc(sizeof(tBTA_HH_CB))) == NULL) {
  408. goto error_exit;
  409. }
  410. memset((void *)bta_hh_cb_ptr, 0, sizeof(tBTA_HH_CB));
  411. #endif
  412. #if BTA_HD_INCLUDED==TRUE
  413. if ((bta_hd_cb_ptr = (tBTA_HD_CB *)osi_malloc(sizeof(tBTA_HD_CB))) == NULL) {
  414. goto error_exit;
  415. }
  416. memset((void *)bta_hd_cb_ptr, 0, sizeof(tBTA_HD_CB));
  417. #endif
  418. #if BTA_HL_INCLUDED==TRUE
  419. memset((void *)bta_hl_cb_ptr, 0, sizeof(tBTA_HL_CB));
  420. #endif
  421. #if GATTC_INCLUDED==TRUE
  422. if ((bta_gattc_cb_ptr = (tBTA_GATTC_CB *)osi_malloc(sizeof(tBTA_GATTC_CB))) == NULL) {
  423. goto error_exit;
  424. }
  425. memset((void *)bta_gattc_cb_ptr, 0, sizeof(tBTA_GATTC_CB));
  426. #endif
  427. #if GATTS_INCLUDED == TRUE
  428. if ((bta_gatts_cb_ptr = (tBTA_GATTS_CB *)osi_malloc(sizeof(tBTA_GATTS_CB))) == NULL) {
  429. goto error_exit;
  430. }
  431. memset((void *)bta_gatts_cb_ptr, 0, sizeof(tBTA_GATTS_CB));
  432. #endif
  433. #if BTA_PAN_INCLUDED==TRUE
  434. memset((void *)bta_pan_cb_ptr, 0, sizeof(tBTA_PAN_CB));
  435. #endif
  436. #endif // BTA_INCLUDED == TRUE
  437. return BT_STATUS_SUCCESS;
  438. error_exit:;
  439. LOG_ERROR("%s failed due to no memory", __func__);
  440. BTE_DeinitStack();
  441. return BT_STATUS_NOMEM;
  442. }