Skip to content

Commit

Permalink
Merge pull request #2272 from bkueng/component_metadata
Browse files Browse the repository at this point in the history
Implement component metadata protocol
  • Loading branch information
julianoes committed Apr 26, 2024
2 parents 0a3b714 + a13aa62 commit d68204a
Show file tree
Hide file tree
Showing 62 changed files with 11,115 additions and 11,461 deletions.
1 change: 1 addition & 0 deletions src/CMakeLists.txt
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
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
@@ -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)
{
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
@@ -0,0 +1,20 @@
#pragma once

#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

0 comments on commit d68204a

Please sign in to comment.