Aktualizr
C++ SOTA Client
types.cc
1 #include "utilities/types.h"
2 
3 #include <stdexcept>
4 #include <utility>
5 
6 TimeStamp TimeStamp::Now() {
7  time_t raw_time;
8  struct tm time_struct {};
9  time(&raw_time);
10  gmtime_r(&raw_time, &time_struct);
11  char formatted[22];
12  strftime(formatted, 22, "%Y-%m-%dT%H:%M:%SZ", &time_struct);
13  return TimeStamp(formatted);
14 }
15 
16 TimeStamp::TimeStamp(std::string rfc3339) {
17  if (rfc3339.length() != 20 || rfc3339[19] != 'Z') {
19  }
20  time_ = rfc3339;
21 }
22 
23 bool TimeStamp::IsValid() const { return time_.length() != 0; }
24 
25 bool TimeStamp::IsExpiredAt(const TimeStamp &now) const {
26  if (!IsValid()) {
27  return true;
28  }
29  if (!now.IsValid()) {
30  return true;
31  }
32  return *this < now;
33 }
34 
35 bool TimeStamp::operator<(const TimeStamp &other) const { return IsValid() && other.IsValid() && time_ < other.time_; }
36 
37 bool TimeStamp::operator>(const TimeStamp &other) const { return (other < *this); }
38 
39 std::ostream &operator<<(std::ostream &os, const TimeStamp &t) {
40  os << t.time_;
41  return os;
42 }
43 
44 namespace data {
45 Json::Value Package::toJson() {
46  Json::Value json;
47  json["name"] = name;
48  json["version"] = version;
49  return json;
50 }
51 
52 Package Package::fromJson(const std::string &json_str) {
53  Json::Reader reader;
54  Json::Value json;
55  reader.parse(json_str, json);
56  Package package;
57  package.name = json["name"].asString();
58  package.version = json["version"].asString();
59  return package;
60 }
61 
62 OperationResult::OperationResult(std::string id_in, UpdateResultCode result_code_in, std::string result_text_in)
63  : id(std::move(id_in)), result_code(result_code_in), result_text(std::move(result_text_in)) {}
64 
65 OperationResult::OperationResult(std::string id_in, InstallOutcome outcome_in)
66  : id(std::move(id_in)), result_code(outcome_in.first), result_text(outcome_in.second) {}
67 
68 InstallOutcome OperationResult::toOutcome() const { return InstallOutcome(result_code, result_text); }
69 
70 Json::Value OperationResult::toJson() const {
71  Json::Value json;
72  json["id"] = id;
73  json["result_code"] = static_cast<int>(result_code);
74  json["result_text"] = result_text;
75  return json;
76 }
77 
78 OperationResult OperationResult::fromJson(const std::string &json_str) {
79  Json::Reader reader;
80  Json::Value json;
81  reader.parse(json_str, json);
82  OperationResult operation_result;
83  operation_result.id = json["id"].asString();
84  operation_result.result_code = static_cast<UpdateResultCode>(json["result_code"].asUInt());
85  operation_result.result_text = json["result_text"].asString();
86  return operation_result;
87 }
88 
89 OperationResult OperationResult::fromOutcome(const std::string &id, const InstallOutcome &outcome) {
90  OperationResult operation_result(id, outcome);
91  return operation_result;
92 }
93 
94 } // namespace data
95 
96 RunningMode RunningModeFromString(const std::string &mode) {
97  if (mode == "full" || mode.empty()) {
98  return RunningMode::kFull;
99  } else if (mode == "once") {
100  return RunningMode::kOnce;
101  } else if (mode == "check") {
102  return RunningMode::kCheck;
103  } else if (mode == "download") {
104  return RunningMode::kDownload;
105  } else if (mode == "install") {
106  return RunningMode::kInstall;
107  } else if (mode == "campaign_check") {
109  } else if (mode == "campaign_accept") {
111  } else {
112  throw std::runtime_error(std::string("Incorrect running mode: ") + mode);
113  }
114 }
115 
116 std::string StringFromRunningMode(RunningMode mode) {
117  std::string mode_str = "full";
118  if (mode == RunningMode::kFull) {
119  mode_str = "full";
120  } else if (mode == RunningMode::kOnce) {
121  mode_str = "once";
122  } else if (mode == RunningMode::kCheck) {
123  mode_str = "check";
124  } else if (mode == RunningMode::kDownload) {
125  mode_str = "download";
126  } else if (mode == RunningMode::kInstall) {
127  mode_str = "install";
128  } else if (mode == RunningMode::kCampaignCheck) {
129  return "campaign_check";
130  } else if (mode == RunningMode::kCampaignAccept) {
131  return "campaign_accept";
132  }
133  return mode_str;
134 }
135 
136 // vim: set tabstop=2 shiftwidth=2 expandtab:
Only accept an existing campaign.
Download any available updates and then shut down.
TimeStamp()
An invalid TimeStamp.
Definition: types.h:116
General data structures.
Definition: types.cc:44
Fully automated mode.
RunningMode
Execution mode to run aktualizr in.
Definition: types.h:63
Only check for an existing campaign related to the device.
One complete cycle.
UpdateResultCode
Result of an update.
Definition: types.h:150
Only check for updates.
Install any available updates and then shut down.