← All posts

ModemBridge: one modem, multiple payphone protocols

ModemBridge logo: an analog wave becoming digital packets across a bridge icon, beside the ModemBridge wordmark.

Millennium and Jatena payphones both phone home through a modem, and each has its own backend server. Run both and you'd normally want a modem and a phone line per backend, which gets out of hand quickly.

ModemBridge is a small Go service that sits in between. It answers the call on a physical modem, works out which phone is calling, and pipes the raw bytes to the right TCP backend.

Payphone → telephone line → modem → ModemBridge → server of your choice

Why a hardware modem at all

Softmodems exist, and some of the recent LLM-written ones support even more protocols (having a Bell 212A softmodem seemed like mission impossible just a few years ago!). But a softmodem over SIP/VoIP will never be as reliable as a modem over POTS, especially if you have an EWSD DLU (such as the one provided by C3ISDN at Congress) to carry your traffic. :)

Detecting the phone without talking to it

The bridge is a purely passive bystander. After CONNECT it buffers the first bytes the phone sends and asks each enabled detector whether those bytes look like a valid opening frame.

Millennium frames start with 02, end with 03, and carry a CRC16 just before the end byte. A random byte stream doesn't pass that by accident. Jatena frames start with 01 and end with 03, and the check is the phone's own FCC checksum, summed over the bytes between start and checksum.

The Jatena detector has one quirk. The original OMC echoes an 07 identification frame before any application data, so the detector accepts 05, 06 and 07 as the control byte. Otherwise routing would wait for an ACK that only the backend can send, and the backend isn't connected yet.

If two detectors match, the call is rejected as ambiguous. If nothing matches within ten seconds, it's rejected too. Whatever the bridge buffered, noise included, goes to the backend untouched once a route is chosen, so the backend sees exactly what the phone sent.

What the backend sees

Two transports are enabled for now. raw is a plain TCP byte stream, one connection per call, which is what the existing mm_manager and Jatena server expect. enriched is a tiny framed protocol: a one-byte type, a four-byte big-endian length, then a payload. It gives the backend a JSON START message with the session ID and the modem's CONNECT string, and lets it send HANGUP to drop the line.

Neither transport has authentication or encryption. Keep it on loopback or tunnel it.

Hanging up properly

The call lifecycle needed as much care as the detection. The bridge watches DCD, drops DTR to hang up, and enforces limits: 120 seconds idle, 30 minutes per session, a five-second write deadline on the backend socket. A call that dies on either side shouldn't leave the modem off-hook for the next phone, so the bridge recovers the modem (two seconds by default) before it answers again.

The tests run against a simulated modem, so go test -race ./... needs no hardware. The code also cross-compiles for amd64, arm64 and armv7, which matters if the bridge lives on a Raspberry Pi next to the modem.

Using it

Adding a new protocol means writing one detector function and registering it, as long as the phone speaks first. If the bridge has to transmit something before the phone says anything, that needs more work than a detector. A possible next step is support for server-initiated calls, the way Elcotel, Protel and some other payphone manufacturers do it.

It's GPLv3 and the code is at github.com/vekejsn/modembridge.