Selaa lähdekoodia

Merge branch 'feature/btdm_bluedroid_env_dynomic_malloc_enable' into 'master'

This MR added the bluedroid env variable can dynamic malloc and BT variable use psram malloc supported

See merge request !1698

Jiang Jiang Jian 8 vuotta sitten
vanhempi
sitoutus
5b1f869880
38 muutettua tiedostoa jossa 379 lisäystä ja 51 poistoa
  1. 14 0
      components/bt/Kconfig
  2. 2 0
      components/bt/bluedroid/bta/ar/bta_ar.c
  3. 2 0
      components/bt/bluedroid/bta/av/bta_av_main.c
  4. 18 0
      components/bt/bluedroid/bta/dm/bta_dm_main.c
  5. 11 0
      components/bt/bluedroid/bta/gatt/bta_gattc_main.c
  6. 11 0
      components/bt/bluedroid/bta/gatt/bta_gatts_main.c
  7. 2 0
      components/bt/bluedroid/bta/hh/bta_hh_main.c
  8. 0 0
      components/bt/bluedroid/bta/include/bta_ar_int.h
  9. 0 0
      components/bt/bluedroid/bta/include/bta_av_int.h
  10. 1 0
      components/bt/bluedroid/bta/include/bta_dm_int.h
  11. 1 0
      components/bt/bluedroid/bta/include/bta_gattc_int.h
  12. 1 1
      components/bt/bluedroid/bta/include/bta_gatts_int.h
  13. 0 0
      components/bt/bluedroid/bta/include/bta_hh_int.h
  14. 0 0
      components/bt/bluedroid/bta/include/bta_sdp_int.h
  15. 2 0
      components/bt/bluedroid/bta/sdp/bta_sdp.c
  16. 5 0
      components/bt/bluedroid/bta/sys/bta_sys_main.c
  17. 12 0
      components/bt/bluedroid/btc/core/btc_main.c
  18. 6 0
      components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c
  19. 1 0
      components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h
  20. 71 6
      components/bt/bluedroid/main/bte_init.c
  21. 18 4
      components/bt/bluedroid/osi/allocator.c
  22. 72 3
      components/bt/bluedroid/osi/include/allocator.h
  23. 6 0
      components/bt/bluedroid/stack/a2dp/a2d_api.c
  24. 2 0
      components/bt/bluedroid/stack/avct/avct_api.c
  25. 2 0
      components/bt/bluedroid/stack/avdt/avdt_api.c
  26. 6 0
      components/bt/bluedroid/stack/avrc/avrc_sdp.c
  27. 8 1
      components/bt/bluedroid/stack/btm/btm_main.c
  28. 10 1
      components/bt/bluedroid/stack/btu/btu_init.c
  29. 2 0
      components/bt/bluedroid/stack/btu/btu_task.c
  30. 13 5
      components/bt/bluedroid/stack/gatt/gatt_main.c
  31. 36 29
      components/bt/bluedroid/stack/include/dyn_mem.h
  32. 12 0
      components/bt/bluedroid/stack/include/smp_api.h
  33. 8 1
      components/bt/bluedroid/stack/l2cap/l2c_main.c
  34. 3 0
      components/bt/bluedroid/stack/rfcomm/port_api.c
  35. 2 0
      components/bt/bluedroid/stack/rfcomm/rfc_port_if.c
  36. 5 0
      components/bt/bluedroid/stack/sdp/sdp_main.c
  37. 12 0
      components/bt/bluedroid/stack/smp/smp_api.c
  38. 2 0
      components/bt/bluedroid/stack/smp/smp_main.c

+ 14 - 0
components/bt/Kconfig

@@ -173,6 +173,20 @@ config BT_ACL_CONNECTIONS
     help 
         Maximum BT/BLE connection count
 
+config BT_ALLOCATION_FROM_SPIRAM_FIRST
+    bool "BT/BLE will first malloc the memory from the PSRAM"
+    depends on BLUEDROID_ENABLED
+    default n
+    help
+        This select can save the internal RAM if there have the PSRAM
+
+config BT_BLE_DYNAMIC_ENV_MEMORY
+    bool "Use dynamic memory allocation in BT/BLE stack"
+    depends on BLUEDROID_ENABLED
+    default n
+    help
+        This select can make the allocation of memory will become more flexible
+
 config SMP_ENABLE
    bool
    depends on BLUEDROID_ENABLED

+ 2 - 0
components/bt/bluedroid/bta/ar/bta_ar.c

@@ -32,6 +32,8 @@
 /* AV control block */
 #if BTA_DYNAMIC_MEMORY == FALSE
 tBTA_AR_CB  bta_ar_cb;
+#else
+tBTA_AR_CB  *bta_ar_cb_ptr;
 #endif
 
 /*******************************************************************************

+ 2 - 0
components/bt/bluedroid/bta/av/bta_av_main.c

@@ -194,6 +194,8 @@ const tBTA_AV_NSM_ACT bta_av_nsm_act[] = {
 /* AV control block */
 #if BTA_DYNAMIC_MEMORY == FALSE
 tBTA_AV_CB  bta_av_cb;
+#else
+tBTA_AV_CB  *bta_av_cb_ptr;
 #endif
 
 #if (defined(BTA_AV_DEBUG) && BTA_AV_DEBUG == TRUE)

+ 18 - 0
components/bt/bluedroid/bta/dm/bta_dm_main.c

@@ -25,6 +25,8 @@
 #include "bta_api.h"
 #include "bta_sys.h"
 #include "bta_dm_int.h"
+#include "allocator.h"
+#include <string.h>
 
 
 /*****************************************************************************
@@ -35,6 +37,10 @@
 tBTA_DM_CB  bta_dm_cb;
 tBTA_DM_SEARCH_CB bta_dm_search_cb;
 tBTA_DM_DI_CB       bta_dm_di_cb;
+#else
+tBTA_DM_CB  *bta_dm_cb_ptr;
+tBTA_DM_SEARCH_CB *bta_dm_search_cb_ptr;
+tBTA_DM_DI_CB       *bta_dm_di_cb_ptr;
 #endif
 
 
@@ -346,6 +352,18 @@ void bta_dm_sm_disable( )
     bta_sys_deregister( BTA_ID_DM );
 }
 
+void bta_dm_sm_deinit(void)
+{
+    memset(&bta_dm_cb, 0, sizeof(tBTA_DM_CB));
+    memset(&bta_dm_search_cb, 0, sizeof(tBTA_DM_SEARCH_CB));
+    memset(&bta_dm_di_cb, 0, sizeof(tBTA_DM_DI_CB));
+#if BTA_DYNAMIC_MEMORY
+    FREE_AND_RESET(bta_dm_cb_ptr);
+    FREE_AND_RESET(bta_dm_search_cb_ptr);
+    FREE_AND_RESET(bta_dm_di_cb_ptr);
+#endif /* #if BTA_DYNAMIC_MEMORY */
+}
+
 
 /*******************************************************************************
 **

+ 11 - 0
components/bt/bluedroid/bta/gatt/bta_gattc_main.c

@@ -29,6 +29,7 @@
 #include <string.h>
 
 #include "bta_gattc_int.h"
+#include "allocator.h"
 
 
 /*****************************************************************************
@@ -237,6 +238,8 @@ const tBTA_GATTC_ST_TBL bta_gattc_st_tbl[] = {
 /* GATTC control block */
 #if BTA_DYNAMIC_MEMORY == FALSE
 tBTA_GATTC_CB  bta_gattc_cb;
+#else
+tBTA_GATTC_CB  *bta_gattc_cb_ptr;
 #endif
 
 #if BTA_GATT_DEBUG == TRUE
@@ -493,4 +496,12 @@ static char *gattc_state_code(tBTA_GATTC_STATE state_code)
 }
 
 #endif  /* Debug Functions */
+
+void bta_gattc_deinit(void)
+{
+#if BTA_DYNAMIC_MEMORY
+    memset(bta_gattc_cb_ptr, 0, sizeof(tBTA_GATTC_CB));
+    FREE_AND_RESET(bta_gattc_cb_ptr);
+#endif /* #if BTA_DYNAMIC_MEMORY */
+}
 #endif /* GATTC_INCLUDED == TRUE && BLE_INCLUDED == TRUE */

+ 11 - 0
components/bt/bluedroid/bta/gatt/bta_gatts_main.c

@@ -29,6 +29,7 @@
 #include <string.h>
 
 #include "bta_gatts_int.h"
+#include "allocator.h"
 
 /* type for service building action functions */
 typedef void (*tBTA_GATTS_SRVC_ACT)(tBTA_GATTS_SRVC_CB *p_rcb, tBTA_GATTS_DATA *p_data);
@@ -46,6 +47,8 @@ const tBTA_GATTS_SRVC_ACT bta_gatts_srvc_build_act[] = {
 /* GATTS control block */
 #if BTA_DYNAMIC_MEMORY == FALSE
 tBTA_GATTS_CB  bta_gatts_cb;
+#else
+tBTA_GATTS_CB *bta_gatts_cb_ptr;
 #endif
 
 /*******************************************************************************
@@ -135,4 +138,12 @@ BOOLEAN bta_gatts_hdl_event(BT_HDR *p_msg)
     return (TRUE);
 }
 
+void bta_gatts_deinit(void)
+{
+    memset(&bta_gatts_cb, 0, sizeof(tBTA_GATTS_CB));
+#if BTA_DYNAMIC_MEMORY
+    FREE_AND_RESET(bta_gatts_cb_ptr);
+#endif /* #if BTA_DYNAMIC_MEMORY */
+}
+
 #endif /* GATTS_INCLUDED */

+ 2 - 0
components/bt/bluedroid/bta/hh/bta_hh_main.c

@@ -245,6 +245,8 @@ const tBTA_HH_ST_TBL bta_hh_st_tbl[] = {
 *****************************************************************************/
 #if BTA_DYNAMIC_MEMORY == FALSE
 tBTA_HH_CB  bta_hh_cb;
+#else
+tBTA_HH_CB  *bta_hh_cb_ptr;
 #endif
 /*****************************************************************************
 ** Static functions

+ 0 - 0
components/bt/bluedroid/bta/ar/bta_ar_int.h → components/bt/bluedroid/bta/include/bta_ar_int.h


+ 0 - 0
components/bt/bluedroid/bta/av/bta_av_int.h → components/bt/bluedroid/bta/include/bta_av_int.h


+ 1 - 0
components/bt/bluedroid/bta/dm/bta_dm_int.h → components/bt/bluedroid/bta/include/bta_dm_int.h

@@ -1130,6 +1130,7 @@ extern tBTA_DM_DI_CB *bta_dm_di_cb_ptr;
 
 extern BOOLEAN bta_dm_sm_execute(BT_HDR *p_msg);
 extern void bta_dm_sm_disable( void );
+extern void bta_dm_sm_deinit(void);
 extern BOOLEAN bta_dm_search_sm_execute(BT_HDR *p_msg);
 extern void bta_dm_search_sm_disable( void );
 

+ 1 - 0
components/bt/bluedroid/bta/include/bta_gattc_int.h

@@ -514,5 +514,6 @@ extern BOOLEAN bta_gattc_conn_dealloc(BD_ADDR remote_bda);
 
 extern bool bta_gattc_cache_load(tBTA_GATTC_CLCB *p_clcb);
 extern void bta_gattc_cache_reset(BD_ADDR server_bda);
+extern void bta_gattc_deinit(void);
 
 #endif /* BTA_GATTC_INT_H */

+ 1 - 1
components/bt/bluedroid/bta/include/bta_gatts_int.h

@@ -249,7 +249,7 @@ extern UINT8 bta_gatts_find_app_rcb_idx_by_app_if(tBTA_GATTS_CB *p_cb, tBTA_GATT
 extern UINT8 bta_gatts_alloc_srvc_cb(tBTA_GATTS_CB *p_cb, UINT8 rcb_idx);
 extern tBTA_GATTS_SRVC_CB *bta_gatts_find_srvc_cb_by_srvc_id(tBTA_GATTS_CB *p_cb, UINT16 service_id);
 extern tBTA_GATTS_SRVC_CB *bta_gatts_find_srvc_cb_by_attr_id(tBTA_GATTS_CB *p_cb, UINT16 attr_id);
-
+extern void bta_gatts_deinit(void);
 
 #endif /* BTA_GATTS_INT_H */
 

+ 0 - 0
components/bt/bluedroid/bta/hh/bta_hh_int.h → components/bt/bluedroid/bta/include/bta_hh_int.h


+ 0 - 0
components/bt/bluedroid/bta/sdp/bta_sdp_int.h → components/bt/bluedroid/bta/include/bta_sdp_int.h


+ 2 - 0
components/bt/bluedroid/bta/sdp/bta_sdp.c

@@ -37,6 +37,8 @@
 
 #if BTA_DYNAMIC_MEMORY == FALSE
 tBTA_SDP_CB bta_sdp_cb;
+#else
+tBTA_SDP_CB *bta_sdp_cb_ptr;
 #endif
 
 /* state machine action enumeration list */

+ 5 - 0
components/bt/bluedroid/bta/sys/bta_sys_main.c

@@ -48,6 +48,8 @@
 /* system manager control block definition */
 #if BTA_DYNAMIC_MEMORY == FALSE
 tBTA_SYS_CB bta_sys_cb;
+#else
+tBTA_SYS_CB *bta_sys_cb_ptr;
 #endif
 
 static hash_map_t *bta_alarm_hash_map;
@@ -190,6 +192,9 @@ void bta_sys_free(void)
 {
     hash_map_free(bta_alarm_hash_map);
     osi_mutex_free(&bta_alarm_lock);
+#if BTA_DYNAMIC_MEMORY
+    FREE_AND_RESET(bta_sys_cb_ptr);
+#endif
 }
 
 /*******************************************************************************

+ 12 - 0
components/bt/bluedroid/btc/core/btc_main.c

@@ -21,6 +21,10 @@
 #include "alarm.h"
 #include "btc_ble_storage.h"
 #include "bta_gatt_common.h"
+#include "btc_gap_ble.h"
+#include "bta_gattc_int.h"
+#include "bta_gatts_int.h"
+#include "bta_dm_int.h"
 
 static future_t *main_future[BTC_MAIN_FUTURE_NUM];
 
@@ -67,6 +71,14 @@ static void btc_init_bluetooth(void)
 
 static void btc_deinit_bluetooth(void)
 {
+    btc_gap_ble_deinit();
+    bta_dm_sm_deinit();
+#if (GATTC_INCLUDED)
+    bta_gattc_deinit();
+#endif /* #if (GATTC_INCLUDED) */
+#if (GATTS_INCLUDED)
+    bta_gatts_deinit();
+#endif /* #if (GATTS_INCLUDED) */
     bte_main_shutdown();
 #if (SMP_INCLUDED)
     btc_config_clean_up();

+ 6 - 0
components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c

@@ -1164,3 +1164,9 @@ void btc_gap_callback_init(void)
     BTM_BleRegiseterConnParamCallback(btc_update_conn_param_callback);
 
 }
+
+void btc_gap_ble_deinit(void)
+{
+    btc_cleanup_adv_data(&gl_bta_adv_data);
+    btc_cleanup_adv_data(&gl_bta_scan_rsp_data);
+}

+ 1 - 0
components/bt/bluedroid/btc/profile/std/include/btc_gap_ble.h

@@ -160,5 +160,6 @@ void btc_gap_ble_arg_deep_free(btc_msg_t *msg);
 void btc_gap_ble_cb_deep_free(btc_msg_t *msg);
 void btc_gap_ble_cb_deep_copy(btc_msg_t *msg, void *p_dest, void *p_src);
 void btc_gap_callback_init(void);
+void btc_gap_ble_deinit(void);
 
 #endif /* __BTC_GAP_BLE_H__ */

+ 71 - 6
components/bt/bluedroid/main/bte_init.c

@@ -26,6 +26,7 @@
 #include "bt_target.h"
 #include <string.h>
 
+
 /* Stack Configuation Related Init Definaton
  * TODO: Now Just Unmask these defination until stack layer is OK
  */
@@ -59,6 +60,14 @@
 #include "a2d_api.h"
 #endif
 
+#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE)
+#include "avdt_int.h"
+#endif
+
+#if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE)
+#include "avct_int.h"
+#endif
+
 #if (defined(HID_HOST_INCLUDED) && HID_HOST_INCLUDED == TRUE)
 #include "hidh_api.h"
 #endif
@@ -78,8 +87,13 @@
 #if BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
 #include "bta_api.h"
 #include "bta_sys.h"
+#include "allocator.h"
+
+//#include "bta_ag_int.h"
 
-#include "bta_ag_int.h"
+#if BTA_SDP_INCLUDED == TRUE
+#include "bta_sdp_int.h"
+#endif
 
 #if BTA_HS_INCLUDED == TRUE
 #include "bta_hs_int.h"
@@ -119,9 +133,9 @@ tBTA_JV_CB *bta_jv_cb_ptr = NULL;
 #include "bta_sys_int.h"
 
 // control block for patch ram downloading
-#include "bta_prm_int.h"
+//#include "bta_prm_int.h"
 
-#endif // BTA_INCLUDED
+#endif // BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE
 
 
 /*****************************************************************************
@@ -169,6 +183,20 @@ void BTE_InitStack(void)
     AVRC_Init();
 #endif
 
+#if (defined(AVDT_INCLUDED) && AVDT_INCLUDED == TRUE && AVDT_DYNAMIC_MEMORY == TRUE)
+    if ((avdt_cb_ptr = (tAVDT_CB *)osi_malloc(sizeof(tAVDT_CB))) == NULL) {
+        return;
+    }
+    memset((void *)avdt_cb_ptr, 0, sizeof(tAVDT_CB));
+#endif
+
+#if (defined(AVCT_INCLUDED) && AVCT_INCLUDED == TRUE && AVCT_DYNAMIC_MEMORY == TRUE)
+    if ((avct_cb_ptr = (tAVCT_CB *)osi_malloc(sizeof(tAVCT_CB))) == NULL) {
+        return;
+    }
+    memset((void *)avct_cb_ptr, 0, sizeof(tAVCT_CB));
+#endif
+
 #if (defined(GAP_INCLUDED) && GAP_INCLUDED == TRUE)
     GAP_Init();
 #endif
@@ -183,29 +211,66 @@ void BTE_InitStack(void)
 
     //BTA Modules
 #if (BTA_INCLUDED == TRUE && BTA_DYNAMIC_MEMORY == TRUE)
+    if ((bta_sys_cb_ptr = (tBTA_SYS_CB *)osi_malloc(sizeof(tBTA_SYS_CB))) == NULL) {
+        return;
+    }
+    if ((bta_dm_cb_ptr = (tBTA_DM_CB *)osi_malloc(sizeof(tBTA_DM_CB))) == NULL) {
+        return;
+    }
+    if ((bta_dm_search_cb_ptr = (tBTA_DM_SEARCH_CB *)osi_malloc(sizeof(tBTA_DM_SEARCH_CB))) == NULL) {
+        return;
+    }
+    if ((bta_dm_di_cb_ptr = (tBTA_DM_DI_CB *)osi_malloc(sizeof(tBTA_DM_DI_CB))) == NULL) {
+        return;
+    }
     memset((void *)bta_sys_cb_ptr, 0, sizeof(tBTA_SYS_CB));
     memset((void *)bta_dm_cb_ptr, 0, sizeof(tBTA_DM_CB));
     memset((void *)bta_dm_search_cb_ptr, 0, sizeof(tBTA_DM_SEARCH_CB));
     memset((void *)bta_dm_di_cb_ptr, 0, sizeof(tBTA_DM_DI_CB));
-    memset((void *)bta_prm_cb_ptr, 0, sizeof(tBTA_PRM_CB));
-    memset((void *)bta_ag_cb_ptr, 0, sizeof(tBTA_AG_CB));
+    //memset((void *)bta_prm_cb_ptr, 0, sizeof(tBTA_PRM_CB));
+    //memset((void *)bta_ag_cb_ptr, 0, sizeof(tBTA_AG_CB));
 #if BTA_HS_INCLUDED == TRUE
     memset((void *)bta_hs_cb_ptr, 0, sizeof(tBTA_HS_CB));
 #endif
+#if BTA_SDP_INCLUDED == TRUE
+    if ((bta_sdp_cb_ptr = (tBTA_SDP_CB *)osi_malloc(sizeof(tBTA_SDP_CB))) == NULL) {
+        return;
+    }
+    memset((void *)bta_sdp_cb_ptr, 0, sizeof(tBTA_SDP_CB));
+#endif
 #if BTA_AR_INCLUDED==TRUE
+    if ((bta_ar_cb_ptr = (tBTA_AR_CB *)osi_malloc(sizeof(tBTA_AR_CB))) == NULL) {
+        return;
+    }
     memset((void *)bta_ar_cb_ptr, 0, sizeof(tBTA_AR_CB));
 #endif
 #if BTA_AV_INCLUDED==TRUE
+    if ((bta_av_cb_ptr = (tBTA_AV_CB *)osi_malloc(sizeof(tBTA_AV_CB))) == NULL) {
+        return;
+    }
     memset((void *)bta_av_cb_ptr, 0, sizeof(tBTA_AV_CB));
 #endif
 #if BTA_HH_INCLUDED==TRUE
+    if ((bta_hh_cb_ptr = (tBTA_HH_CB *)osi_malloc(sizeof(tBTA_HH_CB))) == NULL) {
+        return;
+    } 
     memset((void *)bta_hh_cb_ptr, 0, sizeof(tBTA_HH_CB));
 #endif
 #if BTA_HL_INCLUDED==TRUE
     memset((void *)bta_hl_cb_ptr, 0, sizeof(tBTA_HL_CB));
 #endif
-#if BTA_GATT_INCLUDED==TRUE
+#if GATTC_INCLUDED==TRUE
+    if ((bta_gattc_cb_ptr = (tBTA_GATTC_CB *)osi_malloc(sizeof(tBTA_GATTC_CB))) == NULL) {
+        return;
+    }
+    memset((void *)bta_gattc_cb_ptr, 0, sizeof(tBTA_GATTC_CB));
+#endif
+#if GATTS_INCLUDED == TRUE
+    if ((bta_gatts_cb_ptr = (tBTA_GATTS_CB *)osi_malloc(sizeof(tBTA_GATTS_CB))) == NULL) {
+        return;
+    }
     memset((void *)bta_gattc_cb_ptr, 0, sizeof(tBTA_GATTC_CB));
+    //
     memset((void *)bta_gatts_cb_ptr, 0, sizeof(tBTA_GATTS_CB));
 #endif
 #if BTA_PAN_INCLUDED==TRUE

+ 18 - 4
components/bt/bluedroid/osi/allocator.c

@@ -132,26 +132,40 @@ void *osi_malloc_func(size_t size)
 {
 #ifdef CONFIG_BLUEDROID_MEM_DEBUG
     void *p;
-
+#if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
+    p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
+#else
     p = malloc(size);
+#endif /* #if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST */
     osi_mem_dbg_record(p, size, __func__, __LINE__);
     return p;
+#else
+#if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
+    return heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
 #else
     return malloc(size);
-#endif
+#endif /* #if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST */
+#endif /* #ifdef CONFIG_BLUEDROID_MEM_DEBUG */
 }
 
 void *osi_calloc_func(size_t size)
 {
 #ifdef CONFIG_BLUEDROID_MEM_DEBUG
     void *p;
-
+#if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
+    p = heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
+#else
     p = calloc(1, size);
+#endif /* #if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST */
     osi_mem_dbg_record(p, size, __func__, __LINE__);
     return p;
+#else
+#if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
+    return heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);
 #else
     return calloc(1, size);
-#endif
+#endif /* #if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST */ 
+#endif /* #ifdef CONFIG_BLUEDROID_MEM_DEBUG */
 }
 
 void osi_free_func(void *ptr)

+ 72 - 3
components/bt/bluedroid/osi/include/allocator.h

@@ -21,6 +21,7 @@
 
 #include <stddef.h>
 #include <stdlib.h>
+#include "esp_heap_caps.h"
 #include "sdkconfig.h"
 
 typedef void *(*alloc_fn)(size_t size);
@@ -48,11 +49,13 @@ void osi_mem_dbg_record(void *p, int size, const char *func, int line);
 void osi_mem_dbg_clean(void *p, const char *func, int line);
 void osi_mem_dbg_show(void);
 
+#if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
 #define osi_malloc(size)                                \
 ({                                                      \
     void *p;                                            \
-                                                        \
-    p = malloc((size));                                 \
+    p = heap_caps_malloc_prefer(size, 2,                \
+        MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM,           \
+        MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);        \
     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
     (void *)p;                                          \
 })
@@ -60,12 +63,64 @@ void osi_mem_dbg_show(void);
 #define osi_calloc(size)                                \
 ({                                                      \
     void *p;                                            \
-                                                        \
+    p = heap_caps_calloc_prefer(1, size, 2,             \
+        MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM,           \
+        MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);        \
+    osi_mem_dbg_record(p, size, __func__, __LINE__);    \
+    (void *)p;                                          \
+})
+
+#else
+
+#define osi_malloc(size)                                \
+({                                                      \
+    void *p;                                            \
+    p = malloc((size));                                 \
+    osi_mem_dbg_record(p, size, __func__, __LINE__);    \
+    (void *)p;                                          \
+})
+
+#define osi_calloc(size)                               \
+({                                                    \
+    void *p;                                           \
     p = calloc(1, (size));                              \
     osi_mem_dbg_record(p, size, __func__, __LINE__);    \
     (void *)p;                                          \
 })
 
+#endif /* #if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST */
+
+
+#if 0
+#define osi_malloc(size)                                \
+do {                                                    \
+    void *p;                                            \
+                                                        \
+#if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST              \
+    p = heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL); \
+#else                                                   \
+    p = malloc((size));                                 \
+#endif /* #if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST */ \
+    osi_mem_dbg_record(p, size, __func__, __LINE__);    \
+    (void *)p;                                          \
+}while(0)
+
+#define osi_calloc(size)                                \
+do {                                                    \
+    void *p;                                            \
+                                                        \
+#if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST              \
+        p = heap_caps_calloc_prefer(1, size, 2,         \
+            MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM,       \
+            MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL);    \
+#else                                                   \
+    p = calloc(1, (size));                              \
+#endif /* #if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST */ \
+    osi_mem_dbg_record(p, size, __func__, __LINE__);    \
+    (void *)p;                                          \
+} while(0)
+#endif
+
 #define osi_free(ptr)                                   \
 do {                                                    \
     void *tmp_point = (void *)(ptr);                    \
@@ -75,10 +130,24 @@ do {                                                    \
 
 #else
 
+#if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST
+#define osi_malloc(size)                  heap_caps_malloc_prefer(size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
+#define osi_calloc(size)                  heap_caps_calloc_prefer(1, size, 2, MALLOC_CAP_DEFAULT|MALLOC_CAP_SPIRAM, MALLOC_CAP_DEFAULT|MALLOC_CAP_INTERNAL)
+#else
 #define osi_malloc(size)                  malloc((size))
 #define osi_calloc(size)                  calloc(1, (size))
+#endif /* #if CONFIG_BT_ALLOCATION_FROM_SPIRAM_FIRST */
 #define osi_free(p)                       free((p))
 
 #endif /* CONFIG_BLUEDROID_MEM_DEBUG */
 
+#define FREE_AND_RESET(a)   \
+do {                        \
+    if (a) {                \
+        osi_free(a);        \
+        a = NULL;           \
+    }                       \
+}while (0)
+
+
 #endif /* _ALLOCATOR_H_ */

+ 6 - 0
components/bt/bluedroid/stack/a2dp/a2d_api.c

@@ -27,6 +27,7 @@
 #include "a2d_api.h"
 #include "a2d_int.h"
 #include "avdt_api.h"
+#include "allocator.h"
 
 #if (defined(A2D_INCLUDED) && A2D_INCLUDED == TRUE)
 
@@ -35,6 +36,8 @@
 *****************************************************************************/
 #if A2D_DYNAMIC_MEMORY == FALSE
 tA2D_CB a2d_cb;
+#else
+tA2D_CB *a2d_cb_ptr;
 #endif
 
 
@@ -374,6 +377,9 @@ UINT8 A2D_BitsSet(UINT8 num)
 *******************************************************************************/
 void A2D_Init(void)
 {
+#if (A2D_DYNAMIC_MEMORY)
+    a2d_cb_ptr = (tA2D_CB *)osi_malloc(sizeof(tA2D_CB));
+#endif /* #if (A2D_DYNAMIC_MEMORY) */
     memset(&a2d_cb, 0, sizeof(tA2D_CB));
 
     a2d_cb.avdt_sdp_ver = AVDT_VERSION;

+ 2 - 0
components/bt/bluedroid/stack/avct/avct_api.c

@@ -38,6 +38,8 @@
 /* Control block for AVCT */
 #if AVCT_DYNAMIC_MEMORY == FALSE
 tAVCT_CB avct_cb;
+#else 
+tAVCT_CB *avct_cb_ptr;
 #endif
 
 /*******************************************************************************

+ 2 - 0
components/bt/bluedroid/stack/avdt/avdt_api.c

@@ -39,6 +39,8 @@
 /* Control block for AVDT */
 #if AVDT_DYNAMIC_MEMORY == FALSE
 tAVDT_CB avdt_cb;
+#else
+tAVDT_CB *avdt_cb_ptr;
 #endif
 
 /*******************************************************************************

+ 6 - 0
components/bt/bluedroid/stack/avrc/avrc_sdp.c

@@ -25,6 +25,7 @@
 #include "bt_target.h"
 #include "avrc_api.h"
 #include "avrc_int.h"
+#include "allocator.h"
 
 #if (defined(AVRC_INCLUDED) && AVRC_INCLUDED == TRUE)
 
@@ -41,6 +42,8 @@
 *****************************************************************************/
 #if AVRC_DYNAMIC_MEMORY == FALSE
 tAVRC_CB avrc_cb;
+#else
+tAVRC_CB *avrc_cb_ptr;
 #endif
 
 /* update AVRC_NUM_PROTO_ELEMS if this constant is changed */
@@ -342,6 +345,9 @@ UINT8 AVRC_SetTraceLevel (UINT8 new_level)
 *******************************************************************************/
 void AVRC_Init(void)
 {
+#if AVRC_DYNAMIC_MEMORY
+    avrc_cb_ptr = (tAVRC_CB *)osi_malloc(sizeof(tAVRC_CB));
+#endif /* #if AVRC_DYNAMIC_MEMORY */
     memset(&avrc_cb, 0, sizeof(tAVRC_CB));
 
 #if defined(AVRC_INITIAL_TRACE_LEVEL)

+ 8 - 1
components/bt/bluedroid/stack/btm/btm_main.c

@@ -33,6 +33,8 @@
 */
 #if BTM_DYNAMIC_MEMORY == FALSE
 tBTM_CB  btm_cb;
+#else
+tBTM_CB  *btm_cb_ptr;
 #endif
 
 /*******************************************************************************
@@ -49,9 +51,11 @@ tBTM_CB  btm_cb;
 *******************************************************************************/
 void btm_init (void)
 {
+#if BTM_DYNAMIC_MEMORY
+    btm_cb_ptr = (tBTM_CB *)osi_malloc(sizeof(tBTM_CB));
+#endif /* #if BTM_DYNAMIC_MEMORY */
     /* All fields are cleared; nonzero fields are reinitialized in appropriate function */
     memset(&btm_cb, 0, sizeof(tBTM_CB));
-
     btm_cb.page_queue = fixed_queue_new(SIZE_MAX);
     btm_cb.sec_pending_q = fixed_queue_new(SIZE_MAX);
 
@@ -87,4 +91,7 @@ void btm_free(void)
 {
     fixed_queue_free(btm_cb.page_queue, osi_free_func);
     fixed_queue_free(btm_cb.sec_pending_q, osi_free_func);
+#if BTM_DYNAMIC_MEMORY
+    FREE_AND_RESET(btm_cb_ptr);
+#endif
 }

+ 10 - 1
components/bt/bluedroid/stack/btu/btu_init.c

@@ -119,12 +119,15 @@ void btu_free_core(void)
     l2c_free();
 
 #if BLE_INCLUDED == TRUE
-#if (defined(GATTS_INCLUDED) && GATTS_INCLUDED == true)
+#if (defined(GATT_INCLUDED) && GATT_INCLUDED == true)
     gatt_free();
 #endif
     btm_ble_free();
 #endif
     btm_free();
+#if SMP_INCLUDED == TRUE
+    SMP_Free();
+#endif
 }
 
 /*****************************************************************************
@@ -141,6 +144,9 @@ void btu_free_core(void)
 ******************************************************************************/
 void BTU_StartUp(void)
 {
+#if BTU_DYNAMIC_MEMORY
+    btu_cb_ptr = (tBTU_CB *)osi_malloc(sizeof(tBTU_CB));
+#endif /* #if BTU_DYNAMIC_MEMORY */
     memset (&btu_cb, 0, sizeof (tBTU_CB));
     btu_cb.trace_level = HCI_INITIAL_TRACE_LEVEL;
 
@@ -182,6 +188,9 @@ error_exit:;
 
 void BTU_ShutDown(void)
 {
+#if BTU_DYNAMIC_MEMORY
+    FREE_AND_RESET(btu_cb_ptr);
+#endif
     btu_task_shut_down();
 
     hash_map_free(btu_general_alarm_hash_map);

+ 2 - 0
components/bt/bluedroid/stack/btu/btu_task.c

@@ -91,6 +91,8 @@ extern void BTE_InitStack(void);
 */
 #if BTU_DYNAMIC_MEMORY == FALSE
 tBTU_CB  btu_cb;
+#else
+tBTU_CB  *btu_cb_ptr;
 #endif
 
 extern hash_map_t *btu_general_alarm_hash_map;

+ 13 - 5
components/bt/bluedroid/stack/gatt/gatt_main.c

@@ -77,6 +77,8 @@ static const tL2CAP_APPL_INFO dyn_info = {
 
 #if GATT_DYNAMIC_MEMORY == FALSE
 tGATT_CB  gatt_cb;
+#else
+tGATT_CB  *gatt_cb_ptr;
 #endif
 
 tGATT_DEFAULT gatt_default;
@@ -94,9 +96,9 @@ tGATT_DEFAULT gatt_default;
 void gatt_init (void)
 {
     tL2CAP_FIXED_CHNL_REG  fixed_reg;
-
-    GATT_TRACE_DEBUG("gatt_init()");
-
+#if GATT_DYNAMIC_MEMORY
+    gatt_cb_ptr = (tGATT_CB *)osi_malloc(sizeof(tGATT_CB));
+#endif /* #if GATT_DYNAMIC_MEMORY */
     memset (&gatt_cb, 0, sizeof(tGATT_CB));
     memset (&fixed_reg, 0, sizeof(tL2CAP_FIXED_CHNL_REG));
 
@@ -152,7 +154,7 @@ void gatt_init (void)
 ** Returns          void
 **
 *******************************************************************************/
-#if (GATTS_INCLUDED == TRUE)
+#if (GATT_INCLUDED == TRUE)
 void gatt_free(void)
 {
     int i;
@@ -171,14 +173,20 @@ void gatt_free(void)
 
         fixed_queue_free(gatt_cb.tcb[i].pending_ind_q, NULL);
         gatt_cb.tcb[i].pending_ind_q = NULL;
-
+#if (GATTS_INCLUDED == TRUE)
         fixed_queue_free(gatt_cb.tcb[i].sr_cmd.multi_rsp_q, NULL);
         gatt_cb.tcb[i].sr_cmd.multi_rsp_q = NULL;
+#endif /* #if (GATTS_INCLUDED == TRUE) */
     }
 
+#if (GATTS_INCLUDED == TRUE)
     for (i = 0; i < GATT_MAX_SR_PROFILES; i++) {
         gatt_free_hdl_buffer(&gatt_cb.hdl_list[i]);
     }
+#endif /* #if (GATTS_INCLUDED == TRUE) */
+#if GATT_DYNAMIC_MEMORY
+    FREE_AND_RESET(gatt_cb_ptr);
+#endif /* #if GATT_DYNAMIC_MEMORY */
 }
 #endif  ///GATTS_INCLUDED == TRUE
 

+ 36 - 29
components/bt/bluedroid/stack/include/dyn_mem.h

@@ -19,7 +19,40 @@
 #define DYN_MEM_H
 
 #include "sdkconfig.h"
-#if CONFIG_CLASSIC_BT_ENABLED
+#if CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY
+#define BTU_DYNAMIC_MEMORY         TRUE
+#define BTM_DYNAMIC_MEMORY         TRUE
+#define L2C_DYNAMIC_MEMORY         TRUE
+#define GATT_DYNAMIC_MEMORY        TRUE
+#define SMP_DYNAMIC_MEMORY         TRUE
+#define BTA_DYNAMIC_MEMORY         TRUE
+#define SDP_DYNAMIC_MEMORY         TRUE
+#define RFC_DYNAMIC_MEMORY         TRUE
+#define TCS_DYNAMIC_MEMORY         TRUE
+#define BNEP_DYNAMIC_MEMORY        TRUE
+#define AVDT_DYNAMIC_MEMORY        TRUE
+#define AVCT_DYNAMIC_MEMORY        TRUE
+#define MCA_DYNAMIC_MEMORY         TRUE
+#define A2D_DYNAMIC_MEMORY         TRUE
+#define VDP_DYNAMIC_MEMORY         TRUE
+#define AVRC_DYNAMIC_MEMORY        TRUE
+#define BIP_DYNAMIC_MEMORY         TRUE
+#define BPP_DYNAMIC_MEMORY         TRUE
+#define CTP_DYNAMIC_MEMORY         TRUE
+#define FTP_DYNAMIC_MEMORY         TRUE
+#define HCRP_DYNAMIC_MEMORY        TRUE
+#define HFP_DYNAMIC_MEMORY         TRUE
+#define HID_DYNAMIC_MEMORY         TRUE
+#define HSP2_DYNAMIC_MEMORY        TRUE
+#define ICP_DYNAMIC_MEMORY         TRUE
+#define OPP_DYNAMIC_MEMORY         TRUE
+#define PAN_DYNAMIC_MEMORY         TRUE
+#define SPP_DYNAMIC_MEMORY         TRUE
+#define SLIP_DYNAMIC_MEMORY        TRUE
+#define LLCP_DYNAMIC_MEMORY        TRUE
+#define BTC_SBC_DEC_DYNAMIC_MEMORY TRUE
+
+#else  /* #if CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY */
 
 #define SDP_DYNAMIC_MEMORY         FALSE
 #define RFC_DYNAMIC_MEMORY         FALSE
@@ -45,35 +78,9 @@
 #define SPP_DYNAMIC_MEMORY         FALSE
 #define SLIP_DYNAMIC_MEMORY        FALSE
 #define LLCP_DYNAMIC_MEMORY        FALSE
+#define BTC_SBC_DEC_DYNAMIC_MEMORY FALSE
 
-#else  /* #if CONFIG_CLASSIC_BT_ENABLED */
-
-#define SDP_DYNAMIC_MEMORY         TRUE
-#define RFC_DYNAMIC_MEMORY         TRUE
-#define TCS_DYNAMIC_MEMORY         TRUE
-#define BNEP_DYNAMIC_MEMORY        TRUE
-#define AVDT_DYNAMIC_MEMORY        TRUE
-#define AVCT_DYNAMIC_MEMORY        TRUE
-#define MCA_DYNAMIC_MEMORY         TRUE
-#define A2D_DYNAMIC_MEMORY         TRUE
-#define VDP_DYNAMIC_MEMORY         TRUE
-#define AVRC_DYNAMIC_MEMORY        TRUE
-#define BIP_DYNAMIC_MEMORY         TRUE
-#define BPP_DYNAMIC_MEMORY         TRUE
-#define CTP_DYNAMIC_MEMORY         TRUE
-#define FTP_DYNAMIC_MEMORY         TRUE
-#define HCRP_DYNAMIC_MEMORY        TRUE
-#define HFP_DYNAMIC_MEMORY         TRUE
-#define HID_DYNAMIC_MEMORY         TRUE
-#define HSP2_DYNAMIC_MEMORY        TRUE
-#define ICP_DYNAMIC_MEMORY         TRUE
-#define OPP_DYNAMIC_MEMORY         TRUE
-#define PAN_DYNAMIC_MEMORY         TRUE
-#define SPP_DYNAMIC_MEMORY         TRUE
-#define SLIP_DYNAMIC_MEMORY        TRUE
-#define LLCP_DYNAMIC_MEMORY        TRUE
-
-#endif /* #if CONFIG_CLASSIC_BT_ENABLED */
+#endif /* #if CONFIG_BT_BLE_DYNAMIC_ENV_MEMORY */
 /****************************************************************************
 ** Define memory usage for each CORE component (if not defined in bdroid_buildcfg.h)
 **  The default for each component is to use static memory allocations.

+ 12 - 0
components/bt/bluedroid/stack/include/smp_api.h

@@ -303,6 +303,18 @@ extern "C"
 *******************************************************************************/
 extern void SMP_Init(void);
 
+/*******************************************************************************
+**
+** Function         SMP_Free
+**
+** Description      This function de initializes the SMP unit.
+**
+** Returns          void
+**
+*******************************************************************************/
+extern void SMP_Free(void);
+
+
 /*******************************************************************************
 **
 ** Function         SMP_SetTraceLevel

+ 8 - 1
components/bt/bluedroid/stack/l2cap/l2c_main.c

@@ -48,6 +48,8 @@ static void process_l2cap_cmd (tL2C_LCB *p_lcb, UINT8 *p, UINT16 pkt_len);
 /********************************************************************************/
 #if L2C_DYNAMIC_MEMORY == FALSE
 tL2C_CB l2cb;
+#else
+tL2C_CB *l2c_cb_ptr;
 #endif
 
 /*******************************************************************************
@@ -815,7 +817,9 @@ void l2c_process_held_packets(BOOLEAN timed_out)
 void l2c_init (void)
 {
     INT16  xx;
-
+#if L2C_DYNAMIC_MEMORY
+    l2c_cb_ptr = (tL2C_CB *)osi_malloc(sizeof(tL2C_CB));
+#endif /* #if L2C_DYNAMIC_MEMORY */
     memset (&l2cb, 0, sizeof (tL2C_CB));
     /* the psm is increased by 2 before being used */
     l2cb.dyn_psm = 0xFFF;
@@ -874,6 +878,9 @@ void l2c_free(void)
 {
     list_free(l2cb.rcv_pending_q);
     l2cb.rcv_pending_q = NULL;
+#if L2C_DYNAMIC_MEMORY
+    FREE_AND_RESET(l2c_cb_ptr);
+#endif 
 }
 
 /*******************************************************************************

+ 3 - 0
components/bt/bluedroid/stack/rfcomm/port_api.c

@@ -1731,6 +1731,9 @@ int PORT_Test (UINT16 handle, UINT8 *p_data, UINT16 len)
 *******************************************************************************/
 void RFCOMM_Init (void)
 {
+#if (RFC_DYNAMIC_MEMORY)
+    rfc_cb_ptr = (tRFC_CB *)osi_malloc(sizeof(tRFC_CB));
+#endif /* #if (RFC_DYNAMIC_MEMORY) */
     memset (&rfc_cb, 0, sizeof (tRFC_CB));  /* Init RFCOMM control block */
 
     rfc_cb.rfc.last_mux = MAX_BD_CONNECTIONS;

+ 2 - 0
components/bt/bluedroid/stack/rfcomm/rfc_port_if.c

@@ -36,6 +36,8 @@
 
 #if RFC_DYNAMIC_MEMORY == FALSE
 tRFC_CB rfc_cb;
+#else
+tRFC_CB *rfc_cb_ptr;
 #endif
 
 /*******************************************************************************

+ 5 - 0
components/bt/bluedroid/stack/sdp/sdp_main.c

@@ -47,6 +47,8 @@
 /********************************************************************************/
 #if SDP_DYNAMIC_MEMORY == FALSE
 tSDP_CB  sdp_cb;
+#else
+tSDP_CB  *sdp_cb_ptr;
 #endif
 
 /********************************************************************************/
@@ -79,6 +81,9 @@ static void sdp_disconnect_cfm (UINT16 l2cap_cid, UINT16 result);
 *******************************************************************************/
 void sdp_init (void)
 {
+#if SDP_DYNAMIC_MEMORY
+    sdp_cb_ptr = (tSDP_CB *)osi_malloc(sizeof(tSDP_CB));
+#endif /* #if SDP_DYNAMIC_MEMORY */
     /* Clears all structures and local SDP database (if Server is enabled) */
     memset (&sdp_cb, 0, sizeof (tSDP_CB));
 

+ 12 - 0
components/bt/bluedroid/stack/smp/smp_api.c

@@ -36,6 +36,7 @@
 
 #include "btu.h"
 #include "p_256_ecc_pp.h"
+#include "allocator.h"
 
 /*******************************************************************************
 **
@@ -48,6 +49,9 @@
 *******************************************************************************/
 void SMP_Init(void)
 {
+#if SMP_DYNAMIC_MEMORY
+    smp_cb_ptr = (tSMP_CB *)osi_malloc(sizeof(tSMP_CB));
+#endif
     memset(&smp_cb, 0, sizeof(tSMP_CB));
 
 #if defined(SMP_INITIAL_TRACE_LEVEL)
@@ -62,6 +66,14 @@ void SMP_Init(void)
     p_256_init_curve(KEY_LENGTH_DWORDS_P256);
 }
 
+void SMP_Free(void)
+{
+    memset(&smp_cb, 0, sizeof(tSMP_CB));
+#if SMP_DYNAMIC_MEMORY
+    FREE_AND_RESET(smp_cb_ptr);
+#endif /* #if SMP_DYNAMIC_MEMORY */
+}
+
 
 /*******************************************************************************
 **

+ 2 - 0
components/bt/bluedroid/stack/smp/smp_main.c

@@ -676,6 +676,8 @@ static const tSMP_ENTRY_TBL smp_entry_table[] = {
 
 #if SMP_DYNAMIC_MEMORY == FALSE
 tSMP_CB  smp_cb;
+#else
+tSMP_CB  *smp_cb_ptr;
 #endif
 #define SMP_ALL_TBL_MASK        0x80