Aktualizr
C++ SOTA Client
opcuasecondary.cc
1 #include "opcuasecondary.h"
2 
3 #include "opcuabridge/opcuabridgeclient.h"
4 #include "secondaryconfig.h"
5 
6 #include "logging/logging.h"
7 #include "package_manager/ostreereposync.h"
8 #include "utilities/utils.h"
9 
10 #include <algorithm>
11 #include <iterator>
12 
13 #include <boost/filesystem.hpp>
14 #include <boost/iostreams/device/mapped_file.hpp>
15 
16 namespace fs = boost::filesystem;
17 
18 namespace Uptane {
19 
20 OpcuaSecondary::OpcuaSecondary(const SecondaryConfig& sconfig_in) : SecondaryInterface(sconfig_in) {}
21 
22 OpcuaSecondary::~OpcuaSecondary() = default;
23 
24 Uptane::EcuSerial OpcuaSecondary::getSerial() {
25  opcuabridge::Client client{opcuabridge::SelectEndPoint(SecondaryInterface::sconfig)};
26  return client.recvConfiguration().getSerial();
27 }
28 Uptane::HardwareIdentifier OpcuaSecondary::getHwId() {
29  opcuabridge::Client client{opcuabridge::SelectEndPoint(SecondaryInterface::sconfig)};
30  return Uptane::HardwareIdentifier(client.recvConfiguration().getHwId());
31 }
32 PublicKey OpcuaSecondary::getPublicKey() {
33  opcuabridge::Client client{opcuabridge::SelectEndPoint(SecondaryInterface::sconfig)};
34  return PublicKey(client.recvConfiguration().getPublicKey(), client.recvConfiguration().getPublicKeyType());
35 }
36 
37 Json::Value OpcuaSecondary::getManifest() {
38  opcuabridge::Client client{opcuabridge::SelectEndPoint(SecondaryInterface::sconfig)};
39  auto original_manifest = client.recvOriginalManifest().getBlock();
40  return Utils::parseJSON(std::string(original_manifest.begin(), original_manifest.end()));
41 }
42 
43 bool OpcuaSecondary::putMetadata(const RawMetaPack& meta_pack) {
44  std::vector<opcuabridge::MetadataFile> metadatafiles;
45  {
47  mf.setMetadata(meta_pack.director_root);
48  metadatafiles.push_back(mf);
49  }
50  {
52  mf.setMetadata(meta_pack.director_targets);
53  metadatafiles.push_back(mf);
54  }
55  opcuabridge::Client client{opcuabridge::SelectEndPoint(SecondaryInterface::sconfig)};
56  return client.sendMetadataFiles(metadatafiles);
57 }
58 
59 bool OpcuaSecondary::sendFirmwareAsync(const std::shared_ptr<std::string>& data) {
60  sendEvent(std::make_shared<event::InstallStarted>(getSerial()));
61 
62  Json::Value data_json = Utils::parseJSON(*data);
63 
64  const fs::path source_repo_dir_path(ostree_repo_sync::GetOstreeRepoPath(data_json["sysroot_path"].asString()));
65 
66  opcuabridge::Client client{opcuabridge::SelectEndPoint(SecondaryInterface::sconfig)};
67  if (!client) {
68  sendEvent(std::make_shared<event::InstallComplete>(getSerial()));
69  return false;
70  }
71 
72  bool retval = true;
73  if (ostree_repo_sync::ArchiveModeRepo(source_repo_dir_path)) {
74  retval = client.syncDirectoryFiles(source_repo_dir_path);
75  } else {
76  TemporaryDirectory temp_dir("opcuabridge-ostree-sync-working-repo");
77  const fs::path working_repo_dir_path = temp_dir.Path();
78 
79  if (!ostree_repo_sync::LocalPullRepo(source_repo_dir_path, working_repo_dir_path,
80  data_json["ref_hash"].asString())) {
81  LOG_ERROR << "OSTree repo sync failed: unable to local pull from " << source_repo_dir_path.native();
82  sendEvent(std::make_shared<event::InstallComplete>(getSerial()));
83  return false;
84  }
85  retval = client.syncDirectoryFiles(working_repo_dir_path);
86  }
87  sendEvent(std::make_shared<event::InstallComplete>(getSerial()));
88  return retval;
89 }
90 
91 int OpcuaSecondary::getRootVersion(bool /* director */) {
92  LOG_ERROR << "OpcuaSecondary::getRootVersion is not implemented yet";
93  return 0;
94 }
95 
96 bool OpcuaSecondary::putRoot(const std::string& /* root */, bool /* director */) {
97  LOG_ERROR << "OpcuaSecondary::putRoot is not implemented yet";
98  return false;
99 }
100 
101 } // namespace Uptane
General data structures.
Definition: types.cc:6
Base data types that are used in The Update Framework (TUF), part of UPTANE.