Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
main.cc
1 #include <boost/filesystem.hpp>
2 #include <boost/program_options.hpp>
3 #include <iostream>
4 #include <memory>
5 #include <thread>
6 
7 #include "aktualizr_secondary.h"
8 #include "aktualizr_secondary_config.h"
9 #include "aktualizr_secondary_factory.h"
10 #include "utilities/aktualizr_version.h"
11 #include "utilities/utils.h"
12 
13 #include "logging/logging.h"
14 #include "secondary_tcp_server.h"
15 
16 namespace bpo = boost::program_options;
17 
18 void check_secondary_options(const bpo::options_description &description, const bpo::variables_map &vm) {
19  if (vm.count("help") != 0) {
20  std::cout << description << '\n';
21  exit(EXIT_SUCCESS);
22  }
23  if (vm.count("version") != 0) {
24  std::cout << "Current aktualizr-secondary version is: " << aktualizr_version() << "\n";
25  exit(EXIT_SUCCESS);
26  }
27 }
28 
29 bpo::variables_map parse_options(int argc, char *argv[]) {
30  bpo::options_description description("aktualizr-secondary command line options");
31  // clang-format off
32  description.add_options()
33  ("help,h", "print usage")
34  ("version,v", "Current aktualizr-secondary version")
35  ("loglevel", bpo::value<int>(), "set log level 0-5 (trace, debug, info, warning, error, fatal)")
36  ("config,c", bpo::value<std::vector<boost::filesystem::path> >()->composing(), "configuration file or directory")
37  ("server-port,p", bpo::value<int>(), "command server listening port")
38  ("ecu-serial", bpo::value<std::string>(), "serial number of Secondary ECU")
39  ("ecu-hardware-id", bpo::value<std::string>(), "hardware ID of Secondary ECU");
40  // clang-format on
41 
42  bpo::variables_map vm;
43  std::vector<std::string> unregistered_options;
44  try {
45  bpo::basic_parsed_options<char> parsed_options =
46  bpo::command_line_parser(argc, argv).options(description).allow_unregistered().run();
47  bpo::store(parsed_options, vm);
48  check_secondary_options(description, vm);
49  bpo::notify(vm);
50  unregistered_options = bpo::collect_unrecognized(parsed_options.options, bpo::include_positional);
51  if (vm.count("help") == 0 && !unregistered_options.empty()) {
52  std::cout << description << "\n";
53  exit(EXIT_FAILURE);
54  }
55  } catch (const bpo::required_option &ex) {
56  // print the error and append the default commandline option description
57  std::cout << ex.what() << std::endl << description;
58  exit(EXIT_FAILURE);
59  } catch (const bpo::error &ex) {
60  check_secondary_options(description, vm);
61 
62  // log boost error
63  LOG_WARNING << "boost command line option error: " << ex.what();
64 
65  // print the error message to the standard output too, as the user provided
66  // a non-supported commandline option
67  std::cout << ex.what() << '\n';
68 
69  // set the returnValue, thereby ctest will recognize
70  // that something went wrong
71  exit(EXIT_FAILURE);
72  }
73 
74  return vm;
75 }
76 
77 /*****************************************************************************/
78 int main(int argc, char *argv[]) {
79  logger_init();
80  logger_set_threshold(boost::log::trivial::info);
81  LOG_INFO << "aktualizr-secondary version " << aktualizr_version() << " starting";
82 
83  bpo::variables_map commandline_map = parse_options(argc, argv);
84 
85  int ret = EXIT_SUCCESS;
86  try {
87  AktualizrSecondaryConfig config(commandline_map);
88  LOG_DEBUG << "Current directory: " << boost::filesystem::current_path().string();
89 
90  auto secondary = AktualizrSecondaryFactory::create(config);
91  SecondaryTcpServer tcp_server(secondary->getDispatcher(), config.network.primary_ip, config.network.primary_port,
92  config.network.port, config.uptane.force_install_completion);
93 
94  tcp_server.run();
95 
96  if (tcp_server.exit_reason() == SecondaryTcpServer::ExitReason::kRebootNeeded) {
97  secondary->completeInstall();
98  }
99 
100  } catch (std::runtime_error &exc) {
101  LOG_ERROR << "Error: " << exc.what();
102  ret = EXIT_FAILURE;
103  }
104  return ret;
105 }
AktualizrSecondaryConfig
Definition: aktualizr_secondary_config.h:40
SecondaryTcpServer
Listens on a socket, decodes calls (ASN.1) and forwards them to an Uptane Secondary implementation.
Definition: secondary_tcp_server.h:16
SecondaryTcpServer::run
void run()
Accept connections on the socket, decode requests and respond using the secondary implementation.
Definition: secondary_tcp_server.cc:32