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