Skip to content

Commit

Permalink
Merge pull request #1918 from CY-1992/add-version-type
Browse files Browse the repository at this point in the history
Add software version type to system info
  • Loading branch information
julianoes authored Nov 2, 2022
2 parents 7ebcc68 + f745a76 commit 9177ef0
Show file tree
Hide file tree
Showing 11 changed files with 461 additions and 36 deletions.
1 change: 1 addition & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ add_subdirectory(multiple_drones)
add_subdirectory(offboard)
add_subdirectory(parachute)
add_subdirectory(set_actuator)
add_subdirectory(system_info)
add_subdirectory(takeoff_and_land)
add_subdirectory(terminate)
add_subdirectory(transponder)
Expand Down
22 changes: 22 additions & 0 deletions examples/system_info/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(system_info)

add_executable(system_info
system_info.cpp
)

find_package(MAVSDK REQUIRED)

target_link_libraries(system_info
MAVSDK::mavsdk
)

if(NOT MSVC)
add_compile_options(takeoff_and_land PRIVATE -Wall -Wextra)
else()
add_compile_options(takeoff_and_land PRIVATE -WX -W2)
endif()
100 changes: 100 additions & 0 deletions examples/system_info/system_info.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
//
// Simple example to demonstrate how to query system info using MAVSDK.
//

#include <chrono>
#include <cstdint>
#include <mavsdk/mavsdk.h>
#include <mavsdk/plugins/info/info.h>
#include <iostream>
#include <future>
#include <memory>
#include <thread>

using namespace mavsdk;
using std::chrono::seconds;
using std::this_thread::sleep_for;

void usage(const std::string& bin_name)
{
std::cerr << "Usage : " << bin_name << " <connection_url>\n"
<< "Connection URL format should be :\n"
<< " For TCP : tcp://[server_host][:server_port]\n"
<< " For UDP : udp://[bind_host][:bind_port]\n"
<< " For Serial : serial:///path/to/serial/dev[:baudrate]\n"
<< "For example, to connect to the simulator use URL: udp://:14540\n";
}

std::shared_ptr<System> get_system(Mavsdk& mavsdk)
{
std::cout << "Waiting to discover system...\n";
auto prom = std::promise<std::shared_ptr<System>>{};
auto fut = prom.get_future();

// We wait for new systems to be discovered, once we find one that has an
// autopilot, we decide to use it.
Mavsdk::NewSystemHandle handle = mavsdk.subscribe_on_new_system([&mavsdk, &prom, &handle]() {
auto system = mavsdk.systems().back();

if (system->has_autopilot()) {
std::cout << "Discovered autopilot\n";

// Unsubscribe again as we only want to find one system.
mavsdk.unsubscribe_on_new_system(handle);
prom.set_value(system);
}
});

// We usually receive heartbeats at 1Hz, therefore we should find a
// system after around 3 seconds max, surely.
if (fut.wait_for(seconds(3)) == std::future_status::timeout) {
std::cerr << "No autopilot found.\n";
return {};
}

// Get discovered system now.
return fut.get();
}

int main(int argc, char** argv)
{
if (argc != 2) {
usage(argv[0]);
return 1;
}

Mavsdk mavsdk;
ConnectionResult connection_result = mavsdk.add_any_connection(argv[1]);

if (connection_result != ConnectionResult::Success) {
std::cerr << "Connection failed: " << connection_result << '\n';
return 1;
}

auto system = get_system(mavsdk);
if (!system) {
return 1;
}

auto info = Info{system};

// Wait until version/firmware information has been populated from the vehicle
while (info.get_identification().first == Info::Result::InformationNotReceivedYet) {
std::cout << "Waiting for Version information to populate from system." << '\n';
std::this_thread::sleep_for(std::chrono::seconds(1));
}

// Get the system Version struct
const Info::Version& system_version = info.get_version().second;

// Print out the vehicle version information.
std::cout << system_version << std::endl;

// Get the system Product struct
const Info::Product& system_product = info.get_product().second;

// Print out the vehicle product information.
std::cout << system_product << std::endl;

return 0;
}
2 changes: 1 addition & 1 deletion proto
Submodule proto updated 1 files
+11 −0 protos/info/info.proto
23 changes: 23 additions & 0 deletions src/mavsdk/plugins/info/include/plugins/info/info.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,27 @@ class Info : public PluginBase {
* @brief System version information.
*/
struct Version {
/**
* @brief These values define the type of firmware/flight software release
*/
enum class FlightSoftwareVersionType {
Unknown, /**< @brief Unknown type. */
Dev, /**< @brief Development release. */
Alpha, /**< @brief Alpha release. */
Beta, /**< @brief Beta release. */
Rc, /**< @brief Release candidate. */
Release, /**< @brief Official stable release. */
};

/**
* @brief Stream operator to print information about a `Info::FlightSoftwareVersionType`.
*
* @return A reference to the stream.
*/
friend std::ostream& operator<<(
std::ostream& str,
Info::Version::FlightSoftwareVersionType const& flight_software_version_type);

int32_t flight_sw_major{}; /**< @brief Flight software major version */
int32_t flight_sw_minor{}; /**< @brief Flight software minor version */
int32_t flight_sw_patch{}; /**< @brief Flight software patch version */
Expand All @@ -145,6 +166,8 @@ class Info : public PluginBase {
int32_t os_sw_patch{}; /**< @brief Operating system software patch version */
std::string flight_sw_git_hash{}; /**< @brief Flight software git hash */
std::string os_sw_git_hash{}; /**< @brief Operating system software git hash */
FlightSoftwareVersionType
flight_sw_version_type{}; /**< @brief Flight software version type */
};

/**
Expand Down
24 changes: 23 additions & 1 deletion src/mavsdk/plugins/info/info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,26 @@ std::ostream& operator<<(std::ostream& str, Info::Product const& product)
return str;
}

std::ostream& operator<<(
std::ostream& str, Info::Version::FlightSoftwareVersionType const& flight_software_version_type)
{
switch (flight_software_version_type) {
case Info::Version::FlightSoftwareVersionType::Unknown:
return str << "Unknown";
case Info::Version::FlightSoftwareVersionType::Dev:
return str << "Dev";
case Info::Version::FlightSoftwareVersionType::Alpha:
return str << "Alpha";
case Info::Version::FlightSoftwareVersionType::Beta:
return str << "Beta";
case Info::Version::FlightSoftwareVersionType::Rc:
return str << "Rc";
case Info::Version::FlightSoftwareVersionType::Release:
return str << "Release";
default:
return str << "Unknown";
}
}
bool operator==(const Info::Version& lhs, const Info::Version& rhs)
{
return (rhs.flight_sw_major == lhs.flight_sw_major) &&
Expand All @@ -105,7 +125,8 @@ bool operator==(const Info::Version& lhs, const Info::Version& rhs)
(rhs.os_sw_major == lhs.os_sw_major) && (rhs.os_sw_minor == lhs.os_sw_minor) &&
(rhs.os_sw_patch == lhs.os_sw_patch) &&
(rhs.flight_sw_git_hash == lhs.flight_sw_git_hash) &&
(rhs.os_sw_git_hash == lhs.os_sw_git_hash);
(rhs.os_sw_git_hash == lhs.os_sw_git_hash) &&
(rhs.flight_sw_version_type == lhs.flight_sw_version_type);
}

std::ostream& operator<<(std::ostream& str, Info::Version const& version)
Expand All @@ -123,6 +144,7 @@ std::ostream& operator<<(std::ostream& str, Info::Version const& version)
str << " os_sw_patch: " << version.os_sw_patch << '\n';
str << " flight_sw_git_hash: " << version.flight_sw_git_hash << '\n';
str << " os_sw_git_hash: " << version.os_sw_git_hash << '\n';
str << " flight_sw_version_type: " << version.flight_sw_version_type << '\n';
str << '}';
return str;
}
Expand Down
27 changes: 27 additions & 0 deletions src/mavsdk/plugins/info/info_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,9 @@ void InfoImpl::process_autopilot_version(const mavlink_message_t& message)
_version.flight_sw_major = (autopilot_version.flight_sw_version >> (8 * 3)) & 0xFF;
_version.flight_sw_minor = (autopilot_version.flight_sw_version >> (8 * 2)) & 0xFF;
_version.flight_sw_patch = (autopilot_version.flight_sw_version >> (8 * 1)) & 0xFF;
_version.flight_sw_version_type =
get_flight_software_version_type(static_cast<FIRMWARE_VERSION_TYPE>(
(autopilot_version.flight_sw_version >> (8 * 0)) & 0xFF));

// first three bytes of flight_custom_version (little endian) describe vendor version
_version.flight_sw_git_hash = swap_and_translate_binary_to_str(
Expand Down Expand Up @@ -151,6 +154,30 @@ void InfoImpl::process_autopilot_version(const mavlink_message_t& message)
_information_received = true;
}

Info::Version::FlightSoftwareVersionType
InfoImpl::get_flight_software_version_type(FIRMWARE_VERSION_TYPE firmwareVersionType)
{
switch (firmwareVersionType) {
case FIRMWARE_VERSION_TYPE_DEV:
return Info::Version::FlightSoftwareVersionType::Dev;

case FIRMWARE_VERSION_TYPE_ALPHA:
return Info::Version::FlightSoftwareVersionType::Alpha;

case FIRMWARE_VERSION_TYPE_BETA:
return Info::Version::FlightSoftwareVersionType::Beta;

case FIRMWARE_VERSION_TYPE_RC:
return Info::Version::FlightSoftwareVersionType::Rc;

case FIRMWARE_VERSION_TYPE_OFFICIAL:
return Info::Version::FlightSoftwareVersionType::Release;

default:
return Info::Version::FlightSoftwareVersionType::Unknown;
}
}

void InfoImpl::process_flight_information(const mavlink_message_t& message)
{
std::lock_guard<std::mutex> lock(_mutex);
Expand Down
3 changes: 3 additions & 0 deletions src/mavsdk/plugins/info/info_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class InfoImpl : public PluginImplBase {
void process_flight_information(const mavlink_message_t& message);
void process_attitude(const mavlink_message_t& message);

Info::Version::FlightSoftwareVersionType
get_flight_software_version_type(FIRMWARE_VERSION_TYPE);

mutable std::mutex _mutex{};

Info::Version _version{};
Expand Down
Loading

0 comments on commit 9177ef0

Please sign in to comment.