Skip to content

Commit

Permalink
Add suffix to BLE device name
Browse files Browse the repository at this point in the history
  • Loading branch information
olegv142 committed Apr 28, 2024
1 parent 3734c85 commit 976e6e1
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 18 deletions.
7 changes: 4 additions & 3 deletions main/ble_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,12 @@ static const uint16_t spp_service_uuid = 0xFFE0;
#define BLE_ADV_NAME_LEN (sizeof(BLE_ADV_NAME)-1)
#define BLE_ADV_NAME_OFF 15

static uint8_t spp_adv_data[BLE_ADV_NAME_OFF+BLE_ADV_NAME_LEN] = {
static uint8_t spp_adv_data[BLE_ADV_NAME_OFF+BLE_ADV_NAME_LEN+DEV_NAME_SUFF_LEN] = {
0x02, 0x01, 0x08, // flags
0x03, 0x03, 0xe0, 0xff, // service UID
0x05, 0x12, 0x20, 0x00, 0x40, 0x00, // conn interval range
1+BLE_ADV_NAME_LEN, 0x09,
// [BLE_ADV_NAME_OFF..] BLE_ADV_NAME
1+BLE_ADV_NAME_LEN+DEV_NAME_SUFF_LEN, 0x09,
// [BLE_ADV_NAME_OFF..] BLE_ADV_NAME + suffix
};

#define BLE_UART_NUM UART_NUM_2
Expand Down Expand Up @@ -370,6 +370,7 @@ void ble_server_init(void)
{
for (int i = 0; i < BLE_ADV_NAME_LEN; ++i)
spp_adv_data[BLE_ADV_NAME_OFF+i] = BLE_ADV_NAME[i];
get_device_name_suff((char*)&spp_adv_data[BLE_ADV_NAME_OFF+BLE_ADV_NAME_LEN]);

esp_ble_gatts_register_callback(gatts_event_handler);
esp_ble_gap_register_callback(gap_event_handler);
Expand Down
3 changes: 3 additions & 0 deletions main/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

const char* get_device_name(void);

#define DEV_NAME_SUFF_LEN 6

void get_device_name_suff(char buff[DEV_NAME_SUFF_LEN]);
29 changes: 14 additions & 15 deletions main/spp_vfs_acceptor.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "esp_bt_device.h"
#include "esp_spp_api.h"
#include "spp_task.h"
#include "main.h"

#include "time.h"
#include "sys/time.h"
Expand Down Expand Up @@ -168,30 +169,28 @@ static inline char byte_signature(uint8_t v)
return hex_digit((v & 0xf) ^ (v >> 4));
}

#define BT_MAC_LEN 6

static const char* bt_get_dev_name(void)
void get_device_name_suff(char buff[DEV_NAME_SUFF_LEN])
{
static char dev_name[BT_DEV_NAME_PREFIX_LEN + BT_MAC_LEN + 1] = BT_DEV_NAME_PREFIX;
const uint8_t * mac = esp_bt_dev_get_address();
int i;
for (i = 0; i < BT_MAC_LEN; ++i) {
dev_name[BT_DEV_NAME_PREFIX_LEN + i] = byte_signature(mac[i]);
for (int i = 0; i < DEV_NAME_SUFF_LEN; ++i) {
buff[i] = byte_signature(mac[i]);
}
dev_name[BT_DEV_NAME_PREFIX_LEN + BT_MAC_LEN] = 0;
}

static const char* bt_get_dev_name(void)
{
static char dev_name[BT_DEV_NAME_PREFIX_LEN + DEV_NAME_SUFF_LEN + 1] = BT_DEV_NAME_PREFIX;
get_device_name_suff(&dev_name[BT_DEV_NAME_PREFIX_LEN]);
dev_name[BT_DEV_NAME_PREFIX_LEN + DEV_NAME_SUFF_LEN] = 0;
ESP_LOGI(SPP_TAG, "Device name is %s", dev_name);
return dev_name;
}

static const char* bt_get_alt_dev_name(void)
{
static char dev_name[BT_DEV_NAME_PREFIX_LEN_ALT + BT_MAC_LEN + 1] = BT_DEV_NAME_PREFIX_ALT;
const uint8_t * mac = esp_bt_dev_get_address();
int i;
for (i = 0; i < BT_MAC_LEN; ++i) {
dev_name[BT_DEV_NAME_PREFIX_LEN_ALT + i] = byte_signature(mac[i]);
}
dev_name[BT_DEV_NAME_PREFIX_LEN_ALT + BT_MAC_LEN] = 0;
static char dev_name[BT_DEV_NAME_PREFIX_LEN_ALT + DEV_NAME_SUFF_LEN + 1] = BT_DEV_NAME_PREFIX_ALT;
get_device_name_suff(&dev_name[BT_DEV_NAME_PREFIX_LEN_ALT]);
dev_name[BT_DEV_NAME_PREFIX_LEN_ALT + DEV_NAME_SUFF_LEN] = 0;
ESP_LOGI(SPP_TAG, "Device name (alt) is %s", dev_name);
return dev_name;
}
Expand Down

0 comments on commit 976e6e1

Please sign in to comment.