Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement component metadata protocol #2272

Merged
merged 15 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ hunter_add_package(jsoncpp)
hunter_add_package(CURL)

find_package(CURL REQUIRED)
find_package(LibLZMA REQUIRED)

if(BUILD_TESTS AND (IOS OR ANDROID))
message(STATUS "Building for iOS or Android: forcing BUILD_TESTS to FALSE...")
Expand Down
7 changes: 7 additions & 0 deletions src/mavsdk/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ target_sources(mavsdk
system.cpp
system_impl.cpp
flight_mode.cpp
fs_utils.cpp
inflate_lzma.cpp
math_conversions.cpp
mavsdk.cpp
mavsdk_impl.cpp
Expand Down Expand Up @@ -63,6 +65,11 @@ cmake_policy(SET CMP0079 NEW)
target_link_libraries(mavsdk
PRIVATE
Threads::Threads
${LIBLZMA_LIBRARIES}
)
target_include_directories(mavsdk
PRIVATE
${LIBLZMA_INCLUDE_DIRS}
)

if (NOT BUILD_WITHOUT_CURL)
Expand Down
115 changes: 115 additions & 0 deletions src/mavsdk/core/fs_utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@

#include "fs_utils.h"
#include "log.h"
#include <fstream>
#include <random>

#if defined(LINUX) || defined(APPLE)
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>
#endif

#ifdef WINDOWS
#include <windows.h>
#include <shlobj.h>
#endif

namespace mavsdk {

#ifdef WINDOWS
static std::optional<std::filesystem::path> get_known_windows_path(REFKNOWNFOLDERID folderId)
julianoes marked this conversation as resolved.
Show resolved Hide resolved
{
std::filesystem::path path;
PWSTR path_tmp;

/* Attempt to get user's AppData folder
*
* Microsoft Docs:
* https://learn.microsoft.com/en-us/windows/win32/api/shlobj_core/nf-shlobj_core-shgetknownfolderpath
* https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid
*/
auto get_folder_path_ret = SHGetKnownFolderPath(folderId, 0, nullptr, &path_tmp);

/* Error check */
if (get_folder_path_ret != S_OK) {
CoTaskMemFree(path_tmp);
return std::nullopt;
}

/* Convert the Windows path type to a C++ path */
path = path_tmp;

CoTaskMemFree(path_tmp);
return path;
}
#endif

std::optional<std::filesystem::path> get_cache_directory()
{
// Windows: %LOCALAPPDATA%
// Android: /data/data/<package>/cache
// Linux: ~/.cache
// macOS: ~/Library/Caches
#if defined(WINDOWS)
auto path = get_known_windows_path(FOLDERID_LocalAppData);
if (path) {
return path->append("mavsdk_cache");
}
#elif defined(ANDROID)
// Read /proc/self/cmdline
std::ifstream cmdline("/proc/self/cmdline");
std::string line;
if (std::getline(cmdline, line)) {
// line might have a trailing \0
if (line.length() > 0 && *line.end() == 0) {
line.pop_back();
}
return "/data/data/" + line + "/mavsdk_cache";
}
#elif defined(APPLE) || defined(LINUX)
const char* homedir;
if ((homedir = getenv("HOME")) == NULL) {
homedir = getpwuid(getuid())->pw_dir;
}
if (!homedir) {
return std::nullopt;
}
#if defined(APPLE)
return std::filesystem::path(homedir) / "Library" / "Caches" / "mavsdk";
#else // Linux
return std::filesystem::path(homedir) / ".cache" / "mavsdk";
#endif
#else
#error "Unsupported platform"
#endif

return std::nullopt;
}

std::optional<std::filesystem::path> create_tmp_directory(const std::string& prefix)
{
// Inspired by https://stackoverflow.com/a/58454949/8548472
const auto tmp_dir = std::filesystem::temp_directory_path();

std::random_device dev;
std::mt19937 prng(dev());
std::uniform_int_distribution<uint32_t> rand(0);

static constexpr unsigned max_tries = 100;

for (unsigned i = 0; i < max_tries; ++i) {
std::stringstream ss;
ss << prefix << '-' << std::hex << rand(prng);
auto path = tmp_dir / ss.str();

const auto created = std::filesystem::create_directory(path);
if (created) {
return path.string();
}
}

LogErr() << "Could not create a temporary directory, aborting.";
return std::nullopt;
}
} // namespace mavsdk
20 changes: 20 additions & 0 deletions src/mavsdk/core/fs_utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably merge this file with https://github.com/mavlink/MAVSDK/blob/main/src/system_tests/fs_helpers.h but it can happen after this PR.


#include <optional>
#include <filesystem>

namespace mavsdk {

/**
* Get the path to the system's cache directory with a MAVSDK-specific subdirectory.
* The path does not necessarily exist yet.
*/
std::optional<std::filesystem::path> get_cache_directory();

/**
* Create a random subdirectory in the system's tmp directory
* @param prefix directory prefix
*/
std::optional<std::filesystem::path> create_tmp_directory(const std::string& prefix);

} // namespace mavsdk
Loading
Loading