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 | |
Václav Kubernát | b7f2635 | 2021-11-26 14:58:33 +0100 | [diff] [blame] | 8 | #include <regex> |
Tomáš Pecka | 594a676 | 2021-01-29 11:06:08 +0100 | [diff] [blame] | 9 | #include "Firmware.h" |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 10 | #include "utils/log.h" |
| 11 | #include "utils/sysrepo.h" |
| 12 | |
| 13 | using namespace std::literals; |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | const auto CZECHLIGHT_SYSTEM_MODULE_NAME = "czechlight-system"s; |
| 18 | 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] | 19 | const auto FIRMWARE_SLOTS = {"rootfs.0"s, "rootfs.1"s}; |
Václav Kubernát | b7f2635 | 2021-11-26 14:58:33 +0100 | [diff] [blame] | 20 | // Modified regex of yang:date-and-time |
| 21 | const auto DATE_TIME_REGEX = std::regex{R"(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}Z)"}; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 22 | } |
| 23 | |
| 24 | namespace velia::system { |
| 25 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 26 | Firmware::Firmware(::sysrepo::Connection srConn, sdbus::IConnection& dbusConnectionSignals, sdbus::IConnection& dbusConnectionMethods) |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 27 | : m_rauc(std::make_shared<RAUC>( |
| 28 | dbusConnectionSignals, |
| 29 | dbusConnectionMethods, |
| 30 | [this](const std::string& operation) { |
| 31 | if (operation == "installing") { |
| 32 | std::lock_guard<std::mutex> lck(m_mtx); |
Tomáš Pecka | 0eefdf1 | 2021-02-17 19:28:13 +0100 | [diff] [blame] | 33 | m_installMessage = ""; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 34 | m_installStatus = "in-progress"; |
| 35 | } |
| 36 | }, |
| 37 | [this](int32_t perc, const std::string& msg) { |
| 38 | std::map<std::string, std::string> data = { |
| 39 | {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/update/message", msg}, |
| 40 | {CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/update/progress", std::to_string(perc)}, |
| 41 | }; |
| 42 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 43 | std::optional<libyang::DataNode> dataNode; |
| 44 | auto session = m_srConn.sessionStart(); |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 45 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 46 | utils::valuesToYang(data, {}, session, dataNode); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 47 | session.sendNotification(*dataNode, sysrepo::Wait::No); // No need to wait, it's just a notification. |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 48 | }, |
| 49 | [this](int32_t retVal, const std::string& lastError) { |
| 50 | auto lock = updateSlotStatus(); |
| 51 | m_installStatus = retVal == 0 ? "succeeded" : "failed"; |
| 52 | m_installMessage = lastError; |
| 53 | })) |
| 54 | , m_log(spdlog::get("system")) |
| 55 | , m_srConn(std::move(srConn)) |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 56 | , m_srSessionOps(m_srConn.sessionStart()) |
| 57 | , m_srSessionRPC(m_srConn.sessionStart()) |
| 58 | , m_srSubscribeOps() |
| 59 | , m_srSubscribeRPC() |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 60 | { |
Tomáš Pecka | b78372f | 2021-04-22 11:09:20 +0200 | [diff] [blame] | 61 | utils::ensureModuleImplemented(m_srSessionOps, "czechlight-system", "2021-01-13"); |
| 62 | |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 63 | { |
| 64 | auto raucOperation = m_rauc->operation(); |
| 65 | auto raucLastError = m_rauc->lastError(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 66 | |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 67 | auto lock = updateSlotStatus(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 68 | |
| 69 | m_installMessage = raucLastError; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 70 | |
| 71 | if (raucOperation == "installing") { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 72 | m_installStatus = "in-progress"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 73 | } else if (!raucLastError.empty()) { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 74 | m_installStatus = "failed"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 75 | } else { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 76 | m_installStatus = "none"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 77 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 78 | } |
| 79 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 80 | ::sysrepo::RpcActionCb cbRPC = [this](auto session, auto, auto, auto input, auto, auto, auto) { |
| 81 | auto lock = updateSlotStatus(); |
| 82 | |
| 83 | try { |
| 84 | auto source = std::string{input.findPath("url")->asTerm().valueStr()}; |
| 85 | m_rauc->install(source); |
| 86 | } catch (sdbus::Error& e) { |
| 87 | m_log->warn("RAUC install error: '{}'", e.what()); |
| 88 | utils::setErrors(session, e.getMessage()); |
| 89 | return ::sysrepo::ErrorCode::OperationFailed; |
| 90 | } |
| 91 | return ::sysrepo::ErrorCode::Ok; |
| 92 | }; |
| 93 | |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame^] | 94 | m_srSubscribeRPC = m_srSessionRPC.onRPCAction(CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/install", cbRPC); |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 95 | |
| 96 | ::sysrepo::OperGetCb cbOper = [this](auto session, auto, auto, auto, auto, auto, auto& parent) { |
| 97 | std::map<std::string, std::string> data; |
| 98 | |
| 99 | { |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 100 | auto lock = updateSlotStatus(); |
| 101 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 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; |
| 105 | } |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 106 | |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 107 | utils::valuesToYang(data, {}, session, parent); |
| 108 | return ::sysrepo::ErrorCode::Ok; |
| 109 | }; |
| 110 | |
| 111 | m_srSubscribeOps = m_srSessionOps.onOperGet( |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame^] | 112 | CZECHLIGHT_SYSTEM_MODULE_NAME, |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 113 | cbOper, |
Jan Kundrát | b3e9998 | 2022-03-18 17:38:20 +0100 | [diff] [blame^] | 114 | CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "*", |
Václav Kubernát | 7efd6d5 | 2021-11-09 01:31:11 +0100 | [diff] [blame] | 115 | ::sysrepo::SubscribeOptions::Passive | ::sysrepo::SubscribeOptions::OperMerge); |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 116 | } |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 117 | |
| 118 | /** @brief Updates the slot status cache with the new data obtained via RAUC |
| 119 | * |
| 120 | * Gets current slot status data from RAUC and updates the local slot status cache if new data are available. |
| 121 | * The methods manipulates with the local cache which is shared among multiple thread. |
| 122 | * |
| 123 | * @return an unique_lock (in locked state) that can be further used to manipulate with the local cache |
| 124 | * */ |
| 125 | std::unique_lock<std::mutex> Firmware::updateSlotStatus() |
| 126 | { |
| 127 | std::map<std::string, velia::system::RAUC::SlotProperties> slotStatus; |
| 128 | |
| 129 | try { |
| 130 | slotStatus = m_rauc->slotStatus(); |
| 131 | } catch (sdbus::Error& e) { |
| 132 | m_log->warn("Could not fetch RAUC slot status data: {}", e.getMessage()); |
| 133 | } |
| 134 | |
| 135 | std::unique_lock<std::mutex> lck(m_mtx); |
| 136 | |
| 137 | for (const auto& slotName : FIRMWARE_SLOTS) { |
| 138 | if (auto it = slotStatus.find(slotName); it != slotStatus.end()) { // if there is an update for the slot "slotName" |
Václav Kubernát | b7f2635 | 2021-11-26 14:58:33 +0100 | [diff] [blame] | 139 | auto& props = it->second; |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 140 | std::string xpathPrefix; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 141 | |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 142 | // Better be defensive about provided properties. If somebody removes /slot.raucs, RAUC doesn't provide all the data (at least bundle.version and installed.timestamp). |
| 143 | if (auto pit = props.find("bootname"); pit != props.end()) { |
| 144 | xpathPrefix = CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot[name='" + std::get<std::string>(pit->second) + "']/"; |
| 145 | } else { |
| 146 | m_log->error("RAUC didn't provide 'bootname' property for slot '{}'. Skipping update for that slot."); |
| 147 | continue; |
| 148 | } |
| 149 | |
Václav Kubernát | b7f2635 | 2021-11-26 14:58:33 +0100 | [diff] [blame] | 150 | // sysrepo needs the "-00:00" suffix instead of the "Z" suffix. |
| 151 | if (auto pit = props.find("installed.timestamp"); pit != props.end()) { |
| 152 | auto& ts = std::get<std::string>(pit->second); |
| 153 | if (std::regex_match(ts, DATE_TIME_REGEX)) { |
| 154 | ts.pop_back(); // Get rid of the "Z". |
| 155 | ts.append("-00:00"); |
| 156 | } else { |
| 157 | m_log->warn("RAUC provided a timestamp in an unexpected format: {}", ts); |
| 158 | } |
| 159 | } |
| 160 | |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 161 | for (const auto& [yangKey, raucKey] : {std::pair{"state", "state"}, {"boot-status", "boot-status"}, {"version", "bundle.version"}, {"installed", "installed.timestamp"}}) { |
| 162 | if (auto pit = props.find(raucKey); pit != props.end()) { |
| 163 | m_slotStatusCache[xpathPrefix + yangKey] = std::get<std::string>(pit->second); |
| 164 | } else { |
| 165 | m_log->warn("RAUC didn't provide '{}' property for slot '{}'.", raucKey, slotName); |
| 166 | } |
| 167 | } |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | |
| 171 | return lck; |
| 172 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 173 | } |