lager-cli 0.63.0__tar.gz → 0.65.0__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.
- {lager_cli-0.63.0/lager_cli.egg-info → lager_cli-0.65.0}/PKG-INFO +4 -1
- {lager_cli-0.63.0 → lager_cli-0.65.0}/README.md +3 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/__init__.py +1 -1
- {lager_cli-0.63.0 → lager_cli-0.65.0}/box_storage.py +30 -1
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/diagnose.py +1 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/nets.py +3 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/ssh.py +30 -9
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/__init__.py +3 -0
- lager_cli-0.65.0/commands/communication/ble_sniffer.py +423 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/scope.py +2 -2
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/defaults.py +6 -2
- {lager_cli-0.63.0 → lager_cli-0.65.0}/gateway_auth.py +11 -3
- {lager_cli-0.63.0 → lager_cli-0.65.0/lager_cli.egg-info}/PKG-INFO +4 -1
- {lager_cli-0.63.0 → lager_cli-0.65.0}/lager_cli.egg-info/SOURCES.txt +2 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/main.py +4 -3
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/ui/display.py +1 -1
- {lager_cli-0.63.0 → lager_cli-0.65.0}/LICENSE +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/MANIFEST.in +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/NOTICE +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/__main__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/address_utils.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/battery/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/battery/battery_tui.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/battery/websocket_client.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/_device_identity.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/_gateway.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/_host_ops.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/_mount_prep.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/_net_preflight.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/_pip_validation.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/_shim_verbs.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/_ssh.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/bench.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/box_group.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/boxes.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/config.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/drivers.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/dut.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/hello.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/instruments.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/labjack_pins.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/lock.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/net_tui.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/box/ssh_setup.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/ble.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/blufi.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/i2c.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/router.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/spi.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/uart.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/usb.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/websocket_client.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/communication/wifi.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/custom.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/arm.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/debug/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/debug/commands.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/debug/net_cache.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/debug/rtt_websocket_client.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/debug/service_client.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/debug/service_helper.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/debug/tunnel.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/devenv.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/development/python.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/adc.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/dac.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/dmm.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/energy.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/gpi.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/gpo.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/logic.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/thermocouple.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/measurement/watt.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/power/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/power/battery.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/power/battery_model_csv.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/power/eload.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/power/solar.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/power/supply.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/security/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/security/hsm.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/_host_cli.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/binaries.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/exec_.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/install.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/install_wheel.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/lease.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/login.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/logs.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/uninstall.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/update.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/commands/utility/webcam.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/config.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/context/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/context/ci_detection.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/context/constants.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/context/core.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/context/error_handlers.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/context/session.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/custom_drivers.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/group_usage.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/matchers.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/net_group.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/net_helpers.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/net_storage.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/param_types.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/ssh_utils.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/tui_keys.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/tui_ws_bridge.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/utils.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/version_skew.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/core/ws_diagnose.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/scripts/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/scripts/configure_docker_dns.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/scripts/configure_docker_dns.sh +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/scripts/convert_to_sparse_checkout.sh +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/scripts/setup_and_deploy_box.sh +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/scripts/setup_ssh_key.sh +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/security/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/security/etc_lager_perms.sh +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/deployment/security/secure_box_firewall.sh +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/errors.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/exceptions.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/gateway_tunnel.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/impl/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/impl/box_config.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/impl/measurement/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/impl/measurement/scope.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/impl/measurement/scope_stream.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/impl/power/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/impl/power/enable_disable.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/lager_cli.egg-info/dependency_links.txt +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/lager_cli.egg-info/entry_points.txt +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/lager_cli.egg-info/requires.txt +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/lager_cli.egg-info/top_level.txt +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/lease_client.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/pyproject.toml +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/run_record.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/safe_unpickle.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/setup.cfg +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/setup.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/simple_hdlc.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/sort_utils.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/status.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/supply/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/supply/supply_tui.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/supply/websocket_client.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/core/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/core/executor.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/main.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/ui/__init__.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/ui/completer.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/ui/logo.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/ui/repl.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/terminal/ui/themes.py +0 -0
- {lager_cli-0.63.0 → lager_cli-0.65.0}/update_check.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: lager-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.65.0
|
|
4
4
|
Summary: Lager CLI - Box and Docker connectivity
|
|
5
5
|
Home-page: https://github.com/lagerdata/lager
|
|
6
6
|
Author: Lager Data
|
|
@@ -205,6 +205,9 @@ lager ble scan --timeout 5.0
|
|
|
205
205
|
|
|
206
206
|
# Connect to device
|
|
207
207
|
lager ble connect <address>
|
|
208
|
+
|
|
209
|
+
# Capture 30 s of radio traffic to pcap with a BLE sniffer net (nRF52840 Dongle)
|
|
210
|
+
lager ble-sniffer <net> capture --duration 30 --out cap.pcap
|
|
208
211
|
```
|
|
209
212
|
|
|
210
213
|
## Configuration
|
|
@@ -151,6 +151,9 @@ lager ble scan --timeout 5.0
|
|
|
151
151
|
|
|
152
152
|
# Connect to device
|
|
153
153
|
lager ble connect <address>
|
|
154
|
+
|
|
155
|
+
# Capture 30 s of radio traffic to pcap with a BLE sniffer net (nRF52840 Dongle)
|
|
156
|
+
lager ble-sniffer <net> capture --duration 30 --out cap.pcap
|
|
154
157
|
```
|
|
155
158
|
|
|
156
159
|
## Configuration
|
|
@@ -912,6 +912,7 @@ def acquire_box_lock(
|
|
|
912
912
|
wait_seconds=0,
|
|
913
913
|
poll=2.0,
|
|
914
914
|
quiet=False,
|
|
915
|
+
fail_open=False,
|
|
915
916
|
):
|
|
916
917
|
"""Acquire the box lock for ``holder``.
|
|
917
918
|
|
|
@@ -930,6 +931,10 @@ def acquire_box_lock(
|
|
|
930
931
|
|
|
931
932
|
``holder_type`` and ``ttl_seconds`` are forwarded to the server. Older
|
|
932
933
|
box versions that don't understand these fields will just ignore them.
|
|
934
|
+
|
|
935
|
+
Any other reply (no lock API, a server error) exits too, unless
|
|
936
|
+
``fail_open`` is set: then it warns and returns ``("unreachable", None)``,
|
|
937
|
+
so the caller runs without a lock. A collision still exits either way.
|
|
933
938
|
"""
|
|
934
939
|
import time
|
|
935
940
|
import click
|
|
@@ -1053,6 +1058,23 @@ def acquire_box_lock(
|
|
|
1053
1058
|
time.sleep(min(poll, max(0.1, deadline - time.monotonic())))
|
|
1054
1059
|
continue
|
|
1055
1060
|
|
|
1061
|
+
# A box that cannot take a lock at all: an image older than the lock
|
|
1062
|
+
# API (404), or a lock server that errors. With fail_open the caller
|
|
1063
|
+
# carries on unlocked, as the pre-command lock check does; `lager ssh`
|
|
1064
|
+
# uses this because it is how such a box gets repaired.
|
|
1065
|
+
if fail_open:
|
|
1066
|
+
if not quiet:
|
|
1067
|
+
reason = (
|
|
1068
|
+
'runs an old image without lock support'
|
|
1069
|
+
if resp.status_code == 404
|
|
1070
|
+
else f'did not take the lock (HTTP {resp.status_code})'
|
|
1071
|
+
)
|
|
1072
|
+
click.secho(
|
|
1073
|
+
f"Warning: box '{display}' {reason}; continuing without a lock.",
|
|
1074
|
+
fg='yellow', err=True,
|
|
1075
|
+
)
|
|
1076
|
+
return ('unreachable', None)
|
|
1077
|
+
|
|
1056
1078
|
# Any other status: bail out.
|
|
1057
1079
|
if not quiet:
|
|
1058
1080
|
click.secho(
|
|
@@ -1580,9 +1602,15 @@ def auto_lock_around_command(
|
|
|
1580
1602
|
holder_type=None,
|
|
1581
1603
|
heartbeat_interval=None,
|
|
1582
1604
|
release_retry_seconds=0,
|
|
1605
|
+
fail_open=False,
|
|
1583
1606
|
):
|
|
1584
1607
|
"""Context manager: auto-acquire a box lock for the duration of an
|
|
1585
|
-
admin command (`install`, `uninstall`, `update`, `install-wheel`)
|
|
1608
|
+
admin command (`install`, `uninstall`, `update`, `install-wheel`), or
|
|
1609
|
+
of a `lager ssh` session.
|
|
1610
|
+
|
|
1611
|
+
``fail_open`` is passed to :func:`acquire_box_lock`: a box that cannot
|
|
1612
|
+
take a lock (no lock API, a server error) yields state ``"unreachable"``
|
|
1613
|
+
instead of exiting. `lager ssh` sets it; the admin commands do not.
|
|
1586
1614
|
|
|
1587
1615
|
Use as::
|
|
1588
1616
|
|
|
@@ -1652,6 +1680,7 @@ def auto_lock_around_command(
|
|
|
1652
1680
|
holder_type=resolved_type,
|
|
1653
1681
|
ttl_seconds=resolved_ttl,
|
|
1654
1682
|
wait_seconds=resolved_wait,
|
|
1683
|
+
fail_open=fail_open,
|
|
1655
1684
|
)
|
|
1656
1685
|
|
|
1657
1686
|
should_release = (state == 'acquired')
|
|
@@ -41,6 +41,7 @@ NET_ROLE_CHOICES = [
|
|
|
41
41
|
'uart', 'webcam', 'adc', 'dac', 'gpio', 'thermocouple', 'i2c', 'spi',
|
|
42
42
|
'watt-meter', 'energy-analyzer', 'router', 'rotation', 'arm', 'actuate',
|
|
43
43
|
'power-supply-2q', 'wifi', 'analog', 'logic', 'waveform', 'hsm', 'dmm',
|
|
44
|
+
'ble-sniffer',
|
|
44
45
|
]
|
|
45
46
|
|
|
46
47
|
|
|
@@ -522,6 +522,7 @@ INSTRUMENT_NET_MAP: dict[str, list[str]] = {
|
|
|
522
522
|
# these instruments.
|
|
523
523
|
"Joulescope_JS220": ["watt-meter", "energy-analyzer"],
|
|
524
524
|
"Nordic_PPK2": ["watt-meter", "energy-analyzer"],
|
|
525
|
+
"Nordic_nRF_Sniffer": ["ble-sniffer"],
|
|
525
526
|
"Yocto_Watt": ["watt-meter"],
|
|
526
527
|
|
|
527
528
|
# scope
|
|
@@ -1987,9 +1988,11 @@ def assign_cmd(ctx, device, list_, usb_serial, port_path, baud, remove_, as_net,
|
|
|
1987
1988
|
# Name prefixes for roles whose first hyphenated word is not the prefix. A
|
|
1988
1989
|
# `scope` net stands for the whole instrument and would otherwise share the
|
|
1989
1990
|
# `scope` prefix with its `scope-channel` nets; the channels keep `scope<N>`.
|
|
1991
|
+
# A `ble-sniffer` net would otherwise take `ble`, which reads as a BLE device.
|
|
1990
1992
|
_NET_NAME_PREFIX = {
|
|
1991
1993
|
"power-supply": "supply",
|
|
1992
1994
|
"scope": "oscilloscope",
|
|
1995
|
+
"ble-sniffer": "sniffer",
|
|
1993
1996
|
}
|
|
1994
1997
|
|
|
1995
1998
|
|
|
@@ -6,12 +6,13 @@
|
|
|
6
6
|
|
|
7
7
|
SSH into boxes
|
|
8
8
|
"""
|
|
9
|
+
import os
|
|
9
10
|
import click
|
|
10
11
|
from click.exceptions import Exit
|
|
11
12
|
import shlex
|
|
12
13
|
import subprocess
|
|
13
14
|
import platform
|
|
14
|
-
from ...box_storage import resolve_and_validate_box
|
|
15
|
+
from ...box_storage import auto_lock_around_command, resolve_and_validate_box
|
|
15
16
|
from ...context import get_default_box
|
|
16
17
|
from ._ssh import resolve_box_user, widened_identity_args
|
|
17
18
|
|
|
@@ -95,8 +96,20 @@ def ssh(ctx, box, command):
|
|
|
95
96
|
if box is None:
|
|
96
97
|
box = get_default_box(ctx)
|
|
97
98
|
|
|
98
|
-
#
|
|
99
|
-
|
|
99
|
+
# Hold an ephemeral auto-lock for the whole SSH session (interactive or
|
|
100
|
+
# one-shot). While it is held, other users' lager commands that change
|
|
101
|
+
# the box are refused. A direct `ssh user@box` takes no lock.
|
|
102
|
+
# Skip the pre-resolve lock check when we will acquire: acquire_box_lock
|
|
103
|
+
# waits in CI and treats an already-ours user lock as fine. When the
|
|
104
|
+
# LAGER_AUTO_LOCK_DISABLE escape hatch is set, keep the pre-check so a
|
|
105
|
+
# foreign user lock is still refused.
|
|
106
|
+
auto_lock_enabled = not os.getenv('LAGER_AUTO_LOCK_DISABLE')
|
|
107
|
+
resolved_box = resolve_and_validate_box(
|
|
108
|
+
ctx, box, _skip_lock_check=auto_lock_enabled,
|
|
109
|
+
)
|
|
110
|
+
# `is None`, not `or`: an empty --box is falsy and must not fall back
|
|
111
|
+
# (see test_empty_box_everywhere).
|
|
112
|
+
box_label = resolved_box if box is None else box
|
|
100
113
|
|
|
101
114
|
# The stored user for a saved name, else the one saved for this IP (an
|
|
102
115
|
# IP or the default box never matches a name), else 'lagerdata'.
|
|
@@ -119,12 +132,20 @@ def ssh(ctx, box, command):
|
|
|
119
132
|
ssh_cmd.append(' '.join(shlex.quote(arg) for arg in command))
|
|
120
133
|
|
|
121
134
|
try:
|
|
122
|
-
#
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
135
|
+
# Heartbeat keeps a long interactive session alive; release runs on
|
|
136
|
+
# normal exit, Ctrl-C, ctx.exit, and process death (atexit).
|
|
137
|
+
# fail_open: a box with no lock API, or a lock server that errors,
|
|
138
|
+
# still gets a shell -- `lager ssh` is how such a box is repaired.
|
|
139
|
+
with auto_lock_around_command(
|
|
140
|
+
resolved_box, box_label, 'ssh', fail_open=True,
|
|
141
|
+
):
|
|
142
|
+
# Run SSH as a child process (not exec) so Click ctx.call_on_close
|
|
143
|
+
# hooks and the auto-lock release still run.
|
|
144
|
+
try:
|
|
145
|
+
ret = subprocess.call(ssh_cmd)
|
|
146
|
+
except KeyboardInterrupt:
|
|
147
|
+
ret = 130
|
|
148
|
+
ctx.exit(ret if ret is not None else 0)
|
|
128
149
|
except FileNotFoundError:
|
|
129
150
|
click.secho('Error: SSH client not found', fg='red', err=True)
|
|
130
151
|
click.secho(_get_ssh_install_hint(), err=True)
|
|
@@ -7,6 +7,7 @@ Communication Commands Package
|
|
|
7
7
|
This package groups communication-related CLI commands:
|
|
8
8
|
- uart: UART serial port connection and communication
|
|
9
9
|
- ble: Bluetooth Low Energy scanning and device interaction
|
|
10
|
+
- ble_sniffer: Bluetooth LE over-the-air capture to pcap with a sniffer net
|
|
10
11
|
- wifi: WiFi network management and configuration
|
|
11
12
|
- usb: USB hub port control (enable/disable/toggle)
|
|
12
13
|
- spi: SPI (Serial Peripheral Interface) communication
|
|
@@ -18,6 +19,7 @@ connected to the Lager box.
|
|
|
18
19
|
|
|
19
20
|
from .uart import uart
|
|
20
21
|
from .ble import ble
|
|
22
|
+
from .ble_sniffer import ble_sniffer
|
|
21
23
|
from .blufi import blufi
|
|
22
24
|
from .wifi import _wifi
|
|
23
25
|
from .usb import usb
|
|
@@ -28,6 +30,7 @@ from .router import router
|
|
|
28
30
|
__all__ = [
|
|
29
31
|
"uart",
|
|
30
32
|
"ble",
|
|
33
|
+
"ble_sniffer",
|
|
31
34
|
"blufi",
|
|
32
35
|
"_wifi",
|
|
33
36
|
"usb",
|
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
# Copyright 2024-2026 Lager Data
|
|
2
|
+
# SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
"""
|
|
5
|
+
Bluetooth LE sniffer commands.
|
|
6
|
+
|
|
7
|
+
``lager ble-sniffer NET capture`` records the Bluetooth LE radio traffic a
|
|
8
|
+
sniffer net hears for a fixed time and saves it here as a pcap file. It starts
|
|
9
|
+
a capture job on the box ("start"), polls it ("job"), sends "stop" on Ctrl+C,
|
|
10
|
+
then downloads the file from /tmp/lager-output through GET /download-file and
|
|
11
|
+
deletes the box copy unless ``--keep`` is given.
|
|
12
|
+
"""
|
|
13
|
+
from __future__ import annotations
|
|
14
|
+
|
|
15
|
+
import gzip
|
|
16
|
+
import json
|
|
17
|
+
import re
|
|
18
|
+
import time
|
|
19
|
+
from datetime import datetime, timezone
|
|
20
|
+
from pathlib import Path
|
|
21
|
+
from typing import NoReturn
|
|
22
|
+
|
|
23
|
+
import click
|
|
24
|
+
|
|
25
|
+
from ...context import get_default_net
|
|
26
|
+
from ...core.net_group import NetGroup
|
|
27
|
+
from ...core.net_helpers import (
|
|
28
|
+
NET_HTTP_PORT,
|
|
29
|
+
display_nets,
|
|
30
|
+
post_net_command,
|
|
31
|
+
resolve_box,
|
|
32
|
+
resolve_box_locked,
|
|
33
|
+
validate_net_exists,
|
|
34
|
+
)
|
|
35
|
+
|
|
36
|
+
SNIFFER_ROLE = "ble-sniffer"
|
|
37
|
+
|
|
38
|
+
# Same limits as box/lager/protocols/ble_sniffer/ble_sniffer_net.py.
|
|
39
|
+
MIN_DURATION_S = 1.0
|
|
40
|
+
MAX_DURATION_S = 86400.0
|
|
41
|
+
RSSI_MIN_DBM = -255
|
|
42
|
+
RSSI_MAX_DBM = -10
|
|
43
|
+
|
|
44
|
+
_ADDRESS_RE = re.compile(r"^([0-9A-Fa-f]{2}[:-]){5}[0-9A-Fa-f]{2}$")
|
|
45
|
+
# Seconds between job polls while a capture runs, and the shortest wait (near
|
|
46
|
+
# the end, and after Ctrl+C, when a stop takes effect within one 50 ms read).
|
|
47
|
+
_POLL_S = 1.0
|
|
48
|
+
_MIN_POLL_S = 0.2
|
|
49
|
+
# (connect, read) timeout for the pcap download.
|
|
50
|
+
_DOWNLOAD_TIMEOUT_S = (7, 120)
|
|
51
|
+
# HTTP timeout for "start". The box waits up to 10 s for the sniffer to begin
|
|
52
|
+
# recording (_START_TIMEOUT_S in capture_jobs.py), after locking and scanning
|
|
53
|
+
# USB, then returns the job. A client timeout at or under that gives up on a
|
|
54
|
+
# job that still starts, and nothing would stop or download it.
|
|
55
|
+
_START_HTTP_TIMEOUT_S = 30.0
|
|
56
|
+
# After Ctrl+C during "start": how many times, a second apart, to send "stop"
|
|
57
|
+
# while the box may still be registering the capture.
|
|
58
|
+
_STOP_AFTER_START_TRIES = 3
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _require_netname(ctx: click.Context) -> str | None:
|
|
62
|
+
netname = getattr(ctx.obj, "ble_sniffer_netname", None)
|
|
63
|
+
if netname is None:
|
|
64
|
+
netname = get_default_net(ctx, "ble_sniffer")
|
|
65
|
+
return netname
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _resolve_net(ctx: click.Context, box: str | None, lock: bool) -> tuple[str, str] | None:
|
|
69
|
+
"""Box address and net name for a subcommand, or None after listing nets."""
|
|
70
|
+
if box is None:
|
|
71
|
+
box = getattr(ctx.obj, "ble_sniffer_box", None)
|
|
72
|
+
box_ip = resolve_box_locked(ctx, box, "ble-sniffer") if lock else resolve_box(
|
|
73
|
+
ctx, box, read_only=True)
|
|
74
|
+
netname = _require_netname(ctx)
|
|
75
|
+
if netname is None:
|
|
76
|
+
display_nets(ctx, box_ip, None, SNIFFER_ROLE, "BLE sniffer")
|
|
77
|
+
return None
|
|
78
|
+
netname = netname.strip()
|
|
79
|
+
if validate_net_exists(ctx, box_ip, netname, SNIFFER_ROLE) is None:
|
|
80
|
+
return None
|
|
81
|
+
return box_ip, netname
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _download(box_ip: str, box_path: str) -> bytes:
|
|
85
|
+
"""Fetch a capture file from the box.
|
|
86
|
+
|
|
87
|
+
Raises:
|
|
88
|
+
click.ClickException: The box did not return the file.
|
|
89
|
+
"""
|
|
90
|
+
import requests
|
|
91
|
+
|
|
92
|
+
from ...box_storage import _check_gateway
|
|
93
|
+
from ...gateway_auth import auth_headers_for_box
|
|
94
|
+
|
|
95
|
+
url = f"http://{box_ip}:{NET_HTTP_PORT}/download-file"
|
|
96
|
+
try:
|
|
97
|
+
resp = requests.get(url, params={"filename": box_path},
|
|
98
|
+
headers=auth_headers_for_box(box_ip),
|
|
99
|
+
timeout=_DOWNLOAD_TIMEOUT_S)
|
|
100
|
+
resp = _check_gateway(resp, box_ip)
|
|
101
|
+
except requests.RequestException as e:
|
|
102
|
+
raise click.ClickException(f"Download of {box_path} failed: {e}") from e
|
|
103
|
+
if resp.status_code != 200:
|
|
104
|
+
try:
|
|
105
|
+
detail = resp.json().get("error") or resp.text
|
|
106
|
+
except ValueError:
|
|
107
|
+
detail = resp.text
|
|
108
|
+
raise click.ClickException(
|
|
109
|
+
f"Download of {box_path} failed: HTTP {resp.status_code} {detail}".strip())
|
|
110
|
+
content = resp.content
|
|
111
|
+
# A box behind the gateway can answer gzipped; a pcap never starts 1f 8b.
|
|
112
|
+
if content[:2] == b"\x1f\x8b":
|
|
113
|
+
content = gzip.decompress(content)
|
|
114
|
+
return content
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
@click.group(name="ble-sniffer", cls=NetGroup, invoke_without_command=True,
|
|
118
|
+
help="Capture Bluetooth LE radio traffic to pcap with a sniffer net")
|
|
119
|
+
@click.option("--box", required=False, help="Lager Box name or IP")
|
|
120
|
+
@click.argument("netname", required=False, metavar="[NET_NAME]")
|
|
121
|
+
@click.pass_context
|
|
122
|
+
def ble_sniffer(ctx, box, netname):
|
|
123
|
+
"""BLE sniffer group. Usage: lager ble-sniffer [NET_NAME] [COMMAND] --box [BOX_NAME]"""
|
|
124
|
+
if netname is None:
|
|
125
|
+
netname = get_default_net(ctx, "ble_sniffer")
|
|
126
|
+
if netname is not None:
|
|
127
|
+
ctx.obj.ble_sniffer_netname = netname
|
|
128
|
+
ctx.obj.ble_sniffer_box = box
|
|
129
|
+
|
|
130
|
+
if ctx.invoked_subcommand is None:
|
|
131
|
+
box_ip = resolve_box(ctx, box, read_only=True)
|
|
132
|
+
display_nets(ctx, box_ip, None, SNIFFER_ROLE, "BLE sniffer")
|
|
133
|
+
|
|
134
|
+
|
|
135
|
+
ble_sniffer.net_examples = [
|
|
136
|
+
"lager ble-sniffer sniffer1 capture --duration 10 --box <BOX>",
|
|
137
|
+
"lager ble-sniffer sniffer1 capture -d 30 --follow AA:BB:CC:DD:EE:FF --out dut.pcap --box <BOX>",
|
|
138
|
+
"lager ble-sniffer sniffer1 capture --rssi-cut-off -70 --box <BOX>",
|
|
139
|
+
"lager ble-sniffer sniffer1 status --box <BOX>",
|
|
140
|
+
"lager ble-sniffer sniffer1 stop --box <BOX>",
|
|
141
|
+
"lager ble-sniffer --box <BOX> (list BLE sniffer nets)",
|
|
142
|
+
]
|
|
143
|
+
|
|
144
|
+
|
|
145
|
+
def _check_options(duration, follow, address_type, follow_by_name, only_advertising, coded,
|
|
146
|
+
rssi_cut_off):
|
|
147
|
+
# The same rules as CaptureOptions.validate() on the box, so a bad
|
|
148
|
+
# combination fails here before the box is locked.
|
|
149
|
+
if not MIN_DURATION_S <= duration <= MAX_DURATION_S:
|
|
150
|
+
raise click.BadParameter(
|
|
151
|
+
f"must be between {MIN_DURATION_S:g} and {MAX_DURATION_S:g} seconds",
|
|
152
|
+
param_hint="--duration")
|
|
153
|
+
if follow is not None and not _ADDRESS_RE.match(follow):
|
|
154
|
+
raise click.BadParameter(f"'{follow}' is not an address like AA:BB:CC:DD:EE:FF",
|
|
155
|
+
param_hint="--follow")
|
|
156
|
+
if follow is not None and follow_by_name is not None:
|
|
157
|
+
raise click.UsageError("--follow and --follow-by-name cannot be combined")
|
|
158
|
+
if address_type is not None and follow is None:
|
|
159
|
+
raise click.UsageError("--public and --random apply only to --follow")
|
|
160
|
+
if only_advertising and follow is None and follow_by_name is None:
|
|
161
|
+
raise click.UsageError("--only-advertising needs --follow or --follow-by-name")
|
|
162
|
+
if coded and follow_by_name is not None:
|
|
163
|
+
raise click.UsageError("--follow-by-name cannot be combined with --coded: names are "
|
|
164
|
+
"read from legacy advertising, which is not sent on the LE "
|
|
165
|
+
"Coded PHY")
|
|
166
|
+
if rssi_cut_off is not None and not RSSI_MIN_DBM <= rssi_cut_off <= RSSI_MAX_DBM:
|
|
167
|
+
raise click.BadParameter(f"must be between {RSSI_MIN_DBM} and {RSSI_MAX_DBM} dBm",
|
|
168
|
+
param_hint="--rssi-cut-off")
|
|
169
|
+
|
|
170
|
+
|
|
171
|
+
def _give_up(netname: str) -> NoReturn:
|
|
172
|
+
click.secho(f"Not downloaded. 'lager ble-sniffer {netname} status' shows "
|
|
173
|
+
"the file on the box, which is kept for a day.", fg="yellow", err=True)
|
|
174
|
+
raise click.exceptions.Exit(130)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def _stop_after_interrupted_start(ctx, box_ip: str, netname: str) -> NoReturn:
|
|
178
|
+
"""Ctrl+C while "start" was in flight: stop whatever capture it began, then exit 130.
|
|
179
|
+
|
|
180
|
+
The box keeps handling a request the client gave up on, so the capture
|
|
181
|
+
can start after the Ctrl+C. Without a stop it would hold the sniffer for
|
|
182
|
+
its whole duration with no client to end it or download the file.
|
|
183
|
+
"""
|
|
184
|
+
click.secho("Stopping the capture on the Lager Box...", fg="yellow", err=True)
|
|
185
|
+
stopped = False
|
|
186
|
+
try:
|
|
187
|
+
for attempt in range(_STOP_AFTER_START_TRIES):
|
|
188
|
+
if attempt:
|
|
189
|
+
time.sleep(1.0)
|
|
190
|
+
try:
|
|
191
|
+
value = post_net_command(ctx, box_ip, netname, "stop", role=SNIFFER_ROLE,
|
|
192
|
+
quiet=True).get("value") or {}
|
|
193
|
+
except SystemExit:
|
|
194
|
+
continue
|
|
195
|
+
if value.get("stopped"):
|
|
196
|
+
stopped = True
|
|
197
|
+
break
|
|
198
|
+
except KeyboardInterrupt:
|
|
199
|
+
pass
|
|
200
|
+
if not stopped:
|
|
201
|
+
click.secho(f"If a capture started, 'lager ble-sniffer {netname} stop' ends it.",
|
|
202
|
+
fg="yellow", err=True)
|
|
203
|
+
raise click.exceptions.Exit(130)
|
|
204
|
+
|
|
205
|
+
|
|
206
|
+
def _poll(ctx, box_ip: str, netname: str, job_id: str, duration: float) -> tuple[dict, bool]:
|
|
207
|
+
"""Poll a capture job until it ends; ``(job, interrupted)``.
|
|
208
|
+
|
|
209
|
+
The first Ctrl+C asks the box to stop the capture and keeps polling, so
|
|
210
|
+
what was recorded is still downloaded; a second one exits with status
|
|
211
|
+
130 and leaves the file on the box.
|
|
212
|
+
"""
|
|
213
|
+
interrupted = False
|
|
214
|
+
started = time.monotonic()
|
|
215
|
+
while True:
|
|
216
|
+
try:
|
|
217
|
+
left = duration - (time.monotonic() - started)
|
|
218
|
+
time.sleep(min(_POLL_S, max(left, _MIN_POLL_S)) if not interrupted else _MIN_POLL_S)
|
|
219
|
+
job = post_net_command(ctx, box_ip, netname, "job", role=SNIFFER_ROLE, quiet=True,
|
|
220
|
+
job=job_id).get("value") or {}
|
|
221
|
+
except KeyboardInterrupt:
|
|
222
|
+
if interrupted:
|
|
223
|
+
_give_up(netname)
|
|
224
|
+
interrupted = True
|
|
225
|
+
click.secho("Stopping the capture on the Lager Box...", fg="yellow", err=True)
|
|
226
|
+
try:
|
|
227
|
+
post_net_command(ctx, box_ip, netname, "stop", role=SNIFFER_ROLE, quiet=True)
|
|
228
|
+
except KeyboardInterrupt:
|
|
229
|
+
_give_up(netname)
|
|
230
|
+
continue
|
|
231
|
+
if job.get("state") in ("done", "failed"):
|
|
232
|
+
return job, interrupted
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
@ble_sniffer.command(name="capture", help="Capture Bluetooth LE traffic for a fixed time to a pcap file")
|
|
236
|
+
@click.option("--box", required=False, help="Lager Box name or IP")
|
|
237
|
+
@click.option("--duration", "-d", type=float, default=10.0, show_default=True,
|
|
238
|
+
help=f"Capture time in seconds ({MIN_DURATION_S:g}-{MAX_DURATION_S:g})")
|
|
239
|
+
@click.option("--out", "-o", "out_path", type=click.Path(dir_okay=False, writable=True),
|
|
240
|
+
default=None, help="Local pcap file to write (default: <NET>-<UTC time>.pcap)")
|
|
241
|
+
@click.option("--follow", default=None, metavar="ADDRESS",
|
|
242
|
+
help="Follow the device with this address (AA:BB:CC:DD:EE:FF) and its connections")
|
|
243
|
+
@click.option("--public", "address_type", flag_value="public", default=None,
|
|
244
|
+
help="The --follow address is public (default: taken from its advertising)")
|
|
245
|
+
@click.option("--random", "address_type", flag_value="random",
|
|
246
|
+
help="The --follow address is random (default: taken from its advertising)")
|
|
247
|
+
@click.option("--follow-by-name", default=None, metavar="NAME",
|
|
248
|
+
help="Follow the first device whose legacy advertising carries this exact name")
|
|
249
|
+
@click.option("--only-advertising", is_flag=True, default=False,
|
|
250
|
+
help="With --follow or --follow-by-name, record the device's advertising only")
|
|
251
|
+
@click.option("--coded", is_flag=True, default=False,
|
|
252
|
+
help="Scan and follow on the LE Coded PHY")
|
|
253
|
+
@click.option("--rssi-cut-off", type=int, default=None, metavar="DBM",
|
|
254
|
+
help=f"Drop advertising packets weaker than this RSSI "
|
|
255
|
+
f"({RSSI_MIN_DBM} to {RSSI_MAX_DBM} dBm)")
|
|
256
|
+
@click.option("--keep", is_flag=True, default=False,
|
|
257
|
+
help="Keep the capture file on the Lager Box (for a day) after the download")
|
|
258
|
+
@click.option("--json", "as_json", is_flag=True, default=False,
|
|
259
|
+
help="Emit a machine-readable JSON object instead of formatted text")
|
|
260
|
+
@click.pass_context
|
|
261
|
+
def capture(ctx, box, duration, out_path, follow, address_type, follow_by_name,
|
|
262
|
+
only_advertising, coded, rssi_cut_off, keep, as_json):
|
|
263
|
+
_check_options(duration, follow, address_type, follow_by_name, only_advertising, coded,
|
|
264
|
+
rssi_cut_off)
|
|
265
|
+
resolved = _resolve_net(ctx, box, lock=True)
|
|
266
|
+
if resolved is None:
|
|
267
|
+
return
|
|
268
|
+
box_ip, netname = resolved
|
|
269
|
+
|
|
270
|
+
if out_path is None:
|
|
271
|
+
stamp = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
|
|
272
|
+
out_path = f"{netname}-{stamp}.pcap"
|
|
273
|
+
out = Path(out_path)
|
|
274
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
275
|
+
|
|
276
|
+
try:
|
|
277
|
+
started = post_net_command(
|
|
278
|
+
ctx, box_ip, netname, "start", role=SNIFFER_ROLE, quiet=True,
|
|
279
|
+
http_timeout=_START_HTTP_TIMEOUT_S,
|
|
280
|
+
duration=duration, follow=follow, address_type=address_type,
|
|
281
|
+
follow_by_name=follow_by_name, only_advertising=only_advertising,
|
|
282
|
+
coded=coded, rssi_cut_off=rssi_cut_off,
|
|
283
|
+
).get("value") or {}
|
|
284
|
+
except KeyboardInterrupt:
|
|
285
|
+
_stop_after_interrupted_start(ctx, box_ip, netname)
|
|
286
|
+
job_id = started.get("job")
|
|
287
|
+
if not job_id:
|
|
288
|
+
raise click.ClickException("The Lager Box returned no capture job")
|
|
289
|
+
if not as_json:
|
|
290
|
+
click.secho(f"Capturing on '{netname}' for {duration:g} s (Ctrl+C stops early)...",
|
|
291
|
+
fg="green")
|
|
292
|
+
job, interrupted = _poll(ctx, box_ip, netname, job_id, duration)
|
|
293
|
+
|
|
294
|
+
failed = job.get("state") == "failed"
|
|
295
|
+
if failed and not job.get("file_saved"):
|
|
296
|
+
raise click.ClickException(job.get("error") or "The capture failed")
|
|
297
|
+
value = job.get("result") or {}
|
|
298
|
+
box_path = value.get("file") or job.get("file")
|
|
299
|
+
try:
|
|
300
|
+
data = _download(box_ip, box_path)
|
|
301
|
+
except KeyboardInterrupt:
|
|
302
|
+
# The capture has ended; only the download stopped. Say where the file is.
|
|
303
|
+
_give_up(netname)
|
|
304
|
+
out.write_bytes(data)
|
|
305
|
+
packets = value.get("packets", _count_records(data))
|
|
306
|
+
|
|
307
|
+
deleted = False
|
|
308
|
+
if not keep:
|
|
309
|
+
try:
|
|
310
|
+
post_net_command(ctx, box_ip, netname, "delete", role=SNIFFER_ROLE, quiet=True,
|
|
311
|
+
file=box_path)
|
|
312
|
+
deleted = True
|
|
313
|
+
except SystemExit:
|
|
314
|
+
# The capture is saved here; only the cleanup on the box failed,
|
|
315
|
+
# and post_net_command printed why.
|
|
316
|
+
click.secho(f"Warning: the box copy {box_path} was not deleted", fg="yellow",
|
|
317
|
+
err=True)
|
|
318
|
+
|
|
319
|
+
if follow or follow_by_name:
|
|
320
|
+
_warn_follow(value, follow, follow_by_name)
|
|
321
|
+
stopped = interrupted or bool(value.get("stopped"))
|
|
322
|
+
if as_json:
|
|
323
|
+
click.echo(json.dumps({
|
|
324
|
+
"netname": netname,
|
|
325
|
+
"job": job_id,
|
|
326
|
+
"file": str(out),
|
|
327
|
+
"box_file": None if deleted else box_path,
|
|
328
|
+
"bytes": len(data),
|
|
329
|
+
"packets": packets,
|
|
330
|
+
"duration": value.get("duration", job.get("elapsed")),
|
|
331
|
+
"port": value.get("port"),
|
|
332
|
+
"firmware": value.get("firmware"),
|
|
333
|
+
"followed": value.get("followed"),
|
|
334
|
+
"follow_seen": value.get("follow_seen"),
|
|
335
|
+
"stopped": stopped,
|
|
336
|
+
"error": job.get("error"),
|
|
337
|
+
}))
|
|
338
|
+
else:
|
|
339
|
+
how = "stopped early, " if stopped else ""
|
|
340
|
+
took = value.get("duration", job.get("elapsed", 0.0))
|
|
341
|
+
click.secho(f"Saved {out} ({how}{len(data)} bytes, {packets} packets, {took:g} s)",
|
|
342
|
+
fg="yellow" if failed else "green")
|
|
343
|
+
if value.get("followed") and value.get("follow_seen"):
|
|
344
|
+
click.echo(f"Followed {value['followed']}")
|
|
345
|
+
if not deleted:
|
|
346
|
+
click.echo(f"Box copy: {box_path}")
|
|
347
|
+
if failed:
|
|
348
|
+
raise click.ClickException(job.get("error") or "The capture failed")
|
|
349
|
+
if interrupted:
|
|
350
|
+
ctx.exit(130)
|
|
351
|
+
|
|
352
|
+
|
|
353
|
+
def _warn_follow(value: dict, follow: str | None, follow_by_name: str | None) -> None:
|
|
354
|
+
if value.get("follow_seen") is not False:
|
|
355
|
+
return
|
|
356
|
+
what = f"'{follow_by_name}'" if follow_by_name else follow
|
|
357
|
+
if value.get("followed") is None:
|
|
358
|
+
hint = ("no advertisement with that name was heard" if follow_by_name
|
|
359
|
+
else "it was not heard advertising, so its address type was not known")
|
|
360
|
+
else:
|
|
361
|
+
hint = "check the address and --public/--random"
|
|
362
|
+
click.secho(f"Warning: nothing from {what} was captured; {hint}", fg="yellow", err=True)
|
|
363
|
+
|
|
364
|
+
|
|
365
|
+
def _count_records(data: bytes) -> int:
|
|
366
|
+
# Records in a little-endian classic pcap, for a capture whose result was
|
|
367
|
+
# lost to Ctrl+C; the box writes only that format.
|
|
368
|
+
count, offset = 0, 24
|
|
369
|
+
while offset + 16 <= len(data):
|
|
370
|
+
length = int.from_bytes(data[offset + 8:offset + 12], "little")
|
|
371
|
+
if offset + 16 + length > len(data):
|
|
372
|
+
break
|
|
373
|
+
count += 1
|
|
374
|
+
offset += 16 + length
|
|
375
|
+
return count
|
|
376
|
+
|
|
377
|
+
|
|
378
|
+
@ble_sniffer.command(name="stop", help="Stop the capture on the sniffer")
|
|
379
|
+
@click.option("--box", required=False, help="Lager Box name or IP")
|
|
380
|
+
@click.option("--json", "as_json", is_flag=True, default=False,
|
|
381
|
+
help="Emit a machine-readable JSON object instead of formatted text")
|
|
382
|
+
@click.pass_context
|
|
383
|
+
def stop(ctx, box, as_json):
|
|
384
|
+
# Locked like capture: stopping ends a capture, so it must not reach one
|
|
385
|
+
# running on a box another user has locked.
|
|
386
|
+
resolved = _resolve_net(ctx, box, lock=True)
|
|
387
|
+
if resolved is None:
|
|
388
|
+
return
|
|
389
|
+
box_ip, netname = resolved
|
|
390
|
+
result = post_net_command(ctx, box_ip, netname, "stop", role=SNIFFER_ROLE, quiet=True)
|
|
391
|
+
value = result.get("value") or {}
|
|
392
|
+
if as_json:
|
|
393
|
+
click.echo(json.dumps({"netname": netname, **value}))
|
|
394
|
+
return
|
|
395
|
+
click.echo(result.get("message", ""))
|
|
396
|
+
if value.get("file"):
|
|
397
|
+
click.echo(f"The capture ends within a second; its file on the box is "
|
|
398
|
+
f"{value['file']} (kept for a day)")
|
|
399
|
+
|
|
400
|
+
|
|
401
|
+
@ble_sniffer.command(name="status", help="Show the sniffer's serial port and whether it is busy")
|
|
402
|
+
@click.option("--box", required=False, help="Lager Box name or IP")
|
|
403
|
+
@click.option("--json", "as_json", is_flag=True, default=False,
|
|
404
|
+
help="Emit a machine-readable JSON object instead of formatted text")
|
|
405
|
+
@click.pass_context
|
|
406
|
+
def status(ctx, box, as_json):
|
|
407
|
+
resolved = _resolve_net(ctx, box, lock=False)
|
|
408
|
+
if resolved is None:
|
|
409
|
+
return
|
|
410
|
+
box_ip, netname = resolved
|
|
411
|
+
result = post_net_command(ctx, box_ip, netname, "status", role=SNIFFER_ROLE, quiet=True)
|
|
412
|
+
value = result.get("value") or {}
|
|
413
|
+
if as_json:
|
|
414
|
+
click.echo(json.dumps({"netname": netname, **value}))
|
|
415
|
+
return
|
|
416
|
+
click.echo(result.get("message", ""))
|
|
417
|
+
job = value.get("job")
|
|
418
|
+
if job:
|
|
419
|
+
state = job["state"]
|
|
420
|
+
if (job.get("result") or {}).get("stopped"):
|
|
421
|
+
state += " (stopped early)"
|
|
422
|
+
click.echo(f"Last capture: {state}, {job['packets']} packets in "
|
|
423
|
+
f"{job['elapsed']:g} of {job['duration']:g} s, {job['file']}")
|
|
@@ -1076,7 +1076,7 @@ def cursor(ctx, box, mcu):
|
|
|
1076
1076
|
_run_backend(ctx, box_ip, "measure_cursor", netname=netname, mcu=mcu)
|
|
1077
1077
|
|
|
1078
1078
|
|
|
1079
|
-
@cursor.command("time")
|
|
1079
|
+
@cursor.command("time", context_settings={"ignore_unknown_options": True})
|
|
1080
1080
|
@click.argument("t1", type=float)
|
|
1081
1081
|
@click.argument("t2", type=float)
|
|
1082
1082
|
@click.pass_context
|
|
@@ -1100,7 +1100,7 @@ def cursor_time(ctx, t1, t2, box, mcu):
|
|
|
1100
1100
|
_run_backend(ctx, box_ip, "measure_cursor", netname=netname, mcu=mcu)
|
|
1101
1101
|
|
|
1102
1102
|
|
|
1103
|
-
@cursor.command("volts")
|
|
1103
|
+
@cursor.command("volts", context_settings={"ignore_unknown_options": True})
|
|
1104
1104
|
@click.argument("v1", type=float)
|
|
1105
1105
|
@click.argument("v2", type=float)
|
|
1106
1106
|
@click.pass_context
|