use crate::codec::Reader;
use crate::host::EventKind;
use std::cell::RefCell;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Event {
CharacteristicValue {
device: String,
service: String,
characteristic: String,
value: Vec<u8>,
},
Disconnected { device: String },
ServiceChanged { device: String },
Advertisement {
device: String,
name: Option<String>,
rssi: i32,
tx_power: Option<i32>,
appearance: Option<u32>,
uuids: Vec<String>,
manufacturer_data: Vec<(u16, Vec<u8>)>,
service_data: Vec<(String, Vec<u8>)>,
},
AvailabilityChanged { available: bool },
}
type Sink = Box<dyn Fn(Event)>;
thread_local! {
static SINK: RefCell<Option<Sink>> = const { RefCell::new(None) };
}
pub fn set_sink(sink: impl Fn(Event) + 'static) {
SINK.with(|s| *s.borrow_mut() = Some(Box::new(sink)));
}
pub fn clear_sink() {
SINK.with(|s| *s.borrow_mut() = None);
}
fn deliver(event: Event) {
let sink = SINK.with(|s| s.borrow_mut().take());
if let Some(sink) = sink {
sink(event);
SINK.with(|s| {
let mut slot = s.borrow_mut();
if slot.is_none() {
*slot = Some(sink);
}
});
}
}
pub fn decode(kind: u32, body: &[u8]) -> Option<Event> {
let mut r = Reader::new(body);
let event = match EventKind::from_u32(kind)? {
EventKind::CharacteristicValue => Event::CharacteristicValue {
device: r.str()?,
service: r.str()?,
characteristic: r.str()?,
value: r.bytes()?,
},
EventKind::GattServerDisconnected => Event::Disconnected { device: r.str()? },
EventKind::ServiceChanged => Event::ServiceChanged { device: r.str()? },
EventKind::AdvertisementReceived => Event::Advertisement {
device: r.str()?,
name: r.option_str()?,
rssi: r.i32()?,
tx_power: r.bool()?.then(|| r.i32()).flatten(),
appearance: r.bool()?.then(|| r.u32()).flatten(),
uuids: r.list(|r| r.str())?,
manufacturer_data: r.list(|r| Some((r.u16()?, r.bytes()?)))?,
service_data: r.list(|r| Some((r.str()?, r.bytes()?)))?,
},
EventKind::AvailabilityChanged => Event::AvailabilityChanged {
available: r.bool()?,
},
};
r.is_empty().then_some(event)
}
#[no_mangle]
pub unsafe extern "C" fn wbt_event(kind: u32, ptr: *const u8, len: usize) {
let body = if ptr.is_null() || len == 0 {
&[][..]
} else {
unsafe { std::slice::from_raw_parts(ptr, len) }
};
if let Some(event) = decode(kind, body) {
deliver(event);
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::codec::Writer;
use std::cell::RefCell as Cell2;
use std::rc::Rc;
#[test]
fn a_notification_round_trips() {
let mut w = Writer::new();
w.str("dev-1")
.str("0000180f-0000-1000-8000-00805f9b34fb")
.str("00002a19-0000-1000-8000-00805f9b34fb")
.bytes(&[0x63]);
assert_eq!(
decode(EventKind::CharacteristicValue as u32, &w.finish()),
Some(Event::CharacteristicValue {
device: "dev-1".into(),
service: "0000180f-0000-1000-8000-00805f9b34fb".into(),
characteristic: "00002a19-0000-1000-8000-00805f9b34fb".into(),
value: vec![0x63],
})
);
}
#[test]
fn an_advertisement_round_trips_with_its_optional_fields() {
let mut w = Writer::new();
w.str("dev-2")
.option_str(Some("Thermometer"))
.u32(-55i32 as u32)
.bool(true)
.u32(-4i32 as u32)
.bool(false)
.list(&["0000180d-0000-1000-8000-00805f9b34fb"], |w, s| {
w.str(s);
})
.list(&[(0x004Cu16, vec![9u8])], |w, (c, d)| {
w.u16(*c).bytes(d);
})
.list(
&[("0000180f-0000-1000-8000-00805f9b34fb", vec![0x63u8])],
|w, (u, d)| {
w.str(u).bytes(d);
},
);
let got = decode(EventKind::AdvertisementReceived as u32, &w.finish());
assert_eq!(
got,
Some(Event::Advertisement {
device: "dev-2".into(),
name: Some("Thermometer".into()),
rssi: -55,
tx_power: Some(-4),
appearance: None,
uuids: vec!["0000180d-0000-1000-8000-00805f9b34fb".into()],
manufacturer_data: vec![(0x004C, vec![9])],
service_data: vec![("0000180f-0000-1000-8000-00805f9b34fb".into(), vec![0x63])],
})
);
}
#[test]
fn the_simple_events_round_trip() {
let mut w = Writer::new();
w.str("dev-3");
let body = w.finish();
assert_eq!(
decode(EventKind::GattServerDisconnected as u32, &body),
Some(Event::Disconnected {
device: "dev-3".into()
})
);
assert_eq!(
decode(EventKind::ServiceChanged as u32, &body),
Some(Event::ServiceChanged {
device: "dev-3".into()
})
);
let mut w = Writer::new();
w.bool(true);
assert_eq!(
decode(EventKind::AvailabilityChanged as u32, &w.finish()),
Some(Event::AvailabilityChanged { available: true })
);
}
#[test]
fn a_malformed_event_is_rejected() {
assert_eq!(decode(EventKind::GattServerDisconnected as u32, &[]), None);
assert_eq!(
decode(EventKind::CharacteristicValue as u32, &[5, 0, 0, 0, b'a']),
None
);
assert_eq!(decode(999, &[]), None);
}
#[test]
fn an_event_with_bytes_left_over_is_rejected() {
let mut w = Writer::new();
w.str("dev-4").u32(0xDEAD);
assert_eq!(
decode(EventKind::GattServerDisconnected as u32, &w.finish()),
None
);
}
#[test]
fn events_reach_the_installed_sink() {
let seen = Rc::new(Cell2::new(Vec::new()));
let out = seen.clone();
set_sink(move |e| out.borrow_mut().push(e));
let mut w = Writer::new();
w.str("dev-5");
let body = w.finish();
unsafe {
wbt_event(
EventKind::GattServerDisconnected as u32,
body.as_ptr(),
body.len(),
)
};
assert_eq!(
seen.borrow().as_slice(),
&[Event::Disconnected {
device: "dev-5".into()
}]
);
clear_sink();
}
#[test]
fn an_event_with_no_sink_is_dropped() {
clear_sink();
let mut w = Writer::new();
w.str("dev-6");
let body = w.finish();
unsafe { wbt_event(EventKind::ServiceChanged as u32, body.as_ptr(), body.len()) };
}
}