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 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 44 | utils::valuesToYang(data, {}, session, dataNode); |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 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 | { |
Tomáš Pecka | b78372f | 2021-04-22 11:09:20 +0200 | [diff] [blame^] | 59 | utils::ensureModuleImplemented(m_srSessionOps, "czechlight-system", "2021-01-13"); |
| 60 | |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 61 | { |
| 62 | auto raucOperation = m_rauc->operation(); |
| 63 | auto raucLastError = m_rauc->lastError(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 64 | |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 65 | auto lock = updateSlotStatus(); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 66 | |
| 67 | m_installMessage = raucLastError; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 68 | |
| 69 | if (raucOperation == "installing") { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 70 | m_installStatus = "in-progress"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 71 | } else if (!raucLastError.empty()) { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 72 | m_installStatus = "failed"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 73 | } else { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 74 | m_installStatus = "none"; |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 75 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 76 | } |
| 77 | |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 78 | m_srSubscribeRPC->rpc_subscribe( |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 79 | (CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/install").c_str(), |
Jan Kundrát | ef2b380 | 2021-02-18 09:57:05 +0100 | [diff] [blame] | 80 | [this](auto session, auto, auto input, auto, auto, [[maybe_unused]] auto output) { |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 81 | auto lock = updateSlotStatus(); |
| 82 | |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 83 | try { |
| 84 | std::string source = input->val(0)->val_to_string(); |
| 85 | m_rauc->install(source); |
| 86 | } catch (sdbus::Error& e) { |
| 87 | m_log->warn("RAUC install error: '{}'", e.what()); |
| 88 | session->set_error(e.getMessage().c_str(), nullptr); |
| 89 | return SR_ERR_OPERATION_FAILED; |
| 90 | } |
| 91 | return SR_ERR_OK; |
| 92 | }, |
| 93 | 0, |
| 94 | SR_SUBSCR_CTX_REUSE); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 95 | |
| 96 | m_srSubscribeOps->oper_get_items_subscribe( |
| 97 | CZECHLIGHT_SYSTEM_MODULE_NAME.c_str(), |
Jan Kundrát | ef2b380 | 2021-02-18 09:57:05 +0100 | [diff] [blame] | 98 | [this](auto session, auto, auto, auto, auto, auto& parent) { |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 99 | std::map<std::string, std::string> data; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 100 | |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 101 | { |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 102 | auto lock = updateSlotStatus(); |
| 103 | |
| 104 | data.insert(m_slotStatusCache.begin(), m_slotStatusCache.end()); |
| 105 | data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/status"] = m_installStatus; |
| 106 | data[CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "installation/message"] = m_installMessage; |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 107 | } |
| 108 | |
Tomáš Pecka | 498e91c | 2021-03-02 17:46:47 +0100 | [diff] [blame] | 109 | utils::valuesToYang(data, {}, session, parent); |
Tomáš Pecka | af8f063 | 2021-01-27 16:45:55 +0100 | [diff] [blame] | 110 | return SR_ERR_OK; |
| 111 | }, |
| 112 | (CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "*").c_str(), |
| 113 | SR_SUBSCR_PASSIVE | SR_SUBSCR_OPER_MERGE | SR_SUBSCR_CTX_REUSE); |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 114 | } |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 115 | |
| 116 | /** @brief Updates the slot status cache with the new data obtained via RAUC |
| 117 | * |
| 118 | * Gets current slot status data from RAUC and updates the local slot status cache if new data are available. |
| 119 | * The methods manipulates with the local cache which is shared among multiple thread. |
| 120 | * |
| 121 | * @return an unique_lock (in locked state) that can be further used to manipulate with the local cache |
| 122 | * */ |
| 123 | std::unique_lock<std::mutex> Firmware::updateSlotStatus() |
| 124 | { |
| 125 | std::map<std::string, velia::system::RAUC::SlotProperties> slotStatus; |
| 126 | |
| 127 | try { |
| 128 | slotStatus = m_rauc->slotStatus(); |
| 129 | } catch (sdbus::Error& e) { |
| 130 | m_log->warn("Could not fetch RAUC slot status data: {}", e.getMessage()); |
| 131 | } |
| 132 | |
| 133 | std::unique_lock<std::mutex> lck(m_mtx); |
| 134 | |
| 135 | for (const auto& slotName : FIRMWARE_SLOTS) { |
| 136 | if (auto it = slotStatus.find(slotName); it != slotStatus.end()) { // if there is an update for the slot "slotName" |
| 137 | const auto& props = it->second; |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 138 | std::string xpathPrefix; |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 139 | |
Tomáš Pecka | 825542f | 2021-02-24 14:00:10 +0100 | [diff] [blame] | 140 | // 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). |
| 141 | if (auto pit = props.find("bootname"); pit != props.end()) { |
| 142 | xpathPrefix = CZECHLIGHT_SYSTEM_FIRMWARE_MODULE_PREFIX + "firmware-slot[name='" + std::get<std::string>(pit->second) + "']/"; |
| 143 | } else { |
| 144 | m_log->error("RAUC didn't provide 'bootname' property for slot '{}'. Skipping update for that slot."); |
| 145 | continue; |
| 146 | } |
| 147 | |
| 148 | for (const auto& [yangKey, raucKey] : {std::pair{"state", "state"}, {"boot-status", "boot-status"}, {"version", "bundle.version"}, {"installed", "installed.timestamp"}}) { |
| 149 | if (auto pit = props.find(raucKey); pit != props.end()) { |
| 150 | m_slotStatusCache[xpathPrefix + yangKey] = std::get<std::string>(pit->second); |
| 151 | } else { |
| 152 | m_log->warn("RAUC didn't provide '{}' property for slot '{}'.", raucKey, slotName); |
| 153 | } |
| 154 | } |
Tomáš Pecka | 92b14e4 | 2021-01-27 17:33:11 +0100 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | |
| 158 | return lck; |
| 159 | } |
Tomáš Pecka | cb7a5f8 | 2021-01-20 15:12:00 +0100 | [diff] [blame] | 160 | } |