diff --git a/CMakeLists.txt b/CMakeLists.txt index 18d4d49d..32e5f587 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,6 +27,7 @@ option(WIVRN_USE_ANDROID_VALIDATION_LAYER "Download the android version of the V option(WIVRN_BUILD_CLIENT "Build WiVRn client" OFF) option(WIVRN_BUILD_SERVER "Build WiVRn server" ON) option(WIVRN_BUILD_DASHBOARD "Build WiVRn dashboard" OFF) +option(WIVRN_BUILD_WIVRNCTL "Build wivrnctl" ON) option(WIVRN_BUILD_DISSECTOR "Build Wireshark dissector" OFF) option(WIVRN_WERROR "Treat warnings as errors" OFF) @@ -220,6 +221,11 @@ if (WIVRN_BUILD_DISSECTOR) find_package(Wireshark REQUIRED) endif() +if (WIVRN_BUILD_WIVRNCTL) + find_package(CLI11 REQUIRED) + pkg_check_modules(SYSTEMD REQUIRED IMPORTED_TARGET libsystemd) +endif() + # Common dependencies FetchContent_Declare(boostpfr EXCLUDE_FROM_ALL SYSTEM URL https://github.com/boostorg/pfr/archive/refs/tags/2.2.0.tar.gz) FetchContent_Declare(boost EXCLUDE_FROM_ALL SYSTEM URL https://github.com/boostorg/boost/releases/download/boost-1.84.0/boost-1.84.0.tar.xz) diff --git a/server/start_application.cpp b/server/start_application.cpp index e2943df4..e23f0c82 100644 --- a/server/start_application.cpp +++ b/server/start_application.cpp @@ -35,7 +35,7 @@ static int get_service_pid(const std::string & service_name, pid_t & pid) int ret = 0; - // Connect to the system bus + // Connect to the session bus ret = sd_bus_open_user(&bus); if (ret < 0) { diff --git a/tools/CMakeLists.txt b/tools/CMakeLists.txt index 3cef64b4..0fc6655d 100644 --- a/tools/CMakeLists.txt +++ b/tools/CMakeLists.txt @@ -1,3 +1,7 @@ if(WIVRN_BUILD_DISSECTOR) add_subdirectory(wireshark) endif() + +if (WIVRN_BUILD_WIVRNCTL) + add_subdirectory(wivrnctl) +endif() diff --git a/tools/wivrnctl/CMakeLists.txt b/tools/wivrnctl/CMakeLists.txt new file mode 100644 index 00000000..5308197e --- /dev/null +++ b/tools/wivrnctl/CMakeLists.txt @@ -0,0 +1,6 @@ +add_executable(wivrnctl main.cpp) +set_target_properties(wivrnctl PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/server) +target_link_libraries(wivrnctl PRIVATE CLI11::CLI11 PkgConfig::SYSTEMD) + +install(TARGETS wivrnctl) + diff --git a/tools/wivrnctl/main.cpp b/tools/wivrnctl/main.cpp new file mode 100644 index 00000000..bee0a1ae --- /dev/null +++ b/tools/wivrnctl/main.cpp @@ -0,0 +1,123 @@ +/* + * WiVRn VR streaming + * Copyright (C) 2024 Patrick Nicolas + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include +#include + +struct deleter +{ + void operator()(sd_bus * p) + { + sd_bus_unref(p); + } + void operator()(sd_bus_message * p) + { + sd_bus_message_unref(p); + } +}; + +using sd_bus_ptr = std::unique_ptr; +using sd_bus_message_ptr = std::unique_ptr; + +const char * destination = "io.github.wivrn.Server"; +const char * path = "/io/github/wivrn/Server"; +const char * interface = "io.github.wivrn.Server"; + +sd_bus_ptr get_user_bus() +{ + sd_bus * bus = nullptr; + int ret = sd_bus_open_user(&bus); + if (ret < 0) + throw std::runtime_error(std::string("failed to connect to dbus: ") + strerror(-ret)); + + return sd_bus_ptr(bus); +} + +template +sd_bus_message_ptr call_method(const sd_bus_ptr & bus, const char * member, const char * signature, Args &&... args) +{ + sd_bus_error error = SD_BUS_ERROR_NULL; + sd_bus_message * msg = nullptr; + int ret = sd_bus_call_method(bus.get(), destination, path, interface, member, &error, &msg, signature, std::forward(args)...); + if (ret < 0) + { + std::runtime_error e(std::string("call to ") + member + " failed: " + error.message); + sd_bus_error_free(&error); + throw e; + } + return sd_bus_message_ptr(msg); +} + +void stop_server() +{ + call_method(get_user_bus(), + "Quit", + ""); +} + +void enroll(int duration) +{ + auto pin_msg = call_method(get_user_bus(), + "EnrollHeadset", + "i", + duration * 60); + + const char * pin; + int ret = sd_bus_message_read(pin_msg.get(), "s", &pin); + if (ret < 0) + throw std::system_error(-ret, std::system_category(), "Failed to read PIN"); + + std::cout << "PIN: " << pin << std::endl; +} + +int main(int argc, char ** argv) +{ + CLI::App app; + + app.require_subcommand(1); + + int enroll_duration; + auto enroll_command = app.add_subcommand("enroll", "Allow a new headset to connect") + ->callback([&]() { return enroll(enroll_duration); }); + auto duration = enroll_command + ->add_option("--duration,-d", enroll_duration, "Duration in minutes to allow new connections") + ->transform(CLI::Validator([](std::string & input) { + if (input.starts_with("-")) + throw CLI::ValidationError("duration must be positive"); + if (input == "unlimited") + input = "-1"; + return ""; + }, + "", + "")) + ->default_val(2) + ->option_text("INT|unlimited"); + + app.add_subcommand("stop-server", "Stop wivrn-server process") + ->callback(stop_server); + + try + { + CLI11_PARSE(app, argc, argv); + } + catch (std::exception & e) + { + std::cerr << e.what() << std::endl; + return 1; + } +}