1 #include "libaktualizr-c.h"
4 #include "utilities/utils.h"
10 }
catch (
const std::exception &e) {
11 std::cerr <<
"Aktualizr_create exception: " << e.what() << std::endl;
17 Aktualizr *Aktualizr_create_from_path(
const char *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;
30 }
catch (
const std::exception &e) {
31 std::cerr <<
"Aktualizr_initialize exception: " << e.what() << std::endl;
37 int Aktualizr_uptane_cycle(
Aktualizr *a) {
40 }
catch (
const std::exception &e) {
41 std::cerr <<
"Uptane cycle exception: " << e.what() << std::endl;
47 void Aktualizr_destroy(
Aktualizr *a) {
delete a; }
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;
55 (*handler)(
event->variant.c_str());
58 int Aktualizr_set_signal_handler(
Aktualizr *a,
void (*handler)(
const char *event_name)) {
60 auto functor = std::bind(handler_wrapper, std::placeholders::_1, handler);
63 }
catch (
const std::exception &e) {
64 std::cerr <<
"Aktualizr_set_signal_handler exception: " << e.what() << std::endl;
70 Campaign *Aktualizr_campaigns_check(
Aktualizr *a) {
73 if (!r.campaigns.empty()) {
75 return new Campaign(r.campaigns[0]);
77 }
catch (
const std::exception &e) {
78 std::cerr <<
"Campaign check exception: " << e.what() << std::endl;
84 int Aktualizr_campaign_accept(
Aktualizr *a, Campaign *c) {
87 }
catch (
const std::exception &e) {
88 std::cerr <<
"Campaign accept exception: " << e.what() << std::endl;
94 int Aktualizr_campaign_postpone(
Aktualizr *a, Campaign *c) {
97 }
catch (
const std::exception &e) {
98 std::cerr <<
"Campaign postpone exception: " << e.what() << std::endl;
104 int Aktualizr_campaign_decline(
Aktualizr *a, Campaign *c) {
107 }
catch (
const std::exception &e) {
108 std::cerr <<
"Campaign decline exception: " << e.what() << std::endl;
114 void Aktualizr_campaign_free(Campaign *c) {
delete c; }
116 Updates *Aktualizr_updates_check(
Aktualizr *a) {
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;
126 void Aktualizr_updates_free(Updates *u) {
delete u; }
128 size_t Aktualizr_get_targets_num(Updates *u) {
return (u ==
nullptr) ? 0 : u->size(); }
130 Target *Aktualizr_get_nth_target(Updates *u,
size_t n) {
137 }
catch (
const std::exception &e) {
138 std::cerr <<
"Exception: " << e.what() << std::endl;
144 const char *Aktualizr_get_target_name(Target *t) {
146 auto length = t->filename().length();
147 auto *name =
new char[length + 1];
148 strncpy(name, t->filename().c_str(), length + 1);
155 void Aktualizr_free_target_name(
const char *n) {
delete[] n; }
157 int Aktualizr_download_target(
Aktualizr *a, Target *t) {
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;
167 int Aktualizr_install_target(
Aktualizr *a, Target *t) {
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;
177 int Aktualizr_send_manifest(
Aktualizr *a,
const char *manifest) {
179 Json::Value custom = Utils::parseJSON(manifest);
182 }
catch (
const std::exception &e) {
183 std::cerr <<
"Aktualizr_send_manifest exception: " << e.what() << std::endl;
188 int Aktualizr_send_device_data(
Aktualizr *a) {
192 }
catch (
const std::exception &e) {
193 std::cerr <<
"Aktualizr_send_device_data exception: " << e.what() << std::endl;
198 StorageTargetHandle *Aktualizr_open_stored_target(
Aktualizr *a,
const Target *t) {
200 std::cerr <<
"Aktualizr_open_stored_target failed: invalid input" << std::endl;
207 }
catch (
const std::exception &e) {
208 std::cerr <<
"Aktualizr_open_stored_target exception: " << e.what() << std::endl;
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());
218 std::cerr <<
"Aktualizr_read_stored_target failed: invalid input " << (handle ==
nullptr ?
"handle" :
"buffer")
224 int Aktualizr_close_stored_target(StorageTargetHandle *handle) {
225 if (handle !=
nullptr) {
230 std::cerr <<
"Aktualizr_close_stored_target failed: no input handle" << std::endl;
237 case result::PauseStatus::kSuccess: {
238 return Pause_Status_C::kSuccess;
240 case result::PauseStatus::kAlreadyPaused: {
241 return Pause_Status_C::kAlreadyPaused;
243 case result::PauseStatus::kAlreadyRunning: {
244 return Pause_Status_C::kAlreadyRunning;
246 case result::PauseStatus::kError: {
247 return Pause_Status_C::kError;
251 return Pause_Status_C::kError;
256 Pause_Status_C Aktualizr_pause(
Aktualizr *a) {
258 return ::get_Pause_Status_C(pause.status);
261 Pause_Status_C Aktualizr_resume(
Aktualizr *a) {
263 return ::get_Pause_Status_C(pause.status);