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

Commit

Permalink
C API: UptaneCycle and campaigns
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Smirnov <evgenii.smirnov@here.com>
  • Loading branch information
Eugene Smirnov committed Jul 26, 2019
1 parent 8c8877f commit c9e90c1
Show file tree
Hide file tree
Showing 6 changed files with 175 additions and 0 deletions.
30 changes: 30 additions & 0 deletions include/libaktualizr-c.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef AKTUALIZR_LIBAKTUALIZRC_H
#define AKTUALIZR_LIBAKTUALIZRC_H

#ifdef __cplusplus
#include "primary/aktualizr.h"

using Campaign = campaign::Campaign;

extern "C" {
#else
typedef struct Aktualizr Aktualizr;
typedef struct Campaign Campaign;
#endif

Aktualizr *Aktualizr_create(const char *config_path);
int Aktualizr_initialize(Aktualizr *a);
int Aktualizr_uptane_cycle(Aktualizr *a);
void Aktualizr_destroy(Aktualizr *a);

Campaign *Aktualizr_campaign_check(Aktualizr *a);
int Aktualizr_campaign_accept(Aktualizr *a, Campaign *c);
int Aktualizr_campaign_postpone(Aktualizr *a, Campaign *c);
int Aktualizr_campaign_decline(Aktualizr *a, Campaign *c);
void Aktualizr_campaign_free(Campaign *c);

#ifdef __cplusplus
}
#endif

#endif //AKTUALIZR_LIBAKTUALIZR_H
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ if(BUILD_WITH_CODE_COVERAGE)
endif()

add_subdirectory("libaktualizr")
add_subdirectory("libaktualizr-c")
add_subdirectory("virtual_secondary")
add_subdirectory("sota_tools")
add_subdirectory("aktualizr_primary")
Expand Down
10 changes: 10 additions & 0 deletions src/libaktualizr-c/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
SET(TARGET_NAME aktualizr-c)
SET(SOURCES libaktualizr-c.cc)

add_library(${TARGET_NAME} SHARED ${SOURCES})
target_include_directories(${TARGET_NAME} PUBLIC ${PROJECT_SOURCE_DIR}/include)
target_link_libraries(${TARGET_NAME} PRIVATE aktualizr_static_lib ${AKTUALIZR_EXTERNAL_LIBS})

aktualizr_source_file_checks(${SOURCES})

add_subdirectory(test)
81 changes: 81 additions & 0 deletions src/libaktualizr-c/libaktualizr-c.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include "libaktualizr-c.h"

Aktualizr *Aktualizr_create(const char *config_path) {
Aktualizr *a;
try {
Config cfg(config_path);
a = new Aktualizr(cfg);
} catch (const std::exception &e) {
std::cerr << "Aktualizr_create exception: " << e.what() << std::endl;
return NULL;
}
return a;
}

int Aktualizr_initialize(Aktualizr *a) {
try {
a->Initialize();
} catch (const std::exception &e) {
std::cerr << "Aktualizr_initialize exception: " << e.what() << std::endl;
return -1;
}
return 0;
}

int Aktualizr_uptane_cycle(Aktualizr *a) {
try {
a->UptaneCycle();
} catch (const std::exception &e) {
std::cerr << "Uptane cycle exception: " << e.what() << std::endl;
return -1;
}
return 0;
}

void Aktualizr_destroy(Aktualizr *a) {
delete a;
}

Campaign *Aktualizr_campaign_check(Aktualizr *a) {
try {
auto r = a->CampaignCheck().get();
if (!r.campaigns.empty()) {
// We don't support multiple campaigns at the moment
return new Campaign(r.campaigns[0]);
}
} catch (const std::exception &e) {
std::cerr << "Campaign check exception: " << e.what() << std::endl;
return NULL;
}
return NULL;
}
int Aktualizr_campaign_accept(Aktualizr *a, Campaign *c) {
try {
a->CampaignControl(c->id, campaign::Cmd::Accept).get();
} catch (const std::exception &e) {
std::cerr << "Campaign accept exception: " << e.what() << std::endl;
return -1;
}
return 0;
}
int Aktualizr_campaign_postpone(Aktualizr *a, Campaign *c) {
try {
a->CampaignControl(c->id, campaign::Cmd::Postpone).get();
} catch (const std::exception &e) {
std::cerr << "Campaign postpone exception: " << e.what() << std::endl;
return -1;
}
return 0;
}
int Aktualizr_campaign_decline(Aktualizr *a, Campaign *c) {
try {
a->CampaignControl(c->id, campaign::Cmd::Decline).get();
} catch (const std::exception &e) {
std::cerr << "Campaign decline exception: " << e.what() << std::endl;
return -1;
}
return 0;
}
void Aktualizr_campaign_free(Campaign *c) {
delete c;
}
8 changes: 8 additions & 0 deletions src/libaktualizr-c/test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
SET(TARGET_NAME api-test)
SET(SOURCES api-test.c)

add_executable(${TARGET_NAME} EXCLUDE_FROM_ALL ${SOURCES})
add_dependencies(build_tests ${TEST_TARGET})
target_link_libraries(${TARGET_NAME} aktualizr-c)

aktualizr_source_file_checks(${SOURCES})
45 changes: 45 additions & 0 deletions src/libaktualizr-c/test/api-test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include <stdio.h>
#include <stdlib.h>

#include "libaktualizr-c.h"

int main(int argc, char **argv)
{
Aktualizr *a;
Campaign *c;
int err;

if (argc != 2) {
fprintf(stderr, "Missing config file\nUsage:\n\t%s CONFIG_FILE\n", argv[0]);
return EXIT_FAILURE;
}

a = Aktualizr_create(argv[1]);
if (a == NULL) {
return EXIT_FAILURE;
}

err = Aktualizr_initialize(a);
if (err) {
return EXIT_FAILURE;
}

c = Aktualizr_campaign_check(a);
if (c != NULL) {
printf("Accepting running campaign\n");
err = Aktualizr_campaign_accept(a, c);
Aktualizr_campaign_free(c);
if (err) {
return EXIT_FAILURE;
}
}

err = Aktualizr_uptane_cycle(a);
if (err) {
return EXIT_FAILURE;
}

Aktualizr_destroy(a);

return EXIT_SUCCESS;
}

0 comments on commit c9e90c1

Please sign in to comment.