Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

ref: little step forward to separate lib aktualizr headers #1688

Closed
wants to merge 12 commits into from
Closed
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,6 @@ function(aktualizr_source_file_checks)
endif()
endfunction()


# Use C++11, but without GNU or other extensions
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_EXTENSIONS OFF)
Expand All @@ -319,6 +318,7 @@ set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)

############### BUILD RULES
include_directories(${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/src/libaktualizr)
include_directories(${PROJECT_SOURCE_DIR}/third_party/googletest/googletest/include)
include_directories(${JSONCPP_INCLUDE_DIRS})
Expand Down
1 change: 1 addition & 0 deletions docs/doxygen/Doxyfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,7 @@ WARN_LOGFILE =
INPUT = @CMAKE_SOURCE_DIR@/docs \
@CMAKE_SOURCE_DIR@/docs/doxygen \
@CMAKE_SOURCE_DIR@/include \
@CMAKE_SOURCE_DIR@/include/libaktualizr \
@CMAKE_SOURCE_DIR@/src/aktualizr_get \
@CMAKE_SOURCE_DIR@/src/aktualizr_info \
@CMAKE_SOURCE_DIR@/src/aktualizr_lite \
Expand Down
2 changes: 1 addition & 1 deletion include/libaktualizr-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <stdint.h> // for uint8_t

#ifdef __cplusplus
#include "primary/aktualizr.h"
#include <libaktualizr/aktualizr.h>

using Campaign = campaign::Campaign;
using Updates = std::vector<Uptane::Target>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@

#include <boost/signals2.hpp>

#include "config/config.h"
#include "primary/events.h"
#include "primary/secondaryinterface.h"
#include "sotauptaneclient.h"
#include "storage/invstorage.h"
#include "utilities/apiqueue.h"
#include "libaktualizr/config.h"
#include "libaktualizr/events.h"

class SecondaryInterface;
class SotaUptaneClient;
class INvStorage;
class SecondaryInfo;

namespace api {
class CommandQueue;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

I forget, does class api::CommandQueue work? I thought it did and it's a little cleaner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes we use it, the disadvantage he is that we have to use smart ptr, the reason is that we cant simple uses forward declaration with member objects defined by the value

std::shared_ptr<api::CommandQueue> api_queue_;

Copy link
Collaborator

Choose a reason for hiding this comment

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

Yeah, that's fine, I just meant if we could put this forward declaration on one line by using the scope resolution operator (::) instead of this three-line variant.

Copy link
Contributor

Choose a reason for hiding this comment

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

Why not a unique_ptr, though?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why not a unique_ptr, though?

It will cause a compilation error:

Invalid application of ‘sizeof’ to incomplete type ‘api::CommandQueue’
  static_assert(sizeof(_Tp)>0,

It can be fixed by adding an empty dtor for Aktualizr.

Here is an answer with a good explanation
https://stackoverflow.com/a/9954553


/**
* This class provides the main APIs necessary for launching and controlling
Expand All @@ -24,6 +29,7 @@ class Aktualizr {
explicit Aktualizr(const Config& config);
Aktualizr(const Aktualizr&) = delete;
Aktualizr& operator=(const Aktualizr&) = delete;
~Aktualizr();

/**
* Initialize aktualizr. Any Secondaries should be added before making this
Expand Down Expand Up @@ -242,7 +248,7 @@ class Aktualizr {

std::shared_ptr<INvStorage> storage_;
std::shared_ptr<event::Channel> sig_;
api::CommandQueue api_queue_;
std::unique_ptr<api::CommandQueue> api_queue_;
};

#endif // AKTUALIZR_H_
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@

#include <string>
#include <vector>
#include "http/httpclient.h"
#include "utilities/utils.h"

#include "json/json.h"

class HttpInterface;

namespace campaign {

Expand Down
244 changes: 244 additions & 0 deletions include/libaktualizr/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,244 @@
#ifndef CONFIG_H_
#define CONFIG_H_

#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#include <boost/filesystem.hpp>
#include <boost/program_options.hpp>
#include <boost/property_tree/ini_parser.hpp>

#include "libaktualizr/types.h"

struct LoggerConfig {
int loglevel{2};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

// Try to keep the order of config options the same as in Config::writeToStream()
// and Config::updateFromPropertyTree() in config.cc.
struct TlsConfig {
std::string server;
boost::filesystem::path server_url_path;
CryptoSource ca_source{CryptoSource::kFile};
CryptoSource pkey_source{CryptoSource::kFile};
CryptoSource cert_source{CryptoSource::kFile};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct ProvisionConfig {
std::string server;
std::string p12_password;
std::string expiry_days{"36000"};
boost::filesystem::path provision_path;
ProvisionMode mode{ProvisionMode::kSharedCred};
std::string device_id;
std::string primary_ecu_serial;
std::string primary_ecu_hardware_id;
std::string ecu_registration_endpoint;

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct UptaneConfig {
uint64_t polling_sec{10U};
std::string director_server;
std::string repo_server;
CryptoSource key_source{CryptoSource::kFile};
KeyType key_type{KeyType::kRSA2048};
bool force_install_completion{false};
boost::filesystem::path secondary_config_file;
uint64_t secondary_preinstall_wait_sec{600U};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

// declare p11 types as incomplete so that the header can be used without libp11
struct PKCS11_ctx_st;
struct PKCS11_slot_st;

struct P11Config {
boost::filesystem::path module;
std::string pass;
std::string uptane_key_id;
std::string tls_cacert_id;
std::string tls_pkey_id;
std::string tls_clientcert_id;

void updateFromPropertyTree(const boost::property_tree::ptree &pt);
void writeToStream(std::ostream &out_stream) const;

};

// bundle some parts of the main config together
// Should be derived by calling Config::keymanagerConfig()
struct KeyManagerConfig {
KeyManagerConfig() = delete; // only allow construction by initializer list
P11Config p11;
CryptoSource tls_ca_source;
CryptoSource tls_pkey_source;
CryptoSource tls_cert_source;
KeyType uptane_key_type;
CryptoSource uptane_key_source;
};

enum class RollbackMode { kBootloaderNone = 0, kUbootGeneric, kUbootMasked };
std::ostream& operator<<(std::ostream& os, RollbackMode mode);

struct BootloaderConfig {
RollbackMode rollback_mode{RollbackMode::kBootloaderNone};
boost::filesystem::path reboot_sentinel_dir{"/var/run/aktualizr-session"};
boost::filesystem::path reboot_sentinel_name{"need_reboot"};
std::string reboot_command{"/sbin/reboot"};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

// TODO: move these to their corresponding headers
#define PACKAGE_MANAGER_NONE "none"
#define PACKAGE_MANAGER_OSTREE "ostree"
#define PACKAGE_MANAGER_DEBIAN "debian"
#define PACKAGE_MANAGER_ANDROID "android"
#define PACKAGE_MANAGER_OSTREEDOCKERAPP "ostree+docker-app"

#ifdef BUILD_OSTREE
#define PACKAGE_MANAGER_DEFAULT PACKAGE_MANAGER_OSTREE
#else
#define PACKAGE_MANAGER_DEFAULT PACKAGE_MANAGER_NONE
#endif

struct PackageConfig {
std::string type{PACKAGE_MANAGER_DEFAULT};

// OSTree options
std::string os;
boost::filesystem::path sysroot;
std::string ostree_server;
boost::filesystem::path images_path{"/var/sota/images"};
boost::filesystem::path packages_file{"/usr/package.manifest"};

// Options for simulation (to be used with "none")
bool fake_need_reboot{false};

// for specialized configuration
std::map<std::string, std::string> extra;

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct TelemetryConfig {
/**
* Report device network information: IP address, hostname, MAC address
*/
bool report_network{true};
bool report_config{true};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

std::ostream& operator<<(std::ostream& os, StorageType stype);

struct StorageConfig {
StorageType type{StorageType::kSqlite};
boost::filesystem::path path{"/var/sota"};

// FS storage
BasedPath uptane_metadata_path{"metadata"};
BasedPath uptane_private_key_path{"ecukey.der"};
BasedPath uptane_public_key_path{"ecukey.pub"};
BasedPath tls_cacert_path{"root.crt"};
BasedPath tls_pkey_path{"pkey.pem"};
BasedPath tls_clientcert_path{"client.pem"};

// SQLite storage
BasedPath sqldb_path{"sql.db"}; // based on `/var/sota`

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

struct ImportConfig {
boost::filesystem::path base_path{"/var/sota/import"};
BasedPath uptane_private_key_path{""};
BasedPath uptane_public_key_path{""};
BasedPath tls_cacert_path{""};
BasedPath tls_pkey_path{""};
BasedPath tls_clientcert_path{""};

void updateFromPropertyTree(const boost::property_tree::ptree& pt);
void writeToStream(std::ostream& out_stream) const;
};

class BaseConfig {
public:
virtual ~BaseConfig() = default;
void updateFromToml(const boost::filesystem::path& filename);
virtual void updateFromPropertyTree(const boost::property_tree::ptree& pt) = 0;

protected:
void updateFromDirs(const std::vector<boost::filesystem::path>& configs);

static void checkDirs(const std::vector<boost::filesystem::path>& configs) {
for (const auto& config : configs) {
if (!boost::filesystem::exists(config)) {
throw std::runtime_error("Config directory " + config.string() + " does not exist.");
}
}
}

std::vector<boost::filesystem::path> config_dirs_ = {"/usr/lib/sota/conf.d", "/etc/sota/conf.d/"};
};

/**
* Configuration object for an aktualizr instance running on a Primary ECU.
*
* This class is a parent to a series of smaller configuration objects for
* specific subsystems. Note that most other aktualizr-related tools have their
* own parent configuration objects with a reduced set of members.
*/
class Config : public BaseConfig {
public:
Config();
explicit Config(const boost::program_options::variables_map& cmd);
explicit Config(const boost::filesystem::path& filename);
explicit Config(const std::vector<boost::filesystem::path>& config_dirs);

KeyManagerConfig keymanagerConfig() const;

void updateFromTomlString(const std::string& contents);
void postUpdateValues();
void writeToStream(std::ostream& sink) const;

// Config data structures. Keep logger first so that it is taken into account
// while processing the others.
LoggerConfig logger;
P11Config p11;
TlsConfig tls;
ProvisionConfig provision;
UptaneConfig uptane;
PackageConfig pacman;
StorageConfig storage;
ImportConfig import;
TelemetryConfig telemetry;
BootloaderConfig bootloader;

private:
void updateFromPropertyTree(const boost::property_tree::ptree& pt) override;
void updateFromCommandLine(const boost::program_options::variables_map& cmd);
bool loglevel_from_cmdline{false};
};

std::ostream& operator<<(std::ostream& os, const Config& cfg);

#endif // CONFIG_H_
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@

#include <boost/signals2.hpp>

#include "primary/results.h"
#include "uptane/fetcher.h"
#include "uptane/tuf.h"
#include "utilities/types.h"
#include "libaktualizr/results.h"

/**
* Aktualizr status events.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <string>
#include <vector>

#include "campaign/campaign.h"
#include "uptane/fetcher.h"
#include "uptane/tuf.h"
#include "libaktualizr/types.h"
#include "libaktualizr/campaign.h"

/** Results of libaktualizr API calls. */
namespace result {
Expand Down
Loading