feat(attach-linux,fallback-openni2): usbip shim (socket-verified) + OpenNI2 SynthKinect driver (depth verified via real consumer) (wave 6; todo 15 done, 9 partial)
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python tracker (pytest) (push) Canceled after 0s

This commit is contained in:
kinect-bot committed 2026-11-25 05:09:31 -05:00
1 parent cb311381a2
commit 76f3b6447f
19 files changed
+2241 -8

No files matched your search

@@ -0,0 +1,34 @@
#ifndef NUI_ATTACH_LINUX_KINECT_EXPORT_H
#define NUI_ATTACH_LINUX_KINECT_EXPORT_H
#include <array>
#include "attach_linux/usbip_protocol.h"
#include "protocol/protocol.h"
namespace attach::linux_attach {
// The three USB/IP-exportable busids for one virtual Kinect v1 (camera,
// audio, motor — core/protocol/wire.h's three sibling USB functions), in
// UsbFunction enum order, with the EXACT descriptor fields
// core/protocol/descriptors.cpp (wire::device_descriptor/config_descriptor)
// already commits the device to over control transfers.
struct ExportSet {
std::array<usbip::ExportedDevice, 3> devices;
};
ExportSet default_export_set(uint32_t busnum);
nui::protocol::UsbFunction function_for_busid(const ExportSet& set, const std::string& busid);
const usbip::ExportedDevice* device_for_busid(const ExportSet& set, const std::string& busid);
// A placeholder frame source for the standalone `serve` CLI command: a
// single all-background (zero depth, black color) 640x480 frame, re-returned
// on every call. Real frame data comes from wiring this device to the
// tracker/synth pipeline (plan todos 5/7/10) at a higher integration layer
// that this attach shim does not own.
std::shared_ptr<const nui::protocol::SynthFrame> placeholder_frame_source();
} // namespace attach::linux_attach
#endif // NUI_ATTACH_LINUX_KINECT_EXPORT_H
@@ -0,0 +1,183 @@
#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
@@ -0,0 +1,47 @@
#ifndef NUI_ATTACH_LINUX_USBIP_SERVER_H
#define NUI_ATTACH_LINUX_USBIP_SERVER_H
#include <atomic>
#include <cstdint>
#include "attach_linux/kinect_export.h"
#include "protocol/protocol.h"
namespace attach::linux_attach {
// Default usbip TCP port (tools/usb/usbip's own default; the client's
// `usbip attach -r <host>` assumes it unless told otherwise).
constexpr uint16_t kDefaultPort = 3240;
// A userspace USB/IP server: answers OP_REQ_DEVLIST/OP_REQ_IMPORT over a
// listening TCP socket, then on the same connection relays
// USBIP_CMD_SUBMIT/USBIP_CMD_UNLINK traffic from the usbip kernel client
// (vhci-hcd) into `backend`, one connection at a time (usbip's own kernel
// module and the real `usbipd` both serve one importer per exported device
// at a time; this mirrors that).
class UsbipServer {
public:
UsbipServer(nui::protocol::UsbDeviceBackend& backend, ExportSet devices);
// Binds `port` and serves forever (blocks) until `stop()` is called from
// another thread or signal handler. Returns a process-exit-style code:
// 0 on a clean stop, nonzero if the listening socket could not be set up.
int run(uint16_t port);
void stop();
private:
int accept_loop(int listen_fd);
void serve_connection(int client_fd);
bool handle_control_phase(int client_fd, nui::protocol::UsbFunction& function, std::string& busid);
void session_loop(int client_fd, nui::protocol::UsbFunction function);
nui::protocol::UsbDeviceBackend& backend_;
ExportSet devices_;
std::atomic<bool> stop_requested_{false};
int listen_fd_ = -1;
};
} // namespace attach::linux_attach
#endif // NUI_ATTACH_LINUX_USBIP_SERVER_H
@@ -0,0 +1,30 @@
#ifndef NUI_ATTACH_LINUX_VHCI_LIFECYCLE_H
#define NUI_ATTACH_LINUX_VHCI_LIFECYCLE_H
#include <cstdint>
#include <optional>
#include <string>
namespace attach::linux_attach {
struct CommandResult {
bool succeeded;
int exit_code;
std::string output;
};
// Each wraps the stock `usbip` client CLI (not reimplemented here: the
// client side of USB/IP is the kernel vhci-hcd driver plus the usbip
// userspace tool, both out of this shim's scope per todo 9's "no out-of-tree
// kernel modules beyond stock usbip/vhci"). This shim is the SERVER half
// (UsbipServer); these functions only drive the matching client commands.
CommandResult usbip_attach(const std::string& host, uint16_t port, const std::string& busid);
CommandResult usbip_detach(int vhci_port);
CommandResult usbip_list_ports();
CommandResult modprobe_vhci();
std::optional<int> parse_attached_port(const std::string& usbip_attach_output);
} // namespace attach::linux_attach
#endif // NUI_ATTACH_LINUX_VHCI_LIFECYCLE_H