blob: 796f98b2bc520b35ab0b8ae0ad6ec64762d6dc1c [file] [log] [blame]
Tomáš Pecka991e4d52021-01-11 10:03:14 +01001/*
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
10namespace {
11
12const std::string INTERFACE = "de.pengutronix.rauc.Installer";
13const std::string BUS = "de.pengutronix.rauc";
14const std::string OBJPATH = "/";
15
16std::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
35namespace velia::system {
36
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010037/** @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 */
45RAUC::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áš Pecka9cc00942021-01-14 22:45:10 +010048 , m_operCb(std::move(operCb))
49 , m_progressCb(std::move(progressCb))
50 , m_completedCb(std::move(completedCb))
Tomáš Pecka991e4d52021-01-11 10:03:14 +010051 , m_log(spdlog::get("system"))
52{
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010053 m_dbusObjectProxySignals->uponSignal("Completed").onInterface(INTERFACE).call([this](int32_t returnValue) {
54 std::string lastError = m_dbusObjectProxySignals->getProperty("LastError").onInterface(INTERFACE);
Tomáš Pecka9cc00942021-01-14 22:45:10 +010055 m_log->info("InstallBundle completed. Return value {}, last error: '{}'", returnValue, lastError);
56 m_completedCb(returnValue, lastError);
57 });
58
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010059 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áš Pecka9cc00942021-01-14 22:45:10 +010060 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áš Peckad51d4cb2021-02-03 14:15:49 +010081 m_dbusObjectProxySignals->finishRegistration();
Tomáš Pecka991e4d52021-01-11 10:03:14 +010082}
83
84/** @brief Get current primary slot.
85 *
86 * RAUC's DBus GetPrimary method wrapper.
87 * See https://rauc.readthedocs.io/en/v1.4/reference.html#the-getprimary-method).
88 */
89std::string RAUC::primarySlot() const
90{
91 std::string primarySlot;
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010092 m_dbusObjectProxyMethods->callMethod("GetPrimary").onInterface(INTERFACE).storeResultsTo(primarySlot);
Tomáš Pecka991e4d52021-01-11 10:03:14 +010093 return primarySlot;
94}
95
96/** @brief Get current status of all slots.
97 *
98 * RAUC's DBus GetSlotStatus method wrapper.
99 * The return value is restructualized from sdbus++ data structures to C++ data structures.
100 * (see https://rauc.readthedocs.io/en/v1.4/reference.html#gdbus-method-de-pengutronix-rauc-installer-getslotstatus)
101 */
102std::map<std::string, RAUC::SlotProperties> RAUC::slotStatus() const
103{
104 std::vector<sdbus::Struct<std::string, std::map<std::string, sdbus::Variant>>> slots;
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100105 m_dbusObjectProxyMethods->callMethod("GetSlotStatus").onInterface(INTERFACE).storeResultsTo(slots);
Tomáš Pecka991e4d52021-01-11 10:03:14 +0100106
107 std::map<std::string, SlotProperties> res;
108 for (const auto& slot : slots) {
109 SlotProperties status;
110
111 for (const auto& [key, val] : std::get<1>(slot)) {
112 status.insert(std::make_pair(key, sdbusVariantToCPPVariant(val)));
113 }
114
115 res.insert(std::make_pair(std::get<0>(slot), status));
116 }
117
118 return res;
119}
120
Tomáš Pecka9cc00942021-01-14 22:45:10 +0100121/** @brief Install new bundle.
122 *
123 * RAUC's DBus InstallBundle method wrapper.
124 * This method is non-blocking. The status of the installation progress is announced via DBus properties (LastError, Progress)
125 * and after the installation finishes, the Completed signal is triggered.
126 * (see https://rauc.readthedocs.io/en/v1.4/reference.html#gdbus-method-de-pengutronix-rauc-installer-installbundle)
127 */
128void RAUC::install(const std::string& source)
129{
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100130 m_dbusObjectProxyMethods->callMethod("InstallBundle").onInterface(INTERFACE).withArguments(source, std::map<std::string, sdbus::Variant> {});
Tomáš Pecka9cc00942021-01-14 22:45:10 +0100131}
Tomáš Peckac764b762021-01-23 21:46:21 +0100132
133std::string RAUC::operation() const
134{
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100135 return m_dbusObjectProxyMethods->getProperty("Operation").onInterface(INTERFACE);
Tomáš Peckac764b762021-01-23 21:46:21 +0100136}
137
138std::string RAUC::lastError() const
139{
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100140 return m_dbusObjectProxyMethods->getProperty("LastError").onInterface(INTERFACE);
Tomáš Peckac764b762021-01-23 21:46:21 +0100141}
Tomáš Pecka991e4d52021-01-11 10:03:14 +0100142}