#ifndef NUI_CORE_PROTOCOL_H #define NUI_CORE_PROTOCOL_H #include #include #include #include #include #include #include #include "synth/synth.h" namespace nui::protocol { using Bytes = std::vector; // Values are wire values, not native structs copied onto USB. enum class CameraMagic : uint16_t { command = 0x4d47, reply = 0x4252 }; struct CameraCommand { CameraMagic magic; uint16_t command; uint16_t sequence; Bytes payload; }; std::optional encode_command(const CameraCommand& command); std::optional decode_command(const Bytes& bytes); // Three sibling devices behind the attach shim's hub, not three interfaces // of one composite device. The index is stable for per-device USB state. enum class UsbFunction : uint8_t { camera, audio, motor }; struct ControlSetup { uint8_t request_type; uint8_t request; uint16_t value; uint16_t index; uint16_t length; }; enum class TransferStatus { completed, stall, idle, invalid_frame }; struct TransferResult { TransferStatus status; Bytes data; }; class UsbDeviceBackend { public: virtual ~UsbDeviceBackend() = default; virtual TransferResult answer_control_transfer( UsbFunction function, const ControlSetup& setup, const Bytes& out = {}) = 0; virtual TransferResult next_packet(UsbFunction function, uint8_t endpoint) = 0; virtual void reset(UsbFunction function) = 0; }; struct SynthFrame { contract::FrameData metadata{}; synth::DepthFrame depth; synth::BgraFrame color; }; // The producer owns the newest-wins slot. This callback must return an immutable // snapshot immediately (nullptr before the first frame), never wait for capture. // Called only at frame boundaries; a packetized frame cannot tear on publication. using FrameSource = std::function()>; class KinectV1Device final : public UsbDeviceBackend { public: explicit KinectV1Device(FrameSource source); TransferResult answer_control_transfer( UsbFunction function, const ControlSetup& setup, const Bytes& out = {}) override; TransferResult next_packet(UsbFunction function, uint8_t endpoint) override; void reset(UsbFunction function) override; private: struct Stream { Bytes bytes; std::size_t offset = 0; uint8_t sequence = 0; uint32_t timestamp = 0; std::optional frame_number; }; TransferResult camera_control(const ControlSetup& setup, const Bytes& out); std::optional execute_command(const CameraCommand& command); bool write_register(uint16_t reg, uint16_t value); TransferResult standard_control(UsbFunction function, const ControlSetup& setup); FrameSource source_; std::array configurations_{}; std::map registers_; Bytes pending_reply_; Stream depth_; Stream video_; int8_t tilt_ = 0; }; } // namespace nui::protocol #endif // NUI_CORE_PROTOCOL_H