From 6a13a3b9d964c55b184313d3e3f7efbe903d8f68 Mon Sep 17 00:00:00 2001 From: Jack Chen Date: Tue, 9 Feb 2021 13:29:42 +0800 Subject: [PATCH] main: clean up log outputs --- main/inc/user/ble_app.h | 4 +- main/inc/user/ble_gatts.h | 4 +- main/inc/user/bt_app_core.h | 15 +-- main/inc/user/bt_av.h | 17 +-- main/src/board/ws2812.c | 2 +- main/src/user/audio_player.c | 3 +- main/src/user/audio_render.c | 4 +- main/src/user/ble_app.c | 18 +-- main/src/user/ble_gatts.c | 68 +++++------ main/src/user/bt_app.c | 5 +- main/src/user/bt_app_core.c | 7 +- main/src/user/bt_av.c | 216 +++++++++++++++++------------------ main/src/user/led.c | 1 - main/src/user/ota.c | 22 ++-- main/src/user/vfx.c | 2 +- 15 files changed, 173 insertions(+), 215 deletions(-) diff --git a/main/inc/user/ble_app.h b/main/inc/user/ble_app.h index 19e218b7..d1f9e34e 100644 --- a/main/inc/user/ble_app.h +++ b/main/inc/user/ble_app.h @@ -8,9 +8,7 @@ #ifndef INC_USER_BLE_APP_H_ #define INC_USER_BLE_APP_H_ -#include "esp_gap_ble_api.h" - -extern esp_ble_adv_params_t adv_params; +extern void ble_gap_start_advertising(void); extern void ble_app_init(void); diff --git a/main/inc/user/ble_gatts.h b/main/inc/user/ble_gatts.h index cb6d9fec..5894cdbd 100644 --- a/main/inc/user/ble_gatts.h +++ b/main/inc/user/ble_gatts.h @@ -32,9 +32,7 @@ typedef struct gatts_profile_inst { esp_bt_uuid_t descr_uuid; } gatts_profile_inst_t; -extern gatts_profile_inst_t gatts_profile_tbl[]; - -extern void gatts_ota_send_notification(const char *data, uint32_t len); +extern gatts_profile_inst_t gatts_profile_tbl[PROFILE_IDX_MAX]; extern void ble_gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); diff --git a/main/inc/user/bt_app_core.h b/main/inc/user/bt_app_core.h index 6f2f3087..305301fc 100644 --- a/main/inc/user/bt_app_core.h +++ b/main/inc/user/bt_app_core.h @@ -12,12 +12,8 @@ #define BT_APP_SIG_WORK_DISPATCH (0x01) -/** - * @brief handler for the dispatched work - */ typedef void (*bt_app_cb_t)(uint16_t event, void *param); -/* message to be sent */ typedef struct { uint16_t sig; /*!< signal to bt_app_task */ uint16_t event; /*!< message event id */ @@ -25,16 +21,9 @@ typedef struct { void *param; /*!< parameter area needs to be last */ } bt_app_msg_t; -/** - * @brief parameter deep-copy function to be customized - */ typedef void (*bt_app_copy_cb_t)(bt_app_msg_t *msg, void *p_dest, void *p_src); -/** - * @brief work dispatcher for the application task - */ -bool bt_app_work_dispatch(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); +extern bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event, void *p_params, int param_len, bt_app_copy_cb_t p_copy_cback); +extern void bt_app_task_start_up(void); #endif /* INC_USER_BT_APP_CORE_H_ */ diff --git a/main/inc/user/bt_av.h b/main/inc/user/bt_av.h index 2b510db5..48d20998 100644 --- a/main/inc/user/bt_av.h +++ b/main/inc/user/bt_av.h @@ -16,19 +16,8 @@ extern esp_bd_addr_t a2d_remote_bda; extern unsigned int a2d_sample_rate; -/** - * @brief callback function for A2DP sink - */ -void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param); - -/** - * @brief callback function for A2DP sink audio data stream - */ -void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len); - -/** - * @brief callback function for AVRCP controller - */ -void bt_app_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param); +extern void bt_app_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param); +extern void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param); +extern void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len); #endif /* INC_USER_BT_AV_H_*/ diff --git a/main/src/board/ws2812.c b/main/src/board/ws2812.c index 5027a866..7f5c8064 100644 --- a/main/src/board/ws2812.c +++ b/main/src/board/ws2812.c @@ -37,7 +37,7 @@ void ws2812_init_board(void) led_strip_config_t strip_config = LED_STRIP_DEFAULT_CONFIG(WS2812_X * WS2812_Y, RMT_CHANNEL_0); strip = led_strip_new_rmt_ws2812(&strip_config); if (!strip) { - ESP_LOGE(TAG, "initialization failed"); + ESP_LOGE(TAG, "initialization failed."); } else { ESP_LOGI(TAG, "initialized, din: %d", CONFIG_WS2812_DIN_PIN); } diff --git a/main/src/user/audio_player.c b/main/src/user/audio_player.c index c326651f..e3a40881 100644 --- a/main/src/user/audio_player.c +++ b/main/src/user/audio_player.c @@ -90,7 +90,7 @@ static void audio_player_task(void *pvParameters) xEventGroupSetBits(user_event_group, AUDIO_PLAYER_IDLE_BIT); xEventGroupClearBits(user_event_group, AUDIO_PLAYER_RUN_BIT); - ESP_LOGE(TAG, "allocate memory failed."); + ESP_LOGE(TAG, "failed to allocate memory"); playback_pending = false; @@ -142,7 +142,6 @@ static void audio_player_task(void *pvParameters) void audio_player_play_file(mp3_file_t idx) { if (idx >= sizeof(mp3_file_ptr) / sizeof(mp3_file_ptr[0])) { - ESP_LOGE(TAG, "invalid file index"); return; } diff --git a/main/src/user/audio_render.c b/main/src/user/audio_render.c index 68605bed..19886200 100644 --- a/main/src/user/audio_render.c +++ b/main/src/user/audio_render.c @@ -47,11 +47,11 @@ static void audio_render_task(void *pvParameter) if (remain >= FFT_BLOCK_SIZE) { delay = 0; - data = (uint8_t *)xRingbufferReceiveUpTo(audio_buff, &size, portMAX_DELAY, FFT_BLOCK_SIZE); + data = xRingbufferReceiveUpTo(audio_buff, &size, portMAX_DELAY, FFT_BLOCK_SIZE); } else if (remain > 0) { delay = 0; - data = (uint8_t *)xRingbufferReceiveUpTo(audio_buff, &size, portMAX_DELAY, remain); + data = xRingbufferReceiveUpTo(audio_buff, &size, portMAX_DELAY, remain); } else { if (++delay <= 10) { vTaskDelay(256000 / a2d_sample_rate / portTICK_RATE_MS); diff --git a/main/src/user/ble_app.c b/main/src/user/ble_app.c index 2f59eb88..1c9cc040 100644 --- a/main/src/user/ble_app.c +++ b/main/src/user/ble_app.c @@ -19,7 +19,7 @@ #define BLE_APP_TAG "ble_app" #define BLE_GAP_TAG "ble_gap" -esp_ble_adv_params_t adv_params = { +static esp_ble_adv_params_t adv_params = { .adv_int_min = 0x20, .adv_int_max = 0x40, .adv_type = ADV_TYPE_IND, @@ -35,14 +35,13 @@ static void ble_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_p esp_ble_gap_start_advertising(&adv_params); break; case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: - // advertising start complete event to indicate advertising start successfully or failed if (param->adv_start_cmpl.status != ESP_BT_STATUS_SUCCESS) { - ESP_LOGE(BLE_GAP_TAG, "advertising start failed"); + ESP_LOGE(BLE_GAP_TAG, "failed to start advertising"); } break; case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: if (param->adv_stop_cmpl.status != ESP_BT_STATUS_SUCCESS) { - ESP_LOGE(BLE_GAP_TAG, "advertising stop failed"); + ESP_LOGE(BLE_GAP_TAG, "failed to stop advertising"); } break; case ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT: @@ -54,7 +53,7 @@ static void ble_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_p } } -static void gap_config_adv_data(const char *name) +static void ble_gap_config_adv_data(const char *name) { size_t len = strlen(name); uint8_t raw_adv_data[len + 5]; @@ -71,10 +70,15 @@ static void gap_config_adv_data(const char *name) esp_err_t raw_adv_ret = esp_ble_gap_config_adv_data_raw(raw_adv_data, sizeof(raw_adv_data)); if (raw_adv_ret) { - ESP_LOGE(BLE_GAP_TAG, "config raw adv data failed, error code = 0x%x ", raw_adv_ret); + ESP_LOGE(BLE_GAP_TAG, "failed to config raw adv data: %d", raw_adv_ret); } } +void ble_gap_start_advertising(void) +{ + esp_ble_gap_start_advertising(&adv_params); +} + void ble_app_init(void) { xEventGroupSetBits(user_event_group, BLE_GATTS_IDLE_BIT); @@ -88,7 +92,7 @@ void ble_app_init(void) ESP_ERROR_CHECK(esp_ble_gatt_set_local_mtu(ESP_GATT_MAX_MTU_SIZE)); - gap_config_adv_data(CONFIG_BT_NAME); + ble_gap_config_adv_data(CONFIG_BT_NAME); ESP_LOGI(BLE_APP_TAG, "started."); } diff --git a/main/src/user/ble_gatts.c b/main/src/user/ble_gatts.c index 36d5b5f7..ca746d4e 100644 --- a/main/src/user/ble_gatts.c +++ b/main/src/user/ble_gatts.c @@ -43,20 +43,11 @@ static const char *s_gatts_conn_state_str[] = {"disconnected", "connected"}; static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param); -/* one gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */ gatts_profile_inst_t gatts_profile_tbl[PROFILE_IDX_MAX] = { [PROFILE_IDX_OTA] = { .gatts_cb = profile_ota_event_handler, .gatts_if = ESP_GATT_IF_NONE }, [PROFILE_IDX_VFX] = { .gatts_cb = profile_vfx_event_handler, .gatts_if = ESP_GATT_IF_NONE } }; -void gatts_ota_send_notification(const char *data, uint32_t len) -{ - esp_ble_gatts_send_indicate(gatts_profile_tbl[PROFILE_IDX_OTA].gatts_if, - gatts_profile_tbl[PROFILE_IDX_OTA].conn_id, - gatts_profile_tbl[PROFILE_IDX_OTA].char_handle, - len, (uint8_t *)data, false); -} - static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { switch (event) { @@ -118,7 +109,7 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t NULL, NULL); if (add_char_ret) { - ESP_LOGE(GATTS_OTA_TAG, "add char failed, error code =%x", add_char_ret); + ESP_LOGE(GATTS_OTA_TAG, "failed to add char: %d", add_char_ret); } break; case ESP_GATTS_ADD_INCL_SRVC_EVT: @@ -134,7 +125,7 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t NULL, NULL); if (add_descr_ret) { - ESP_LOGE(GATTS_OTA_TAG, "add char descr failed, error code =%x", add_descr_ret); + ESP_LOGE(GATTS_OTA_TAG, "failed to add char descr: %d", add_descr_ret); } break; case ESP_GATTS_ADD_CHAR_DESCR_EVT: @@ -146,35 +137,37 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t break; case ESP_GATTS_STOP_EVT: break; - case ESP_GATTS_CONNECT_EVT: { + case ESP_GATTS_CONNECT_EVT: xEventGroupClearBits(user_event_group, BLE_GATTS_IDLE_BIT); esp_ble_gap_stop_advertising(); - uint8_t *bda = param->connect.remote_bda; ESP_LOGI(GATTS_OTA_TAG, "GATTS connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]", - s_gatts_conn_state_str[1], bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); + s_gatts_conn_state_str[1], + param->connect.remote_bda[0], param->connect.remote_bda[1], + param->connect.remote_bda[2], param->connect.remote_bda[3], + param->connect.remote_bda[4], param->connect.remote_bda[5]); gatts_profile_tbl[PROFILE_IDX_OTA].conn_id = param->connect.conn_id; break; - } - case ESP_GATTS_DISCONNECT_EVT: { - uint8_t *bda = param->connect.remote_bda; + case ESP_GATTS_DISCONNECT_EVT: ESP_LOGI(GATTS_OTA_TAG, "GATTS connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]", - s_gatts_conn_state_str[0], bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); + s_gatts_conn_state_str[0], + param->connect.remote_bda[0], param->connect.remote_bda[1], + param->connect.remote_bda[2], param->connect.remote_bda[3], + param->connect.remote_bda[4], param->connect.remote_bda[5]); ota_end(); EventBits_t uxBits = xEventGroupGetBits(user_event_group); if (!(uxBits & OS_PWR_RESET_BIT) && !(uxBits & OS_PWR_SLEEP_BIT)) { - esp_ble_gap_start_advertising(&adv_params); + ble_gap_start_advertising(); } xEventGroupSetBits(user_event_group, BLE_GATTS_IDLE_BIT); break; - } case ESP_GATTS_CONF_EVT: case ESP_GATTS_OPEN_EVT: case ESP_GATTS_CANCEL_OPEN_EVT: @@ -285,7 +278,7 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t app_setenv("AIN_INIT_CFG", &ain_mode, sizeof(ain_mode_t)); #endif } else { - ESP_LOGE(GATTS_VFX_TAG, "command 0x%02X error", param->write.value[0]); + ESP_LOGE(GATTS_VFX_TAG, "invalid command: 0x%02X", param->write.value[0]); } break; default: @@ -320,7 +313,7 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t NULL, NULL); if (add_char_ret) { - ESP_LOGE(GATTS_VFX_TAG, "add char failed, error code =%x", add_char_ret); + ESP_LOGE(GATTS_VFX_TAG, "failed to add char: %d", add_char_ret); } break; case ESP_GATTS_ADD_INCL_SRVC_EVT: @@ -336,7 +329,7 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t NULL, NULL); if (add_descr_ret) { - ESP_LOGE(GATTS_VFX_TAG, "add char descr failed, error code =%x", add_descr_ret); + ESP_LOGE(GATTS_VFX_TAG, "failed to add char descr: %d", add_descr_ret); } break; case ESP_GATTS_ADD_CHAR_DESCR_EVT: @@ -348,22 +341,23 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t break; case ESP_GATTS_STOP_EVT: break; - case ESP_GATTS_CONNECT_EVT: { - uint8_t *bda = param->connect.remote_bda; + case ESP_GATTS_CONNECT_EVT: ESP_LOGI(GATTS_VFX_TAG, "GATTS connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]", - s_gatts_conn_state_str[1], bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); + s_gatts_conn_state_str[1], + param->connect.remote_bda[0], param->connect.remote_bda[1], + param->connect.remote_bda[2], param->connect.remote_bda[3], + param->connect.remote_bda[4], param->connect.remote_bda[5]); gatts_profile_tbl[PROFILE_IDX_VFX].conn_id = param->connect.conn_id; break; - } - case ESP_GATTS_DISCONNECT_EVT: { - uint8_t *bda = param->connect.remote_bda; + case ESP_GATTS_DISCONNECT_EVT: ESP_LOGI(GATTS_VFX_TAG, "GATTS connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]", - s_gatts_conn_state_str[0], bda[0], bda[1], bda[2], bda[3], bda[4], bda[5]); - + s_gatts_conn_state_str[0], + param->connect.remote_bda[0], param->connect.remote_bda[1], + param->connect.remote_bda[2], param->connect.remote_bda[3], + param->connect.remote_bda[4], param->connect.remote_bda[5]); break; - } case ESP_GATTS_CONF_EVT: case ESP_GATTS_OPEN_EVT: case ESP_GATTS_CANCEL_OPEN_EVT: @@ -377,23 +371,17 @@ static void profile_vfx_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t void ble_gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) { - /* if event is register event, store the gatts_if for each profile */ if (event == ESP_GATTS_REG_EVT) { if (param->reg.status == ESP_GATT_OK) { gatts_profile_tbl[param->reg.app_id].gatts_if = gatts_if; } else { - ESP_LOGE(BLE_GATTS_TAG, "reg app failed, app_id %04x, status %d", - param->reg.app_id, - param->reg.status); + ESP_LOGE(BLE_GATTS_TAG, "reg app failed, app_id: %04x, status: %d", param->reg.app_id, param->reg.status); return; } } - /* if the gatts_if equal to profile A, call profile A cb handler, - * so here call each profile's callback */ for (int idx = 0; idx < PROFILE_IDX_MAX; idx++) { - if (gatts_if == ESP_GATT_IF_NONE || /* ESP_GATT_IF_NONE, not specify a certain gatt_if, need to call every profile cb function */ - gatts_if == gatts_profile_tbl[idx].gatts_if) { + if (gatts_if == ESP_GATT_IF_NONE || gatts_if == gatts_profile_tbl[idx].gatts_if) { if (gatts_profile_tbl[idx].gatts_cb) { gatts_profile_tbl[idx].gatts_cb(event, gatts_if, param); } diff --git a/main/src/user/bt_app.c b/main/src/user/bt_app.c index af6dbd2a..35a27879 100644 --- a/main/src/user/bt_app.c +++ b/main/src/user/bt_app.c @@ -27,7 +27,6 @@ esp_bd_addr_t last_remote_bda = {0}; -/* event for handler "bt_app_hdl_stack_up */ enum { BT_APP_EVT_STACK_UP = 0 }; @@ -39,7 +38,7 @@ static void bt_app_gap_cb(esp_bt_gap_cb_event_t event, esp_bt_gap_cb_param_t *pa if (param->auth_cmpl.stat == ESP_BT_STATUS_SUCCESS) { ESP_LOGI(BT_GAP_TAG, "authentication success: %s", param->auth_cmpl.device_name); } else { - ESP_LOGE(BT_GAP_TAG, "authentication failed, status: %d", param->auth_cmpl.stat); + ESP_LOGE(BT_GAP_TAG, "authentication failed: %d", param->auth_cmpl.stat); } break; default: @@ -84,7 +83,7 @@ static void bt_app_hdl_stack_evt(uint16_t event, void *p_param) break; default: - ESP_LOGE(BT_APP_TAG, "%s unhandled evt %d", __func__, event); + ESP_LOGW(BT_APP_TAG, "unhandled evt: %d", event); break; } } diff --git a/main/src/user/bt_app_core.c b/main/src/user/bt_app_core.c index 5d3fcbbb..93420248 100644 --- a/main/src/user/bt_app_core.c +++ b/main/src/user/bt_app_core.c @@ -34,13 +34,12 @@ static void bt_app_task(void *pvParameter) while (1) { if (pdTRUE == xQueueReceive(s_bt_app_task_queue, &msg, portMAX_DELAY)) { - ESP_LOGD(BT_APP_CORE_TAG, "%s, sig 0x%x, 0x%x", __func__, msg.sig, msg.event); switch (msg.sig) { case BT_APP_SIG_WORK_DISPATCH: bt_app_work_dispatched(&msg); break; default: - ESP_LOGW(BT_APP_CORE_TAG, "%s, unhandled sig: %d", __func__, msg.sig); + ESP_LOGW(BT_APP_CORE_TAG, "unhandled sig: %d", msg.sig); break; } @@ -58,7 +57,6 @@ static bool bt_app_send_msg(bt_app_msg_t *msg) } if (xQueueSend(s_bt_app_task_queue, msg, 10 / portTICK_RATE_MS) != pdTRUE) { - ESP_LOGE(BT_APP_CORE_TAG, "%s xQueue send failed", __func__); return false; } @@ -78,10 +76,11 @@ bool bt_app_work_dispatch(bt_app_cb_t p_cback, uint16_t event, void *p_params, i } 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_send_msg(&msg); } } diff --git a/main/src/user/bt_av.c b/main/src/user/bt_av.c index 9db8569f..54dc257a 100644 --- a/main/src/user/bt_av.c +++ b/main/src/user/bt_av.c @@ -29,7 +29,7 @@ #define BT_RC_CT_TAG "bt_rc_ct" #define BT_RC_RN_TAG "bt_rc_rn" -// AVRCP used transaction label +// AVRCP transaction labels #define APP_RC_CT_TL_GET_CAPS (0) #define APP_RC_CT_TL_GET_META_DATA (1) #define APP_RC_CT_TL_RN_TRACK_CHANGE (2) @@ -39,89 +39,25 @@ esp_bd_addr_t a2d_remote_bda = {0}; unsigned int a2d_sample_rate = 16000; -/* a2dp event handler */ -static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param); -/* avrc CT event handler */ -static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param); +static esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap = {0}; static const char *s_a2d_conn_state_str[] = {"disconnected", "connecting", "connected", "disconnecting"}; static const char *s_a2d_audio_state_str[] = {"suspended", "stopped", "started"}; - -static esp_avrc_rn_evt_cap_mask_t s_avrc_peer_rn_cap; - -/* callback for A2DP sink */ -void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param) -{ - switch (event) { - case ESP_A2D_CONNECTION_STATE_EVT: - case ESP_A2D_AUDIO_STATE_EVT: - case ESP_A2D_AUDIO_CFG_EVT: - bt_app_work_dispatch(bt_av_hdl_a2d_evt, event, param, sizeof(esp_a2d_cb_param_t), NULL); - break; - default: - break; - } -} - -void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len) -{ - if (audio_buff) { - uint32_t pkt = 0, remain = 0; - - for (pkt = 0; pkt < len / FFT_BLOCK_SIZE; pkt++) { - xRingbufferSend(audio_buff, (void *)(data + pkt * FFT_BLOCK_SIZE), FFT_BLOCK_SIZE, portMAX_DELAY); - taskYIELD(); - } - - remain = len - pkt * FFT_BLOCK_SIZE; - if (remain != 0) { - xRingbufferSend(audio_buff, (void *)(data + pkt * FFT_BLOCK_SIZE), remain, portMAX_DELAY); - taskYIELD(); - } - } -} - -static void bt_app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param) -{ - esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(param); - uint8_t *attr_text = (uint8_t *)malloc(rc->meta_rsp.attr_length + 1); - - memcpy(attr_text, rc->meta_rsp.attr_text, rc->meta_rsp.attr_length); - - attr_text[rc->meta_rsp.attr_length] = 0; - rc->meta_rsp.attr_text = attr_text; -} - -void bt_app_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param) -{ - switch (event) { - case ESP_AVRC_CT_METADATA_RSP_EVT: - bt_app_alloc_meta_buffer(param); - /* fall through */ - case ESP_AVRC_CT_CONNECTION_STATE_EVT: - case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: - case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: - case ESP_AVRC_CT_REMOTE_FEATURES_EVT: - case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT: - bt_app_work_dispatch(bt_av_hdl_avrc_ct_evt, event, param, sizeof(esp_avrc_ct_cb_param_t), NULL); - break; - default: - break; - } -} +static const char *s_avrc_conn_state_str[] = {"disconnected", "connected"}; static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param) { - esp_a2d_cb_param_t *a2d = (esp_a2d_cb_param_t *)(p_param); + esp_a2d_cb_param_t *param = (esp_a2d_cb_param_t *)p_param; + switch (event) { case ESP_A2D_CONNECTION_STATE_EVT: ESP_LOGI(BT_A2D_TAG, "A2DP connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]", - s_a2d_conn_state_str[a2d->conn_stat.state], - a2d->conn_stat.remote_bda[0], a2d->conn_stat.remote_bda[1], - a2d->conn_stat.remote_bda[2], a2d->conn_stat.remote_bda[3], - a2d->conn_stat.remote_bda[4], a2d->conn_stat.remote_bda[5]); + s_a2d_conn_state_str[param->conn_stat.state], + param->conn_stat.remote_bda[0], param->conn_stat.remote_bda[1], + param->conn_stat.remote_bda[2], param->conn_stat.remote_bda[3], + param->conn_stat.remote_bda[4], param->conn_stat.remote_bda[5]); - if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) { + if (param->conn_stat.state == ESP_A2D_CONNECTION_STATE_DISCONNECTED) { memset(&a2d_remote_bda, 0x00, sizeof(esp_bd_addr_t)); EventBits_t uxBits = xEventGroupGetBits(user_event_group); @@ -139,10 +75,10 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param) led_set_mode(LED_MODE_IDX_BLINK_M0); #endif xEventGroupSetBits(user_event_group, BT_A2DP_IDLE_BIT); - } else if (a2d->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED) { + } else if (param->conn_stat.state == ESP_A2D_CONNECTION_STATE_CONNECTED) { xEventGroupClearBits(user_event_group, BT_A2DP_IDLE_BIT); - memcpy(&a2d_remote_bda, a2d->conn_stat.remote_bda, sizeof(esp_bd_addr_t)); + memcpy(&a2d_remote_bda, param->conn_stat.remote_bda, sizeof(esp_bd_addr_t)); if (memcmp(&last_remote_bda, &a2d_remote_bda, sizeof(esp_bd_addr_t)) != 0) { memcpy(&last_remote_bda, &a2d_remote_bda, sizeof(esp_bd_addr_t)); @@ -171,10 +107,10 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param) } break; case ESP_A2D_AUDIO_STATE_EVT: - ESP_LOGI(BT_A2D_TAG, "A2DP audio state: %s", s_a2d_audio_state_str[a2d->audio_stat.state]); + ESP_LOGI(BT_A2D_TAG, "A2DP audio state: %s", s_a2d_audio_state_str[param->audio_stat.state]); #ifdef CONFIG_ENABLE_LED - if (a2d->audio_stat.state == ESP_A2D_AUDIO_STATE_STARTED) { + if (param->audio_stat.state == ESP_A2D_AUDIO_STATE_STARTED) { led_set_mode(LED_MODE_IDX_BLINK_S0); } else { led_set_mode(LED_MODE_IDX_BLINK_M1); @@ -182,12 +118,10 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param) #endif break; case ESP_A2D_AUDIO_CFG_EVT: - ESP_LOGI(BT_A2D_TAG, "A2DP audio stream configuration, codec type %d", a2d->audio_cfg.mcc.type); + ESP_LOGI(BT_A2D_TAG, "A2DP audio stream configuration, codec type: %d", param->audio_cfg.mcc.type); // for now only SBC stream is supported - if (a2d->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) { - a2d_sample_rate = 16000; - - char oct0 = a2d->audio_cfg.mcc.cie.sbc[0]; + if (param->audio_cfg.mcc.type == ESP_A2D_MCT_SBC) { + char oct0 = param->audio_cfg.mcc.cie.sbc[0]; if (oct0 & (0x01 << 6)) { a2d_sample_rate = 32000; @@ -195,29 +129,27 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param) a2d_sample_rate = 44100; } else if (oct0 & (0x01 << 4)) { a2d_sample_rate = 48000; + } else { + a2d_sample_rate = 16000; } - ESP_LOGI(BT_A2D_TAG, "configure audio player %x-%x-%x-%x", - a2d->audio_cfg.mcc.cie.sbc[0], - a2d->audio_cfg.mcc.cie.sbc[1], - a2d->audio_cfg.mcc.cie.sbc[2], - a2d->audio_cfg.mcc.cie.sbc[3]); - ESP_LOGI(BT_A2D_TAG, "audio player configured, sample rate=%d", a2d_sample_rate); + ESP_LOGI(BT_A2D_TAG, "A2DP audio player configuration, sample rate: %d", a2d_sample_rate); } break; default: - ESP_LOGE(BT_A2D_TAG, "%s unhandled evt %d", __func__, event); + ESP_LOGW(BT_A2D_TAG, "unhandled evt: %d", event); break; } } static void bt_av_new_track(void) { + uint8_t attr_mask = ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM; + // request metadata - uint8_t attr_mask = ESP_AVRC_MD_ATTR_TITLE | ESP_AVRC_MD_ATTR_ARTIST | ESP_AVRC_MD_ATTR_ALBUM | ESP_AVRC_MD_ATTR_GENRE; esp_avrc_ct_send_metadata_cmd(APP_RC_CT_TL_GET_META_DATA, attr_mask); - // register notification if peer support the event_id + if (esp_avrc_rn_evt_bit_mask_operation(ESP_AVRC_BIT_MASK_OP_TEST, &s_avrc_peer_rn_cap, ESP_AVRC_RN_TRACK_CHANGE)) { esp_avrc_ct_send_register_notification_cmd(APP_RC_CT_TL_RN_TRACK_CHANGE, ESP_AVRC_RN_TRACK_CHANGE, 0); } @@ -237,18 +169,18 @@ static void bt_av_play_pos_changed(void) } } -static void bt_av_notify_evt_handler(uint8_t event_id, esp_avrc_rn_param_t *event_parameter) +static void bt_av_notify_evt_handler(uint8_t event, esp_avrc_rn_param_t *param) { - switch (event_id) { + switch (event) { case ESP_AVRC_RN_TRACK_CHANGE: bt_av_new_track(); break; case ESP_AVRC_RN_PLAY_STATUS_CHANGE: - ESP_LOGI(BT_RC_RN_TAG, "Play status changed: 0x%x", event_parameter->playback); + ESP_LOGI(BT_RC_RN_TAG, "play status changed: %d", param->playback); bt_av_play_status_changed(); break; case ESP_AVRC_RN_PLAY_POS_CHANGED: - ESP_LOGI(BT_RC_RN_TAG, "Play position changed: %d ms", event_parameter->play_pos); + ESP_LOGI(BT_RC_RN_TAG, "play position changed: %d ms", param->play_pos); bt_av_play_pos_changed(); break; } @@ -256,16 +188,17 @@ static void bt_av_notify_evt_handler(uint8_t event_id, esp_avrc_rn_param_t *even static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param) { - esp_avrc_ct_cb_param_t *rc = (esp_avrc_ct_cb_param_t *)(p_param); + esp_avrc_ct_cb_param_t *param = (esp_avrc_ct_cb_param_t *)p_param; + switch (event) { case ESP_AVRC_CT_CONNECTION_STATE_EVT: - ESP_LOGI(BT_RC_CT_TAG, "AVRC conn_state evt: state %d, [%02x:%02x:%02x:%02x:%02x:%02x]", - rc->conn_stat.connected, - rc->conn_stat.remote_bda[0], rc->conn_stat.remote_bda[1], - rc->conn_stat.remote_bda[2], rc->conn_stat.remote_bda[3], - rc->conn_stat.remote_bda[4], rc->conn_stat.remote_bda[5]); + ESP_LOGI(BT_RC_CT_TAG, "AVRC connection state: %s, [%02x:%02x:%02x:%02x:%02x:%02x]", + s_avrc_conn_state_str[param->conn_stat.connected], + param->conn_stat.remote_bda[0], param->conn_stat.remote_bda[1], + param->conn_stat.remote_bda[2], param->conn_stat.remote_bda[3], + param->conn_stat.remote_bda[4], param->conn_stat.remote_bda[5]); - if (rc->conn_stat.connected) { + if (param->conn_stat.connected) { // get remote supported event_ids of peer AVRCP Target esp_avrc_ct_send_get_rn_capabilities_cmd(APP_RC_CT_TL_GET_CAPS); } else { @@ -274,29 +207,86 @@ static void bt_av_hdl_avrc_ct_evt(uint16_t event, void *p_param) } break; case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: - ESP_LOGI(BT_RC_CT_TAG, "AVRC passthrough rsp: key_code 0x%x, key_state %d", rc->psth_rsp.key_code, rc->psth_rsp.key_state); + ESP_LOGI(BT_RC_CT_TAG, "AVRC passthrough rsp, key_code: 0x%x, key_state: %d", param->psth_rsp.key_code, param->psth_rsp.key_state); break; case ESP_AVRC_CT_METADATA_RSP_EVT: - ESP_LOGI(BT_RC_CT_TAG, "AVRC metadata rsp: attribute id 0x%x, %s", rc->meta_rsp.attr_id, rc->meta_rsp.attr_text); - free(rc->meta_rsp.attr_text); + ESP_LOGI(BT_RC_CT_TAG, "AVRC metadata rsp, attr_id: 0x%x, attr_text: %s", param->meta_rsp.attr_id, param->meta_rsp.attr_text); + free(param->meta_rsp.attr_text); break; case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: - ESP_LOGI(BT_RC_CT_TAG, "AVRC event notification: %d", rc->change_ntf.event_id); - bt_av_notify_evt_handler(rc->change_ntf.event_id, &rc->change_ntf.event_parameter); + ESP_LOGI(BT_RC_CT_TAG, "AVRC event notification: %d", param->change_ntf.event_id); + bt_av_notify_evt_handler(param->change_ntf.event_id, ¶m->change_ntf.event_parameter); break; case ESP_AVRC_CT_REMOTE_FEATURES_EVT: - ESP_LOGI(BT_RC_CT_TAG, "AVRC remote features %x, TG features %x", rc->rmt_feats.feat_mask, rc->rmt_feats.tg_feat_flag); + ESP_LOGI(BT_RC_CT_TAG, "AVRC remote features: %x, TG features: %x", param->rmt_feats.feat_mask, param->rmt_feats.tg_feat_flag); break; case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT: - ESP_LOGI(BT_RC_CT_TAG, "remote rn_cap: count %d, bitmask 0x%x", rc->get_rn_caps_rsp.cap_count, - rc->get_rn_caps_rsp.evt_set.bits); - s_avrc_peer_rn_cap.bits = rc->get_rn_caps_rsp.evt_set.bits; + ESP_LOGI(BT_RC_CT_TAG, "AVRC rn capabilities: %d, bitmask: 0x%x", param->get_rn_caps_rsp.cap_count, param->get_rn_caps_rsp.evt_set.bits); + s_avrc_peer_rn_cap.bits = param->get_rn_caps_rsp.evt_set.bits; bt_av_new_track(); bt_av_play_status_changed(); bt_av_play_pos_changed(); break; default: - ESP_LOGE(BT_RC_CT_TAG, "%s unhandled evt %d", __func__, event); + ESP_LOGW(BT_RC_CT_TAG, "unhandled evt: %d", event); + break; + } +} + +static void bt_app_alloc_meta_buffer(esp_avrc_ct_cb_param_t *param) +{ + uint8_t *attr_text = calloc(param->meta_rsp.attr_length + 1, sizeof(uint8_t)); + + memcpy(attr_text, param->meta_rsp.attr_text, param->meta_rsp.attr_length); + + param->meta_rsp.attr_text = attr_text; +} + +void bt_app_avrc_ct_cb(esp_avrc_ct_cb_event_t event, esp_avrc_ct_cb_param_t *param) +{ + switch (event) { + case ESP_AVRC_CT_METADATA_RSP_EVT: + bt_app_alloc_meta_buffer(param); + /* fall through */ + case ESP_AVRC_CT_CONNECTION_STATE_EVT: + case ESP_AVRC_CT_PASSTHROUGH_RSP_EVT: + case ESP_AVRC_CT_CHANGE_NOTIFY_EVT: + case ESP_AVRC_CT_REMOTE_FEATURES_EVT: + case ESP_AVRC_CT_GET_RN_CAPABILITIES_RSP_EVT: + bt_app_work_dispatch(bt_av_hdl_avrc_ct_evt, event, param, sizeof(esp_avrc_ct_cb_param_t), NULL); + break; + default: + break; + } +} + +void bt_app_a2d_cb(esp_a2d_cb_event_t event, esp_a2d_cb_param_t *param) +{ + switch (event) { + case ESP_A2D_CONNECTION_STATE_EVT: + case ESP_A2D_AUDIO_STATE_EVT: + case ESP_A2D_AUDIO_CFG_EVT: + bt_app_work_dispatch(bt_av_hdl_a2d_evt, event, param, sizeof(esp_a2d_cb_param_t), NULL); + break; + default: break; } } + +void bt_app_a2d_data_cb(const uint8_t *data, uint32_t len) +{ + if (audio_buff) { + uint32_t pkt = 0, remain = 0; + + for (pkt = 0; pkt < len / FFT_BLOCK_SIZE; pkt++) { + xRingbufferSend(audio_buff, data + pkt * FFT_BLOCK_SIZE, FFT_BLOCK_SIZE, portMAX_DELAY); + taskYIELD(); + } + + remain = len - pkt * FFT_BLOCK_SIZE; + if (remain != 0) { + xRingbufferSend(audio_buff, data + pkt * FFT_BLOCK_SIZE, remain, portMAX_DELAY); + taskYIELD(); + } + } +} diff --git a/main/src/user/led.c b/main/src/user/led.c index 11535b0c..55666f3e 100644 --- a/main/src/user/led.c +++ b/main/src/user/led.c @@ -77,7 +77,6 @@ static void led_task(void *pvParameter) void led_set_mode(led_mode_t idx) { if (idx >= sizeof(led_mode_table) / sizeof(led_mode_table[0])) { - ESP_LOGE(TAG, "invalid mode index"); return; } diff --git a/main/src/user/ota.c b/main/src/user/ota.c index 2c43415a..03141f3e 100644 --- a/main/src/user/ota.c +++ b/main/src/user/ota.c @@ -30,10 +30,16 @@ #define OTA_TAG "ota" #define ota_send_response(X) \ - gatts_ota_send_notification((const char *)rsp_str[X], strlen(rsp_str[X])) + esp_ble_gatts_send_indicate(gatts_profile_tbl[PROFILE_IDX_OTA].gatts_if, \ + gatts_profile_tbl[PROFILE_IDX_OTA].conn_id, \ + gatts_profile_tbl[PROFILE_IDX_OTA].char_handle, \ + strlen(rsp_str[X]), (uint8_t *)rsp_str[X], false); #define ota_send_data(X, N) \ - gatts_ota_send_notification((const char *)X, N) + esp_ble_gatts_send_indicate(gatts_profile_tbl[PROFILE_IDX_OTA].gatts_if, \ + gatts_profile_tbl[PROFILE_IDX_OTA].conn_id, \ + gatts_profile_tbl[PROFILE_IDX_OTA].char_handle, \ + N, (uint8_t *)X, false); #define RX_BUF_SIZE 512 @@ -118,9 +124,9 @@ static void ota_write_task(void *pvParameter) } if (data_length >= RX_BUF_SIZE) { - data = (uint8_t *)xRingbufferReceiveUpTo(ota_buff, &size, 10 / portTICK_RATE_MS, RX_BUF_SIZE); + data = xRingbufferReceiveUpTo(ota_buff, &size, 10 / portTICK_RATE_MS, RX_BUF_SIZE); } else { - data = (uint8_t *)xRingbufferReceiveUpTo(ota_buff, &size, 10 / portTICK_RATE_MS, data_length); + data = xRingbufferReceiveUpTo(ota_buff, &size, 10 / portTICK_RATE_MS, data_length); } if (data == NULL || size == 0) { @@ -143,7 +149,7 @@ static void ota_write_task(void *pvParameter) if (data_length == 0) { err = esp_ota_end(update_handle); if (err != ESP_OK) { - ESP_LOGE(OTA_TAG, "image data error."); + ESP_LOGE(OTA_TAG, "image data error"); data_err = true; @@ -154,7 +160,7 @@ static void ota_write_task(void *pvParameter) err = esp_ota_set_boot_partition(update_partition); if (err != ESP_OK) { - ESP_LOGE(OTA_TAG, "set boot partition failed."); + ESP_LOGE(OTA_TAG, "failed to set boot partition"); data_err = true; @@ -337,7 +343,7 @@ void ota_exec(const char *data, uint32_t len) break; } default: - ESP_LOGW(OTA_TAG, "unknown command."); + ESP_LOGW(OTA_TAG, "unknown command"); ota_send_response(RSP_IDX_ERROR); @@ -345,7 +351,7 @@ void ota_exec(const char *data, uint32_t len) } } else { if (ota_buff) { - xRingbufferSend(ota_buff, (void *)data, len, portMAX_DELAY); + xRingbufferSend(ota_buff, data, len, portMAX_DELAY); } } } diff --git a/main/src/user/vfx.c b/main/src/user/vfx.c index 500d1d13..48835bc3 100644 --- a/main/src/user/vfx.c +++ b/main/src/user/vfx.c @@ -1185,7 +1185,7 @@ void vfx_set_conf(vfx_config_t *cfg) xEventGroupSetBits(user_event_group, VFX_RLD_MODE_BIT); - ESP_LOGI(TAG, "mode: 0x%02X, scale-factor: %u, lightness: 0x%04X, backlight: %u", + ESP_LOGI(TAG, "mode: 0x%02X, scale-factor: 0x%04X, lightness: 0x%04X, backlight: %u", vfx.mode, vfx.scale_factor, vfx.lightness, vfx.backlight); }