133 lines
3.9 KiB
C++
133 lines
3.9 KiB
C++
#include <csignal>
|
|
#include <cstring>
|
|
#include <iostream>
|
|
#include <thread>
|
|
|
|
#include "attach_linux/kinect_export.h"
|
|
#include "attach_linux/usbip_server.h"
|
|
#include "attach_linux/vhci_lifecycle.h"
|
|
|
|
namespace {
|
|
|
|
using attach::linux_attach::UsbipServer;
|
|
|
|
void print_usage(std::ostream& out) {
|
|
out << "kinect-attach - USB/IP server + vhci attach lifecycle for the virtual Kinect v1\n\n"
|
|
<< "Usage:\n"
|
|
<< " kinect-attach serve [--port PORT]\n"
|
|
<< " Run the USB/IP server (exports busids 1-1 camera, 1-2 audio, 1-3 motor).\n"
|
|
<< " kinect-attach attach --host HOST [--port PORT] --busid BUSID\n"
|
|
<< " Load vhci-hcd if needed, then run 'usbip attach' against a running server.\n"
|
|
<< " kinect-attach detach --vhci-port PORT\n"
|
|
<< " Run 'usbip detach' for the given vhci port.\n"
|
|
<< " kinect-attach list\n"
|
|
<< " Run 'usbip port' to list attached vhci ports.\n"
|
|
<< " kinect-attach --help\n";
|
|
}
|
|
|
|
std::optional<std::string> flag_value(int argc, char** argv, const std::string& name) {
|
|
for (int i = 0; i < argc - 1; ++i) {
|
|
if (argv[i] == name) {
|
|
return std::string(argv[i + 1]);
|
|
}
|
|
}
|
|
return std::nullopt;
|
|
}
|
|
|
|
UsbipServer* g_server_for_signal = nullptr;
|
|
|
|
void handle_sigint(int) {
|
|
if (g_server_for_signal) {
|
|
g_server_for_signal->stop();
|
|
}
|
|
}
|
|
|
|
int run_serve(int argc, char** argv) {
|
|
uint16_t port = attach::linux_attach::kDefaultPort;
|
|
if (const auto value = flag_value(argc, argv, "--port")) {
|
|
port = static_cast<uint16_t>(std::stoi(*value));
|
|
}
|
|
|
|
auto frame_source = attach::linux_attach::placeholder_frame_source();
|
|
nui::protocol::KinectV1Device device([frame_source] { return frame_source; });
|
|
const auto devices = attach::linux_attach::default_export_set(1);
|
|
UsbipServer server(device, devices);
|
|
|
|
g_server_for_signal = &server;
|
|
std::signal(SIGINT, handle_sigint);
|
|
std::signal(SIGTERM, handle_sigint);
|
|
|
|
std::cerr << "kinect-attach: serving USB/IP on port " << port
|
|
<< " (busids 1-1 camera, 1-2 audio, 1-3 motor)\n";
|
|
return server.run(port);
|
|
}
|
|
|
|
int run_attach(int argc, char** argv) {
|
|
const auto host = flag_value(argc, argv, "--host");
|
|
const auto busid = flag_value(argc, argv, "--busid");
|
|
if (!host || !busid) {
|
|
std::cerr << "attach requires --host and --busid\n";
|
|
return 2;
|
|
}
|
|
uint16_t port = attach::linux_attach::kDefaultPort;
|
|
if (const auto value = flag_value(argc, argv, "--port")) {
|
|
port = static_cast<uint16_t>(std::stoi(*value));
|
|
}
|
|
|
|
const auto probe = attach::linux_attach::usbip_list_ports();
|
|
if (!probe.succeeded) {
|
|
std::cerr << "'usbip port' failed (is the usbip userspace installed and vhci-hcd loaded?): "
|
|
<< probe.output;
|
|
}
|
|
|
|
const auto result = attach::linux_attach::usbip_attach(*host, port, *busid);
|
|
std::cout << result.output;
|
|
return result.succeeded ? 0 : 1;
|
|
}
|
|
|
|
int run_detach(int argc, char** argv) {
|
|
const auto vhci_port = flag_value(argc, argv, "--vhci-port");
|
|
if (!vhci_port) {
|
|
std::cerr << "detach requires --vhci-port\n";
|
|
return 2;
|
|
}
|
|
const auto result = attach::linux_attach::usbip_detach(std::stoi(*vhci_port));
|
|
std::cout << result.output;
|
|
return result.succeeded ? 0 : 1;
|
|
}
|
|
|
|
int run_list() {
|
|
const auto result = attach::linux_attach::usbip_list_ports();
|
|
std::cout << result.output;
|
|
return result.succeeded ? 0 : 1;
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char** argv) {
|
|
if (argc < 2) {
|
|
print_usage(std::cerr);
|
|
return 2;
|
|
}
|
|
const std::string command = argv[1];
|
|
if (command == "--help" || command == "-h" || command == "help") {
|
|
print_usage(std::cout);
|
|
return 0;
|
|
}
|
|
if (command == "serve") {
|
|
return run_serve(argc - 2, argv + 2);
|
|
}
|
|
if (command == "attach") {
|
|
return run_attach(argc - 2, argv + 2);
|
|
}
|
|
if (command == "detach") {
|
|
return run_detach(argc - 2, argv + 2);
|
|
}
|
|
if (command == "list") {
|
|
return run_list();
|
|
}
|
|
std::cerr << "unknown command: " << command << "\n";
|
|
print_usage(std::cerr);
|
|
return 2;
|
|
}
|