Skip to content

Commit

Permalink
main: clean up log outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
redchenjs committed Feb 10, 2021
1 parent fb1fb2d commit 6a13a3b
Show file tree
Hide file tree
Showing 15 changed files with 173 additions and 215 deletions.
4 changes: 1 addition & 3 deletions main/inc/user/ble_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions main/inc/user/ble_gatts.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
15 changes: 2 additions & 13 deletions main/inc/user/bt_app_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,18 @@

#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 */
bt_app_cb_t cb; /*!< context switch callback */
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_ */
17 changes: 3 additions & 14 deletions main/inc/user/bt_av.h
Original file line number Diff line number Diff line change
Expand Up @@ -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_*/
2 changes: 1 addition & 1 deletion main/src/board/ws2812.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
3 changes: 1 addition & 2 deletions main/src/user/audio_player.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions main/src/user/audio_render.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
18 changes: 11 additions & 7 deletions main/src/user/ble_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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:
Expand All @@ -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];
Expand All @@ -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);
Expand All @@ -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.");
}
68 changes: 28 additions & 40 deletions main/src/user/ble_gatts.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand All @@ -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:
Expand All @@ -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:
Expand All @@ -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);
}
Expand Down
5 changes: 2 additions & 3 deletions main/src/user/bt_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
Expand All @@ -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:
Expand Down Expand Up @@ -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;
}
}
Expand Down
Loading

0 comments on commit 6a13a3b

Please sign in to comment.