"""Shared helpers of the bench tests: pcap reading and a Python port of obu-firmware/main/gn_unwrap.c. The port is written from the C file and EN 302 636-4-1, not from the firmware's behaviour on a board, so it is a second opinion on what the firmware's own unwrapper should extract from a frame. It is what the bench tests use to check the frames the station puts on the radio (RAW_FRAME own-TX copies) and what compare_phone_vs_node.py uses to read a pcap. """ import struct PORT_NAMES = {2001: "CAM", 2002: "DENM", 2003: "MAPEM", 2004: "SPATEM", 2018: "VAM"} LLC_SNAP = b"\xaa\xaa\x03\x00\x00\x00\x89\x47" def read_pcap(path): """Yield (time_s, 802.11 frame) for every record of a linktype 105 (802.11) or 127 (radiotap) pcap.""" d = open(path, "rb").read() if len(d) < 24: return magic = struct.unpack("" nano = magic == 0xA1B23C4D or struct.unpack(">I", d[:4])[0] == 0xA1B23C4D link = struct.unpack(e + "I", d[20:24])[0] if link not in (105, 127): raise SystemExit("pcap linktype %d: only 105 (802.11) and 127 (radiotap) are read" % link) off = 24 while off + 16 <= len(d): s, frac, incl, _ = struct.unpack(e + "IIII", d[off:off + 16]) if incl > len(d) - off - 16: break pkt = d[off + 16:off + 16 + incl] off += 16 + incl if link == 127: if len(pkt) < 4: continue rl = pkt[2] | pkt[3] << 8 if rl > len(pkt): continue pkt = pkt[rl:] yield s + frac / (1e9 if nano else 1e6), pkt def coer_length(p, i): """(length, bytes used) of a COER length determinant at p[i], or (None, 0).""" if i >= len(p): return None, 0 if p[i] < 0x80: return p[i], 1 n = p[i] & 0x7F if n < 1 or n > 2 or i + 1 + n > len(p): return None, 0 return int.from_bytes(p[i + 1:i + 1 + n], "big"), 1 + n def unwrap(f, ports=None): """gn_unwrap_its in Python. Returns dict(port, payload, signed, area, truncated, src, dst, header_type) or None. `ports` limits the BTP destination ports accepted (default: the five the firmware forwards). """ ports = PORT_NAMES if ports is None else ports if len(f) < 24: return None t, sub = (f[0] >> 2) & 3, (f[0] >> 4) & 15 if t != 2 or (f[1] & 3) == 3: return None o = 24 + (2 if sub & 8 else 0) if len(f) < o + 8 or f[o:o + 8] != LLC_SNAP or len(f) < o + 12: return None o += 8 bnh = f[o] & 0x0F o += 4 limit, env_end, signed = len(f), -1, False if bnh == 2: p, avail = f[o:], len(f) - o if avail < 2 or p[0] != 3: return None if p[1] == 0x81: if avail < 6 or p[2] >= 0x80 or not p[3] & 0x40 or p[4] != 3 or p[5] != 0x80: return None i, signed = 6, True elif p[1] == 0x80: i = 2 else: return None n, used = coer_length(p, i) if not used: return None env_end = o + i + used + n o += i + used limit = min(limit, env_end) elif bnh != 1: return None if limit < o + 8: return None nh, ht, hst = f[o] >> 4, f[o + 1] >> 4, f[o + 1] & 15 gn_len = int.from_bytes(f[o + 4:o + 6], "big") if nh != 2: return None o += 8 area = None if ht == 5 and hst == 0: ext = 28 elif ht == 4: ext = 44 else: return None if limit < o + ext: return None if ht == 4: area = (int.from_bytes(f[o + 28:o + 32], "big", signed=True), int.from_bytes(f[o + 32:o + 36], "big", signed=True), int.from_bytes(f[o + 36:o + 38], "big")) o += ext if limit < o + 4: return None port = int.from_bytes(f[o:o + 2], "big") if port not in ports or gn_len <= 4: return None start, end = o + 4, o + gn_len if env_end >= 0 and end > env_end: return None trunc = end > len(f) payload = f[start:(len(f) if trunc else end)] if not payload: return None return dict(port=port, payload=bytes(payload), signed=signed, area=area, truncated=trunc, src=f[10:16].hex(":"), dst=f[4:10].hex(":"), header_type=(ht, hst)) def station_of(payload): """ItsPduHeader.stationId (CAM/DENM/SPATEM/MAPEM/VAM all start protocolVersion, messageId, stationId).""" return int.from_bytes(payload[2:6], "big") if len(payload) >= 6 else None