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

Implement Display and Debug traits for BLERemoteCharacteristic & BLERemoteService #66

Merged
merged 2 commits into from
Jan 11, 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
8 changes: 4 additions & 4 deletions examples/ble_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ fn main() {
let characteristic = service.get_characteristic(uuid).await.unwrap();
let value = characteristic.read_value().await.unwrap();
::log::info!(
"{:?} value: {}",
uuid,
"{} value: {}",
characteristic,
core::str::from_utf8(&value).unwrap()
);

let uuid = uuid128!("a3c87500-8ed3-4bdf-8a39-a01bebede295");
let characteristic = service.get_characteristic(uuid).await.unwrap();

if !characteristic.can_notify() {
return ::log::error!("characteristic can't notify: {:?}", uuid);
return ::log::error!("characteristic can't notify: {}", characteristic);
}

::log::info!("subscribe {:?}", uuid);
::log::info!("subscribe to {}", characteristic);
characteristic
.on_notify(|data| {
::log::info!("{}", core::str::from_utf8(data).unwrap());
Expand Down
27 changes: 25 additions & 2 deletions src/client/ble_remote_characteristic.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use core::borrow::Borrow;

use super::ble_remote_service::BLERemoteServiceState;
use super::{BLEReader, BLEWriter};
use crate::{
Expand Down Expand Up @@ -27,8 +29,8 @@ bitflags! {

#[allow(clippy::type_complexity)]
pub struct BLERemoteCharacteristicState {
service: WeakUnsafeCell<BLERemoteServiceState>,
uuid: BleUuid,
pub(crate) service: WeakUnsafeCell<BLERemoteServiceState>,
pub(crate) uuid: BleUuid,
pub handle: u16,
end_handle: u16,
properties: GattCharacteristicProperties,
Expand Down Expand Up @@ -264,3 +266,24 @@ impl BLERemoteCharacteristic {
}
}
}

impl core::fmt::Display for BLERemoteCharacteristic {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "BLERemoteCharacteristic[{}]", self.state.uuid)
}
}

impl core::fmt::Debug for BLERemoteCharacteristic {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.debug_struct("BLERemoteCharacteristic")
.field("uuid", &self.state.uuid)
.field(
"service",
&self.state.service.upgrade().map(|svc| svc.borrow().uuid),
)
.field("handle", &self.state.handle)
.field("end_handle", &self.state.end_handle)
.field("properties", &self.state.properties)
.finish()
}
}
23 changes: 20 additions & 3 deletions src/client/ble_remote_service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use core::ffi::c_void;

pub struct BLERemoteServiceState {
client: WeakUnsafeCell<BLEClientState>,
uuid: BleUuid,
pub(crate) uuid: BleUuid,
start_handle: u16,
pub(crate) end_handle: u16,
pub(crate) characteristics: Option<Vec<BLERemoteCharacteristic>>,
Expand Down Expand Up @@ -104,9 +104,26 @@ impl BLERemoteService {
}
}

impl core::fmt::Display for BLERemoteService {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "BLERemoteService[{}]", self.state.uuid)
}
}

impl core::fmt::Debug for BLERemoteService {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "BLERemoteService[{}]", self.state.uuid)?;
Ok(())
f.debug_struct("BLERemoteService")
.field("uuid", &self.state.uuid)
.field("start_handle", &self.state.start_handle)
.field("end_handle", &self.state.end_handle)
.field(
"characteristics",
&self
.state
.characteristics
.as_ref()
.map(|chars| chars.iter().map(|c| c.uuid()).collect::<Vec<_>>()),
)
.finish()
}
}