ソースを参照

component/bt: clean up the A2DP sink example

wangmengyang 9 年 前
コミット
2a5d01a930

+ 18 - 18
components/bt/Kconfig

@@ -4,34 +4,34 @@ menuconfig BT_ENABLED
         Select this option to enable Bluetooth stack and show the submenu with Bluetooth configuration choices.
         Select this option to enable Bluetooth stack and show the submenu with Bluetooth configuration choices.
 
 
 config BTC_TASK_STACK_SIZE
 config BTC_TASK_STACK_SIZE
-	int "Bluetooth event (callback to application) task stack size"
+    int "Bluetooth event (callback to application) task stack size"
     depends on BT_ENABLED
     depends on BT_ENABLED
-	default 3072
-	help
-		This select btc task stack size
+    default 3072
+    help
+        This select btc task stack size
 
 
 config BLUEDROID_MEM_DEBUG
 config BLUEDROID_MEM_DEBUG
-	bool "Bluedroid memory debug"
+    bool "Bluedroid memory debug"
     depends on BT_ENABLED
     depends on BT_ENABLED
-	default n
-	help
-		Bluedroid memory debug
+    default n
+    help
+        Bluedroid memory debug
 
 
 config CLASSIC_BT_ENABLED
 config CLASSIC_BT_ENABLED
-	bool "Classic Bluetooth"
+    bool "Classic Bluetooth"
     depends on BT_ENABLED
     depends on BT_ENABLED
-	default n
-	help
-		BT_DRAM_RELEASE should be set TRUE if Classic Bluetooth is enabled
+    default n
+    help
+        BT_DRAM_RELEASE should be set TRUE if Classic Bluetooth is enabled
 
 
 config BT_DRAM_RELEASE
 config BT_DRAM_RELEASE
-	bool "Release DRAM from Classic BT controller"
+    bool "Release DRAM from Classic BT controller"
     depends on BT_ENABLED
     depends on BT_ENABLED
-	default n
-	help
-		This option should only be used when BLE only.
-		Open this option will release about 30K DRAM from Classic BT.
-		The released DRAM will be used as system heap memory.
+    default n
+    help
+        This option should only be used when BLE only.
+        Open this option will release about 30K DRAM from Classic BT.
+        The released DRAM will be used as system heap memory.
 
 
 # Memory reserved at start of DRAM for Bluetooth stack
 # Memory reserved at start of DRAM for Bluetooth stack
 config BT_RESERVE_DRAM
 config BT_RESERVE_DRAM

+ 2 - 3
components/bt/bluedroid/osi/config.c

@@ -22,15 +22,14 @@
 #include <stdio.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
-// #include <sys/stat.h>
 
 
 #include "allocator.h"
 #include "allocator.h"
 #include "config.h"
 #include "config.h"
 #include "list.h"
 #include "list.h"
 #include "bt_trace.h"
 #include "bt_trace.h"
 
 
-#define CONFIG_FILE_MAX_SIZE      (512)
-#define CONFIG_KEY                "cfg_key1"
+#define CONFIG_FILE_MAX_SIZE      (1024)
+#define CONFIG_KEY                "bt_cfg_key"
 typedef struct {
 typedef struct {
     char *key;
     char *key;
     char *value;
     char *value;

+ 11 - 2
examples/bluetooth/a2dp_sink/README.rst

@@ -1,5 +1,14 @@
-ESP-IDF A2DP-SINK
-=======================
+ESP-IDF A2DP-SINK demo
+======================
 
 
 Demo of A2DP audio sink role
 Demo of A2DP audio sink role
 
 
+This is the demo for user to use ESP_APIs to create a GATT Server.
+
+Options choose step:
+    1. make menuconfig.
+    2. enter menuconfig "Component config", choose "Bluetooth"
+    3. enter menu Bluetooth, choose "Classic Bluetooth" and do not choose "Release DRAM from Classic BT controller"
+    4. choose your options.
+
+After the program started, other bluetooth devices such as smart phones can discover this device named "ESP_SPEAKER", and after connection is established, audio data can be transmitted and there will occur a count of audio data packets printed.

+ 4 - 4
examples/bluetooth/a2dp_sink/components/bluedroid_demos/app_core/bt_app_main.c → examples/bluetooth/a2dp_sink/components/bt_app/app_core/bt_app_main.c

@@ -22,14 +22,14 @@ static xTaskHandle bt_app_task_handle = NULL;
 bool bt_app_transfer_context (bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback)
 bool bt_app_transfer_context (bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback)
 {
 {
     bt_app_msg_t msg;
     bt_app_msg_t msg;
-    
+
     memset(&msg, 0, sizeof(bt_app_msg_t));
     memset(&msg, 0, sizeof(bt_app_msg_t));
     BT_APP_TRACE_EVENT("btapp_transfer_context evt 0x%x, len %d\n", event, param_len);
     BT_APP_TRACE_EVENT("btapp_transfer_context evt 0x%x, len %d\n", event, param_len);
 
 
     msg.sig = BT_APP_SIG_CONTEXT_SWITCH;
     msg.sig = BT_APP_SIG_CONTEXT_SWITCH;
     msg.event = event;
     msg.event = event;
     msg.cb = p_cback;
     msg.cb = p_cback;
-    
+
     if (param_len == 0) {
     if (param_len == 0) {
         return bt_app_post_msg(&msg);
         return bt_app_post_msg(&msg);
     } else if (p_params && param_len > 0) {
     } else if (p_params && param_len > 0) {
@@ -73,7 +73,7 @@ static void bt_app_task_handler(void *arg)
     bt_app_msg_t msg;
     bt_app_msg_t msg;
     for (;;) {
     for (;;) {
         if (pdTRUE == xQueueReceive(bt_app_task_queue, &msg, (portTickType)portMAX_DELAY)) {
         if (pdTRUE == xQueueReceive(bt_app_task_queue, &msg, (portTickType)portMAX_DELAY)) {
-	    BT_APP_TRACE_EVENT("btapp handle evt, sig 0x%x, 0x%x\n", msg.sig, msg.event);
+            BT_APP_TRACE_EVENT("btapp handle evt, sig 0x%x, 0x%x\n", msg.sig, msg.event);
             switch (msg.sig) {
             switch (msg.sig) {
             case BT_APP_SIG_CONTEXT_SWITCH:
             case BT_APP_SIG_CONTEXT_SWITCH:
                 bt_app_context_switched(&msg);
                 bt_app_context_switched(&msg);
@@ -82,7 +82,7 @@ static void bt_app_task_handler(void *arg)
                 BT_APP_TRACE_WARNING("unhandled BT_APP event (%d)\n", msg.sig);
                 BT_APP_TRACE_WARNING("unhandled BT_APP event (%d)\n", msg.sig);
                 break;
                 break;
             } // switch (msg.sig)
             } // switch (msg.sig)
-            
+
             if (msg.param) {
             if (msg.param) {
                 free(msg.param);
                 free(msg.param);
             }
             }

+ 20 - 24
examples/bluetooth/a2dp_sink/components/bluedroid_demos/app_project/SampleAV.c → examples/bluetooth/a2dp_sink/components/bt_app/app_project/SampleAV.c

@@ -2,13 +2,10 @@
 #include <stdbool.h>
 #include <stdbool.h>
 #include <stdlib.h>
 #include <stdlib.h>
 #include <string.h>
 #include <string.h>
-
 #include "freertos/FreeRTOS.h"
 #include "freertos/FreeRTOS.h"
 #include "freertos/task.h"
 #include "freertos/task.h"
 #include "freertos/timers.h"
 #include "freertos/timers.h"
-
 #include "esp_system.h"
 #include "esp_system.h"
-
 #include "bt_app_common.h"
 #include "bt_app_common.h"
 #include "esp_bt_main.h"
 #include "esp_bt_main.h"
 #include "esp_bt_device.h"
 #include "esp_bt_device.h"
@@ -16,47 +13,49 @@
 #include "esp_a2dp_api.h"
 #include "esp_a2dp_api.h"
 #include "esp_avrc_api.h"
 #include "esp_avrc_api.h"
 
 
-static uint32_t m_pkt_cnt = 0;
+
 
 
 typedef enum {
 typedef enum {
     BT_APP_EVT_STACK_ON = 0xa0,
     BT_APP_EVT_STACK_ON = 0xa0,
     BT_APP_EVT_MAX
     BT_APP_EVT_MAX
 } bt_app_evt_t;
 } bt_app_evt_t;
 
 
-
 typedef union {
 typedef union {
     esp_a2d_cb_param_t a2d;
     esp_a2d_cb_param_t a2d;
     esp_avrc_ct_cb_event_t rc;
     esp_avrc_ct_cb_event_t rc;
 } bt_app_evt_arg;
 } bt_app_evt_arg;
 
 
+
+static uint32_t m_pkt_cnt = 0;
 static esp_a2d_audio_state_t m_audio_state = ESP_A2D_AUDIO_STATE_STOPPED;
 static esp_a2d_audio_state_t m_audio_state = ESP_A2D_AUDIO_STATE_STOPPED;
 
 
 static void bt_app_handle_evt(uint16_t event, void *p_param);
 static void bt_app_handle_evt(uint16_t event, void *p_param);
-
 static void bt_app_handle_rc_evt(uint16_t event, void *p_param);
 static void bt_app_handle_rc_evt(uint16_t event, void *p_param);
+static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param);
+static void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len);
+static void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param);
+static void bt_app_handle_evt(uint16_t event, void *p_param);
 
 
 static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
 static void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param)
 {
 {
     switch (event) {
     switch (event) {
     case ESP_A2D_CONNECTION_STATE_EVT:
     case ESP_A2D_CONNECTION_STATE_EVT:
     case ESP_A2D_AUDIO_STATE_EVT:
     case ESP_A2D_AUDIO_STATE_EVT:
-    case ESP_A2D_AUDIO_CFG_EVT:
-    {
-	bt_app_transfer_context(bt_app_handle_evt, event, param, sizeof(bt_app_evt_arg), NULL);
+    case ESP_A2D_AUDIO_CFG_EVT: {
+        bt_app_transfer_context(bt_app_handle_evt, event, param, sizeof(bt_app_evt_arg), NULL);
         break;
         break;
     }
     }
     default:
     default:
-        BT_APP_TRACE_ERROR("===a2dp invalid cb event: %d\n", event);
+        BT_APP_TRACE_ERROR("===a2dp invalid cb event: %d===\n", event);
         break;
         break;
     }
     }
 }
 }
 
 
 static void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
 static void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len)
 {
 {
-    if (++m_pkt_cnt % 50 == 0) {
+    if (++m_pkt_cnt % 100 == 0) {
         BT_APP_TRACE_ERROR("audio data pkt cnt %u\n", m_pkt_cnt);
         BT_APP_TRACE_ERROR("audio data pkt cnt %u\n", m_pkt_cnt);
     }
     }
-    // EspAudioPlayerStreamWrite((uint8_t *)data, len, 10);
 }
 }
 
 
 static void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
 static void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param)
@@ -68,20 +67,20 @@ static void bt_app_rc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t
         break;
         break;
     }
     }
     default:
     default:
-        BT_APP_TRACE_ERROR("===a2dp invalid cb event: %d\n", event);
+        BT_APP_TRACE_ERROR("===a2dp invalid cb event: %d===\n", event);
         break;
         break;
     }
     }
 }
 }
 
 
 static void bt_app_handle_evt(uint16_t event, void *p_param)
 static void bt_app_handle_evt(uint16_t event, void *p_param)
 {
 {
-    BT_APP_TRACE_DEBUG("bt_app_handle_evt 0x%x\n", event);
+    BT_APP_TRACE_DEBUG("===bt_app_handle_evt 0x%x===\n", event);
     esp_a2d_cb_param_t *a2d = NULL;
     esp_a2d_cb_param_t *a2d = NULL;
     switch (event) {
     switch (event) {
     case BT_APP_EVT_STACK_ON: {
     case BT_APP_EVT_STACK_ON: {
         char *dev_name = "ESP_SPEAKER";
         char *dev_name = "ESP_SPEAKER";
 
 
-	esp_bt_dev_set_device_name(dev_name);
+        esp_bt_dev_set_device_name(dev_name);
 
 
         esp_a2d_register_callback(&bt_app_a2d_cb);
         esp_a2d_register_callback(&bt_app_a2d_cb);
         esp_a2d_register_data_callback(bt_app_a2d_data_cb);
         esp_a2d_register_data_callback(bt_app_a2d_data_cb);
@@ -90,7 +89,7 @@ static void bt_app_handle_evt(uint16_t event, void *p_param)
 
 
         esp_avrc_ct_init();
         esp_avrc_ct_init();
         esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
         esp_avrc_ct_register_callback(bt_app_rc_ct_cb);
-        
+
         esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
         esp_bt_gap_set_scan_mode(ESP_BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE);
         break;
         break;
     }
     }
@@ -107,21 +106,18 @@ static void bt_app_handle_evt(uint16_t event, void *p_param)
     }
     }
     case ESP_A2D_AUDIO_CFG_EVT: {
     case ESP_A2D_AUDIO_CFG_EVT: {
         a2d = (esp_a2d_cb_param_t *)(p_param);
         a2d = (esp_a2d_cb_param_t *)(p_param);
-        BT_APP_TRACE_EVENT("===a2dp audio_cfg_cb type %d ===\n", a2d->audio_cfg.mcc.type);
+        BT_APP_TRACE_EVENT("===a2dp audio_cfg_cb type %d===\n", a2d->audio_cfg.mcc.type);
+        // for now only SBC stream is supported
         if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) {
         if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) {
-            // temporarily hardcoded the PCM configuaration
             BT_APP_TRACE_EVENT("configure audio player\n");
             BT_APP_TRACE_EVENT("configure audio player\n");
-            // EspAudioPlayerStreamCfg(StreamSampleRate_44k, 2, StreamBitLen_16BIT);
-            // EspAudio_SetupStream("stream.pcm", InputSrcType_Stream);
-            // EspAudio_SetVolume(99);
         }
         }
         break;
         break;
     }
     }
     default:
     default:
-        BT_APP_TRACE_ERROR("===application invalid event: %d\n", event);
+        BT_APP_TRACE_ERROR("===application invalid event: %d===\n", event);
         break;
         break;
     }
     }
-    
+
 }
 }
 
 
 void bt_app_handle_rc_evt(uint16_t event, void *p_param)
 void bt_app_handle_rc_evt(uint16_t event, void *p_param)
@@ -140,7 +136,7 @@ void bt_app_handle_rc_evt(uint16_t event, void *p_param)
         break;
         break;
     }
     }
     default:
     default:
-        BT_APP_TRACE_ERROR("===application invalid event: %d\n", event);
+        BT_APP_TRACE_ERROR("===application invalid event: %d===\n", event);
         break;
         break;
     }
     }
 }
 }

+ 0 - 0
examples/bluetooth/a2dp_sink/components/bluedroid_demos/component.mk → examples/bluetooth/a2dp_sink/components/bt_app/component.mk


+ 1 - 1
examples/bluetooth/a2dp_sink/components/bluedroid_demos/include/bt_app_common.h → examples/bluetooth/a2dp_sink/components/bt_app/include/bt_app_common.h

@@ -19,7 +19,7 @@ extern int bt_app_trace_level;
 
 
 #define BT_APP_TRACE_ERROR(fmt, args...)       {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_ERROR) BT_APP_PRINTF(fmt, ## args);}
 #define BT_APP_TRACE_ERROR(fmt, args...)       {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_ERROR) BT_APP_PRINTF(fmt, ## args);}
 #define BT_APP_TRACE_WARNING(fmt, args...)     {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_WARNING) BT_APP_PRINTF(fmt, ## args);}
 #define BT_APP_TRACE_WARNING(fmt, args...)     {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_WARNING) BT_APP_PRINTF(fmt, ## args);}
-#define BT_APP_TRACE_API(fmt, args...)     {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_API) BT_APP_PRINTF(fmt, ## args);}
+#define BT_APP_TRACE_API(fmt, args...)         {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_API) BT_APP_PRINTF(fmt, ## args);}
 #define BT_APP_TRACE_EVENT(fmt, args...)       {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_EVENT) BT_APP_PRINTF(fmt, ## args);}
 #define BT_APP_TRACE_EVENT(fmt, args...)       {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_EVENT) BT_APP_PRINTF(fmt, ## args);}
 #define BT_APP_TRACE_DEBUG(fmt, args...)       {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_DEBUG) BT_APP_PRINTF(fmt, ## args);}
 #define BT_APP_TRACE_DEBUG(fmt, args...)       {if (bt_app_trace_level >= BT_APP_TRACE_LEVEL_DEBUG) BT_APP_PRINTF(fmt, ## args);}
 
 

+ 0 - 4
examples/bluetooth/a2dp_sink/main/demo_main.c

@@ -8,7 +8,6 @@
 
 
 #include "nvs_flash.h"
 #include "nvs_flash.h"
 #include "esp_system.h"
 #include "esp_system.h"
-// #include "EspAudio.h"
 
 
 extern void bte_main_boot_entry(void *);
 extern void bte_main_boot_entry(void *);
 extern void bt_app_task_start_up(void);
 extern void bt_app_task_start_up(void);
@@ -17,9 +16,6 @@ extern void bt_app_core_start(void);
 void app_main()
 void app_main()
 {
 {
     nvs_flash_init();
     nvs_flash_init();
-    // system_init();
-    // printf("Free memory: %d bytes\n", system_get_free_heap_size());
-    // EspAudio_Init();
     esp_bt_controller_init();
     esp_bt_controller_init();
 
 
     if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) {
     if (esp_bt_controller_enable(ESP_BT_MODE_BTDM) != ESP_OK) {

+ 0 - 23
examples/bluetooth/a2dp_sink/main/include/psramApi.h

@@ -1,23 +0,0 @@
-// Copyright 2010-2016 Espressif Systems (Shanghai) PTE LTD
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-
-//     http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "freertos/queue.h"
-#include "string.h"
-#include "stdlib.h"
-
-void psram_cache_enable(void);
-
-void psram_cache_memcpy(uint8_t *dst, uint8_t *src, uint16_t len);

+ 6 - 0
examples/bluetooth/a2dp_sink/sdkconfig.defaults

@@ -0,0 +1,6 @@
+# Override some defaults so BT stack is enabled and
+# Classic BT is enabled and BT_DRAM_RELEASE is disabled
+CONFIG_BT_ENABLED=y
+CONFIG_CLASSIC_BT_ENABLED=y
+# CONFIG_BT_DRAM_RELEASE is not set
+