blob: 7360e9159c0f90501cd41b621be0f1ea3464fc86 [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.
Jan Kundráte55b56b2022-07-13 12:51:44 +020087 * 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áš Pecka991e4d52021-01-11 10:03:14 +010089 * See https://rauc.readthedocs.io/en/v1.4/reference.html#the-getprimary-method).
90 */
91std::string RAUC::primarySlot() const
92{
93 std::string primarySlot;
Tomáš Peckad51d4cb2021-02-03 14:15:49 +010094 m_dbusObjectProxyMethods->callMethod("GetPrimary").onInterface(INTERFACE).storeResultsTo(primarySlot);
Tomáš Pecka991e4d52021-01-11 10:03:14 +010095 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 */
104std::map<std::string, RAUC::SlotProperties> RAUC::slotStatus() const
105{
106 std::vector<sdbus::Struct<std::string, std::map<std::string, sdbus::Variant>>> slots;
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100107 m_dbusObjectProxyMethods->callMethod("GetSlotStatus").onInterface(INTERFACE).storeResultsTo(slots);
Tomáš Pecka991e4d52021-01-11 10:03:14 +0100108
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áš Pecka9cc00942021-01-14 22:45:10 +0100123/** @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 */
130void RAUC::install(const std::string& source)
131{
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100132 m_dbusObjectProxyMethods->callMethod("InstallBundle").onInterface(INTERFACE).withArguments(source, std::map<std::string, sdbus::Variant> {});
Tomáš Pecka9cc00942021-01-14 22:45:10 +0100133}
Tomáš Peckac764b762021-01-23 21:46:21 +0100134
135std::string RAUC::operation() const
136{
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100137 return m_dbusObjectProxyMethods->getProperty("Operation").onInterface(INTERFACE);
Tomáš Peckac764b762021-01-23 21:46:21 +0100138}
139
140std::string RAUC::lastError() const
141{
Tomáš Peckad51d4cb2021-02-03 14:15:49 +0100142 return m_dbusObjectProxyMethods->getProperty("LastError").onInterface(INTERFACE);
Tomáš Peckac764b762021-01-23 21:46:21 +0100143}
Tomáš Pecka991e4d52021-01-11 10:03:14 +0100144}