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 OnWriteDescriptorArgs, OnWriteArgs::notify #75

Merged
merged 1 commit into from
Jan 26, 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
7 changes: 7 additions & 0 deletions src/server/ble_characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,21 +291,28 @@ impl BLECharacteristic {
om = unsafe { (*om).om_next.sle_next };
}

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 _;
}
notify = arg.notify;
}
characteristic.set_value(&buf);
if notify {
characteristic.notify();
}

0
}
Expand Down
8 changes: 4 additions & 4 deletions src/server/ble_descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::{
ble_npl_hw_enter_critical, ble_npl_hw_exit_critical, mutex::Mutex, os_mbuf_append,
voidp_to_ref, BleUuid,
},
AttValue, OnWriteArgs,
AttValue, OnWriteDescriptorArgs,
};

bitflags! {
Expand All @@ -32,7 +32,7 @@ pub struct BLEDescriptor {
pub(crate) properties: DescriptorProperties,
value: AttValue,
on_read: Option<Box<dyn FnMut(&mut AttValue, &BLEConnDesc) + Send + Sync>>,
on_write: Option<Box<dyn FnMut(&mut OnWriteArgs) + Send + Sync>>,
on_write: Option<Box<dyn FnMut(&mut OnWriteDescriptorArgs) + Send + Sync>>,
}

impl BLEDescriptor {
Expand Down Expand Up @@ -70,7 +70,7 @@ impl BLEDescriptor {

pub fn on_write(
&mut self,
callback: impl FnMut(&mut OnWriteArgs) + Send + Sync + 'static,
callback: impl FnMut(&mut OnWriteDescriptorArgs) + Send + Sync + 'static,
) -> &mut Self {
self.on_write = Some(Box::new(callback));
self
Expand Down Expand Up @@ -125,7 +125,7 @@ impl BLEDescriptor {

if let Some(callback) = &mut descriptor.on_write {
let desc = crate::utilities::ble_gap_conn_find(conn_handle).unwrap();
let mut arg = OnWriteArgs {
let mut arg = OnWriteDescriptorArgs {
recv_data: &buf,
desc: &desc,
reject: false,
Expand Down
1 change: 1 addition & 0 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ pub mod hid;

mod on_write_args;
pub use self::on_write_args::OnWriteArgs;
pub use self::on_write_args::OnWriteDescriptorArgs;
27 changes: 27 additions & 0 deletions src/server/on_write_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub struct OnWriteArgs<'a> {
pub desc: &'a BLEConnDesc,
pub(crate) reject: bool,
pub(crate) error_code: u8,
pub(crate) notify: bool,
}

impl OnWriteArgs<'_> {
Expand All @@ -20,4 +21,30 @@ impl OnWriteArgs<'_> {
self.reject = true;
self.error_code = error_code;
}

pub fn notify(&mut self) {
self.notify = true;
}
}

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

impl OnWriteDescriptorArgs<'_> {
/// 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) {
self.reject_with_error_code(0xFF);
}

/// If the reject is called, no value is written to BLECharacteristic or BLEDescriptor.
/// The argument error code is sent to the sender.
pub fn reject_with_error_code(&mut self, error_code: u8) {
self.reject = true;
self.error_code = error_code;
}
}