summaryrefslogtreecommitdiff
path: root/stacks/tncs/tnc2c-boot-wait.py
blob: ba24e2638da6ea2c6347d9950ad1aad3d3c5cb01 (plain)
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
#!/usr/bin/env python3
"""
Hold /dev/ttyS4 open with DTR+RTS during TNC power-on, then native TF probe (ESC V).

Usage:
  1. Run this FIRST (waits for port):
     ./tnc2c-boot-wait.sh
  2. While it says "waiting", power-cycle the TNC (off 10s, on).
  3. Script detects banner or runs KISS return + ESC V automatically.
"""

from __future__ import annotations

import argparse
import importlib.util
import fcntl
import os
import struct
import sys
import time
import termios

def load_recovery():
    root = os.path.dirname(os.path.abspath(__file__))
    path = os.path.join(root, "tnc_serial_recovery.py")
    spec = importlib.util.spec_from_file_location("tnc_serial_recovery", path)
    if spec is None or spec.loader is None:
        return None
    mod = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(mod)
    return mod


FIRMWARE_MARKERS = (
    b"TheFirmware",
    b"NORD",
    b"Version 2.7",
    b"Checksum",
    b"Copyright",
)


def has_banner(data: bytes) -> bool:
    lower = data.lower()
    return any(m.lower() in lower for m in FIRMWARE_MARKERS)


def parse_line(line: str) -> tuple[int, int]:
    line = line.lower()
    if line == "7e1":
        return termios.CS7, termios.PARENB
    if line == "8n1":
        return termios.CS8, 0
    raise ValueError(f"unsupported serial line: {line}")


def parse_baud(baud: int) -> int:
    table = {
        1200: termios.B1200,
        2400: termios.B2400,
        4800: termios.B4800,
        9600: termios.B9600,
        19200: termios.B19200,
    }
    if baud not in table:
        raise ValueError(f"unsupported baud: {baud}")
    return table[baud]


def open_serial(dev: str, baud: int, line: str) -> int:
    speed = parse_baud(baud)
    databits, parity = parse_line(line)
    fd = os.open(dev, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK)
    t = termios.tcgetattr(fd)
    t[0] = t[1] = 0
    t[2] = termios.CLOCAL | termios.CREAD | databits | parity
    t[3] = t[4] = t[5] = speed
    t[6][termios.VMIN] = 0
    t[6][termios.VTIME] = 5
    termios.tcsetattr(fd, termios.TCSANOW, t)
    termios.tcflush(fd, termios.TCIOFLUSH)
    flags = struct.unpack("I", fcntl.ioctl(fd, 0x5415, struct.pack("I", 0)))[0]
    flags |= 0x004 | 0x002
    fcntl.ioctl(fd, 0x5416, struct.pack("I", flags))
    return fd


def read_for(fd: int, seconds: float) -> bytes:
    end = time.time() + seconds
    chunks: list[bytes] = []
    while time.time() < end:
        try:
            b = os.read(fd, 4096)
            if b:
                chunks.append(b)
        except BlockingIOError:
            time.sleep(0.02)
    return b"".join(chunks)


def write_flush(fd: int, data: bytes) -> None:
    os.write(fd, data)
    termios.tcdrain(fd)


def show(data: bytes) -> None:
    if data:
        print(data.decode("ascii", errors="replace"))


def is_cmd_echo(cmd: bytes, reply: bytes) -> bool:
    c = cmd.strip(b"\r\n")
    r = reply.strip()
    return r in (c, c + b"\r", c + b"\n", c + b"\r\n")


def run_tx_rx(fd: int) -> None:
    """TX KISS frame + passive RX on same open fd (before port close)."""
    print("\n--- TX (KISS TEST-0 -> CQ, TXRX) ---")
    write_flush(fd, b"\x1b@K")
    time.sleep(0.5)
    read_for(fd, 0.3)

    def kiss_escape(data: bytes) -> bytes:
        out = bytearray()
        for b in data:
            if b == 0xC0:
                out.extend((0xDB, 0xDC))
            elif b == 0xDB:
                out.extend((0xDB, 0xDD))
            else:
                out.append(b)
        return bytes(out)

    def ax25_addr(call: str, last: bool) -> bytes:
        call = call.upper().ljust(6)[:6]
        raw = bytes((ord(c) << 1) for c in call)
        ssid = ((0 & 0x0F) << 1) | (0x01 if last else 0x00)
        return raw + bytes([ssid])

    def ax25_crc(data: bytes) -> int:
        crc = 0xFFFF
        for b in data:
            crc ^= b
            for _ in range(8):
                crc = (crc >> 1) ^ 0x8408 if crc & 1 else crc >> 1
        return crc ^ 0xFFFF

    body = ax25_addr("CQ", False) + ax25_addr("TEST-0", True) + b"\x03\xF0TXRX"
    crc = ax25_crc(body)
    payload = body + bytes((crc & 0xFF, crc >> 8))
    pkt = b"\xC0\x00" + kiss_escape(payload) + b"\xC0"
    write_flush(fd, pkt)
    time.sleep(3.0)
    tx_rx = read_for(fd, 1.0)
    print(f"  Sent {len(pkt)} B, serial RX {len(tx_rx)} B")
    print("  -> CHECK: LED2 PTT + modem tone on 2m CB")

    write_flush(fd, b"\xc0\xff\xc0")
    time.sleep(1.0)
    read_for(fd, 0.5)

    print("\n--- RX (passive 10s) ---")
    print("  Listening ... (empty band = 0 bytes OK; LED3 CD with noise?)")
    rx = read_for(fd, 10.0)
    print(f"  Passive RX: {len(rx)} bytes")
    if rx and b"\xc0" in rx:
        print("  OK: KISS frame received")


def verify_hybbx_ready(fd: int) -> tuple[bool, bytes, bool]:
    """ESC V on open fd. Returns (banner_in_reply, data, only_echo)."""
    write_flush(fd, b"\x1bV\r")
    time.sleep(0.4)
    info = read_for(fd, 5.0)
    only_echo = is_cmd_echo(b"\x1bV\r", info)
    return has_banner(info), info, only_echo


def finish_host(fd: int, hybbx_ready: bool, do_tx_rx: bool) -> int:
    if hybbx_ready:
        print("\n--- HyBBX-Verify (ESC V) ---")
        ok, verify, only_echo = verify_hybbx_ready(fd)
        show(verify)
        host_ok = ok or only_echo
        if not host_ok:
            if do_tx_rx:
                run_tx_rx(fd)
            os.close(fd)
            print("\nWARN: unexpected ESC V response")
            return 1
    else:
        host_ok = True

    if do_tx_rx:
        run_tx_rx(fd)

    os.close(fd)
    if host_ok:
        print("\nOK: HOST - HyBBX-ready" + (" + TX/RX tested" if do_tx_rx else ""))
        return 0
    return 1


def main() -> int:
    parser = argparse.ArgumentParser(description="Hold DTR during TNC boot")
    default_dev = (
        os.environ.get("TNC_DEV")
        or os.environ.get("TNC2C_DEV")
        or os.environ.get("PKTNC2_DEV")
        or "/dev/ttyS4"
    )
    default_baud = int(
        os.environ.get("TNC_BAUD")
        or os.environ.get("TNC2C_BAUD")
        or os.environ.get("PKTNC2_BAUD")
        or "19200"
    )
    default_line = (
        os.environ.get("TNC_LINE")
        or os.environ.get("TNC2C_LINE")
        or os.environ.get("PKTNC2_LINE")
        or "8n1"
    )

    parser.add_argument("device", nargs="?", default=default_dev)
    parser.add_argument("--baud", type=int, default=default_baud)
    parser.add_argument("--line", default=default_line, choices=("8n1", "7e1"))
    parser.add_argument("--wait", type=int, default=45, help="seconds to listen for boot")
    parser.add_argument(
        "--no-hybbx-ready",
        action="store_true",
        help="only detect boot banner, skip ESC V verify",
    )
    parser.add_argument(
        "--tx-rx",
        action="store_true",
        help="TX KISS + passive RX on same port before close (with power cycle)",
    )
    parser.add_argument(
        "--recover-only",
        action="store_true",
        help="skip boot listen; run software recovery ladder only (no power cycle)",
    )
    args = parser.parse_args()
    hybbx_ready = not args.no_hybbx_ready
    do_tx_rx = args.tx_rx

    if not os.access(args.device, os.R_OK | os.W_OK):
        print(f"FAIL: no access to {args.device}", file=sys.stderr)
        return 2

    fd = open_serial(args.device, args.baud, args.line)

    if args.recover_only:
        print(f"=== TNC recover-only @ {args.device} {args.baud} {args.line.upper()} ===")
        rec = load_recovery()
        if rec is None:
            print("FAIL: tnc_serial_recovery.py not found", file=sys.stderr)
            os.close(fd)
            return 2

        def wf(data: bytes) -> None:
            write_flush(fd, data)

        def rf(seconds: float) -> bytes:
            return read_for(fd, seconds)

        ok, rx = rec.recover_terminal(wf, rf, log=lambda m: print(f"  {m}"))
        if ok:
            show(rx[-800:] if len(rx) > 800 else rx)
            return finish_host(fd, hybbx_ready, do_tx_rx)
        os.close(fd)
        print("\nFAIL: software recovery — try power-cycle with DTR high (re-run without --recover-only)")
        return 1

    print(f"=== TNC boot-wait @ {args.device} {args.baud} {args.line.upper()} ===")
    print("DTR+RTS HIGH - power OFF the TNC now (10s), then power ON.")
    print("CB: squelch CLOSED (CD off) - otherwise TX/boot may be disturbed.")
    print(f"Listening {args.wait}s for boot banner ...\n")

    buf = read_for(fd, args.wait)

    if has_banner(buf):
        print("--- Boot banner (passive) ---")
        show(buf)
        return finish_host(fd, hybbx_ready, do_tx_rx)

    print(f"(no banner in {args.wait}s - trying KISS return + ESC V)\n")
    write_flush(fd, b"\xc0\xff\xc0")
    time.sleep(1.0)
    buf += read_for(fd, 2.5)
    write_flush(fd, b"\x1bV\r")
    time.sleep(0.4)
    info = read_for(fd, 4.0)
    buf += info

    print("--- KISS return + ESC V ---")
    show(info if info else buf[-500:])

    if has_banner(buf):
        return finish_host(fd, hybbx_ready, do_tx_rx)

    rec = load_recovery()
    if rec is not None:
        print("\n--- software recovery ladder ---")

        def wf(data: bytes) -> None:
            write_flush(fd, data)

        def rf(seconds: float) -> bytes:
            return read_for(fd, seconds)

        ok, rx = rec.recover_terminal(wf, rf, log=lambda m: print(f"  {m}"))
        buf += rx
        if ok:
            show(rx[-800:] if len(rx) > 800 else rx)
            return finish_host(fd, hybbx_ready, do_tx_rx)

    os.close(fd)
    print("\nDEGRADED: try ./tnc2c-host-reset.sh or power-cycle with this script running")
    return 1


if __name__ == "__main__":
    sys.exit(main())
git clone -b <branch> https://cgit.mode42.com/<repo>.git
git clone -b <branch> git://cgit.mode42.com/<repo>.git

info@mode42.com