blob: 49479f2340021329a5bc62275f27be5389d0adbe [file] [log] [blame]
Tomáš Peckacb7a5f82021-01-20 15:12:00 +01001/*
2 * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/
3 *
4 * Written by Tomáš Pecka <tomas.pecka@cesnet.cz>
5 *
6 */
7
8#include "CzechlightSystem.h"
9#include "utils/log.h"
10#include "utils/sysrepo.h"
11
12using namespace std::literals;
13
14namespace {
15
16const auto CZECHLIGHT_SYSTEM_MODULE_NAME = "czechlight-system"s;
17const auto CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX = "/"s + CZECHLIGHT_SYSTEM_MODULE_NAME + ":firmware/"s;
18
19}
20
21namespace velia::system {
22
23CzechlightSystem::CzechlightSystem(std::shared_ptr<::sysrepo::Connection> srConn, sdbus::IConnection& dbusConnection)
24 : m_srConn(std::move(srConn))
25 , m_srSession(std::make_shared<::sysrepo::Session>(m_srConn))
26 , m_srSubscribe(std::make_shared<::sysrepo::Subscribe>(m_srSession))
27 , m_rauc(std::make_shared<RAUC>(
28 dbusConnection,
29 [this](const std::string& operation) {
30 if (operation == "installing") {
31 std::map<std::string, std::string> data {
32 {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status", "in-progress"},
33 };
34
35 utils::valuesPush(data, std::make_shared<::sysrepo::Session>(m_srConn, SR_DS_OPERATIONAL));
36 }
37 },
38 []([[maybe_unused]] int32_t perc, [[maybe_unused]] const std::string& msg) { /* TODO notifications */ },
39 [this](int32_t retVal, const std::string& lastError) {
40 std::map<std::string, std::string> data {
41 {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/message", lastError},
42 {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status", retVal == 0 ? "succeeded" : "failed"},
43 };
44
45 utils::valuesPush(data, std::make_shared<::sysrepo::Session>(m_srConn, SR_DS_OPERATIONAL));
46 }))
47 , m_log(spdlog::get("system"))
48{
49 {
50 auto raucOperation = m_rauc->operation();
51 auto raucLastError = m_rauc->lastError();
52 std::map<std::string, std::string> data {
53 {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/message", raucLastError},
54 };
55
56 if (raucOperation == "installing") {
57 data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status"] = "in-progress";
58 } else if (!raucLastError.empty()) {
59 data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status"] = "failed";
60 } else {
61 data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status"] = "none";
62 }
63
64 utils::valuesPush(data, m_srSession, SR_DS_OPERATIONAL);
65 }
66
67 m_srSubscribe->rpc_subscribe(
68 (CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/install").c_str(),
69 [this](::sysrepo::S_Session session, [[maybe_unused]] const char* op_path, const ::sysrepo::S_Vals input, [[maybe_unused]] sr_event_t event, [[maybe_unused]] uint32_t request_id, [[maybe_unused]] ::sysrepo::S_Vals_Holder output) {
70 try {
71 std::string source = input->val(0)->val_to_string();
72 m_rauc->install(source);
73 } catch (sdbus::Error& e) {
74 m_log->warn("RAUC install error: '{}'", e.what());
75 session->set_error(e.getMessage().c_str(), nullptr);
76 return SR_ERR_OPERATION_FAILED;
77 }
78 return SR_ERR_OK;
79 },
80 0,
81 SR_SUBSCR_CTX_REUSE);
82}
83}