Aktualizr
C++ SOTA Client
libaktualizr-c.cc
1 #include "libaktualizr-c.h"
2 #include "libaktualizr/events.h"
3 
4 #include "utilities/utils.h"
5 
6 Aktualizr *Aktualizr_create_from_cfg(Config *cfg) {
7  Aktualizr *a;
8  try {
9  a = new Aktualizr(*cfg);
10  } catch (const std::exception &e) {
11  std::cerr << "Aktualizr_create exception: " << e.what() << std::endl;
12  return nullptr;
13  }
14  return a;
15 }
16 
17 Aktualizr *Aktualizr_create_from_path(const char *config_path) {
18  try {
19  Config cfg(config_path);
20  return Aktualizr_create_from_cfg(&cfg);
21  } catch (const std::exception &e) {
22  std::cerr << "Aktualizr_create exception: " << e.what() << std::endl;
23  return nullptr;
24  }
25 }
26 
27 int Aktualizr_initialize(Aktualizr *a) {
28  try {
29  a->Initialize();
30  } catch (const std::exception &e) {
31  std::cerr << "Aktualizr_initialize exception: " << e.what() << std::endl;
32  return -1;
33  }
34  return 0;
35 }
36 
37 int Aktualizr_uptane_cycle(Aktualizr *a) {
38  try {
39  a->UptaneCycle();
40  } catch (const std::exception &e) {
41  std::cerr << "Uptane cycle exception: " << e.what() << std::endl;
42  return -1;
43  }
44  return 0;
45 }
46 
47 void Aktualizr_destroy(Aktualizr *a) { delete a; }
48 
49 static void handler_wrapper(const std::shared_ptr<event::BaseEvent> &event, void (*handler)(const char *)) {
50  if (handler == nullptr) {
51  std::cerr << "handler_wrapper error: no external handler" << std::endl;
52  return;
53  }
54 
55  (*handler)(event->variant.c_str());
56 }
57 
58 int Aktualizr_set_signal_handler(Aktualizr *a, void (*handler)(const char *event_name)) {
59  try {
60  auto functor = std::bind(handler_wrapper, std::placeholders::_1, handler);
61  a->SetSignalHandler(functor);
62 
63  } catch (const std::exception &e) {
64  std::cerr << "Aktualizr_set_signal_handler exception: " << e.what() << std::endl;
65  return -1;
66  }
67  return 0;
68 }
69 
70 Campaign *Aktualizr_campaigns_check(Aktualizr *a) {
71  try {
72  auto r = a->CampaignCheck().get();
73  if (!r.campaigns.empty()) {
74  // We don't support multiple campaigns at the moment
75  return new Campaign(r.campaigns[0]);
76  }
77  } catch (const std::exception &e) {
78  std::cerr << "Campaign check exception: " << e.what() << std::endl;
79  return nullptr;
80  }
81  return nullptr;
82 }
83 
84 int Aktualizr_campaign_accept(Aktualizr *a, Campaign *c) {
85  try {
86  a->CampaignControl(c->id, campaign::Cmd::Accept).get();
87  } catch (const std::exception &e) {
88  std::cerr << "Campaign accept exception: " << e.what() << std::endl;
89  return -1;
90  }
91  return 0;
92 }
93 
94 int Aktualizr_campaign_postpone(Aktualizr *a, Campaign *c) {
95  try {
96  a->CampaignControl(c->id, campaign::Cmd::Postpone).get();
97  } catch (const std::exception &e) {
98  std::cerr << "Campaign postpone exception: " << e.what() << std::endl;
99  return -1;
100  }
101  return 0;
102 }
103 
104 int Aktualizr_campaign_decline(Aktualizr *a, Campaign *c) {
105  try {
106  a->CampaignControl(c->id, campaign::Cmd::Decline).get();
107  } catch (const std::exception &e) {
108  std::cerr << "Campaign decline exception: " << e.what() << std::endl;
109  return -1;
110  }
111  return 0;
112 }
113 
114 void Aktualizr_campaign_free(Campaign *c) { delete c; }
115 
116 Updates *Aktualizr_updates_check(Aktualizr *a) {
117  try {
118  auto r = a->CheckUpdates().get();
119  return (!r.updates.empty()) ? new Updates(std::move(r.updates)) : nullptr;
120  } catch (const std::exception &e) {
121  std::cerr << "Campaign decline exception: " << e.what() << std::endl;
122  return nullptr;
123  }
124 }
125 
126 void Aktualizr_updates_free(Updates *u) { delete u; }
127 
128 size_t Aktualizr_get_targets_num(Updates *u) { return (u == nullptr) ? 0 : u->size(); }
129 
130 Target *Aktualizr_get_nth_target(Updates *u, size_t n) {
131  try {
132  if (u != nullptr) {
133  return &u->at(n);
134  } else {
135  return nullptr;
136  }
137  } catch (const std::exception &e) {
138  std::cerr << "Exception: " << e.what() << std::endl;
139  return nullptr;
140  }
141 }
142 
143 // TODO: Would it be nicer if t->filename returned const ref?
144 const char *Aktualizr_get_target_name(Target *t) {
145  if (t != nullptr) {
146  auto length = t->filename().length();
147  auto *name = new char[length + 1];
148  strncpy(name, t->filename().c_str(), length + 1);
149  return name;
150  } else {
151  return nullptr;
152  }
153 }
154 
155 void Aktualizr_free_target_name(const char *n) { delete[] n; }
156 
157 int Aktualizr_download_target(Aktualizr *a, Target *t) {
158  try {
159  a->Download(std::vector<Uptane::Target>({*t})).get();
160  } catch (const std::exception &e) {
161  std::cerr << "Campaign decline exception: " << e.what() << std::endl;
162  return -1;
163  }
164  return 0;
165 }
166 
167 int Aktualizr_install_target(Aktualizr *a, Target *t) {
168  try {
169  a->Install(std::vector<Uptane::Target>({*t})).get();
170  } catch (const std::exception &e) {
171  std::cerr << "Campaign decline exception: " << e.what() << std::endl;
172  return -1;
173  }
174  return 0;
175 }
176 
177 int Aktualizr_send_manifest(Aktualizr *a, const char *manifest) {
178  try {
179  Json::Value custom = Utils::parseJSON(manifest);
180  bool r = a->SendManifest(custom).get();
181  return r ? 0 : -1;
182  } catch (const std::exception &e) {
183  std::cerr << "Aktualizr_send_manifest exception: " << e.what() << std::endl;
184  return -1;
185  }
186 }
187 
188 int Aktualizr_send_device_data(Aktualizr *a) {
189  try {
190  a->SendDeviceData().get();
191  return 0;
192  } catch (const std::exception &e) {
193  std::cerr << "Aktualizr_send_device_data exception: " << e.what() << std::endl;
194  return -1;
195  }
196 }
197 
198 StorageTargetHandle *Aktualizr_open_stored_target(Aktualizr *a, const Target *t) {
199  if (t == nullptr) {
200  std::cerr << "Aktualizr_open_stored_target failed: invalid input" << std::endl;
201  return nullptr;
202  }
203 
204  try {
205  auto *stream = new auto(a->OpenStoredTarget(*t));
206  return stream;
207  } catch (const std::exception &e) {
208  std::cerr << "Aktualizr_open_stored_target exception: " << e.what() << std::endl;
209  return nullptr;
210  }
211 }
212 
213 size_t Aktualizr_read_stored_target(StorageTargetHandle *handle, uint8_t *buf, size_t size) {
214  if (handle != nullptr && buf != nullptr) {
215  handle->read(reinterpret_cast<char *>(buf), static_cast<std::streamsize>(size));
216  return static_cast<size_t>(handle->gcount());
217  } else {
218  std::cerr << "Aktualizr_read_stored_target failed: invalid input " << (handle == nullptr ? "handle" : "buffer")
219  << std::endl;
220  return 0;
221  }
222 }
223 
224 int Aktualizr_close_stored_target(StorageTargetHandle *handle) {
225  if (handle != nullptr) {
226  handle->close();
227  delete handle;
228  return 0;
229  } else {
230  std::cerr << "Aktualizr_close_stored_target failed: no input handle" << std::endl;
231  return -1;
232  }
233 }
234 
235 static Pause_Status_C get_Pause_Status_C(result::PauseStatus in) {
236  switch (in) {
237  case result::PauseStatus::kSuccess: {
238  return Pause_Status_C::kSuccess;
239  }
240  case result::PauseStatus::kAlreadyPaused: {
241  return Pause_Status_C::kAlreadyPaused;
242  }
243  case result::PauseStatus::kAlreadyRunning: {
244  return Pause_Status_C::kAlreadyRunning;
245  }
246  case result::PauseStatus::kError: {
247  return Pause_Status_C::kError;
248  }
249  default: {
250  assert(false);
251  return Pause_Status_C::kError;
252  }
253  }
254 }
255 
256 Pause_Status_C Aktualizr_pause(Aktualizr *a) {
257  result::Pause pause = a->Pause();
258  return ::get_Pause_Status_C(pause.status);
259 }
260 
261 Pause_Status_C Aktualizr_resume(Aktualizr *a) {
262  result::Pause pause = a->Resume();
263  return ::get_Pause_Status_C(pause.status);
264 }
265 
266 void Aktualizr_abort(Aktualizr *a) { a->Abort(); }
Aktualizr::Resume
result::Pause Resume()
Resume the library operations.
Definition: aktualizr.cc:181
Aktualizr::Install
std::future< result::Install > Install(const std::vector< Uptane::Target > &updates)
Install targets.
Definition: aktualizr.cc:158
Aktualizr::SendDeviceData
std::future< void > SendDeviceData(const Json::Value &custom_hwinfo=Json::nullValue)
Send local device data to the server.
Definition: aktualizr.cc:142
Aktualizr::OpenStoredTarget
std::ifstream OpenStoredTarget(const Uptane::Target &target)
Get target downloaded in Download call.
Definition: aktualizr.cc:223
Aktualizr::SendManifest
std::future< bool > SendManifest(const Json::Value &custom=Json::nullValue)
Send installation report to the backend.
Definition: aktualizr.cc:167
Aktualizr::CampaignControl
std::future< void > CampaignControl(const std::string &campaign_id, campaign::Cmd cmd)
Act on campaign: accept, decline or postpone.
Definition: aktualizr.cc:123
Aktualizr::Initialize
void Initialize()
Initialize aktualizr.
Definition: aktualizr.cc:33
Aktualizr::CheckUpdates
std::future< result::UpdateCheck > CheckUpdates()
Fetch Uptane metadata and check for updates.
Definition: aktualizr.cc:147
Aktualizr::CampaignCheck
std::future< result::CampaignCheck > CampaignCheck()
Check for campaigns.
Definition: aktualizr.cc:118
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:208
events.h
Aktualizr
This class provides the main APIs necessary for launching and controlling libaktualizr.
Definition: aktualizr.h:24
Aktualizr::Abort
void Abort()
Aborts the currently running command, if it can be aborted, or waits for it to finish; then removes a...
Definition: aktualizr.cc:190
result::Pause
Definition: results.h:68
Aktualizr::SetSignalHandler
boost::signals2::connection SetSignalHandler(const SigHandler &handler)
Provide a function to receive event notifications.
Definition: aktualizr.cc:192
result::PauseStatus
PauseStatus
Result of an attempt to pause or resume a download.
Definition: results.h:57
Aktualizr::UptaneCycle
bool UptaneCycle()
Synchronously run an Uptane cycle: check for updates, download any new targets, install them,...
Definition: aktualizr.cc:38
Aktualizr::Download
std::future< result::Download > Download(const std::vector< Uptane::Target > &updates)
Download targets.
Definition: aktualizr.cc:152
event
Aktualizr status events.
Definition: events.h:15
Aktualizr::Pause
result::Pause Pause()
Pause the library operations.
Definition: aktualizr.cc:172