Files
kinect/core/protocol/protocol.cpp
T
kinect-bot cb311381a2
ci / Linux C++ core (cmake + ctest) (push) Canceled after 0s
ci / Python tracker (pytest) (push) Canceled after 0s
feat(protocol): byte-exact Kinect v1 virtual USB device emitter (wave 5; todo 8 done)
2026-11-25 04:50:42 -05:00

87 lines
2.9 KiB
C++

#include "protocol/protocol.h"
#include <algorithm>
#include <cstddef>
#include "protocol/wire.h"
namespace nui::protocol {
namespace {
// Every register the host writes during Kinect v1 camera bring-up, so an
// out-of-table write is rejected as unsupported rather than silently stored.
// Depth start (libfreenect 09a1f098 src/cameras.c:880-899): 0x105,0x06,0x12,
// 0x13,0x14,0x17. Video/IR start (cameras.c:936-1050): 0x0c/0x0d/0x0e,0x47
// (Bayer) and 0x19/0x1a/0x1b,0x48 (IR), 0x05. IR brightness 0x15 (flags.c:175).
std::map<uint16_t, uint16_t> default_registers() {
std::map<uint16_t, uint16_t> registers;
for (uint16_t reg : {0x05, 0x06, 0x0c, 0x0d, 0x0e, 0x12, 0x13, 0x14, 0x15,
0x17, 0x19, 0x1a, 0x1b, 0x47, 0x48, 0x105}) {
registers[reg] = 0;
}
return registers;
}
std::size_t function_index(UsbFunction function) {
return static_cast<std::size_t>(function);
}
} // namespace
KinectV1Device::KinectV1Device(FrameSource source)
: source_(std::move(source)), registers_(default_registers()) {}
void KinectV1Device::reset(UsbFunction function) {
if (function == UsbFunction::camera) {
registers_ = default_registers();
depth_ = {};
video_ = {};
pending_reply_.clear();
}
configurations_[function_index(function)] = 0;
}
TransferResult KinectV1Device::answer_control_transfer(
UsbFunction function, const ControlSetup& setup, const Bytes& out) {
// bmRequestType bits 6:5 select the request type: 0 = standard (enumeration),
// 2 = vendor (the camera command channel send_cmd rides, cameras.c:213/221).
const uint8_t type = setup.request_type & 0x60;
if (type == 0x40) {
if (function != UsbFunction::camera) {
return {TransferStatus::stall, {}};
}
return camera_control(setup, out);
}
if (type == 0x00) {
return standard_control(function, setup);
}
return {TransferStatus::stall, {}};
}
TransferResult KinectV1Device::standard_control(
UsbFunction function, const ControlSetup& setup) {
switch (setup.request) {
case 0x06: { // GET_DESCRIPTOR: wValue = (type << 8) | index
auto descriptor = wire::descriptor(function, setup.value);
if (!descriptor) {
return {TransferStatus::stall, {}};
}
descriptor->resize(std::min<std::size_t>(setup.length, descriptor->size()));
return {TransferStatus::completed, std::move(*descriptor)};
}
case 0x08: // GET_CONFIGURATION
return {TransferStatus::completed,
Bytes{configurations_[function_index(function)]}};
case 0x09: // SET_CONFIGURATION
configurations_[function_index(function)] =
static_cast<uint8_t>(setup.value);
return {TransferStatus::completed, {}};
case 0x0b: // SET_INTERFACE: the 1414 camera stays on alt 0 (cameras.c:318)
return {TransferStatus::completed, {}};
default:
return {TransferStatus::stall, {}};
}
}
} // namespace nui::protocol