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 
8 // Keep these int sync with AKIpUptaneKeyType ASN.1 definitions
9 enum class KeyType {
10  kED25519 = 0,
11  kFirstKnown = kED25519,
12  kRSA2048,
13  kRSA3072,
14  kRSA4096,
15  kLastKnown = kRSA4096,
16  kUnknown = 0xff
17 };
18 
19 inline std::ostream& operator<<(std::ostream& os, const KeyType kt) {
20  std::string kt_str;
21  switch (kt) {
22  case KeyType::kRSA2048:
23  kt_str = "RSA2048";
24  break;
25  case KeyType::kRSA3072:
26  kt_str = "RSA3072";
27  break;
28  case KeyType::kRSA4096:
29  kt_str = "RSA4096";
30  break;
31  case KeyType::kED25519:
32  kt_str = "ED25519";
33  break;
34  default:
35  kt_str = "unknown";
36  break;
37  }
38  os << '"' << kt_str << '"';
39  return os;
40 }
41 
42 inline std::istream& operator>>(std::istream& is, KeyType& kt) {
43  std::string kt_str;
44 
45  is >> kt_str;
46 
47  if (kt_str == "\"RSA2048\"") {
48  kt = KeyType::kRSA2048;
49  } else if (kt_str == "\"RSA3072\"") {
50  kt = KeyType::kRSA3072;
51  } else if (kt_str == "\"RSA4096\"") {
52  kt = KeyType::kRSA4096;
53  } else if (kt_str == "\"ED25519\"") {
54  kt = KeyType::kED25519;
55  } else {
56  kt = KeyType::kUnknown;
57  }
58  return is;
59 }
60 
61 /** Execution mode to run aktualizr in. */
62 enum class RunningMode {
63  /** Fully automated mode. Regularly checks for updates and downloads and
64  * installs automatically. Runs indefinitely. */
65  kFull = 0,
66  /** One complete cycle. Checks once for updates, downloads and installs
67  * anything found, and then shuts down. */
68  kOnce,
69  /** Only check for an existing campaign related to the device */
71  /** Only accept an existing campaign */
73  /** Only reject an existing campaign */
75  /** Only check for updates. Sends a manifest and device data, checks for
76  * updates, and then shuts down. */
77  kCheck,
78  /** Download any available updates and then shut down. */
79  kDownload,
80  /** Install any available updates and then shut down. Does not requite network
81  * connectivity. */
82  kInstall,
83  /** Completely manual operation. Send commands via the aktualizr class's API.
84  * Runs indefinitely until a Shutdown command is received. */
85  kManual,
86 };
87 
88 RunningMode RunningModeFromString(const std::string& mode);
89 std::string StringFromRunningMode(RunningMode mode);
90 
91 enum class CryptoSource { kFile = 0, kPkcs11 };
92 
93 inline std::ostream& operator<<(std::ostream& os, CryptoSource cs) {
94  std::string cs_str;
95  switch (cs) {
96  case CryptoSource::kFile:
97  cs_str = "file";
98  break;
99  case CryptoSource::kPkcs11:
100  cs_str = "pkcs11";
101  break;
102  default:
103  cs_str = "unknown";
104  break;
105  }
106  os << '"' << cs_str << '"';
107  return os;
108 }
109 
110 /// General data structures.
111 namespace data {
112 
113 using UpdateRequestId = std::string;
114 struct Package {
115  std::string name;
116  std::string version;
117  Json::Value toJson();
118  static Package fromJson(const std::string& /*json_str*/);
119 };
120 
121 /// Result of an update.
122 enum class UpdateResultCode {
123  /// Operation executed successfully
124  kOk = 0,
125  /// Operation has already been processed
127  /// Dependency failure during package install, upgrade, or removal
129  /// Update image integrity has been compromised
131  /// Package installation failed
133  /// Package upgrade failed
135  /// Package removal failed
137  /// The module loader could not flash its managed module
138  kFlashFailed,
139  /// Partition creation failed
141  /// Partition deletion failed
143  /// Partition resize failed
145  /// Partition write failed
147  /// Partition patching failed
149  /// User declined the update
151  /// Software was blacklisted
153  /// Ran out of disk space
154  kDiskFull,
155  /// Software package not found
156  kNotFound,
157  /// Tried to downgrade to older version
158  kOldVersion,
159  /// SWM Internal integrity error
161  /// Other error
163  /// Updating process in progress
165 };
166 
167 typedef std::pair<UpdateResultCode, std::string> InstallOutcome;
168 
170  OperationResult() : result_code(UpdateResultCode::kOk) {}
171  OperationResult(std::string id_in, UpdateResultCode result_code_in, std::string result_text_in);
172  OperationResult(std::string id_in, InstallOutcome outcome_in);
173  std::string id;
174  UpdateResultCode result_code{};
175  std::string result_text;
176  Json::Value toJson() const;
177  bool isSuccess() const {
178  return result_code == UpdateResultCode::kOk || result_code == UpdateResultCode::kAlreadyProcessed;
179  };
180  InstallOutcome toOutcome() const;
181  static OperationResult fromJson(const std::string& json_str);
182  static OperationResult fromOutcome(const std::string& id, const InstallOutcome& outcome);
183 };
184 
185 } // namespace data
186 
187 #endif
Package installation failed.
Only accept an existing campaign.
Download any available updates and then shut down.
Ran out of disk space.
General data structures.
Definition: types.cc:6
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:62
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:122
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.