Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
sotauptaneclient.h
1 #ifndef SOTA_UPTANE_CLIENT_H_
2 #define SOTA_UPTANE_CLIENT_H_
3 
4 #include <map>
5 #include <memory>
6 #include <string>
7 #include <utility>
8 #include <vector>
9 
10 #include <boost/signals2.hpp>
11 #include "gtest/gtest_prod.h"
12 #include "json/json.h"
13 
14 #include "bootloader/bootloader.h"
15 #include "campaign/campaign.h"
16 #include "config/config.h"
17 #include "http/httpclient.h"
18 #include "package_manager/packagemanagerfactory.h"
19 #include "package_manager/packagemanagerinterface.h"
20 #include "primary/events.h"
21 #include "primary/results.h"
22 #include "reportqueue.h"
23 #include "storage/invstorage.h"
24 #include "uptane/directorrepository.h"
25 #include "uptane/exceptions.h"
26 #include "uptane/fetcher.h"
27 #include "uptane/imagerepository.h"
28 #include "uptane/iterator.h"
29 #include "uptane/secondaryinterface.h"
30 
32  public:
33  SotaUptaneClient(Config &config_in, std::shared_ptr<INvStorage> storage_in, std::shared_ptr<HttpInterface> http_in,
34  std::shared_ptr<event::Channel> events_channel_in,
35  const Uptane::EcuSerial &primary_serial = Uptane::EcuSerial::Unknown(),
36  const Uptane::HardwareIdentifier &hwid = Uptane::HardwareIdentifier::Unknown())
37  : config(config_in),
38  storage(std::move(storage_in)),
39  http(std::move(http_in)),
40  package_manager_(PackageManagerFactory::makePackageManager(config.pacman, config.bootloader, storage, http)),
41  uptane_fetcher(new Uptane::Fetcher(config, http)),
42  events_channel(std::move(events_channel_in)),
43  primary_ecu_serial_(primary_serial),
44  primary_ecu_hw_id_(hwid) {
45  report_queue = std_::make_unique<ReportQueue>(config, http, storage);
46  }
47 
48  SotaUptaneClient(Config &config_in, const std::shared_ptr<INvStorage> &storage_in,
49  std::shared_ptr<HttpInterface> http_in)
50  : SotaUptaneClient(config_in, storage_in, std::move(http_in), nullptr) {}
51 
52  SotaUptaneClient(Config &config_in, const std::shared_ptr<INvStorage> &storage_in)
53  : SotaUptaneClient(config_in, storage_in, std::make_shared<HttpClient>()) {}
54 
55  void initialize();
56  void addSecondary(const std::shared_ptr<Uptane::SecondaryInterface> &sec);
57  result::Download downloadImages(const std::vector<Uptane::Target> &targets,
58  const api::FlowControlToken *token = nullptr);
59  std::pair<bool, Uptane::Target> downloadImage(const Uptane::Target &target,
60  const api::FlowControlToken *token = nullptr);
61  void reportPause();
62  void reportResume();
63  void sendDeviceData(const Json::Value &custom_hwinfo = Json::nullValue);
64  result::UpdateCheck fetchMeta();
65  bool putManifest(const Json::Value &custom = Json::nullValue);
66  result::UpdateCheck checkUpdates();
67  result::Install uptaneInstall(const std::vector<Uptane::Target> &updates);
68  result::CampaignCheck campaignCheck();
69  void campaignAccept(const std::string &campaign_id);
70  void campaignDecline(const std::string &campaign_id);
71  void campaignPostpone(const std::string &campaign_id);
72 
73  bool hasPendingUpdates() const;
74  bool isInstallCompletionRequired() const;
75  void completeInstall() const;
76  Uptane::LazyTargetsList allTargets() const;
77  Uptane::Target getCurrent() const { return package_manager_->getCurrent(); }
78 
79  void updateImageMeta(); // TODO: make private once aktualizr has a proper TUF API
80  void checkImageMetaOffline();
81  data::InstallationResult PackageInstall(const Uptane::Target &target);
82  TargetStatus VerifyTarget(const Uptane::Target &target) const { return package_manager_->verifyTarget(target); }
83 
84  private:
85  FRIEND_TEST(Aktualizr, FullNoUpdates);
86  FRIEND_TEST(Aktualizr, DeviceInstallationResult);
87  FRIEND_TEST(Aktualizr, FullMultipleSecondaries);
88  FRIEND_TEST(Aktualizr, CheckNoUpdates);
89  FRIEND_TEST(Aktualizr, DownloadWithUpdates);
90  FRIEND_TEST(Aktualizr, FinalizationFailure);
91  FRIEND_TEST(Aktualizr, InstallationFailure);
92  FRIEND_TEST(Aktualizr, AutoRebootAfterUpdate);
93  FRIEND_TEST(Aktualizr, EmptyTargets);
94  FRIEND_TEST(Aktualizr, FullOstreeUpdate);
95  FRIEND_TEST(Aktualizr, DownloadNonOstreeBin);
97  FRIEND_TEST(DockerAppManager, DockerAppBundles);
98  FRIEND_TEST(Uptane, AssembleManifestGood);
99  FRIEND_TEST(Uptane, AssembleManifestBad);
100  FRIEND_TEST(Uptane, InstallFakeGood);
101  FRIEND_TEST(Uptane, restoreVerify);
102  FRIEND_TEST(Uptane, PutManifest);
103  FRIEND_TEST(Uptane, offlineIteration);
104  FRIEND_TEST(Uptane, IgnoreUnknownUpdate);
105  FRIEND_TEST(Uptane, kRejectAllTest);
106  FRIEND_TEST(UptaneCI, ProvisionAndPutManifest);
107  FRIEND_TEST(UptaneCI, CheckKeys);
108  FRIEND_TEST(UptaneKey, Check); // Note hacky name
109  FRIEND_TEST(UptaneNetwork, DownloadFailure);
110  FRIEND_TEST(UptaneNetwork, LogConnectivityRestored);
111  FRIEND_TEST(UptaneVector, Test);
112  FRIEND_TEST(aktualizr_secondary_uptane, credentialsPassing);
113  friend class CheckForUpdate; // for load tests
114  friend class ProvisionDeviceTask; // for load tests
115 
116  void uptaneIteration(std::vector<Uptane::Target> *targets, unsigned int *ecus_count);
117  void uptaneOfflineIteration(std::vector<Uptane::Target> *targets, unsigned int *ecus_count);
118  result::UpdateStatus checkUpdatesOffline(const std::vector<Uptane::Target> &targets);
119  Json::Value AssembleManifest();
120  std::string secondaryTreehubCredentials() const;
121  std::exception_ptr getLastException() const { return last_exception; }
122  static std::vector<Uptane::Target> findForEcu(const std::vector<Uptane::Target> &targets,
123  const Uptane::EcuSerial &ecu_id);
124  data::InstallationResult PackageInstallSetResult(const Uptane::Target &target);
125  void finalizeAfterReboot();
126  void reportHwInfo(const Json::Value &custom_hwinfo);
127  void reportInstalledPackages();
128  void reportNetworkInfo();
129  void reportAktualizrConfiguration();
130  void verifySecondaries();
131  bool waitSecondariesReachable(const std::vector<Uptane::Target> &updates);
132  bool sendMetadataToEcus(const std::vector<Uptane::Target> &targets);
133  std::future<data::ResultCode::Numeric> sendFirmwareAsync(Uptane::SecondaryInterface &secondary,
134  const Uptane::Target &target);
135  std::vector<result::Install::EcuReport> sendImagesToEcus(const std::vector<Uptane::Target> &targets);
136 
137  bool putManifestSimple(const Json::Value &custom = Json::nullValue);
138  void storeInstallationFailure(const data::InstallationResult &result);
139  void getNewTargets(std::vector<Uptane::Target> *new_targets, unsigned int *ecus_count = nullptr);
140  void rotateSecondaryRoot(Uptane::RepositoryType repo, Uptane::SecondaryInterface &secondary);
141  void updateDirectorMeta();
142  void checkDirectorMetaOffline();
143  void computeDeviceInstallationResult(data::InstallationResult *result, std::string *raw_installation_report) const;
144  std::unique_ptr<Uptane::Target> findTargetInDelegationTree(const Uptane::Target &target, bool offline);
145  std::unique_ptr<Uptane::Target> findTargetHelper(const Uptane::Targets &cur_targets,
146  const Uptane::Target &queried_target, int level, bool terminating,
147  bool offline);
148  void checkAndUpdatePendingSecondaries();
149  const Uptane::EcuSerial &primaryEcuSerial() const { return primary_ecu_serial_; }
150  boost::optional<Uptane::HardwareIdentifier> ecuHwId(const Uptane::EcuSerial &serial) const;
151 
152  template <class T, class... Args>
153  void sendEvent(Args &&... args) {
154  std::shared_ptr<event::BaseEvent> event = std::make_shared<T>(std::forward<Args>(args)...);
155  if (events_channel) {
156  (*events_channel)(std::move(event));
157  } else if (!event->isTypeOf<event::DownloadProgressReport>()) {
158  LOG_INFO << "got " << event->variant << " event";
159  }
160  }
161 
162  Config &config;
163  Uptane::DirectorRepository director_repo;
164  Uptane::ImageRepository image_repo;
165  Uptane::ManifestIssuer::Ptr uptane_manifest;
166  std::shared_ptr<INvStorage> storage;
167  std::shared_ptr<HttpInterface> http;
168  std::shared_ptr<PackageManagerInterface> package_manager_;
169  std::shared_ptr<Uptane::Fetcher> uptane_fetcher;
170  std::unique_ptr<ReportQueue> report_queue;
171  std::shared_ptr<event::Channel> events_channel;
172  boost::signals2::scoped_connection conn;
173  std::exception_ptr last_exception;
174  // ecu_serial => secondary*
175  std::map<Uptane::EcuSerial, Uptane::SecondaryInterface::Ptr> secondaries;
176  std::mutex download_mutex;
177  Uptane::EcuSerial primary_ecu_serial_;
178  Uptane::HardwareIdentifier primary_ecu_hw_id_;
179 };
180 
182  public:
183  explicit TargetCompare(const Uptane::Target &target_in) : target(target_in) {}
184  bool operator()(const Uptane::Target &in) const { return (in.MatchTarget(target)); }
185 
186  private:
187  const Uptane::Target &target;
188 };
189 
191  public:
192  explicit SerialCompare(Uptane::EcuSerial serial_in) : serial(std::move(serial_in)) {}
193  bool operator()(const std::pair<Uptane::EcuSerial, Uptane::HardwareIdentifier> &in) const {
194  return (in.first == serial);
195  }
196 
197  private:
198  const Uptane::EcuSerial serial;
199 };
200 
201 #endif // SOTA_UPTANE_CLIENT_H_
Provides a thread-safe way to pause and terminate task execution.
Definition: apiqueue.h:19
Container for information about downloading an update.
Definition: results.h:117
A report for a download in progress.
Definition: events.h:71
Container for information about available campaigns.
Definition: results.h:17
UpdateStatus
Status of an update.
Definition: results.h:26
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:74
Container for information about installing an update.
Definition: results.h:130
Container for information about available updates.
Definition: results.h:38
Results of libaktualizr API calls.
Definition: results.h:13
Base data types that are used in The Update Framework (TUF), part of Uptane.
This class provides the main APIs necessary for launching and controlling libaktualizr.
Definition: aktualizr.h:20
Aktualizr status events.
Definition: events.h:18