persello/bluedroid

Change method type of `on_write`

Closed this issue · 6 comments

Currently on_write uses fn for callbacks. The downside of that is that you cannot capture the environment that way. Does it make sense to change it to Fn to allow that?

I started a conversion draft for read callbacks (will also follow with writes). Now they are Fn instead of function pointers. The work is being done in the feature/fn-callbacks branch, but it’s not my highest priority (I’m working on a firmware that uses this library as of now).

I made a commit where I changed the type of the read callbacks and it builds, but when you read a characteristic, you’ll always get an empty response. I think there might be some reference issues.

Since this is the first time I work with Fn, FnMut and FnOnce, any help would be appreciated.

I would love to help out! This is also my first time working with the different types of closures 🙂

I forked and played around with this a bit. I think FnMut is the right way to go on this but I am having trouble getting it implemented. I'll need to keep messing more with it. To go the FnMut route, I needed to use a lot of generics which are causing other problems.

Here's the link to the commit.

cwoolum@4e918af

That's the initial approach I tried, but after making the entire GATT Server attribute "tree" generic for the read/write callbacks, many type issues arose.

I opted for a dynamic dispatch approach on my branch, but I need to understand what needs to be modified in gatts_event_handler.rs in order for the callback to be called correctly.

If you have any progress on your side, let me know.

Merged feature/fn-callbacks into develop. Have a look at the new server.rs example to see how it works. Is it correct?

Closing as the variables can now be moved inside closures. Feel free to reopen if needed.

Aaah, this works great for me! I couldn't get the right types for write_callback when I was working on it but now that I see it, it makes sense! Thanks!