Skip to content

Commit 34e90eb

Browse files
authored
Feature: BLE GATT Server Extended Advertising Support (#184)
* feat(ble_gatt_server): update API * Update advertising parameters API to take in interval milliseconds instead of units, and warn if the specific value had to be rounded * Update advertising parameters API to take in directed address and connectable flag for setting up directed, undirected, and non-connectable advertisements * Added advertising API to allow reuse of previously configured data / parameters * Added APIs to server for easily getting the paired and connected devices, and quickly disconnecting from all clients * Added some logging utilities and debug logs * Refactored the ble gatt server menu to be a class (for better docs and to allow subclassing) and extended its functionality * Updated example to use partitions file and run the menu first before going into demo * feat(ble_gatt_server): wip support for extended advertisements * Updated APIs to take in actual NimBLEAdvertisement data (ext and legacy) and make the apis consistent between legacy and ext as much as possible * update and rebuild docs * doc: update * update gfps example to throw an error if extended advertisements are enabled * rebuild docs
1 parent 982b6ff commit 34e90eb

File tree

116 files changed

+1749
-575
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+1749
-575
lines changed

components/ble_gatt_server/example/main/ble_gatt_server_example.cpp

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,11 @@ extern "C" void app_main(void) {
4343
},
4444
});
4545
ble_gatt_server.init(device_name);
46+
#if !CONFIG_BT_NIMBLE_EXT_ADV
47+
// extended advertisement does not support automatically advertising on
48+
// disconnect
4649
ble_gatt_server.set_advertise_on_disconnect(true);
50+
#endif
4751

4852
// let's configure the security
4953
bool bonding = true;
@@ -87,16 +91,38 @@ extern "C" void app_main(void) {
8791
device_info_service.set_firmware_version("1.0.0");
8892
device_info_service.set_hardware_version("1.0.0");
8993

94+
// set the advertising data
95+
espp::BleGattServer::AdvertisedData adv_data;
96+
// uint8_t flags = BLE_HS_ADV_F_DISC_LTD;
97+
uint8_t flags = BLE_HS_ADV_F_DISC_GEN;
98+
adv_data.setFlags(flags);
99+
adv_data.setName(device_name);
100+
adv_data.setAppearance((uint16_t)espp::BleAppearance::GENERIC_COMPUTER);
101+
adv_data.addTxPower();
102+
ble_gatt_server.set_advertisement_data(adv_data);
103+
104+
#if CONFIG_COMPILER_CXX_EXCEPTIONS
105+
// let's test and use the BLE menu (CLI)
106+
// turn off some of the logs so that it doesn't clutter up the CLI
107+
ble_gatt_server.set_log_level(espp::Logger::Verbosity::WARN);
108+
// and make the CLI
109+
auto ble_menu = espp::BleGattServerMenu(ble_gatt_server);
110+
cli::SetColor();
111+
cli::Cli cli(ble_menu.get());
112+
cli.ExitAction([](auto &out) { out << "Goodbye and thanks for all the fish.\n"; });
113+
114+
espp::Cli input(cli);
115+
input.SetInputHistorySize(10);
116+
input.Start(); // this will not return until the user enters the `exit` command.
117+
#endif
118+
119+
// The menu has finished, so let's go into a loop to keep the device running
90120
// now lets start advertising
91-
espp::BleGattServer::AdvertisingData adv_data = {
92-
.name = device_name,
93-
};
94-
espp::BleGattServer::AdvertisingParameters adv_params = {};
95-
ble_gatt_server.start_advertising(adv_data, adv_params);
121+
ble_gatt_server.start_advertising();
96122

97123
// now lets update the battery level every second for a little while
98124
uint8_t battery_level = 99;
99-
for (int i = 0; i < 200; i++) {
125+
while (true) {
100126
auto start = std::chrono::steady_clock::now();
101127

102128
// update the battery level
@@ -106,18 +132,5 @@ extern "C" void app_main(void) {
106132
// sleep
107133
std::this_thread::sleep_until(start + 1s);
108134
}
109-
110-
// Now let's test and use the BLE menu (CLI)
111-
// turn off some of the logs so that it doesn't clutter up the CLI
112-
ble_gatt_server.set_log_level(espp::Logger::Verbosity::WARN);
113-
// and make the CLI
114-
auto ble_menu = espp::make_ble_gatt_server_menu(ble_gatt_server);
115-
cli::SetColor();
116-
cli::Cli cli(std::move(ble_menu));
117-
cli.ExitAction([](auto &out) { out << "Goodbye and thanks for all the fish.\n"; });
118-
119-
espp::Cli input(cli);
120-
input.SetInputHistorySize(10);
121-
input.Start(); // this will not return until the user enters the `exit` command.
122-
//! [ble gatt server example]
135+
//! [ble gatt server example]
123136
}

components/ble_gatt_server/example/partitions.csv

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
nvs, data, nvs, 0x9000, 0x6000
33
phy_init, data, phy, 0xf000, 0x1000
44
factory, app, factory, 0x10000, 2M
5-
littlefs, data, spiffs, , 1M

components/ble_gatt_server/example/sdkconfig.defaults

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ CONFIG_FREERTOS_HZ=1000
1313

1414
CONFIG_ESPTOOLPY_FLASHSIZE_4MB=y
1515

16+
# Partition Table
17+
CONFIG_PARTITION_TABLE_CUSTOM=y
18+
CONFIG_PARTITION_TABLE_CUSTOM_FILENAME="partitions.csv"
19+
1620
#
1721
# BT config
1822
#
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
5+
namespace espp {
6+
/// \brief The appearance of a BLE device
7+
/// \note This is a subset of the Bluetooth SIG Assigned Numbers
8+
/// \note See
9+
/// https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile/
10+
/// specifically, pages 29-30 of
11+
/// https://www.bluetooth.com/wp-content/uploads/Files/Specification/HTML/Assigned_Numbers/out/en/Assigned_Numbers.pdf?v=1710445481092
12+
enum class BleAppearance : uint16_t {
13+
UNKNOWN = 0,
14+
GENERIC_PHONE = 64,
15+
GENERIC_COMPUTER = 128,
16+
GENERIC_WATCH = 192,
17+
WATCH_SPORTS_WATCH = 193,
18+
GENERIC_CLOCK = 256,
19+
GENERIC_DISPLAY = 320,
20+
GENERIC_REMOTE_CONTROL = 384,
21+
GENERIC_EYE_GLASSES = 448,
22+
GENERIC_TAG = 512,
23+
GENERIC_KEYRING = 576,
24+
GENERIC_MEDIA_PLAYER = 640,
25+
GENERIC_BARCODE_SCANNER = 704,
26+
GENERIC_THERMOMETER = 768,
27+
THERMOMETER_EAR = 769,
28+
GENERIC_HEART_RATE_SENSOR = 832,
29+
HEART_RATE_SENSOR_HEART_RATE_BELT = 833,
30+
GENERIC_BLOOD_PRESSURE = 896,
31+
BLOOD_PRESSURE_ARM = 897,
32+
BLOOD_PRESSURE_WRIST = 898,
33+
HUMAN_INTERFACE_DEVICE = 960,
34+
KEYBOARD = 961,
35+
MOUSE = 962,
36+
JOYSTICK = 963,
37+
GAMEPAD = 964,
38+
DIGITIZER_TABLET = 965,
39+
CARD_READER = 966,
40+
DIGITAL_PEN = 967,
41+
BARCODE_SCANNER = 968,
42+
};
43+
} // namespace espp

0 commit comments

Comments
 (0)