Skip to content

Commit

Permalink
ble_app: code clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
redchenjs committed Jun 23, 2020
1 parent 7ef3bd0 commit dbd57e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 47 deletions.
2 changes: 0 additions & 2 deletions main/inc/user/ble_app.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@

extern esp_ble_adv_params_t adv_params;

extern void ble_gap_init_adv_data(const char *name);

extern void ble_app_init(void);

#endif /* INC_USER_BLE_APP_H_ */
69 changes: 26 additions & 43 deletions main/src/user/ble_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@
#define BLE_APP_TAG "ble_app"
#define BLE_GAP_TAG "ble_gap"

static uint8_t adv_config_done = 0;

#define adv_config_flag (1 << 0)
#define scan_rsp_config_flag (1 << 1)

esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
Expand All @@ -33,47 +28,11 @@ esp_ble_adv_params_t adv_params = {
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
};

void ble_gap_init_adv_data(const char *name)
{
int len = strlen(name);
uint8_t raw_adv_data[len+5];
// flag
raw_adv_data[0] = 2;
raw_adv_data[1] = ESP_BT_EIR_TYPE_FLAGS;
raw_adv_data[2] = (ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT);
// adv name
raw_adv_data[3] = len + 1;
raw_adv_data[4] = ESP_BLE_AD_TYPE_NAME_CMPL;
for (int i=0; i<len; i++) {
raw_adv_data[i+5] = *(name++);
}
// the length of adv data must be less than 31 bytes
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);
}
adv_config_done |= adv_config_flag;
esp_err_t raw_scan_ret = esp_ble_gap_config_scan_rsp_data_raw(raw_adv_data, sizeof(raw_adv_data));
if (raw_scan_ret) {
ESP_LOGE(BLE_GAP_TAG, "config raw scan rsp data failed, error code = 0x%x", raw_scan_ret);
}
adv_config_done |= scan_rsp_config_flag;
}

static void ble_gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
switch (event) {
case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT:
adv_config_done &= (~adv_config_flag);
if (adv_config_done == 0) {
esp_ble_gap_start_advertising(&adv_params);
}
break;
case ESP_GAP_BLE_SCAN_RSP_DATA_RAW_SET_COMPLETE_EVT:
adv_config_done &= (~scan_rsp_config_flag);
if (adv_config_done == 0) {
esp_ble_gap_start_advertising(&adv_params);
}
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
Expand All @@ -95,17 +54,41 @@ 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)
{
size_t len = strlen(name);
uint8_t raw_adv_data[len+5];

// flag
raw_adv_data[0] = 2;
raw_adv_data[1] = ESP_BT_EIR_TYPE_FLAGS;
raw_adv_data[2] = ESP_BLE_ADV_FLAG_GEN_DISC | ESP_BLE_ADV_FLAG_BREDR_NOT_SPT;

// adv name
raw_adv_data[3] = len + 1;
raw_adv_data[4] = ESP_BLE_AD_TYPE_NAME_CMPL;
memcpy(raw_adv_data+5, name, len);

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);
}
}

void ble_app_init(void)
{
xEventGroupSetBits(user_event_group, BLE_GATTS_IDLE_BIT);

ESP_ERROR_CHECK(esp_ble_gatts_register_callback(ble_gatts_event_handler));
ESP_ERROR_CHECK(esp_ble_gap_register_callback(ble_gap_event_handler));
ESP_ERROR_CHECK(esp_ble_gap_set_device_name(CONFIG_BT_NAME));

ESP_ERROR_CHECK(esp_ble_gatts_register_callback(ble_gatts_event_handler));
ESP_ERROR_CHECK(esp_ble_gatts_app_register(PROFILE_IDX_OTA));
ESP_ERROR_CHECK(esp_ble_gatts_app_register(PROFILE_IDX_VFX));

ESP_ERROR_CHECK(esp_ble_gatt_set_local_mtu(ESP_GATT_MAX_MTU_SIZE));

gap_config_adv_data(CONFIG_BT_NAME);

ESP_LOGI(BLE_APP_TAG, "started.");
}
2 changes: 0 additions & 2 deletions main/src/user/ble_gatts.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ static void profile_ota_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t
{
switch (event) {
case ESP_GATTS_REG_EVT:
ble_gap_init_adv_data(CONFIG_BT_NAME);

gatts_profile_tbl[PROFILE_IDX_OTA].service_id.is_primary = true;
gatts_profile_tbl[PROFILE_IDX_OTA].service_id.id.inst_id = 0x00;
gatts_profile_tbl[PROFILE_IDX_OTA].service_id.id.uuid.len = ESP_UUID_LEN_16;
Expand Down

0 comments on commit dbd57e2

Please sign in to comment.