Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@cesnet.cz> |
| 5 | * |
| 6 | */ |
| 7 | |
Tomáš Pecka | 594a676 | 2021-01-29 11:06:08 +0100 | [diff] [blame] | 8 | #include "Firmware.h" |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 9 | #include "utils/log.h" |
| 10 | #include "utils/sysrepo.h" |
| 11 | |
| 12 | using namespace std::literals; |
| 13 | |
| 14 | namespace { |
| 15 | |
| 16 | const auto CZECHLIGHT_SYSTEM_MODULE_NAME = "czechlight-system"s; |
| 17 | const auto CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX = "/"s + CZECHLIGHT_SYSTEM_MODULE_NAME + ":firmware/"s; |
| 18 | |
| 19 | } |
| 20 | |
| 21 | namespace velia::system { |
| 22 | |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame^] | 23 | Firmware::Firmware(std::shared_ptr<::sysrepo::Connection> srConn, sdbus::IConnection& dbusConnectionSignals, sdbus::IConnection& dbusConnectionMethods) |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 24 | : m_srConn(std::move(srConn)) |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 25 | , 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áš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 29 | , m_rauc(std::make_shared<RAUC>( |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame^] | 30 | dbusConnectionSignals, |
| 31 | dbusConnectionMethods, |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 32 | [this](const std::string& operation) { |
| 33 | if (operation == "installing") { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 34 | std::lock_guard<std::mutex> lck(m_mtx); |
| 35 | m_installStatus = "in-progress"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 36 | } |
| 37 | }, |
Tomáš Pecka | 9a6e15b | 2021-01-25 18:57:48 +0100 | [diff] [blame] | 38 | [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áš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 50 | [this](int32_t retVal, const std::string& lastError) { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 51 | std::lock_guard<std::mutex> lck(m_mtx); |
| 52 | m_installStatus = retVal == 0 ? "succeeded" : "failed"; |
| 53 | m_installMessage = lastError; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 54 | })) |
| 55 | , m_log(spdlog::get("system")) |
| 56 | { |
| 57 | { |
| 58 | auto raucOperation = m_rauc->operation(); |
| 59 | auto raucLastError = m_rauc->lastError(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 60 | |
| 61 | std::lock_guard<std::mutex> lck(m_mtx); |
| 62 | |
| 63 | m_installMessage = raucLastError; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 64 | |
| 65 | if (raucOperation == "installing") { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 66 | m_installStatus = "in-progress"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 67 | } else if (!raucLastError.empty()) { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 68 | m_installStatus = "failed"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 69 | } else { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 70 | m_installStatus = "none"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 71 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 72 | } |
| 73 | |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 74 | m_srSubscribeRPC->rpc_subscribe( |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 75 | (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áš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 89 | |
| 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áš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 107 | } |
| 108 | } |