Aktualizr
C++ SOTA Client
types.cc
1 #include "utilities/types.h"
2 
3 #include <stdexcept>
4 #include <utility>
5 
6 namespace data {
7 Json::Value Package::toJson() {
8  Json::Value json;
9  json["name"] = name;
10  json["version"] = version;
11  return json;
12 }
13 
14 Package Package::fromJson(const std::string& json_str) {
15  Json::Reader reader;
16  Json::Value json;
17  reader.parse(json_str, json);
18  Package package;
19  package.name = json["name"].asString();
20  package.version = json["version"].asString();
21  return package;
22 }
23 
24 OperationResult::OperationResult(std::string id_in, UpdateResultCode result_code_in, std::string result_text_in)
25  : id(std::move(id_in)), result_code(result_code_in), result_text(std::move(result_text_in)) {}
26 
27 OperationResult::OperationResult(std::string id_in, InstallOutcome outcome_in)
28  : id(std::move(id_in)), result_code(outcome_in.first), result_text(outcome_in.second) {}
29 
30 InstallOutcome OperationResult::toOutcome() const { return InstallOutcome(result_code, result_text); }
31 
32 Json::Value OperationResult::toJson() const {
33  Json::Value json;
34  json["id"] = id;
35  json["result_code"] = static_cast<int>(result_code);
36  json["result_text"] = result_text;
37  return json;
38 }
39 
40 OperationResult OperationResult::fromJson(const std::string& json_str) {
41  Json::Reader reader;
42  Json::Value json;
43  reader.parse(json_str, json);
44  OperationResult operation_result;
45  operation_result.id = json["id"].asString();
46  operation_result.result_code = static_cast<UpdateResultCode>(json["result_code"].asUInt());
47  operation_result.result_text = json["result_text"].asString();
48  return operation_result;
49 }
50 
51 OperationResult OperationResult::fromOutcome(const std::string& id, const InstallOutcome& outcome) {
52  OperationResult operation_result(id, outcome);
53  return operation_result;
54 }
55 
56 } // namespace data
57 
58 RunningMode RunningModeFromString(const std::string& mode) {
59  if (mode == "full" || mode.empty()) {
60  return RunningMode::kFull;
61  } else if (mode == "once") {
62  return RunningMode::kOnce;
63  } else if (mode == "check") {
64  return RunningMode::kCheck;
65  } else if (mode == "download") {
67  } else if (mode == "install") {
68  return RunningMode::kInstall;
69  } else if (mode == "campaign_check") {
71  } else if (mode == "campaign_accept") {
73  } else {
74  throw std::runtime_error(std::string("Incorrect running mode: ") + mode);
75  }
76 }
77 
78 std::string StringFromRunningMode(RunningMode mode) {
79  std::string mode_str = "full";
80  if (mode == RunningMode::kFull) {
81  mode_str = "full";
82  } else if (mode == RunningMode::kOnce) {
83  mode_str = "once";
84  } else if (mode == RunningMode::kCheck) {
85  mode_str = "check";
86  } else if (mode == RunningMode::kDownload) {
87  mode_str = "download";
88  } else if (mode == RunningMode::kInstall) {
89  mode_str = "install";
90  } else if (mode == RunningMode::kCampaignCheck) {
91  return "campaign_check";
92  } else if (mode == RunningMode::kCampaignAccept) {
93  return "campaign_accept";
94  }
95  return mode_str;
96 }
97 
98 // vim: set tabstop=2 shiftwidth=2 expandtab:
Only accept an existing campaign.
Download any available updates and then shut down.
General data structures.
Definition: types.cc:6
Fully automated mode.
RunningMode
Execution mode to run aktualizr in.
Definition: types.h:62
Only check for an existing campaign related to the device.
One complete cycle.
UpdateResultCode
Result of an update.
Definition: types.h:122
Only check for updates.
Install any available updates and then shut down.