WebThingsIO / gateway

WebThings Gateway
http://webthings.io/gateway
Mozilla Public License 2.0
2.61k stars 337 forks source link

Can I use bluepy to build an addon? #1280

Closed brianpeiris closed 6 years ago

brianpeiris commented 6 years ago

I've started building an addon in python for an existing brand of commercial smart home devices. My addon uses bluepy and I've run into a couple of problems.

  1. bluepy includes a helper executable (bluepy-helper) that it uses to communicate with bluetooth devices. This executable needs the appropriate bluetooth network capabilities to function (cap_net_admin,cap_net_raw+eip). Is there a mechanism for me to grant these capabilities to the helper when someone installs my addon?

  2. While testing my addon locally (after manually granting the above capabilities), I noticed that the Web Thing addon (thing-url-adapter) interferes with my addon. It seems that it uses noble for eddystone beacon scanning and that overrides my addon's ability to connect to its own bluetooth devices. I can work around the issue by disabling thing-url-adapter. I see this in the gateway logs:

    # My addon attempting to change a property on its bluetooth device
    PropertyProxy: setProperty property: on for: [device id] to value: false
    # thing-url-adapter seems to intercept the bluetooth connection and 
    # my addon fails to set the property
    thing-url: noble: unknown peripheral [mac address] connected!                  
    thing-url: noble: unknown peripheral [mac address] disconnected!               

    Is this a bug in thing-url-adapter, or will you disallow other bluetooth libraries (like bluepy) from also accessing bluetooth devices?

dhylands commented 6 years ago

Your adapter can use sudo (at least on the RPi) to add capabilities if they haven't been added yet. This won't work under regular linux. Under ubuntu 18.04, you can do something like:

pkexec --user root command that you'd like to run as root

which would prompt the user using a graphical dialog and then run the command ala sudo.

brianpeiris commented 6 years ago

Hmm. Would that actually work through the gateway's usual addon installation UI, if I publish my addon?

dhylands commented 6 years ago

When the addon is executed it can check to see if the appropriate capabilities are assigned, and if not, call sudo setcap some-capability to set the capbility. Since the pi user doesn't need a password to execute sudo, this will work for any process. Each addon is started as a separate process.

brianpeiris commented 6 years ago

Thanks, that seems to work!

Any help with the second part of the issue? thing-url-adapter still interferes with bluepy when it tries to connect to a bluetooth device.

mrstegeman commented 6 years ago

It seems that bluepy doesn't play very well with noble, which thing-url-adapter (one of our default adapters) uses. Since we have that as a default, and that's the adapter which talks to native web things (our end goal), it would be best to stick with noble or other solutions that don't interfere.