anybox-rs 0.10.0

one API for the most popular passthrough boxes
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
use std::sync::Arc;

use serde_json::Value;

use super::{KEYBOARD, Shared, lock};
use crate::backend::{Input, InputKind, Sink};
use crate::capability::{Action, Capability};
use crate::event::DropSource;
use crate::key::Key;
use crate::transport::codec::{self, Codec, Frame, Incoming};

const START: &[u8] = b"<START>";
const END: &[u8] = b"<END>";
const PROMPT: &[u8] = b">>>";
pub(super) const MAX_PAYLOAD: usize = 4096;
const MAX_FRAME: usize = MAX_PAYLOAD + 14;
const MAX_TEXT: usize = 1024;

pub(super) fn json(payload: &str) -> Option<Vec<u8>> {
    if payload.is_empty() || payload.len() > MAX_PAYLOAD {
        return None;
    }
    let mut bytes = START.to_vec();
    bytes.extend_from_slice(&u16::try_from(payload.len() + 14).ok()?.to_be_bytes());
    bytes.extend_from_slice(payload.as_bytes());
    bytes.extend_from_slice(END);
    Some(bytes)
}

pub(super) fn object(frame: &[u8]) -> Option<Value> {
    let length = usize::from(u16::from_be_bytes([*frame.get(7)?, *frame.get(8)?]));
    if !frame.starts_with(START)
        || !(15..=MAX_FRAME).contains(&length)
        || frame.len() != length
        || !frame.ends_with(END)
    {
        return None;
    }
    let value: Value = serde_json::from_slice(&frame[9..length - 5]).ok()?;
    value.is_object().then_some(value)
}

fn left(version: &str) -> bool {
    version
        .strip_prefix("ysc-towmcu-L")
        .is_some_and(|rest| rest.is_empty() || rest.starts_with(char::is_whitespace))
}

fn conflicting(value: &Value) -> bool {
    match value {
        Value::String(text) => text.contains("ysc-towmcu-R") || text.contains("IAP"),
        Value::Array(values) => values.iter().any(conflicting),
        Value::Object(values) => values.values().any(conflicting),
        _ => false,
    }
}

pub(super) fn version(frame: &[u8]) -> Option<String> {
    let value = object(frame)?;
    if value.get("code")?.as_u64()? != 200 || conflicting(&value) {
        return None;
    }
    // the vendor's driver takes it from `data` and then from `message`, whatever the message says
    ["data", "message"]
        .into_iter()
        .filter_map(|field| value.get(field)?.as_str())
        .find(|text| left(text))
        .map(str::to_owned)
}

/// the V1 app's cmd 132 answer, `{"code":200,"message":"version","data":"<build date>"}`. a V2 or bootloader answer
/// isn't one
pub(super) fn v1_version(frame: &[u8]) -> Option<String> {
    let value = object(frame)?;
    if value.get("code")?.as_u64()? != 200
        || conflicting(&value)
        || value.get("message")?.as_str()? != "version"
    {
        return None;
    }
    let date = value.get("data")?.as_str()?;
    (!date.is_empty() && !date.starts_with("ysc-towmcu")).then(|| date.to_owned())
}

/// the bootloader answers cmd 132 with `YSC-IAP` on V1 and V2
pub(super) fn bootloader(frame: &[u8]) -> bool {
    object(frame).is_some_and(|value| {
        value.get("code").and_then(Value::as_u64) == Some(200) && names_iap(&value)
    })
}

fn names_iap(value: &Value) -> bool {
    match value {
        Value::String(text) => text.contains("-IAP"),
        Value::Array(values) => values.iter().any(names_iap),
        Value::Object(values) => values.values().any(names_iap),
        _ => false,
    }
}

fn data(value: &Value) -> Option<Value> {
    match value.get("data")? {
        Value::Object(_) => Some(value["data"].clone()),
        Value::String(text) if text.len() <= MAX_PAYLOAD => {
            let nested: Value = serde_json::from_str(text).ok()?;
            nested.is_object().then_some(nested)
        }
        _ => None,
    }
}

pub(super) fn debug(frame: &[u8]) -> Option<bool> {
    let value = object(frame)?;
    if value.get("code")?.as_u64()? != 702 {
        return None;
    }
    data(&value)?.get("debug")?.as_bool()
}

/// a V1 sends its mouse reports with a null message, the vendor's driver also takes them with none at all, from older
/// firmware, and named `mouse`
fn mouse(message: Option<&Value>) -> bool {
    message.is_none_or(|message| message.is_null() || message == "mouse")
}

/// the keys a keyboard report says are down, and whether more are down than it names. the box sends
/// `{"code":200,"message":"kbd","data":"{\"mod\":2,\"n\":1,\"ov\":0,\"k\":[4],...}"}` by itself, with the modifiers
/// from 0xE0 up in `mod`, the other keys in the first `n` of `k`, at most 6 and in ascending order, and how many more
/// it couldn't name in `ov`. the shape is what a V1 sent, what the fields mean comes from the vendor's driver
fn keyboard(value: &Value) -> Option<(Vec<Key>, bool)> {
    let value = data(value)?;
    let modifiers = u8::try_from(value.get("mod")?.as_u64()?).ok()?;
    let named = usize::try_from(value.get("n")?.as_u64()?).ok()?;
    let overflow = value.get("ov")?.as_u64()? > 0;
    let listed = value.get("k")?.as_array()?;
    let mut keys: Vec<Key> = (0..8u8)
        .filter(|bit| modifiers & (1 << bit) != 0)
        .map(|bit| Key(0xE0 + bit))
        .collect();
    for key in listed.iter().take(named) {
        let key = Key(u8::try_from(key.as_u64()?).ok()?);
        if key.is_valid() && !keys.contains(&key) {
            keys.push(key);
        }
    }
    Some((keys, overflow))
}

/// every reply code the vendor documents or its app reads, and anything with a `cmd`, which is a command going to
/// the box
fn known_reply(value: &Value) -> bool {
    value.get("cmd").is_some()
        || value
            .get("code")
            .and_then(Value::as_u64)
            .is_some_and(|code| matches!(code, 200 | 404 | 100..=104 | 700..=715))
}

/// what a cmd 45 waits for. the answer doesn't name the key, which is fine because only one goes out at a time
pub(super) fn key_reply() -> codec::ReplyKey {
    key(b"cmd 45")
}

pub(super) fn key(command: &[u8]) -> codec::ReplyKey {
    // a collision only matters between commands in flight, so the requests are serialized
    command.iter().fold(0x811c_9dc5, |hash: u32, &byte| {
        (hash ^ u32::from(byte)).wrapping_mul(0x0100_0193)
    })
}

#[derive(Default)]
pub(super) struct Seen {
    line: Vec<u8>,
    reply: Vec<u8>,
    prompt_space: bool,
    pub(super) buttons: Option<u8>,
    /// the keys down on the keyboard behind the box, by its last report
    keys: Vec<Key>,
}

pub(super) struct YscFrames {
    shared: Arc<Shared>,
    /// none while probing a bare port, where nobody listens yet. it only queues, so the reader never waits on
    /// the core, and the reader is also what reports Disconnected, so these always land before it
    sink: Option<Sink>,
}

impl YscFrames {
    pub(super) fn new(shared: Arc<Shared>, sink: Option<Sink>) -> Self {
        Self { shared, sink }
    }

    pub(super) fn probe() -> Self {
        Self::new(
            Arc::new(Shared::new(super::snapshot(
                String::new(),
                super::Variant::V2,
            ))),
            None,
        )
    }

    /// malformed input proves at least one report got lost on the PC side, not how many the box sent
    fn lost(&self) {
        if let Some(sink) = &self.sink {
            sink.dropped(1, DropSource::Host);
        }
    }

    /// the box streams `{"code":200,"message":null,"data":"{\"b\":0,\"x\":1,\"y\":0,\"wh\":0}"}`, the data a json string
    /// and the wheel called `wh` (seen on a V1). older firmware leaves `wh` out, the vendor's driver reads that as no
    /// wheel
    fn upload(&self, value: &Value) -> Option<Vec<Input>> {
        if value.get("code")?.as_u64()? != 200 || !mouse(value.get("message")) {
            return None;
        }
        let value = data(value)?;
        let signed = |name| i32::try_from(value.get(name)?.as_i64()?).ok();
        let dx = signed("x")?;
        let dy = signed("y")?;
        let wheel = match value.get("wh") {
            Some(_) => signed("wh")?,
            None => 0,
        };
        let buttons = u8::try_from(value.get("b")?.as_u64()?).ok()? & 0x1f;
        let mut inputs = Vec::new();
        let mut state = lock(&self.shared.state);
        if let Some(previous) = state.seen.buttons {
            let changed = previous ^ buttons;
            for pressed in [false, true] {
                for (bit, &button) in super::BUTTONS.iter().enumerate() {
                    if changed & (1 << bit) != 0 && (buttons & (1 << bit) != 0) == pressed {
                        inputs.push(Input::Button { button, pressed });
                    }
                }
            }
        }
        state.seen.buttons = Some(buttons);
        drop(state);
        inputs.push(Input::Motion {
            dx,
            dy,
            wheel,
            pan: 0,
        });
        Some(inputs)
    }

    /// the box sends its keyboard report whether anyone listens or not, so the keys are followed from the first
    /// report on. they stop going out once the box said there's no keyboard, because a receiver that's already
    /// there doesn't get checked against the new snapshot
    fn keys(&self, value: &Value) -> Incoming {
        let report = keyboard(value);
        let mut state = lock(&self.shared.state);
        let Some((mut held, overflow)) = report else {
            let keys = state.keys;
            drop(state);
            if keys && let Some(sink) = &self.sink {
                sink.lost(1, InputKind::Keys);
            }
            return Incoming::Noise;
        };
        if overflow {
            // the box names 6 keys in ascending order, so only one above the highest it names can still be down
            let highest = held.iter().filter(|key| !key.is_modifier()).max().copied();
            let unnamed: Vec<Key> = state
                .seen
                .keys
                .iter()
                .copied()
                .filter(|&key| {
                    !key.is_modifier()
                        && !held.contains(&key)
                        && highest.is_none_or(|highest| key > highest)
                })
                .collect();
            held.extend(unnamed);
        }
        // releases first, so a swap in one report reads as one going up and then the other going down
        let edges: Vec<Input> = state
            .seen
            .keys
            .iter()
            .filter(|key| !held.contains(key))
            .map(|&key| Input::Key {
                key,
                pressed: false,
            })
            .chain(
                held.iter()
                    .filter(|key| !state.seen.keys.contains(key))
                    .map(|&key| Input::Key { key, pressed: true }),
            )
            .collect();
        state.seen.keys = held;
        if state.keys {
            Incoming::Inputs(edges)
        } else {
            Incoming::Noise
        }
    }
}

impl Codec for YscFrames {
    fn split(&mut self, buffer: &mut Vec<u8>) -> Option<Frame> {
        while !buffer.is_empty() {
            if buffer.starts_with(START) {
                if buffer.len() < 9 {
                    return None;
                }
                let length = usize::from(u16::from_be_bytes([buffer[7], buffer[8]]));
                if !(15..=MAX_FRAME).contains(&length) {
                    buffer.remove(0);
                    self.lost();
                    continue;
                }
                if buffer.len() < length {
                    return None;
                }
                if &buffer[length - 5..length] != END {
                    buffer.remove(0);
                    self.lost();
                    continue;
                }
                return Some(buffer.drain(..length).collect());
            }
            if buffer.starts_with(PROMPT) {
                buffer.drain(..PROMPT.len());
                if buffer.first() == Some(&b' ') {
                    buffer.remove(0);
                }
                let mut state = lock(&self.shared.state);
                state.seen.prompt_space = buffer.is_empty();
                state.seen.line.clear();
                let mut reply = std::mem::take(&mut state.seen.reply);
                reply.extend_from_slice(PROMPT);
                return Some(reply);
            }
            if START.starts_with(buffer) || PROMPT.starts_with(buffer) {
                return None;
            }
            let byte = buffer.remove(0);
            let mut state = lock(&self.shared.state);
            if std::mem::take(&mut state.seen.prompt_space) && byte == b' ' {
                continue;
            }
            state.seen.line.push(byte);
            if state.seen.line.ends_with(b"\r\n") {
                let line = std::mem::take(&mut state.seen.line);
                if line.starts_with(b"km.") && line.ends_with(b")\r\n") {
                    state.seen.reply = line;
                } else if !state.seen.reply.is_empty() {
                    state.seen.reply.extend(line);
                }
            }
            if state.seen.line.len() + state.seen.reply.len() > MAX_TEXT {
                state.seen.line.clear();
                state.seen.reply.clear();
                drop(state);
                self.lost();
            }
        }
        None
    }

    fn classify(&mut self, frame: &Frame) -> Incoming {
        if frame.starts_with(START) {
            let Some(value) = object(frame) else {
                self.lost();
                return Incoming::Noise;
            };
            if value.get("code").and_then(Value::as_u64) == Some(404)
                && value.get("message").and_then(Value::as_str) == Some("no_keyboard")
            {
                let mut state = lock(&self.shared.state);
                let first = std::mem::replace(&mut state.keys, false);
                if first {
                    for action in [
                        Action::KeyPress,
                        Action::KeyRelease,
                        Action::KeyTap,
                        Action::KeyEvents,
                    ] {
                        state.snapshot.capabilities = state
                            .snapshot
                            .capabilities
                            .clone()
                            .set(action, Capability::unsupported(KEYBOARD));
                    }
                }
                // the refusal doesn't name the key or the direction, so it only gets blamed on one kind of
                // command when that's the only kind this connection sent
                let action = match (state.pressed_keys, state.released_keys) {
                    (true, false) => Some(Action::KeyPress),
                    (false, true) => Some(Action::KeyRelease),
                    _ => None,
                }
                .filter(|_| !state.refused);
                state.refused |= action.is_some();
                let snapshot = first.then(|| state.snapshot.clone());
                drop(state);
                if let Some(sink) = &self.sink {
                    if let Some(snapshot) = snapshot {
                        sink.changed(snapshot);
                    }
                    if let Some(action) = action {
                        sink.refused(action);
                    }
                }
                return Incoming::Reply(key_reply());
            }
            let code = value.get("code").and_then(Value::as_u64);
            // a 400 is the box failing to read the last command. requests go out one at a time, so while a key
            // waits it's that key's, and a text command waiting for its echo never takes this key
            if code == Some(400)
                || (code == Some(200)
                    && matches!(
                        value.get("message").and_then(Value::as_str),
                        Some("key_down" | "key_up")
                    ))
            {
                return Incoming::Reply(key_reply());
            }
            if code == Some(200) && value.get("message").and_then(Value::as_str) == Some("kbd") {
                return self.keys(&value);
            }
            if let Some(inputs) = self.upload(&value) {
                return Incoming::Inputs(inputs);
            }
            // a 200 reply always names itself in its message, so one named like a mouse report, or with none, is an
            // upload nobody could read
            if !known_reply(&value) || (code == Some(200) && mouse(value.get("message"))) {
                self.lost();
            }
            return Incoming::Noise;
        }
        if frame.starts_with(b"km.")
            && frame.ends_with(PROMPT)
            && let Some(end) = frame.windows(2).position(|bytes| bytes == b"\r\n")
            && frame[..end].ends_with(b")")
            && std::str::from_utf8(frame).is_ok()
        {
            Incoming::Reply(key(&frame[..end]))
        } else {
            Incoming::Noise
        }
    }
}

#[cfg(test)]
mod tests;