Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Commit

Permalink
OTA-3213 Pause()/Resume()/Abort() C APIs
Browse files Browse the repository at this point in the history
Signed-off-by: Mykola Chekhovoi <[email protected]>
  • Loading branch information
Mykola Chekhovoi committed Nov 4, 2019
1 parent 777f51e commit 9b5505d
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 1 deletion.
11 changes: 11 additions & 0 deletions include/libaktualizr-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,17 @@ StorageTargetHandle *Aktualizr_open_stored_target(Aktualizr *a, const Target *t)
size_t Aktualizr_read_stored_target(StorageTargetHandle *handle, uint8_t* buf, size_t size);
int Aktualizr_close_stored_target(StorageTargetHandle *handle);

typedef enum {
kSuccess = 0,
kAlreadyPaused,
kAlreadyRunning,
kError }
Pause_Status_C;

Pause_Status_C Aktualizr_pause(Aktualizr *a);
Pause_Status_C Aktualizr_resume(Aktualizr *a);
void Aktualizr_abort(Aktualizr *a);

#ifdef __cplusplus
}
#endif
Expand Down
43 changes: 42 additions & 1 deletion src/libaktualizr-c/libaktualizr-c.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "libaktualizr-c.h"
#include <mutex>
#include "primary/events.h"

Aktualizr *Aktualizr_create_from_cfg(Config *cfg) {
Expand Down Expand Up @@ -183,7 +184,7 @@ int Aktualizr_send_manifest(Aktualizr *a, const char *manifest) {

int Aktualizr_send_device_data(Aktualizr *a) {
try {
a->SendDeviceData();
a->SendDeviceData().get();
return 0;
} catch (const std::exception &e) {
std::cerr << "Aktualizr_send_device_data exception: " << e.what() << std::endl;
Expand Down Expand Up @@ -226,3 +227,43 @@ int Aktualizr_close_stored_target(StorageTargetHandle *handle) {
return -1;
}
}

static Pause_Status_C get_Pause_Status_C(result::PauseStatus in) {
switch (in) {
case result::PauseStatus::kSuccess: {
return Pause_Status_C::kSuccess;
}
case result::PauseStatus::kAlreadyPaused: {
return Pause_Status_C::kAlreadyPaused;
}
case result::PauseStatus::kAlreadyRunning: {
return Pause_Status_C::kAlreadyRunning;
}
case result::PauseStatus::kError: {
return Pause_Status_C::kError;
}
default: {
assert(false);
return Pause_Status_C::kError;
}
}
}

static std::mutex queue_wrapper_mutex;

Pause_Status_C Aktualizr_pause(Aktualizr *a) {
std::lock_guard<std::mutex> lock(queue_wrapper_mutex);
result::Pause pause = a->Pause();
return ::get_Pause_Status_C(pause.status);
}

Pause_Status_C Aktualizr_resume(Aktualizr *a) {
std::lock_guard<std::mutex> lock(queue_wrapper_mutex);
result::Pause pause = a->Resume();
return ::get_Pause_Status_C(pause.status);
}

void Aktualizr_abort(Aktualizr *a) {
std::lock_guard<std::mutex> lock(queue_wrapper_mutex);
a->Abort();
}
21 changes: 21 additions & 0 deletions src/libaktualizr-c/test/api-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,27 @@ int main(int argc, char **argv) {
CLEANUP_AND_RETURN_FAILED;
}

Pause_Status_C status1 = Aktualizr_pause(a);
Pause_Status_C status2 = Aktualizr_pause(a);
if (status1 != kSuccess || status2 != kAlreadyPaused) {
printf("Aktualizr_pause failed, returned %i after first call, %i after second call\n", status1, status2);
CLEANUP_AND_RETURN_FAILED;
}

Aktualizr_abort(a);
status1 = Aktualizr_pause(a);
if (status1 != kAlreadyPaused) {
printf("Aktualizr_pause failed, returned %i after Aktualizr_abort\n", status1);
CLEANUP_AND_RETURN_FAILED;
}

status1 = Aktualizr_resume(a);
status2 = Aktualizr_resume(a);
if (status1 != kSuccess || status2 != kAlreadyRunning) {
printf("Aktualizr_resume failed, returned %i after first call, %i after second call\n", status1, status2);
CLEANUP_AND_RETURN_FAILED;
}

size_t targets_num = Aktualizr_get_targets_num(u);
if (targets_num == 0) {
printf("Aktualizr_get_targets_num returned 0 targets\n");
Expand Down

0 comments on commit 9b5505d

Please sign in to comment.