Tomáš Pecka | 991e4d5 | 2021-01-11 10:03:14 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2021 CESNET, https://photonics.cesnet.cz/ |
| 3 | * |
| 4 | * Written by Tomáš Pecka <tomas.pecka@fit.cvut.cz> |
| 5 | * |
| 6 | */ |
| 7 | #include "RAUC.h" |
| 8 | #include "utils/log.h" |
| 9 | |
| 10 | namespace { |
| 11 | |
| 12 | const std::string INTERFACE = "de.pengutronix.rauc.Installer"; |
| 13 | const std::string BUS = "de.pengutronix.rauc"; |
| 14 | const std::string OBJPATH = "/"; |
| 15 | |
| 16 | std::variant<std::string, uint64_t, uint32_t> sdbusVariantToCPPVariant(const sdbus::Variant& v) |
| 17 | { |
| 18 | // see https://www.freedesktop.org/software/systemd/man/sd_bus_message_read.html |
| 19 | auto peek = v.peekValueType(); |
| 20 | |
| 21 | // so far (v1.4), RAUC uses only these types |
| 22 | if (peek == "s") { |
| 23 | return v.get<std::string>(); |
| 24 | } else if (peek == "u") { |
| 25 | return v.get<uint32_t>(); |
| 26 | } else if (peek == "t") { |
| 27 | return v.get<uint64_t>(); |
| 28 | } |
| 29 | |
| 30 | throw std::invalid_argument("Unimplemented sdbus::variant type readout."); |
| 31 | } |
| 32 | |
| 33 | } |
| 34 | |
| 35 | namespace velia::system { |
| 36 | |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 37 | /** @brief Constructs a class communicating with RAUC via D-Bus. |
| 38 | * |
| 39 | * @param signalConnection A D-Bus connection. Used for handling signals on the object. |
| 40 | * @param methodConnection A D-Bus connection. Used for calling D-Bus methods on the object. |
| 41 | * @param operCb A function to execute whene RAUC's operation status changes. |
| 42 | * @param progressCb A function to execute when RAUC's installation makes progress. |
| 43 | * @param completedCb A function to execute when RAUC's installation completes. |
| 44 | */ |
| 45 | RAUC::RAUC(sdbus::IConnection& signalConnection, sdbus::IConnection& methodConnection, std::function<void(const std::string&)> operCb, std::function<void(int32_t, const std::string&)> progressCb, std::function<void(int32_t, const std::string&)> completedCb) |
| 46 | : m_dbusObjectProxySignals(sdbus::createProxy(signalConnection, BUS, OBJPATH)) |
| 47 | , m_dbusObjectProxyMethods(sdbus::createProxy(methodConnection, BUS, OBJPATH)) |
Tomáš Pecka | 9cc0094 | 2021-01-14 22:45:10 +0100 | [diff] [blame] | 48 | , m_operCb(std::move(operCb)) |
| 49 | , m_progressCb(std::move(progressCb)) |
| 50 | , m_completedCb(std::move(completedCb)) |
Tomáš Pecka | 991e4d5 | 2021-01-11 10:03:14 +0100 | [diff] [blame] | 51 | , m_log(spdlog::get("system")) |
| 52 | { |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 53 | m_dbusObjectProxySignals->uponSignal("Completed").onInterface(INTERFACE).call([this](int32_t returnValue) { |
| 54 | std::string lastError = m_dbusObjectProxySignals->getProperty("LastError").onInterface(INTERFACE); |
Tomáš Pecka | 9cc0094 | 2021-01-14 22:45:10 +0100 | [diff] [blame] | 55 | m_log->info("InstallBundle completed. Return value {}, last error: '{}'", returnValue, lastError); |
| 56 | m_completedCb(returnValue, lastError); |
| 57 | }); |
| 58 | |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 59 | m_dbusObjectProxySignals->uponSignal("PropertiesChanged").onInterface("org.freedesktop.DBus.Properties").call([this](const std::string& iface, const std::map<std::string, sdbus::Variant>& changed, [[maybe_unused]] const std::vector<std::string>& invalidated) { |
Tomáš Pecka | 9cc0094 | 2021-01-14 22:45:10 +0100 | [diff] [blame] | 60 | if (iface != INTERFACE) { |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | if (auto itProgress = changed.find("Progress"); itProgress != changed.end()) { |
| 65 | // https://rauc.readthedocs.io/en/v1.4/using.html#sec-processing-progress |
| 66 | auto progress = itProgress->second.get<sdbus::Struct<int32_t, std::string, int32_t>>(); |
| 67 | int32_t percentage = progress.get<0>(); |
| 68 | std::string message = progress.get<1>(); |
| 69 | |
| 70 | m_log->debug("InstallBundle progress changed: {} {}", percentage, message); |
| 71 | m_progressCb(percentage, message); |
| 72 | } |
| 73 | |
| 74 | if (auto itOper = changed.find("Operation"); itOper != changed.end()) { |
| 75 | auto oper = itOper->second.get<std::string>(); |
| 76 | m_log->debug("Operation changed: {}", oper); |
| 77 | m_operCb(oper); |
| 78 | } |
| 79 | }); |
| 80 | |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 81 | m_dbusObjectProxySignals->finishRegistration(); |
Tomáš Pecka | 991e4d5 | 2021-01-11 10:03:14 +0100 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /** @brief Get current primary slot. |
| 85 | * |
| 86 | * RAUC's DBus GetPrimary method wrapper. |
Jan Kundrát | e55b56b | 2022-07-13 12:51:44 +0200 | [diff] [blame] | 87 | * Note that the "slot name" is not the boot name. Typically, a boot name is A or B, |
| 88 | * while a slot name is something like rootfs.0 or cfg.1. |
Tomáš Pecka | 991e4d5 | 2021-01-11 10:03:14 +0100 | [diff] [blame] | 89 | * See https://rauc.readthedocs.io/en/v1.4/reference.html#the-getprimary-method). |
| 90 | */ |
| 91 | std::string RAUC::primarySlot() const |
| 92 | { |
| 93 | std::string primarySlot; |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 94 | m_dbusObjectProxyMethods->callMethod("GetPrimary").onInterface(INTERFACE).storeResultsTo(primarySlot); |
Tomáš Pecka | 991e4d5 | 2021-01-11 10:03:14 +0100 | [diff] [blame] | 95 | return primarySlot; |
| 96 | } |
| 97 | |
| 98 | /** @brief Get current status of all slots. |
| 99 | * |
| 100 | * RAUC's DBus GetSlotStatus method wrapper. |
| 101 | * The return value is restructualized from sdbus++ data structures to C++ data structures. |
| 102 | * (see https://rauc.readthedocs.io/en/v1.4/reference.html#gdbus-method-de-pengutronix-rauc-installer-getslotstatus) |
| 103 | */ |
| 104 | std::map<std::string, RAUC::SlotProperties> RAUC::slotStatus() const |
| 105 | { |
| 106 | std::vector<sdbus::Struct<std::string, std::map<std::string, sdbus::Variant>>> slots; |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 107 | m_dbusObjectProxyMethods->callMethod("GetSlotStatus").onInterface(INTERFACE).storeResultsTo(slots); |
Tomáš Pecka | 991e4d5 | 2021-01-11 10:03:14 +0100 | [diff] [blame] | 108 | |
| 109 | std::map<std::string, SlotProperties> res; |
| 110 | for (const auto& slot : slots) { |
| 111 | SlotProperties status; |
| 112 | |
| 113 | for (const auto& [key, val] : std::get<1>(slot)) { |
| 114 | status.insert(std::make_pair(key, sdbusVariantToCPPVariant(val))); |
| 115 | } |
| 116 | |
| 117 | res.insert(std::make_pair(std::get<0>(slot), status)); |
| 118 | } |
| 119 | |
| 120 | return res; |
| 121 | } |
| 122 | |
Tomáš Pecka | 9cc0094 | 2021-01-14 22:45:10 +0100 | [diff] [blame] | 123 | /** @brief Install new bundle. |
| 124 | * |
| 125 | * RAUC's DBus InstallBundle method wrapper. |
| 126 | * This method is non-blocking. The status of the installation progress is announced via DBus properties (LastError, Progress) |
| 127 | * and after the installation finishes, the Completed signal is triggered. |
| 128 | * (see https://rauc.readthedocs.io/en/v1.4/reference.html#gdbus-method-de-pengutronix-rauc-installer-installbundle) |
| 129 | */ |
| 130 | void RAUC::install(const std::string& source) |
| 131 | { |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 132 | m_dbusObjectProxyMethods->callMethod("InstallBundle").onInterface(INTERFACE).withArguments(source, std::map<std::string, sdbus::Variant> {}); |
Tomáš Pecka | 9cc0094 | 2021-01-14 22:45:10 +0100 | [diff] [blame] | 133 | } |
Tomáš Pecka | c764b76 | 2021-01-23 21:46:21 +0100 | [diff] [blame] | 134 | |
Jan Kundrát | 3795cab | 2022-07-13 18:08:19 +0200 | [diff] [blame] | 135 | /** @brief Mark a given slot as active/good/... |
| 136 | * |
| 137 | * This wraps RAUC's DBus method `Mark`. |
| 138 | */ |
| 139 | void RAUC::mark(const std::string& how, const std::string& slot) |
| 140 | { |
| 141 | m_dbusObjectProxyMethods->callMethod("Mark").onInterface(INTERFACE).withArguments(how, slot); |
| 142 | } |
| 143 | |
Tomáš Pecka | c764b76 | 2021-01-23 21:46:21 +0100 | [diff] [blame] | 144 | std::string RAUC::operation() const |
| 145 | { |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 146 | return m_dbusObjectProxyMethods->getProperty("Operation").onInterface(INTERFACE); |
Tomáš Pecka | c764b76 | 2021-01-23 21:46:21 +0100 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | std::string RAUC::lastError() const |
| 150 | { |
Tomáš Pecka | d51d4cb | 2021-02-03 14:15:49 +0100 | [diff] [blame] | 151 | return m_dbusObjectProxyMethods->getProperty("LastError").onInterface(INTERFACE); |
Tomáš Pecka | c764b76 | 2021-01-23 21:46:21 +0100 | [diff] [blame] | 152 | } |
Tomáš Pecka | 991e4d5 | 2021-01-11 10:03:14 +0100 | [diff] [blame] | 153 | } |