Skip to content

Commit

Permalink
Change the argument of set_auth function to bitflag
Browse files Browse the repository at this point in the history
  • Loading branch information
taks committed Nov 9, 2023
1 parent 79e53ad commit 6da5709
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
- Upgraded to `esp-idf-hal` 0.42 and `esp-idf-svc` 0.47
- Added Self parameter to BLEScan::on_result.
- Add feature for building with std support ([#36](https://github.com/taks/esp32-nimble/pull/36))
- Change the argument of set_auth function to bitflag.

## [0.2.2]
- Fix advertising regression in v0.2.1 ([#31](https://github.com/taks/esp32-nimble/pull/31))
Expand Down
2 changes: 1 addition & 1 deletion examples/ble_keyboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ impl Keyboard {
let device = BLEDevice::take();
device
.security()
.set_auth(true, true, true)
.set_auth(AuthReq::all())
.set_io_cap(SecurityIOCap::NoInputNoOutput);

let server = device.get_server();
Expand Down
2 changes: 1 addition & 1 deletion examples/ble_secure_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fn main() {
.unwrap();
device
.security()
.set_auth(true, true, true)
.set_auth(AuthReq::all())
.set_io_cap(SecurityIOCap::KeyboardOnly);

let ble_scan = device.get_scan();
Expand Down
2 changes: 1 addition & 1 deletion examples/ble_secure_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ fn main() {
let device = BLEDevice::take();
device
.security()
.set_auth(true, true, true)
.set_auth(AuthReq::all())
.set_passkey(123456)
.set_io_cap(SecurityIOCap::DisplayOnly);

Expand Down
8 changes: 4 additions & 4 deletions src/ble_security.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ impl BLESecurity {
Self { passkey: 0 }
}

pub fn set_auth(&mut self, bonding: bool, mitm: bool, sc: bool) -> &mut Self {
pub fn set_auth(&mut self, auth_req: enums::AuthReq) -> &mut Self {
unsafe {
esp_idf_sys::ble_hs_cfg.set_sm_bonding(bonding as _);
esp_idf_sys::ble_hs_cfg.set_sm_mitm(mitm as _);
esp_idf_sys::ble_hs_cfg.set_sm_sc(sc as _);
esp_idf_sys::ble_hs_cfg.set_sm_bonding(auth_req.contains(enums::AuthReq::Bond) as _);
esp_idf_sys::ble_hs_cfg.set_sm_mitm(auth_req.contains(enums::AuthReq::Mitm) as _);
esp_idf_sys::ble_hs_cfg.set_sm_sc(auth_req.contains(enums::AuthReq::Sc) as _);
}

self
Expand Down
13 changes: 13 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use bitflags::bitflags;
use esp_idf_sys::*;

#[repr(u8)]
Expand Down Expand Up @@ -79,3 +80,15 @@ pub enum PowerType {
/// For default, if not set other, it will use default value
Default = esp_ble_power_type_t_ESP_BLE_PWR_TYPE_DEFAULT as _,
}

bitflags! {
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct AuthReq: u8 {
/// allow bounding
const Bond = 0b001;
/// man in the middle protection
const Mitm = 0b010;
/// secure connection pairing
const Sc = 0b100;
}
}

0 comments on commit 6da5709

Please sign in to comment.