wasptool 0.1.0a1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- wasptool-0.1.0a1/PKG-INFO +73 -0
- wasptool-0.1.0a1/README.md +61 -0
- wasptool-0.1.0a1/pyproject.toml +26 -0
- wasptool-0.1.0a1/pyproject.toml.orig +29 -0
- wasptool-0.1.0a1/src/wasptool/__init__.py +10 -0
- wasptool-0.1.0a1/src/wasptool/__main__.py +4 -0
- wasptool-0.1.0a1/src/wasptool/ble.py +206 -0
- wasptool-0.1.0a1/src/wasptool/cli.py +476 -0
- wasptool-0.1.0a1/src/wasptool/console.py +76 -0
- wasptool-0.1.0a1/src/wasptool/dfu.py +200 -0
- wasptool-0.1.0a1/src/wasptool/releases.py +82 -0
- wasptool-0.1.0a1/src/wasptool/repl.py +130 -0
- wasptool-0.1.0a1/src/wasptool/watch.py +243 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: wasptool
|
|
3
|
+
Version: 0.1.0a1
|
|
4
|
+
Summary: Talk to a wasp-os smart watch over Bluetooth: update, set the time, notify
|
|
5
|
+
License-Expression: LGPL-3.0-or-later
|
|
6
|
+
Requires-Dist: bleak>=3.0
|
|
7
|
+
Requires-Python: >=3.11
|
|
8
|
+
Project-URL: Homepage, https://github.com/wasp-os/wasp-os
|
|
9
|
+
Project-URL: Source, https://github.com/wasp-os/wasp-os/tree/master/tools/wasptool
|
|
10
|
+
Project-URL: Issues, https://github.com/wasp-os/wasp-os/issues
|
|
11
|
+
Description-Content-Type: text/markdown
|
|
12
|
+
|
|
13
|
+
# wasptool
|
|
14
|
+
|
|
15
|
+
Talk to a [wasp-os](https://github.com/wasp-os/wasp-os) smart watch over
|
|
16
|
+
Bluetooth: update its firmware, set its clock, send it notifications, copy
|
|
17
|
+
files to and from it, or open a MicroPython prompt on it. It runs on
|
|
18
|
+
Linux, macOS and Windows, and needs nothing but Python and
|
|
19
|
+
[bleak](https://github.com/hbldh/bleak).
|
|
20
|
+
|
|
21
|
+
## Install
|
|
22
|
+
|
|
23
|
+
From a wasp-os checkout:
|
|
24
|
+
|
|
25
|
+
```sh
|
|
26
|
+
uv tool install ./tools/wasptool
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Use
|
|
30
|
+
|
|
31
|
+
```sh
|
|
32
|
+
wasptool scan # the watches in range
|
|
33
|
+
wasptool info # version, battery, memory, ...
|
|
34
|
+
wasptool info --json # the same, for scripts
|
|
35
|
+
wasptool rtc # how far off is the watch's clock?
|
|
36
|
+
wasptool rtc set # set it to this computer's time
|
|
37
|
+
wasptool notify "Build" "Passed" # show a notification
|
|
38
|
+
wasptool alarm # the alarms
|
|
39
|
+
wasptool alarm set 7:30 --days weekdays # add one, or change the one at 7:30
|
|
40
|
+
wasptool alarm unset 7:30 # remove it ("all" removes them all)
|
|
41
|
+
wasptool face # the watch faces, * marks the one in use
|
|
42
|
+
wasptool face PixelClock # use another one
|
|
43
|
+
wasptool update # update to the latest build, after asking
|
|
44
|
+
wasptool update --check # only show what's new
|
|
45
|
+
wasptool ota micropython.zip # update the firmware from a file
|
|
46
|
+
wasptool push apps/snake.py # copy a file to the watch
|
|
47
|
+
wasptool pull alarms.txt # and back
|
|
48
|
+
wasptool eval "print(wasp.watch.battery.voltage_mv())"
|
|
49
|
+
wasptool exec script.py
|
|
50
|
+
wasptool console # a MicroPython prompt (Ctrl-X exits)
|
|
51
|
+
wasptool reset [--bootloader]
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
With more than one watch around, pick one with `--device NAME` or
|
|
55
|
+
`--device ADDRESS`. Connecting is retried (`--retries`, 3 by default):
|
|
56
|
+
some radios, especially ones shared with Wi-Fi, fail the first attempt
|
|
57
|
+
now and again.
|
|
58
|
+
|
|
59
|
+
`update` gets the builds published on https://wasp-os.x14.nl/ (see
|
|
60
|
+
`tools/publish.py`), checks the firmware's sha256 against `releases.json`
|
|
61
|
+
and installs it like `ota`. Use `--url` for another site.
|
|
62
|
+
|
|
63
|
+
Commands exit with status 0 on success and 1 on failure; `eval` and
|
|
64
|
+
`exec` also exit with 1 when the code raises, with the traceback on
|
|
65
|
+
stderr.
|
|
66
|
+
|
|
67
|
+
## How it works
|
|
68
|
+
|
|
69
|
+
The watch's console is the Nordic UART Service. wasptool runs Python on it
|
|
70
|
+
through MicroPython's raw REPL, using raw-paste mode for flow control
|
|
71
|
+
where the firmware has it (MicroPython 1.14 and later), and paced writes
|
|
72
|
+
where it does not. Firmware updates use Nordic's legacy DFU protocol,
|
|
73
|
+
which the PineTime bootloader speaks.
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# wasptool
|
|
2
|
+
|
|
3
|
+
Talk to a [wasp-os](https://github.com/wasp-os/wasp-os) smart watch over
|
|
4
|
+
Bluetooth: update its firmware, set its clock, send it notifications, copy
|
|
5
|
+
files to and from it, or open a MicroPython prompt on it. It runs on
|
|
6
|
+
Linux, macOS and Windows, and needs nothing but Python and
|
|
7
|
+
[bleak](https://github.com/hbldh/bleak).
|
|
8
|
+
|
|
9
|
+
## Install
|
|
10
|
+
|
|
11
|
+
From a wasp-os checkout:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
uv tool install ./tools/wasptool
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Use
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
wasptool scan # the watches in range
|
|
21
|
+
wasptool info # version, battery, memory, ...
|
|
22
|
+
wasptool info --json # the same, for scripts
|
|
23
|
+
wasptool rtc # how far off is the watch's clock?
|
|
24
|
+
wasptool rtc set # set it to this computer's time
|
|
25
|
+
wasptool notify "Build" "Passed" # show a notification
|
|
26
|
+
wasptool alarm # the alarms
|
|
27
|
+
wasptool alarm set 7:30 --days weekdays # add one, or change the one at 7:30
|
|
28
|
+
wasptool alarm unset 7:30 # remove it ("all" removes them all)
|
|
29
|
+
wasptool face # the watch faces, * marks the one in use
|
|
30
|
+
wasptool face PixelClock # use another one
|
|
31
|
+
wasptool update # update to the latest build, after asking
|
|
32
|
+
wasptool update --check # only show what's new
|
|
33
|
+
wasptool ota micropython.zip # update the firmware from a file
|
|
34
|
+
wasptool push apps/snake.py # copy a file to the watch
|
|
35
|
+
wasptool pull alarms.txt # and back
|
|
36
|
+
wasptool eval "print(wasp.watch.battery.voltage_mv())"
|
|
37
|
+
wasptool exec script.py
|
|
38
|
+
wasptool console # a MicroPython prompt (Ctrl-X exits)
|
|
39
|
+
wasptool reset [--bootloader]
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
With more than one watch around, pick one with `--device NAME` or
|
|
43
|
+
`--device ADDRESS`. Connecting is retried (`--retries`, 3 by default):
|
|
44
|
+
some radios, especially ones shared with Wi-Fi, fail the first attempt
|
|
45
|
+
now and again.
|
|
46
|
+
|
|
47
|
+
`update` gets the builds published on https://wasp-os.x14.nl/ (see
|
|
48
|
+
`tools/publish.py`), checks the firmware's sha256 against `releases.json`
|
|
49
|
+
and installs it like `ota`. Use `--url` for another site.
|
|
50
|
+
|
|
51
|
+
Commands exit with status 0 on success and 1 on failure; `eval` and
|
|
52
|
+
`exec` also exit with 1 when the code raises, with the traceback on
|
|
53
|
+
stderr.
|
|
54
|
+
|
|
55
|
+
## How it works
|
|
56
|
+
|
|
57
|
+
The watch's console is the Nordic UART Service. wasptool runs Python on it
|
|
58
|
+
through MicroPython's raw REPL, using raw-paste mode for flow control
|
|
59
|
+
where the firmware has it (MicroPython 1.14 and later), and paced writes
|
|
60
|
+
where it does not. Firmware updates use Nordic's legacy DFU protocol,
|
|
61
|
+
which the PineTime bootloader speaks.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "wasptool"
|
|
3
|
+
version = "0.1.0a1"
|
|
4
|
+
description = "Talk to a wasp-os smart watch over Bluetooth: update, set the time, notify"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "LGPL-3.0-or-later"
|
|
7
|
+
requires-python = ">=3.11"
|
|
8
|
+
dependencies = ["bleak>=3.0"]
|
|
9
|
+
|
|
10
|
+
[project.urls]
|
|
11
|
+
Homepage = "https://github.com/wasp-os/wasp-os"
|
|
12
|
+
Source = "https://github.com/wasp-os/wasp-os/tree/master/tools/wasptool"
|
|
13
|
+
Issues = "https://github.com/wasp-os/wasp-os/issues"
|
|
14
|
+
|
|
15
|
+
[project.scripts]
|
|
16
|
+
wasptool = "wasptool.cli:main"
|
|
17
|
+
|
|
18
|
+
[dependency-groups]
|
|
19
|
+
dev = ["pytest"]
|
|
20
|
+
|
|
21
|
+
[build-system]
|
|
22
|
+
requires = ["uv_build>=0.9,<0.14"]
|
|
23
|
+
build-backend = "uv_build"
|
|
24
|
+
|
|
25
|
+
[tool.pytest.ini_options]
|
|
26
|
+
pythonpath = ["tests"]
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "wasptool"
|
|
3
|
+
version = "0.1.0a1"
|
|
4
|
+
description = "Talk to a wasp-os smart watch over Bluetooth: update, set the time, notify"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
license = "LGPL-3.0-or-later"
|
|
7
|
+
requires-python = ">=3.11"
|
|
8
|
+
dependencies = [
|
|
9
|
+
"bleak>=3.0",
|
|
10
|
+
]
|
|
11
|
+
|
|
12
|
+
[project.urls]
|
|
13
|
+
Homepage = "https://github.com/wasp-os/wasp-os"
|
|
14
|
+
Source = "https://github.com/wasp-os/wasp-os/tree/master/tools/wasptool"
|
|
15
|
+
Issues = "https://github.com/wasp-os/wasp-os/issues"
|
|
16
|
+
|
|
17
|
+
[project.scripts]
|
|
18
|
+
wasptool = "wasptool.cli:main"
|
|
19
|
+
|
|
20
|
+
[dependency-groups]
|
|
21
|
+
dev = [
|
|
22
|
+
"pytest",
|
|
23
|
+
]
|
|
24
|
+
|
|
25
|
+
[build-system]
|
|
26
|
+
requires = ["uv_build>=0.9,<0.14"]
|
|
27
|
+
build-backend = "uv_build"
|
|
28
|
+
[tool.pytest.ini_options]
|
|
29
|
+
pythonpath = ["tests"]
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
"""Talk to a wasp-os smart watch over Bluetooth."""
|
|
3
|
+
|
|
4
|
+
# The one version is the one in pyproject.toml; this is what is installed.
|
|
5
|
+
from importlib.metadata import PackageNotFoundError, version
|
|
6
|
+
|
|
7
|
+
try:
|
|
8
|
+
__version__ = version('wasptool')
|
|
9
|
+
except PackageNotFoundError: # run from a checkout without installing
|
|
10
|
+
__version__ = 'unknown'
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
# SPDX-License-Identifier: LGPL-3.0-or-later
|
|
2
|
+
"""Find a wasp-os watch and talk to its console over Bluetooth.
|
|
3
|
+
|
|
4
|
+
The console is the Nordic UART Service (NUS): the host writes to one
|
|
5
|
+
characteristic and the watch notifies on another. Both the old wasp-os
|
|
6
|
+
firmware and upstream MicroPython send and accept at most 20 bytes at a
|
|
7
|
+
time, and the watch keeps only 128 received bytes, so anything bigger
|
|
8
|
+
than a line needs flow control on top (see :mod:`wasptool.repl`).
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
import asyncio
|
|
12
|
+
import logging
|
|
13
|
+
|
|
14
|
+
from bleak import BleakClient, BleakScanner
|
|
15
|
+
from bleak.exc import BleakError
|
|
16
|
+
|
|
17
|
+
NUS_SERVICE = '6e400001-b5a3-f393-e0a9-e50e24dcca9e'
|
|
18
|
+
NUS_RX = '6e400002-b5a3-f393-e0a9-e50e24dcca9e' # host to watch
|
|
19
|
+
NUS_TX = '6e400003-b5a3-f393-e0a9-e50e24dcca9e' # watch to host
|
|
20
|
+
CHUNK = 20
|
|
21
|
+
# The name upstream MicroPython gives its console. BlueZ does not always
|
|
22
|
+
# pass on the service UUIDs it advertises (it can drop them after a
|
|
23
|
+
# connection), so the name is the fallback.
|
|
24
|
+
NAMES = ('mpus',)
|
|
25
|
+
|
|
26
|
+
log = logging.getLogger(__name__)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class NotFound(Exception):
|
|
30
|
+
pass
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class Stream:
|
|
34
|
+
"""Bytes from the watch, buffered until a reader asks for them."""
|
|
35
|
+
|
|
36
|
+
def __init__(self):
|
|
37
|
+
self._buf = bytearray()
|
|
38
|
+
self._more = asyncio.Event()
|
|
39
|
+
self.echo = None
|
|
40
|
+
self.connected = True
|
|
41
|
+
|
|
42
|
+
def feed(self, data):
|
|
43
|
+
if self.echo:
|
|
44
|
+
self.echo(bytes(data))
|
|
45
|
+
self._buf += data
|
|
46
|
+
self._more.set()
|
|
47
|
+
|
|
48
|
+
async def _wait(self, deadline):
|
|
49
|
+
if not self.connected:
|
|
50
|
+
raise ConnectionError('The watch disconnected')
|
|
51
|
+
timeout = deadline - asyncio.get_running_loop().time()
|
|
52
|
+
if timeout <= 0:
|
|
53
|
+
raise TimeoutError()
|
|
54
|
+
self._more.clear()
|
|
55
|
+
await asyncio.wait_for(self._more.wait(), timeout)
|
|
56
|
+
|
|
57
|
+
async def read_until(self, marker, timeout=10.0):
|
|
58
|
+
"""Return everything up to marker, and drop the marker itself."""
|
|
59
|
+
deadline = asyncio.get_running_loop().time() + timeout
|
|
60
|
+
while (i := self._buf.find(marker)) < 0:
|
|
61
|
+
try:
|
|
62
|
+
await self._wait(deadline)
|
|
63
|
+
except TimeoutError:
|
|
64
|
+
raise TimeoutError(f'Timed out waiting for {marker!r}, '
|
|
65
|
+
f'got {bytes(self._buf[-80:])!r}') from None
|
|
66
|
+
data = bytes(self._buf[:i])
|
|
67
|
+
del self._buf[:i + len(marker)]
|
|
68
|
+
return data
|
|
69
|
+
|
|
70
|
+
async def read(self, n, timeout=10.0):
|
|
71
|
+
"""Return exactly n bytes."""
|
|
72
|
+
deadline = asyncio.get_running_loop().time() + timeout
|
|
73
|
+
while len(self._buf) < n:
|
|
74
|
+
await self._wait(deadline)
|
|
75
|
+
data = bytes(self._buf[:n])
|
|
76
|
+
del self._buf[:n]
|
|
77
|
+
return data
|
|
78
|
+
|
|
79
|
+
def pending(self):
|
|
80
|
+
return len(self._buf)
|
|
81
|
+
|
|
82
|
+
async def settle(self, quiet=0.3, timeout=3.0):
|
|
83
|
+
"""Wait until the watch has stopped sending.
|
|
84
|
+
|
|
85
|
+
Hanging up while the watch is still sending output hangs the
|
|
86
|
+
watch: upstream MicroPython's nRF port then waits forever for
|
|
87
|
+
notifications to complete, and the watchdog resets it into the
|
|
88
|
+
bootloader. So let it finish first.
|
|
89
|
+
"""
|
|
90
|
+
loop = asyncio.get_running_loop()
|
|
91
|
+
deadline = loop.time() + timeout
|
|
92
|
+
while self.connected and loop.time() < deadline:
|
|
93
|
+
self._more.clear()
|
|
94
|
+
try:
|
|
95
|
+
await asyncio.wait_for(self._more.wait(), quiet)
|
|
96
|
+
except TimeoutError:
|
|
97
|
+
return
|
|
98
|
+
|
|
99
|
+
def discard(self):
|
|
100
|
+
self._buf.clear()
|
|
101
|
+
|
|
102
|
+
async def write(self, data, response=True):
|
|
103
|
+
raise NotImplementedError
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def _is_address(target):
|
|
107
|
+
# A MAC address (Linux, Windows) or a CoreBluetooth UUID (macOS)
|
|
108
|
+
return target.count(':') == 5 or target.count('-') == 4
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def matches(device, adv, target=None):
|
|
112
|
+
"""True if the advertisement is a wasp-os console we are looking for."""
|
|
113
|
+
if target:
|
|
114
|
+
if _is_address(target):
|
|
115
|
+
return device.address.upper() == target.upper()
|
|
116
|
+
return target in (device.name, adv.local_name)
|
|
117
|
+
return NUS_SERVICE in adv.service_uuids or adv.local_name in NAMES
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
async def find(target=None, timeout=10.0):
|
|
121
|
+
"""Scan for a watch: by name or address, or else any wasp-os watch."""
|
|
122
|
+
device = await BleakScanner.find_device_by_filter(
|
|
123
|
+
lambda d, a: matches(d, a, target), timeout=timeout)
|
|
124
|
+
if device is None:
|
|
125
|
+
what = target or 'a wasp-os watch'
|
|
126
|
+
raise NotFound(f'Cannot find {what}: is it awake, and not '
|
|
127
|
+
'connected to a phone or another computer?')
|
|
128
|
+
return device
|
|
129
|
+
|
|
130
|
+
|
|
131
|
+
async def scan(timeout=5.0):
|
|
132
|
+
"""List the watches in range, as (device, advertisement) pairs."""
|
|
133
|
+
found = await BleakScanner.discover(timeout=timeout, return_adv=True)
|
|
134
|
+
return [(d, a) for d, a in found.values() if matches(d, a)]
|
|
135
|
+
|
|
136
|
+
|
|
137
|
+
class Console(Stream):
|
|
138
|
+
"""A connection to the console of a watch."""
|
|
139
|
+
|
|
140
|
+
def __init__(self, device):
|
|
141
|
+
super().__init__()
|
|
142
|
+
self.device = device
|
|
143
|
+
self._client = BleakClient(device, disconnected_callback=self._lost)
|
|
144
|
+
self.connected = False
|
|
145
|
+
self.on_lost = None
|
|
146
|
+
|
|
147
|
+
def _lost(self, client):
|
|
148
|
+
self.connected = False
|
|
149
|
+
self._more.set()
|
|
150
|
+
if self.on_lost:
|
|
151
|
+
self.on_lost()
|
|
152
|
+
|
|
153
|
+
@property
|
|
154
|
+
def address(self):
|
|
155
|
+
return self.device.address
|
|
156
|
+
|
|
157
|
+
@property
|
|
158
|
+
def name(self):
|
|
159
|
+
return self.device.name
|
|
160
|
+
|
|
161
|
+
async def open(self, timeout=10.0):
|
|
162
|
+
await self._client.connect(timeout=timeout)
|
|
163
|
+
self.connected = True
|
|
164
|
+
await self._client.start_notify(NUS_TX, lambda c, d: self.feed(d))
|
|
165
|
+
|
|
166
|
+
async def close(self):
|
|
167
|
+
if self._client.is_connected:
|
|
168
|
+
await self.settle()
|
|
169
|
+
await self._client.disconnect()
|
|
170
|
+
self.connected = False
|
|
171
|
+
|
|
172
|
+
async def write(self, data, response=True):
|
|
173
|
+
"""Write in 20 byte pieces.
|
|
174
|
+
|
|
175
|
+
With response, each piece waits until the watch has it, which paces
|
|
176
|
+
the writes. Without, they go out as fast as the link allows: only use
|
|
177
|
+
that when something else stops the watch's buffer overflowing.
|
|
178
|
+
"""
|
|
179
|
+
if not self.connected:
|
|
180
|
+
raise ConnectionError('The watch disconnected')
|
|
181
|
+
for i in range(0, len(data), CHUNK):
|
|
182
|
+
await self._client.write_gatt_char(NUS_RX, data[i:i + CHUNK],
|
|
183
|
+
response=response)
|
|
184
|
+
|
|
185
|
+
|
|
186
|
+
async def connect(target=None, retries=3, timeout=10.0):
|
|
187
|
+
"""Find a watch and connect to its console, retrying on failure.
|
|
188
|
+
|
|
189
|
+
The first connection attempt fails now and again, more so on radios
|
|
190
|
+
that share their antenna with Wi-Fi. Each attempt scans again, since
|
|
191
|
+
BlueZ forgets devices it has not seen for a while.
|
|
192
|
+
"""
|
|
193
|
+
for attempt in range(1, retries + 1):
|
|
194
|
+
device = await find(target, timeout)
|
|
195
|
+
console = Console(device)
|
|
196
|
+
try:
|
|
197
|
+
log.debug('Connecting to %s (%s)', device.name, device.address)
|
|
198
|
+
await console.open(timeout)
|
|
199
|
+
return console
|
|
200
|
+
except (BleakError, TimeoutError, OSError) as e:
|
|
201
|
+
await console.close()
|
|
202
|
+
if attempt == retries:
|
|
203
|
+
raise ConnectionError(f'Cannot connect to {device.address} '
|
|
204
|
+
f'after {retries} attempts: {e}') from e
|
|
205
|
+
log.warning('Connecting failed (%s), retrying', e or type(e).__name__)
|
|
206
|
+
await asyncio.sleep(1)
|