In logger.* the device was printed out as "<bluepy.btle.Peripheral object
at 0x************>" which is not very useful to the user. Print the
deviceName variable instead.
Also add missing "f" for f-Strings.
The README instruction notes to run scratch_link.py with sudo. This is
not good for normal users especially for children. To allow normal users
to run scratch_link.py, revise the instruction.
Add a step to run setcap.sh at set up. Remove the step of btmgmt commands
with sudo which are not required when bluetooth.service is running.
Also add some more troubleshooting guides users might be caught at
scratch_link.py without sudo.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
To avoid sudo for scratch_link.py, it is required to set capability to
bluepy-helper executable so that normal users can connect to BLE devices
through bluepy. To simplify the command line to do this, add the helper
script setcap.sh.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
Guard peripheral disconnect handling with lock not to race with other
peripheral operations. After disconnect, set None to the peripheral
reference to ensure no use.
When scratch closes websocket connection, bluepy-scratch-link does not
close Bluetooth connection cleanly. This causes connection failure on
reconnect.
To close Bluetooth connection, catch ConnectionClosedError of websocket
and call close handler of the session.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
It was reported that bluepy-scratch-link fails to connect to BLE devices
with the PC has multiple Bluetooth adapters if the first adapter does not
support BLE. To support such a case, scan Bluetooth adapters up to 3.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
It was reported that the LEGO Boost brick connection needs 2 attempts.
The cause could be too short scan time. Extend the scan time from 1
second to 10 seconds.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
It was reported that Lego WeDo2 advertises its service class UUIDs with
adtype 0x6 "incomplete 128 bit service class UUIDs". Add this adtype as
well as some more adtypes 0x5, 0x4 and 0x2 defined in Bluetooth spec [1].
Also introduce _get_dev_uuid() helper function to simplify the code.
[1] https://www.bluetooth.com/specifications/assigned-numbers/generic-access-profile/
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
Now notification wait has 1 second timeout. This might be too long for
Lego Boost. Reduce it to 1 millisecond.
If BLE device does not send out any notification, bluepy-scratch-link
wait for 1 second and during this wait, write or read message from
Scratch is not sent to Boost. This is not an issue for micro:bit since
micro:bit sends out notifications frequently, the notification wait
completes before 1 second timeout.
It was reported that Lego Boost has a few seconds latency between Scratch
motor control command execution to Lego Boost motor move. To try out
smaller timeout, reduce the value.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
It turned out that bluepy Peripheral's getCharacteristics() and
getServiceByUUID() accesses shared resource with other bluepy APIs. Then
it needs guard with lock in same manner as other bluepy API calls. Guard
them with the lock of the session.
Introduce _get_characteristic() and _get_service() helper functions to
wrap the API calls with lock guard, and replace API calls with them.
Also remove unnecessary getCharacteristics() calls.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
To debug the dead lock status, print stack trace of all threads when
scratch_link.py stops by key interrupt.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
During trials to make Lego Boost work with bluepy-scratch-link, deadlock
race was observed between the lock for write to BLE device and
waitForNotifcations to BLE device. The cause of the dead lock is
coroutine call by BLE delegate handler to send out notifications to
Scratch through websocket conflict with websocket receive call by
websocket loop thread.
To avoid the conflict, only allow websocket loop thread to send out
through websocket. Have BLE thread to queue notifications so that it does
not need to touch websocket. On the other hand, introduce timeout when
websocket loop thread receives message from Scratch. This allow the
websocket loop thread to check the notifications queued. If any
notification is queued, the websocket loop thread sends it to Scratch.
Also fix notify() function argument signature, which was broken by commit
ba8eba4 ("BLESession: Introduce notification queue").
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
This reverts commit 2c7a10f848.
It turned out that string join with '\n' is not the proper way to pack
multiple JSON messages to Scratch Lego Boost extension.
During bluepy-scratch-link handles requests from Scratch, it throws away
notifications from BLE devices. This loses those notifications.
Not to lose the notifications, introduce a queue to keep notifications
from BLE devices. Once request handling completes, flush the
notifications kept in the queue.
The value 'startNotification' is optional for the "read" request.
However, bluepy-scratch-link expects Scratch always set the value in the
requests. This may cause KeyError.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
In the GitHub issue "Scratux support #9", debug message log was required
for analysis. However, script edit by users was required to enable debug
print. This is not handy.
To avoid that the chore by users, add -d debug print option.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
In the GitHub issue "Scratux support #9", it was noticed that
bluepy-scratch-link is not so informative to tell what is happening.
Especially when BLE controllers or BLE devices are not available, no
information is printed.
Enrich log information so that users can tell what is happening easier.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
With previous commit, gencert.sh automatically add the certificate to
FireFox or Chrome using certutil command. Blupy-scratch-link users no
need to allow the certificate explicitly.
Also add NSS to the required package list to install, which includes the
certutil command.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
As of today, bluepy-scratch-link users need to do special action to allow
local server certificates. This is trouble some and James Le Cuirot
suggested to automate the action with certutil tools
To avoid the user action, check if NSS DB of FireFox or Chrome exists. If
NSS DBs exist, add the certificate to those DBs.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
Now gencert.sh generates private key and certificate for the Secure WSS
server in a single file. This is not good to automate certification
addition to NSS databases. Generate them separately into two files and
initialize the Secure WSS server specifying them.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
To have new line after list items with numbers, the list item needs
period following empty line.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
The command lines to generate certificate file for web server are
troublesome and their parameters confuse users. To clean up the steps per
James Le Cuirot suggestions, simplify openssl options and bash commands
in the generation script and README.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
Added "Confirmed devices and distros" section to clarify who confirmed
with which device or distros bluepy-scratch-link working well. Remove
trailing white spaces. Modified git repo URL for installation.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
LEGO Boost communicates with a Scratch extension through Scratch-link.
It was reported that bluepy-scratch-link fails to connect to LEGO Boost.
LEGO Boost advertises adtype 0x7 "Complete List of 128-bit Service Class
UUIDs". However, bluepy-scratch-link checks only adtype 0x3 "Complete
List of 16-bit Service Class UUIDs" which is valid for micro:bit.
To allow bluepy-scratch-link, check both adtypes 0x7 and 0x3. Introduce
constants to note those two adtype values.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
As ngammarano reported in a GitHub issue, Chrome browser has bypass key
sequence to allow local server certificate. Add that description to
README.md merging jazzpecq's patch. Thank goes to the two :)
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
Clarify license of the bluepy-scratch-link script so that those who caring about licenses can take a look in the script code. I chose BSD 3-Clause "New" or "Revised" license, which is same as scratch-link to seek for the opportunity to get integrated into scratch-link, if possible.
It was reported that the bluepy-scratch-link does not run on Ubuntu 16.04
which has python version 3.5. The script stopped with an error about
f-format string, which was introduce with python 3.6.
Even removing f-format string from the script, bluepy-scratch-link still
requires python 3.6 because websockets module requires it. Clarify that
python version requirement to avoid confusion.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
Described why this script was written. Wrote down installation and usage
guide. Confirmed the guide with elementary os 5.
Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>