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