From: wangmengyang Date: Fri, 2 Dec 2016 13:50:32 +0000 (+0800) Subject: component/bt: refactor the A2DP application task X-Git-Tag: v2.1-rc1~196^2~60 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=56bbc17ddcff8c4227a4f22ccc10940abfc7bb14;p=esp-idf component/bt: refactor the A2DP application task --- diff --git a/examples/09_a2dp/components/bluedroid_demos/app_core/bt_app_core.c b/examples/09_a2dp/components/bluedroid_demos/app_core/bt_app_core.c deleted file mode 100644 index 96c748d838..0000000000 --- a/examples/09_a2dp/components/bluedroid_demos/app_core/bt_app_core.c +++ /dev/null @@ -1,172 +0,0 @@ -#include -#include -#include - -#include "fixed_queue.h" -#include "gki.h" -#include "bt_defs.h" -#include "bt_trace.h" -#include "bt_types.h" -#include "allocator.h" - -#include "bta_api.h" -#include "bta_gatt_api.h" -#include "bt_app_common.h" - -#include "controller.h" -#include "thread.h" -#include "bt_app_common.h" - -static fixed_queue_t *bt_app_msg_queue; - -xQueueHandle xBtAppQueue; -xTaskHandle xBtAppTaskHandle; - -static void bt_app_context_switched(void *p_msg); -static void bt_app_send_msg(void *p_msg); -static void bt_app_task_handler(void *arg); -static void bta_app_msg_ready(fixed_queue_t *queue); -static void bt_app_task_shut_down(void); - - -extern void app_main_entry(void); - -static void bt_app_task_handler(void *arg) -{ - app_main_entry(); - BtTaskEvt_t *e; - for (;;) { - if (pdTRUE == xQueueReceive(xBtAppQueue, &e, (portTickType)portMAX_DELAY)) { - if (e->sig == 0xff) { - fixed_queue_process(bt_app_msg_queue); - } - osi_free(e); - } - } -} - -static void bt_app_task_post(void) -{ - BtTaskEvt_t *evt = (BtTaskEvt_t *)osi_malloc(sizeof(BtTaskEvt_t)); - if (evt == NULL) { - return; - } - - evt->sig = 0xff; - evt->par = 0; - - if (xQueueSend(xBtAppQueue, &evt, 10 / portTICK_RATE_MS) != pdTRUE) { - ets_printf("btdm_post failed\n"); - } -} - -static void bta_app_msg_ready(fixed_queue_t *queue) -{ - BT_HDR *p_msg; - while (!fixed_queue_is_empty(queue)) { - p_msg = (BT_HDR *)fixed_queue_dequeue(queue); - LOG_ERROR("bta_app_msg_ready, evt: %d\n", p_msg->event); - switch (p_msg->event) { - case BT_EVT_APP_CONTEXT_SWITCH: - bt_app_context_switched(p_msg); - break; - default: - LOG_ERROR("unhandled BT_APP event (%d)\n", p_msg->event & BT_EVT_MASK); - break; - } - GKI_freebuf(p_msg); - } -} - -static void bt_app_context_switched(void *p_msg) -{ - tBTAPP_CONTEXT_SWITCH_CBACK *p = (tBTAPP_CONTEXT_SWITCH_CBACK *) p_msg; - - if (p->p_cb) { - p->p_cb(p->event, p->p_param); - } -} - -static void bt_app_send_msg(void *p_msg) -{ - if (bt_app_msg_queue) { - fixed_queue_enqueue(bt_app_msg_queue, p_msg); - bt_app_task_post(); - } -} - -bt_status_t bt_app_transfer_context (tBTAPP_CBACK *p_cback, UINT16 event, char *p_params, int param_len, tBTAPP_COPY_CBACK *p_copy_cback) -{ - tBTAPP_CONTEXT_SWITCH_CBACK *p_msg; - - LOG_ERROR("btapp_transfer_context evt %d, len %d\n", event, param_len); - - /* allocate and send message that will be executed in btif context */ - if ((p_msg = (tBTAPP_CONTEXT_SWITCH_CBACK *) GKI_getbuf(sizeof(tBTAPP_CONTEXT_SWITCH_CBACK) + param_len)) != NULL) { - p_msg->hdr.event = BT_EVT_APP_CONTEXT_SWITCH; /* internal event */ - p_msg->p_cb = p_cback; - - p_msg->event = event; /* callback event */ - - /* check if caller has provided a copy callback to do the deep copy */ - if (p_copy_cback) { - p_copy_cback(event, p_msg->p_param, p_params); - } else if (p_params) { - memcpy(p_msg->p_param, p_params, param_len); /* callback parameter data */ - } - - bt_app_send_msg(p_msg); - return BT_STATUS_SUCCESS; - } else { - /* let caller deal with a failed allocation */ - return BT_STATUS_NOMEM; - } -} - -void bt_app_task_start_up(void) -{ - bt_app_msg_queue = fixed_queue_new(SIZE_MAX); - if (bt_app_msg_queue == NULL) { - goto error_exit; - } - //ke_event_callback_set(KE_EVENT_BT_APP_TASK, &bt_app_task_handler); - - xBtAppQueue = xQueueCreate(3, sizeof(void *)); - xTaskCreate(bt_app_task_handler, "BtaApp1T", 2048, NULL, configMAX_PRIORITIES - 3, xBtAppTaskHandle); - - fixed_queue_register_dequeue(bt_app_msg_queue, bta_app_msg_ready); - - return; - -error_exit: - LOG_ERROR("%s Unable to allocate resources for bt_app\n", __func__); - bt_app_task_shut_down(); -} - -static void bt_app_task_shut_down(void) -{ - fixed_queue_unregister_dequeue(bt_app_msg_queue); - fixed_queue_free(bt_app_msg_queue, NULL); - bt_app_msg_queue = NULL; - - vTaskDelete(xBtAppTaskHandle); - vQueueDelete(xBtAppQueue); -} - -/* -static void bt_app_upstreams_evt(UINT16 event, char *p_param) -{ - tBTA_DM_SEC *p_data = (tBTA_DM_SEC*)p_param; - switch (event) { - default: - break; - } -} - -static void bt_stack_evt(tBTA_DM_SEC_EVT event, tBTA_DM_SEC* p_data) -{ - LOG_ERROR("bt_stack_evt: %d\n", (uint16_t)event); - bt_app_transfer_context(bt_app_upstreams_evt, (uint16_t)event, - (void *)p_data, sizeof(tBTA_DM_SEC), NULL); -} -*/ diff --git a/examples/09_a2dp/components/bluedroid_demos/app_core/bt_app_main.c b/examples/09_a2dp/components/bluedroid_demos/app_core/bt_app_main.c new file mode 100644 index 0000000000..1f47962aa3 --- /dev/null +++ b/examples/09_a2dp/components/bluedroid_demos/app_core/bt_app_main.c @@ -0,0 +1,110 @@ +#include +#include +#include + +#include "bt_app_common.h" +#include "freertos/xtensa_api.h" +#include "freertos/FreeRTOSConfig.h" +#include "freertos/FreeRTOS.h" +#include "freertos/queue.h" +#include "freertos/task.h" + +int bt_app_trace_level = BT_APP_TRACE_LEVEL_DEBUG; + +static bool bt_app_post_msg(bt_app_msg_t *msg); +static void bt_app_context_switched(bt_app_msg_t *msg); +static void bt_app_task_handler(void *arg); +extern void app_main_entry(void); + +static xQueueHandle bt_app_task_queue = NULL; +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) +{ + bt_app_msg_t msg; + + memset(&msg, 0, sizeof(bt_app_msg_t)); + BT_APP_TRACE_EVENT("btapp_transfer_context evt 0x%x, len %d\n", event, param_len); + + msg.sig = BT_APP_SIG_CONTEXT_SWITCH; + msg.event = event; + msg.cb = p_cback; + + if (param_len == 0) { + return bt_app_post_msg(&msg); + } else if (p_params && param_len > 0) { + if ((msg.param = malloc(param_len)) != NULL) { + memcpy(msg.param, p_params, param_len); + /* check if caller has provided a copy callback to do the deep copy */ + if (p_copy_cback) { + p_copy_cback(&msg, msg.param, p_params); + } + return bt_app_post_msg(&msg); + } + } + + return false; +} + +static bool bt_app_post_msg(bt_app_msg_t *msg) +{ + if (msg == NULL) { + return false; + } + + if (xQueueSend(bt_app_task_queue, msg, 10 / portTICK_RATE_MS) != pdTRUE) { + BT_APP_TRACE_ERROR("bt_app msg post failed\n"); + return false; + } + return true; +} + +static void bt_app_context_switched(bt_app_msg_t *msg) +{ + BT_APP_TRACE_DEBUG(" context switched\n"); + if (msg->cb) { + msg->cb(msg->event, msg->param); + } +} + +static void bt_app_task_handler(void *arg) +{ + app_main_entry(); + bt_app_msg_t msg; + for (;;) { + 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); + switch (msg.sig) { + case BT_APP_SIG_CONTEXT_SWITCH: + bt_app_context_switched(&msg); + break; + default: + BT_APP_TRACE_WARNING("unhandled BT_APP event (%d)\n", msg.sig); + break; + } // switch (msg.sig) + + if (msg.param) { + free(msg.param); + } + } + } +} + +void bt_app_task_start_up(void) +{ + bt_app_task_queue = xQueueCreate(10, sizeof(bt_app_msg_t)); + xTaskCreate(bt_app_task_handler, "BtAppT", 2048, NULL, configMAX_PRIORITIES - 3, bt_app_task_handle); + return; +} + +void bt_app_task_shut_down(void) +{ + if (bt_app_task_handle) { + vTaskDelete(bt_app_task_handle); + bt_app_task_handle = NULL; + } + if (bt_app_task_queue) { + vQueueDelete(bt_app_task_queue); + bt_app_task_queue = NULL; + } +} diff --git a/examples/09_a2dp/components/bluedroid_demos/app_project/SampleBtSdp.c b/examples/09_a2dp/components/bluedroid_demos/app_project/SampleA2dp.c similarity index 71% rename from examples/09_a2dp/components/bluedroid_demos/app_project/SampleBtSdp.c rename to examples/09_a2dp/components/bluedroid_demos/app_project/SampleA2dp.c index 01f0208269..41f40c66cf 100644 --- a/examples/09_a2dp/components/bluedroid_demos/app_project/SampleBtSdp.c +++ b/examples/09_a2dp/components/bluedroid_demos/app_project/SampleA2dp.c @@ -22,13 +22,13 @@ typedef enum { BT_APP_EVT_STACK_ON = 0xa0, BT_APP_EVT_MAX -} tBT_APP_EVT; +} bt_app_evt_t; typedef union { esp_a2d_cb_param_t a2d; -} tBT_APP_EVT_DATA; +} bt_app_evt_arg; -static void bt_app_evt(tBT_APP_EVT event, tBT_APP_EVT_DATA *p_data); +static void bt_app_handle_evt(UINT16 event, void *p_param); static void bt_app_a2d_cb(uint32_t event, void *param) { @@ -37,11 +37,11 @@ static void bt_app_a2d_cb(uint32_t event, void *param) case ESP_A2D_AUDIO_STATE_EVT: case ESP_A2D_AUDIO_CFG_EVT: { - bt_app_evt(event, (tBT_APP_EVT_DATA *)param); + bt_app_transfer_context(bt_app_handle_evt, event, param, sizeof(bt_app_evt_arg), NULL); break; } default: - LOG_ERROR("===a2dp invalid cb event: %d\n", event); + BT_APP_TRACE_ERROR("===a2dp invalid cb event: %d\n", event); break; } } @@ -61,22 +61,23 @@ static void btav_set_device_class(void) 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); - LOG_ERROR("set class of device: major 0x%x, minor 0x%x, service 0x%x\n", + 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, char *p_param) +static void bt_app_handle_evt(UINT16 event, void *p_param) { + BT_APP_TRACE_EVENT("bt_app_handle_evt 0x%x\n", event); esp_a2d_cb_param_t *a2d = NULL; switch (event) { case BT_APP_EVT_STACK_ON: { - char *dev_name = "SDP_SERVER_CLIENT"; + char *dev_name = "ESP_SPEAKER"; BTM_SetTraceLevel(BT_TRACE_LEVEL_WARNING); btav_set_device_class(); BTA_DmSetDeviceName(dev_name); 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); esp_a2d_sink_init(); esp_bt_gap_set_scan_mode(BT_SCAN_MODE_CONNECTABLE_DISCOVERABLE); @@ -84,19 +85,20 @@ static void bt_app_handle_evt(UINT16 event, char *p_param) } case ESP_A2D_CONNECTION_STATE_EVT: { a2d = (esp_a2d_cb_param_t *)(p_param); - LOG_ERROR("===a2dp conn_state_cb %d ===\n", a2d->conn_stat.state); + BT_APP_TRACE_ERROR("===a2dp conn_state_cb %d ===\n", a2d->conn_stat.state); break; } case ESP_A2D_AUDIO_STATE_EVT: { a2d = (esp_a2d_cb_param_t *)(p_param); - LOG_ERROR("===a2dp audio_state_cb %d ===\n", a2d->audio_stat.state); + BT_APP_TRACE_ERROR("===a2dp audio_state_cb %d ===\n", a2d->audio_stat.state); break; } case ESP_A2D_AUDIO_CFG_EVT: { a2d = (esp_a2d_cb_param_t *)(p_param); - LOG_ERROR("===a2dp audio_cfg_cb type %d ===\n", a2d->audio_cfg.mcc.type); + BT_APP_TRACE_ERROR("===a2dp audio_cfg_cb type %d ===\n", a2d->audio_cfg.mcc.type); if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) { // temporarily hardcoded the PCM configuaration + BT_APP_TRACE_ERROR("configure audio player\n"); EspAudioPlayerStreamCfg(StreamSampleRate_44k, 2, StreamBitLen_16BIT); EspAudio_SetupStream("stream.pcm", InputSrcType_Stream); EspAudio_SetVolume(99); @@ -104,16 +106,10 @@ static void bt_app_handle_evt(UINT16 event, char *p_param) break; } default: - LOG_ERROR("===application invalid event: %d\n", event); + BT_APP_TRACE_ERROR("===application invalid event: %d\n", event); break; } -} - -static void bt_app_evt(tBT_APP_EVT event, tBT_APP_EVT_DATA *p_data) -{ - LOG_ERROR("bt_app_evt: %d\n", (uint16_t)event); - bt_app_transfer_context(bt_app_handle_evt, (uint16_t)event, - (void *)p_data, sizeof(tBT_APP_EVT_DATA), NULL); + } void app_main_entry(void) @@ -128,7 +124,6 @@ void app_main_entry(void) if (enable != BT_STATUS_SUCCESS) { return; } - - bt_app_evt(BT_APP_EVT_STACK_ON, NULL); -} + bt_app_transfer_context(bt_app_handle_evt, BT_APP_EVT_STACK_ON, NULL, 0, NULL); +} diff --git a/examples/09_a2dp/components/bluedroid_demos/include/bt_app_common.h b/examples/09_a2dp/components/bluedroid_demos/include/bt_app_common.h index 7b3f3cd71d..75c23f312d 100644 --- a/examples/09_a2dp/components/bluedroid_demos/include/bt_app_common.h +++ b/examples/09_a2dp/components/bluedroid_demos/include/bt_app_common.h @@ -2,28 +2,43 @@ #define __BT_APP_COMMON_H__ #include -#include "osi.h" -#include "bt_common_types.h" -#include "bt_defs.h" +#include +#include -/* BT APP Events */ -#define BT_EVT_APP (0xB000) -#define BT_EVT_APP_CONTEXT_SWITCH (0x0001 | BT_EVT_APP) +#define BT_APP_TRACE_LEVEL_NONE 0 /* No trace messages to be generated */ +#define BT_APP_TRACE_LEVEL_ERROR 1 /* Error condition trace messages */ +#define BT_APP_TRACE_LEVEL_WARNING 2 /* Warning condition trace messages */ +#define BT_APP_TRACE_LEVEL_API 3 /* API traces */ +#define BT_APP_TRACE_LEVEL_EVENT 4 /* Debug messages for events */ +#define BT_APP_TRACE_LEVEL_DEBUG 5 /* Full debug messages */ +#define BT_APP_TRACE_LEVEL_VERBOSE 6 /* Verbose debug messages */ -typedef void (tBTAPP_CBACK) (uint16_t event, char *p_param); -typedef void (tBTAPP_COPY_CBACK) (uint16_t event, char *p_dest, char *p_src); +#define BT_APP_PRINTF printf +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_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_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_SIG_CONTEXT_SWITCH (0x90) + +typedef void (* bt_app_cb_t) (uint16_t event, void *param); +/* context switch message */ typedef struct { - BT_HDR hdr; - tBTAPP_CBACK *p_cb; /* context switch callback */ + uint16_t sig; /* signal to bt_app_task */ + uint16_t event; /* message event id */ + bt_app_cb_t cb; /* context switch callback */ + void *param; /* parameter area needs to be last */ +} bt_app_msg_t; - /* parameters passed to callback */ - UINT16 event; /* message event id */ - char p_param[0]; /* parameter area needs to be last */ -} tBTAPP_CONTEXT_SWITCH_CBACK; +typedef void (* bt_app_copy_cb_t) (bt_app_msg_t *msg, void *p_dest, void *p_src); -bt_status_t bt_app_transfer_context (tBTAPP_CBACK *p_cback, UINT16 event, char *p_params, int param_len, tBTAPP_COPY_CBACK *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); void bt_app_task_start_up(void); +void bt_app_task_shut_down(void); #endif /* __BT_APP_COMMON_H__ */ diff --git a/examples/09_a2dp/main/demo_main.c b/examples/09_a2dp/main/demo_main.c index e556165e44..e5077f3d33 100755 --- a/examples/09_a2dp/main/demo_main.c +++ b/examples/09_a2dp/main/demo_main.c @@ -22,5 +22,4 @@ void app_main() EspAudio_Init(); bt_controller_init(); bt_app_task_start_up(); - // bte_main_boot_entry(bt_app_core_start); }