Aktualizr
C++ SOTA Client
aktualizr.h
1 #ifndef AKTUALIZR_H_
2 #define AKTUALIZR_H_
3 
4 #include <atomic>
5 #include <memory>
6 
7 #include <gtest/gtest.h>
8 #include <boost/signals2.hpp>
9 
10 #include "config/config.h"
11 #include "sotauptaneclient.h"
12 #include "storage/invstorage.h"
13 #include "uptane/secondaryinterface.h"
14 #include "utilities/events.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(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  * Run aktualizr indefinitely until Shutdown is called. Intended to be used
37  * with the Full \ref RunningMode setting. You may want to run this on its own
38  * thread.
39  */
40  int RunForever();
41 
42  /**
43  * Asynchronously shut aktualizr down if it is running indefinitely with the
44  * Full \ref RunningMode.
45  */
46  void Shutdown();
47 
48  /**
49  * Check for campaigns.
50  * Campaigns are a concept outside of Uptane, and allow for user approval of
51  * updates before the contents of the update are known.
52  * @return Data about available campaigns.
53  */
55 
56  /**
57  * Accept a campaign for the current device.
58  * Campaigns are a concept outside of Uptane, and allow for user approval of
59  * updates before the contents of the update are known.
60  * @param campaign_id Campaign ID as provided by CampaignCheck.
61  */
62  void CampaignAccept(const std::string& campaign_id);
63 
64  /**
65  * Send local device data to the server.
66  * This includes network status, installed packages, hardware etc.
67  */
68  void SendDeviceData();
69 
70  /**
71  * Fetch Uptane metadata and check for updates.
72  * This collects a client manifest, PUTs it to the director, updates the
73  * Uptane metadata (including root and targets), and then checks the metadata
74  * for target updates.
75  * @return Information about available updates.
76  */
78 
79  /**
80  * Download targets.
81  * @param updates Vector of targets to download as provided by CheckUpdates.
82  * @return Information about download results.
83  */
84  DownloadResult Download(const std::vector<Uptane::Target>& updates);
85 
86  /**
87  * Get target downloaded in Download call. Returned target is guaranteed to be verified and up-to-date
88  * according to the Uptane metadata downloaded in CheckUpdates call.
89  * @param filename Name of the binary in the storage
90  * @return Handle to the stored binary. nullptr if none is found.
91  */
92  std::unique_ptr<StorageTargetRHandle> GetStoredTarget(const std::string& filename);
93 
94  /**
95  * Install targets.
96  * @param updates Vector of targets to install as provided by CheckUpdates or
97  * Download.
98  * @return Information about installation results.
99  */
100  InstallResult Install(const std::vector<Uptane::Target>& updates);
101 
102  /**
103  * Pause a download current in progress.
104  * @return Information about pause results.
105  */
106  PauseResult Pause();
107 
108  /**
109  * Resume a paused download.
110  * @return Information about resume results.
111  */
113 
114  /**
115  * Synchronously run an uptane cycle.
116  *
117  * Behaviour depends on the configured running mode (full cycle, check and
118  * download or check and install)
119  */
120  void UptaneCycle();
121 
122  /**
123  * Add new secondary to aktualizr. Must be called before Initialize.
124  * @param secondary An object to perform installation on a secondary ECU.
125  */
126  void AddSecondary(const std::shared_ptr<Uptane::SecondaryInterface>& secondary);
127 
128  /**
129  * Provide a function to receive event notifications.
130  * @param handler a function that can receive event objects.
131  * @return a signal connection object, which can be disconnected if desired.
132  */
133  boost::signals2::connection SetSignalHandler(std::function<void(std::shared_ptr<event::BaseEvent>)>& handler);
134 
135  private:
136  FRIEND_TEST(Aktualizr, FullNoUpdates);
137  FRIEND_TEST(Aktualizr, FullWithUpdates);
138  FRIEND_TEST(Aktualizr, FullMultipleSecondaries);
139  FRIEND_TEST(Aktualizr, CheckWithUpdates);
140  FRIEND_TEST(Aktualizr, DownloadWithUpdates);
141  FRIEND_TEST(Aktualizr, InstallWithUpdates);
142  FRIEND_TEST(Aktualizr, CampaignCheck);
143  FRIEND_TEST(Aktualizr, FullNoCorrelationId);
144  Aktualizr(Config& config, std::shared_ptr<INvStorage> storage_in, std::shared_ptr<SotaUptaneClient> uptane_client_in,
145  std::shared_ptr<event::Channel> sig_in);
146  void systemSetup();
147 
148  Config& config_;
149  std::shared_ptr<INvStorage> storage_;
150  std::shared_ptr<SotaUptaneClient> uptane_client_;
151  std::shared_ptr<event::Channel> sig_;
152  std::atomic<bool> shutdown_ = {false};
153 };
154 
155 #endif // AKTUALIZR_H_
void CampaignAccept(const std::string &campaign_id)
Accept a campaign for the current device.
Definition: aktualizr.cc:86
void AddSecondary(const std::shared_ptr< Uptane::SecondaryInterface > &secondary)
Add new secondary to aktualizr.
Definition: aktualizr.cc:78
PauseResult
Result of an attempt to pause or resume a download.
Definition: results.h:55
boost::signals2::connection SetSignalHandler(std::function< void(std::shared_ptr< event::BaseEvent >)> &handler)
Provide a function to receive event notifications.
Definition: aktualizr.cc:104
void SendDeviceData()
Send local device data to the server.
Definition: aktualizr.cc:88
PauseResult Resume()
Resume a paused download.
Definition: aktualizr.cc:102
void UptaneCycle()
Synchronously run an uptane cycle.
Definition: aktualizr.cc:48
Container for information about downloading an update.
Definition: results.h:87
Configuration object for an aktualizr instance running on a primary ECU.
Definition: config.h:97
void Shutdown()
Asynchronously shut aktualizr down if it is running indefinitely with the Full RunningMode.
Definition: aktualizr.cc:82
Container for information about available campaigns.
Definition: results.h:14
InstallResult Install(const std::vector< Uptane::Target > &updates)
Install targets.
Definition: aktualizr.cc:96
int RunForever()
Run aktualizr indefinitely until Shutdown is called.
Definition: aktualizr.cc:69
UpdateCheckResult CheckUpdates()
Fetch Uptane metadata and check for updates.
Definition: aktualizr.cc:90
Container for information about available updates.
Definition: results.h:35
Aktualizr(Config &config)
Aktualizr requires a configuration object.
Definition: aktualizr.cc:14
PauseResult Pause()
Pause a download current in progress.
Definition: aktualizr.cc:100
DownloadResult Download(const std::vector< Uptane::Target > &updates)
Download targets.
Definition: aktualizr.cc:92
This class provides the main APIs necessary for launching and controlling libaktualizr.
Definition: aktualizr.h:20
CampaignCheckResult CampaignCheck()
Check for campaigns.
Definition: aktualizr.cc:84
Container for information about installing an update.
Definition: results.h:112
std::unique_ptr< StorageTargetRHandle > GetStoredTarget(const std::string &filename)
Get target downloaded in Download call.
Definition: aktualizr.cc:108