Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Add or change a protocol feature

This page takes a feature from a .proto message to a button in the desktop app. It follows one small feature that already exists: Ring phone (capability ring, message RingPhone). Open the files named in each step and copy the pattern.

A protocol change touches proto/, the Rust side and the Kotlin side in the same pull request, together with the reference pages that describe it.

Before you begin

The steps cover frames on the control connection between the phone app and the daemon. Messages between the daemon and the shell helper of the enhanced tier live in proto/tether/v1/shell.proto. They are outside Envelope and are negotiated through the abilities in ShellHello. The compatibility rules below apply to them as well.

Overview of the path

proto/tether/v1/misc.proto         message RingPhone
proto/tether/v1/envelope.proto     Envelope.msg, field 102
        |
        +-- desktop/crates/tether-proto    Rust types (prost, at build time)
        |     tether-core                  capability, gating, Core::ring_phone
        |     tetherd                      Features::ring_phone, D-Bus RingPhone
        |     tether-cli                   tether-cli ring <id>
        |     tether-gtk                   Backend::ring_phone, Overview row
        |
        +-- android/app                    Kotlin types (Wire, at build time)
              transport/Protocol.kt        capability "ring"
              features/misc/RingController.kt   handler

Write the contract

  1. Define the message in the .proto file of its feature area. RingPhone is in proto/tether/v1/misc.proto:

    // Desktop -> phone. start = true plays the alarm ringtone at full volume with
    // vibration and shows a full-screen stop activity; start = false stops it.
    message RingPhone {
      bool start = 1;
    }
    

    State the direction and the semantics in the comment. If you create a new .proto file, give it the same header as the others (package tether.v1, java_package = "org.tether.proto.v1", java_multiple_files = true), import it in envelope.proto and add it to the file list in desktop/crates/tether-proto/build.rs. Wire on the Android side picks up every file under proto/ by itself.

  2. Add the message to the oneof msg of Envelope in proto/tether/v1/envelope.proto. Field numbers are grouped in blocks of ten per feature area:

    NumbersArea
    10–13session (Hello, Ping, Pong, Disconnect)
    20–24pairing
    30–31device information and battery
    40–43notifications
    50–51blobs
    60StreamOpen
    70–77messages
    80–82contacts
    90–93media sessions and volume
    100–105Do Not Disturb, ring, connectivity, clipboard, share
    110–113files
    120–127photos
    130–137calls, call log, Bluetooth identity
    140–145enhanced tier, app side

    A message that belongs to an existing area takes the next free number in that block (RingPhone ring_phone = 102;). A new area starts the next free block.

  3. Choose the capability. A feature is active only when both sides list its capability in Hello.capabilities. Reuse the capability of the area when the new frame cannot be used without the existing ones. Add a new string when a peer could support one without the other (ring is separate from dnd). Sub-features use a dotted name: messages.mms, calls.dial.

  4. Describe the contract in docs/reference/protocol.md: a row in the “Capabilities” table and the behaviour in the section of the feature area (for ring: “DND, ring, connectivity”). Include limits, timeouts and what the receiver does when it cannot comply.

Implement the desktop side

Rust types are generated at build time by desktop/crates/tether-proto/build.rs with protox and prost-build. You do not need protoc. After cargo build the message is tether_proto::v1::RingPhone and the oneof case is tether_proto::v1::envelope::Msg::RingPhone.

  1. In desktop/crates/tether-core/:

    FileWhat to add
    src/lib.rsthe capability string in CAPABILITIES, which must match Protocol.kt on Android
    src/session.rsan arm in required_capability (Msg::RingPhone(_) => "ring") and the frame’s name for the log in msg_name
    src/device.rsfor a frame the peer sends to ask for something, a PeerRequest variant (PeerRequest::Ring). For state the peer reports, an Event variant
    src/core/mod.rsan arm in the dispatch match that turns the frame into the request or event
    src/core/feature_flow.rsa typed send helper on Core (Core::ring_phone), which calls Core::send
    tests/messages_contacts.rsa round trip between two in-process cores

    Core::send refuses a frame whose capability the peer did not announce (Error::Unsupported). Capabilities added in Milestone 3 and 4 are also checked on receipt (M3_CAPABILITIES, M4_CAPABILITIES in lib.rs, helpers in src/core/call_flow.rs and src/core/enhance_flow.rs). Do the same for a new capability.

    To try the feature without a phone, teach examples/fake_phone.rs to answer or print the frame.

  2. In desktop/crates/tetherd/, add the feature logic to the module of the feature area under src/features/. Each module is an impl Features block:

    • src/features/misc.rs has Features::ring_phone, which sends the frame through the core, keeps the ringing flag in MiscState and announces the property change.
    • Events that arrive from the phone are routed to the feature in Features::handle in src/features/mod.rs.
    • Data that must survive a restart goes into the per-device cache in src/db.rs.
  3. Expose it on D-Bus in desktop/crates/tetherd/src/dbus/device.rs (the org.tether.Device1 interface) or dbus/daemon.rs (org.tether.Daemon1). For ring this is the method ring_phone (RingPhone(b start) on the bus) and the property ringing (Ringing). Signals are emitted from the feature code with the device_signal! macro, property changes with device_props_changed! (both in src/features/mod.rs).

  4. Document every new method, property and signal in docs/reference/dbus-api.md, in the section of the capability. In the pull request, state who can call a new method and what it can do (see the hardening checklist).

  5. Add the command to tether-cli in desktop/crates/tether-cli/:

    • src/proxies.rs: the method on the org.tether.Device1 proxy trait (fn ring_phone(&self, start: bool) -> zbus::Result<()>;).
    • src/main.rs: a variant of the Cmd enum (Cmd::Ring) and its arm in the command match.

    Describe the command in the command-line reference.

  6. Add it to the desktop app in desktop/crates/tether-gtk/. The user interface talks to a Backend trait, which has a D-Bus implementation and an in-process fake:

    FileWhat to add
    src/backend/mod.rsthe method on the Backend trait (async fn ring_phone(&self, device_id: &str, start: bool) -> Result<()>;) and any new field of the device snapshot (ringing)
    src/backend/dbus.rsthe method on the zbus proxy trait and the Backend implementation that calls it
    src/backend/mock.rsthe same method on the fake daemon, with plausible behaviour, and the capability in its capability list
    src/widgets/…the widget. ring is a row on the Overview page, src/widgets/overview_page.rs

    Show the control only while the device is connected and has the capability (d.connected && d.has_capability("ring")).

    A method that is missing in the fake daemon breaks the mock build, which screenshots and user interface work depend on. CI does not build that feature, so check it yourself:

    cd desktop
    cargo clippy --workspace --all-targets --features tether-gtk/mock -- -D warnings
    cargo run -p tether-gtk --features mock -- --mock
    

Implement the Android side

Kotlin types are generated at build time by the Wire Gradle plugin from the same proto/ tree (wire { … } in android/app/build.gradle.kts). The classes are in the package org.tether.proto.v1. An Envelope has one nullable property per oneof case (envelope.ring_phone). All paths below are under android/app/src/main/java/org/tether/android/.

  1. Announce the capability in transport/Protocol.kt, function capabilities(). Add it unconditionally if the feature needs no permission (add("ring")), otherwise only while the permission is held and the user has the feature switched on:

    if (settings?.syncContacts != false && Permissions.contacts(context)) add("contacts")
    

    Permission checks live in core/Permissions.kt.

  2. Write the handler as a class that implements Feature (transport/Feature.kt). features/misc/RingController.kt is the model:

    override suspend fun onMessage(session: Session, envelope: Envelope): Boolean {
        val req = envelope.ring_phone ?: return false
        if (session.peer?.has("ring") != true) return true
        if (req.start) start() else stop()
        return true
    }
    

    Return false for frames that are not yours and true once the frame is consumed. Ignore the frame when the peer did not announce the capability. Use onPaired to push initial state after a connection is established (features/misc/DndFeature.kt does this for DndState).

  3. Register the feature in AppGraph.kt: a by lazy property and an entry in the features = listOf(…) argument of TetherEngine.

  4. Declare what the feature needs in android/app/src/main/AndroidManifest.xml. ring added VIBRATE, USE_FULL_SCREEN_INTENT and the activity RingActivity (android:exported="false"). Every new permission gets a comment in the manifest that says why it is there. If the user sees a prompt or has to change a setting, describe it in Grant Android permissions and list the permission in Android permissions and grants.

  5. Add unit tests under android/app/src/test/java/ for logic that does not need a device (parsing, paging, diffing, encoding).

Keep the shared test vectors green

The protocol reference contains byte-exact test vectors that both implementations check: the short authentication string (SAS, the six-digit pairing code), the QR proof and a framed StreamOpen.

VectorRust testKotlin test
SAS and QR proof (“Pairing”, “Test vectors”)desktop/crates/tether-core/src/pairing.rsandroid/app/src/test/java/org/tether/android/pairing/SasTest.kt, QrPairingUriTest.kt
StreamOpen frame (“Test vectors (M2)”)desktop/crates/tether-core/src/streams.rsandroid/app/src/test/java/org/tether/android/transport/StreamManagerTest.kt
  1. If you change pairing, framing or a message that has a vector, the vectors must still pass on both sides. If you add something whose bytes both sides must agree on (a derivation, a hash, a frame layout), add a vector to docs/reference/protocol.md and a test for it in both languages.

  2. Run the checks of both sides as described in Test your changes.

Update the project records

  1. Update, in the same pull request:

    • Feature table: add or flip the row of the feature (tier, milestone, status).
    • Roadmap: the status paragraph of the milestone, with what was and was not verified on a real phone.
    • CHANGELOG.md: a line under “Unreleased”, and whether the protocol change is additive.
    • The user guide, if a user can see the feature: the matching page under docs/how-to/.

Compatibility rules

  • Never renumber or reuse a field number, in Envelope or in any message.
  • When you remove a field, reserve its number (reserved 4;) so that it cannot be reused by accident.
  • Add fields and messages. Do not change existing ones. A new optional field or a new message with a capability is compatible. Changing the type or meaning of an existing field is incompatible.
  • Negotiate with capabilities. A peer that lacks a capability must never receive the frames it enables, and must behave as before. The sender checks the peer’s Hello.capabilities. The receiver ignores frames for capabilities that were not announced.
  • The phone announces a capability only while it holds the permission behind it. The list is fixed for the duration of a connection. The desktop treats Hello.capabilities as the truth for that session and hides controls for missing capabilities.
  • An incompatible change needs an architecture decision record and a bump of the protocol version: PROTOCOL_VERSION in desktop/crates/tether-proto/src/lib.rs and Protocol.VERSION in transport/Protocol.kt. Its commit carries ! and a BREAKING CHANGE: footer (see Submit a pull request).
  • Validate every length and index that comes off the wire. Frames are capped at 4 MiB and inline blobs at 256 KiB.
  • Protocol constants (ports, timeouts, limits) are defined once per side and match docs/reference/protocol.md: desktop/crates/tether-proto/src/lib.rs and the modules of tether-core in Rust, transport/Protocol.kt in Kotlin.

When you need an ADR

Write an architecture decision record before the code when the change is expensive to reverse:

  • the shape of the protocol: transport, framing, a new kind of connection, the pairing or trust model, an incompatible change,
  • cryptography,
  • a platform mechanism (a new Android service type, a hidden API, a kernel module, a new way to reach the phone),
  • a dependency on a system service on either side,
  • packaging or licence.

A message that is added to an existing feature area and follows a decision that is already recorded does not need a new record. Link the existing one in the pull request.