126 lines
4.9 KiB
C++
126 lines
4.9 KiB
C++
#include <cassert>
|
|
#include <iostream>
|
|
|
|
#include "attach_linux/kinect_export.h"
|
|
#include "attach_linux/usbip_protocol.h"
|
|
|
|
namespace {
|
|
|
|
void test_op_header_roundtrip() {
|
|
const auto encoded = attach::usbip::encode_op_header(attach::usbip::kOpReqDevlist, 0);
|
|
const auto decoded = attach::usbip::decode_op_header(encoded);
|
|
assert(decoded.has_value());
|
|
assert(decoded->version == attach::usbip::kProtocolVersion);
|
|
assert(decoded->code == attach::usbip::kOpReqDevlist);
|
|
assert(decoded->status == 0);
|
|
}
|
|
|
|
void test_rep_devlist_layout() {
|
|
const auto devices = attach::linux_attach::default_export_set(7);
|
|
const auto encoded = attach::usbip::encode_rep_devlist(
|
|
{devices.devices.begin(), devices.devices.end()});
|
|
const auto header = attach::usbip::decode_op_header(encoded);
|
|
assert(header.has_value());
|
|
assert(header->code == attach::usbip::kOpRepDevlist);
|
|
// 8-byte OP header + 4-byte device count + 3 entries, each
|
|
// 256 (path) + 32 (busid) + 0x14 fixed + 4 tail bytes + 4 bytes per interface.
|
|
const std::size_t camera_entry = 256 + 32 + 0x14 + 4 + 1 * 4;
|
|
const std::size_t audio_entry = 256 + 32 + 0x14 + 4 + 2 * 4;
|
|
const std::size_t motor_entry = 256 + 32 + 0x14 + 4 + 1 * 4;
|
|
assert(encoded.size() == 8 + 4 + camera_entry + audio_entry + motor_entry);
|
|
}
|
|
|
|
void test_import_busid_roundtrip() {
|
|
attach::usbip::Bytes body(32, 0);
|
|
const std::string busid = "1-1";
|
|
std::copy(busid.begin(), busid.end(), body.begin());
|
|
const auto decoded = attach::usbip::decode_req_import_busid(body);
|
|
assert(decoded.has_value());
|
|
assert(*decoded == busid);
|
|
}
|
|
|
|
void test_header_basic_roundtrip() {
|
|
attach::usbip::Bytes out;
|
|
const attach::usbip::HeaderBasic basic{
|
|
attach::usbip::kCmdSubmit, 42, (1u << 16) | 1u, attach::usbip::kDirIn, 2};
|
|
attach::usbip::append_header_basic(out, basic);
|
|
const auto decoded = attach::usbip::decode_header_basic(out);
|
|
assert(decoded.has_value());
|
|
assert(decoded->command == basic.command);
|
|
assert(decoded->seqnum == basic.seqnum);
|
|
assert(decoded->devid == basic.devid);
|
|
assert(decoded->direction == basic.direction);
|
|
assert(decoded->ep == basic.ep);
|
|
}
|
|
|
|
void test_setup_roundtrip() {
|
|
const attach::usbip::SetupPacket setup{0xc0, 0x06, 0x0100, 0x0000, 0x0012};
|
|
const auto encoded = attach::usbip::encode_setup(setup);
|
|
const auto decoded = attach::usbip::decode_setup(encoded);
|
|
assert(decoded.request_type == setup.request_type);
|
|
assert(decoded.request == setup.request);
|
|
assert(decoded.value == setup.value);
|
|
assert(decoded.index == setup.index);
|
|
assert(decoded.length == setup.length);
|
|
}
|
|
|
|
void test_cmd_submit_roundtrip_out_direction() {
|
|
attach::usbip::Bytes packet;
|
|
attach::usbip::append_header_basic(
|
|
packet, attach::usbip::HeaderBasic{
|
|
attach::usbip::kCmdSubmit, 3, (1u << 16) | 1u, attach::usbip::kDirOut, 0});
|
|
// transfer_flags, transfer_buffer_length, start_frame, number_of_packets,
|
|
// interval: 5 fields of 4 bytes each (offsets 0x14..0x28).
|
|
packet.resize(packet.size() + 5 * 4, 0);
|
|
const attach::usbip::Bytes payload = {0x4d, 0x47, 0x00, 0x04, 0x00, 0x02, 0x00, 0x00};
|
|
packet[0x1b] = static_cast<uint8_t>(payload.size()); // transfer_buffer_length, big-endian
|
|
const auto setup_bytes = attach::usbip::encode_setup(
|
|
attach::usbip::SetupPacket{0x40, 0, 0, 0, 8});
|
|
packet.insert(packet.end(), setup_bytes.begin(), setup_bytes.end());
|
|
packet.insert(packet.end(), payload.begin(), payload.end());
|
|
|
|
const auto decoded = attach::usbip::decode_cmd_submit(packet);
|
|
assert(decoded.has_value());
|
|
assert(decoded->basic.direction == attach::usbip::kDirOut);
|
|
assert(decoded->transfer_buffer_length == payload.size());
|
|
assert(decoded->transfer_buffer == payload);
|
|
}
|
|
|
|
void test_ret_submit_in_direction_carries_payload() {
|
|
const attach::usbip::Bytes payload = {1, 2, 3, 4};
|
|
const auto encoded = attach::usbip::encode_ret_submit(9, 0, payload);
|
|
const auto basic = attach::usbip::decode_header_basic(encoded);
|
|
assert(basic.has_value());
|
|
assert(basic->command == attach::usbip::kRetSubmit);
|
|
assert(basic->seqnum == 9);
|
|
assert(encoded.size() == attach::usbip::kHeaderBasicSize + 0x1c + payload.size());
|
|
}
|
|
|
|
void test_cmd_unlink_roundtrip() {
|
|
attach::usbip::Bytes packet;
|
|
attach::usbip::append_header_basic(
|
|
packet, attach::usbip::HeaderBasic{attach::usbip::kCmdUnlink, 5, 0, 0, 0});
|
|
packet.resize(packet.size() + 4, 0);
|
|
packet[packet.size() - 1] = 11; // unlink_seqnum = 11
|
|
packet.resize(attach::usbip::kUnlinkHeaderSize, 0);
|
|
|
|
const auto decoded = attach::usbip::decode_cmd_unlink(packet);
|
|
assert(decoded.has_value());
|
|
assert(decoded->unlink_seqnum == 11);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main() {
|
|
test_op_header_roundtrip();
|
|
test_rep_devlist_layout();
|
|
test_import_busid_roundtrip();
|
|
test_header_basic_roundtrip();
|
|
test_setup_roundtrip();
|
|
test_cmd_submit_roundtrip_out_direction();
|
|
test_ret_submit_in_direction_carries_payload();
|
|
test_cmd_unlink_roundtrip();
|
|
std::cout << "usbip_protocol_roundtrip: all assertions passed\n";
|
|
return 0;
|
|
}
|