blob: 68fd860d28fee28fce7ba2019085ea685816a5ce [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
Tomáš Pecka594a6762021-01-29 11:06:08 +01008#include "Firmware.h"
Tomáš Peckacb7a5f82021-01-20 15:12:00 +01009#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
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010023Firmware::Firmware(std::shared_ptr<::sysrepo::Connection> srConn, sdbus::IConnection& dbusConnectionSignals, sdbus::IConnection& dbusConnectionMethods)
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010024 : m_srConn(std::move(srConn))
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010025 , m_srSessionOps(std::make_shared<::sysrepo::Session>(m_srConn))
26 , m_srSessionRPC(std::make_shared<::sysrepo::Session>(m_srConn))
27 , m_srSubscribeOps(std::make_shared<::sysrepo::Subscribe>(m_srSessionOps))
28 , m_srSubscribeRPC(std::make_shared<::sysrepo::Subscribe>(m_srSessionRPC))
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010029 , m_rauc(std::make_shared<RAUC>(
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010030 dbusConnectionSignals,
31 dbusConnectionMethods,
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010032 [this](const std::string& operation) {
33 if (operation == "installing") {
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010034 std::lock_guard<std::mutex> lck(m_mtx);
35 m_installStatus = "in-progress";
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010036 }
37 },
Tomáš Pecka9a6e15b2021-01-25 18:57:48 +010038 [this](int32_t perc, const std::string& msg) {
39 std::map<std::string, std::string> data = {
40 {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/update/message", msg},
41 {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/update/progress", std::to_string(perc)},
42 };
43
44 libyang::S_Data_Node dataNode;
45 auto session = std::make_shared<::sysrepo::Session>(m_srConn);
46
47 utils::valuesToYang(data, session, dataNode);
48 session->event_notif_send(dataNode);
49 },
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010050 [this](int32_t retVal, const std::string& lastError) {
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010051 std::lock_guard<std::mutex> lck(m_mtx);
52 m_installStatus = retVal == 0 ? "succeeded" : "failed";
53 m_installMessage = lastError;
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010054 }))
55 , m_log(spdlog::get("system"))
56{
57 {
58 auto raucOperation = m_rauc->operation();
59 auto raucLastError = m_rauc->lastError();
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010060
61 std::lock_guard<std::mutex> lck(m_mtx);
62
63 m_installMessage = raucLastError;
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010064
65 if (raucOperation == "installing") {
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010066 m_installStatus = "in-progress";
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010067 } else if (!raucLastError.empty()) {
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010068 m_installStatus = "failed";
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010069 } else {
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010070 m_installStatus = "none";
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010071 }
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010072 }
73
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010074 m_srSubscribeRPC->rpc_subscribe(
Tomáš Peckacb7a5f82021-01-20 15:12:00 +010075 (CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/install").c_str(),
76 [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) {
77 try {
78 std::string source = input->val(0)->val_to_string();
79 m_rauc->install(source);
80 } catch (sdbus::Error& e) {
81 m_log->warn("RAUC install error: '{}'", e.what());
82 session->set_error(e.getMessage().c_str(), nullptr);
83 return SR_ERR_OPERATION_FAILED;
84 }
85 return SR_ERR_OK;
86 },
87 0,
88 SR_SUBSCR_CTX_REUSE);
Tomáš Peckaaf8f0632021-01-27 16:45:55 +010089
90 m_srSubscribeOps->oper_get_items_subscribe(
91 CZECHLIGHT_SYSTEM_MODULE_NAME.c_str(),
92 [this](::sysrepo::S_Session session, [[maybe_unused]] const char* module_name, [[maybe_unused]] const char* path, [[maybe_unused]] const char* request_xpath, [[maybe_unused]] uint32_t request_id, libyang::S_Data_Node& parent) {
93 std::map<std::string, std::string> data;
94 {
95 std::lock_guard<std::mutex> lck(m_mtx);
96 data = {
97 {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status", m_installStatus},
98 {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/message", m_installMessage},
99 };
100 }
101
102 utils::valuesToYang(data, session, parent);
103 return SR_ERR_OK;
104 },
105 (CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "*").c_str(),
106 SR_SUBSCR_PASSIVE | SR_SUBSCR_OPER_MERGE | SR_SUBSCR_CTX_REUSE);
Tomáš Peckacb7a5f82021-01-20 15:12:00 +0100107}
108}