bte_init.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
  46. #include "stack/avrc_api.h"
  47. #endif
  48. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  49. #include "stack/a2d_api.h"
  50. #endif
  51. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
  52. #include "avdt_int.h"
  53. #endif
  54. #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE)
  55. #include "avct_int.h"
  56. #endif
  57. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  58. #include "stack/hidh_api.h"
  59. #endif
  60. #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
  61. #include "mca_api.h"
  62. #endif
  63. #if (defined(BLE_INCLUDED) && BLE_INCLUDED == TRUE)
  64. #include "stack/gatt_api.h"
  65. #if (defined(SMP_INCLUDED) && SMP_INCLUDED == TRUE)
  66. #include "stack/smp_api.h"
  67. #endif
  68. #endif
  69. //BTA Modules
  70. #if BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
  71. #include "bta/bta_api.h"
  72. #include "bta/bta_sys.h"
  73. #include "osi/allocator.h"
  74. #if BTA_HF_INCLUDED == TRUE
  75. #include "bta_hf_client_int.h"
  76. #endif
  77. #if BTA_AG_INCLUDED == TRUE
  78. #include "bta_ag_int.h"
  79. #endif
  80. #if BTA_SDP_INCLUDED == TRUE
  81. #include "bta_sdp_int.h"
  82. #endif
  83. #if BTA_HS_INCLUDED == TRUE
  84. #include "bta_hs_int.h"
  85. #endif
  86. #include "bta_dm_int.h"
  87. #if BTA_AR_INCLUDED==TRUE
  88. #include "bta_ar_int.h"
  89. #endif
  90. #if BTA_AV_INCLUDED==TRUE
  91. #include "bta_av_int.h"
  92. #endif
  93. #if BTA_HH_INCLUDED==TRUE
  94. #include "bta_hh_int.h"
  95. #endif
  96. #if BTA_JV_INCLUDED==TRUE
  97. #include "bta_jv_int.h"
  98. #endif
  99. #if BTA_HL_INCLUDED == TRUE
  100. #include "bta_hl_int.h"
  101. #endif
  102. #if BTA_GATT_INCLUDED == TRUE
  103. #include "bta_gattc_int.h"
  104. #include "bta_gatts_int.h"
  105. #endif
  106. #if BTA_PAN_INCLUDED==TRUE
  107. #include "bta_pan_int.h"
  108. #endif
  109. #include "bta_sys_int.h"
  110. // control block for patch ram downloading
  111. //#include "bta_prm_int.h"
  112. #endif // BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
  113. /*****************************************************************************
  114. ** F U N C T I O N S *
  115. ******************************************************************************/
  116. /*****************************************************************************
  117. **
  118. ** Function BTE_DeinitStack
  119. **
  120. ** Description Deinitialize control block memory for each component.
  121. **
  122. ** Note: This API must be called
  123. ** after freeing the BTU Task.
  124. **
  125. ** Returns void
  126. **
  127. ******************************************************************************/
  128. void BTE_DeinitStack(void)
  129. {
  130. //BTA Modules
  131. #if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
  132. #if GATTS_INCLUDED == TRUE
  133. if (bta_gatts_cb_ptr){
  134. osi_free(bta_gatts_cb_ptr);
  135. bta_gatts_cb_ptr = NULL;
  136. }
  137. #endif
  138. #if GATTC_INCLUDED==TRUE
  139. if (bta_gattc_cb_ptr){
  140. osi_free(bta_gattc_cb_ptr);
  141. bta_gattc_cb_ptr = NULL;
  142. }
  143. #endif
  144. #if BTA_HH_INCLUDED==TRUE
  145. if (bta_hh_cb_ptr){
  146. osi_free(bta_hh_cb_ptr);
  147. bta_hh_cb_ptr = NULL;
  148. }
  149. #endif
  150. #if BTA_AV_INCLUDED==TRUE
  151. if (bta_av_cb_ptr){
  152. osi_free(bta_av_cb_ptr);
  153. bta_av_cb_ptr = NULL;
  154. }
  155. if (bta_av_sbc_ups_cb_ptr){
  156. osi_free(bta_av_sbc_ups_cb_ptr);
  157. bta_av_sbc_ups_cb_ptr = NULL;
  158. }
  159. #endif
  160. #if BTA_AR_INCLUDED==TRUE
  161. if (bta_ar_cb_ptr){
  162. osi_free(bta_ar_cb_ptr);
  163. bta_ar_cb_ptr = NULL;
  164. }
  165. #endif
  166. #if SDP_INCLUDED == TRUE
  167. if (g_disc_raw_data_buf){
  168. osi_free(g_disc_raw_data_buf);
  169. g_disc_raw_data_buf = NULL;
  170. }
  171. #endif
  172. #if BTA_SDP_INCLUDED == TRUE
  173. if (bta_sdp_cb_ptr){
  174. osi_free(bta_sdp_cb_ptr);
  175. bta_sdp_cb_ptr = NULL;
  176. }
  177. #endif
  178. #if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
  179. if (bta_jv_cb_ptr){
  180. osi_free(bta_jv_cb_ptr);
  181. bta_jv_cb_ptr = NULL;
  182. }
  183. #endif //JV
  184. #if (defined BTA_HF_INCLUDED && BTA_HF_INCLUDED == TRUE)
  185. if (bta_hf_client_cb_ptr){
  186. osi_free(bta_hf_client_cb_ptr);
  187. bta_hf_client_cb_ptr = NULL;
  188. }
  189. #endif
  190. #if (defined BTA_AG_INCLUDED && BTA_AG_INCLUDED == TRUE)
  191. if (bta_ag_cb_ptr){
  192. osi_free(bta_ag_cb_ptr);
  193. bta_ag_cb_ptr = NULL;
  194. }
  195. #endif
  196. if (bta_dm_conn_srvcs_ptr){
  197. osi_free(bta_dm_conn_srvcs_ptr);
  198. bta_dm_conn_srvcs_ptr = NULL;
  199. }
  200. if (bta_dm_di_cb_ptr){
  201. osi_free(bta_dm_di_cb_ptr);
  202. bta_dm_di_cb_ptr = NULL;
  203. }
  204. if (bta_dm_search_cb_ptr){
  205. osi_free(bta_dm_search_cb_ptr);
  206. bta_dm_search_cb_ptr = NULL;
  207. }
  208. if (bta_dm_cb_ptr){
  209. osi_free(bta_dm_cb_ptr);
  210. bta_dm_cb_ptr = NULL;
  211. }
  212. if (bta_sys_cb_ptr){
  213. osi_free(bta_sys_cb_ptr);
  214. bta_sys_cb_ptr = NULL;
  215. }
  216. #endif // BTA_INCLUDED == TRUE
  217. #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
  218. GAP_Deinit();
  219. #endif
  220. #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE && AVCT_DYNAMIC_MEMORY == TRUE)
  221. if (avct_cb_ptr){
  222. osi_free(avct_cb_ptr);
  223. avct_cb_ptr = NULL;
  224. }
  225. #endif
  226. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE && AVDT_DYNAMIC_MEMORY == TRUE)
  227. if (avdt_cb_ptr){
  228. osi_free(avdt_cb_ptr);
  229. avdt_cb_ptr = NULL;
  230. }
  231. #endif
  232. #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
  233. AVRC_Deinit();
  234. #endif
  235. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  236. A2D_Deinit();
  237. #endif
  238. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  239. RFCOMM_Deinit();
  240. #endif
  241. }
  242. /*****************************************************************************
  243. **
  244. ** Function BTE_InitStack
  245. **
  246. ** Description Initialize control block memory for each component.
  247. **
  248. ** Note: The core stack components must be called
  249. ** before creating the BTU Task. The rest of the
  250. ** components can be initialized at a later time if desired
  251. ** as long as the component's init function is called
  252. ** before accessing any of its functions.
  253. **
  254. ** Returns status
  255. **
  256. ******************************************************************************/
  257. bt_status_t BTE_InitStack(void)
  258. {
  259. #if (defined(RFCOMM_INCLUDED) && RFCOMM_INCLUDED == TRUE)
  260. //Initialize the optional stack components
  261. if (RFCOMM_Init() != BT_STATUS_SUCCESS) {
  262. goto error_exit;
  263. }
  264. #endif
  265. //BNEP and its profiles
  266. #if (defined(BNEP_INCLUDED) && BNEP_INCLUDED == TRUE)
  267. BNEP_Init();
  268. #if (defined(PAN_INCLUDED) && PAN_INCLUDED == TRUE)
  269. PAN_Init();
  270. #endif // PAN
  271. #endif // BNEP Included
  272. //AVDT and its profiles
  273. #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
  274. if (A2D_Init() != BT_STATUS_SUCCESS) {
  275. goto error_exit;
  276. }
  277. #endif // AADP
  278. #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
  279. if (AVRC_Init() != BT_STATUS_SUCCESS) {
  280. goto error_exit;
  281. }
  282. #endif
  283. #if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE && AVDT_DYNAMIC_MEMORY == TRUE)
  284. if ((avdt_cb_ptr = (tAVDT_CB *)osi_malloc(sizeof(tAVDT_CB))) == NULL) {
  285. goto error_exit;
  286. }
  287. memset((void *)avdt_cb_ptr, 0, sizeof(tAVDT_CB));
  288. #endif
  289. #if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE && AVCT_DYNAMIC_MEMORY == TRUE)
  290. if ((avct_cb_ptr = (tAVCT_CB *)osi_malloc(sizeof(tAVCT_CB))) == NULL) {
  291. goto error_exit;
  292. }
  293. memset((void *)avct_cb_ptr, 0, sizeof(tAVCT_CB));
  294. #endif
  295. #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
  296. if (GAP_Init() != BT_STATUS_SUCCESS) {
  297. goto error_exit;
  298. }
  299. #endif
  300. #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
  301. HID_HostInit();
  302. #endif
  303. #if (defined(MCA_INCLUDED) && MCA_INCLUDED == TRUE)
  304. MCA_Init();
  305. #endif
  306. //BTA Modules
  307. #if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
  308. if ((bta_sys_cb_ptr = (tBTA_SYS_CB *)osi_malloc(sizeof(tBTA_SYS_CB))) == NULL) {
  309. goto error_exit;
  310. }
  311. if ((bta_dm_cb_ptr = (tBTA_DM_CB *)osi_malloc(sizeof(tBTA_DM_CB))) == NULL) {
  312. goto error_exit;
  313. }
  314. if ((bta_dm_search_cb_ptr = (tBTA_DM_SEARCH_CB *)osi_malloc(sizeof(tBTA_DM_SEARCH_CB))) == NULL) {
  315. goto error_exit;
  316. }
  317. if ((bta_dm_di_cb_ptr = (tBTA_DM_DI_CB *)osi_malloc(sizeof(tBTA_DM_DI_CB))) == NULL) {
  318. goto error_exit;
  319. }
  320. if ((bta_dm_conn_srvcs_ptr = (tBTA_DM_CONNECTED_SRVCS *)osi_malloc(sizeof(tBTA_DM_CONNECTED_SRVCS))) == NULL) {
  321. goto error_exit;
  322. }
  323. memset((void *)bta_sys_cb_ptr, 0, sizeof(tBTA_SYS_CB));
  324. memset((void *)bta_dm_cb_ptr, 0, sizeof(tBTA_DM_CB));
  325. memset((void *)bta_dm_search_cb_ptr, 0, sizeof(tBTA_DM_SEARCH_CB));
  326. memset((void *)bta_dm_di_cb_ptr, 0, sizeof(tBTA_DM_DI_CB));
  327. memset((void *)bta_dm_conn_srvcs_ptr, 0, sizeof(tBTA_DM_CONNECTED_SRVCS));
  328. //memset((void *)bta_prm_cb_ptr, 0, sizeof(tBTA_PRM_CB));
  329. #if (defined BTA_HF_INCLUDED && BTA_HF_INCLUDED == TRUE)
  330. if ((bta_hf_client_cb_ptr = (tBTA_HF_CLIENT_CB *)osi_malloc(sizeof(tBTA_HF_CLIENT_CB))) == NULL) {
  331. goto error_exit;
  332. }
  333. memset((void *)bta_hf_client_cb_ptr, 0, sizeof(tBTA_HF_CLIENT_CB));
  334. #endif
  335. #if (defined BTA_AG_INCLUDED && BTA_AG_INCLUDED == TRUE)
  336. if ((bta_ag_cb_ptr = (tBTA_AG_CB *)osi_malloc(sizeof(tBTA_AG_CB))) == NULL) {
  337. goto error_exit;
  338. }
  339. memset((void *)bta_ag_cb_ptr, 0, sizeof(tBTA_AG_CB));
  340. #endif
  341. #if (defined BTA_JV_INCLUDED && BTA_JV_INCLUDED == TRUE)
  342. if ((bta_jv_cb_ptr = (tBTA_JV_CB *)osi_malloc(sizeof(tBTA_JV_CB))) == NULL) {
  343. goto error_exit;
  344. }
  345. memset((void *)bta_jv_cb_ptr, 0, sizeof(tBTA_JV_CB));
  346. #endif //JV
  347. #if BTA_HS_INCLUDED == TRUE
  348. memset((void *)bta_hs_cb_ptr, 0, sizeof(tBTA_HS_CB));
  349. #endif
  350. #if BTA_SDP_INCLUDED == TRUE
  351. if ((bta_sdp_cb_ptr = (tBTA_SDP_CB *)osi_malloc(sizeof(tBTA_SDP_CB))) == NULL) {
  352. goto error_exit;
  353. }
  354. memset((void *)bta_sdp_cb_ptr, 0, sizeof(tBTA_SDP_CB));
  355. #endif
  356. #if SDP_INCLUDED == TRUE
  357. if ((g_disc_raw_data_buf = (UINT8 *)osi_malloc(MAX_DISC_RAW_DATA_BUF)) == NULL) {
  358. goto error_exit;
  359. }
  360. memset((void *)g_disc_raw_data_buf, 0, MAX_DISC_RAW_DATA_BUF);
  361. #endif
  362. #if BTA_AR_INCLUDED==TRUE
  363. if ((bta_ar_cb_ptr = (tBTA_AR_CB *)osi_malloc(sizeof(tBTA_AR_CB))) == NULL) {
  364. goto error_exit;
  365. }
  366. memset((void *)bta_ar_cb_ptr, 0, sizeof(tBTA_AR_CB));
  367. #endif
  368. #if BTA_AV_INCLUDED==TRUE
  369. if ((bta_av_cb_ptr = (tBTA_AV_CB *)osi_malloc(sizeof(tBTA_AV_CB))) == NULL) {
  370. goto error_exit;
  371. }
  372. memset((void *)bta_av_cb_ptr, 0, sizeof(tBTA_AV_CB));
  373. if ((bta_av_sbc_ups_cb_ptr = (tBTA_AV_SBC_UPS_CB *)osi_malloc(sizeof(tBTA_AV_SBC_UPS_CB))) == NULL) {
  374. goto error_exit;
  375. }
  376. memset((void *)bta_av_sbc_ups_cb_ptr, 0, sizeof(tBTA_AV_SBC_UPS_CB));
  377. #endif
  378. #if BTA_HH_INCLUDED==TRUE
  379. if ((bta_hh_cb_ptr = (tBTA_HH_CB *)osi_malloc(sizeof(tBTA_HH_CB))) == NULL) {
  380. goto error_exit;
  381. }
  382. memset((void *)bta_hh_cb_ptr, 0, sizeof(tBTA_HH_CB));
  383. #endif
  384. #if BTA_HL_INCLUDED==TRUE
  385. memset((void *)bta_hl_cb_ptr, 0, sizeof(tBTA_HL_CB));
  386. #endif
  387. #if GATTC_INCLUDED==TRUE
  388. if ((bta_gattc_cb_ptr = (tBTA_GATTC_CB *)osi_malloc(sizeof(tBTA_GATTC_CB))) == NULL) {
  389. goto error_exit;
  390. }
  391. memset((void *)bta_gattc_cb_ptr, 0, sizeof(tBTA_GATTC_CB));
  392. #endif
  393. #if GATTS_INCLUDED == TRUE
  394. if ((bta_gatts_cb_ptr = (tBTA_GATTS_CB *)osi_malloc(sizeof(tBTA_GATTS_CB))) == NULL) {
  395. goto error_exit;
  396. }
  397. memset((void *)bta_gatts_cb_ptr, 0, sizeof(tBTA_GATTS_CB));
  398. #endif
  399. #if BTA_PAN_INCLUDED==TRUE
  400. memset((void *)bta_pan_cb_ptr, 0, sizeof(tBTA_PAN_CB));
  401. #endif
  402. #endif // BTA_INCLUDED == TRUE
  403. return BT_STATUS_SUCCESS;
  404. error_exit:;
  405. LOG_ERROR("%s failed due to no memory", __func__);
  406. BTE_DeinitStack();
  407. return BT_STATUS_NOMEM;
  408. }