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();
38 if (update_result.updates.empty()) {
39 if (update_result.status == result::UpdateStatus::kError) {
47 if (download_result.status != result::DownloadStatus::kSuccess || download_result.updates.empty()) {
48 if (download_result.status != result::DownloadStatus::kNothingToDownload) {
55 Install(download_result.updates).get();
57 if (uptane_client_->isInstallCompletionRequired()) {
60 LOG_INFO <<
"Exiting aktualizr so that pending updates can be applied after reboot";
64 if (!uptane_client_->hasPendingUpdates()) {
74 std::future<void> future = std::async(std::launch::async, [
this, custom_hwinfo]() {
77 std::unique_lock<std::mutex> l(exit_cond_.m);
83 if (exit_cond_.cv.wait_for(l, std::chrono::seconds(config_.uptane.polling_sec),
84 [
this] { return exit_cond_.flag; })) {
88 uptane_client_->completeInstall();
95 std::lock_guard<std::mutex> g(exit_cond_.m);
96 exit_cond_.flag =
true;
98 exit_cond_.cv.notify_all();
102 uptane_client_->addSecondary(secondary);
106 storage_->saveSecondaryData(ecu, data);
110 std::vector<SecondaryInfo> info;
111 storage_->loadSecondariesInfo(&info);
117 std::function<result::CampaignCheck()> task([
this] {
return uptane_client_->campaignCheck(); });
118 return api_queue_.enqueue(task);
122 std::function<void()> task([
this, campaign_id, cmd] {
124 case campaign::Cmd::Accept:
125 uptane_client_->campaignAccept(campaign_id);
127 case campaign::Cmd::Decline:
128 uptane_client_->campaignDecline(campaign_id);
130 case campaign::Cmd::Postpone:
131 uptane_client_->campaignPostpone(campaign_id);
137 return api_queue_.enqueue(task);
141 std::function<void()> task([
this, custom_hwinfo] { uptane_client_->sendDeviceData(custom_hwinfo); });
142 return api_queue_.enqueue(task);
146 std::function<result::UpdateCheck()> task([
this] {
return uptane_client_->fetchMeta(); });
147 return api_queue_.enqueue(task);
151 std::function<result::Download(const api::FlowControlToken *)> task(
152 [
this, updates](
const api::FlowControlToken *token) {
return uptane_client_->downloadImages(updates, token); });
153 return api_queue_.enqueue(task);
157 std::function<result::Install()> task([
this, updates] {
return uptane_client_->uptaneInstall(updates); });
158 return api_queue_.enqueue(task);
162 std::function<bool()> task([
this, custom]() {
return uptane_client_->putManifest(custom); });
163 return api_queue_.enqueue(task);
167 if (api_queue_.pause(
true)) {
168 uptane_client_->reportPause();
169 return result::PauseStatus::kSuccess;
171 return result::PauseStatus::kAlreadyPaused;
176 if (api_queue_.pause(
false)) {
177 uptane_client_->reportResume();
178 return result::PauseStatus::kSuccess;
180 return result::PauseStatus::kAlreadyRunning;
187 const std::function<
void(shared_ptr<event::BaseEvent>)> &handler) {
188 return sig_->connect(handler);
191 Aktualizr::InstallationLog Aktualizr::GetInstallationLog() {
192 std::vector<Aktualizr::InstallationLogEntry> ilog;
195 if (!storage_->loadEcuSerials(&serials)) {
196 throw std::runtime_error(
"Could not load ECU serials");
199 ilog.reserve(serials.size());
200 for (
const auto &s : serials) {
202 std::vector<Uptane::Target> installs;
204 std::vector<Uptane::Target> log;
205 storage_->loadInstallationLog(serial.ToString(), &log,
true);
218 auto handle = storage_->openTargetFile(target);
219 if (handle->isPartial()) {
220 throw std::runtime_error(
"Target was partially downloaded");
Provides a thread-safe way to pause and terminate task execution.
Container for information about downloading an update.
void AddSecondary(const std::shared_ptr< Uptane::SecondaryInterface > &secondary)
Add new Secondary to aktualizr.
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.
bool IsRegistered() const
Returns true if the device has been registered to the backend succesffully.
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...
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.
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::unique_ptr< StorageTargetRHandle > OpenStoredTarget(const Uptane::Target &target)
Get target downloaded in Download call.
std::future< void > CampaignControl(const std::string &campaign_id, campaign::Cmd cmd)
Act on campaign: accept, decline or postpone.
Get log of installations.