"""Stage 1: does the station answer the link protocol the way docs/station-link.tex and main/link_service.cpp say? Every expectation below is taken from the spec text or from the firmware source (cited in the check's name), not from observed behaviour. What the source leaves open is reported as INFO with the observed value. The first half runs on a freshly reset, unconfigured station, so nothing in it can reach the radio. The second half configures it (security 0, low power) and, unless --no-tx, puts a few frames on air whose payload no ITS receiver accepts as a message (see linklib.test_payload). """ import random import struct import time import itslib import linklib as ll from bench_common import FAIL, INFO, PASS, SKIP, WARN S = "conformance" def expect_code(ctx, name, reply, code): got = reply.name if reply else "no reply" return ctx.res.ok(S, name, reply is not None and reply.code == code, "expected %s, got %s" % (ll.CODES[code], got)) def run(ctx): L, R = ctx.link, ctx.res ctx.phase = S L.reboot() s0 = L.wait_status(20) if not R.ok(S, "station answers STATUS_REQUEST after a reset", s0 is not None, "no CONFIRM within 20 s"): return R.ok(S, "STATUS detail is the 131-octet snapshot (spec: Status confirmation payload)", s0["raw_len"] == 131, "got %d octets" % s0["raw_len"]) R.ok(S, "a reset station is unconfigured", s0["configured"] == 0) R.ok(S, "uptime restarted at the reset", s0["uptime_ms"] < 30000, "uptime %d ms" % s0["uptime_ms"]) time.sleep(1.5) s1 = L.status() if s1: d = (s1["uptime_ms"] - s0["uptime_ms"]) / 1000.0 R.ok(S, "STATUS uptime runs at real time", 1.0 < d < 3.5, "%.2f s of uptime over ~2 s" % d) R.metric(S, "heap_free_after_boot", s1["heap_free"]) R.metric(S, "heap_largest_after_boot", s1["heap_largest"]) R.add(S, "battery reading with no cell", INFO, "battery_mv %d (0 = not measured, no shutdown on it)" % s1["battery_mv"]) crc0 = s1["link_crc_errors"] if s1 else 0 # ---- unknown and malformed (no station needed) for op in (0x00, 0x40, 0x7E, 0x80, 0x85, 0x90, 0xFF): expect_code(ctx, "opcode 0x%02x is answered unknown_opcode (link_service.cpp default)" % op, L.request(op, b""), ll.UNKNOWN_OPCODE) expect_code(ctx, "STATUS_REQUEST with a body is malformed", L.request(ll.STATUS_REQUEST, b"\x00"), ll.MALFORMED) for n in (0, 1, 5, 7, 15, 17): expect_code(ctx, "STATION_CONFIGURE of %d octets is malformed (spec: 6 or 16)" % n, L.request(ll.STATION_CONFIGURE, bytes(n)), ll.MALFORMED) expect_code(ctx, "STATION_CONFIGURE security=2 is malformed", L.request(ll.STATION_CONFIGURE, struct.pack(" 0)" % seq, r is not None and r.code == 0, "no CONFIRM" if r is None else "") # ---- request fragmentation (spec: Fragmented messages; request_reassembly.hpp). Unconfigured: reaching # not_configured proves the whole body was joined and decoded, and nothing can go on air. full = ll.btp_body(ll.test_payload(ctx.run_id, 1, ll.max_btp_payload())) R.ok(S, "test request is exactly 1536 octets (the request maximum)", len(full) == ll.REQUEST_MAX, "%d" % len(full)) for cap in (240, 504): expect_code(ctx, "1536-octet request in fragments of %d octets is joined and decoded (-> not_configured)" % cap, L.request_fragmented(ll.BTP_DATA_REQUEST, full, 3.0, cap=cap), ll.NOT_CONFIGURED) r = _expect_reply(L, 4242, lambda: L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, 4242, valid + bytes(560)))), 1.0) R.add(S, "an unfragmented link message over 512 octets", INFO, "reply: %s (the spec says to fragment; link_service.cpp drops it before dispatch)" % (r.name if r else "none")) over = ll.btp_body(b"y" * 500) seq = L.next_seq() reply = _expect_reply(L, seq, lambda: L.send_fragmented(ll.BTP_DATA_REQUEST, over, seq=seq, cap=240, total_override=1537), 2.0) expect_code(ctx, "fragmented request declaring 1537 octets is resource_limit", reply, ll.RESOURCE_LIMIT) seq = L.next_seq() reply = _expect_reply(L, seq, lambda: _gap(L, full, seq), 1.2) R.ok(S, "a fragment that leaves a gap is dropped without a reply", reply is None, "got %s" % (reply.name if reply else "")) seq = L.next_seq() reply = _expect_reply(L, seq, lambda: _expire(L, full, seq), 1.0) R.ok(S, "an unfinished request is discarded after the 2 s idle timeout (its late last fragment gets no reply)", reply is None, "got %s" % (reply.name if reply else "")) got = _nine_streams(ctx, L) R.ok(S, "at most 8 incomplete requests are held (spec: pending maximum 8)", got == 8, "%d of 9 interleaved requests were answered" % got) # ---- the USB framing s_before = L.status() L.write(ll.frame(ll.LINK, ll.link_message(ll.STATUS_REQUEST, 7), bad_crc=True)) time.sleep(0.4) s_after = L.status() if s_before and s_after: R.ok(S, "a frame with a wrong CRC is counted in link_crc_errors and ignored", s_after["link_crc_errors"] == s_before["link_crc_errors"] + 1, "%d -> %d" % (s_before["link_crc_errors"], s_after["link_crc_errors"])) L.write(bytes(random.Random(5).choice([b for b in range(256) if b != 0xAA]) for _ in range(60))) R.ok(S, "a valid frame after 60 octets of noise is answered (resync)", L.status() is not None) L.write(b"\xAA\xAA" + ll.frame(ll.LINK, ll.link_message(ll.STATUS_REQUEST, 8))) time.sleep(0.3) R.ok(S, "a valid frame after a doubled 0xAA is answered", L.status() is not None) for n in (1537, 4000, 65535): L.write(b"\xAA\x55\x10" + struct.pack(" %d" % (p0["poti_updates"], p1["poti_updates"])) r = L.request(ll.POTI_UPDATE, ll.poti_body(offset_ms=-8000)) R.add(S, "POTI_UPDATE 8 s behind the station clock", INFO, "reply: %s" % (r.name if r else "none (accepted silently)")) L.poti() # ---- unsecured transmission L.raw_frames.clear() sent = [] if ctx.tx_enabled: for i, size in enumerate((24, 43, 300)): payload = ll.test_payload(ctx.run_id, 100 + i, size) r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(payload)) expect_code(ctx, "unsecured BTP_DATA_REQUEST of %d octets is accepted" % size, r, ll.ACCEPTED) sent.append(payload) time.sleep(0.15) time.sleep(1.0) got = [itslib.unwrap(f) for _, fl, _, f in L.raw_frames if fl & 1] got = [g for g in got if g] seen = {g["payload"] for g in got} R.ok(S, "every accepted request leaves the radio, byte for byte (own-TX RAW_FRAME copy, unwrapped independently)", all(p in seen for p in sent), "%d of %d payloads captured" % (sum(p in seen for p in sent), len(sent))) R.ok(S, "unsecured frames carry no security envelope", all(not g["signed"] for g in got)) macs = {g["src"] for g in got} R.add(S, "802.11 source of own unsecured frames", INFO, ", ".join(sorted(macs))) else: R.add(S, "on-air checks", SKIP, "--no-tx") # Only the station's own transmissions count: with RAW_CAPTURE on, frames heard from other stations arrive here too # (the bench has a V2X node and a simulated car on the air; a quiet room does not). own_frames = lambda: sum(1 for _, fl, _, _ in list(L.raw_frames) if fl & 1) n_frames = own_frames() for name, body, code in ( ("transport type 2 (TSB) unsecured is unsupported (station_unsecured: geonet.c builds SHB only)", ll.btp_body(b"z" * 30, transport=2), ll.UNSUPPORTED), ("btp_type 2 is malformed", ll.btp_body(b"z" * 30, btp_type=2), ll.MALFORMED), ): expect_code(ctx, name, L.request(ll.BTP_DATA_REQUEST, body), code) time.sleep(0.8) R.ok(S, "refused requests put nothing on the radio", own_frames() == n_frames, "%d new own frames" % (own_frames() - n_frames)) # ---- secured transmission (configure with security on: the reply says whether the board holds a ticket) mac2 = ctx.next_mac() r = L.request(ll.STATION_CONFIGURE, ll.configure_body(secured=True, power=5, mac=mac2), 8.0) if r and r.code == 0 and len(r.detail) == 18: ctx.credentials = r.detail[16] == 1 and r.detail[17] > 0 R.add(S, "credentials on this board (configure with security on)", INFO, "credentials_loaded %d, tickets %d" % (r.detail[16], r.detail[17])) else: R.add(S, "STATION_CONFIGURE with security on", INFO, "reply: %s" % (r.name if r else "none")) L.poti() L.raw_capture(5) if not ctx.credentials: n_frames = len(L.raw_frames) r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(ll.test_payload(ctx.run_id, 200, 43), secured=True)) R.add(S, "secured request on a board without a ticket", INFO, "reply: %s" % (r.name if r else "none")) time.sleep(1.5) leaked = [itslib.unwrap(f) for _, fl, _, f in list(L.raw_frames)[n_frames:] if fl & 1] R.ok(S, "a secured request without a ticket never goes out unsigned", not any(leaked), "%d frames appeared" % len([g for g in leaked if g])) st = L.status() if st: R.add(S, "counters after the unsignable request", INFO, "refused_no_ticket %d, requests_refused %d, sign_failed %d" % (st["refused_no_ticket"], st["requests_refused"], st["sign_failed"])) elif ctx.tx_enabled: L.raw_frames.clear() payload = ll.test_payload(ctx.run_id, 201, 100) r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(payload, secured=True, generated_at_ms=ll.timestamp_its())) expect_code(ctx, "signed BTP_DATA_REQUEST is accepted", r, ll.ACCEPTED) time.sleep(2.0) got = [itslib.unwrap(f) for _, fl, _, f in L.raw_frames if fl & 1] got = [g for g in got if g and g["payload"] == payload] R.ok(S, "the signed request leaves the radio inside a signed envelope (IEEE 1609.2 signedData)", len(got) == 1 and got[0]["signed"], "%d matching frames, signed=%s" % (len(got), [g["signed"] for g in got])) R.ok(S, "signed frames use the MAC the phone named", len(got) == 1 and got[0]["src"] == mac2.hex(":"), "%s want %s" % ([g["src"] for g in got], mac2.hex(":"))) L.raw_frames.clear() stale = ll.test_payload(ctx.run_id, 202, 100) r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(stale, secured=True, generated_at_ms=ll.timestamp_its() - 5000)) expect_code(ctx, "a signed request generated 5 s ago is rejected as stale (tx_policy::is_stale)", r, ll.REJECTED) time.sleep(1.0) R.ok(S, "a stale request is never transmitted", not any(itslib.unwrap(f) for _, fl, _, f in L.raw_frames if fl & 1)) # How large a signed payload still leaves the radio: Vanetza's itsGnMaxSduSize is 1398 octets; the CONFIRM only says "queued" for size in (1398, 1399, 1400): L.poti() st0 = L.status() L.raw_frames.clear() payload = ll.test_payload(ctx.run_id, 210 + size, size) r = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(payload, secured=True, generated_at_ms=ll.timestamp_its()), 3.0) time.sleep(1.5) out = any((itslib.unwrap(f) or {}).get("payload") == payload for _, fl, _, f in L.raw_frames if fl & 1) st1 = L.status() R.add(S, "signed payload of %d octets" % size, INFO, "CONFIRM %s, transmitted %s, requests_refused %+d, sign_failed %+d" % (r.name if r else "none", "yes" if out else "NO", (st1["requests_refused"] - st0["requests_refused"]) if st0 and st1 else 0, (st1["sign_failed"] - st0["sign_failed"]) if st0 and st1 else 0)) else: R.add(S, "signed transmission", SKIP, "--no-tx") # ---- reconfigure with a named MAC mac = ctx.next_mac() r = L.request(ll.STATION_CONFIGURE, ll.configure_body(mac=mac, power=5)) expect_code(ctx, "16-octet STATION_CONFIGURE naming a MAC is accepted", r, ll.ACCEPTED) if r and len(r.detail) == 18: R.add(S, "GN address after naming MAC %s" % mac.hex(":"), INFO, r.detail[:8].hex(":")) if ctx.tx_enabled and r and r.code == 0: L.raw_frames.clear() L.poti() r2 = L.request(ll.BTP_DATA_REQUEST, ll.btp_body(ll.test_payload(ctx.run_id, 300, 43))) time.sleep(1.0) macs = {itslib.unwrap(f)["src"] for _, fl, _, f in L.raw_frames if fl & 1 and itslib.unwrap(f)} R.ok(S, "frames use the MAC the phone named (spec: MicrOBU 16-octet STATION_CONFIGURE)", macs == {mac.hex(":")}, "on air: %s" % sorted(macs)) L.raw_capture(0) end = L.status() if end: R.add(S, "after the stage", INFO, "uptime %d ms, heap_free %d, heap_min %d, radio failed %d, resets seen %d" % (end["uptime_ms"], end["heap_free"], end["heap_min_free"], end["radio_failed"], L.boots)) R.ok(S, "the station did not reset during the stage", end["uptime_ms"] > s0["uptime_ms"]) def _expect_reply(L, seq, action, wait): """Run `action` (which sends with this sequence) and wait up to `wait` s for a CONFIRM to it.""" import threading ev = [threading.Event(), None, time.time()] with L.lock: L.pending[seq] = ev try: action() ev[0].wait(wait) return ev[1] finally: with L.lock: L.pending.pop(seq, None) def _gap(L, body, seq): cap = 240 L.write(ll.frame(ll.LINK, ll.link_message(ll.BTP_DATA_REQUEST, seq, struct.pack("