Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
aktualizr.h
1 #ifndef AKTUALIZR_H_
2 #define AKTUALIZR_H_
3 
4 #include <future>
5 #include <memory>
6 
7 #include <boost/signals2.hpp>
8 
9 #include "libaktualizr/config.h"
10 #include "libaktualizr/events.h"
11 #include "primary/secondaryinterface.h"
12 #include "sotauptaneclient.h"
13 #include "storage/invstorage.h"
14 #include "utilities/apiqueue.h"
15 
16 /**
17  * This class provides the main APIs necessary for launching and controlling
18  * libaktualizr.
19  */
20 class Aktualizr {
21  public:
22  /** Aktualizr requires a configuration object. Examples can be found in the
23  * config directory. */
24  explicit Aktualizr(const Config& config);
25  Aktualizr(const Aktualizr&) = delete;
26  Aktualizr& operator=(const Aktualizr&) = delete;
27 
28  /**
29  * Initialize aktualizr. Any Secondaries should be added before making this
30  * call. This will provision with the server if required. This must be called
31  * before using any other aktualizr functions except AddSecondary.
32  */
33  void Initialize();
34 
35  /**
36  * Asynchronously run aktualizr indefinitely until Shutdown is called.
37  * @param custom_hwinfo if not empty will be sent to the backend instead of `lshw` output
38  * @return Empty std::future object
39  */
40  std::future<void> RunForever(const Json::Value& custom_hwinfo = Json::nullValue);
41 
42  /**
43  * Shuts down currently running `RunForever()` method
44  */
45  void Shutdown();
46 
47  /**
48  * Check for campaigns.
49  * Campaigns are a concept outside of Uptane, and allow for user approval of
50  * updates before the contents of the update are known.
51  * @return std::future object with data about available campaigns.
52  */
53  std::future<result::CampaignCheck> CampaignCheck();
54 
55  /**
56  * Act on campaign: accept, decline or postpone.
57  * Accepted campaign will be removed from the campaign list but no guarantee
58  * is made for declined or postponed items. Applications are responsible for
59  * tracking their state but this method will notify the server for device
60  * state monitoring purposes.
61  * @param campaign_id Campaign ID as provided by CampaignCheck.
62  * @param cmd action to apply on the campaign: accept, decline or postpone
63  * @return Empty std::future object
64  */
65  std::future<void> CampaignControl(const std::string& campaign_id, campaign::Cmd cmd);
66 
67  /**
68  * Send local device data to the server.
69  * This includes network status, installed packages, hardware etc.
70  * @param custom_hwinfo if not empty will be sent to the backend instead of `lshw` output
71  * @return Empty std::future object
72  */
73  std::future<void> SendDeviceData(const Json::Value& custom_hwinfo = Json::nullValue);
74 
75  /**
76  * Fetch Uptane metadata and check for updates.
77  * This collects a client manifest, PUTs it to the director, updates the
78  * Uptane metadata (including root and targets), and then checks the metadata
79  * for target updates.
80  * @return Information about available updates.
81  */
82  std::future<result::UpdateCheck> CheckUpdates();
83 
84  /**
85  * Download targets.
86  * @param updates Vector of targets to download as provided by CheckUpdates.
87  * @return std::future object with information about download results.
88  */
89  std::future<result::Download> Download(const std::vector<Uptane::Target>& updates);
90 
91  /**
92  * Get log of installations. The log is indexed for every ECU and contains
93  * every change of versions ordered by time. It may contain duplicates in
94  * case of rollbacks.
95  * @return installation log
96  */
99  std::vector<Uptane::Target> installs;
100  };
101  using InstallationLog = std::vector<InstallationLogEntry>;
102  InstallationLog GetInstallationLog();
103 
104  /**
105  * Get list of targets currently in storage. This is intended to be used with
106  * DeleteStoredTarget and targets are not guaranteed to be verified and
107  * up-to-date with current metadata.
108  * @return std::vector of target objects
109  */
110  std::vector<Uptane::Target> GetStoredTargets();
111 
112  /**
113  * Delete a stored target from storage. This only affects storage of the
114  * actual binary data and does not preclude a re-download if a target matches
115  * current metadata.
116  * @param target Target object matching the desired target in the storage
117  * @return true if successful
118  */
119  void DeleteStoredTarget(const Uptane::Target& target);
120 
121  /**
122  * Get target downloaded in Download call. Returned target is guaranteed to be verified and up-to-date
123  * according to the Uptane metadata downloaded in CheckUpdates call.
124  * @param target Target object matching the desired target in the storage.
125  * @return Handle to the stored binary. nullptr if none is found.
126  */
127  std::ifstream OpenStoredTarget(const Uptane::Target& target);
128 
129  /**
130  * Install targets.
131  * @param updates Vector of targets to install as provided by CheckUpdates or
132  * Download.
133  * @return std::future object with information about installation results.
134  */
135  std::future<result::Install> Install(const std::vector<Uptane::Target>& updates);
136 
137  /**
138  * SetInstallationRawReport allows setting a custom raw report field in the device installation result.
139  *
140  * @note An invocation of this method will have effect only after call of Aktualizr::Install and before calling
141  * Aktualizr::SendManifest member function.
142  * @param custom_raw_report is intended to replace a default value in the device installation report.
143  * @return true if the custom raw report was successfully applied to the device installation result.
144  * If there is no installation report in the storage the function will always return false.
145  */
146  bool SetInstallationRawReport(const std::string& custom_raw_report);
147 
148  /**
149  * Send installation report to the backend.
150  *
151  * @note The device manifest is also sent as a part of CheckUpdates and
152  * SendDeviceData calls, as well as after a reboot if it was initiated
153  * by Aktualizr as a part of an installation process.
154  * All these manifests will not include the custom data provided in this call.
155  *
156  * @param custom Project-specific data to put in the custom field of Uptane manifest
157  * @return std::future object with manifest update result (true on success).
158  */
159  std::future<bool> SendManifest(const Json::Value& custom = Json::nullValue);
160 
161  /**
162  * Pause the library operations.
163  * In progress target downloads will be paused and API calls will be deferred.
164  *
165  * @return Information about pause results.
166  */
168 
169  /**
170  * Resume the library operations.
171  * Target downloads will resume and API calls issued during the pause will
172  * execute in fifo order.
173  *
174  * @return Information about resume results.
175  */
177 
178  /**
179  * Aborts the currently running command, if it can be aborted, or waits for it
180  * to finish; then removes all other queued calls.
181  * This doesn't reset the `Paused` state, i.e. if the queue was previously
182  * paused, it will remain paused, but with an emptied queue.
183  * The call is blocking.
184  */
185  void Abort();
186 
187  /**
188  * Synchronously run an Uptane cycle: check for updates, download any new
189  * targets, install them, and send a manifest back to the server.
190  *
191  * @return `false`, if the restart is required to continue, `true` otherwise
192  */
193  bool UptaneCycle();
194 
195  /**
196  * Add new Secondary to aktualizr. Must be called before Initialize.
197  * @param secondary An object to perform installation on a Secondary ECU.
198  */
199  void AddSecondary(const std::shared_ptr<SecondaryInterface>& secondary);
200 
201  /**
202  * Store some free-form data to be associated with a particular Secondary, to
203  * be retrieved later through `GetSecondaries`
204  */
205  void SetSecondaryData(const Uptane::EcuSerial& ecu, const std::string& data);
206 
207  /**
208  * Returns a list of the registered Secondaries, along with some associated
209  * metadata
210  *
211  * @return vector of SecondaryInfo objects
212  */
213  std::vector<SecondaryInfo> GetSecondaries() const;
214 
215  // The type proxy is needed in doxygen 1.8.16 because of this bug
216  // https://github.com/doxygen/doxygen/issues/7236
217  using SigHandler = std::function<void(std::shared_ptr<event::BaseEvent>)>;
218 
219  /**
220  * Provide a function to receive event notifications.
221  * @param handler a function that can receive event objects.
222  * @return a signal connection object, which can be disconnected if desired.
223  */
224  boost::signals2::connection SetSignalHandler(const SigHandler& handler);
225 
226  private:
227  // Make sure this is declared before SotaUptaneClient to prevent Valgrind
228  // complaints with destructors.
229  Config config_;
230 
231  protected:
232  Aktualizr(Config config, std::shared_ptr<INvStorage> storage_in, const std::shared_ptr<HttpInterface>& http_in);
233 
234  std::shared_ptr<SotaUptaneClient> uptane_client_;
235 
236  private:
237  struct {
238  std::mutex m;
239  std::condition_variable cv;
240  bool flag = false;
241  } exit_cond_;
242 
243  std::shared_ptr<INvStorage> storage_;
244  std::shared_ptr<event::Channel> sig_;
245  api::CommandQueue api_queue_;
246 };
247 
248 #endif // AKTUALIZR_H_
void AddSecondary(const std::shared_ptr< SecondaryInterface > &secondary)
Add new Secondary to aktualizr.
Definition: aktualizr.cc:99
General data structures.
Definition: types.h:215
void Abort()
Aborts the currently running command, if it can be aborted, or waits for it to finish; then removes a...
Definition: aktualizr.cc:186
std::vector< Uptane::Target > GetStoredTargets()
Get list of targets currently in storage.
Definition: aktualizr.cc:215
bool UptaneCycle()
Synchronously run an Uptane cycle: check for updates, download any new targets, install them...
Definition: aktualizr.cc:34
result::Pause Resume()
Resume the library operations.
Definition: aktualizr.cc:177
std::vector< SecondaryInfo > GetSecondaries() const
Returns a list of the registered Secondaries, along with some associated metadata.
Definition: aktualizr.cc:107
Aktualizr(const Config &config)
Aktualizr requires a configuration object.
Definition: aktualizr.cc:13
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:210
std::future< void > RunForever(const Json::Value &custom_hwinfo=Json::nullValue)
Asynchronously run aktualizr indefinitely until Shutdown is called.
Definition: aktualizr.cc:71
void DeleteStoredTarget(const Uptane::Target &target)
Delete a stored target from storage.
Definition: aktualizr.cc:217
void Shutdown()
Shuts down currently running RunForever() method.
Definition: aktualizr.cc:91
std::future< result::Download > Download(const std::vector< Uptane::Target > &updates)
Download targets.
Definition: aktualizr.cc:148
std::future< result::UpdateCheck > CheckUpdates()
Fetch Uptane metadata and check for updates.
Definition: aktualizr.cc:143
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...
Definition: aktualizr.cc:103
bool SetInstallationRawReport(const std::string &custom_raw_report)
SetInstallationRawReport allows setting a custom raw report field in the device installation result...
Definition: aktualizr.cc:159
result::Pause Pause()
Pause the library operations.
Definition: aktualizr.cc:168
std::future< result::Install > Install(const std::vector< Uptane::Target > &updates)
Install targets.
Definition: aktualizr.cc:154
std::future< result::CampaignCheck > CampaignCheck()
Check for campaigns.
Definition: aktualizr.cc:114
std::ifstream OpenStoredTarget(const Uptane::Target &target)
Get target downloaded in Download call.
Definition: aktualizr.cc:219
std::future< void > SendDeviceData(const Json::Value &custom_hwinfo=Json::nullValue)
Send local device data to the server.
Definition: aktualizr.cc:138
boost::signals2::connection SetSignalHandler(const SigHandler &handler)
Provide a function to receive event notifications.
Definition: aktualizr.cc:188
std::future< bool > SendManifest(const Json::Value &custom=Json::nullValue)
Send installation report to the backend.
Definition: aktualizr.cc:163
This class provides the main APIs necessary for launching and controlling libaktualizr.
Definition: aktualizr.h:20
void Initialize()
Initialize aktualizr.
Definition: aktualizr.cc:29
std::future< void > CampaignControl(const std::string &campaign_id, campaign::Cmd cmd)
Act on campaign: accept, decline or postpone.
Definition: aktualizr.cc:119
Get log of installations.
Definition: aktualizr.h:97