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

BLE Module for Linux with BlueR #314

Open
MathJud opened this issue Feb 20, 2022 · 4 comments
Open

BLE Module for Linux with BlueR #314

MathJud opened this issue Feb 20, 2022 · 4 comments
Labels
Discussion Feature Request A new feature, that extends functionality Linux Network Networking Issues Rust

Comments

@MathJud
Copy link
Member

MathJud commented Feb 20, 2022

Create a BLE (Bluetooth Low Energy) module in rust which compatible to the two existing modules for Android & iOS.

Existing android BLE module:
https://github.com/qaul/qaul.net/tree/main/qaul_ui/android/blemodule

It shall do the following things:

  • Listen for BLE advertisements
    • detect neighbour nodes
  • Send BLE advertisement messages in regular intervals.
  • send arbitrary binary data to nodes nearby.
  • receive arbitrary binary data from nodes nearby.

The module is independent and will communicate via protobuf to the controlling backend library libqaul.

BlueR is the official Bluetooth low energy repository for for rust.
https://github.com/bluez/bluer

Prototypes with partial functionality already exist.

@MathJud MathJud added Feature Request A new feature, that extends functionality Discussion Linux Network Networking Issues Rust labels Feb 20, 2022
@6d7a
Copy link
Member

6d7a commented Feb 3, 2023

I'd be interested to have a shot at this issue. To give me little headstart, could you point out at what point the BLE module is started from the flutter app? And regarding the existing prototypes, what branches should I have a look at? Thanks.

@MathJud
Copy link
Member Author

MathJud commented Feb 4, 2023

@6d7a great!

I'm here for questions and discussions!

There are a few points about compatibility:

1) The communication via the protobuf sys messages between libqaul and the blemodules. This is the common API for all system dependent modules.

The module can be found here: rust/libqaul/src/connections/ble

The protobuf communication between libqaul and the blemodule is defined in this protobuf file: rust/libqaul/src/connections/ble/ble.proto.

This protobuf file creates the following rust source code: rust/libqaul/src/connections/ble/qaul.sys.ble.rs

The communication principle is relatively simple, libqaul sends a 'request' message and the module responds with the correlating 'response'.

The program flow is the following:

a) At program startup the blemodule is loaded and libqaul sends a BleInfoRequest

  • the blemodule checks the capabilities of the available bluetooth module and sends these information in the BleInfoResponse message to libqaul.
    b) Libqaul checks if a the bluetooth-device is capable of BLE and sends the BleStartRequest message to the ble module.
  • the BleStartRequest sends the shortened 16 byte 'small qaulid'. (this will be changed in the next weeks to the new shorter 8 byte 'q8id')
  • the blemodule starts the bluetooth device and returns a BleStartResult message with the start result: Success or the reasons of an Error
    c) When the blemodule starts the following happens:
  • the GATT service is created
  • the module listenes to advertisements
  • the module sends advertisements
    d) If a new device is discovered, the blemodule sends a BleDeviceDiscovered message to libqaul.
  • The blemodule needs to have a list with all discovered devices and their small qaulid. As some devices change their bluetooth device address randomly every 15 minutes.
  • The device bluetooth addresses are only known by the blemodule and not by libqaul. libqaul interacts with the blemodule only on the basis of the 'small qaulid'.
    e) to send a direct message to another device, libqaul sends a BleDirectSend message to the blemodule.
  • the blemodule connects to the related device and sends the message via the GATT service.
  • the blemodule returns a BleDirectSendResult message to libqaul
    f) When the blemodule receives a message from another device it forwards this message to libqaul via BleDirectReceived message.

2) The already existing modules for Android and iOS that are being integrated at the moment.

We have a custom writable GATT service to send direct messages to a device:

  • Service ID 99E91399-80ED-4943-9BCB-39C532A7602
    • write to 99E91402-80ED-4943-9BCB-39C532A7602 to send a message

General

At the moment we are fixing some last issues of the Android module on the branch ble-startup-fixing

We will document the exact behaviour of it for that it can be used as a reference.

The BLE communcation shall be enhanced over the next months. It has been implemented this way for the biggest compatibility (compatible with bluetooth 4 devices). There are more specific BLE-modes in Bluetooth 5 that can increase range and bandwith and have the possibility to broadcast bigger amount of data to all devices nearby.

To have a linux version for developing and testing would be very helpful.

I hope this was helpful and gave you a first overview.
Don't hesitate to contact me with any questions.

@6d7a
Copy link
Member

6d7a commented Feb 6, 2023

@MathJud, great! Thanks for the explanation. I'll give it a go and push some WIP PRs along the way. I still haven't wrapped my head around the RPC call chain, so just to recap (using Android):

  1. The android app is started (The app at qaul.net/qaul_ui/android/app, right? Is qaul.net/android/app used?)
  2. The android app loads libqaul and blemodule AARs
  3. The app sets up message channels between the UI and libqaul here
  4. And this is where I'm stuck right now, because I see that there are JNI methods to set up channels for sys communication between the blemodule and libqaul here, but I don't see that they are called anywhere. I'm assuming that the UI component acts as a proxy in the communication between libqaul and the blemodule.

Thanks again for the help

@MathJud
Copy link
Member Author

MathJud commented Mar 19, 2023

@kevin

Thanks again for this helpful PR!

The interconnection between libqaul's BLE connectivity-module and the Android BLE module is done in the branch ble-startup-fixing-rebased-06022023 and will be merged to main as soon as all remaining small problems are ironed out.

It is referenced to this PR #533

I rebased your branch on the ble-startup-fixing-rebased-06022023 branch and called it linux_ble pushed it to the qaul repo:
https://github.com/qaul/qaul.net/tree/linux_ble

And created the following PR from it: #539


I change the following things:

TODO:

The BLE module should send it's messages to libqaul.

Please don't hesitate to interact on it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion Feature Request A new feature, that extends functionality Linux Network Networking Issues Rust
Projects
None yet
Development

No branches or pull requests

2 participants