libadb 0.9.0

Low-level ADB protocol library for direct TCP and USB communication
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
use alloc::sync::Arc;
use core::convert::Infallible;
use core::future::{poll_fn, Future};
use core::pin::{pin, Pin};
use core::sync::atomic::Ordering;
use core::task::Poll;
use embedded_io::ErrorType;
use embedded_io_async::Write;
use event_listener::EventListener;

use super::shared::{CreditGuard, Owed, QueueGuard, Shared, SlotGuard};
use super::IncomingListener;
use crate::base::channel::{ChannelId, SelectResult};
use crate::base::connection::ConnectionConfig;
use crate::base::ids::ChannelIds;
use crate::base::negotiated::Negotiated;
use crate::base::protocol::command::Command;
use crate::base::slot::ChannelState;
use crate::base::wire::send_raw;
use crate::device_banner::DeviceBanner;
use crate::error::Error;

#[cfg(doc)]
use super::Reader;

/// Write half of a split [`Connection`](crate::Connection).
///
/// Cheaply cloneable (an `Arc` bump). Methods take `&self`, so a single
/// `Writer` can be shared across threads with no additional wrapping.
pub struct Writer<WH>
where
    WH: Write,
{
    pub(super) shared: Arc<Shared<WH>>,
}

impl<WH> Clone for Writer<WH>
where
    WH: Write,
{
    fn clone(&self) -> Self {
        Self {
            shared: Arc::clone(&self.shared),
        }
    }
}

impl<WH> Writer<WH>
where
    WH: Write,
{
    pub(crate) fn new(shared: Arc<Shared<WH>>) -> Self {
        Self { shared }
    }

    /// Maximum payload size for outbound packets.
    pub fn max_payload(&self) -> u32 {
        self.shared.negotiated.max_payload
    }

    /// Resource limits this connection was opened with.
    pub fn config(&self) -> &ConnectionConfig {
        &self.shared.negotiated.config
    }

    /// Whether delayed-ACK flow control was negotiated.
    pub fn delayed_ack(&self) -> bool {
        self.shared.negotiated.delayed_ack
    }

    /// Protocol version in effect: `min(ADB_VERSION, device version)`.
    pub fn protocol_version(&self) -> u32 {
        self.shared.negotiated.protocol_version
    }

    /// Raw device banner bytes from the CNXN handshake.
    ///
    /// Lock-free connection metadata — available even while the
    /// [`Reader`] is blocked in a read on another thread.
    pub fn device_banner(&self) -> &[u8] {
        self.shared.negotiated.device_banner.raw()
    }

    /// Parsed device banner from the CNXN handshake.
    ///
    /// Lock-free connection metadata — available even while the
    /// [`Reader`] is blocked in a read on another thread.
    pub fn device_banner_parsed(&self) -> &DeviceBanner {
        &self.shared.negotiated.device_banner
    }

    pub(crate) fn negotiated(&self) -> &Negotiated {
        &self.shared.negotiated
    }

    /// Take the channels the device opens toward the host (`adb
    /// reverse`) on a connection whose [`Reader::run`] dispatches
    /// packets. While the listener lives, `run` keeps them for its
    /// [`accept`](IncomingListener::accept) instead of refusing them.
    pub fn listen_incoming(&self) -> IncomingListener<WH> {
        self.shared.listeners.fetch_add(1, Ordering::AcqRel);
        IncomingListener {
            shared: Arc::clone(&self.shared),
        }
    }

    /// Open a channel to `destination` on a connection whose
    /// [`Reader::run`] dispatches packets.
    ///
    /// Sends OPEN and waits until the device accepts or refuses it;
    /// a refusal comes back as [`Error::ChannelClosed`].
    ///
    /// **Not cancellation-safe.** Dropping the future after OPEN went
    /// out gives the channel up before the device answers. Should it
    /// accept after all, the device gets a CLSE, at the latest with the
    /// next packet `run` receives or the next write, open or close on
    /// the connection. So the device's end does not stay open for long,
    /// unless the [`Reader`] is gone and nothing reads the READY.
    pub async fn open_channel(
        &self,
        destination: &[u8],
    ) -> Result<ChannelId, Error<<WH as ErrorType>::Error>> {
        self.shared.check_live()?;
        let (ch, mut guard) = self.shared.send_open(destination).await?;
        self.await_open_ack(ch, &mut guard).await
    }

    async fn await_open_ack(
        &self,
        ch: ChannelId,
        guard: &mut SlotGuard<WH>,
    ) -> Result<ChannelId, Error<<WH as ErrorType>::Error>> {
        let signal = &self.shared.signals[ch.slot];
        loop {
            self.shared.check_in_step()?;
            let listener = signal.listen();
            {
                let chs = self.shared.lock_channels();
                match chs.get(ch) {
                    Some(slot) => match slot.state() {
                        ChannelState::Opening => {}
                        ChannelState::Closed if slot.ids().is_none() => {
                            return Err(slot.closed_error())
                        }
                        ChannelState::Open | ChannelState::Closed => {
                            guard.disarm();
                            return Ok(ch);
                        }
                    },
                    None => return Err(Error::ChannelClosed),
                }
            }
            self.shared.check_live()?;
            listener.await;
        }
    }

    /// Read from channel `ch` into `buf` on a connection whose
    /// [`Reader::run`] dispatches packets. Returns the number of bytes
    /// read.
    ///
    /// Returns at once with whatever is buffered, otherwise waits for
    /// the next WRTE on `ch`. A channel the device closed fails with
    /// [`Error::ChannelClosed`] once its buffered data is read. For how
    /// a read fails on a failing connection, see
    /// [What arrived stays readable](crate::split#what-arrived-stays-readable).
    ///
    /// Cancellation-safe while it waits, for data or for the write half
    /// an OKAY needs: nothing has been taken then, and the data stays
    /// buffered for the next call. Dropping it while that OKAY is
    /// written has taken nothing either, but
    /// [abandons the packet](crate::split#dropping-a-call).
    pub async fn read_channel(
        &self,
        ch: ChannelId,
        buf: &mut [u8],
    ) -> Result<usize, Error<<WH as ErrorType>::Error>> {
        match self
            .select_channel(ch, buf, core::future::pending::<Infallible>())
            .await?
        {
            SelectResult::Data(n) => Ok(n),
            SelectResult::Interrupted(never) => match never {},
        }
    }

    /// [`read_channel`](Self::read_channel), returning early with
    /// [`SelectResult::Interrupted`] if `interrupt` resolves while it
    /// waits for data.
    ///
    /// Buffered data wins over a ready interrupt, and the interrupt is
    /// only raced against the wait, never against the acknowledgement
    /// a read may send, so an interrupted call has taken nothing from
    /// the channel.
    pub async fn select_channel<F: Future>(
        &self,
        ch: ChannelId,
        buf: &mut [u8],
        interrupt: F,
    ) -> Result<SelectResult<F::Output>, Error<<WH as ErrorType>::Error>> {
        let Some(signal) = self.shared.signals.get(ch.slot) else {
            return Err(Error::ChannelClosed);
        };
        let mut interrupt = pin!(interrupt);
        let mut listener: Option<EventListener> = None;
        loop {
            if let Some(n) = self.shared.take_buffered(ch, buf).await? {
                return Ok(SelectResult::Data(n));
            }
            self.shared.check_live()?;
            let Some(waiting) = listener.as_mut() else {
                listener = Some(signal.listen());
                continue;
            };
            let interrupted = poll_fn(|cx| {
                if let Poll::Ready(value) = interrupt.as_mut().poll(cx) {
                    return Poll::Ready(Some(value));
                }
                Pin::new(&mut *waiting).poll(cx).map(|()| None)
            })
            .await;
            listener = None;
            if let Some(value) = interrupted {
                return Ok(SelectResult::Interrupted(value));
            }
        }
    }

    /// Write `data` to the given channel.
    ///
    /// Splits `data` into chunks bounded by `max_payload` and the
    /// currently-granted send budget. Concurrent writers on the same
    /// channel never consume the same credit, and credit is granted in
    /// arrival order: a writer looping back for its next chunk queues
    /// behind the ones already waiting.
    ///
    /// The bytes of one call go out in order; concurrent calls on the
    /// same channel interleave at chunk granularity in unspecified
    /// order, like unsynchronized writes to one socket. A caller that
    /// needs whole messages on the wire serializes the calls itself,
    /// as the FFI shell does around each frame.
    ///
    /// **Not cancellation-safe**: see
    /// [Dropping a call](crate::split#dropping-a-call). Credit reserved
    /// and not yet sent is refunded when the future is dropped.
    pub async fn write_channel(
        &self,
        ch: ChannelId,
        data: &[u8],
    ) -> Result<(), Error<<WH as ErrorType>::Error>> {
        self.shared.desync.check()?;
        if data.is_empty() {
            return Ok(());
        }
        let max = (self.shared.negotiated.max_payload as usize).max(1);
        let checksum = self.shared.negotiated.checksum();

        let mut offset = 0;
        while offset < data.len() {
            let want = (data.len() - offset).min(max);
            let n = self.acquire_credit(ch, want).await?;
            let mut guard = CreditGuard::new(&self.shared, ch, n);

            let ids = self.channel_ids(ch)?;
            let chunk = &data[offset..offset + n];

            {
                let mut wh = self.shared.lock_write().await?;
                send_raw(
                    &mut *wh,
                    &self.shared.desync,
                    Command::Write,
                    ids,
                    chunk,
                    checksum,
                )
                .await?;
            }
            guard.commit();
            offset += n;
        }
        Ok(())
    }

    /// Whether the writers on `ch` have nothing left to do but wait for
    /// the device: one is parked for credit the channel does not have,
    /// and none is still sending.
    ///
    /// For a caller with no task to give [`Reader::run`], to decide when
    /// a pending [`write_channel`](Self::write_channel) needs packets
    /// taken off the connection with [`Reader::dispatch_one`]. That a
    /// write is pending says little: on USB one is pending through every
    /// transfer, and a read begun then could wait for a packet that
    /// never comes, since the device may owe nothing yet.
    ///
    /// A writer still sending answers `false` even with another parked
    /// behind it: its future has to be polled, not left for a read to
    /// finish.
    ///
    /// A closed or freed channel answers `false`: its writers have been
    /// woken to fail. A `true` lasts until the next packet is dispatched,
    /// so a caller about to read asks once it alone reads the connection.
    pub fn waits_for_credit(&self, ch: ChannelId) -> bool {
        let mut chs = self.shared.lock_channels();
        let Some(slot) = chs.get_mut(ch) else {
            return false;
        };
        if slot.is_closed() || slot.budget().is_some() {
            return false;
        }
        let queue = slot.send_queue();
        !queue.is_empty() && !queue.is_sending()
    }

    /// Release the channel slot and send CLSE. Any cloned `Writer`
    /// parked on this channel observes `ChannelClosed` and unblocks,
    /// whether or not the CLSE gets out.
    ///
    /// The CLSE is [owed](crate::split#what-the-device-is-owed) from the
    /// moment the slot goes. With the device gone or the stream over
    /// this returns `Ok` without it. A write that fails any other way
    /// comes back as the error, with the connection desynchronized. A
    /// connection already desynchronized fails with
    /// [`Error::Desynchronized`] and frees nothing: no call that wants a
    /// slot gets anywhere on it.
    ///
    /// Safe to drop while it waits for the write half: the next write,
    /// open, close or receive sends the CLSE. Dropping it during the
    /// write [abandons the packet](crate::split#dropping-a-call).
    ///
    /// A concurrent [`Reader::read_channel`] on the same channel is not
    /// woken: see
    /// [Closing while a read waits](crate::split#closing-while-a-read-waits).
    pub async fn close_channel(
        &self,
        ch: ChannelId,
    ) -> Result<(), Error<<WH as ErrorType>::Error>> {
        close_channel_via(&self.shared, ch).await
    }

    fn channel_ids(&self, ch: ChannelId) -> Result<ChannelIds, Error<<WH as ErrorType>::Error>> {
        self.shared.lock_channels().ids(ch)
    }

    async fn acquire_credit(
        &self,
        ch: ChannelId,
        want: usize,
    ) -> Result<usize, Error<<WH as ErrorType>::Error>> {
        let Some(signal) = self.shared.signals.get(ch.slot) else {
            return Err(Error::ChannelClosed);
        };

        let place = {
            let mut chs = self.shared.lock_channels();
            let Some(slot) = chs.get_mut(ch) else {
                return Err(Error::ChannelClosed);
            };
            if slot.is_closed() {
                return Err(slot.closed_error());
            }
            slot.send_queue().enqueue()
        };
        let mut queue = QueueGuard {
            shared: &self.shared,
            ch,
            place,
            served: false,
        };

        loop {
            let listener = signal.listen();
            match self.try_reserve(ch, want, place) {
                Some(Ok((n, more))) => {
                    queue.served = true;
                    if more {
                        signal.wake();
                    }
                    return Ok(n);
                }
                Some(Err(e)) => return Err(e),
                None => {
                    self.shared.check_live()?;
                    listener.await;
                }
            }
        }
    }

    #[allow(clippy::type_complexity)]
    fn try_reserve(
        &self,
        ch: ChannelId,
        want: usize,
        place: u64,
    ) -> Option<Result<(usize, bool), Error<<WH as ErrorType>::Error>>> {
        let mut chs = self.shared.lock_channels();
        let Some(slot) = chs.get_mut(ch) else {
            return Some(Err(Error::ChannelClosed));
        };
        if slot.is_closed() {
            return Some(Err(slot.closed_error()));
        }
        if slot.send_queue().turn() != place {
            return None;
        }
        slot.try_reserve_send(want).map(|n| {
            slot.send_queue().serve();
            Ok((n, slot.budget().is_some()))
        })
    }
}

pub(super) async fn close_channel_via<WH>(
    shared: &Shared<WH>,
    ch: ChannelId,
) -> Result<(), Error<<WH as ErrorType>::Error>>
where
    WH: Write,
{
    shared.desync.check()?;
    let ids = {
        let mut chs = shared.lock_channels();
        let ids = chs.ids(ch)?;
        chs.free(ch);
        ids
    };
    shared.signals[ch.slot].wake();
    shared.owe(Owed::Close(ids));
    match shared
        .until(Shared::clse_called_off, shared.lock_write())
        .await
    {
        Some(paid) => paid.map(drop),
        None => shared.check_close(),
    }
}