97 lines
2.9 KiB
C++
97 lines
2.9 KiB
C++
#ifndef NUI_CORE_PROTOCOL_H
|
|
#define NUI_CORE_PROTOCOL_H
|
|
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <memory>
|
|
#include <optional>
|
|
#include <vector>
|
|
|
|
#include "synth/synth.h"
|
|
|
|
namespace nui::protocol {
|
|
|
|
using Bytes = std::vector<uint8_t>;
|
|
|
|
// 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<Bytes> encode_command(const CameraCommand& command);
|
|
std::optional<CameraCommand> 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<std::shared_ptr<const SynthFrame>()>;
|
|
|
|
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<uint32_t> frame_number;
|
|
};
|
|
TransferResult camera_control(const ControlSetup& setup, const Bytes& out);
|
|
std::optional<Bytes> 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<uint8_t, 3> configurations_{};
|
|
std::map<uint16_t, uint16_t> registers_;
|
|
Bytes pending_reply_;
|
|
Stream depth_;
|
|
Stream video_;
|
|
int8_t tilt_ = 0;
|
|
};
|
|
|
|
} // namespace nui::protocol
|
|
#endif // NUI_CORE_PROTOCOL_H
|