tessel / openwrt-tessel

OpenWrt build scripts and package feed for Tessel 2
Other
32 stars 14 forks source link

Initializing BLE devices automatically #35

Closed johnnyman727 closed 8 years ago

johnnyman727 commented 8 years ago

After working out a few kinks in how we manage binary dependencies, one should, in theory, be able to just run either noble or bleno to control your USB BLE dongle on T2. However, there is currently one hurdle that we don't have a solid plan for: before using the device, you must initialize it with the command:

hciconfig hci0 up # `hci0` will probably change if you plug in more than one dongle

I have a couple ideas of how we can make this automatically work, but they aren't necessarily ideal. I also imagine that this might not be a one-off case - we may need to create a mechanism for a node package to run arbitrary shell commands (scary!) prior to initializing a script.

  1. Create a daemon that's listening to ubus output for new USB devices. When it detects the USB device, it checks what kind of device it is and if it's a BLE device, we initialize it. We can generalize this a bit more so you can register a script/command for particular types of detected devices.
  2. Create a new field to search for within package.json that documents shell scripts that need to be run on Tessel prior to starting the module. Have the CLI find extract those and run them just before running the actual script.
  3. Make a new module that wraps noble and bleno and spawns a child process to initialize any interfaces before running the actual noble code (not scalable/generalizable).

@kevinmehall you may have opinions/suggestions about this.

johnnyman727 commented 8 years ago

After talking with @rwaldron, option 3 might be best in this case. Not only do we need to run the command to turn on the BLE dongle, but we need to force the download of optional npm dependencies (bluetooth-hci-socket). If we make a wrapper around noble, we will be able to do both of these things. @sandeepmistry - do you have thoughts about this?

sandeepmistry commented 8 years ago

Is having BlueZ's bluetoothd daemon installed and running an option? It would take care of this, and also enable classic Bluetooth features (audio, serial, etc).

One gotcha is a one line patch is needed to make it compatible with bleno currently (see https://github.com/sandeepmistry/bleno/issues/24).

johnnyman727 commented 8 years ago

@sandeepmistry ah cool! I didn't know such a daemon exists. I checked and it is installed on Tessel's OpenWRT image but the added patch makes it a bit more painful than the option of wrapping noble/bleno in a Tessel specific package, IMO.

kevinmehall commented 8 years ago

As a variant of option 1, OpenWrt procd runs scripts in /etc/hotplug.d/usb/ when a USB device is added or removed.

https://wiki.openwrt.org/doc/techref/hotplug

sandeepmistry commented 8 years ago

@johnnyman727 fair enough.

@kevinmehall's hot plug idea sounds good.

Briefly looking at udev rules might be worthwhile as well.

kevinmehall commented 8 years ago

OpenWrt uses procd instead of udev, but it might be beneficial to use the more-standard udev (#36).

johnnyman727 commented 8 years ago

Thanks @kevinmehall and @sandeepmistry. I was able to get a semi-functional implementation with hotplug. The biggest limitation I see currently is that the hotplug scripts don't get if a USB device is plugged in on bootup.

johnnyman727 commented 8 years ago

Resolved with https://github.com/tessel/openwrt-tessel/pull/37.