Aktualizr
C++ SOTA Client
types.h
Go to the documentation of this file.
1 #ifndef TYPES_H_
2 #define TYPES_H_
3 /** \file */
4 
5 #include <json/json.h>
6 #include <boost/filesystem.hpp>
7 #include <stdexcept>
8 
9 // Keep these int sync with AKIpUptaneKeyType ASN.1 definitions
10 enum class KeyType {
11  kED25519 = 0,
12  kFirstKnown = kED25519,
13  kRSA2048,
14  kRSA3072,
15  kRSA4096,
16  kLastKnown = kRSA4096,
17  kUnknown = 0xff
18 };
19 
20 inline std::ostream& operator<<(std::ostream& os, const KeyType kt) {
21  std::string kt_str;
22  switch (kt) {
23  case KeyType::kRSA2048:
24  kt_str = "RSA2048";
25  break;
26  case KeyType::kRSA3072:
27  kt_str = "RSA3072";
28  break;
29  case KeyType::kRSA4096:
30  kt_str = "RSA4096";
31  break;
32  case KeyType::kED25519:
33  kt_str = "ED25519";
34  break;
35  default:
36  kt_str = "unknown";
37  break;
38  }
39  os << '"' << kt_str << '"';
40  return os;
41 }
42 
43 inline std::istream& operator>>(std::istream& is, KeyType& kt) {
44  std::string kt_str;
45 
46  is >> kt_str;
47 
48  if (kt_str == "\"RSA2048\"") {
49  kt = KeyType::kRSA2048;
50  } else if (kt_str == "\"RSA3072\"") {
51  kt = KeyType::kRSA3072;
52  } else if (kt_str == "\"RSA4096\"") {
53  kt = KeyType::kRSA4096;
54  } else if (kt_str == "\"ED25519\"") {
55  kt = KeyType::kED25519;
56  } else {
57  kt = KeyType::kUnknown;
58  }
59  return is;
60 }
61 
62 /** Execution mode to run aktualizr in. */
63 enum class RunningMode {
64  /** Fully automated mode. Regularly checks for updates and downloads and
65  * installs automatically. Runs indefinitely. */
66  kFull = 0,
67  /** One complete cycle. Checks once for updates, downloads and installs
68  * anything found, and then shuts down. */
69  kOnce,
70  /** Only check for an existing campaign related to the device */
72  /** Only accept an existing campaign */
74  /** Only reject an existing campaign */
76  /** Only check for updates. Sends a manifest and device data, checks for
77  * updates, and then shuts down. */
78  kCheck,
79  /** Download any available updates and then shut down. */
80  kDownload,
81  /** Install any available updates and then shut down. Does not requite network
82  * connectivity. */
83  kInstall,
84  /** Completely manual operation. Send commands via the aktualizr class's API.
85  * Runs indefinitely until a Shutdown command is received. */
86  kManual,
87 };
88 
89 RunningMode RunningModeFromString(const std::string& mode);
90 std::string StringFromRunningMode(RunningMode mode);
91 
92 enum class CryptoSource { kFile = 0, kPkcs11 };
93 
94 inline std::ostream& operator<<(std::ostream& os, CryptoSource cs) {
95  std::string cs_str;
96  switch (cs) {
97  case CryptoSource::kFile:
98  cs_str = "file";
99  break;
100  case CryptoSource::kPkcs11:
101  cs_str = "pkcs11";
102  break;
103  default:
104  cs_str = "unknown";
105  break;
106  }
107  os << '"' << cs_str << '"';
108  return os;
109 }
110 
111 // timestamp, compatible with tuf
112 class TimeStamp {
113  public:
114  static TimeStamp Now();
115  /** An invalid TimeStamp */
116  TimeStamp() { ; }
117  explicit TimeStamp(std::string rfc3339);
118  bool IsExpiredAt(const TimeStamp& now) const;
119  bool IsValid() const;
120  std::string ToString() const { return time_; }
121  bool operator<(const TimeStamp& other) const;
122  bool operator>(const TimeStamp& other) const;
123  friend std::ostream& operator<<(std::ostream& os, const TimeStamp& t);
124  bool operator==(const TimeStamp& rhs) const { return time_ == rhs.time_; }
125 
126  class InvalidTimeStamp : public std::domain_error {
127  public:
128  InvalidTimeStamp() : std::domain_error("invalid timestamp") {}
129  ~InvalidTimeStamp() noexcept override = default;
130  };
131 
132  private:
133  std::string time_;
134 };
135 
136 std::ostream& operator<<(std::ostream& os, const TimeStamp& t);
137 
138 /// General data structures.
139 namespace data {
140 
141 using UpdateRequestId = std::string;
142 struct Package {
143  std::string name;
144  std::string version;
145  Json::Value toJson();
146  static Package fromJson(const std::string& /*json_str*/);
147 };
148 
149 /// Result of an update.
150 enum class UpdateResultCode {
151  /// Operation executed successfully
152  kOk = 0,
153  /// Operation has already been processed
155  /// Dependency failure during package install, upgrade, or removal
157  /// Update image integrity has been compromised
159  /// Package installation failed
161  /// Package upgrade failed
163  /// Package removal failed
165  /// The module loader could not flash its managed module
166  kFlashFailed,
167  /// Partition creation failed
169  /// Partition deletion failed
171  /// Partition resize failed
173  /// Partition write failed
175  /// Partition patching failed
177  /// User declined the update
179  /// Software was blacklisted
181  /// Ran out of disk space
182  kDiskFull,
183  /// Software package not found
184  kNotFound,
185  /// Tried to downgrade to older version
186  kOldVersion,
187  /// SWM Internal integrity error
189  /// Other error
191  /// Updating process in progress
193 };
194 
195 typedef std::pair<UpdateResultCode, std::string> InstallOutcome;
196 
198  OperationResult() : result_code(UpdateResultCode::kOk) {}
199  OperationResult(std::string id_in, UpdateResultCode result_code_in, std::string result_text_in);
200  OperationResult(std::string id_in, InstallOutcome outcome_in);
201  std::string id;
202  UpdateResultCode result_code{};
203  std::string result_text;
204  Json::Value toJson() const;
205  bool isSuccess() const {
206  return result_code == UpdateResultCode::kOk || result_code == UpdateResultCode::kAlreadyProcessed;
207  };
208  InstallOutcome toOutcome() const;
209  static OperationResult fromJson(const std::string& json_str);
210  static OperationResult fromOutcome(const std::string& id, const InstallOutcome& outcome);
211 };
212 
213 } // namespace data
214 
215 #endif
Package installation failed.
Only accept an existing campaign.
Download any available updates and then shut down.
TimeStamp()
An invalid TimeStamp.
Definition: types.h:116
Ran out of disk space.
General data structures.
Definition: types.cc:44
Only reject an existing campaign.
Dependency failure during package install, upgrade, or removal.
SWM Internal integrity error.
Fully automated mode.
RunningMode
Execution mode to run aktualizr in.
Definition: types.h:63
Software package not found.
The module loader could not flash its managed module.
Only check for an existing campaign related to the device.
Updating process in progress.
One complete cycle.
UpdateResultCode
Result of an update.
Definition: types.h:150
User declined the update.
Tried to downgrade to older version.
Operation has already been processed.
Completely manual operation.
Only check for updates.
Update image integrity has been compromised.
Operation executed successfully.
Install any available updates and then shut down.