Decode standard PPS sub-types + RST CLK extension → ATR data rate
- capture.py: map GSMTAP PPS request/response (0x02/0x03) to 'pps', matching the pcap-import mapping (was decoded as TPDU live) - decode.py: parse the optional RST CLK extension (sigrok v1.9.0+, payload flag bit 0); ATR gains clk_hz/data_rate/etu_us derived from TA1 Fi/Di (data_rate = clk_hz × D / F) - database.py: CLK state is consumed by the next ATR (no stale rate for an ATR without preceding RST); a late-arriving RST measurement is backfilled onto the ATR it belongs to; replay context carries the CLK across pagination/live polling - PWA: ATR detail shows CLK/Data rate/ETU; RST event detail shows CLK - tests: PPS mapping, CLK parse with/without flag, rate math, DB consume + backfill round-trips - README/RUS updated - Bump 1.22.18, SW cache v49->v50
This commit is contained in:
1 parent
c2f605a391
commit
a46667d93f
12 files changed
+211
-40
No files matched your search
@@ -6,7 +6,7 @@ from .gsmtap import GsmtapSender, GsmtapReceiver
|
||||
from .pcap import PcapWriter
|
||||
from .dump import FileDumper, format_message
|
||||
|
||||
__version__ = '1.22.17'
|
||||
__version__ = '1.22.18'
|
||||
__all__ = [
|
||||
'SniffSession',
|
||||
'DeviceDisconnected',
|
||||
|
||||
@@ -7,6 +7,7 @@ import time
|
||||
import traceback
|
||||
|
||||
from ..gsmtap import (GsmtapReceiver, GSMTAP_SIM_ATR,
|
||||
GSMTAP_SIM_PPS_REQ, GSMTAP_SIM_PPS_RSP,
|
||||
GSMTAP_SIM_RST_EVENT, GSMTAP_SIM_VCC_EVENT)
|
||||
|
||||
|
||||
@@ -22,11 +23,14 @@ def _log(msg):
|
||||
def gsmtap_msg_type(sub_type):
|
||||
"""Map a GSMTAP-SIM sub_type to a stored message type.
|
||||
|
||||
Custom sigrok-iso7816-stream line events (0x10/0x11) become 'rst'/'vcc';
|
||||
everything except ATR defaults to 'tpdu'.
|
||||
PPS request/response (0x02/0x03, standard GSMTAP) become 'pps'; custom
|
||||
sigrok-iso7816-stream line events (0x10/0x11) become 'rst'/'vcc';
|
||||
everything else defaults to 'tpdu'.
|
||||
"""
|
||||
return {
|
||||
GSMTAP_SIM_ATR: 'atr',
|
||||
GSMTAP_SIM_PPS_REQ: 'pps',
|
||||
GSMTAP_SIM_PPS_RSP: 'pps',
|
||||
GSMTAP_SIM_RST_EVENT: 'rst',
|
||||
GSMTAP_SIM_VCC_EVENT: 'vcc',
|
||||
}.get(sub_type, 'tpdu')
|
||||
|
||||
@@ -179,17 +179,18 @@ class Database:
|
||||
'WHERE session_id=? ORDER BY elapsed, id LIMIT ? OFFSET ?',
|
||||
(session_id, limit, offset)).fetchall()
|
||||
initial_states = None
|
||||
clk_hz = None
|
||||
if offset > 0 and rows:
|
||||
initial_states = self._replay_context(session_id, rows[0][0])
|
||||
return self._decode_rows(rows, initial_states=initial_states)
|
||||
initial_states, clk_hz = self._replay_context(session_id, rows[0][0])
|
||||
return self._decode_rows(rows, initial_states=initial_states, clk_hz=clk_hz)
|
||||
|
||||
def get_messages_after(self, session_id, after_id):
|
||||
rows = self._conn.execute(
|
||||
'SELECT id, session_id, elapsed, type, data, flags FROM messages '
|
||||
'WHERE session_id=? AND id > ? ORDER BY elapsed, id',
|
||||
(session_id, after_id)).fetchall()
|
||||
initial_states = self._replay_context(session_id, after_id + 1)
|
||||
return self._decode_rows(rows, initial_states=initial_states)
|
||||
initial_states, clk_hz = self._replay_context(session_id, after_id + 1)
|
||||
return self._decode_rows(rows, initial_states=initial_states, clk_hz=clk_hz)
|
||||
|
||||
def get_messages_raw(self, session_id):
|
||||
rows = self._conn.execute(
|
||||
@@ -226,7 +227,7 @@ class Database:
|
||||
(session_id,)).fetchall()
|
||||
return [{'type': r[0], 'count': r[1]} for r in rows]
|
||||
|
||||
def _row_to_message(self, row, prev=None):
|
||||
def _row_to_message(self, row, prev=None, clk_hz=None):
|
||||
data_blob = row[4]
|
||||
flags = row[5]
|
||||
msg = {
|
||||
@@ -240,7 +241,8 @@ class Database:
|
||||
if row[3] in ('tpdu', 'change', 'fidi', 'atr', 'pps', 'rst', 'vcc'):
|
||||
try:
|
||||
from .decode import decode_sniff_msg
|
||||
msg['decoded'] = decode_sniff_msg(data_blob, row[3], flags, prev=prev)
|
||||
msg['decoded'] = decode_sniff_msg(data_blob, row[3], flags,
|
||||
prev=prev, clk_hz=clk_hz)
|
||||
except Exception as e:
|
||||
import sys
|
||||
print(f'Decode error for msg {row[0]} ({row[3]}, {len(data_blob)} bytes): {e}',
|
||||
@@ -260,16 +262,27 @@ class Database:
|
||||
|
||||
def _replay_context(self, session_id, before_id):
|
||||
"""Replay (bounded) messages before *before_id* to recover per-channel
|
||||
selection, DF, and SFI map."""
|
||||
selection, DF, SFI map, and the last RST-reported CLK frequency.
|
||||
|
||||
Returns ``(states, clk_hz)``."""
|
||||
from .decode import decode_sniff_msg
|
||||
rows = self._conn.execute(
|
||||
"SELECT data, type FROM messages WHERE session_id=? AND id < ? "
|
||||
"AND type IN ('tpdu','atr','change','gap') ORDER BY id DESC LIMIT 500",
|
||||
"AND type IN ('tpdu','atr','change','gap','rst','vcc') "
|
||||
"ORDER BY id DESC LIMIT 500",
|
||||
(session_id, before_id)).fetchall()
|
||||
states = {}
|
||||
clk_hz = None
|
||||
for data, typ in reversed(rows):
|
||||
if typ in ('atr', 'change', 'gap'):
|
||||
states = {}
|
||||
# An ATR consumes the CLK rate; a replay starts fresh after it.
|
||||
clk_hz = None
|
||||
continue
|
||||
if typ in ('rst', 'vcc'):
|
||||
d = decode_sniff_msg(data, typ, 0)
|
||||
if d and d.get('clk_hz'):
|
||||
clk_hz = d['clk_hz']
|
||||
continue
|
||||
channel = data[0] & 0x03 if data and len(data) >= 1 else 0
|
||||
st = self._channel_state(states, channel)
|
||||
@@ -282,7 +295,7 @@ class Database:
|
||||
continue
|
||||
st['sel'], st['df'], st['sfi_map'] = self._advance_state(d, st['sel'], st['df'], st['sfi_map'])
|
||||
st['prev'] = self._context_from_decoded(d)
|
||||
return states
|
||||
return states, clk_hz
|
||||
|
||||
def _advance_state(self, d, sel, df, sfi_map):
|
||||
"""Update (sel, df, sfi_map) for one decoded TPDU."""
|
||||
@@ -350,9 +363,12 @@ class Database:
|
||||
return new
|
||||
return sel
|
||||
|
||||
def _decode_rows(self, rows, initial_states=None):
|
||||
def _decode_rows(self, rows, initial_states=None, clk_hz=None):
|
||||
from .decode import atr_rate_fields
|
||||
msgs = []
|
||||
states = dict(initial_states or {})
|
||||
last_atr_i = None # index of the most recent ATR in *msgs*
|
||||
last_atr_rated = False # whether that ATR already carries a data rate
|
||||
for row in rows:
|
||||
channel = 0
|
||||
if row[3] == 'tpdu' and row[4] and len(row[4]) >= 5:
|
||||
@@ -362,13 +378,18 @@ class Database:
|
||||
ctx['sel'] = st['sel']
|
||||
ctx['df'] = st['df']
|
||||
ctx['sfi_map'] = st['sfi_map']
|
||||
msg = self._row_to_message(row, prev=ctx)
|
||||
msg = self._row_to_message(row, prev=ctx, clk_hz=clk_hz)
|
||||
d = msg.get('decoded')
|
||||
if msg['type'] == 'tpdu' and d and d.get('ins_hex'):
|
||||
st['sel'], st['df'], st['sfi_map'] = self._advance_state(d, st['sel'], st['df'], st['sfi_map'])
|
||||
st['prev'] = self._context_from_decoded(d)
|
||||
elif msg['type'] == 'atr':
|
||||
states = {}
|
||||
# The CLK rate is consumed by the ATR it precedes: an ATR
|
||||
# without a preceding RST event must not inherit a stale rate.
|
||||
last_atr_i = len(msgs)
|
||||
last_atr_rated = bool(d and d.get('data_rate'))
|
||||
clk_hz = None
|
||||
elif msg['type'] == 'gap':
|
||||
# Device disconnected and reconnected — messages may have been
|
||||
# missed, so the selection is uncertain.
|
||||
@@ -389,6 +410,17 @@ class Database:
|
||||
# removal destroys the card's selection state. De-assert /
|
||||
# power-on are markers only (the following ATR resets state).
|
||||
data = row[4] if len(row) > 4 and row[4] else b''
|
||||
if d and d.get('clk_hz'):
|
||||
if (last_atr_i is not None and not last_atr_rated
|
||||
and msgs[last_atr_i].get('decoded')):
|
||||
# Ordering caveat: a short ATR can be emitted before
|
||||
# the RST event that measured its CLK rate — backfill
|
||||
# the rate onto that ATR.
|
||||
msgs[last_atr_i]['decoded'].update(
|
||||
atr_rate_fields(d['clk_hz'], msgs[last_atr_i]['decoded']))
|
||||
last_atr_rated = True
|
||||
else:
|
||||
clk_hz = d['clk_hz']
|
||||
if msg['type'] == 'rst':
|
||||
if data and data[0] == 1: # asserted
|
||||
states = {}
|
||||
|
||||
@@ -3992,6 +3992,11 @@ DATA_FLAGS = {
|
||||
1 << 10: 'parity error',
|
||||
}
|
||||
|
||||
# sigrok-iso7816-stream line-event payload flags (payload[2] of RST/VCC,
|
||||
# decoder v1.9.0+): bit 0 set → a big-endian uint32 CLK frequency in Hz
|
||||
# follows at bytes 3..6.
|
||||
LINE_EVENT_FLAG_CLK_HZ = 0x01
|
||||
|
||||
|
||||
def _flag_names(flags, mapping):
|
||||
names = []
|
||||
@@ -4013,11 +4018,13 @@ def decode_change(flags):
|
||||
|
||||
def decode_line_event(raw_data, kind):
|
||||
"""Decode a sigrok-iso7816-stream RST/VCC line event (GSMTAP sub_type
|
||||
0x10/0x11): payload [direction, level, reserved].
|
||||
0x10/0x11): payload [direction, level, flags, clk_hz_be32?].
|
||||
|
||||
direction — RST: 1 = asserted (high→low), 0 = de-asserted (low→high);
|
||||
VCC: 1 = power applied (low→high), 0 = removed (high→low).
|
||||
level — resulting line level: 0 = low, 1 = high.
|
||||
flags — bit 0 (LINE_EVENT_FLAG_CLK_HZ) set when a CLK frequency follows
|
||||
(decoder v1.9.0+); bytes 3..6 are then a big-endian uint32 in Hz.
|
||||
|
||||
``event`` is the canonical value; ``label`` is the human-facing
|
||||
summary (e.g. RST de-asserted → "ATR STARTS", VCC applied →
|
||||
@@ -4041,6 +4048,8 @@ def decode_line_event(raw_data, kind):
|
||||
result['event'] = 'power removed'
|
||||
result['label'] = 'VCC OFF (power-down)'
|
||||
result['level'] = 'high' if level else 'low'
|
||||
if (len(raw_data) >= 7 and raw_data[2] & LINE_EVENT_FLAG_CLK_HZ):
|
||||
result['clk_hz'] = int.from_bytes(raw_data[3:7], 'big')
|
||||
result['raw'] = raw_data.hex().upper()
|
||||
return result
|
||||
|
||||
@@ -4126,6 +4135,25 @@ def _fidi_dict(fi, di):
|
||||
return entry
|
||||
|
||||
|
||||
def atr_rate_fields(clk_hz, decoded):
|
||||
"""Derive the CLK frequency, data rate and ETU for a decoded ATR.
|
||||
|
||||
The Fi/Di communicated by TA1 (ISO 7816-3 §8.3) give the bit duration:
|
||||
``data_rate = clk_hz × D / F`` and ``ETU = F / (clk_hz × D)``. When TA1
|
||||
is absent the interface defaults (F=372, D=1) apply.
|
||||
"""
|
||||
f, d = 372, 1
|
||||
for ifc in (decoded.get('interface') or []):
|
||||
if ifc.get('name') == 'TA1' and 'f' in ifc and 'd' in ifc:
|
||||
f, d = ifc['f'], ifc['d']
|
||||
break
|
||||
return {
|
||||
'clk_hz': clk_hz,
|
||||
'data_rate': int(round(clk_hz * d / f)),
|
||||
'etu_us': round(f / (clk_hz * d) * 1e6, 1),
|
||||
}
|
||||
|
||||
|
||||
def _decode_historical(hist):
|
||||
"""Decode ATR historical bytes (ISO 7816-4 §8.1.1)."""
|
||||
result = {'raw': hist.hex().upper()}
|
||||
@@ -4149,8 +4177,13 @@ def _decode_historical(hist):
|
||||
return result
|
||||
|
||||
|
||||
def _decode_atr(data):
|
||||
"""Decode an Answer-To-Reset (ISO 7816-3 §8) into a structured dict."""
|
||||
def _decode_atr(data, clk_hz=None):
|
||||
"""Decode an Answer-To-Reset (ISO 7816-3 §8) into a structured dict.
|
||||
|
||||
*clk_hz* — when the sigrok-iso7816-stream decoder reported a CLK
|
||||
frequency on a preceding RST event, the ATR gains ``clk_hz``,
|
||||
``data_rate`` and ``etu_us`` fields derived from TA1 Fi/Di.
|
||||
"""
|
||||
if not data:
|
||||
return {'type': 'atr', 'raw': ''}
|
||||
ts = data[0]
|
||||
@@ -4169,6 +4202,8 @@ def _decode_atr(data):
|
||||
'raw': raw,
|
||||
}
|
||||
if not body:
|
||||
if clk_hz:
|
||||
result.update(atr_rate_fields(clk_hz, result))
|
||||
return result
|
||||
|
||||
t0 = body[0]
|
||||
@@ -4261,6 +4296,9 @@ def _decode_atr(data):
|
||||
result['tck'] = f'{tck:02X}'
|
||||
result['tck_valid'] = check == tck
|
||||
|
||||
if clk_hz:
|
||||
result.update(atr_rate_fields(clk_hz, result))
|
||||
|
||||
return result
|
||||
|
||||
|
||||
@@ -4304,14 +4342,16 @@ def _decode_pps(data):
|
||||
return result
|
||||
|
||||
|
||||
def decode_sniff_msg(raw_data, msg_type, flags=0, prev=None):
|
||||
def decode_sniff_msg(raw_data, msg_type, flags=0, prev=None, clk_hz=None):
|
||||
"""Decode a raw sniff message of the given type.
|
||||
|
||||
Returns a structured dict with at least a 'type' key, or None if
|
||||
the message type has no structured decode.
|
||||
|
||||
*prev* optionally carries the previous TPDU's decoded context for
|
||||
resolving GET RESPONSE.
|
||||
resolving GET RESPONSE. *clk_hz* is the CLK frequency reported by a
|
||||
preceding RST event (sigrok-iso7816-stream v1.9.0+), used to derive
|
||||
the data rate of an ATR.
|
||||
"""
|
||||
if msg_type == 'tpdu' and raw_data:
|
||||
result = decode_message(raw_data, prev=prev)
|
||||
@@ -4331,7 +4371,7 @@ def decode_sniff_msg(raw_data, msg_type, flags=0, prev=None):
|
||||
if msg_type == 'fidi' and raw_data:
|
||||
return decode_fidi(raw_data)
|
||||
if msg_type == 'atr':
|
||||
result = _decode_atr(raw_data)
|
||||
result = _decode_atr(raw_data, clk_hz=clk_hz)
|
||||
if result is not None:
|
||||
errs = (decode_data_flags(flags) or []) + (gsmtap_flag_names(flags) or [])
|
||||
if errs:
|
||||
|
||||
Reference in new issue
Block a user