Add authenticated WiFi transport with BLE fallback

This commit is contained in:
2026-10-01 14:32:18 +08:00
parent 071f06d365
commit f011d8357d
55 changed files with 6327 additions and 29 deletions
+11 -2
View File
@@ -23,7 +23,7 @@ def checked_name(value):
class SessionManager:
def __init__(self, dispatch, identity, *, clock=time.monotonic):
def __init__(self, dispatch, identity, *, clock=time.monotonic, wifi_address=lambda: None):
self.dispatch = dispatch
self.identity = identity
self.clock = clock
@@ -37,6 +37,8 @@ class SessionManager:
self.connected_at = self.last_activity = 0
self.generation = 0
self.peer_mtu = 23
from .wifi import WifiSession
self.wifi = WifiSession(self, wifi_address)
def connect(self, owner):
with self.lock:
@@ -55,6 +57,7 @@ class SessionManager:
self.opened = False
self.last_request = -1
self.peer_mtu = 23
self.wifi.close()
self.generation += 1
def expired(self):
@@ -68,7 +71,8 @@ class SessionManager:
def status(self):
with self.lock:
return dict(connected=self.opened, client_name=self.client_name if self.opened else None,
generation=self.generation)
generation=self.generation, active_transport=("wifi" if self.wifi.valid() else "ble") if self.opened else None,
wifi_counters=dict(self.wifi.counters))
def accept(self, owner, fragment):
with self.lock:
@@ -119,6 +123,11 @@ class SessionManager:
result = dict(client_name=self.client_name)
elif method == "session.open":
raise RpcError("CONFLICT", "会话已经建立")
elif method == "transport.offer":
result = self.wifi.offer()
elif method == "transport.close":
self.wifi.close()
result = dict(closed=True)
else:
result = self.dispatch(method, params)
response.update(ok=True, result=result)