170 lines
6.1 KiB
C++
170 lines
6.1 KiB
C++
#include "protocol/wire.h"
|
|
|
|
#include <algorithm>
|
|
#include <cstring>
|
|
#include <limits>
|
|
|
|
namespace nui::protocol {
|
|
|
|
// Exact envelope provenance (fetched as raw source, no git operation):
|
|
// https://github.com/OpenKinect/libfreenect/blob/09a1f098/src/flags.c#L184-L302
|
|
// cam_hdr: GM, uint16 LE payload WORD count, opcode, tag. Replies use RB.
|
|
std::optional<Bytes> encode_command(const CameraCommand& command) {
|
|
if ((command.magic != CameraMagic::command && command.magic != CameraMagic::reply) ||
|
|
command.payload.size() % 2 != 0 || command.payload.size() > 1016) {
|
|
return std::nullopt;
|
|
}
|
|
Bytes bytes(8 + command.payload.size());
|
|
wire::put16(bytes, 0, static_cast<uint16_t>(command.magic));
|
|
wire::put16(bytes, 2, static_cast<uint16_t>(command.payload.size() / 2));
|
|
wire::put16(bytes, 4, command.command);
|
|
wire::put16(bytes, 6, command.sequence);
|
|
std::copy(command.payload.begin(), command.payload.end(), bytes.begin() + 8);
|
|
return bytes;
|
|
}
|
|
|
|
std::optional<CameraCommand> decode_command(const Bytes& bytes) {
|
|
if (bytes.size() < 8 || bytes.size() > 1024 ||
|
|
bytes.size() != 8u + 2u * wire::u16(bytes, 2)) {
|
|
return std::nullopt;
|
|
}
|
|
const auto magic = static_cast<CameraMagic>(wire::u16(bytes, 0));
|
|
if (magic != CameraMagic::command && magic != CameraMagic::reply) {
|
|
return std::nullopt;
|
|
}
|
|
return CameraCommand{magic, wire::u16(bytes, 4), wire::u16(bytes, 6),
|
|
Bytes(bytes.begin() + 8, bytes.end())};
|
|
}
|
|
|
|
TransferResult KinectV1Device::camera_control(
|
|
const ControlSetup& setup, const Bytes& out) {
|
|
if (setup.request != 0 || setup.value != 0 || setup.index != 0) {
|
|
return {TransferStatus::stall, {}};
|
|
}
|
|
if (setup.request_type == 0xc0) {
|
|
Bytes reply = std::move(pending_reply_);
|
|
pending_reply_.clear();
|
|
reply.resize(std::min<std::size_t>(setup.length, reply.size()));
|
|
return {TransferStatus::completed, std::move(reply)};
|
|
}
|
|
if (setup.request_type != 0x40) {
|
|
return {TransferStatus::stall, {}};
|
|
}
|
|
// A new command supersedes any unread response, including on a stalled OUT.
|
|
pending_reply_.clear();
|
|
const auto command = decode_command(out);
|
|
if (!command || command->magic != CameraMagic::command) {
|
|
return {TransferStatus::stall, {}};
|
|
}
|
|
const auto payload = execute_command(*command);
|
|
if (!payload) {
|
|
return {TransferStatus::stall, {}};
|
|
}
|
|
pending_reply_ = *encode_command(
|
|
{CameraMagic::reply, command->command, command->sequence, *payload});
|
|
return {TransferStatus::completed, {}};
|
|
}
|
|
|
|
std::optional<Bytes> KinectV1Device::execute_command(const CameraCommand& command) {
|
|
const auto& payload = command.payload;
|
|
switch (command.command) {
|
|
case 0x02: { // read_register: status word followed by value
|
|
if (payload.size() != 2) return std::nullopt;
|
|
const auto found = registers_.find(wire::u16(payload, 0));
|
|
if (found == registers_.end()) return std::nullopt;
|
|
Bytes reply(4, 0);
|
|
wire::put16(reply, 2, found->second);
|
|
return reply;
|
|
}
|
|
case 0x03:
|
|
if (payload.size() != 4 ||
|
|
!write_register(wire::u16(payload, 0), wire::u16(payload, 2))) {
|
|
return std::nullopt;
|
|
}
|
|
return Bytes(2, 0);
|
|
case 0x04: {
|
|
if (payload.size() != 10 ||
|
|
std::any_of(payload.begin(), payload.end(), [](uint8_t b) { return b != 0; })) {
|
|
return std::nullopt;
|
|
}
|
|
// cameras.c:794-836: 322 bytes INCLUDING status; four LE floats at +94.
|
|
Bytes reply(322, 0);
|
|
const std::array<float, 4> calibration{
|
|
wire::kEmitterDistance, wire::kCameraDistance,
|
|
wire::kReferenceDistance, wire::kPixelSize};
|
|
static_assert(sizeof(float) == 4 && std::numeric_limits<float>::is_iec559);
|
|
for (std::size_t i = 0; i < calibration.size(); ++i) {
|
|
uint32_t bits;
|
|
std::memcpy(&bits, &calibration[i], sizeof(bits));
|
|
wire::put32(reply, 94 + i * 4, bits);
|
|
}
|
|
return reply;
|
|
}
|
|
case 0x16: {
|
|
// cameras.c:664-791. During camera_init video mode is still zeroed;
|
|
// accept that query as well as medium/30. No other mode is advertised.
|
|
if (payload.size() != 10 || wire::u16(payload, 2) != 0 ||
|
|
wire::u16(payload, 8) != 0 ||
|
|
!((wire::u16(payload, 4) == 0 && wire::u16(payload, 6) == 0) ||
|
|
(wire::u16(payload, 4) == 1 && wire::u16(payload, 6) == 30))) {
|
|
return std::nullopt;
|
|
}
|
|
switch (wire::u16(payload, 0)) {
|
|
case 0x00: {
|
|
Bytes reply(4, 0);
|
|
wire::put16(reply, 2, wire::kConstantShift);
|
|
return reply;
|
|
}
|
|
case 0x40: {
|
|
// No lens distortion in the virtual camera. Cancel libfreenect's
|
|
// +1 pixel offsets with dx_start/dy_start = -256 (registration.c:231,
|
|
// 273, 300). All polynomial derivatives are zero.
|
|
Bytes reply(118, 0);
|
|
wire::put32(reply, 2 + 5 * 4, 0xffffff00u);
|
|
wire::put32(reply, 2 + 10 * 4, 0xffffff00u);
|
|
return reply;
|
|
}
|
|
case 0x41: return Bytes(8, 0); // no crop or padded depth lines
|
|
default: return std::nullopt;
|
|
}
|
|
}
|
|
default: return std::nullopt;
|
|
}
|
|
}
|
|
|
|
bool KinectV1Device::write_register(uint16_t reg, uint16_t value) {
|
|
// Supported wire modes: 640x480 11-bit depth, GRBG Bayer, 640x488 IR
|
|
// (10-bit packed), all 30 Hz. Reject unsupported writes before changing state.
|
|
const auto found = registers_.find(reg);
|
|
if (found == registers_.end()) return false;
|
|
switch (reg) {
|
|
case 0x05:
|
|
if (value != 0 && value != 1 && value != 3) return false;
|
|
video_ = {};
|
|
break;
|
|
case 0x06:
|
|
if (value != 0 && value != 2) return false;
|
|
depth_ = {};
|
|
break;
|
|
case 0x12: if (value != 3) return false; break;
|
|
case 0x13: case 0x0d: case 0x1a:
|
|
if (value != 1) return false;
|
|
break;
|
|
case 0x14: case 0x0e: case 0x1b:
|
|
if (value != 30) return false;
|
|
break;
|
|
case 0x0c: case 0x19: case 0x105:
|
|
if (value != 0) return false;
|
|
break;
|
|
case 0x17: case 0x47: case 0x48:
|
|
if (value > 1) return false;
|
|
break;
|
|
case 0x15: if (value < 1 || value > 50) return false; break;
|
|
default: return false;
|
|
}
|
|
found->second = value;
|
|
return true;
|
|
}
|
|
|
|
} // namespace nui::protocol
|