EnvelopeCodec message layer; scripts are thin business-API wrappers
This commit is contained in:
1 parent
f15ae42db7
commit
363c7cc6a2
14 files changed
+551
-900
No files matched your search
+91
-108
@@ -1,59 +1,52 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Host probe v5 — session behaviour on 9000 (stdlib only).
|
||||
"""Host probe v5 — session sequencing via the library.
|
||||
|
||||
v4 (correct ids + opt payloads) is still met with silence, so this probe
|
||||
tests *session* hypotheses instead of bytes:
|
||||
* persistent connection: discovery → directed queries on ONE socket, with
|
||||
app-like sequential snno (the app reuses its SocketManager connection;
|
||||
the field captures show a discovery frame minutes before directed ones);
|
||||
* close-vs-timeout distinction for every read (immediate close = active
|
||||
reject; 8 s idle + open conn = device thinking/forwarding upstream);
|
||||
* high-snno opens (in case the device tracks sequence per sender).
|
||||
One persistent connection carrying discovery → directed queries with
|
||||
app-like sequential ``snno``, plus close-vs-timeout reporting and
|
||||
high-snno opens. Payload bytes come from ``direct_connect_request`` +
|
||||
``EnvelopeCodec``; only the connection orchestration lives here.
|
||||
|
||||
Run from the repo root on the SoftAP-joined host:
|
||||
python3 scripts/host_probe5.py --sn A03X2516013210060005 --ports 9000
|
||||
"""
|
||||
|
||||
import argparse
|
||||
import json
|
||||
import os
|
||||
import socket
|
||||
import sys
|
||||
import time
|
||||
import zlib
|
||||
|
||||
APOWER, QUERY_INFO, BMS = 0x51B, 0x44D, 0x6A9
|
||||
SNNO = [100]
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(
|
||||
os.path.abspath(__file__))))
|
||||
|
||||
from franklinwh_agate_client.device_local import ( # noqa: E402
|
||||
LocalDeviceClient,
|
||||
)
|
||||
from franklinwh_agate_client.protocol import ( # noqa: E402
|
||||
CommunicationCmd,
|
||||
direct_connect_request,
|
||||
generate_secret_key,
|
||||
)
|
||||
from franklinwh_agate_client.protocol.envelope import ( # noqa: E402
|
||||
SequenceNumber,
|
||||
)
|
||||
from franklinwh_agate_client.protocol.messages import ( # noqa: E402
|
||||
EnvelopeCodec,
|
||||
)
|
||||
|
||||
_seq = SequenceNumber(start=100)
|
||||
|
||||
|
||||
def secret_key(sn):
|
||||
return format(sum(sn.encode()) & 0xFF, "x")
|
||||
def tx_for(codec: EnvelopeCodec, sn: str, cmd: CommunicationCmd,
|
||||
data, equip=None, snno=None) -> bytes:
|
||||
env = direct_connect_request(
|
||||
cmd, sn if equip is None else equip, data=data,
|
||||
snno=_seq.next() if snno is None else snno)
|
||||
return codec.encode(env)
|
||||
|
||||
|
||||
def build_envelope(sn, cmd, args=None, snno=None, ts=None, equip=None):
|
||||
if snno is None:
|
||||
SNNO[0] = (SNNO[0] % 9999) + 1
|
||||
snno = SNNO[0]
|
||||
data = {"opt": 0}
|
||||
if args:
|
||||
data.update(args)
|
||||
compact = json.dumps(data, separators=(",", ":"))
|
||||
env = {"cmdType": cmd, "equipNo": equip if equip is not None else sn,
|
||||
"type": 0,
|
||||
"timeStamp": ts if ts is not None else int(time.time()),
|
||||
"snno": snno,
|
||||
"len": len(compact.encode()),
|
||||
"crc": format(zlib.crc32(compact.encode()), "X"),
|
||||
"dataArea": data}
|
||||
return json.dumps(env, separators=(",", ":"))
|
||||
|
||||
|
||||
def encrypt(json_str, key):
|
||||
parts = json_str.split(",")
|
||||
a = (parts[0] + "," + parts[1] + ",").encode()
|
||||
b = ",".join(parts[2:]).encode()
|
||||
n = int(key, 32)
|
||||
return a + bytes((b[i] + i + len(a) + n) & 0xFF
|
||||
for i in range(len(b)))
|
||||
|
||||
|
||||
def recv_all_verbose(sock, idle=8.0):
|
||||
"""Return (bytes, how) where how is closed|idle-timeout."""
|
||||
def recv_all_verbose(sock: socket.socket, idle: float = 8.0):
|
||||
out = bytearray()
|
||||
sock.settimeout(idle)
|
||||
while True:
|
||||
@@ -66,47 +59,6 @@ def recv_all_verbose(sock, idle=8.0):
|
||||
out.extend(chunk)
|
||||
|
||||
|
||||
def one_shot(label, host, port, tx, timeout, snno):
|
||||
try:
|
||||
with socket.create_connection((host, port),
|
||||
timeout=timeout) as sock:
|
||||
t0 = time.time()
|
||||
sock.sendall(tx)
|
||||
rx, how = recv_all_verbose(sock)
|
||||
dt = time.time() - t0
|
||||
except (ConnectionError, socket.error, TimeoutError) as exc:
|
||||
print("--- %s snno=%d -> %s:%d" % (label, snno, host, port))
|
||||
print(" tx (%d B): %s" % (len(tx), tx.hex()))
|
||||
print(" error: %s: %s" % (type(exc).__name__, exc))
|
||||
return b""
|
||||
print("--- %s snno=%d -> %s:%d (%.1fs, %s)" % (
|
||||
label, snno, host, port, dt, how))
|
||||
print(" tx (%d B): %s" % (len(tx), tx.hex()))
|
||||
print(" rx (%d B): %s" % (len(rx), rx.hex() or "(empty)"))
|
||||
return rx
|
||||
|
||||
|
||||
def session(label, host, port, frames, timeout):
|
||||
print("=== %s -> %s:%d (one connection)" % (label, host, port))
|
||||
try:
|
||||
with socket.create_connection((host, port),
|
||||
timeout=timeout) as sock:
|
||||
for name, tx in frames:
|
||||
t0 = time.time()
|
||||
sock.sendall(tx)
|
||||
rx, how = recv_all_verbose(sock)
|
||||
dt = time.time() - t0
|
||||
print("--- %s tx=%dB rx=%dB (%.1fs, %s)" % (
|
||||
name, len(tx), len(rx), dt, how))
|
||||
if rx:
|
||||
print(" rx: %s" % rx.hex()[:3000])
|
||||
if how == "closed-by-peer" and not rx:
|
||||
print(" (peer closed; stopping session)")
|
||||
break
|
||||
except (ConnectionError, socket.error, TimeoutError) as exc:
|
||||
print(" error: %s: %s" % (type(exc).__name__, exc))
|
||||
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser()
|
||||
ap.add_argument("--sn", required=True)
|
||||
@@ -114,32 +66,63 @@ def main():
|
||||
ap.add_argument("--ports", default="9000")
|
||||
ap.add_argument("--timeout", type=float, default=5.0)
|
||||
args = ap.parse_args()
|
||||
key = secret_key(args.sn)
|
||||
print("secret key for %s: %s (radix 32)" % (args.sn, key))
|
||||
|
||||
def tx(cmd, data, equip=None, snno=None):
|
||||
return encrypt(build_envelope(
|
||||
args.sn, cmd, data, snno=snno,
|
||||
equip=args.sn if equip is None else equip or "00000000"), key)
|
||||
|
||||
codec = EnvelopeCodec.for_serial(args.sn)
|
||||
print("secret key for %s: %s (radix 16)"
|
||||
% (args.sn, generate_secret_key(args.sn)))
|
||||
ports = [int(p) for p in args.ports.split(",") if p.strip()]
|
||||
for port in ports:
|
||||
# A. persistent session: discovery first, then directed (app order)
|
||||
session("discovery-then-directed", args.host, port, [
|
||||
("disc/1101+empty",
|
||||
tx(QUERY_INFO, {"minProtocolVer": "V1.00.00"}, equip="")),
|
||||
("info/1101+SN",
|
||||
tx(QUERY_INFO, {"minProtocolVer": "V1.00.00"})),
|
||||
("apower/1307",
|
||||
tx(APOWER, {"update": 2})),
|
||||
("bms/1705", tx(BMS, None)),
|
||||
], args.timeout)
|
||||
# B. high-snno fresh opens
|
||||
one_shot("apower-high-snno", args.host, port,
|
||||
tx(APOWER, {"update": 2}, snno=500), args.timeout, 500)
|
||||
one_shot("info-high-snno", args.host, port,
|
||||
tx(QUERY_INFO, {"minProtocolVer": "V1.00.00"}, snno=501),
|
||||
args.timeout, 501)
|
||||
print("=== discovery-then-directed -> %s:%d (one connection)"
|
||||
% (args.host, port))
|
||||
try:
|
||||
with socket.create_connection(
|
||||
(args.host, port), timeout=args.timeout) as sock:
|
||||
for name, cmd, data, equip in (
|
||||
("disc/1101+empty", CommunicationCmd.QUERY_DEVICE_INFO,
|
||||
{"minProtocolVer": "V1.00.00"}, ""),
|
||||
("info/1101+SN", CommunicationCmd.QUERY_DEVICE_INFO,
|
||||
{"minProtocolVer": "V1.00.00"}, None),
|
||||
("apower/1307", CommunicationCmd.DEVICE_APOWER_INFO,
|
||||
{"update": 2}, None),
|
||||
("bms/1705", CommunicationCmd.BMS, {"id": 1}, None),
|
||||
):
|
||||
payload = tx_for(codec, args.sn, cmd, data, equip)
|
||||
t0 = time.time()
|
||||
sock.sendall(payload)
|
||||
rx, how = recv_all_verbose(sock)
|
||||
print("--- %s tx=%dB rx=%dB (%.1fs, %s)"
|
||||
% (name, len(payload), len(rx),
|
||||
time.time() - t0, how))
|
||||
if rx:
|
||||
print(" rx: %s" % rx.hex()[:400])
|
||||
parsed = codec.try_decode(rx)
|
||||
if parsed is not None:
|
||||
print(" reply: " + json.dumps(
|
||||
parsed, default=str)[:800])
|
||||
if how == "closed-by-peer" and not rx:
|
||||
print(" (peer closed; stopping session)")
|
||||
break
|
||||
except (ConnectionError, socket.error, TimeoutError) as exc:
|
||||
print(" error: %s: %s" % (type(exc).__name__, exc))
|
||||
|
||||
client = LocalDeviceClient(args.sn, host=args.host,
|
||||
socket_port=port, timeout=args.timeout)
|
||||
for label, cmd, data, snno in (
|
||||
("apower-high-snno", CommunicationCmd.DEVICE_APOWER_INFO,
|
||||
{"update": 2}, 500),
|
||||
("info-high-snno", CommunicationCmd.QUERY_DEVICE_INFO,
|
||||
{"minProtocolVer": "V1.00.00"}, 501),
|
||||
):
|
||||
env = direct_connect_request(cmd, args.sn, data=data, snno=snno)
|
||||
payload = codec.encode(env)
|
||||
try:
|
||||
rx = client.send_raw(payload, timeout=args.timeout)
|
||||
except (ConnectionError, socket.error, TimeoutError) as exc:
|
||||
print("--- %s: error: %s" % (label, exc))
|
||||
continue
|
||||
print("--- %s tx=%dB rx=%dB" % (label, len(payload), len(rx)))
|
||||
parsed = codec.try_decode(rx)
|
||||
if parsed is not None:
|
||||
print(" reply: " + json.dumps(parsed, default=str)[:800])
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
Reference in new issue
Block a user