feat(attach-linux,fallback-openni2): usbip shim (socket-verified) + OpenNI2 SynthKinect driver (depth verified via real consumer) (wave 6; todo 15 done, 9 partial)
This commit is contained in:
1 parent
cb311381a2
commit
76f3b6447f
19 files changed
+2241
-8
No files matched your search
@@ -0,0 +1,235 @@
|
||||
#include "attach_linux/usbip_protocol.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace attach::usbip {
|
||||
namespace {
|
||||
|
||||
void put_be16(Bytes& out, std::size_t offset, uint16_t value) {
|
||||
out[offset] = static_cast<uint8_t>(value >> 8);
|
||||
out[offset + 1] = static_cast<uint8_t>(value);
|
||||
}
|
||||
|
||||
void put_be32(Bytes& out, std::size_t offset, uint32_t value) {
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
out[offset + i] = static_cast<uint8_t>(value >> (8 * (3 - i)));
|
||||
}
|
||||
}
|
||||
|
||||
uint16_t get_be16(const Bytes& bytes, std::size_t offset) {
|
||||
return static_cast<uint16_t>((bytes[offset] << 8) | bytes[offset + 1]);
|
||||
}
|
||||
|
||||
uint32_t get_be32(const Bytes& bytes, std::size_t offset) {
|
||||
return (static_cast<uint32_t>(bytes[offset]) << 24) |
|
||||
(static_cast<uint32_t>(bytes[offset + 1]) << 16) |
|
||||
(static_cast<uint32_t>(bytes[offset + 2]) << 8) |
|
||||
static_cast<uint32_t>(bytes[offset + 3]);
|
||||
}
|
||||
|
||||
void put_padded_string(Bytes& out, std::size_t offset, const std::string& value, std::size_t field_size) {
|
||||
const std::size_t copy_len = std::min(value.size(), field_size - 1);
|
||||
std::copy_n(value.begin(), copy_len, out.begin() + static_cast<std::ptrdiff_t>(offset));
|
||||
}
|
||||
|
||||
void append_device_fixed_fields(Bytes& out, const ExportedDevice& device) {
|
||||
const std::size_t base = out.size();
|
||||
Bytes fixed(0x14, 0);
|
||||
put_be32(fixed, 0x00, device.busnum);
|
||||
put_be32(fixed, 0x04, device.devnum);
|
||||
put_be32(fixed, 0x08, device.speed);
|
||||
put_be16(fixed, 0x0c, device.id_vendor);
|
||||
put_be16(fixed, 0x0e, device.id_product);
|
||||
put_be16(fixed, 0x10, device.bcd_device);
|
||||
fixed[0x12] = device.device_class;
|
||||
fixed[0x13] = device.device_subclass;
|
||||
out.insert(out.end(), fixed.begin(), fixed.end());
|
||||
out.push_back(device.device_protocol);
|
||||
out.push_back(device.configuration_value);
|
||||
out.push_back(device.num_configurations);
|
||||
out.push_back(static_cast<uint8_t>(device.interfaces.size()));
|
||||
(void)base;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
std::optional<OpHeader> decode_op_header(const Bytes& bytes) {
|
||||
if (bytes.size() < 8) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return OpHeader{get_be16(bytes, 0), get_be16(bytes, 2), get_be32(bytes, 4)};
|
||||
}
|
||||
|
||||
Bytes encode_op_header(uint16_t code, uint32_t status) {
|
||||
Bytes out(8, 0);
|
||||
put_be16(out, 0, kProtocolVersion);
|
||||
put_be16(out, 2, code);
|
||||
put_be32(out, 4, status);
|
||||
return out;
|
||||
}
|
||||
|
||||
void append_device_entry(Bytes& out, const ExportedDevice& device) {
|
||||
const std::size_t path_offset = out.size();
|
||||
out.resize(out.size() + 256, 0);
|
||||
put_padded_string(out, path_offset, device.path, 256);
|
||||
|
||||
const std::size_t busid_offset = out.size();
|
||||
out.resize(out.size() + 32, 0);
|
||||
put_padded_string(out, busid_offset, device.busid, 32);
|
||||
|
||||
append_device_fixed_fields(out, device);
|
||||
|
||||
for (const auto& iface : device.interfaces) {
|
||||
out.push_back(iface.interface_class);
|
||||
out.push_back(iface.interface_subclass);
|
||||
out.push_back(iface.interface_protocol);
|
||||
out.push_back(0);
|
||||
}
|
||||
}
|
||||
|
||||
Bytes encode_rep_devlist(const std::vector<ExportedDevice>& devices) {
|
||||
Bytes out = encode_op_header(kOpRepDevlist, 0);
|
||||
Bytes count(4, 0);
|
||||
put_be32(count, 0, static_cast<uint32_t>(devices.size()));
|
||||
out.insert(out.end(), count.begin(), count.end());
|
||||
for (const auto& device : devices) {
|
||||
append_device_entry(out, device);
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
std::optional<std::string> decode_req_import_busid(const Bytes& body) {
|
||||
if (body.size() < 32) {
|
||||
return std::nullopt;
|
||||
}
|
||||
const auto nul = std::find(body.begin(), body.begin() + 32, uint8_t{0});
|
||||
return std::string(body.begin(), nul);
|
||||
}
|
||||
|
||||
Bytes encode_rep_import_ok(const ExportedDevice& device) {
|
||||
Bytes out = encode_op_header(kOpRepImport, 0);
|
||||
const std::size_t path_offset = out.size();
|
||||
out.resize(out.size() + 256, 0);
|
||||
put_padded_string(out, path_offset, device.path, 256);
|
||||
|
||||
const std::size_t busid_offset = out.size();
|
||||
out.resize(out.size() + 32, 0);
|
||||
put_padded_string(out, busid_offset, device.busid, 32);
|
||||
|
||||
append_device_fixed_fields(out, device);
|
||||
return out;
|
||||
}
|
||||
|
||||
Bytes encode_rep_import_error() {
|
||||
return encode_op_header(kOpRepImport, 1);
|
||||
}
|
||||
|
||||
std::optional<HeaderBasic> decode_header_basic(const Bytes& bytes, std::size_t offset) {
|
||||
if (bytes.size() < offset + kHeaderBasicSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return HeaderBasic{
|
||||
get_be32(bytes, offset + 0x00),
|
||||
get_be32(bytes, offset + 0x04),
|
||||
get_be32(bytes, offset + 0x08),
|
||||
get_be32(bytes, offset + 0x0c),
|
||||
get_be32(bytes, offset + 0x10),
|
||||
};
|
||||
}
|
||||
|
||||
void append_header_basic(Bytes& out, const HeaderBasic& basic) {
|
||||
const std::size_t base = out.size();
|
||||
out.resize(out.size() + kHeaderBasicSize, 0);
|
||||
put_be32(out, base + 0x00, basic.command);
|
||||
put_be32(out, base + 0x04, basic.seqnum);
|
||||
put_be32(out, base + 0x08, basic.devid);
|
||||
put_be32(out, base + 0x0c, basic.direction);
|
||||
put_be32(out, base + 0x10, basic.ep);
|
||||
}
|
||||
|
||||
SetupPacket decode_setup(const std::array<uint8_t, 8>& raw) {
|
||||
return SetupPacket{
|
||||
raw[0],
|
||||
raw[1],
|
||||
static_cast<uint16_t>(raw[2] | (raw[3] << 8)),
|
||||
static_cast<uint16_t>(raw[4] | (raw[5] << 8)),
|
||||
static_cast<uint16_t>(raw[6] | (raw[7] << 8)),
|
||||
};
|
||||
}
|
||||
|
||||
std::array<uint8_t, 8> encode_setup(const SetupPacket& setup) {
|
||||
return {
|
||||
setup.request_type,
|
||||
setup.request,
|
||||
static_cast<uint8_t>(setup.value),
|
||||
static_cast<uint8_t>(setup.value >> 8),
|
||||
static_cast<uint8_t>(setup.index),
|
||||
static_cast<uint8_t>(setup.index >> 8),
|
||||
static_cast<uint8_t>(setup.length),
|
||||
static_cast<uint8_t>(setup.length >> 8),
|
||||
};
|
||||
}
|
||||
|
||||
std::optional<CmdSubmit> decode_cmd_submit(const Bytes& bytes) {
|
||||
const auto basic = decode_header_basic(bytes, 0);
|
||||
if (!basic || basic->command != kCmdSubmit || bytes.size() < kSubmitHeaderSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
CmdSubmit submit{};
|
||||
submit.basic = *basic;
|
||||
submit.transfer_flags = get_be32(bytes, 0x14);
|
||||
submit.transfer_buffer_length = get_be32(bytes, 0x18);
|
||||
submit.start_frame = static_cast<int32_t>(get_be32(bytes, 0x1c));
|
||||
submit.number_of_packets = get_be32(bytes, 0x20);
|
||||
submit.interval = get_be32(bytes, 0x24);
|
||||
std::copy_n(bytes.begin() + 0x28, 8, submit.setup.begin());
|
||||
|
||||
if (submit.basic.direction == kDirOut) {
|
||||
if (bytes.size() < kSubmitHeaderSize + submit.transfer_buffer_length) {
|
||||
return std::nullopt;
|
||||
}
|
||||
submit.transfer_buffer.assign(
|
||||
bytes.begin() + static_cast<std::ptrdiff_t>(kSubmitHeaderSize),
|
||||
bytes.begin() + static_cast<std::ptrdiff_t>(kSubmitHeaderSize + submit.transfer_buffer_length));
|
||||
}
|
||||
return submit;
|
||||
}
|
||||
|
||||
Bytes encode_ret_submit(
|
||||
uint32_t seqnum,
|
||||
int32_t status,
|
||||
const Bytes& transfer_buffer,
|
||||
int32_t start_frame,
|
||||
uint32_t number_of_packets,
|
||||
int32_t error_count) {
|
||||
Bytes out;
|
||||
append_header_basic(out, HeaderBasic{kRetSubmit, seqnum, 0, 0, 0});
|
||||
Bytes tail(0x1c, 0);
|
||||
put_be32(tail, 0x00, static_cast<uint32_t>(status));
|
||||
put_be32(tail, 0x04, static_cast<uint32_t>(transfer_buffer.size()));
|
||||
put_be32(tail, 0x08, static_cast<uint32_t>(start_frame));
|
||||
put_be32(tail, 0x0c, number_of_packets);
|
||||
put_be32(tail, 0x10, static_cast<uint32_t>(error_count));
|
||||
out.insert(out.end(), tail.begin(), tail.end());
|
||||
out.insert(out.end(), transfer_buffer.begin(), transfer_buffer.end());
|
||||
return out;
|
||||
}
|
||||
|
||||
std::optional<CmdUnlink> decode_cmd_unlink(const Bytes& bytes) {
|
||||
const auto basic = decode_header_basic(bytes, 0);
|
||||
if (!basic || basic->command != kCmdUnlink || bytes.size() < kUnlinkHeaderSize) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return CmdUnlink{*basic, get_be32(bytes, 0x14)};
|
||||
}
|
||||
|
||||
Bytes encode_ret_unlink(uint32_t seqnum, int32_t status) {
|
||||
Bytes out;
|
||||
append_header_basic(out, HeaderBasic{kRetUnlink, seqnum, 0, 0, 0});
|
||||
out.resize(out.size() + 0x1c, 0);
|
||||
put_be32(out, kHeaderBasicSize, static_cast<uint32_t>(status));
|
||||
return out;
|
||||
}
|
||||
|
||||
} // namespace attach::usbip
|
||||
Reference in new issue
Block a user