bjoernQ / bleps

A toy-level BLE peripheral stack
MIT License
55 stars 18 forks source link

Fallible and async read/write callbacks #14

Closed jneem closed 1 year ago

jneem commented 1 year ago

Would you be interested in generalizing AttData to allow errors and futures? The errors would involve changing AttData to be like

trait AttData {
  fn read(&mut self, offset: usize, data: &mut [u8]) -> Result<usize, AttErrorCode>;
  fn write(&mut self, offset: usize, data: &[u8]) -> Result<(), AttErrorCode>;
}

The async version might have to involve another trait, like bleps::async_attribute::Attribute or something.

You could still of course implement AttData for FnMut(usize, &[u8]) and similar, so it probably wouldn't cause much breakage.

bjoernQ commented 1 year ago

That would probably not hurt but also need some changes to actually report the error to the central device

jneem commented 1 year ago

I had a go at the fallible part of this, but async was trickier than I expected because most (all?) usage of AttData is via &dyn AttData, so slapping async on the methods won't work. I think this can be worked around by something like

trait AsyncAttData {
  fn read<'self>(&'self mut self, offset: usize, data: &mut [u8]) -> Pin<&'self mut dyn Future<Output=Result<usize, AttErrorCode>>>;
}

but then implementing AsyncAttData gets more painful and I start to wonder whether it's worth the effort...

bjoernQ commented 1 year ago

but then implementing AsyncAttData gets more painful and I start to wonder whether it's worth the effort...

I agree - it's probably not worth the effort for now 👍

bjoernQ commented 1 year ago

fixed by #15