From 6163d214b379f5cfc646cf49b753e39b34e998dc Mon Sep 17 00:00:00 2001 From: wangmengyang Date: Mon, 26 Dec 2016 17:26:54 +0800 Subject: [PATCH] component/bt: API clean up for the A2DP example --- .../bt/bluedroid/api/include/esp_gap_bt_api.h | 1 + .../btc/profile/std/gap/btc_gap_bt.c | 14 ++++++ components/bt/bluedroid/include/bt_target.h | 16 ++++++- .../bluedroid_demos/app_project/SampleA2dp.c | 34 ++++----------- .../bluedroid_demos/btif/stack_manager.c | 43 +++++++++++++++---- .../include/esp_bt_stack_manager.h | 14 ++++++ 6 files changed, 85 insertions(+), 37 deletions(-) create mode 100644 examples/09_a2dp/components/bluedroid_demos/include/esp_bt_stack_manager.h diff --git a/components/bt/bluedroid/api/include/esp_gap_bt_api.h b/components/bt/bluedroid/api/include/esp_gap_bt_api.h index 6b958a8910..ccc6f80a92 100644 --- a/components/bt/bluedroid/api/include/esp_gap_bt_api.h +++ b/components/bt/bluedroid/api/include/esp_gap_bt_api.h @@ -28,4 +28,5 @@ typedef enum { esp_err_t esp_bt_gap_set_scan_mode(bt_scan_mode_t mode); +esp_err_t esp_bt_gap_set_device_name(const char *name); #endif /* __ESP_GAP_BT_API_H__ */ diff --git a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c index 52551ec0e1..275e600dfa 100644 --- a/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c +++ b/components/bt/bluedroid/btc/profile/std/gap/btc_gap_bt.c @@ -15,6 +15,7 @@ #include "esp_gap_bt_api.h" #include "bta_api.h" #include "bt_trace.h" +#include esp_err_t esp_bt_gap_set_scan_mode(bt_scan_mode_t mode) { @@ -47,3 +48,16 @@ esp_err_t esp_bt_gap_set_scan_mode(bt_scan_mode_t mode) return ESP_OK; } + +esp_err_t esp_bt_gap_set_device_name(const char *name) +{ + if (name == NULL || *name == '\0') { + return ESP_ERR_INVALID_ARG; + } + #define ESP_GAP_DEVICE_NAME_MAX (32) + char dev_name[ESP_GAP_DEVICE_NAME_MAX+1]; + strncpy(dev_name, name, ESP_GAP_DEVICE_NAME_MAX); + dev_name[ESP_GAP_DEVICE_NAME_MAX] = '\0'; + BTA_DmSetDeviceName(dev_name); + return ESP_OK; +} diff --git a/components/bt/bluedroid/include/bt_target.h b/components/bt/bluedroid/include/bt_target.h index 451badffa5..8a6cdefd71 100644 --- a/components/bt/bluedroid/include/bt_target.h +++ b/components/bt/bluedroid/include/bt_target.h @@ -466,7 +466,7 @@ #define BTM_DEFAULT_DISC_INTERVAL 0x0800 #endif -/* Default class of device +/* * {SERVICE_CLASS, MAJOR_CLASS, MINOR_CLASS} * * SERVICE_CLASS:0x5A (Bit17 -Networking,Bit19 - Capturing,Bit20 -Object Transfer,Bit22 -Telephony) @@ -474,8 +474,20 @@ * MINOR_CLASS:0x0C - SMART_PHONE * */ +#define BTA_DM_COD_SMARTPHONE {0x5A, 0x02, 0x0C} + +/* +* {SERVICE_CLASS, MAJOR_CLASS, MINOR_CLASS} +* +* SERVICE_CLASS:0x2C (Bit21 - Audio, Bit19 - Capturing) +* MAJOR_CLASS:0x04 - Audio/Video +* MINOR_CLASS:0x05 - LoudSpeaker +*/ +#define BTA_DM_COD_LOUDSPEAKER {0x2C, 0x04, 0x14} + +/* Default class of device */ #ifndef BTA_DM_COD -#define BTA_DM_COD {0x5A, 0x02, 0x0C} +#define BTA_DM_COD BTA_DM_COD_LOUDSPEAKER #endif /* The number of SCO links. */ diff --git a/examples/09_a2dp/components/bluedroid_demos/app_project/SampleA2dp.c b/examples/09_a2dp/components/bluedroid_demos/app_project/SampleA2dp.c index 41f40c66cf..3439c1049b 100644 --- a/examples/09_a2dp/components/bluedroid_demos/app_project/SampleA2dp.c +++ b/examples/09_a2dp/components/bluedroid_demos/app_project/SampleA2dp.c @@ -11,14 +11,11 @@ #include "EspAudioCom.h" #include "bt_app_common.h" -#include "btif_stack_manager.h" +#include "esp_bt_stack_manager.h" #include "esp_gap_bt_api.h" #include "bta_api.h" #include "esp_a2dp_api.h" -/* utl_set_device_class() */ -#include "utl.h" - typedef enum { BT_APP_EVT_STACK_ON = 0xa0, BT_APP_EVT_MAX @@ -48,23 +45,9 @@ static void bt_app_a2d_cb(uint32_t event, void *param) static void bt_app_a2d_data_cb(uint8_t *data, uint32_t len) { - // uint32_t t_now = system_get_time(); - // printf("t: %u, l %d\n", t_now, len); - EspAudioPlayerStreamWrite(data, len); + EspAudioPlayerStreamWrite(data, len, 10); } -static void btav_set_device_class(void) -{ - tBTA_UTL_COD cod; - memset(&cod, 0, sizeof(tBTA_UTL_COD)); - cod.major = BTM_COD_MAJOR_AUDIO; - cod.minor = BTM_COD_MINOR_LOUDSPEAKER; - cod.service = BTM_COD_SERVICE_CAPTURING | BTM_COD_SERVICE_AUDIO; - utl_set_device_class(&cod, BTA_UTL_SET_COD_ALL); - BT_APP_TRACE_ERROR("set class of device: major 0x%x, minor 0x%x, service 0x%x\n", - cod.major, cod.minor, cod.service); -} - static void bt_app_handle_evt(UINT16 event, void *p_param) { BT_APP_TRACE_EVENT("bt_app_handle_evt 0x%x\n", event); @@ -72,9 +55,8 @@ static void bt_app_handle_evt(UINT16 event, void *p_param) switch (event) { case BT_APP_EVT_STACK_ON: { char *dev_name = "ESP_SPEAKER"; - BTM_SetTraceLevel(BT_TRACE_LEVEL_WARNING); - btav_set_device_class(); - BTA_DmSetDeviceName(dev_name); + // BTM_SetTraceLevel(BT_TRACE_LEVEL_WARNING); + esp_bt_gap_set_device_name(dev_name); esp_a2d_register_callback(bt_app_a2d_cb); esp_a2d_register_data_callback(bt_app_a2d_data_cb); @@ -115,13 +97,13 @@ static void bt_app_handle_evt(UINT16 event, void *p_param) void app_main_entry(void) { bt_status_t init, enable; - init = BTIF_InitStack(); - if (init != BT_STATUS_SUCCESS) { + init = esp_bt_init_stack(); + if (init != ESP_OK) { return; } - enable = BTIF_EnableStack(); - if (enable != BT_STATUS_SUCCESS) { + enable = esp_bt_enable_stack(); + if (enable != ESP_OK) { return; } diff --git a/examples/09_a2dp/components/bluedroid_demos/btif/stack_manager.c b/examples/09_a2dp/components/bluedroid_demos/btif/stack_manager.c index a93c3e8f6f..f44a707cf9 100644 --- a/examples/09_a2dp/components/bluedroid_demos/btif/stack_manager.c +++ b/examples/09_a2dp/components/bluedroid_demos/btif/stack_manager.c @@ -1,6 +1,6 @@ #include #include -#include "btif_stack_manager.h" +#include "esp_bt_stack_manager.h" #include "stack_manager.h" #include "bt_defs.h" #include "bt_trace.h" @@ -112,24 +112,49 @@ static bt_status_t event_clean_up_stack(void) return BT_STATUS_SUCCESS; } -bt_status_t BTIF_InitStack(void) +esp_err_t esp_bt_init_stack(void) { - return event_init_stack(); + bt_status_t status; + status = event_init_stack(); + switch (status) { + case BT_STATUS_SUCCESS: return ESP_OK; + case BT_STATUS_DONE: return ESP_ERR_INVALID_STATE; + default: return ESP_FAIL; + } } -bt_status_t BTIF_EnableStack(void) +esp_err_t esp_bt_deinit_stack(void) { - return event_start_up_stack(); + bt_status_t status; + status = event_clean_up_stack(); + switch (status) { + case BT_STATUS_SUCCESS: return ESP_OK; + default: return ESP_ERR_INVALID_STATE; + } } -bt_status_t BTIF_DisableStack(void) +esp_err_t esp_bt_enable_stack(void) { - return event_shut_down_stack(); + bt_status_t status; + status = event_start_up_stack(); + switch (status) { + case BT_STATUS_SUCCESS: return ESP_OK; + case BT_STATUS_NOT_READY: + case BT_STATUS_DONE: + return ESP_ERR_INVALID_STATE; + default: return ESP_FAIL; + } } -bt_status_t BTIF_CleanUpStack(void) +esp_err_t esp_bt_disable_stack(void) { - return event_clean_up_stack(); + bt_status_t status; + status = event_shut_down_stack(); + switch (status) { + case BT_STATUS_SUCCESS: return ESP_OK; + case BT_STATUS_DONE: return ESP_ERR_INVALID_STATE; + default: return ESP_FAIL; + } } bool stack_manager_is_stack_running(void) diff --git a/examples/09_a2dp/components/bluedroid_demos/include/esp_bt_stack_manager.h b/examples/09_a2dp/components/bluedroid_demos/include/esp_bt_stack_manager.h new file mode 100644 index 0000000000..33045b4e0c --- /dev/null +++ b/examples/09_a2dp/components/bluedroid_demos/include/esp_bt_stack_manager.h @@ -0,0 +1,14 @@ +#ifndef __ESP_BT_STACK_MANAGER_H__ +#define __ESP_BT_STACK_MANAGER_H__ + +#include "esp_err.h" + +esp_err_t esp_bt_init_stack(void); + +esp_err_t esp_bt_deinit_stack(void); + +esp_err_t esp_bt_enable_stack(void); + +esp_err_t esp_bt_disable_stack(void); + +#endif /* __ESP_BT_STACK_MANAGER_H__ */ -- 2.40.0