|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +// Copyright Pionix GmbH and Contributors to EVerest |
| 3 | + |
| 4 | +#pragma once |
| 5 | + |
| 6 | +#include <sdbus-c++/sdbus-c++.h> |
| 7 | + |
| 8 | +#include <everest/system/dbus_base.hpp> |
| 9 | + |
| 10 | +#include <cstdint> |
| 11 | +#include <string> |
| 12 | +#include <string_view> |
| 13 | + |
| 14 | +namespace everest::lib::system::rauc_dbus { |
| 15 | + |
| 16 | +namespace defaults { |
| 17 | +constexpr const char* service_domain = "de.pengutronix.rauc"; |
| 18 | +constexpr const char* object_path = "/"; |
| 19 | +} // namespace defaults |
| 20 | + |
| 21 | +namespace interface { |
| 22 | +constexpr const char* Installer = "de.pengutronix.rauc.Installer"; |
| 23 | +constexpr const char* DBus_Properties = "org.freedesktop.DBus.Properties"; |
| 24 | +} // namespace interface |
| 25 | + |
| 26 | +namespace property { |
| 27 | +constexpr const char* Progress = "Progress"; |
| 28 | +constexpr const char* Operation = "Operation"; |
| 29 | +constexpr const char* LastError = "LastError"; |
| 30 | +constexpr const char* BootSlot = "BootSlot"; |
| 31 | +} // namespace property |
| 32 | + |
| 33 | +namespace signal { |
| 34 | +constexpr const char* PropertiesChanged = "PropertiesChanged"; |
| 35 | +constexpr const char* Completed = "Completed"; |
| 36 | +} // namespace signal |
| 37 | + |
| 38 | +namespace method { |
| 39 | +constexpr const char* InstallBundle = "InstallBundle"; |
| 40 | +constexpr const char* InspectBundle = "InspectBundle"; |
| 41 | +constexpr const char* Mark = "Mark"; |
| 42 | +constexpr const char* GetSlotStatus = "GetSlotStatus"; |
| 43 | +constexpr const char* GetPrimary = "GetPrimary"; |
| 44 | +} // namespace method |
| 45 | + |
| 46 | +namespace rauc_messages { |
| 47 | +struct Progress { |
| 48 | + int percent; |
| 49 | + std::string description; |
| 50 | + int level; |
| 51 | +}; |
| 52 | + |
| 53 | +struct CmdResult { |
| 54 | + bool success; |
| 55 | + std::string error_msg; |
| 56 | +}; |
| 57 | + |
| 58 | +enum class Operation { |
| 59 | + Unknown, |
| 60 | + Idle, |
| 61 | + Installing |
| 62 | +}; |
| 63 | + |
| 64 | +struct UpdateTransaction { |
| 65 | + std::int32_t request_id; |
| 66 | + std::string boot_slot; |
| 67 | + std::string primary_slot; |
| 68 | +}; |
| 69 | + |
| 70 | +enum class HealthCheckStatus { |
| 71 | + ScriptExitedWithError, |
| 72 | + ScriptNotExecutable, |
| 73 | + ScriptNotSet, |
| 74 | + ScriptTerminatedBySignal, |
| 75 | + SetupFailed, |
| 76 | + Success, |
| 77 | + UnknownError |
| 78 | +}; |
| 79 | + |
| 80 | +Operation string_to_operation(const std::string_view& s); |
| 81 | +// Stream operators are needed for type conversion in sdbus |
| 82 | +sdbus::Message& operator<<(sdbus::Message& msg, const Progress& items); |
| 83 | +sdbus::Message& operator>>(sdbus::Message& msg, Progress& items); |
| 84 | + |
| 85 | +} // namespace rauc_messages |
| 86 | + |
| 87 | +// ---------------------------------------------------------------------------- |
| 88 | +// Base class |
| 89 | + |
| 90 | +class RaucBase { |
| 91 | +public: |
| 92 | + using property_cb = dbus::async_property_callback; |
| 93 | + using method_cb = dbus::async_method_callback; |
| 94 | + using slot_info_t = std::map<std::string, std::map<std::string, std::string>>; |
| 95 | + |
| 96 | + struct CurrentState { |
| 97 | + rauc_messages::HealthCheckStatus system_health_rc; |
| 98 | + std::string boot_slot; |
| 99 | + std::string primary_slot; |
| 100 | + }; |
| 101 | + |
| 102 | + RaucBase(); |
| 103 | + RaucBase(sdbus::dont_run_event_loop_thread_t); |
| 104 | + |
| 105 | + void configure(const std::string& verify_update_script_path = ""); |
| 106 | + bool check_previous_transaction(const CurrentState& current, const rauc_messages::UpdateTransaction& saved); |
| 107 | + |
| 108 | + // methods |
| 109 | + sdbus::PendingAsyncCall install_bundle(method_cb handler, const std::string& filename, uint64_t timeout_us); |
| 110 | + sdbus::PendingAsyncCall mark(method_cb handler, const std::string& mark, const std::string& slot, |
| 111 | + uint64_t timeout_us); |
| 112 | + |
| 113 | + // properties |
| 114 | + sdbus::PendingAsyncCall get_boot_slot(property_cb handler) const; |
| 115 | + |
| 116 | +protected: |
| 117 | + std::unique_ptr<sdbus::IProxy> proxy; |
| 118 | + |
| 119 | + using slots_t = std::vector<sdbus::Struct<std::string, std::map<std::string, sdbus::Variant>>>; |
| 120 | + static slot_info_t convert(slots_t& slots); |
| 121 | + |
| 122 | + rauc_messages::HealthCheckStatus check_system_health(); |
| 123 | + |
| 124 | + // methods |
| 125 | + sdbus::PendingAsyncCall get_primary_slot(method_cb handler, uint64_t timeout_us); |
| 126 | + sdbus::PendingAsyncCall get_slot_status(method_cb handler, uint64_t timeout_us); |
| 127 | + |
| 128 | + // properties |
| 129 | + sdbus::PendingAsyncCall get_last_error(property_cb handler) const; |
| 130 | + sdbus::PendingAsyncCall get_operation(property_cb handler) const; |
| 131 | + sdbus::PendingAsyncCall get_progress(property_cb handler) const; |
| 132 | + |
| 133 | + virtual bool decide_if_good(const rauc_messages::UpdateTransaction& saved, const CurrentState& current); |
| 134 | + virtual void configure_handlers() = 0; |
| 135 | + |
| 136 | +private: |
| 137 | + std::string verify_update_script_path; |
| 138 | +}; |
| 139 | + |
| 140 | +// ---------------------------------------------------------------------------- |
| 141 | +// Synchronous base class |
| 142 | + |
| 143 | +class RaucBaseSync : public RaucBase { |
| 144 | +public: |
| 145 | + using RaucBase::RaucBase; |
| 146 | + |
| 147 | + rauc_messages::UpdateTransaction create_transaction(std::int32_t request_id, uint64_t timeout_us); |
| 148 | + bool check_previous_transaction(const rauc_messages::UpdateTransaction& saved, uint64_t timeout_us); |
| 149 | + |
| 150 | + // methods |
| 151 | + rauc_messages::CmdResult install_bundle(const std::string& filename, uint64_t timeout_us); |
| 152 | + void mark(const std::string& mark, const std::string& slot, uint64_t timeout_us); |
| 153 | + |
| 154 | + // properties |
| 155 | + std::string get_boot_slot() const; |
| 156 | + |
| 157 | +protected: |
| 158 | + // methods |
| 159 | + std::string get_primary_slot(uint64_t timeout_us); |
| 160 | + slot_info_t get_slot_status(uint64_t timeout_us); |
| 161 | + |
| 162 | + // properties |
| 163 | + std::string get_last_error() const; |
| 164 | + rauc_messages::Operation get_operation() const; |
| 165 | + rauc_messages::Progress get_progress() const; |
| 166 | +}; |
| 167 | + |
| 168 | +// ---------------------------------------------------------------------------- |
| 169 | +// Asynchronous base class |
| 170 | + |
| 171 | +class RaucBaseAsync : public RaucBase { |
| 172 | +public: |
| 173 | + using RaucBase::RaucBase; |
| 174 | + |
| 175 | + // methods |
| 176 | + bool install_bundle(const std::string& filename, uint64_t timeout_us); |
| 177 | + bool mark(const std::string& mark, const std::string& slot, uint64_t timeout_us); |
| 178 | + |
| 179 | + // properties |
| 180 | + bool get_boot_slot(); |
| 181 | + |
| 182 | +protected: |
| 183 | + sdbus::PendingAsyncCall active_request; |
| 184 | + |
| 185 | + // methods |
| 186 | + bool get_primary_slot(uint64_t timeout_us); |
| 187 | + bool get_slot_status(uint64_t timeout_us); |
| 188 | + |
| 189 | + // properties |
| 190 | + bool get_last_error(); |
| 191 | + bool get_operation(); |
| 192 | + bool get_progress(); |
| 193 | + |
| 194 | + // callbacks |
| 195 | + enum class error_t : std::uint8_t { |
| 196 | + install_bundle, |
| 197 | + mark, |
| 198 | + boot_slot, |
| 199 | + primary_slot, |
| 200 | + slot_status, |
| 201 | + last_error, |
| 202 | + operation, |
| 203 | + progress |
| 204 | + }; |
| 205 | + |
| 206 | + virtual void cb_install_bundle() = 0; |
| 207 | + virtual void cb_mark() = 0; |
| 208 | + virtual void cb_boot_slot(const std::string_view& value) = 0; |
| 209 | + virtual void cb_primary_slot(const std::string_view& value) = 0; |
| 210 | + virtual void cb_slot_status(const slot_info_t& value) = 0; |
| 211 | + virtual void cb_last_error(const std::string_view& value) = 0; |
| 212 | + virtual void cb_operation(rauc_messages::Operation value) = 0; |
| 213 | + virtual void cb_progress(const rauc_messages::Progress& value) = 0; |
| 214 | + virtual void cb_error(error_t fn, const std::string_view& error) = 0; |
| 215 | +}; |
| 216 | + |
| 217 | +} // namespace everest::lib::system::rauc_dbus |
| 218 | + |
| 219 | +namespace sdbus { |
| 220 | +template <> |
| 221 | +struct signature_of<everest::lib::system::rauc_dbus::rauc_messages::Progress> |
| 222 | + : signature_of<Struct<int, std::string, int>> {}; |
| 223 | +} // namespace sdbus |
0 commit comments