8 #include "utilities/timer.h"
10 using std::make_shared;
11 using std::shared_ptr;
17 : config_{std::move(config)}, sig_{
new event::Channel()} {
18 if (sodium_init() == -1) {
19 throw std::runtime_error(
"Unable to initialize libsodium");
22 storage_ = std::move(storage_in);
23 storage_->importData(config_.import);
25 uptane_client_ = std::make_shared<SotaUptaneClient>(config_, storage_, http_in, sig_);
29 uptane_client_->initialize();
37 if (update_result.updates.empty()) {
38 if (update_result.status == result::UpdateStatus::kError) {
46 if (download_result.status != result::DownloadStatus::kSuccess || download_result.updates.empty()) {
47 if (download_result.status != result::DownloadStatus::kNothingToDownload) {
54 Install(download_result.updates).get();
56 if (uptane_client_->isInstallCompletionRequired()) {
59 LOG_INFO <<
"About to exit aktualizr so the pending updates can be applied after reboot";
63 if (!uptane_client_->hasPendingUpdates()) {
73 std::future<void> future = std::async(std::launch::async, [&]() {
76 std::unique_lock<std::mutex> l(exit_cond_.m);
82 if (exit_cond_.cv.wait_for(l, std::chrono::seconds(config_.uptane.polling_sec),
83 [
this] { return exit_cond_.flag; })) {
87 uptane_client_->completeInstall();
94 std::lock_guard<std::mutex> g(exit_cond_.m);
95 exit_cond_.flag =
true;
97 exit_cond_.cv.notify_all();
101 uptane_client_->addNewSecondary(secondary);
106 return api_queue_.enqueue(task);
110 std::function<void()> task([
this, campaign_id, cmd] {
112 case campaign::Cmd::Accept:
113 uptane_client_->campaignAccept(campaign_id);
115 case campaign::Cmd::Decline:
116 uptane_client_->campaignDecline(campaign_id);
118 case campaign::Cmd::Postpone:
119 uptane_client_->campaignPostpone(campaign_id);
125 return api_queue_.enqueue(task);
129 std::function<void()> task([
this] { uptane_client_->sendDeviceData(); });
130 return api_queue_.enqueue(task);
135 return api_queue_.enqueue(task);
140 [
this, updates](
const api::FlowControlToken *token) {
return uptane_client_->downloadImages(updates, token); });
141 return api_queue_.enqueue(task);
145 std::function<
result::Install()> task([
this, updates] {
return uptane_client_->uptaneInstall(updates); });
146 return api_queue_.enqueue(task);
150 std::function<bool()> task([
this, custom]() {
return uptane_client_->putManifest(custom); });
151 return api_queue_.enqueue(task);
155 if (api_queue_.pause(
true)) {
156 uptane_client_->reportPause();
157 return result::PauseStatus::kSuccess;
159 return result::PauseStatus::kAlreadyPaused;
164 if (api_queue_.pause(
false)) {
165 uptane_client_->reportResume();
166 return result::PauseStatus::kSuccess;
168 return result::PauseStatus::kAlreadyRunning;
175 const std::function<
void(shared_ptr<event::BaseEvent>)> &handler) {
176 return sig_->connect(handler);
179 Aktualizr::InstallationLog Aktualizr::GetInstallationLog() {
180 std::vector<Aktualizr::InstallationLogEntry> ilog;
183 if (!storage_->loadEcuSerials(&serials)) {
184 throw std::runtime_error(
"Could not load ecu serials");
187 ilog.reserve(serials.size());
188 for (
const auto &s : serials) {
190 std::vector<Uptane::Target> installs;
192 std::vector<Uptane::Target> log;
193 storage_->loadInstallationLog(serial.ToString(), &log,
true);
206 auto handle = storage_->openTargetFile(target);
207 if (handle->isPartial()) {
208 throw std::runtime_error(
"Target was partially downloaded");