From e8c96cc50b7cb9bb10fb1dee561328af85a468b1 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Tue, 17 Dec 2024 14:20:40 +1300 Subject: [PATCH 1/2] examples: Add example on how to use mavsdk_server lib This just shows how to use the C API to instantiate the gRPC server. --- examples/CMakeLists.txt | 3 ++ examples/start_stop_server/CMakeLists.txt | 22 ++++++++++++ .../start_stop_server/start_stop_server.cpp | 36 +++++++++++++++++++ 3 files changed, 61 insertions(+) create mode 100644 examples/start_stop_server/CMakeLists.txt create mode 100644 examples/start_stop_server/start_stop_server.cpp diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 183a27865..66c59d252 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -46,3 +46,6 @@ add_subdirectory(transponder) add_subdirectory(tune) add_subdirectory(vtol_transition) add_subdirectory(winch) + +# Disabled, requires mavsdk_server library. +#add_subdirectory(start_stop_server) diff --git a/examples/start_stop_server/CMakeLists.txt b/examples/start_stop_server/CMakeLists.txt new file mode 100644 index 000000000..f520f98e4 --- /dev/null +++ b/examples/start_stop_server/CMakeLists.txt @@ -0,0 +1,22 @@ +cmake_minimum_required(VERSION 3.10.2) + +set(CMAKE_CXX_STANDARD 17) +set(CMAKE_CXX_STANDARD_REQUIRED ON) + +project(start_stop_server) + +add_executable(start_stop_server + start_stop_server.cpp +) + +find_package(MAVSDK REQUIRED) + +target_link_libraries(start_stop_server + MAVSDK::mavsdk_server +) + +if(NOT MSVC) + add_compile_options(start_stop_server PRIVATE -Wall -Wextra) +else() + add_compile_options(start_stop_server PRIVATE -W2) +endif() diff --git a/examples/start_stop_server/start_stop_server.cpp b/examples/start_stop_server/start_stop_server.cpp new file mode 100644 index 000000000..6bad0bfa2 --- /dev/null +++ b/examples/start_stop_server/start_stop_server.cpp @@ -0,0 +1,36 @@ +// +// Example how to use C API of mavsdk_server. +// + +#include +#include +#include +#include + +using namespace mavsdk; + +static struct MavsdkServer* mavsdk_server; + +void signal_handler(int sig) +{ + std::cout << "Received SIGUSR1 signal" << std::endl; + + mavsdk_server_stop(mavsdk_server); +} + +int main(int argc, char* argv[]) +{ + if (argc != 2) { + std::cout << "Connection string required as argument" << std::endl; + return 1; + } + + signal(SIGUSR1, signal_handler); + + mavsdk_server_init(&mavsdk_server); + mavsdk_server_run(mavsdk_server, argv[1], 50051); + + mavsdk_server_destroy(mavsdk_server); + + return 0; +} From c18538b001d741797bbdd647c1062e8ecd999320 Mon Sep 17 00:00:00 2001 From: Julian Oes Date: Wed, 18 Dec 2024 15:19:30 +1300 Subject: [PATCH 2/2] examples: close with Ctrl+C instead of SIGUSR1 --- examples/start_stop_server/start_stop_server.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/start_stop_server/start_stop_server.cpp b/examples/start_stop_server/start_stop_server.cpp index 6bad0bfa2..ff7286a67 100644 --- a/examples/start_stop_server/start_stop_server.cpp +++ b/examples/start_stop_server/start_stop_server.cpp @@ -13,7 +13,7 @@ static struct MavsdkServer* mavsdk_server; void signal_handler(int sig) { - std::cout << "Received SIGUSR1 signal" << std::endl; + std::cout << "Received signal " << sig << std::endl; mavsdk_server_stop(mavsdk_server); } @@ -25,7 +25,7 @@ int main(int argc, char* argv[]) return 1; } - signal(SIGUSR1, signal_handler); + signal(SIGINT, signal_handler); mavsdk_server_init(&mavsdk_server); mavsdk_server_run(mavsdk_server, argv[1], 50051);