8 #include "utilities/timer.h" 10 using std::make_shared;
11 using std::shared_ptr;
17 const std::shared_ptr<HttpInterface> &http_in)
18 : config_{std::move(config)}, sig_{
new event::Channel()} {
19 if (sodium_init() == -1) {
20 throw std::runtime_error(
"Unable to initialize libsodium");
23 storage_ = std::move(storage_in);
24 storage_->importData(config_.import);
26 uptane_client_ = std::make_shared<SotaUptaneClient>(config_, storage_, http_in, sig_);
30 uptane_client_->initialize();
36 if (update_result.updates.empty()) {
37 if (update_result.status == result::UpdateStatus::kError) {
45 if (download_result.status != result::DownloadStatus::kSuccess || download_result.updates.empty()) {
46 if (download_result.status != result::DownloadStatus::kNothingToDownload) {
53 Install(download_result.updates).get();
55 if (uptane_client_->isInstallCompletionRequired()) {
58 LOG_INFO <<
"Exiting aktualizr so that pending updates can be applied after reboot";
62 if (!uptane_client_->hasPendingUpdates()) {
72 std::future<void> future = std::async(std::launch::async, [
this, custom_hwinfo]() {
75 std::unique_lock<std::mutex> l(exit_cond_.m);
81 if (exit_cond_.cv.wait_for(l, std::chrono::seconds(config_.uptane.polling_sec),
82 [
this] { return exit_cond_.flag; })) {
86 uptane_client_->completeInstall();
93 std::lock_guard<std::mutex> g(exit_cond_.m);
94 exit_cond_.flag =
true;
96 exit_cond_.cv.notify_all();
100 uptane_client_->addSecondary(secondary);
104 storage_->saveSecondaryData(ecu, data);
108 std::vector<SecondaryInfo> info;
109 storage_->loadSecondariesInfo(&info);
115 std::function<result::CampaignCheck()> task([
this] {
return uptane_client_->campaignCheck(); });
116 return api_queue_.enqueue(task);
120 std::function<void()> task([
this, campaign_id, cmd] {
122 case campaign::Cmd::Accept:
123 uptane_client_->campaignAccept(campaign_id);
125 case campaign::Cmd::Decline:
126 uptane_client_->campaignDecline(campaign_id);
128 case campaign::Cmd::Postpone:
129 uptane_client_->campaignPostpone(campaign_id);
135 return api_queue_.enqueue(task);
139 std::function<void()> task([
this, custom_hwinfo] { uptane_client_->sendDeviceData(custom_hwinfo); });
140 return api_queue_.enqueue(task);
144 std::function<result::UpdateCheck()> task([
this] {
return uptane_client_->fetchMeta(); });
145 return api_queue_.enqueue(task);
149 std::function<result::Download(const api::FlowControlToken *)> task(
150 [
this, updates](
const api::FlowControlToken *token) {
return uptane_client_->downloadImages(updates, token); });
151 return api_queue_.enqueue(task);
155 std::function<result::Install()> task([
this, updates] {
return uptane_client_->uptaneInstall(updates); });
156 return api_queue_.enqueue(task);
160 return storage_->storeDeviceInstallationRawReport(custom_raw_report);
164 std::function<bool()> task([
this, custom]() {
return uptane_client_->putManifest(custom); });
165 return api_queue_.enqueue(task);
169 if (api_queue_.pause(
true)) {
170 uptane_client_->reportPause();
171 return result::PauseStatus::kSuccess;
173 return result::PauseStatus::kAlreadyPaused;
178 if (api_queue_.pause(
false)) {
179 uptane_client_->reportResume();
180 return result::PauseStatus::kSuccess;
182 return result::PauseStatus::kAlreadyRunning;
189 const std::function<
void(shared_ptr<event::BaseEvent>)> &handler) {
190 return sig_->connect(handler);
193 Aktualizr::InstallationLog Aktualizr::GetInstallationLog() {
194 std::vector<Aktualizr::InstallationLogEntry> ilog;
197 if (!storage_->loadEcuSerials(&serials)) {
198 throw std::runtime_error(
"Could not load ECU serials");
201 ilog.reserve(serials.size());
202 for (
const auto &s : serials) {
204 std::vector<Uptane::Target> installs;
206 std::vector<Uptane::Target> log;
207 storage_->loadInstallationLog(serial.ToString(), &log,
true);
220 return uptane_client_->openStoredTarget(target);
Provides a thread-safe way to pause and terminate task execution.
void AddSecondary(const std::shared_ptr< SecondaryInterface > &secondary)
Add new Secondary to aktualizr.
Container for information about downloading an update.
void Abort()
Aborts the currently running command, if it can be aborted, or waits for it to finish; then removes a...
std::vector< Uptane::Target > GetStoredTargets()
Get list of targets currently in storage.
bool UptaneCycle()
Synchronously run an Uptane cycle: check for updates, download any new targets, install them...
result::Pause Resume()
Resume the library operations.
std::vector< SecondaryInfo > GetSecondaries() const
Returns a list of the registered Secondaries, along with some associated metadata.
Aktualizr(const Config &config)
Aktualizr requires a configuration object.
Configuration object for an aktualizr instance running on a Primary ECU.
std::future< void > RunForever(const Json::Value &custom_hwinfo=Json::nullValue)
Asynchronously run aktualizr indefinitely until Shutdown is called.
void DeleteStoredTarget(const Uptane::Target &target)
Delete a stored target from storage.
void Shutdown()
Shuts down currently running RunForever() method.
std::future< result::Download > Download(const std::vector< Uptane::Target > &updates)
Download targets.
std::future< result::UpdateCheck > CheckUpdates()
Fetch Uptane metadata and check for updates.
void SetSecondaryData(const Uptane::EcuSerial &ecu, const std::string &data)
Store some free-form data to be associated with a particular Secondary, to be retrieved later through...
bool SetInstallationRawReport(const std::string &custom_raw_report)
SetInstallationRawReport allows setting a custom raw report field in the device installation result...
result::Pause Pause()
Pause the library operations.
std::future< result::Install > Install(const std::vector< Uptane::Target > &updates)
Install targets.
std::future< result::CampaignCheck > CampaignCheck()
Check for campaigns.
std::ifstream OpenStoredTarget(const Uptane::Target &target)
Get target downloaded in Download call.
Container for information about available updates.
std::future< void > SendDeviceData(const Json::Value &custom_hwinfo=Json::nullValue)
Send local device data to the server.
boost::signals2::connection SetSignalHandler(const SigHandler &handler)
Provide a function to receive event notifications.
std::future< bool > SendManifest(const Json::Value &custom=Json::nullValue)
Send installation report to the backend.
This class provides the main APIs necessary for launching and controlling libaktualizr.
void Initialize()
Initialize aktualizr.
std::future< void > CampaignControl(const std::string &campaign_id, campaign::Cmd cmd)
Act on campaign: accept, decline or postpone.
Get log of installations.