diff --git a/courier_emu/bridge.py b/courier_emu/bridge.py index b3018c6..1a5370e 100644 --- a/courier_emu/bridge.py +++ b/courier_emu/bridge.py @@ -524,6 +524,10 @@ class CourierDspBridge: "budget_frames": 0, "tx_peak_codec": 0, "tx_consumed": 0, + "ring_frames": 0, + "peer_ring_frames": 0, + "peer_ring_bursts": 0, + "peer_ring_runs": [], "tx_index": 0, "tx_peak_line": 0, } @@ -676,6 +680,22 @@ class CourierDspBridge: # with no exchange on this path, the digits on the wire are the only # signal that the firmware has started dialing. self._line_dtmf = DtmfDecoder(sample_rate=LINE_RATE) + self._last_digit_at: int | None = None + self._peer_ring_last = False + self._peer_ring_runs: list[int] = [] + # What the called subscriber's bell does once the switch has the + # number. `LineExchange` already models the sequence - collect until + # the interdigit timeout, spend `routing_ms` setting the call up, then + # ring - and its own defaults are the one source for the timings. + # + # It has to be a cadence, not a level. The frame below used to assert + # ringing for as long as the seizure was a dialing one, which is true + # for the rest of the call, and `machine.py` drives the ring detect bit + # on port 0x14 straight from it: the answer machine at 0x70fb4 waits on + # an edge in every state, so a bit that goes high and stays high parks + # it in its first state exactly as a floating one does. + self._called_ring = RingSource( + start_ms=LineExchange.interdigit_ms + LineExchange.routing_ms) # Band-limited rather than a hold: the modem's transmit is tones and # modulation, and the images a hold leaves at 6:5 land inside the # band the far end demodulates in. @@ -2871,6 +2891,16 @@ class CourierDspBridge: self._line_service["drained_frames"] += 1 self._service_line_frame() + def _called_party_ringing(self) -> bool: + """Whether the called subscriber's bell is ringing this frame. + + Measured from the last digit the line carried, so it follows the + number actually dialed rather than anything read out of the firmware. + """ + if self._last_digit_at is None: + return False + return self._called_ring.present(self._instructions - self._last_digit_at) + def _service_line_frame(self) -> None: """One socket line frame: ship what the codec clocked, take the peer's.""" off_hook = self.daa is not None and self.daa.off_hook @@ -2882,6 +2912,19 @@ class CourierDspBridge: # callback transition temporarily leaves the datapump without a # complete fresh block. samples.extend([0] * (LINE_FRAME_SAMPLES - len(samples))) + ringing = (off_hook and not self.line.peer_off_hook + and self._called_party_ringing()) + if ringing: + self._line_service["ring_frames"] += 1 + if self.line.peer_ringing: + self._line_service["peer_ring_frames"] += 1 + if not self._peer_ring_last: + self._line_service["peer_ring_bursts"] += 1 + self._peer_ring_runs.append(1) + elif self._peer_ring_runs: + self._peer_ring_runs[-1] += 1 + self._peer_ring_last = bool(self.line.peer_ringing) + self._line_service["peer_ring_runs"] = list(self._peer_ring_runs)[:12] self.line.exchange( LineFrame( instructions=self.line.frames * LINE_FRAME_INSTRUCTIONS, @@ -2889,8 +2932,9 @@ class CourierDspBridge: # A dialing seizure supplies the network-side ring cadence # to the far subscriber. This is call progress, not modem # carrier audio; the far DAA still has to qualify it. - ringing=off_hook and self.daa is not None - and self.daa.operation in ("dialing", "trying"), + # The far end going off hook trips the ring, as it does on a + # switch, so the bell stops the moment the call is answered. + ringing=ringing, samples=samples, ) ) @@ -2945,6 +2989,9 @@ class CourierDspBridge: digits = self._line_dtmf.feed(samples) if digits: self.dial_digits += digits + # Routing waits this long after the *last* digit for one + # more, so every digit pushes the ring back. + self._last_digit_at = self._instructions self.begin_dialing() if self.daa.operation == "originate": self.daa.line_state = "dial-tone" diff --git a/courier_emu/cli.py b/courier_emu/cli.py index 0b67bf9..d49eda9 100644 --- a/courier_emu/cli.py +++ b/courier_emu/cli.py @@ -335,6 +335,8 @@ def _worker_command(args: argparse.Namespace) -> list[str]: serial_input += b"".join(value.encode("ascii") + b"\r" for value in args.at) if serial_input: command.extend(("--serial-input-hex", serial_input.hex())) + if getattr(args, "serial_input_on_ring", False): + command.append("--serial-input-on-ring") return command @@ -423,6 +425,13 @@ def _link_side(args: argparse.Namespace, commands: list[str], listen: bool) -> l # correctly strapped the ends in opposite roles. for text in commands or ["AT&L1"]: command.extend(("--at", text)) + # The answering end waits to be offered the call. Its commands are held + # until the ring detector reads high, which is the order a real answering + # modem sees: the switch rings it, then the host answers. Typing ATA into + # a line nobody has called put the answer tone on the wire before the + # originator had finished dialing, and it was never repeated. + if not listen and args.answer_on_ring: + command.append("--serial-input-on-ring") return command @@ -1278,6 +1287,12 @@ def build_parser() -> argparse.ArgumentParser: "idsdl403 seeds the board's own +S block at the offset 7.4.16 reads " "it from, and nothing else", ) + run.add_argument( + "--serial-input-on-ring", + action="store_true", + help="hold the AT input until the line rings, the way a host answers " + "a call it has been offered rather than typing into a dead line", + ) run.add_argument( "--dsp-rx-pcm", metavar="PATH", @@ -1354,6 +1369,12 @@ def build_parser() -> argparse.ArgumentParser: metavar="COMMAND", help="AT command for side B; repeatable (default AT&L1)", ) + link.add_argument( + "--answer-on-ring", + action="store_true", + help="hold side B's AT commands until the line rings, so the " + "answering end is offered the call before it answers it", + ) link.add_argument( "--dip-preset", choices=sorted(DIP_PRESETS), diff --git a/courier_emu/machine.py b/courier_emu/machine.py index b223759..8238e26 100644 --- a/courier_emu/machine.py +++ b/courier_emu/machine.py @@ -458,6 +458,7 @@ class CourierMachine: dsp_rx_samples: list[int] | None = None, dsp_tx_pcm: str | None = None, serial_input: bytes = b"", + serial_input_on_ring: bool = False, daa: CourierDaa | None = None, ring: RingSource | None = None, codec: CodecBringUp | None = None, @@ -573,7 +574,15 @@ class CourierMachine: self.data_tx_bytes = 0 self.online_mode = False self.force_online = force_online - self.serial_rx: deque[int] = deque(serial_input) + # A DTE does not type ATA into a line that is not ringing. Holding the + # commands back until the ring detector reads high models the host + # answering an offered call, which is the only order in which an + # answering modem ever sees one. + self._serial_input_on_ring = bool(serial_input_on_ring) + self._serial_input_pending: deque[int] = deque( + serial_input if self._serial_input_on_ring else b"") + self.serial_rx: deque[int] = deque( + b"" if self._serial_input_on_ring else serial_input) self._alternate_line = bytearray() self._rom_command_line = bytearray() self.console = console @@ -2313,6 +2322,12 @@ class CourierMachine: or (self.ring is not None and self.ring.present(self.instructions)) ): value |= RING_DETECT_BIT + if self._serial_input_pending: + # The line is ringing: the host may answer now. + self.serial_rx.extend(self._serial_input_pending) + self._serial_input_pending.clear() + self._terminal_connected = True + self._trace_serial("ring: released held AT input") else: value &= ~RING_DETECT_BIT if self.nvram is not None and port == 0x10 and size == 1: diff --git a/courier_emu/worker.py b/courier_emu/worker.py index c8b5b2d..7d78ea5 100644 --- a/courier_emu/worker.py +++ b/courier_emu/worker.py @@ -104,6 +104,9 @@ def main() -> int: parser.add_argument("--dsp-rx-pcm") parser.add_argument("--dsp-tx-pcm") parser.add_argument("--serial-input-hex", default="") + parser.add_argument( + "--serial-input-on-ring", action="store_true", + help="hold the AT input until the line rings, as a host answering a call does") parser.add_argument("--nvram") parser.add_argument("--nvram-fixture", choices=("idsdl302", "idsdl403")) parser.add_argument("--board-id", default="") @@ -254,6 +257,7 @@ def main() -> int: dsp_rx_samples=dsp_rx_samples, dsp_tx_pcm=args.dsp_tx_pcm, serial_input=bytes.fromhex(args.serial_input_hex), + serial_input_on_ring=args.serial_input_on_ring, daa=CourierDaa(args.daa_line) if args.daa_line else None, ring=ring, codec=codec,