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; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 18 | const auto FIRMWARE_SLOTS = {"rootfs.0"s, "rootfs.1"s}; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 19 | |
| 20 | } |
| 21 | |
| 22 | namespace velia::system { |
| 23 | |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 24 | Firmware::Firmware(std::shared_ptr<::sysrepo::Connection> srConn, sdbus::IConnection& dbusConnectionSignals, sdbus::IConnection& dbusConnectionMethods) |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 25 | : m_rauc(std::make_shared<RAUC>( |
| 26 | dbusConnectionSignals, |
| 27 | dbusConnectionMethods, |
| 28 | [this](const std::string& operation) { |
| 29 | if (operation == "installing") { |
| 30 | std::lock_guard<std::mutex> lck(m_mtx); |
Tomáš Pecka | 0eefdf1 | 2021-02-17 19:28:13 +0100 | [diff] [blame] | 31 | m_installMessage = ""; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 32 | m_installStatus = "in-progress"; |
| 33 | } |
| 34 | }, |
| 35 | [this](int32_t perc, const std::string& msg) { |
| 36 | std::map<std::string, std::string> data = { |
| 37 | {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/update/message", msg}, |
| 38 | {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/update/progress", std::to_string(perc)}, |
| 39 | }; |
| 40 | |
| 41 | libyang::S_Data_Node dataNode; |
| 42 | auto session = std::make_shared<::sysrepo::Session>(m_srConn); |
| 43 | |
| 44 | utils::valuesToYang(data, session, dataNode); |
| 45 | session->event_notif_send(dataNode); |
| 46 | }, |
| 47 | [this](int32_t retVal, const std::string& lastError) { |
| 48 | auto lock = updateSlotStatus(); |
| 49 | m_installStatus = retVal == 0 ? "succeeded" : "failed"; |
| 50 | m_installMessage = lastError; |
| 51 | })) |
| 52 | , m_log(spdlog::get("system")) |
| 53 | , m_srConn(std::move(srConn)) |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 54 | , m_srSessionOps(std::make_shared<::sysrepo::Session>(m_srConn)) |
| 55 | , m_srSessionRPC(std::make_shared<::sysrepo::Session>(m_srConn)) |
| 56 | , m_srSubscribeOps(std::make_shared<::sysrepo::Subscribe>(m_srSessionOps)) |
| 57 | , m_srSubscribeRPC(std::make_shared<::sysrepo::Subscribe>(m_srSessionRPC)) |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 58 | { |
| 59 | { |
| 60 | auto raucOperation = m_rauc->operation(); |
| 61 | auto raucLastError = m_rauc->lastError(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 62 | |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 63 | auto lock = updateSlotStatus(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 64 | |
| 65 | m_installMessage = raucLastError; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 66 | |
| 67 | if (raucOperation == "installing") { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 68 | m_installStatus = "in-progress"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 69 | } else if (!raucLastError.empty()) { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 70 | m_installStatus = "failed"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 71 | } else { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 72 | m_installStatus = "none"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 73 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 74 | } |
| 75 | |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 76 | m_srSubscribeRPC->rpc_subscribe( |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 77 | (CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/install").c_str(), |
Jan Kundrát | ef2b380 | 2021-02-18 09:57:05 +0100 | [diff] [blame^] | 78 | [this](auto session, auto, auto input, auto, auto, [[maybe_unused]] auto output) { |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 79 | auto lock = updateSlotStatus(); |
| 80 | |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 81 | try { |
| 82 | std::string source = input->val(0)->val_to_string(); |
| 83 | m_rauc->install(source); |
| 84 | } catch (sdbus::Error& e) { |
| 85 | m_log->warn("RAUC install error: '{}'", e.what()); |
| 86 | session->set_error(e.getMessage().c_str(), nullptr); |
| 87 | return SR_ERR_OPERATION_FAILED; |
| 88 | } |
| 89 | return SR_ERR_OK; |
| 90 | }, |
| 91 | 0, |
| 92 | SR_SUBSCR_CTX_REUSE); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 93 | |
| 94 | m_srSubscribeOps->oper_get_items_subscribe( |
| 95 | CZECHLIGHT_SYSTEM_MODULE_NAME.c_str(), |
Jan Kundrát | ef2b380 | 2021-02-18 09:57:05 +0100 | [diff] [blame^] | 96 | [this](auto session, auto, auto, auto, auto, auto& parent) { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 97 | std::map<std::string, std::string> data; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 98 | |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 99 | { |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 100 | auto lock = updateSlotStatus(); |
| 101 | |
| 102 | data.insert(m_slotStatusCache.begin(), m_slotStatusCache.end()); |
| 103 | data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status"] = m_installStatus; |
| 104 | data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/message"] = m_installMessage; |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | utils::valuesToYang(data, session, parent); |
| 108 | return SR_ERR_OK; |
| 109 | }, |
| 110 | (CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "*").c_str(), |
| 111 | SR_SUBSCR_PASSIVE | SR_SUBSCR_OPER_MERGE | SR_SUBSCR_CTX_REUSE); |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 112 | } |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 113 | |
| 114 | /** @brief Updates the slot status cache with the new data obtained via RAUC |
| 115 | * |
| 116 | * Gets current slot status data from RAUC and updates the local slot status cache if new data are available. |
| 117 | * The methods manipulates with the local cache which is shared among multiple thread. |
| 118 | * |
| 119 | * @return an unique_lock (in locked state) that can be further used to manipulate with the local cache |
| 120 | * */ |
| 121 | std::unique_lock<std::mutex> Firmware::updateSlotStatus() |
| 122 | { |
| 123 | std::map<std::string, velia::system::RAUC::SlotProperties> slotStatus; |
| 124 | |
| 125 | try { |
| 126 | slotStatus = m_rauc->slotStatus(); |
| 127 | } catch (sdbus::Error& e) { |
| 128 | m_log->warn("Could not fetch RAUC slot status data: {}", e.getMessage()); |
| 129 | } |
| 130 | |
| 131 | std::unique_lock<std::mutex> lck(m_mtx); |
| 132 | |
| 133 | for (const auto& slotName : FIRMWARE_SLOTS) { |
| 134 | if (auto it = slotStatus.find(slotName); it != slotStatus.end()) { // if there is an update for the slot "slotName" |
| 135 | const auto& props = it->second; |
| 136 | auto xpathPrefix = CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot[name='" + std::get<std::string>(props.at("bootname")) + "']/"; |
| 137 | |
| 138 | m_slotStatusCache[xpathPrefix + "state"] = std::get<std::string>(props.at("state")); |
| 139 | m_slotStatusCache[xpathPrefix + "version"] = std::get<std::string>(props.at("bundle.version")); |
| 140 | m_slotStatusCache[xpathPrefix + "installed"] = std::get<std::string>(props.at("installed.timestamp")); |
| 141 | m_slotStatusCache[xpathPrefix + "boot-status"] = std::get<std::string>(props.at("boot-status")); |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return lck; |
| 146 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 147 | } |