Skip to main content

consortium_ipc/
connect.rs

1// Copyright 2026 Ethan Wu
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14//
15// SPDX-License-Identifier: Apache-2.0
16
17//! Readiness rendezvous with the remote end of a transport.
18//!
19//! [`Connect`] covers exactly one thing: **waiting until the far side of a
20//! link is ready to carry traffic.** A transport implements it when a peer
21//! must agree — on a memory layout, a protocol version, an MTU — before the
22//! first message can be sent.
23//!
24//! # What belongs here
25//!
26//! The defining property is that there is *another party*, and that the
27//! operation can fail because that party never showed up or disagreed. That is
28//! why [`connect`](Connect::connect) is async: the wait is genuine, not
29//! incidental. Concretely:
30//!
31//! - the shared-memory transport, where the primary publishes a descriptor and
32//!   the secondary validates and acks it;
33//! - a hypothetical link needing PHY training or a name-service announce —
34//!   still a rendezvous, even though the "peer" is hardware.
35//!
36//! # What does not belong here
37//!
38//! Local bring-up is *not* a rendezvous, and forcing it through this trait
39//! would make simple register writes pretend to await something:
40//!
41//! - **Peripherals.** Enabling a clock, muxing pins and programming registers
42//!   has no peer and no protocol. Its real complexity is the ordering graph
43//!   between peripherals, which a per-object method cannot express; in this
44//!   workspace that sequencing is emitted by `consortium-cfg` into the
45//!   generated `init()`. Peripheral drivers should keep the ecosystem
46//!   convention instead: constructor plus config struct, with typestate where
47//!   readiness is worth encoding in the type.
48//! - **Doorbells.** They have local setup (unmasking an interrupt, binding to
49//!   the Linux UIO fan-out) but no handshake, which is why that stays plain
50//!   inherent API (`bind_notifier` / `bind_service`).
51//!
52//! Waiting on *someone else* is `Connect`; configuring *your own* silicon is a
53//! constructor.
54//!
55//! # Contract
56//!
57//! - Call `connect` **once**, after construction and before any
58//!   [`send`](crate::SendTransport::send) or [`recv`](crate::RecvTransport::recv),
59//!   and before splitting a full-duplex transport into halves — the handshake
60//!   is a property of the whole link, not of one direction.
61//! - Implementations are side-asymmetric where the protocol is: the two ends of
62//!   one link may run different code, selected by the transport's type (e.g. its
63//!   [`Side`](crate::Side)), not by a runtime branch in the caller.
64//! - `connect` carries **no timeout**. A peer that never answers parks the
65//!   caller forever, so callers that need bounded bring-up must impose their own
66//!   deadline (`tokio::time::timeout`, `embassy_time::with_timeout`).
67//! - `connect` is safe. Transports whose construction is `unsafe` (raw shared
68//!   memory, say) discharge that obligation in their constructor; `connect`
69//!   then relies on the invariant already established there.
70
71use crate::future::MaybeSend;
72use crate::transport::TransportError;
73
74/// A transport that must rendezvous with its remote peer before carrying
75/// traffic.
76///
77/// The trait covers exactly one thing: **waiting until the far side of a link is
78/// ready.** Implement it when a peer must agree — on a memory layout, a protocol
79/// version, an MTU — before the first message can be sent. The defining property
80/// is that there is *another party*, and that the operation can fail because that
81/// party never showed up or disagreed. That is why `connect` is async: the wait is
82/// genuine, not incidental.
83///
84/// # Contract
85///
86/// - Call [`connect`](Connect::connect) **once**, after construction and before any
87///   [`send`](crate::SendTransport::send) or [`recv`](crate::RecvTransport::recv),
88///   and before splitting a full-duplex transport into halves — the handshake is a
89///   property of the whole link, not of one direction.
90/// - Implementations are side-asymmetric where the protocol is. The two ends of one
91///   link may run different code, selected by the transport's type (typically its
92///   [`Side`](crate::Side)) rather than by a runtime branch in the caller. Generated
93///   bring-up code can then emit one `connect` call for both sides.
94/// - `connect` carries **no timeout**. A peer that never answers parks the caller
95///   forever, so callers needing bounded bring-up impose their own deadline
96///   (`tokio::time::timeout`, `embassy_time::with_timeout`).
97/// - `connect` is safe. A transport whose construction is `unsafe` (raw shared
98///   memory, say) discharges that obligation in its constructor; `connect` relies on
99///   the invariant already established there.
100///
101/// # What does not belong here
102///
103/// Local bring-up is not a rendezvous, and routing it through this trait would make
104/// plain register writes pretend to await something.
105///
106/// - **Peripherals.** Enabling a clock, muxing pins and programming registers has no
107///   peer and no protocol. Its real complexity is the ordering graph between
108///   peripherals, which a per-object method cannot express; in this workspace that
109///   sequencing is emitted by `consortium-cfg` into the generated `init()`.
110///   Peripheral drivers keep the ecosystem convention instead: constructor plus
111///   config struct, with typestate where readiness is worth encoding in the type.
112/// - **Doorbells.** They have local setup (unmasking an interrupt, binding to the
113///   Linux UIO fan-out) but no handshake, which is why that stays plain inherent API
114///   (`bind_notifier` / `bind_service`).
115///
116/// Waiting on *someone else* is `Connect`; configuring *your own* silicon is a
117/// constructor.
118///
119/// # Example
120///
121/// The uniform shape lets bring-up code drive any link the same way:
122///
123/// ```ignore
124/// async fn bring_up<T: Connect>(transport: &mut T, params: T::Params) -> Result<(), T::Error> {
125///     transport.connect(params).await
126/// }
127/// ```
128pub trait Connect: TransportError {
129    /// Handshake inputs that are negotiated rather than owned by the transport.
130    ///
131    /// Anything that identifies the link — channel ids, the doorbell, the
132    /// region base — belongs in the constructor. What lands here is what the
133    /// two ends must *agree on*, and whose agreed value the transport learns
134    /// only by completing the handshake (for shared memory: the channel count
135    /// and the proposed MTU). Transports with nothing to negotiate use `()`.
136    type Params;
137
138    /// Run the readiness handshake with the remote end.
139    ///
140    /// Resolves once the peer is ready; the transport may record what the
141    /// handshake settled (a negotiated MTU, say) before returning. Call once,
142    /// before any traffic and before splitting into halves.
143    fn connect(
144        &mut self,
145        params: Self::Params,
146    ) -> impl Future<Output = Result<(), Self::Error>> + MaybeSend;
147}