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

Allow requiring pairing for GATT notifications/indications #43

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions src/gatt/characteristic.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{descriptor::Descriptor, event::EventSender};
use super::descriptor::Descriptor;
use std::{
collections::HashSet,
hash::{Hash, Hasher},
Expand Down Expand Up @@ -31,4 +31,4 @@ impl Characteristic {

impl_uuid_hash_eq!(Characteristic);

properties!(WriteWithAndWithoutResponse, EventSender, { notify: EventSender, indicate: EventSender });
properties!(WriteWithAndWithoutResponse, {notify, indicate});
3 changes: 1 addition & 2 deletions src/gatt/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use super::event::EventSender;
use std::hash::{Hash, Hasher};
use uuid::Uuid;

Expand All @@ -21,4 +20,4 @@ impl Descriptor {

impl_uuid_hash_eq!(Descriptor);

properties!(WriteWithResponse, EventSender);
properties!(WriteWithResponse);
102 changes: 38 additions & 64 deletions src/gatt/gatt_properties.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,51 @@
macro_rules! _define_operation_struct {
($struct:ident) => {
#[derive(Debug, Clone)]
pub struct $struct(pub Secure);

impl $struct {
pub fn sender(self: Self) -> crate::gatt::event::EventSender {
self.0.sender()
}
}
};
}
macro_rules! _write_type {
(WriteWithAndWithoutResponse, $event_sender:ident, $secure:ident) => {
(WriteWithAndWithoutResponse) => {
#[derive(Debug, Clone)]
pub enum Write {
WithResponse($secure),
WithoutResponse($event_sender),
WithResponse(Secure),
WithoutResponse(crate::gatt::event::EventSender),
}

impl Write {
pub fn sender(self: Self) -> $event_sender {
pub fn sender(self: Self) -> crate::gatt::event::EventSender {
match self {
Write::WithResponse(event_sender) => event_sender.sender(),
Write::WithoutResponse(event_sender) => event_sender,
}
}
}
};
(WriteWithResponse, $event_sender:ident, $secure:ident) => {
#[derive(Debug, Clone)]
pub struct Write(pub $secure);

impl Write {
pub fn sender(self: Self) -> $event_sender {
self.0.sender()
}
}

impl std::ops::Deref for Write {
type Target = $secure;

fn deref(&self) -> &Self::Target {
&self.0
}
}
(WriteWithResponse) => {
_define_operation_struct!(Write);
};
}

macro_rules! _properties {
($event_sender:ident, { $($member:ident: $member_type:ty,)* }) => {
({ $($member:ident,)* }) => {
#[derive(Debug, Clone)]
pub struct Properties {
pub(crate) read: Option<Read>,
pub(crate) write: Option<Write>,
$(pub(crate) $member: Option<$member_type>,)*
$(pub(crate) $member: Option<ServerInitiated>,)*
}

impl Properties {
pub fn new(
read: Option<Read>,
write: Option<Write>,
$($member: Option<$member_type>,)*
$($member: Option<ServerInitiated>,)*
) -> Self {
Properties {
read,
Expand All @@ -62,31 +59,17 @@ macro_rules! _properties {
}
}

#[derive(Debug, Clone)]
pub struct Read(pub Secure);

impl Read {
pub fn sender(self: Self) -> $event_sender {
self.0.sender()
}
}

impl std::ops::Deref for Read {
type Target = Secure;

fn deref(&self) -> &Secure {
&self.0
}
}
_define_operation_struct!(Read);
_define_operation_struct!(ServerInitiated);

#[derive(Debug, Clone)]
pub enum Secure {
Secure($event_sender),
Insecure($event_sender),
Secure(crate::gatt::event::EventSender),
Insecure(crate::gatt::event::EventSender),
}

impl Secure {
pub fn sender(self: Self) -> $event_sender {
pub fn sender(self: Self) -> crate::gatt::event::EventSender {
match self {
Secure::Secure(event_sender) => event_sender,
Secure::Insecure(event_sender) => event_sender,
Expand All @@ -95,30 +78,21 @@ macro_rules! _properties {
}
}
}

macro_rules! properties {
(WriteWithResponse, $event_sender:ident, { $($member:ident: $member_type:ty,)* }) => {
_write_type!(WriteWithResponse, $event_sender, Secure);
_properties!($event_sender, { $($member: $member_type,)* });
};
(WriteWithResponse, $event_sender:ident, { $($member:ident: $member_type:ty),* }) => {
_write_type!(WriteWithResponse, $event_sender, Secure);
_properties!($event_sender, { $($member: $member_type,)* });
};
(WriteWithResponse, $event_sender:ident) => {
_write_type!(WriteWithResponse, $event_sender, Secure);
_properties!($event_sender, {});
(WriteWithResponse, { $($member:ident),* }) => {
_write_type!(WriteWithResponse);
_properties!({ $($member,)* });
};
(WriteWithAndWithoutResponse, $event_sender:ident, { $($member:ident: $member_type:ty,)* }) => {
_write_type!(WriteWithAndWithoutResponse, $event_sender, Secure);
_properties!($event_sender, { $($member: $member_type,)* });
(WriteWithResponse) => {
_write_type!(WriteWithResponse);
_properties!({});
};
(WriteWithAndWithoutResponse, $event_sender:ident, { $($member:ident: $member_type:ty),* }) => {
_write_type!(WriteWithAndWithoutResponse, $event_sender, Secure);
_properties!($event_sender, { $($member: $member_type,)* });
(WriteWithAndWithoutResponse, { $($member:ident),* }) => {
_write_type!(WriteWithAndWithoutResponse);
_properties!({ $($member,)* });
};
(WriteWithAndWithoutResponse, $event_sender:ident) => {
_write_type!(WriteWithAndWithoutResponse, $event_sender:ident, Secure);
properties!($event_sender, {});
(WriteWithAndWithoutResponse) => {
_write_type!(WriteWithAndWithoutResponse);
properties!({});
};
}
6 changes: 4 additions & 2 deletions src/peripheral/bluez/gatt/characteristic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ impl Characteristic {
.notify
.clone()
.or_else(|| characteristic.properties.indicate.clone())
.ok_or_else(|| MethodErr::from((BLUEZ_ERROR_NOTSUPPORTED, "")))?;
.ok_or_else(|| MethodErr::from((BLUEZ_ERROR_NOTSUPPORTED, "")))?
.sender();
event_sender
.send(gatt::event::Event::NotifySubscribe(notify_subscribe))
.await
Expand All @@ -189,7 +190,8 @@ impl Characteristic {
.notify
.clone()
.or_else(|| characteristic.properties.indicate.clone())
.ok_or_else(|| MethodErr::from((BLUEZ_ERROR_NOTSUPPORTED, "")))?;
.ok_or_else(|| MethodErr::from((BLUEZ_ERROR_NOTSUPPORTED, "")))?
.sender();
event_sender
.send(gatt::event::Event::NotifyUnsubscribe)
.await
Expand Down
21 changes: 17 additions & 4 deletions src/peripheral/bluez/gatt/flags.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub trait Flags {
impl Flags for CharacteristicProperties {
fn flags(self: &Self) -> Vec<String> {
let mut flags = vec![];

if let Some(ref read) = self.read {
let read_flags: &[&str] = match read.0 {
characteristic::Secure::Secure(_) => &["secure-read", "encrypt-authenticated-read"],
Expand All @@ -31,12 +32,24 @@ impl Flags for CharacteristicProperties {
flags.extend_from_slice(write_flag);
}

if self.notify.is_some() {
flags.push("notify");
if let Some(ref notify) = self.notify {
let notify_flags: &[&str] = match notify.0 {
characteristic::Secure::Secure(_) => {
&["encrypt-authenticated-notify", "secure-notify"]
}
characteristic::Secure::Insecure(_) => &["notify"],
};
flags.extend_from_slice(notify_flags);
}

if self.indicate.is_some() {
flags.push("indicate");
if let Some(ref indicate) = self.indicate {
let indicate_flags: &[&str] = match indicate.0 {
characteristic::Secure::Secure(_) => {
&["encrypt-authenticated-indicate", "secure-indicate"]
}
characteristic::Secure::Insecure(_) => &["indicate"],
};
flags.extend_from_slice(indicate_flags);
}

flags.iter().map(|s| String::from(*s)).collect()
Expand Down
4 changes: 3 additions & 1 deletion tests/peripheral.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ async fn it_advertises_gatt() {
Some(characteristic::Write::WithResponse(
characteristic::Secure::Insecure(sender_characteristic.clone()),
)),
Some(sender_characteristic),
Some(characteristic::ServerInitiated(
characteristic::Secure::Insecure(sender_characteristic),
)),
None,
),
None,
Expand Down