Aktualizr
C++ SOTA Client
aktualizr_cycle_simple.cc
1 #include <gtest/gtest.h>
2 
3 #include <future>
4 #include <iostream>
5 #include <string>
6 #include <thread>
7 
8 #include "libaktualizr/aktualizr.h"
9 #include "libaktualizr/config.h"
10 #include "logging/logging.h"
11 #include "storage/sqlstorage.h"
12 
13 int updateOneCycle(const boost::filesystem::path &storage_dir, const std::string &server) {
14  Config conf;
15  conf.pacman.type = PACKAGE_MANAGER_NONE;
16  conf.pacman.fake_need_reboot = true;
17  conf.provision.device_id = "device_id";
18  conf.provision.ecu_registration_endpoint = server + "/director/ecus";
19  conf.tls.server = server;
20  conf.uptane.director_server = server + "/director";
21  conf.uptane.repo_server = server + "/repo";
22  conf.uptane.key_type = KeyType::kED25519;
23  conf.provision.server = server;
24  conf.provision.provision_path = "tests/test_data/cred.zip";
25  conf.provision.primary_ecu_serial = "CA:FE:A6:D2:84:9D";
26  conf.provision.primary_ecu_hardware_id = "primary_hw";
27  conf.storage.path = storage_dir;
28  conf.bootloader.reboot_sentinel_dir = storage_dir;
29  conf.postUpdateValues();
30  logger_set_threshold(boost::log::trivial::debug);
31 
32  {
33  Aktualizr aktualizr(conf);
34 
35  aktualizr.Initialize();
36 
37  result::UpdateCheck update_result = aktualizr.CheckUpdates().get();
38  if (update_result.status != result::UpdateStatus::kUpdatesAvailable) {
39  LOG_ERROR << "no update available";
40  return 0;
41  }
42 
43  result::Download download_result = aktualizr.Download(update_result.updates).get();
44  if (download_result.status != result::DownloadStatus::kSuccess) {
45  LOG_ERROR << "download failed";
46  return 1;
47  }
48 
49  result::Install install_result = aktualizr.Install(update_result.updates).get();
50  if (install_result.ecu_reports.size() != 1) {
51  LOG_ERROR << "install failed";
52  return 1;
53  }
54  if (install_result.ecu_reports[0].install_res.result_code.num_code != data::ResultCode::Numeric::kNeedCompletion) {
55  return 1;
56  }
57  }
58 
59  // do "reboot" and finalize
60  boost::filesystem::remove(conf.bootloader.reboot_sentinel_dir / conf.bootloader.reboot_sentinel_name);
61 
62  {
63  Aktualizr aktualizr(conf);
64 
65  aktualizr.Initialize();
66 
67  result::UpdateCheck update_result = aktualizr.CheckUpdates().get();
68  if (update_result.status != result::UpdateStatus::kNoUpdatesAvailable) {
69  LOG_ERROR << "finalize failed";
70  return 1;
71  }
72  }
73 
74  return 0;
75 }
76 
77 int main(int argc, char **argv) {
78  logger_init();
79 
80  if (argc != 3) {
81  std::cerr << "Error: " << argv[0] << " requires the path to the storage directory "
82  << "and url of uptane server\n";
83  return EXIT_FAILURE;
84  }
85  boost::filesystem::path storage_dir = argv[1];
86  std::string server = argv[2];
87 
88  return updateOneCycle(storage_dir, server);
89 }
result::UpdateCheck
Container for information about available updates.
Definition: results.h:37
Config
Configuration object for an aktualizr instance running on a Primary ECU.
Definition: config.h:208
Aktualizr
This class provides the main APIs necessary for launching and controlling libaktualizr.
Definition: aktualizr.h:24
result::Download
Container for information about downloading an update.
Definition: results.h:116
result::Install
Container for information about installing an update.
Definition: results.h:129