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