blob: 4338446e1eadbbe546849a61ff71b56baa5d7588 [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áš Pecka9cc00942021-01-14 22:45:10 +010037RAUC::RAUC(sdbus::IConnection& connection, 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)
Tomáš Pecka991e4d52021-01-11 10:03:14 +010038 : m_dbusObjectProxy(sdbus::createProxy(connection, BUS, OBJPATH))
Tomáš Pecka9cc00942021-01-14 22:45:10 +010039 , m_operCb(std::move(operCb))
40 , m_progressCb(std::move(progressCb))
41 , m_completedCb(std::move(completedCb))
Tomáš Pecka991e4d52021-01-11 10:03:14 +010042 , m_log(spdlog::get("system"))
43{
Tomáš Pecka9cc00942021-01-14 22:45:10 +010044 m_dbusObjectProxy->uponSignal("Completed").onInterface(INTERFACE).call([this](int32_t returnValue) {
45 std::string lastError = m_dbusObjectProxy->getProperty("LastError").onInterface(INTERFACE);
46 m_log->info("InstallBundle completed. Return value {}, last error: '{}'", returnValue, lastError);
47 m_completedCb(returnValue, lastError);
48 });
49
50 m_dbusObjectProxy->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) {
51 if (iface != INTERFACE) {
52 return;
53 }
54
55 if (auto itProgress = changed.find("Progress"); itProgress != changed.end()) {
56 // https://rauc.readthedocs.io/en/v1.4/using.html#sec-processing-progress
57 auto progress = itProgress->second.get<sdbus::Struct<int32_t, std::string, int32_t>>();
58 int32_t percentage = progress.get<0>();
59 std::string message = progress.get<1>();
60
61 m_log->debug("InstallBundle progress changed: {} {}", percentage, message);
62 m_progressCb(percentage, message);
63 }
64
65 if (auto itOper = changed.find("Operation"); itOper != changed.end()) {
66 auto oper = itOper->second.get<std::string>();
67 m_log->debug("Operation changed: {}", oper);
68 m_operCb(oper);
69 }
70 });
71
72 m_dbusObjectProxy->finishRegistration();
Tomáš Pecka991e4d52021-01-11 10:03:14 +010073}
74
75/** @brief Get current primary slot.
76 *
77 * RAUC's DBus GetPrimary method wrapper.
78 * See https://rauc.readthedocs.io/en/v1.4/reference.html#the-getprimary-method).
79 */
80std::string RAUC::primarySlot() const
81{
82 std::string primarySlot;
83 m_dbusObjectProxy->callMethod("GetPrimary").onInterface(INTERFACE).storeResultsTo(primarySlot);
84 return primarySlot;
85}
86
87/** @brief Get current status of all slots.
88 *
89 * RAUC's DBus GetSlotStatus method wrapper.
90 * The return value is restructualized from sdbus++ data structures to C++ data structures.
91 * (see https://rauc.readthedocs.io/en/v1.4/reference.html#gdbus-method-de-pengutronix-rauc-installer-getslotstatus)
92 */
93std::map<std::string, RAUC::SlotProperties> RAUC::slotStatus() const
94{
95 std::vector<sdbus::Struct<std::string, std::map<std::string, sdbus::Variant>>> slots;
96 m_dbusObjectProxy->callMethod("GetSlotStatus").onInterface(INTERFACE).storeResultsTo(slots);
97
98 std::map<std::string, SlotProperties> res;
99 for (const auto& slot : slots) {
100 SlotProperties status;
101
102 for (const auto& [key, val] : std::get<1>(slot)) {
103 status.insert(std::make_pair(key, sdbusVariantToCPPVariant(val)));
104 }
105
106 res.insert(std::make_pair(std::get<0>(slot), status));
107 }
108
109 return res;
110}
111
Tomáš Pecka9cc00942021-01-14 22:45:10 +0100112/** @brief Install new bundle.
113 *
114 * RAUC's DBus InstallBundle method wrapper.
115 * This method is non-blocking. The status of the installation progress is announced via DBus properties (LastError, Progress)
116 * and after the installation finishes, the Completed signal is triggered.
117 * (see https://rauc.readthedocs.io/en/v1.4/reference.html#gdbus-method-de-pengutronix-rauc-installer-installbundle)
118 */
119void RAUC::install(const std::string& source)
120{
121 m_dbusObjectProxy->callMethod("InstallBundle").onInterface(INTERFACE).withArguments(source, std::map<std::string, sdbus::Variant> {});
122}
Tomáš Peckac764b762021-01-23 21:46:21 +0100123
124std::string RAUC::operation() const
125{
126 return m_dbusObjectProxy->getProperty("Operation").onInterface(INTERFACE);
127}
128
129std::string RAUC::lastError() const
130{
131 return m_dbusObjectProxy->getProperty("LastError").onInterface(INTERFACE);
132}
Tomáš Pecka991e4d52021-01-11 10:03:14 +0100133}