Skip to content

Commit

Permalink
Merge pull request #2473 from mavlink/pr-server-example
Browse files Browse the repository at this point in the history
examples: Add example on how to use mavsdk_server lib
  • Loading branch information
julianoes authored Dec 18, 2024
2 parents fb5bd98 + c18538b commit f09fa8d
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
22 changes: 22 additions & 0 deletions examples/start_stop_server/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
36 changes: 36 additions & 0 deletions examples/start_stop_server/start_stop_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
//
// Example how to use C API of mavsdk_server.
//

#include <mavsdk/mavsdk.h>
#include <mavsdk/mavsdk_server/mavsdk_server_api.h>
#include <iostream>
#include <csignal>

using namespace mavsdk;

static struct MavsdkServer* mavsdk_server;

void signal_handler(int sig)
{
std::cout << "Received signal " << sig << 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(SIGINT, signal_handler);

mavsdk_server_init(&mavsdk_server);
mavsdk_server_run(mavsdk_server, argv[1], 50051);

mavsdk_server_destroy(mavsdk_server);

return 0;
}

0 comments on commit f09fa8d

Please sign in to comment.