mirror of
https://github.com/kawasaki/pyscrlink.git
synced 2025-09-06 01:30:08 +02:00
Compare commits
9 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
ed4df282b1 | ||
|
00154f2b51 | ||
|
dc2ee5a22f | ||
|
caa344ecbb | ||
|
0f9ccd3b63 | ||
|
d1f7f58ca2 | ||
|
7050016ee6 | ||
|
d845d69bb8 | ||
|
751190935a |
27
README.md
27
README.md
@@ -102,7 +102,7 @@ Installation
|
||||
|
||||
5. For micro:bit, install Scratch-link hex on your device.
|
||||
|
||||
* Download and unzip the [micro:bit Scratch Hex file](https://downloads.scratch.mit.edu/microbit/scratch-microbit-1.1.0.hex.zip).
|
||||
* Download and unzip the [micro:bit Scratch Hex file](https://downloads.scratch.mit.edu/microbit/scratch-microbit.hex.zip).
|
||||
* Flash the micro:bit over USB with the Scratch Hex File, you will see the
|
||||
five character name of the micro:bit scroll across the screen such as
|
||||
'zo9ev'.
|
||||
@@ -141,10 +141,22 @@ In Case You Fail to Connect
|
||||
* If the service is not working, refer guide of your distro to set it up.
|
||||
* If the service is working, also check that /etc/bluetooth/main.conf sets AutoEnable=true.
|
||||
|
||||
3. If scratch_link.py says "failed to connect to BT device: [Errno 13] Permission denied",
|
||||
3. If device scan still fails, use -r option to retry device scan.
|
||||
The command line below does device scan twice. Each scan takes 10 seconds.
|
||||
```
|
||||
$ scratch_link -r 2
|
||||
```
|
||||
It would be good to use -s option together to reduce each scan duration.
|
||||
The command line below does 3 seconds device scan twice.
|
||||
|
||||
```
|
||||
$ scratch_link -r 2 -s 3
|
||||
```
|
||||
|
||||
4. If scratch_link.py says "failed to connect to BT device: [Errno 13] Permission denied",
|
||||
make sure to pair the bluetooth device to your PC before connecting to Scratch.
|
||||
|
||||
4. To connect to multiple devices at the same time, make all the target devices
|
||||
5. To connect to multiple devices at the same time, make all the target devices
|
||||
ready for scan at the first device scan. This is important for toio. The toio
|
||||
allows a single project to connect to two toio devices.
|
||||
* When the second device was prepared after the first device was connected, device scan can not find the second device.
|
||||
@@ -158,6 +170,15 @@ Please file issues to [GitHub issue tracker](https://github.com/kawasaki/pyscrli
|
||||
Releases
|
||||
--------
|
||||
|
||||
Release 0.2.8
|
||||
|
||||
* Supported Microbit More v2
|
||||
|
||||
Release 0.2.7
|
||||
|
||||
* Supported Snap Firefox and Chromium
|
||||
* Added -r option to retry BLE scan
|
||||
|
||||
Release 0.2.6
|
||||
|
||||
* Removed Bluetooth Classic and LEGO Mindstorm EV3 support
|
||||
|
@@ -152,36 +152,37 @@ def prep_nss_cert(dir, cert, nickname):
|
||||
remove_cert(dir, nickname)
|
||||
add_cert(dir, cert, nickname)
|
||||
|
||||
def prep_cert():
|
||||
# Generate certification and key
|
||||
gen_cert(cert_file_path, key_file_path)
|
||||
|
||||
# Add certificate to FireFox
|
||||
def prep_cert_for_app(cert, app, search_path):
|
||||
"""
|
||||
Find a NSS DB in the search_path for the app and prepare the cert in the DB.
|
||||
"""
|
||||
nssdb = None
|
||||
firefox_nss_path = os.path.join(homedir, ".mozilla/firefox/")
|
||||
for root, dirs, files in os.walk(firefox_nss_path):
|
||||
for root, dirs, files in os.walk(os.path.join(homedir, search_path)):
|
||||
for name in files:
|
||||
if not re.match("key.*\.db", name):
|
||||
continue
|
||||
nssdb = root
|
||||
if prep_nss_cert(nssdb, cert_file_path, SCRATCH_CERT_NICKNAME):
|
||||
logger.error(f"Failed to add certificate to FireFox NSS DB: {nssdb}")
|
||||
if prep_nss_cert(nssdb, cert, SCRATCH_CERT_NICKNAME):
|
||||
logger.error(f"Failed to add certificate to {app}: {nssdb}")
|
||||
sys.exit(3)
|
||||
else:
|
||||
logger.info(f"Certificate is ready in FireFox NSS DB: {nssdb}")
|
||||
logger.info(f"Certificate is ready in {app} NSS DB: {nssdb}")
|
||||
if not nssdb:
|
||||
logger.info("FireFox NSS DB not found. Do not add certificate.")
|
||||
logger.debug(f"NSS DB for {app} not found. Do not add certificate.")
|
||||
|
||||
# Add certificate to Chrome
|
||||
nssdb = os.path.join(homedir, ".pki/nssdb")
|
||||
if os.path.isdir(nssdb):
|
||||
if prep_nss_cert(nssdb, cert_file_path, SCRATCH_CERT_NICKNAME):
|
||||
logger.error(f"Failed to add certificate to Chrome")
|
||||
sys.exit(4)
|
||||
else:
|
||||
logger.info("Certificate is ready for Chrome")
|
||||
else:
|
||||
logger.info("Chrome NSS DB not found. Do not add certificate.")
|
||||
|
||||
def prep_cert():
|
||||
# Generate certification and key
|
||||
gen_cert(cert_file_path, key_file_path)
|
||||
|
||||
nss_dbs = {
|
||||
"FireFox": ".mozilla/firefox/",
|
||||
"FireFox(Snap)": "snap/firefox/common/.mozilla/firefox/",
|
||||
"Chrome": ".pki",
|
||||
"Chromium(Snap)": "snap/chromium",
|
||||
}
|
||||
|
||||
[ prep_cert_for_app(cert_file_path, k, nss_dbs[k]) for k in nss_dbs ]
|
||||
|
||||
if __name__ == "__main__":
|
||||
prep_cert()
|
||||
|
@@ -18,7 +18,7 @@ import traceback
|
||||
import argparse
|
||||
|
||||
# for BLESession (e.g. BBC micro:bit)
|
||||
from bluepy.btle import Scanner, UUID, Peripheral, DefaultDelegate
|
||||
from bluepy.btle import Scanner, UUID, Peripheral, DefaultDelegate, ScanEntry
|
||||
from bluepy.btle import BTLEDisconnectError, BTLEManagementError
|
||||
from pyscrlink import bluepy_helper_cap
|
||||
|
||||
@@ -292,15 +292,19 @@ class BLESession(Session):
|
||||
logger.debug("match...")
|
||||
return True
|
||||
if 'namePrefix' in f:
|
||||
# 0x08: Shortened Local Name
|
||||
deviceName = dev.getValueText(0x08)
|
||||
if not deviceName:
|
||||
continue
|
||||
logger.debug(f"Name of \"{deviceName}\" begins with: \"{f['namePrefix']}\"?")
|
||||
if(deviceName.startswith(f['namePrefix'])):
|
||||
logger.debug("Yes")
|
||||
return True
|
||||
logger.debug("No")
|
||||
logger.debug(f"given namePrefix: {f['namePrefix']}")
|
||||
deviceName = dev.getValueText(ScanEntry.SHORT_LOCAL_NAME)
|
||||
if deviceName:
|
||||
logger.debug(f"SHORT_LOCAL_NAME: {deviceName}")
|
||||
if deviceName.startswith(f['namePrefix']):
|
||||
logger.debug(f"match...")
|
||||
return True
|
||||
deviceName = dev.getValueText(ScanEntry.COMPLETE_LOCAL_NAME)
|
||||
if deviceName:
|
||||
logger.debug(f"COMPLETE_LOCAL_NAME: {deviceName}")
|
||||
if deviceName.startswith(f['namePrefix']):
|
||||
logger.debug(f"match...")
|
||||
return True
|
||||
if 'name' in f or 'manufactureData' in f:
|
||||
logger.error("name/manufactureData filters not implemented")
|
||||
# TODO: implement other filters defined:
|
||||
@@ -318,16 +322,23 @@ class BLESession(Session):
|
||||
BLESession.found_devices.clear()
|
||||
for i in range(self.MAX_SCANNER_IF):
|
||||
scanner = Scanner(iface=i)
|
||||
try:
|
||||
logger.debug(f"start BLE scan: {scan_seconds} seconds")
|
||||
devices = scanner.scan(scan_seconds)
|
||||
for dev in devices:
|
||||
if self.matches(dev, params['filters']):
|
||||
BLESession.found_devices.append(dev)
|
||||
found = True
|
||||
logger.debug(f"BLE device found with iface #{i}");
|
||||
except BTLEManagementError as e:
|
||||
logger.debug(f"BLE iface #{i}: {e}");
|
||||
for j in range(scan_retry):
|
||||
try:
|
||||
logger.debug(f"start BLE scan: {scan_seconds} seconds")
|
||||
devices = scanner.scan(scan_seconds)
|
||||
for dev in devices:
|
||||
if self.matches(dev, params['filters']):
|
||||
BLESession.found_devices.append(dev)
|
||||
found = True
|
||||
logger.debug(f"BLE device found with iface #{i}");
|
||||
if found:
|
||||
break
|
||||
except BTLEDisconnectError as de:
|
||||
logger.debug(f"BLE iface #{i}: {de}");
|
||||
except BTLEManagementError as me:
|
||||
logger.debug(f"BLE iface #{i}: {me}");
|
||||
if found:
|
||||
break
|
||||
else:
|
||||
found = len(BLESession.found_devices) > 0
|
||||
return found
|
||||
@@ -534,11 +545,14 @@ def stack_trace():
|
||||
|
||||
def main():
|
||||
global scan_seconds
|
||||
global scan_retry
|
||||
parser = argparse.ArgumentParser(description='start Scratch-link')
|
||||
parser.add_argument('-d', '--debug', action='store_true',
|
||||
help='print debug messages')
|
||||
parser.add_argument('-s', '--scan_seconds', type=float, default=10.0,
|
||||
help='specifiy duration to scan BLE devices in seconds')
|
||||
parser.add_argument('-r', '--scan_retry', type=int, default=1,
|
||||
help='specifiy retry times to scan BLE devices')
|
||||
args = parser.parse_args()
|
||||
if args.debug:
|
||||
print("Print debug messages")
|
||||
@@ -546,7 +560,9 @@ def main():
|
||||
handler.setLevel(logLevel)
|
||||
logger.setLevel(logLevel)
|
||||
scan_seconds = args.scan_seconds
|
||||
scan_retry = args.scan_retry
|
||||
logger.debug(f"set scan_seconds: {scan_seconds}")
|
||||
logger.debug(f"set scan_retry: {scan_retry}")
|
||||
|
||||
# Prepare certificate of the WSS server
|
||||
gencert.prep_cert()
|
||||
|
@@ -4,7 +4,11 @@ usage() {
|
||||
echo "Usage: ${0} COMMAND"
|
||||
echo -e "COMMAND:"
|
||||
echo -e "\tbuild"
|
||||
echo -e "\tupload FILES"
|
||||
echo -e "\tupload-testpypi FILES"
|
||||
echo -e "examples:"
|
||||
echo -e "\t${0} build"
|
||||
echo -e "\t${0} upload dist/*0.2.6*"
|
||||
exit 1
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user