Skip to content

Commit

Permalink
Added BLEAddress::from_str
Browse files Browse the repository at this point in the history
  • Loading branch information
taks committed Dec 13, 2023
1 parent 47c4d7d commit 613def4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## [0.4.1]
- Added `BLEScan::find_device` ([#55](https::/github.com/taks/esp32-nimble/pull/55))
- Added `BLEAdvertisedDevice::adv_type`, `BLEAdvertisedDevice::adv_flags`
- Added `BLEAddress::from_str`

## [0.4.0] - 2023-12-01
- Added `BLEAdvertising::min_interval`, `BLEAdvertising::max_interval` ([#51](https::/github.com/taks/esp32-nimble/pull/51))
Expand Down
22 changes: 22 additions & 0 deletions src/ble_address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub enum BLEAddressType {
RandomID = BLE_ADDR_RANDOM_ID as _,
}

#[repr(transparent)]
#[derive(Copy, Clone)]
pub struct BLEAddress {
pub(crate) value: esp_idf_sys::ble_addr_t,
Expand All @@ -24,6 +25,27 @@ impl BLEAddress {
ret.value.val.reverse();
ret
}

pub fn from_str(input: &str, addr_type: BLEAddressType) -> Option<Self> {
let mut val = [0u8; 6];

let mut nth = 0;
for byte in input.split(|c| c == ':' || c == '-') {
if nth == 6 {
return None;
}

val[nth] = u8::from_str_radix(byte, 16).ok()?;

nth += 1;
}

if nth != 6 {
return None;
}

Some(Self::new(val, addr_type))
}
}

impl From<esp_idf_sys::ble_addr_t> for BLEAddress {
Expand Down

0 comments on commit 613def4

Please sign in to comment.