// SPDX-FileCopyrightText: © 2025 Jeffrey C. Ollie // SPDX-License-Identifier: MIT //! The framing a Stream Deck speaks over TCP, with the socket taken out //! of it. //! //! A networked Stream Deck -- the Studio, and the Network Dock -- does //! not speak a protocol of its own. It carries the very same USB HID //! reports this library already builds, wrapped in one of two framings, //! alongside a small set of commands that have no USB equivalent. So //! what is here is only the wrapping and unwrapping: everything that //! turns bytes into a structure and back, and nothing that touches a //! socket. //! //! That split is the same one `Events.decode` and `Keys.encodeImage` //! already make, and for the same reason. These bytes come from a //! network peer, which is a good deal less trustworthy than a USB //! device, and no device is needed to test or fuzz any of it. //! //! Elgato publishes nothing about this protocol. What follows was read //! out of three independent reverse-engineered implementations, which //! the README names; where they disagree, the comments say so. const std = @import("std"); /// Which of the two framings a device speaks. /// /// Which one a given device uses is not a thing that can be known in /// advance -- it depends on the model and on the firmware, and no /// source documents the cutover -- so it is decided by looking at the /// first bytes the device sends. See `classify`. pub const Framing = enum { /// A sixteen byte header and a payload of the length it declares. /// The Network Dock and newer Studio firmware. cora, /// No header at all: fixed size frames, a different size in each /// direction. Original Studio firmware. legacy, }; /// The four bytes a `cora` frame begins with. pub const magic = [4]u8{ 0x43, 0x93, 0x8a, 0x41 }; /// The size of a `cora` header, and so the smallest a frame can be. pub const header_size = 16; /// How big a frame a device sends in `legacy` framing. The two /// directions differ, which is the whole reason both constants exist. pub const legacy_recv_size = 512; /// How big a frame a device expects in `legacy` framing. Anything /// shorter is padded out to this with zeroes. pub const legacy_send_size = 1024; /// The largest payload this library will accept in a `cora` header. /// /// The header declares its own payload length, so a peer can ask for /// any size at all. Nothing legitimate is anywhere near this -- the /// largest thing either side sends is a 1024 byte image packet -- so /// this is only here to turn a wild length into an error rather than an /// enormous read. pub const max_payload = 4096; /// What a `cora` header's flag word can carry. /// /// `verbatim` is the one that matters: it says the payload is a USB HID /// report and nothing else, to be handed to the device as-is. Without /// it the payload is a command in the host namespace, which has no USB /// counterpart at all. See `HostCommand`. pub const Flags = struct { pub const none: u16 = 0x0000; /// Set by a device on a reply to a `get_report`. pub const result: u16 = 0x0100; /// Set by a device answering something that asked for `req_ack`, /// and by this library on a keepalive acknowledgement. pub const ack_nak: u16 = 0x0200; /// Asks the device to acknowledge. This library never sets it; the /// reference implementation does not either, which is what says it /// is optional. pub const req_ack: u16 = 0x4000; /// The payload is a USB HID report, byte for byte. pub const verbatim: u16 = 0x8000; }; /// Which of the three hidapi calls a `cora` frame is carrying. /// /// This is the whole of what the framing does: it is a remote procedure /// call for `hid_write`, `hid_send_feature_report` and /// `hid_get_feature_report`, which is exactly the set of operations /// this library already makes of a USB device. pub const HidOp = enum(u8) { write = 0x00, send_report = 0x01, get_report = 0x02, _, }; /// A `cora` header, in the order and the widths it goes on the wire. /// /// Everything here is little-endian. That is worth saying out loud /// because the *other* namespace this protocol carries is not: a host /// command reply declares its length big-endian. See `HostReply`. pub const Header = extern struct { magic: [4]u8 = framing_magic, flags: u16, hid_op: u8, /// Always zero. What it is for is not known; no implementation /// reads it and every one writes zero. reserved: u8 = 0, /// Correlates a reply with the request that asked for it. A device /// echoes whatever it was sent, so the value itself means nothing. message_id: u32, payload_len: u32, const framing_magic = magic; }; comptime { std.debug.assert(@sizeOf(Header) == header_size); } /// A whole `cora` frame taken off the front of a stream: the header, /// the payload it declared, and how many bytes of the stream the two of /// them occupied. /// /// `consumed` is not simply `header_size + payload.len`. A stream can /// arrive misaligned, and `decode` skips whatever it had to skip to /// find the magic, so `consumed` counts that too. pub const Frame = struct { header: Header, payload: []const u8, consumed: usize, }; /// What can be wrong with a frame a device sent. /// /// As with `ReportError` in `root.zig`, these are errors rather than a /// null with a log line beside it: a caller knows whether it is running /// a daemon or a one-shot tool, and a library does not. pub const FramingError = error{ /// The header declared a payload larger than `max_payload`. The /// peer is confused, or this is not a Stream Deck. PayloadTooLarge, /// The buffer handed in cannot hold the frame being built. BufferTooSmall, /// The first bytes a device sent match neither framing. UnknownFraming, }; /// Which framing these bytes are the start of, or null if there are not /// yet enough of them to tell. /// /// The device speaks first -- there is no client hello -- so this runs /// on whatever it opens with. A `cora` device opens with the magic; a /// `legacy` one opens with a keepalive probe, which begins `01 0a`. /// /// Note that this insists the magic be at offset zero. A device that /// sent anything at all before its first frame would be classified as /// `legacy` if those bytes happened to begin `01 0a` and refused /// otherwise. One implementation reports hardware emitting a few zero /// bytes on connection setup, which would be refused here; it is the /// only source that mentions it, and inventing a tolerance for /// something only one source has seen would be guessing. pub fn classify(data: []const u8) FramingError!?Framing { if (data.len >= magic.len and std.mem.eql(u8, data[0..magic.len], &magic)) return .cora; // Two bytes is enough to recognise a keepalive probe, and one is // not enough to rule the magic out. if (data.len < magic.len) { if (data.len >= 2 and data[0] == 0x01 and data[1] == 0x0a) return .legacy; // Still consistent with the magic, so wait for more. if (std.mem.startsWith(u8, &magic, data)) return null; if (data.len < 2) return null; return error.UnknownFraming; } if (data[0] == 0x01 and data[1] == 0x0a) return .legacy; return error.UnknownFraming; } /// Where the `cora` magic starts in `data`, or null if it is not there. /// /// The decoder resynchronises rather than trusting that a stream is /// aligned. Every implementation read does the same, which says they /// all meet misaligned streams in practice. pub fn findMagic(data: []const u8) ?usize { return std.mem.find(u8, data, &magic); } /// Take one whole `cora` frame off the front of `data`, or null if /// `data` does not yet hold one. /// /// The returned payload points into `data` and is good only until /// `data` is next written to. pub fn decode(data: []const u8) FramingError!?Frame { const start = findMagic(data) orelse return null; const rest = data[start..]; if (rest.len < header_size) return null; const flags = std.mem.readInt(u16, rest[4..6], .little); const hid_op = rest[6]; const reserved = rest[7]; const message_id = std.mem.readInt(u32, rest[8..12], .little); const payload_len = std.mem.readInt(u32, rest[12..16], .little); if (payload_len > max_payload) return error.PayloadTooLarge; const total = header_size + payload_len; if (rest.len < total) return null; return .{ .header = .{ .flags = flags, .hid_op = hid_op, .reserved = reserved, .message_id = message_id, .payload_len = payload_len, }, .payload = rest[header_size..total], .consumed = start + total, }; } /// Build a `cora` frame into `buf` and return the part of it written. /// /// `buf` has to hold the header and the payload together; anything /// smaller is `error.BufferTooSmall`. pub fn encode( buf: []u8, flags: u16, hid_op: HidOp, message_id: u32, payload: []const u8, ) FramingError![]const u8 { if (payload.len > max_payload) return error.PayloadTooLarge; const total = header_size + payload.len; if (buf.len < total) return error.BufferTooSmall; var writer = std.Io.Writer.fixed(buf[0..header_size]); writer.writeStruct(Header{ .flags = flags, .hid_op = @intFromEnum(hid_op), .message_id = message_id, .payload_len = @intCast(payload.len), }, .little) catch unreachable; @memcpy(buf[header_size..total], payload); return buf[0..total]; } /// Whether this payload is the keepalive a device sends unprompted. /// /// The device pings and the host answers; there is no ping in the other /// direction. A host that does not answer is disconnected after about /// five seconds. pub fn isKeepaliveProbe(payload: []const u8) bool { return payload.len > 5 and payload[0] == 0x01 and payload[1] == 0x0a; } /// Whether this payload is the notification a dock sends when /// something is plugged into or pulled out of it. /// /// Nothing is done with these yet -- driving a device behind a dock is /// not implemented -- but they have to be recognised so that they are /// not mistaken for an input report, which is what anything else /// beginning `0x01` is. pub fn isChildNotification(payload: []const u8) bool { return payload.len > 1 and payload[0] == 0x01 and payload[1] == 0x0b; } /// Whether this payload is an input report: a key pressed, an encoder /// turned, a screen touched. /// /// This is what `Events.decode` parses, and it is byte for byte the /// report the same device would send over USB, leading report id and /// all. pub fn isInputReport(payload: []const u8) bool { return payload.len > 0 and payload[0] == 0x01 and !isKeepaliveProbe(payload) and !isChildNotification(payload); } /// The connection number a keepalive probe carries, which its /// acknowledgement has to quote back. /// /// A device accepts several clients at once and tells them apart by /// this, so answering with the wrong one is answering on somebody /// else's behalf. pub fn keepaliveConnection(probe: []const u8) u8 { std.debug.assert(isKeepaliveProbe(probe)); return probe[5]; } /// Build the payload acknowledging a keepalive probe. /// /// `buf` is zeroed and the whole of it is returned, because its length /// is the acknowledgement's length. What that length should be is not /// settled: implementations send 32 bytes and 1024 bytes and both are /// reported to work, so the device evidently does not care. The caller /// picks by the size of the buffer it passes. pub fn keepaliveAck(buf: []u8, probe: []const u8) FramingError![]const u8 { if (buf.len < 3) return error.BufferTooSmall; @memset(buf, 0); buf[0] = 0x03; buf[1] = 0x1a; buf[2] = keepaliveConnection(probe); return buf; } /// The commands that have no USB equivalent. /// /// These are the other half of what a `cora` frame can carry, selected /// by *not* setting `Flags.verbatim`. A device reached directly -- a /// Studio, or a dock itself -- answers questions about itself here /// rather than through the feature reports its USB counterpart uses. A /// device reached through a dock does not: everything to one of those /// is verbatim, and the USB report ids apply unchanged. pub const HostCommand = enum(u8) { /// Vendor and product id. The reply is the only way to learn what /// model is on the other end of a socket. identity = 0x80, /// The AP2 firmware version, which is what stands in for the /// firmware version a USB device reports through feature report /// `0x05`. firmware = 0x83, /// The serial number, standing in for USB feature report `0x06`. serial = 0x84, /// What is plugged into a Network Dock, and the port to reach it /// on. The reply is not shaped like the others: it begins `01 0b`, /// like the notification a dock sends of its own accord when a deck /// is plugged in or pulled out, and is read with `parseChildInfo`. child = 0x1c, _, }; /// Build the payload asking a device a host command. pub fn hostRequest(buf: []u8, command: HostCommand) FramingError![]const u8 { if (buf.len < 2) return error.BufferTooSmall; @memset(buf, 0); buf[0] = 0x03; buf[1] = @intFromEnum(command); return buf; } /// A device's answer to a host command: which command it is answering, /// and the body it carried. pub const HostReply = struct { command: u8, /// The bytes after the length field. Points into the payload it was /// parsed from. body: []const u8, /// Parse a host command reply, or null if this payload is not one. /// /// The shape is `03 `, and **the length is /// big-endian** -- in a protocol that is little-endian everywhere /// else, including the header this payload arrived in. Two /// independent implementations read it that way and a third reads /// only its low byte, which agrees for every length either has ever /// seen. It is the single most likely thing here to be got wrong. /// /// A length reaching past what the device actually sent is clamped /// rather than refused: the body is whatever really arrived. A /// device that lies about its length cannot make this read past the /// end of the payload. pub fn parse(payload: []const u8) ?HostReply { if (payload.len < 4) return null; if (payload[0] != 0x03) return null; const declared = std.mem.readInt(u16, payload[2..4], .big); const available = payload.len - 4; const len = @min(@as(usize, declared), available); return .{ .command = payload[1], .body = payload[4 .. 4 + len], }; } }; /// The vendor and product id out of an `identity` reply, or null if the /// reply is too short to carry them. /// /// These sit at a fixed offset in the payload rather than in the body, /// so this takes the whole payload. Both are little-endian, unlike the /// length field above them. pub fn parseIdentity(payload: []const u8) ?struct { vendor: u16, product: u16 } { if (payload.len < 16) return null; return .{ .vendor = std.mem.readInt(u16, payload[12..14], .little), .product = std.mem.readInt(u16, payload[14..16], .little), }; } /// The version string out of a `firmware` reply, or null if the reply /// is too short to carry one. /// /// This reply does **not** have the shape `HostReply` reads. A Studio /// answers `03 83 00 00`, then a four byte word, then the version at /// offset 8 -- the length field is zero, so read as a `HostReply` the /// body is empty. What follows the zero is laid out like the USB /// firmware feature report, a checksum word and then the string, which /// is presumably where it came from. So this reads it at its offset and /// cuts it at the first NUL, and leaves the length field alone. pub fn parseFirmware(payload: []const u8) ?[]const u8 { const offset = 8; if (payload.len <= offset) return null; if (payload[0] != 0x03 or payload[1] != @intFromEnum(HostCommand.firmware)) return null; const string = payload[offset..]; if (std.mem.findScalar(u8, string, 0)) |end| return string[0..end]; return string; } /// What a Network Dock says about the deck plugged into it: the reply to /// `HostCommand.child`, or the notification it sends when that changes. pub const ChildInfo = struct { /// Whether a deck is plugged in at all. Everything below is /// meaningless when it is not. present: bool, vendor: u16, product: u16, /// The TCP port the deck itself is reached on, at the dock's own /// address. A dock carries one deck, on port 20001 in the one seen. port: u16, serial_buf: [serial_len]u8, const serial_len = 31; /// The deck's serial number, as the dock reports it. pub fn serial(self: *const ChildInfo) []const u8 { const end = std.mem.findScalar(u8, &self.serial_buf, 0) orelse serial_len; return self.serial_buf[0..end]; } }; /// Read a `ChildInfo` out of a dock's `01 0b` payload, or null if it is /// too short to be one. /// /// The offsets are node-elgato-stream-deck's, and a Network Dock with a /// Neo plugged in answered with the Neo's ids, serial and port exactly /// where they say: `02` at byte 4 for present, the vendor and product at /// 26 and 28, the serial from 94, and the port at 126, all /// little-endian. What else is in there -- the deck's unit information /// from byte 5, its manufacturer and product names from 30 and 62 -- is /// not read. pub fn parseChildInfo(payload: []const u8) ?ChildInfo { if (payload.len < 128 or !isChildNotification(payload)) return null; var info: ChildInfo = .{ .present = payload[4] == 0x02, .vendor = std.mem.readInt(u16, payload[26..28], .little), .product = std.mem.readInt(u16, payload[28..30], .little), .port = std.mem.readInt(u16, payload[126..128], .little), .serial_buf = undefined, }; @memcpy(&info.serial_buf, payload[94..125]); return info; } /// The string in a host command reply: its body, cut at the first NUL. /// /// A device pads with zeroes, and the declared length does not always /// exclude the padding, so the terminator is what actually ends the /// string. pub fn replyString(reply: HostReply) []const u8 { if (std.mem.findScalar(u8, reply.body, 0)) |end| return reply.body[0..end]; return reply.body; } test classify { // Not yet enough to tell. try std.testing.expectEqual(null, try classify(&.{})); try std.testing.expectEqual(null, try classify(&.{0x43})); try std.testing.expectEqual(null, try classify(&.{ 0x43, 0x93 })); try std.testing.expectEqual(Framing.cora, (try classify(&magic)).?); try std.testing.expectEqual(Framing.cora, (try classify(&(magic ++ [_]u8{ 0, 0 }))).?); try std.testing.expectEqual(Framing.legacy, (try classify(&.{ 0x01, 0x0a })).?); try std.testing.expectEqual( Framing.legacy, (try classify(&.{ 0x01, 0x0a, 0x00, 0x00, 0x00, 0x07 })).?, ); // A device that is not a Stream Deck. try std.testing.expectError(error.UnknownFraming, classify(&.{ 0xde, 0xad })); try std.testing.expectError(error.UnknownFraming, classify(&.{ 0xde, 0xad, 0xbe, 0xef })); // One byte that cannot be the start of the magic, and is not enough // to be a probe either. try std.testing.expectEqual(null, try classify(&.{0xff})); } test "a frame round-trips through encode and decode" { var buf: [64]u8 = undefined; const payload = [_]u8{ 0x02, 0x07, 0x00, 0x01, 0x10, 0x00, 0x00, 0x00 }; const bytes = try encode(&buf, Flags.verbatim, .write, 0x11223344, &payload); try std.testing.expectEqual(header_size + payload.len, bytes.len); // The header goes out exactly as it is described, little-endian. try std.testing.expectEqualSlices(u8, &magic, bytes[0..4]); try std.testing.expectEqualSlices(u8, &.{ 0x00, 0x80 }, bytes[4..6]); try std.testing.expectEqual(@as(u8, 0x00), bytes[6]); try std.testing.expectEqual(@as(u8, 0x00), bytes[7]); try std.testing.expectEqualSlices(u8, &.{ 0x44, 0x33, 0x22, 0x11 }, bytes[8..12]); try std.testing.expectEqualSlices(u8, &.{ 0x08, 0x00, 0x00, 0x00 }, bytes[12..16]); const frame = (try decode(bytes)).?; try std.testing.expectEqual(Flags.verbatim, frame.header.flags); try std.testing.expectEqual(HidOp.write, @as(HidOp, @enumFromInt(frame.header.hid_op))); try std.testing.expectEqual(@as(u32, 0x11223344), frame.header.message_id); try std.testing.expectEqualSlices(u8, &payload, frame.payload); try std.testing.expectEqual(bytes.len, frame.consumed); } test "a frame split across two reads is not returned until it is whole" { var buf: [64]u8 = undefined; const payload = [_]u8{ 1, 2, 3, 4 }; const bytes = try encode(&buf, Flags.none, .get_report, 1, &payload); // Every prefix short of the whole thing has to say "not yet". for (0..bytes.len) |n| { try std.testing.expectEqual(null, try decode(bytes[0..n])); } try std.testing.expect(try decode(bytes) != null); } test "the decoder resynchronises on the magic" { var buf: [64]u8 = undefined; const payload = [_]u8{ 0xaa, 0xbb }; const bytes = try encode(&buf, Flags.none, .write, 7, &payload); var stream: [80]u8 = undefined; const junk = [_]u8{ 0x00, 0x00, 0xde, 0xad, 0xbe }; @memcpy(stream[0..junk.len], &junk); @memcpy(stream[junk.len..][0..bytes.len], bytes); const frame = (try decode(stream[0 .. junk.len + bytes.len])).?; try std.testing.expectEqualSlices(u8, &payload, frame.payload); // `consumed` counts the junk that was skipped, or the caller would // leave it in the buffer and find it again forever. try std.testing.expectEqual(junk.len + bytes.len, frame.consumed); } test "a header claiming an enormous payload is refused" { var bytes: [header_size]u8 = undefined; @memcpy(bytes[0..4], &magic); @memset(bytes[4..12], 0); std.mem.writeInt(u32, bytes[12..16], max_payload + 1, .little); try std.testing.expectError(error.PayloadTooLarge, decode(&bytes)); // And the largest allowed length is accepted, not off by one -- // though the frame is not complete, so it reads as "not yet". std.mem.writeInt(u32, bytes[12..16], max_payload, .little); try std.testing.expectEqual(null, try decode(&bytes)); } test "a buffer too small for the frame is refused rather than truncated" { var buf: [8]u8 = undefined; try std.testing.expectError( error.BufferTooSmall, encode(&buf, Flags.none, .write, 0, &.{ 1, 2, 3, 4 }), ); } test isKeepaliveProbe { const probe = [_]u8{ 0x01, 0x0a, 0x00, 0x00, 0x00, 0x05 }; try std.testing.expect(isKeepaliveProbe(&probe)); try std.testing.expectEqual(@as(u8, 5), keepaliveConnection(&probe)); // An input report begins with the same byte and is not a probe. try std.testing.expect(!isKeepaliveProbe(&.{ 0x01, 0x00, 0x00, 0x00 })); try std.testing.expect(isInputReport(&.{ 0x01, 0x00, 0x00, 0x00 })); try std.testing.expect(!isInputReport(&probe)); // Nor is a hotplug notification. const hotplug = [_]u8{ 0x01, 0x0b, 0x7c, 0x00, 0x02 }; try std.testing.expect(isChildNotification(&hotplug)); try std.testing.expect(!isInputReport(&hotplug)); try std.testing.expect(!isKeepaliveProbe(&hotplug)); // Too short to be anything. try std.testing.expect(!isKeepaliveProbe(&.{0x01})); try std.testing.expect(!isChildNotification(&.{0x01})); try std.testing.expect(!isInputReport(&.{})); } test keepaliveAck { const probe = [_]u8{ 0x01, 0x0a, 0x00, 0x00, 0x00, 0x2a }; var buf: [32]u8 = undefined; const ack = try keepaliveAck(&buf, &probe); try std.testing.expectEqual(32, ack.len); try std.testing.expectEqual(@as(u8, 0x03), ack[0]); try std.testing.expectEqual(@as(u8, 0x1a), ack[1]); try std.testing.expectEqual(@as(u8, 0x2a), ack[2]); // Everything after the connection number is zero. for (ack[3..]) |b| try std.testing.expectEqual(@as(u8, 0), b); var tiny: [2]u8 = undefined; try std.testing.expectError(error.BufferTooSmall, keepaliveAck(&tiny, &probe)); } test "a host reply declares its length big-endian" { // This is the one field in the whole protocol that is not // little-endian, so it gets a test of its own. `0x0004` read the // wrong way round is `0x0400`, which would run off the end. const payload = [_]u8{ 0x03, 0x84, 0x00, 0x04, 'A', 'B', '1', '2', 0x00, 0x00 }; const reply = HostReply.parse(&payload).?; try std.testing.expectEqual(@as(u8, 0x84), reply.command); try std.testing.expectEqualStrings("AB12", reply.body); try std.testing.expectEqualStrings("AB12", replyString(reply)); } test "a host reply claiming more than it carries is clamped" { // A device that lies about its length must not make this read past // the end of what actually arrived. const payload = [_]u8{ 0x03, 0x84, 0xff, 0xff, 'x', 'y' }; const reply = HostReply.parse(&payload).?; try std.testing.expectEqualStrings("xy", reply.body); // And something that is not a host reply at all. try std.testing.expectEqual(null, HostReply.parse(&.{ 0x01, 0x00, 0x00, 0x00 })); try std.testing.expectEqual(null, HostReply.parse(&.{ 0x03, 0x84 })); } test "a string reply is cut at its terminator" { const payload = [_]u8{ 0x03, 0x84, 0x00, 0x08, 'C', 'L', '1', '2', 0x00, 0x00, 0x00, 0x00 }; const reply = HostReply.parse(&payload).?; // The device declared eight bytes and four of them are padding. try std.testing.expectEqual(8, reply.body.len); try std.testing.expectEqualStrings("CL12", replyString(reply)); } test parseIdentity { var payload = std.mem.zeroes([16]u8); payload[0] = 0x03; payload[1] = 0x80; std.mem.writeInt(u16, payload[12..14], 0x0fd9, .little); std.mem.writeInt(u16, payload[14..16], 0x00aa, .little); const id = parseIdentity(&payload).?; try std.testing.expectEqual(@as(u16, 0x0fd9), id.vendor); try std.testing.expectEqual(@as(u16, 0x00aa), id.product); // Too short to carry them. try std.testing.expectEqual(null, parseIdentity(payload[0..15])); } // The three replies below are the ones a Stream Deck Studio sent, on // firmware 2.01.000 in legacy framing, trimmed of their zero padding. test "a Studio's serial number reply" { const payload = [_]u8{ 0x03, 0x84, 0x00, 0x0e, 'A', '9', 'I', 'J', 'A', '5', '2', '7', '3', '1', 'F', 'R', 'K', 'Z', 0x00, 0x00, }; const reply = HostReply.parse(&payload).?; try std.testing.expectEqualStrings("A9IJA52731FRKZ", replyString(reply)); } test "a Studio's firmware reply" { const payload = [_]u8{ 0x03, 0x83, 0x00, 0x00, 0x98, 0x59, 0xe0, 0xc0, '2', '.', '0', '1', '.', '0', '0', '0', 0x00, 0x00, }; try std.testing.expectEqualStrings("2.01.000", parseFirmware(&payload).?); // Read the ordinary way, the zero length leaves nothing -- which is // what this library once reported as the Studio's firmware. try std.testing.expectEqualStrings("", replyString(HostReply.parse(&payload).?)); try std.testing.expectEqual(null, parseFirmware(payload[0..8])); try std.testing.expectEqual(null, parseFirmware(&.{ 0x03, 0x84, 0, 0, 0, 0, 0, 0, 'x' })); } test "a Studio's identity reply" { const payload = [_]u8{ 0x03, 0x80, 0x02, 0x10, 0x90, 0x00, 0x70, 0x00, 0x90, 0x05, 0x18, 0x01, 0xd9, 0x0f, 0xaa, 0x00, }; const id = parseIdentity(&payload).?; try std.testing.expectEqual(@as(u16, 0x0fd9), id.vendor); try std.testing.expectEqual(@as(u16, 0x00aa), id.product); } test "a Network Dock's answer about the Neo plugged into it" { // The first 128 bytes a dock sent in reply to `HostCommand.child`, // with a Neo plugged in. const payload = [_]u8{ 0x01, 0x0b, 0x7c, 0x00, 0x02, 0x03, 0x04, 0x00, 0x60, 0x00, 0x60, 0x00, 0xe0, 0x01, 0x40, 0x01, 0x04, 0x05, 0x01, 0x04, 0x04, 0x00, 0xf8, 0x00, 0x3a, 0x00, 0xd9, 0x0f, 0x9a, 0x00, 'E', 'l', 'g', 'a', 't', 'o', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'S', 't', 'r', 'e', 'a', 'm', ' ', 'D', 'e', 'c', 'k', ' ', 'N', 'e', 'o', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'A', '7', 'B', 'S', 'A', '4', '0', '9', '1', '3', '8', '0', '1', '1', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x21, 0x4e, }; const info = parseChildInfo(&payload).?; try std.testing.expect(info.present); try std.testing.expectEqual(@as(u16, 0x0fd9), info.vendor); try std.testing.expectEqual(@as(u16, 0x009a), info.product); try std.testing.expectEqual(@as(u16, 20001), info.port); try std.testing.expectEqualStrings("A7BSA409138011", info.serial()); // Nothing plugged in. var empty = payload; empty[4] = 0x00; try std.testing.expect(!parseChildInfo(&empty).?.present); // Too short, or not this kind of payload at all. try std.testing.expectEqual(null, parseChildInfo(payload[0..127])); var other = payload; other[1] = 0x0a; try std.testing.expectEqual(null, parseChildInfo(&other)); } test "a Network Dock's firmware reply reads like a Studio's" { const payload = [_]u8{ 0x03, 0x83, 0x00, 0x00, 0x62, 0x85, 0x62, 0x35, '1', '.', '0', '1', '.', '0', '1', '3', 0x00 }; try std.testing.expectEqualStrings("1.01.013", parseFirmware(&payload).?); } test hostRequest { var buf: [32]u8 = undefined; const req = try hostRequest(&buf, .serial); try std.testing.expectEqual(32, req.len); try std.testing.expectEqual(@as(u8, 0x03), req[0]); try std.testing.expectEqual(@as(u8, 0x84), req[1]); for (req[2..]) |b| try std.testing.expectEqual(@as(u8, 0), b); var tiny: [1]u8 = undefined; try std.testing.expectError(error.BufferTooSmall, hostRequest(&tiny, .serial)); } test { std.testing.refAllDecls(@This()); }