mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-12-02 06:53:40 +08:00
Galaxy: Let the hotspot stand down briefly while the comma joins Wi-Fi
This commit is contained in:
@@ -16,6 +16,8 @@ ADDRESS = '172.31.254.1'
|
||||
NETWORK = '172.31.254.0/24'
|
||||
STATUS_PATH = Path('/tmp/starpilot-galaxy-hotspot-status.json')
|
||||
DEFAULT = {'enabled': False, 'ssid': 'Galaxy', 'password': ''}
|
||||
PAUSE_PATH = Path('/tmp/starpilot-galaxy-hotspot-pause.json')
|
||||
MAX_PAUSE_S = 120.0
|
||||
|
||||
|
||||
def device_dongle_id():
|
||||
@@ -38,6 +40,26 @@ def validate_config(value):
|
||||
return dict(value)
|
||||
|
||||
|
||||
def request_pause(seconds, path=PAUSE_PATH, clock=time.monotonic):
|
||||
"""Ask the hotspot to stand down briefly, for example while the comma joins a Wi-Fi network during a drive."""
|
||||
atomic_json(path, {'until': clock() + min(float(seconds), MAX_PAUSE_S)}, mode=0o644)
|
||||
|
||||
|
||||
def clear_pause(path=PAUSE_PATH):
|
||||
try:
|
||||
path.unlink()
|
||||
except FileNotFoundError:
|
||||
pass
|
||||
|
||||
|
||||
def paused(path=PAUSE_PATH, clock=time.monotonic):
|
||||
try:
|
||||
until = bounded_json(path)['until']
|
||||
return type(until) in (int, float) and 0 < until - clock() <= MAX_PAUSE_S
|
||||
except (OSError, ValueError, KeyError, TypeError):
|
||||
return False
|
||||
|
||||
|
||||
def atomic_json(path, value, mode=0o600):
|
||||
path.parent.mkdir(parents=True, exist_ok=True, mode=0o700)
|
||||
fd, temporary = tempfile.mkstemp(prefix='.' + path.name, dir=path.parent)
|
||||
|
||||
@@ -7,7 +7,7 @@ import subprocess
|
||||
import threading
|
||||
import time
|
||||
|
||||
from openpilot.starpilot.galaxy.hotspot import HotspotSettings, STATUS_PATH, atomic_json
|
||||
from openpilot.starpilot.galaxy.hotspot import HotspotSettings, STATUS_PATH, atomic_json, paused
|
||||
from openpilot.starpilot.system.pipe_worker import spawn
|
||||
|
||||
|
||||
@@ -41,7 +41,8 @@ def main():
|
||||
runtime = {'active': False, 'state': 'starting', 'reason': 'Starting hotspot service.'}
|
||||
if child.poll() is not None:
|
||||
raise RuntimeError('Worker exited')
|
||||
child.stdin.write(json.dumps(config).encode() + b'\n')
|
||||
standing_down = paused()
|
||||
child.stdin.write(json.dumps({**config, 'enabled': not standing_down}).encode() + b'\n')
|
||||
child.stdin.flush()
|
||||
stdout = child.stdout
|
||||
if stdout is None:
|
||||
@@ -57,6 +58,10 @@ def main():
|
||||
line, buffer = buffer.split(b'\n', 1)
|
||||
runtime = json.loads(line)
|
||||
last_reply = time.monotonic()
|
||||
if standing_down:
|
||||
runtime = {'active': False, 'state': 'waiting', 'reason': 'Paused while the comma joins Wi-Fi.', 'revision': revision}
|
||||
elif runtime.get('state') == 'disabled':
|
||||
runtime = {'active': False, 'state': 'starting', 'reason': 'Starting hotspot service.', 'revision': revision}
|
||||
if time.monotonic() - last_reply > 20:
|
||||
raise RuntimeError('Worker unresponsive')
|
||||
atomic_json(STATUS_PATH, {**runtime, 'revision': runtime.get('revision', revision), 'updated': time.monotonic()})
|
||||
|
||||
@@ -456,3 +456,21 @@ def test_netlink_new_interface_payloads():
|
||||
wireless.create('aa0', iftype=2)
|
||||
station = attribute(1, struct.pack('I', 0)) + attribute(4, b'aa0\0') + attribute(5, struct.pack('I', 2))
|
||||
wireless.query.assert_called_with(22, 7, station, ack=True)
|
||||
|
||||
|
||||
def test_a_pause_request_expires_on_its_own_and_a_bad_file_never_pauses(tmp_path):
|
||||
from openpilot.starpilot.galaxy import hotspot
|
||||
path, now = tmp_path / 'pause.json', [100.0]
|
||||
assert not hotspot.paused(path, lambda: now[0])
|
||||
hotspot.request_pause(30, path, lambda: now[0])
|
||||
assert hotspot.paused(path, lambda: now[0])
|
||||
now[0] += 31
|
||||
assert not hotspot.paused(path, lambda: now[0])
|
||||
hotspot.request_pause(10_000, path, lambda: now[0])
|
||||
now[0] += hotspot.MAX_PAUSE_S + 1
|
||||
assert not hotspot.paused(path, lambda: now[0])
|
||||
path.write_text('{"until": 1e18}')
|
||||
assert not hotspot.paused(path, lambda: now[0]) # a stale or forged file cannot hold the hotspot down
|
||||
hotspot.clear_pause(path)
|
||||
hotspot.clear_pause(path)
|
||||
assert not path.exists()
|
||||
|
||||
Reference in New Issue
Block a user