Phone <-> bridge protocol, version 1
===================================

The bridge (bridge\signal_bridge.py) sits between signal-cli and the Nokia 9500.
The phone talks to it over one TCP connection (default port 7700).


1. Handshake
------------
Both sides know the same 32-byte pre-shared key PSK (signal.ini on the phone,
signal_key.txt next to the bridge, 64 hex digits).

  phone  -> bridge : "SIG1" + Np (12 bytes, unique per connection)
  bridge -> phone  : "SIG1" + Nb (12 bytes, random)

  K1 = first 32 bytes of ChaCha20 block(key = PSK, counter = 0, nonce = Np)
  Ks = first 32 bytes of ChaCha20 block(key = K1,  counter = 0, nonce = Nb)

Ks is the session key. A fresh Nb from the bridge means frames recorded from an
old session can't be replayed to the bridge; Np does the same for the phone.


2. Frames
---------
  u16 big-endian  L        (= payload length + 16)
  L-16 bytes      ciphertext
  16 bytes        Poly1305 tag

AEAD is ChaCha20-Poly1305 (RFC 8439) with key Ks and empty AAD.
Nonce = 4-byte direction (00 00 00 00 phone->bridge, 00 00 00 01 bridge->phone)
        + 8-byte big-endian frame counter, starting from 0 in each direction.

A frame that fails the tag check closes the connection. Payload is at most
16000 bytes. The phone's first frame must be HELLO.


3. Payload
----------
One command per frame: UTF-8 text, fields separated by TAB. Inside a field
"\\" means a backslash, "\t" a tab and "\n" a newline. Text the bridge sends
has emoji etc. already turned into plain text (":)", "[kissa]") so the
9500's fonts can show it: everything is within U+0000-U+024F plus common
punctuation (U+2010-U+2026, U+20AC).

Chat ids are small decimal integers assigned by the bridge.
"when" is a display string in the bridge's local time: "14:05", or
"12.10. 14:05" for older days.

Phone -> bridge
  HELLO  <proto=1> <client name>
  CHATS                                list all chats, newest first
  HIST   <chat> <max>                  last <max> messages of a chat
  SEND   <chat> <ref> <text>           <ref> is the phone's own number
  READ   <chat>                        mark read (sends read receipts)
  PING

Bridge -> phone
  WELCOME <proto> <account>
  CHAT  <chat> <unread> <when> <name> <preview> <secs>
                                        reply to CHATS (then "END CHATS"),
                                        also pushed whenever a chat changes;
                                        <secs> = time of the last message in
                                        Unix seconds, for sorting the list
  MSG   <chat> <id> <when> <dir> <sender> <text>
                                        dir is "i" (incoming) or "o" (outgoing);
                                        reply to HIST, oldest first
                                        (then "END HIST <chat>"),
                                        also pushed for every new message
  SENT  <ref> <id> <when>
  FAIL  <ref> <reason>
  PONG
  ERR   <text>
