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_->addSecondary(secondary);
105 storage_->saveSecondaryData(ecu,
data);
109 std::vector<SecondaryInfo> info;
110 storage_->loadSecondariesInfo(&info);
117 return api_queue_.enqueue(task);
121 std::function<void()> task([
this, campaign_id, cmd] {
123 case campaign::Cmd::Accept:
124 uptane_client_->campaignAccept(campaign_id);
126 case campaign::Cmd::Decline:
127 uptane_client_->campaignDecline(campaign_id);
129 case campaign::Cmd::Postpone:
130 uptane_client_->campaignPostpone(campaign_id);
136 return api_queue_.enqueue(task);
140 std::function<void()> task([
this] { uptane_client_->sendDeviceData(); });
141 return api_queue_.enqueue(task);
146 return api_queue_.enqueue(task);
151 [
this, updates](
const api::FlowControlToken *token) {
return uptane_client_->downloadImages(updates, token); });
152 return api_queue_.enqueue(task);
156 std::function<
result::Install()> task([
this, updates] {
return uptane_client_->uptaneInstall(updates); });
157 return api_queue_.enqueue(task);
161 std::function<bool()> task([
this, custom]() {
return uptane_client_->putManifest(custom); });
162 return api_queue_.enqueue(task);
166 if (api_queue_.pause(
true)) {
167 uptane_client_->reportPause();
168 return result::PauseStatus::kSuccess;
170 return result::PauseStatus::kAlreadyPaused;
175 if (api_queue_.pause(
false)) {
176 uptane_client_->reportResume();
177 return result::PauseStatus::kSuccess;
179 return result::PauseStatus::kAlreadyRunning;
186 const std::function<
void(shared_ptr<event::BaseEvent>)> &handler) {
187 return sig_->connect(handler);
190 Aktualizr::InstallationLog Aktualizr::GetInstallationLog() {
191 std::vector<Aktualizr::InstallationLogEntry> ilog;
194 if (!storage_->loadEcuSerials(&serials)) {
195 throw std::runtime_error(
"Could not load ecu serials");
198 ilog.reserve(serials.size());
199 for (
const auto &s : serials) {
201 std::vector<Uptane::Target> installs;
203 std::vector<Uptane::Target> log;
204 storage_->loadInstallationLog(serial.ToString(), &log,
true);
217 auto handle = storage_->openTargetFile(target);
218 if (handle->isPartial()) {
219 throw std::runtime_error(
"Target was partially downloaded");