mirror of
https://github.com/D-Modem/app.git
synced 2026-11-28 17:38:05 -08:00
A new line, "A phone line, through a USB modem": the phone runs the modem, a Smart Link SmartUSB56 (0483:7554) or a pico-serial Line stick (1209:0002) plugged into it carries its samples to a real phone line, with D-Modem-RE's usb_line.cpp as dmodemd --usb drives them (the stick's caller ID and the country's DAA settings too). - libusb 1.0.27 (D-Modem-RE's pinned release) built for Android (usb/libusb.cmake); usb_line.cpp built with libusb_init, libusb_get_device_list and libusb_open renamed to usb_android.cpp's, which start libusb without device discovery and give it the descriptor Android opened for the app, wrapped, as the only device. - usb_modem.cpp: the core on that line, the run loop of dmodemd's run_line() with a stop pipe and the engine's status; Engine::Config usb_fd and usb_rate, through JNI. - UsbModems: the devices by their IDs, Android's permission asked when the modem starts, the connection held while it runs; ModemSettings LINE_USB (no SIP account, not a peer line); the settings, Home and the notifications say so; Android offers the app when one is plugged in. - native-test: usb_host (the engine on D-Modem-RE's fake stick, linked in as libusb) and usb_line_test.py: two of them through its simulated exchange: caller ID, CONNECT 33600, data each way, hang up, unplugged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0172Hejd12i4aYikYW9vgpZs
151 lines
5.4 KiB
Python
151 lines
5.4 KiB
Python
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
# Copyright (C) 2026 Neo-Desktop
|
|
#
|
|
# usb_line_test.py <usb_host> <line_exchange>: the app's engine on a USB
|
|
# modem's line (usb_modem.h), two of them (usb_host) on D-Modem-RE's fake
|
|
# pico-serial Line sticks whose lines are the simulated exchange's, 1001
|
|
# and 1002 (as D-Modem-RE's pico_line_test.py runs dmodemd --usb):
|
|
#
|
|
# - each says it's on a pico-serial Line (ATI3);
|
|
# - 1001 dials 1002 (ATX4: after the dial tone, in DTMF); 1002 rings,
|
|
# shows the caller ID (AT#CID=1), answers at the second ring (ATS0=2);
|
|
# both CONNECT, and the engine's status says online; a line of text
|
|
# goes each way; 1001 hangs up (+++ ATH), 1002 NO CARRIER;
|
|
# - the exchange gone (the sticks unplugged): both engines stop with an
|
|
# error (EXIT -1), as the app's modem does when the stick is pulled.
|
|
import os, re, shutil, subprocess, sys, tempfile, threading, time
|
|
|
|
host, exchange = sys.argv[1], sys.argv[2]
|
|
rate = "9600"
|
|
tmp = tempfile.mkdtemp(prefix="dmodem-usb-line-")
|
|
|
|
procs = []
|
|
events = []
|
|
ex = subprocess.Popen([exchange, rate, tmp, "1001", "1002"], stdin=subprocess.PIPE,
|
|
stdout=subprocess.PIPE, text=True)
|
|
procs.append(ex)
|
|
threading.Thread(target=lambda: [events.append(l.strip()) for l in ex.stdout], daemon=True).start()
|
|
t0 = time.time()
|
|
while "ready" not in events and time.time() - t0 < 5:
|
|
time.sleep(0.05)
|
|
|
|
|
|
class Modem:
|
|
"""the engine (usb_host) on line <n>'s fake stick: its DTE, its log"""
|
|
def __init__(self, n):
|
|
self.n = n
|
|
env = dict(os.environ, FAKE_PICO_LINE_SOCKET=os.path.join(tmp, n + ".sock"))
|
|
self.p = subprocess.Popen([host, rate], stdin=subprocess.PIPE, stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE, env=env)
|
|
procs.append(self.p)
|
|
self.buf = b""
|
|
self.log = []
|
|
self.lock = threading.Lock()
|
|
threading.Thread(target=self._dte, daemon=True).start()
|
|
threading.Thread(target=self._log, daemon=True).start()
|
|
|
|
def _dte(self):
|
|
while True:
|
|
d = os.read(self.p.stdout.fileno(), 4096)
|
|
if not d:
|
|
return
|
|
with self.lock:
|
|
self.buf += d
|
|
|
|
def _log(self):
|
|
for l in self.p.stderr:
|
|
self.log.append(l.decode(errors="replace").rstrip())
|
|
|
|
def write(self, b):
|
|
self.p.stdin.write(b)
|
|
self.p.stdin.flush()
|
|
|
|
def expect(self, pat, t):
|
|
end = time.time() + t
|
|
while time.time() < end:
|
|
with self.lock:
|
|
m = re.search(pat, self.buf)
|
|
if m:
|
|
self.buf = self.buf[m.end():]
|
|
return m
|
|
time.sleep(0.05)
|
|
return None
|
|
|
|
def cmd(self, c, pat=rb"OK\r\n", t=5):
|
|
self.write(c + b"\r")
|
|
return self.expect(pat, t)
|
|
|
|
def logged(self, pat, t):
|
|
end = time.time() + t
|
|
while time.time() < end:
|
|
if any(re.search(pat, l) for l in list(self.log)):
|
|
return True
|
|
time.sleep(0.1)
|
|
return False
|
|
|
|
|
|
ok = True
|
|
def check(what, cond):
|
|
global ok
|
|
print("%s: %s" % (what, "yes" if cond else "NO"))
|
|
ok = ok and bool(cond)
|
|
|
|
|
|
try:
|
|
a = Modem("1001")
|
|
b = Modem("1002")
|
|
for m in (a, b):
|
|
check("%s ATE0" % m.n, m.cmd(b"ATE0", t=15))
|
|
check("%s ATI3: pico-serial Line" % m.n,
|
|
m.cmd(b"ATI3", rb"pico-serial Line \(Si3056\)\r\n(\r\n)?OK\r\n"))
|
|
check("1002 answers at the 2nd ring", b.cmd(b"ATS0=2"))
|
|
check("1002 caller ID on", b.cmd(b"AT#CID=1"))
|
|
check("1001 waits for the dial tone", a.cmd(b"ATX4"))
|
|
|
|
t = time.time()
|
|
a.write(b"ATD1002\r")
|
|
check("1002 RING", b.expect(rb"RING\r\n", 15))
|
|
cid = b.expect(rb"NMBR = (\d+)\r\n", 6)
|
|
check("1002 caller ID 1001", cid and cid.group(1) == b"1001")
|
|
ca = a.expect(rb"(CONNECT[^\r]*|NO CARRIER|BUSY|NO DIALTONE)\r\n", 50)
|
|
cb = b.expect(rb"(CONNECT[^\r]*|NO CARRIER)\r\n", 10)
|
|
print("1001: %s, 1002: %s, %.1f s" % (ca and ca.group(1).decode(),
|
|
cb and cb.group(1).decode(), time.time() - t))
|
|
check("both CONNECT", ca and cb and ca.group(1).startswith(b"CONNECT")
|
|
and cb.group(1).startswith(b"CONNECT"))
|
|
check("the exchange heard 1002 dialled", "1001 dialled 1002" in events)
|
|
check("1001's status: online, off hook", a.logged(r"^STATUS 2 1 [1-9]\d* [1-9]\d*$", 5))
|
|
a.write(b"hello from 1001\r\n")
|
|
check("1002 gets 1001's line", b.expect(rb"hello from 1001", 10))
|
|
b.write(b"hello from 1002\r\n")
|
|
check("1001 gets 1002's line", a.expect(rb"hello from 1002", 10))
|
|
time.sleep(1.2)
|
|
a.write(b"+++")
|
|
check("1001 +++ OK", a.expect(rb"OK\r\n", 5))
|
|
check("1001 ATH", a.cmd(b"ATH"))
|
|
check("1002 NO CARRIER", b.expect(rb"NO CARRIER\r\n", 30))
|
|
check("1001's status: idle, on hook", a.logged(r"^STATUS 0 0 0 0$", 5))
|
|
|
|
ex.terminate()
|
|
ex.wait(5)
|
|
for m in (a, b):
|
|
check("%s stops once unplugged (EXIT -1)" % m.n, m.logged(r"^EXIT -1$", 15))
|
|
finally:
|
|
for p in reversed(procs):
|
|
if p.poll() is None:
|
|
p.terminate()
|
|
for p in procs:
|
|
try:
|
|
p.wait(5)
|
|
except subprocess.TimeoutExpired:
|
|
p.kill()
|
|
if not ok:
|
|
for m in ("a", "b"):
|
|
if m in globals():
|
|
print("--- %s's log" % globals()[m].n)
|
|
print("\n".join(globals()[m].log[-60:]))
|
|
shutil.rmtree(tmp, ignore_errors=True)
|
|
print("PASS" if ok else "FAIL")
|
|
sys.exit(0 if ok else 1)
|