Files
kinect/attach/linux/include/attach_linux/usbip_protocol.h
T
2026-11-25 05:09:31 -05:00

184 lines
7.2 KiB
C++

#ifndef NUI_ATTACH_LINUX_USBIP_PROTOCOL_H
#define NUI_ATTACH_LINUX_USBIP_PROTOCOL_H
// USB/IP wire protocol (server side: "export" a USB device to a usbip client
// bound to vhci-hcd). All multi-byte fields are network byte order (big
// endian), per the Linux kernel documentation this was transcribed from:
// https://www.kernel.org/doc/html/latest/usb/usbip_protocol.html
// (retrieved 2026-11-25; protocol version v1.1.1, wire value 0x0111).
//
// This header only encodes/decodes byte buffers; it has no socket I/O so it
// can be unit-tested without a network stack (see tests/test_usbip_protocol).
#include <array>
#include <cstdint>
#include <optional>
#include <string>
#include <vector>
namespace attach::usbip {
using Bytes = std::vector<uint8_t>;
// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------
constexpr uint16_t kProtocolVersion = 0x0111;
// OP_* codes (control connection: device list + import handshake).
constexpr uint16_t kOpReqDevlist = 0x8005;
constexpr uint16_t kOpRepDevlist = 0x0005;
constexpr uint16_t kOpReqImport = 0x8003;
constexpr uint16_t kOpRepImport = 0x0003;
// USBIP_CMD_*/USBIP_RET_* basic-header 'command' values (data connection:
// URB submit/unlink after a successful import).
constexpr uint32_t kCmdSubmit = 0x00000001;
constexpr uint32_t kCmdUnlink = 0x00000002;
constexpr uint32_t kRetSubmit = 0x00000003;
constexpr uint32_t kRetUnlink = 0x00000004;
constexpr uint32_t kDirOut = 0;
constexpr uint32_t kDirIn = 1;
// usbip_header_basic / usbip_header are always this size before the
// variable-length transfer_buffer + iso_packet_descriptor tail (both
// USBIP_CMD_SUBMIT and USBIP_RET_SUBMIT share this fixed prefix length).
constexpr std::size_t kHeaderBasicSize = 20;
constexpr std::size_t kSubmitHeaderSize = 0x30; // 48
constexpr std::size_t kUnlinkHeaderSize = 0x30; // 48 (basic + unlink_seqnum + 24 pad)
// Linux errno values the server reports back to the client in RET_SUBMIT's
// status field (negative, per URB/errno convention) when a control transfer
// stalls or an endpoint/transfer is invalid. usbip itself just forwards
// whatever the real USB stack returned; a virtual device picks the same
// codes a real stalled endpoint would produce.
constexpr int32_t kErrnoPipeStall = -32; // -EPIPE
constexpr int32_t kErrnoNoDevice = -19; // -ENODEV
constexpr int32_t kErrnoConnReset = -104; // -ECONNRESET (RET_UNLINK success)
// ---------------------------------------------------------------------------
// OP_REQ_DEVLIST / OP_REP_DEVLIST / OP_REQ_IMPORT / OP_REP_IMPORT
// ---------------------------------------------------------------------------
// Common 8-byte prefix of every OP_* message: version(2) + code(2) + status(4).
struct OpHeader {
uint16_t version;
uint16_t code;
uint32_t status;
};
std::optional<OpHeader> decode_op_header(const Bytes& bytes);
Bytes encode_op_header(uint16_t code, uint32_t status);
// One USB interface's class/subclass/protocol triad, as carried in
// OP_REP_DEVLIST (4 bytes per interface including a zero pad byte).
struct InterfaceDescriptor {
uint8_t interface_class;
uint8_t interface_subclass;
uint8_t interface_protocol;
};
// Describes one USB/IP-exportable device (one Kinect v1 sibling function:
// camera, audio, or motor — see core/protocol/wire.h for the real values).
struct ExportedDevice {
std::string path; // synthetic sysfs-like path, <= 255 bytes, NUL-padded to 256
std::string busid; // e.g. "1-1", <= 31 bytes, NUL-padded to 32
uint32_t busnum;
uint32_t devnum;
uint32_t speed; // Linux enum usb_device_speed; Kinect v1 negotiates HIGH (3)
uint16_t id_vendor;
uint16_t id_product;
uint16_t bcd_device;
uint8_t device_class;
uint8_t device_subclass;
uint8_t device_protocol;
uint8_t configuration_value;
uint8_t num_configurations;
std::vector<InterfaceDescriptor> interfaces; // bNumInterfaces entries
};
// Appends one device's OP_REP_DEVLIST entry (path[256] + busid[32] + the
// fixed device fields + bNumInterfaces * 4-byte interface entries).
void append_device_entry(Bytes& out, const ExportedDevice& device);
Bytes encode_rep_devlist(const std::vector<ExportedDevice>& devices);
// Decodes the busid out of an OP_REQ_IMPORT body (the 32 bytes after the
// 8-byte OP header).
std::optional<std::string> decode_req_import_busid(const Bytes& body);
// OP_REP_IMPORT: status 0 + the device's import-reply fields (no interface
// list, unlike devlist) on success; status 1 with nothing else on failure.
Bytes encode_rep_import_ok(const ExportedDevice& device);
Bytes encode_rep_import_error();
// ---------------------------------------------------------------------------
// usbip_header_basic + USBIP_CMD_SUBMIT / USBIP_RET_SUBMIT /
// USBIP_CMD_UNLINK / USBIP_RET_UNLINK (the data-connection URB traffic)
// ---------------------------------------------------------------------------
struct HeaderBasic {
uint32_t command;
uint32_t seqnum;
uint32_t devid; // client: (busnum << 16) | devnum; server reply: 0
uint32_t direction; // kDirOut / kDirIn; server reply: 0
uint32_t ep; // endpoint number without the direction bit; server reply: 0
};
std::optional<HeaderBasic> decode_header_basic(const Bytes& bytes, std::size_t offset = 0);
void append_header_basic(Bytes& out, const HeaderBasic& basic);
// The 8-byte raw USB control setup packet, little-endian per the USB spec
// (USBIP_CMD_SUBMIT's 'setup' field carries it verbatim, untouched by the
// big-endian byte-order rule that governs the rest of the USB/IP header).
struct SetupPacket {
uint8_t request_type;
uint8_t request;
uint16_t value;
uint16_t index;
uint16_t length;
};
SetupPacket decode_setup(const std::array<uint8_t, 8>& raw);
std::array<uint8_t, 8> encode_setup(const SetupPacket& setup);
struct CmdSubmit {
HeaderBasic basic;
uint32_t transfer_flags;
uint32_t transfer_buffer_length;
int32_t start_frame;
uint32_t number_of_packets;
uint32_t interval;
std::array<uint8_t, 8> setup;
Bytes transfer_buffer; // present (length transfer_buffer_length) iff direction == OUT
};
// Decodes one full USBIP_CMD_SUBMIT packet (header + OUT payload if any).
// ISO packet descriptors (trailing the OUT buffer) are not modelled: the
// Kinect camera's iso endpoints are read-only (IN) from the host's point of
// view, so a CMD_SUBMIT against them never carries ISO descriptors in or out
// of this decode path; this is a documented simplification, not a protocol
// gap for the direction this server actually serves.
std::optional<CmdSubmit> decode_cmd_submit(const Bytes& bytes);
// Encodes USBIP_RET_SUBMIT. `basic` must already have devid/direction/ep set
// to zero per the server-reply convention; this function sets `command`.
Bytes encode_ret_submit(
uint32_t seqnum,
int32_t status,
const Bytes& transfer_buffer,
int32_t start_frame = 0,
uint32_t number_of_packets = 0xffffffffu,
int32_t error_count = 0);
struct CmdUnlink {
HeaderBasic basic;
uint32_t unlink_seqnum;
};
std::optional<CmdUnlink> decode_cmd_unlink(const Bytes& bytes);
Bytes encode_ret_unlink(uint32_t seqnum, int32_t status);
} // namespace attach::usbip
#endif // NUI_ATTACH_LINUX_USBIP_PROTOCOL_H