323 lines
12 KiB
C++
323 lines
12 KiB
C++
#include <gtest/gtest.h>
|
|
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <memory>
|
|
#include <vector>
|
|
|
|
#include "protocol/protocol.h"
|
|
#include "protocol/wire.h"
|
|
#include "synth/synth.h"
|
|
|
|
namespace {
|
|
|
|
using nui::protocol::Bytes;
|
|
using nui::protocol::CameraCommand;
|
|
using nui::protocol::CameraMagic;
|
|
using nui::protocol::ControlSetup;
|
|
using nui::protocol::KinectV1Device;
|
|
using nui::protocol::SynthFrame;
|
|
using nui::protocol::TransferStatus;
|
|
using nui::protocol::UsbFunction;
|
|
|
|
constexpr uint32_t kDepthWidth = 640;
|
|
constexpr uint32_t kDepthHeight = 480;
|
|
|
|
Bytes le16_words(std::initializer_list<uint16_t> words) {
|
|
Bytes bytes;
|
|
for (const uint16_t word : words) {
|
|
bytes.push_back(static_cast<uint8_t>(word));
|
|
bytes.push_back(static_cast<uint8_t>(word >> 8));
|
|
}
|
|
return bytes;
|
|
}
|
|
|
|
// Replica of host-side raw->mm (libfreenect registration.c:320-325) under the
|
|
// virtual calibration, to prove the reassembled disparity decodes to the depth
|
|
// the synth frame carried.
|
|
// Test-local copy of streams.cpp's millimeters_to_raw, so the reassembled
|
|
// disparity can be checked bit-exact against the depth the synth frame carried.
|
|
uint16_t expected_raw(uint16_t millimeters) {
|
|
if (millimeters == 0) {
|
|
return 2047;
|
|
}
|
|
const double scaled = millimeters / 10.0;
|
|
const double metric =
|
|
(scaled - nui::protocol::wire::kReferenceDistance) *
|
|
nui::protocol::wire::kEmitterDistance / scaled;
|
|
const double fixed_ref_x = metric / nui::protocol::wire::kPixelSize;
|
|
const double raw =
|
|
4.0 * fixed_ref_x + 1.5 + 4.0 * nui::protocol::wire::kConstantShift;
|
|
const long rounded = std::lround(raw);
|
|
if (rounded < 0 || rounded >= 2047) {
|
|
return 2047;
|
|
}
|
|
return static_cast<uint16_t>(rounded);
|
|
}
|
|
|
|
double host_millimeters(uint16_t raw) {
|
|
const double fixed_ref_x =
|
|
((raw - 4.0 * nui::protocol::wire::kConstantShift) / 4.0) - 0.375;
|
|
const double metric = fixed_ref_x * nui::protocol::wire::kPixelSize;
|
|
return 10.0 * ((metric * nui::protocol::wire::kReferenceDistance /
|
|
(nui::protocol::wire::kEmitterDistance - metric)) +
|
|
nui::protocol::wire::kReferenceDistance);
|
|
}
|
|
|
|
std::vector<uint16_t> unpack_msb_first(const Bytes& data, unsigned width,
|
|
std::size_t count) {
|
|
const uint32_t mask = (1u << width) - 1u;
|
|
uint32_t buffer = 0;
|
|
unsigned bits = 0;
|
|
std::size_t cursor = 0;
|
|
std::vector<uint16_t> out;
|
|
out.reserve(count);
|
|
while (out.size() < count) {
|
|
while (bits < width) {
|
|
buffer = (buffer << 8) | data[cursor++];
|
|
bits += 8;
|
|
}
|
|
bits -= width;
|
|
out.push_back(static_cast<uint16_t>((buffer >> bits) & mask));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Bytes run_command(KinectV1Device& device, uint16_t command, const Bytes& payload) {
|
|
const auto out = nui::protocol::encode_command(
|
|
{CameraMagic::command, command, 0x1234, payload});
|
|
EXPECT_TRUE(out.has_value());
|
|
const ControlSetup write{0x40, 0, 0, 0, static_cast<uint16_t>(out->size())};
|
|
const auto wrote = device.answer_control_transfer(UsbFunction::camera, write, *out);
|
|
EXPECT_EQ(wrote.status, TransferStatus::completed);
|
|
const ControlSetup read{0xc0, 0, 0, 0, 512};
|
|
const auto reply = device.answer_control_transfer(UsbFunction::camera, read, {});
|
|
EXPECT_EQ(reply.status, TransferStatus::completed);
|
|
return reply.data;
|
|
}
|
|
|
|
Bytes command_payload(KinectV1Device& device, uint16_t command, const Bytes& in) {
|
|
const auto decoded = nui::protocol::decode_command(run_command(device, command, in));
|
|
EXPECT_TRUE(decoded.has_value());
|
|
EXPECT_EQ(decoded->magic, CameraMagic::reply);
|
|
EXPECT_EQ(decoded->command, command);
|
|
EXPECT_EQ(decoded->sequence, 0x1234);
|
|
return decoded ? decoded->payload : Bytes{};
|
|
}
|
|
|
|
std::shared_ptr<const SynthFrame> make_depth_frame() {
|
|
auto frame = std::make_shared<SynthFrame>();
|
|
frame->metadata.frame_number = 7;
|
|
frame->depth.size = {kDepthWidth, kDepthHeight};
|
|
frame->depth.pitch = kDepthWidth * 2;
|
|
frame->depth.pixels.resize(static_cast<std::size_t>(kDepthWidth) * kDepthHeight);
|
|
for (uint32_t y = 0; y < kDepthHeight; ++y) {
|
|
const uint16_t millimeters = static_cast<uint16_t>(900 + (y % 600) * 5);
|
|
for (uint32_t x = 0; x < kDepthWidth; ++x) {
|
|
frame->depth.pixels[static_cast<std::size_t>(y) * kDepthWidth + x] =
|
|
nui::synth::pack_depth(millimeters, 1);
|
|
}
|
|
}
|
|
return frame;
|
|
}
|
|
|
|
void start_depth_stream(KinectV1Device& device) {
|
|
for (const auto& pair : std::vector<std::pair<uint16_t, uint16_t>>{
|
|
{0x105, 0}, {0x06, 0}, {0x12, 3}, {0x13, 1}, {0x14, 30}, {0x06, 2}, {0x17, 0}}) {
|
|
const auto reply = command_payload(
|
|
device, 0x03, le16_words({pair.first, pair.second}));
|
|
EXPECT_EQ(reply.size(), 2u);
|
|
}
|
|
}
|
|
|
|
} // namespace
|
|
|
|
TEST(protocol, CommandEnvelopeRoundTripsWithMagicSequenceAndLength) {
|
|
const Bytes payload = le16_words({0x0006, 0x0000});
|
|
const auto encoded = nui::protocol::encode_command(
|
|
{CameraMagic::command, 0x02, 0x00ab, payload});
|
|
ASSERT_TRUE(encoded.has_value());
|
|
|
|
const Bytes expected{0x47, 0x4d, 0x02, 0x00, 0x02, 0x00,
|
|
0xab, 0x00, 0x06, 0x00, 0x00, 0x00};
|
|
EXPECT_EQ(*encoded, expected);
|
|
|
|
const auto decoded = nui::protocol::decode_command(*encoded);
|
|
ASSERT_TRUE(decoded.has_value());
|
|
EXPECT_EQ(decoded->magic, CameraMagic::command);
|
|
EXPECT_EQ(decoded->command, 0x02);
|
|
EXPECT_EQ(decoded->sequence, 0x00ab);
|
|
EXPECT_EQ(decoded->payload, payload);
|
|
}
|
|
|
|
TEST(protocol, DecodeRejectsTruncatedAndMislengthedEnvelopes) {
|
|
EXPECT_FALSE(nui::protocol::decode_command(Bytes{0x47, 0x4d, 0x01}).has_value());
|
|
Bytes wrong_length{0x47, 0x4d, 0x05, 0x00, 0x02, 0x00, 0x00, 0x00};
|
|
EXPECT_FALSE(nui::protocol::decode_command(wrong_length).has_value());
|
|
}
|
|
|
|
TEST(protocol, KnownRegisterReadReturnsTheValueThatWasWritten) {
|
|
KinectV1Device device([] { return nullptr; });
|
|
|
|
const auto write_reply = command_payload(device, 0x03, le16_words({0x0012, 0x0003}));
|
|
ASSERT_EQ(write_reply.size(), 2u);
|
|
|
|
const auto read_reply = command_payload(device, 0x02, le16_words({0x0012}));
|
|
ASSERT_EQ(read_reply.size(), 4u);
|
|
EXPECT_EQ(static_cast<uint16_t>(read_reply[2] | (read_reply[3] << 8)), 0x0003);
|
|
}
|
|
|
|
TEST(protocol, FixedParamQueriesReturnCorrectlySizedCalibrationBlocks) {
|
|
KinectV1Device device([] { return nullptr; });
|
|
|
|
const auto zero_plane = command_payload(device, 0x04, Bytes(10, 0));
|
|
EXPECT_EQ(zero_plane.size(), 322u);
|
|
|
|
const auto reg_info =
|
|
command_payload(device, 0x16, le16_words({0x40, 0, 1, 30, 0}));
|
|
EXPECT_EQ(reg_info.size(), 118u);
|
|
|
|
const auto pad_info =
|
|
command_payload(device, 0x16, le16_words({0x41, 0, 1, 30, 0}));
|
|
EXPECT_EQ(pad_info.size(), 8u);
|
|
|
|
const auto const_shift =
|
|
command_payload(device, 0x16, le16_words({0x00, 0, 1, 30, 0}));
|
|
ASSERT_EQ(const_shift.size(), 4u);
|
|
EXPECT_EQ(static_cast<uint16_t>(const_shift[2] | (const_shift[3] << 8)),
|
|
nui::protocol::wire::kConstantShift);
|
|
}
|
|
|
|
TEST(protocol, DepthFrameChunksIntoIsoPacketsThatReassembleToTheInputDepth) {
|
|
KinectV1Device device(make_depth_frame);
|
|
start_depth_stream(device);
|
|
|
|
const std::size_t frame_bytes =
|
|
static_cast<std::size_t>(kDepthWidth) * kDepthHeight * 11 / 8;
|
|
const std::size_t payload_size = 1748;
|
|
const std::size_t expected_packets =
|
|
(frame_bytes + payload_size - 1) / payload_size;
|
|
|
|
Bytes reassembled;
|
|
std::size_t packet_index = 0;
|
|
uint8_t expected_sequence = 0;
|
|
while (true) {
|
|
const auto packet = device.next_packet(UsbFunction::camera, 0x82);
|
|
ASSERT_EQ(packet.status, TransferStatus::completed);
|
|
ASSERT_GE(packet.data.size(), 12u);
|
|
EXPECT_EQ(packet.data[0], 0x52);
|
|
EXPECT_EQ(packet.data[1], 0x42);
|
|
EXPECT_EQ(packet.data[5], expected_sequence++);
|
|
|
|
const bool is_last = packet_index == expected_packets - 1;
|
|
const uint8_t expected_flag =
|
|
is_last ? 0x75 : (packet_index == 0 ? 0x71 : 0x72);
|
|
EXPECT_EQ(packet.data[3], expected_flag);
|
|
|
|
const std::size_t data_len = packet.data.size() - 12;
|
|
EXPECT_EQ(data_len, is_last ? frame_bytes % payload_size : payload_size);
|
|
reassembled.insert(reassembled.end(), packet.data.begin() + 12, packet.data.end());
|
|
if (is_last) {
|
|
break;
|
|
}
|
|
++packet_index;
|
|
}
|
|
|
|
ASSERT_EQ(packet_index + 1, expected_packets);
|
|
ASSERT_EQ(reassembled.size(), frame_bytes);
|
|
|
|
const auto raw = unpack_msb_first(
|
|
reassembled, 11, static_cast<std::size_t>(kDepthWidth) * kDepthHeight);
|
|
for (uint32_t y = 0; y < kDepthHeight; ++y) {
|
|
const uint16_t expected_mm = static_cast<uint16_t>(900 + (y % 600) * 5);
|
|
const uint16_t sample = raw[static_cast<std::size_t>(y) * kDepthWidth];
|
|
EXPECT_EQ(sample, expected_raw(expected_mm));
|
|
EXPECT_LT(sample, 2047);
|
|
EXPECT_NEAR(host_millimeters(sample), expected_mm, 40.0);
|
|
}
|
|
}
|
|
|
|
TEST(protocol, NextDepthPacketStartsOnlyAfterTheStartRegisterIsWritten) {
|
|
KinectV1Device device(make_depth_frame);
|
|
EXPECT_EQ(device.next_packet(UsbFunction::camera, 0x82).status, TransferStatus::idle);
|
|
start_depth_stream(device);
|
|
EXPECT_EQ(device.next_packet(UsbFunction::camera, 0x82).status,
|
|
TransferStatus::completed);
|
|
}
|
|
|
|
TEST(protocol, UnsupportedModeRegisterAndEndpointAndCommandAreRejected) {
|
|
KinectV1Device device([] { return nullptr; });
|
|
const auto out_status = [&](uint16_t command, const Bytes& payload) {
|
|
const auto out = nui::protocol::encode_command(
|
|
{CameraMagic::command, command, 1, payload});
|
|
return device
|
|
.answer_control_transfer(
|
|
UsbFunction::camera, {0x40, 0, 0, 0, static_cast<uint16_t>(out->size())}, *out)
|
|
.status;
|
|
};
|
|
|
|
EXPECT_EQ(out_status(0x03, le16_words({0x0012, 0x0005})), TransferStatus::stall);
|
|
EXPECT_EQ(out_status(0x02, le16_words({0x0999})), TransferStatus::stall);
|
|
EXPECT_EQ(out_status(0x77, Bytes{}), TransferStatus::stall);
|
|
|
|
EXPECT_EQ(device.next_packet(UsbFunction::camera, 0x83).status,
|
|
TransferStatus::invalid_frame);
|
|
EXPECT_EQ(device.next_packet(UsbFunction::audio, 0x81).status,
|
|
TransferStatus::stall);
|
|
}
|
|
|
|
TEST(protocol, ColorStreamEmitsGrbgBayerThatReassembles) {
|
|
auto frame = std::make_shared<SynthFrame>();
|
|
frame->color.size = {4, 4};
|
|
frame->color.pitch = 16;
|
|
frame->color.pixels.assign(4 * 4 * 4, 0);
|
|
for (std::size_t pixel = 0; pixel < 16; ++pixel) {
|
|
frame->color.pixels[pixel * 4 + 0] = 10; // blue
|
|
frame->color.pixels[pixel * 4 + 1] = 20; // green
|
|
frame->color.pixels[pixel * 4 + 2] = 30; // red
|
|
frame->color.pixels[pixel * 4 + 3] = 255;
|
|
}
|
|
KinectV1Device device([frame] { return frame; });
|
|
|
|
for (const auto& pair : std::vector<std::pair<uint16_t, uint16_t>>{
|
|
{0x0c, 0}, {0x0d, 1}, {0x0e, 30}, {0x05, 1}, {0x47, 0}}) {
|
|
device.answer_control_transfer(
|
|
UsbFunction::camera, {0x40, 0, 0, 0, 12},
|
|
*nui::protocol::encode_command(
|
|
{CameraMagic::command, 0x03, 0, le16_words({pair.first, pair.second})}));
|
|
device.answer_control_transfer(UsbFunction::camera, {0xc0, 0, 0, 0, 512}, {});
|
|
}
|
|
|
|
Bytes reassembled;
|
|
while (true) {
|
|
const auto packet = device.next_packet(UsbFunction::camera, 0x81);
|
|
ASSERT_EQ(packet.status, TransferStatus::completed);
|
|
EXPECT_EQ(packet.data[1], 0x42);
|
|
reassembled.insert(reassembled.end(), packet.data.begin() + 12, packet.data.end());
|
|
if (packet.data[3] == 0x85) {
|
|
break;
|
|
}
|
|
}
|
|
|
|
ASSERT_EQ(reassembled.size(), static_cast<std::size_t>(640) * 480);
|
|
EXPECT_EQ(reassembled[0], 20); // row0 col0: green in GRBG
|
|
EXPECT_EQ(reassembled[1], 30); // row0 col1: red
|
|
EXPECT_EQ(reassembled[640], 10); // row1 col0: blue
|
|
}
|
|
|
|
TEST(protocol, DescriptorsAdvertiseTheKinectIdentityAndSiblingTopology) {
|
|
const auto camera = nui::protocol::wire::descriptor(UsbFunction::camera, 0x0100);
|
|
ASSERT_TRUE(camera.has_value());
|
|
EXPECT_EQ(static_cast<uint16_t>((*camera)[8] | ((*camera)[9] << 8)), 0x045e);
|
|
EXPECT_EQ(static_cast<uint16_t>((*camera)[10] | ((*camera)[11] << 8)), 0x02ae);
|
|
EXPECT_EQ(static_cast<uint16_t>((*camera)[12] | ((*camera)[13] << 8)), 0x010b);
|
|
|
|
EXPECT_EQ(nui::protocol::wire::interface_count(UsbFunction::audio), 2);
|
|
EXPECT_TRUE(nui::protocol::wire::endpoint_exists(UsbFunction::camera, 0x82));
|
|
EXPECT_FALSE(nui::protocol::wire::endpoint_exists(UsbFunction::motor, 0x82));
|
|
|
|
const auto config = nui::protocol::wire::descriptor(UsbFunction::camera, 0x0200);
|
|
ASSERT_TRUE(config.has_value());
|
|
EXPECT_EQ((*config)[1], 0x02);
|
|
}
|