Skip to content

Commit

Permalink
bt: code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
redchenjs committed Feb 11, 2021
1 parent 6a13a3b commit d5d06c6
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 5 deletions.
8 changes: 7 additions & 1 deletion main/inc/chip/bt.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,13 @@
#ifndef INC_CHIP_BT_H_
#define INC_CHIP_BT_H_

extern const char *bt_dev_address;
#include <stdint.h>

extern char *bt_get_mac_string(void);
extern char *ble_get_mac_string(void);

extern uint8_t *bt_get_mac_address(void);
extern uint8_t *ble_get_mac_address(void);

extern void bt_init(void);

Expand Down
36 changes: 34 additions & 2 deletions main/src/chip/bt.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,40 @@
* Author: Jack Chen <redchenjs@live.com>
*/

#include <string.h>

#include "esp_bt.h"
#include "esp_log.h"
#include "esp_bt_main.h"
#include "esp_bt_device.h"

#define TAG "bt"

const char *bt_dev_address = NULL;
static char bt_mac_string[18] = {0};
static char ble_mac_string[18] = {0};

static uint8_t bt_mac_address[6] = {0};
static uint8_t ble_mac_address[6] = {0};

char *bt_get_mac_string(void)
{
return bt_mac_string;
}

char *ble_get_mac_string(void)
{
return ble_mac_string;
}

uint8_t *bt_get_mac_address(void)
{
return bt_mac_address;
}

uint8_t *ble_get_mac_address(void)
{
return ble_mac_address;
}

void bt_init(void)
{
Expand All @@ -26,7 +52,13 @@ void bt_init(void)
ESP_ERROR_CHECK(esp_bluedroid_init());
ESP_ERROR_CHECK(esp_bluedroid_enable());

bt_dev_address = (const char *)esp_bt_dev_get_address();
memcpy(bt_mac_address, esp_bt_dev_get_address(), sizeof(bt_mac_address));
memcpy(ble_mac_address, esp_bt_dev_get_address(), sizeof(ble_mac_address));

ble_mac_address[0] |= 0xC0;

snprintf(bt_mac_string, sizeof(bt_mac_string), MACSTR, MAC2STR(bt_mac_address));
snprintf(ble_mac_string, sizeof(ble_mac_string), MACSTR, MAC2STR(ble_mac_address));

ESP_LOGI(TAG, "initialized, bt: 1, ble: %d",
#ifdef CONFIG_ENABLE_BLE_CONTROL_IF
Expand Down
6 changes: 5 additions & 1 deletion main/src/user/ble_app.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
#include "esp_gatt_common_api.h"

#include "core/os.h"
#include "chip/bt.h"

#include "user/ble_gatts.h"

#define BLE_APP_TAG "ble_app"
Expand All @@ -23,7 +25,7 @@ static esp_ble_adv_params_t adv_params = {
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.own_addr_type = BLE_ADDR_TYPE_RANDOM,
.channel_map = ADV_CHNL_ALL,
.adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY
};
Expand Down Expand Up @@ -66,6 +68,7 @@ static void ble_gap_config_adv_data(const char *name)
// 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));
Expand All @@ -84,6 +87,7 @@ void ble_app_init(void)
xEventGroupSetBits(user_event_group, BLE_GATTS_IDLE_BIT);

ESP_ERROR_CHECK(esp_ble_gap_register_callback(ble_gap_event_handler));
ESP_ERROR_CHECK(esp_ble_gap_set_rand_addr(ble_get_mac_address()));
ESP_ERROR_CHECK(esp_ble_gap_set_device_name(CONFIG_BT_NAME));

ESP_ERROR_CHECK(esp_ble_gatts_register_callback(ble_gatts_event_handler));
Expand Down
2 changes: 1 addition & 1 deletion main/src/user/bt_av.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static void bt_av_hdl_a2d_evt(uint16_t event, void *p_param)
a2d_sample_rate = 16000;
}

ESP_LOGI(BT_A2D_TAG, "A2DP audio player configuration, sample rate: %d", a2d_sample_rate);
ESP_LOGI(BT_A2D_TAG, "A2DP audio player configuration, sample rate: %d Hz", a2d_sample_rate);
}

break;
Expand Down

0 comments on commit d5d06c6

Please sign in to comment.