Skip to content

Instantly share code, notes, and snippets.

@lupyuen
Last active April 11, 2020 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lupyuen/0db460687b5aea48885ab7859fda17cc to your computer and use it in GitHub Desktop.
Save lupyuen/0db460687b5aea48885ab7859fda17cc to your computer and use it in GitHub Desktop.

PineTime Challenge: Rust Bindings for Bluetooth Mesh

Bluetooth Mesh is incredibly hard to program in C. Can we create simpler bindings in Rust? Maybe even a Domain-Specific Language?

Read this article first to understand Bluetooth Mesh basics:

https://medium.com/@ly.lee/bluetooth-mesh-with-nrf52-and-apache-mynewt-44823407c471?source=friends_link&sk=ac433fa04e13f75985927c37db1e2b10

Here's the Bluetooth Mesh Code from the open-source Apache NimBLE project that needs to be simplified. NimBLE is the open-source Bluetooth Stack for Mynewt, Zephyr and RIOT OSes.

https://github.com/apache/mynewt-nimble/blob/master/apps/blemesh_models_example_2/src/device_composition.c

  1. BT Mesh runs on Models. A Model is a global schema that defines what kind of data the nodes will send and receive...

  2. On-Off Model: Switch a light on or off, or query its state, for example

  3. Level Model: Set or get the brightness of a light, for example

  4. You can see the On-Off and Level Models defined here (yes this example has so many Models): https://github.com/apache/mynewt-nimble/blob/master/apps/blemesh_models_example_2/src/device_composition.c#L2691-L2749

  5. Within On-Off and Level Models, there are Server Models and Client Models. Bluetooth Mesh supports Publish and Subscribe, so that the Servers can push data updates to the Clients

  6. The Message Handlers for the Models are mapped here, so that a light knows how to switch itself on or off when it receives a message: https://github.com/apache/mynewt-nimble/blob/master/apps/blemesh_models_example_2/src/device_composition.c#L2546-L2689

  7. Here's the Message Handler code that switches the light on/off: https://github.com/apache/mynewt-nimble/blob/master/apps/blemesh_models_example_2/src/device_composition.c#L289-L356

  8. Here's the Message Handler code that queries the on/off status of the light: https://github.com/apache/mynewt-nimble/blob/master/apps/blemesh_models_example_2/src/device_composition.c#L174-L196

  9. There seem to be Acknowledgement vs No Acknowledgement versions off the Message Handlers. Don't really think both are necessary. We should just support one type for now.

  10. The code uses "Transitions" because real devices have some delay when changing on/off states. Again let's ignore this.

  11. If you understand everything I've said so far... Now read the really terse official ReadMe from the NimBLE source :-) https://github.com/apache/mynewt-nimble/blob/master/apps/blemesh_models_example_2/README.md

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment