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

Added current_data to OnWriteArgs #81

Merged
merged 1 commit into from
Jan 29, 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: 6 additions & 2 deletions examples/ble_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,12 @@ fn main() {
.on_read(move |_, _| {
::log::info!("Read from writable characteristic.");
})
.on_write(move |args| {
::log::info!("Wrote to writable characteristic: {:?}", args.recv_data);
.on_write(|args| {
::log::info!(
"Wrote to writable characteristic: {:?} -> {:?}",
args.current_data(),
args.recv_data()
);
});

let ble_advertising = ble_device.get_advertising();
Expand Down
10 changes: 7 additions & 3 deletions examples/ble_start_stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,15 @@ fn main() {
);
writable_characteristic
.lock()
.on_read(move |_, _| {
.on_read(|_, _| {
::log::info!("Read from writable characteristic.");
})
.on_write(move |args| {
::log::info!("Wrote to writable characteristic: {:?}", args.recv_data);
.on_write(|args| {
::log::info!(
"Wrote to writable characteristic: {:?} -> {:?}",
args.current_data(),
args.recv_data()
);
});

let ble_advertising = ble_device.get_advertising();
Expand Down
32 changes: 18 additions & 14 deletions src/server/ble_characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,25 @@ impl BLECharacteristic {

let mut notify = false;

if let Some(callback) = &mut characteristic.on_write {
let desc = crate::utilities::ble_gap_conn_find(conn_handle).unwrap();
let mut arg = OnWriteArgs {
recv_data: &buf,
desc: &desc,
reject: false,
error_code: 0,
notify: false,
};
callback(&mut arg);

if arg.reject {
return arg.error_code as _;
unsafe {
let characteristic = UnsafeCell::new(&mut characteristic);
if let Some(callback) = &mut (*characteristic.get()).on_write {
let desc = crate::utilities::ble_gap_conn_find(conn_handle).unwrap();
let mut arg = OnWriteArgs {
current_data: (*characteristic.get()).value.value(),
recv_data: &buf,
desc: &desc,
reject: false,
error_code: 0,
notify: false,
};
callback(&mut arg);

if arg.reject {
return arg.error_code as _;
}
notify = arg.notify;
}
notify = arg.notify;
}
characteristic.set_value(&buf);
if notify {
Expand Down
28 changes: 16 additions & 12 deletions src/server/ble_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,18 +123,22 @@ impl BLEDescriptor {
om = unsafe { (*om).om_next.sle_next };
}

if let Some(callback) = &mut descriptor.on_write {
let desc = crate::utilities::ble_gap_conn_find(conn_handle).unwrap();
let mut arg = OnWriteDescriptorArgs {
recv_data: &buf,
desc: &desc,
reject: false,
error_code: 0,
};
callback(&mut arg);

if arg.reject {
return arg.error_code as _;
unsafe {
let descriptor = UnsafeCell::new(&mut descriptor);
if let Some(callback) = &mut (*descriptor.get()).on_write {
let desc = crate::utilities::ble_gap_conn_find(conn_handle).unwrap();
let mut arg = OnWriteDescriptorArgs {
current_data: (*descriptor.get()).value.value(),
recv_data: &buf,
desc: &desc,
reject: false,
error_code: 0,
};
callback(&mut arg);

if arg.reject {
return arg.error_code as _;
}
}
}
descriptor.set_value(&buf);
Expand Down
34 changes: 30 additions & 4 deletions src/server/on_write_args.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,27 @@
use crate::BLEConnDesc;

pub struct OnWriteArgs<'a> {
pub recv_data: &'a [u8],
pub desc: &'a BLEConnDesc,
pub(crate) current_data: &'a [u8],
pub(crate) recv_data: &'a [u8],
pub(crate) desc: &'a BLEConnDesc,
pub(crate) reject: bool,
pub(crate) error_code: u8,
pub(crate) notify: bool,
}

impl OnWriteArgs<'_> {
pub fn current_data(&self) -> &[u8] {
self.current_data
}

pub fn recv_data(&self) -> &[u8] {
self.recv_data
}

pub fn desc(&self) -> &BLEConnDesc {
self.desc
}

/// If the reject is called, no value is written to BLECharacteristic or BLEDescriptor.
/// A write error (0xFF) is sent to the sender.
pub fn reject(&mut self) {
Expand All @@ -28,13 +41,26 @@ impl OnWriteArgs<'_> {
}

pub struct OnWriteDescriptorArgs<'a> {
pub recv_data: &'a [u8],
pub desc: &'a BLEConnDesc,
pub(crate) current_data: &'a [u8],
pub(crate) recv_data: &'a [u8],
pub(crate) desc: &'a BLEConnDesc,
pub(crate) reject: bool,
pub(crate) error_code: u8,
}

impl OnWriteDescriptorArgs<'_> {
pub fn current_data(&self) -> &[u8] {
self.current_data
}

pub fn recv_data(&self) -> &[u8] {
self.recv_data
}

pub fn desc(&self) -> &BLEConnDesc {
self.desc
}

/// If the reject is called, no value is written to BLECharacteristic or BLEDescriptor.
/// A write error (0xFF) is sent to the sender.
pub fn reject(&mut self) {
Expand Down