Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changed the return type of get_advertising to &'static Mutex<BLEAdvertising> #84

Merged
merged 4 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 14 additions & 12 deletions examples/ble5_multi_advertiser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,21 @@ fn main() {
BLEExtAdvertisement::new(BLE_HCI_LE_PHY_1M as _, BLE_HCI_LE_PHY_1M as _);
legacy_scan_response.service_data(SERVICE_UUID, "Legacy SR".as_bytes());

let advertising = ble_device.get_advertising();
advertising
.set_instance_data(0, &mut ext_scannable)
.unwrap();
advertising
.set_instance_data(1, &mut legacy_connectable)
.unwrap();
advertising
.set_scan_response_data(1, &mut legacy_scan_response)
.unwrap();
{
let mut advertising = ble_device.get_advertising().lock();
advertising
.set_instance_data(0, &mut ext_scannable)
.unwrap();
advertising
.set_instance_data(1, &mut legacy_connectable)
.unwrap();
advertising
.set_scan_response_data(1, &mut legacy_scan_response)
.unwrap();

advertising.start(0).unwrap();
advertising.start(1).unwrap();
advertising.start(0).unwrap();
advertising.start(1).unwrap();
}

loop {
esp_idf_hal::delay::FreeRtos::delay_ms(5000);
Expand Down
6 changes: 4 additions & 2 deletions examples/ble_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,11 +247,13 @@ impl Keyboard {

let ble_advertising = device.get_advertising();
ble_advertising
.lock()
.name("ESP32 Keyboard")
.appearance(0x03C1)
.add_service_uuid(hid.hid_service().lock().uuid())
.scan_response(false);
ble_advertising.start().unwrap();
.scan_response(false)
.start()
.unwrap();

Self {
server,
Expand Down
7 changes: 4 additions & 3 deletions examples/ble_secure_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ fn main() {
esp_idf_svc::log::EspLogger::initialize_default();

let device = BLEDevice::take();
let ble_advertising = device.get_advertising();

device
.security()
.set_auth(AuthReq::all())
Expand All @@ -19,7 +21,7 @@ fn main() {

if server.connected_count() < (esp_idf_sys::CONFIG_BT_NIMBLE_MAX_CONNECTIONS as _) {
::log::info!("Multi-connect support: start advertising");
device.get_advertising().start().unwrap();
ble_advertising.lock().start().unwrap();
}
});
server.on_disconnect(|_desc, reason| {
Expand All @@ -46,15 +48,14 @@ fn main() {
.lock()
.set_value("secure_characteristic".as_bytes());

let ble_advertising = device.get_advertising();

// With esp32-c3, advertising stops when a device is bonded.
// (https://github.com/taks/esp32-nimble/issues/70)
#[cfg(esp32c3)]
ble_advertising.on_complete(|_| {
BLEDevice::take().get_advertising().start().unwrap();
});
ble_advertising
.lock()
.name("ESP32-GATT-Server")
.add_service_uuid(BleUuid::Uuid16(0xABCD))
.start()
Expand Down
11 changes: 6 additions & 5 deletions examples/ble_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ fn main() {
esp_idf_svc::log::EspLogger::initialize_default();

let ble_device = BLEDevice::take();
let ble_advertising = ble_device.get_advertising();

let server = ble_device.get_server();
server.on_connect(|server, desc| {
Expand All @@ -18,7 +19,7 @@ fn main() {

if server.connected_count() < (esp_idf_sys::CONFIG_BT_NIMBLE_MAX_CONNECTIONS as _) {
::log::info!("Multi-connect support: start advertising");
ble_device.get_advertising().start().unwrap();
ble_advertising.lock().start().unwrap();
}
});

Expand Down Expand Up @@ -62,12 +63,12 @@ fn main() {
);
});

let ble_advertising = ble_device.get_advertising();
ble_advertising
.lock()
.name("ESP32-GATT-Server")
.add_service_uuid(uuid128!("fafafafa-fafa-fafa-fafa-fafafafafafa"));

ble_advertising.start().unwrap();
.add_service_uuid(uuid128!("fafafafa-fafa-fafa-fafa-fafafafafafa"))
.start()
.unwrap();

server.ble_gatts_show_local();

Expand Down
13 changes: 7 additions & 6 deletions examples/ble_start_stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ fn main() {
.unwrap();

let ble_device = BLEDevice::take();
let ble_advertising = ble_device.get_advertising();

let server = ble_device.get_server();
server.on_connect(|server, desc| {
Expand All @@ -32,7 +33,7 @@ fn main() {
.unwrap();

::log::info!("Multi-connect support: start advertising");
ble_device.get_advertising().start().unwrap();
ble_advertising.lock().start().unwrap();
});
let service = server.create_service(uuid128!("fafafafa-fafa-fafa-fafa-fafafafafafa"));

Expand Down Expand Up @@ -70,12 +71,12 @@ fn main() {
);
});

let ble_advertising = ble_device.get_advertising();
ble_advertising
.lock()
.name("ESP32-GATT-Server")
.add_service_uuid(uuid128!("fafafafa-fafa-fafa-fafa-fafafafafafa"));

ble_advertising.start().unwrap();
.add_service_uuid(uuid128!("fafafafa-fafa-fafa-fafa-fafafafafafa"))
.start()
.unwrap();

let mut buf = [0_u8; 10];
let mut initialized = true;
Expand All @@ -89,7 +90,7 @@ fn main() {
} else {
::log::info!("start BLE");
BLEDevice::init();
ble_advertising.start().unwrap();
ble_advertising.lock().start().unwrap();
}
initialized = !initialized;
}
Expand Down
12 changes: 8 additions & 4 deletions src/ble_device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use core::ffi::c_void;
use esp_idf_sys::esp_nofail;
use once_cell::sync::Lazy;

use crate::{ble, client::BLEScan, enums::*, BLEAddress, BLEReturnCode, BLESecurity, BLEServer};
use crate::{
ble, client::BLEScan, enums::*, utilities::mutex::Mutex, BLEAddress, BLEReturnCode, BLESecurity,
BLEServer,
};

#[cfg(not(esp_idf_bt_nimble_ext_adv))]
type BLEAdvertising = crate::BLEAdvertising;
Expand Down Expand Up @@ -33,7 +36,8 @@ static mut BLE_DEVICE: Lazy<BLEDevice> = Lazy::new(|| {
});
static mut BLE_SCAN: Lazy<BLEScan> = Lazy::new(BLEScan::new);
pub static mut BLE_SERVER: Lazy<BLEServer> = Lazy::new(BLEServer::new);
static mut BLE_ADVERTISING: Lazy<BLEAdvertising> = Lazy::new(BLEAdvertising::new);
static BLE_ADVERTISING: Lazy<Mutex<BLEAdvertising>> =
Lazy::new(|| Mutex::new(BLEAdvertising::new()));

pub static mut OWN_ADDR_TYPE: OwnAddrType = OwnAddrType::Public;
static mut INITIALIZED: bool = false;
Expand Down Expand Up @@ -129,8 +133,8 @@ impl BLEDevice {
unsafe { Lazy::force_mut(&mut BLE_SERVER) }
}

pub fn get_advertising(&self) -> &'static mut BLEAdvertising {
unsafe { Lazy::force_mut(&mut BLE_ADVERTISING) }
pub fn get_advertising(&self) -> &'static Mutex<BLEAdvertising> {
Lazy::force(&BLE_ADVERTISING)
}

pub fn set_power(
Expand Down
2 changes: 1 addition & 1 deletion src/server/ble_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ impl BLEServer {

#[cfg(not(esp_idf_bt_nimble_ext_adv))]
if server.advertise_on_disconnect {
if let Err(err) = BLEDevice::take().get_advertising().start() {
if let Err(err) = BLEDevice::take().get_advertising().lock().start() {
::log::warn!("can't start advertising: {:?}", err);
}
}
Expand Down