Repository navigation
Expand file tree
/
Copy pathbts_simulation.py
More file actions
127 lines (108 loc) · 6.03 KB
/
Copy pathbts_simulation.py
File metadata and controls
127 lines (108 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# Copyright 2026 momentics <momentics@gmail.com>
# Copyright libgsml3parser contributors
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import sys
from pathlib import Path
# Bootstrap: make the `gsml3parser` package (sibling of this examples/ directory)
# importable regardless of the working directory the unified gate launches us from
# (sys.path[0] would otherwise only be the examples/ folder). Same trick as
# bindings/python/conftest.py for pytest.
sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
import gsml3parser as g
TMSI = 0x87654321
def fail(what):
print(f"FAIL: {what}")
sys.exit(1)
def main() -> int:
"""MO call over SDCCH through the full C core: LAPDm entity (callbacks into
Python), orchestrator chain and auto-response build/send — identical
behavioral vectors to examples/example_reference_bts.cpp. Exit 0 only if
every step's expectations hold (the unified gate relies on this contract)."""
# Both values come straight from the loaded C core (gsml3_version() /
# gsml3_abi_version()); the package performs no file I/O for its version.
print(f"version {g.version()} (C ABI revision {g.abi_version()})")
# BTS-side stack: registry + borrowed session + orchestrator + LAPDm entity
# with the C->Python callbacks; auto_response=True drives parse -> feed ->
# required_size -> build_response -> send_ui inside each send_frame, all
# AFTER the C call returned (queue model).
stack = g.GsmL3Stack(tmsi=TMSI, auto_response=True)
try:
# -- [1] CMServiceRequest (MO call) ------------------------------------------
# L3 is generated by a C typed builder (no hardcoded wire constants),
# wrapped into an MS-side UI frame (C/R=0, sapi 0) by the mini-codec.
l3 = g.build_cm_service_request(1, g.ID_TMSI, TMSI, None) # service_type=1
txs = stack.send_frame(g.lapdm_mini.ui(0, False, l3))
assert len(txs) == 1, f"expected one auto-response frame, got {len(txs)}"
# The response is a BTS-side UI (C/R=1); verify the payload via the C
# zero-copy decoder — the decoded view sat INSIDE txs[0] at info_offset.
dec = g.lapdm_frame_decode(txs[0])
assert (dec.format, dec.u_type, dec.sapi, dec.command) == (2, 3, 0, 1), dec
assert dec.info_offset is not None, "decoder must report a zero-copy view"
assert txs[0][dec.info_offset:dec.info_offset + dec.info_len] == dec.info
resp = g.Message.from_bytes(dec.info)
try:
assert resp.name == "CMServiceAccept", f"got {resp.name}"
assert resp.pd == g.PD_MM
print(f"[1] CMServiceRequest -> token={stack.last_step.token} (CM_SERVICE_ACCEPT)")
assert stack.last_step.token == 10, stack.last_step.token
print(f"[1] response: {resp.name} ({resp.size()} bytes): {resp.hex()}")
finally:
resp.close()
# -- [2] CC Setup (TI=3) -> CallProceeding, chain enters CALL_SETUP_MO ------
l3s = g.build_setup(3, "123456789")
txs = stack.send_frame(g.lapdm_mini.ui(0, False, l3s))
assert len(txs) == 1, f"expected one auto-response frame, got {len(txs)}"
dec = g.lapdm_frame_decode(txs[0])
cp = g.Message.from_bytes(dec.info)
try:
assert cp.name == "CallProceeding", f"got {cp.name}"
assert stack.last_step.token == 17, stack.last_step.token
print(f"[2] Setup -> token={stack.last_step.token} (CALL_PROCEEDING)")
assert cp.ti == 3, cp.ti
finally:
cp.close()
phase = stack.chain_phase
assert phase == 4, f"chain phase expected GSML3_PROC_CALL_SETUP_MO(4), got {phase}"
print(f"[2] chain phase = {phase} (GSML3_PROC_CALL_SETUP_MO)")
# -- [3] T3101 = 3 s: inside the window nothing fails; expiry times the
# chain out and resets it to idle. T200(SDCCH)=900 ms is entity-level
# (covered by the tests), this exercises the CHAIN timers.
n0 = stack.tick_procedures(1000)
assert n0 == 0 and stack.take_retransmit() == g.TOKEN_NONE, (n0,)
print(f"[3] T3101 window: tick(1000 ms) failures={n0}, retransmit=NONE")
n1 = stack.tick_procedures(2500) # crosses 3 s total
assert n1 == 1, f"expected one T3101 failure at expiry, got {n1}"
phase = stack.chain_phase
assert phase == g.PROC_UNKNOWN, f"chain must be idle after expiry, got {phase:#x}"
print(f"[3] T3101 expired: tick(+2500 ms) failures={n1}, chain phase = {phase} (GSML3_PROC_UNKNOWN)")
stack.close()
except AssertionError as exc:
fail(str(exc) or "assertion failed")
return 1 # unreachable (fail() exits), keeps static checkers happy
except g.GsmL3Error as exc:
fail(f"{type(exc).__name__}: {exc.message}")
return 1
except Exception as exc: # noqa: BLE001 — the gate contract is "any failure = exit 1"
fail(f"{type(exc).__name__}: {exc}")
return 1
print("OK: MO-call simulation completed (all steps asserted).")
return 0
if __name__ == "__main__":
sys.exit(main())