bte_init.c 12 KB

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