Andrew Jeffery | 275f7c3 | 2024-01-31 12:41:14 +1030 | [diff] [blame] | 1 | #include "MCTPEndpoint.hpp" |
| 2 | |
| 3 | #include "Utils.hpp" |
| 4 | #include "VariantVisitors.hpp" |
| 5 | |
| 6 | #include <bits/fs_dir.h> |
| 7 | |
| 8 | #include <boost/system/detail/errc.hpp> |
| 9 | #include <phosphor-logging/lg2.hpp> |
| 10 | #include <sdbusplus/asio/connection.hpp> |
| 11 | #include <sdbusplus/bus.hpp> |
| 12 | #include <sdbusplus/bus/match.hpp> |
| 13 | #include <sdbusplus/exception.hpp> |
| 14 | #include <sdbusplus/message.hpp> |
| 15 | #include <sdbusplus/message/native_types.hpp> |
| 16 | |
| 17 | #include <cassert> |
| 18 | #include <charconv> |
| 19 | #include <cstdint> |
| 20 | #include <exception> |
| 21 | #include <filesystem> |
| 22 | #include <format> |
| 23 | #include <functional> |
| 24 | #include <map> |
| 25 | #include <memory> |
| 26 | #include <optional> |
| 27 | #include <set> |
| 28 | #include <stdexcept> |
| 29 | #include <string> |
| 30 | #include <system_error> |
| 31 | #include <utility> |
| 32 | #include <variant> |
| 33 | #include <vector> |
| 34 | |
| 35 | PHOSPHOR_LOG2_USING; |
| 36 | |
Thu Nguyen | 19d1fda | 2024-12-05 08:43:06 +0000 | [diff] [blame] | 37 | static constexpr const char* mctpdBusName = "au.com.codeconstruct.MCTP1"; |
| 38 | static constexpr const char* mctpdControlPath = "/au/com/codeconstruct/mctp1"; |
Andrew Jeffery | 275f7c3 | 2024-01-31 12:41:14 +1030 | [diff] [blame] | 39 | static constexpr const char* mctpdControlInterface = |
Thu Nguyen | 19d1fda | 2024-12-05 08:43:06 +0000 | [diff] [blame] | 40 | "au.com.codeconstruct.MCTP.BusOwner1"; |
Andrew Jeffery | 275f7c3 | 2024-01-31 12:41:14 +1030 | [diff] [blame] | 41 | static constexpr const char* mctpdEndpointControlInterface = |
Thu Nguyen | 19d1fda | 2024-12-05 08:43:06 +0000 | [diff] [blame] | 42 | "au.com.codeconstruct.MCTP.Endpoint1"; |
Andrew Jeffery | 275f7c3 | 2024-01-31 12:41:14 +1030 | [diff] [blame] | 43 | |
| 44 | MCTPDDevice::MCTPDDevice( |
| 45 | const std::shared_ptr<sdbusplus::asio::connection>& connection, |
| 46 | const std::string& interface, const std::vector<uint8_t>& physaddr) : |
| 47 | connection(connection), interface(interface), physaddr(physaddr) |
| 48 | {} |
| 49 | |
| 50 | void MCTPDDevice::onEndpointInterfacesRemoved( |
| 51 | const std::weak_ptr<MCTPDDevice>& weak, const std::string& objpath, |
| 52 | sdbusplus::message_t& msg) |
| 53 | { |
| 54 | auto path = msg.unpack<sdbusplus::message::object_path>(); |
| 55 | assert(path.str == objpath); |
| 56 | |
| 57 | auto removedIfaces = msg.unpack<std::set<std::string>>(); |
| 58 | if (!removedIfaces.contains(mctpdEndpointControlInterface)) |
| 59 | { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | if (auto self = weak.lock()) |
| 64 | { |
| 65 | self->endpointRemoved(); |
| 66 | } |
| 67 | else |
| 68 | { |
| 69 | info( |
| 70 | "Device for inventory at '{INVENTORY_PATH}' was destroyed concurrent to endpoint removal", |
| 71 | "INVENTORY_PATH", objpath); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | void MCTPDDevice::finaliseEndpoint( |
| 76 | const std::string& objpath, uint8_t eid, int network, |
| 77 | std::function<void(const std::error_code& ec, |
| 78 | const std::shared_ptr<MCTPEndpoint>& ep)>& added) |
| 79 | { |
| 80 | const auto matchSpec = |
| 81 | sdbusplus::bus::match::rules::interfacesRemovedAtPath(objpath); |
| 82 | removeMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 83 | *connection, matchSpec, |
| 84 | std::bind_front(MCTPDDevice::onEndpointInterfacesRemoved, |
| 85 | weak_from_this(), objpath)); |
| 86 | endpoint = std::make_shared<MCTPDEndpoint>(shared_from_this(), connection, |
| 87 | objpath, network, eid); |
| 88 | added({}, endpoint); |
| 89 | } |
| 90 | |
| 91 | void MCTPDDevice::setup( |
| 92 | std::function<void(const std::error_code& ec, |
| 93 | const std::shared_ptr<MCTPEndpoint>& ep)>&& added) |
| 94 | { |
| 95 | // Use a lambda to separate state validation from business logic, |
| 96 | // where the business logic for a successful setup() is encoded in |
| 97 | // MctpdDevice::finaliseEndpoint() |
| 98 | auto onSetup = [weak{weak_from_this()}, added{std::move(added)}]( |
| 99 | const boost::system::error_code& ec, uint8_t eid, |
| 100 | int network, const std::string& objpath, |
| 101 | bool allocated [[maybe_unused]]) mutable { |
| 102 | if (ec) |
| 103 | { |
| 104 | added(ec, {}); |
| 105 | return; |
| 106 | } |
| 107 | |
| 108 | if (auto self = weak.lock()) |
| 109 | { |
| 110 | self->finaliseEndpoint(objpath, eid, network, added); |
| 111 | } |
| 112 | else |
| 113 | { |
| 114 | info( |
| 115 | "Device object for inventory at '{INVENTORY_PATH}' was destroyed concurrent to completion of its endpoint setup", |
| 116 | "INVENTORY_PATH", objpath); |
| 117 | } |
| 118 | }; |
Thu Nguyen | 19d1fda | 2024-12-05 08:43:06 +0000 | [diff] [blame] | 119 | connection->async_method_call( |
| 120 | onSetup, mctpdBusName, |
| 121 | mctpdControlPath + std::string("/interfaces/") + interface, |
| 122 | mctpdControlInterface, "AssignEndpoint", physaddr); |
Andrew Jeffery | 275f7c3 | 2024-01-31 12:41:14 +1030 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | void MCTPDDevice::endpointRemoved() |
| 126 | { |
| 127 | if (endpoint) |
| 128 | { |
| 129 | debug("Endpoint removed @ [ {MCTP_ENDPOINT} ]", "MCTP_ENDPOINT", |
| 130 | endpoint->describe()); |
| 131 | removeMatch.reset(); |
| 132 | endpoint->removed(); |
| 133 | endpoint.reset(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | void MCTPDDevice::remove() |
| 138 | { |
| 139 | if (endpoint) |
| 140 | { |
| 141 | debug("Removing endpoint @ [ {MCTP_ENDPOINT} ]", "MCTP_ENDPOINT", |
| 142 | endpoint->describe()); |
| 143 | endpoint->remove(); |
| 144 | } |
| 145 | } |
| 146 | |
| 147 | std::string MCTPDDevice::describe() const |
| 148 | { |
| 149 | std::string description = std::format("interface: {}", interface); |
| 150 | if (!physaddr.empty()) |
| 151 | { |
| 152 | description.append(", address: 0x [ "); |
| 153 | auto it = physaddr.begin(); |
| 154 | for (; it != physaddr.end() - 1; it++) |
| 155 | { |
| 156 | description.append(std::format("{:02x} ", *it)); |
| 157 | } |
| 158 | description.append(std::format("{:02x} ]", *it)); |
| 159 | } |
| 160 | return description; |
| 161 | } |
| 162 | |
| 163 | std::string MCTPDEndpoint::path(const std::shared_ptr<MCTPEndpoint>& ep) |
| 164 | { |
Thu Nguyen | 19d1fda | 2024-12-05 08:43:06 +0000 | [diff] [blame] | 165 | return std::format("{}/networks/{}/endpoints/{}", mctpdControlPath, |
| 166 | ep->network(), ep->eid()); |
Andrew Jeffery | 275f7c3 | 2024-01-31 12:41:14 +1030 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | void MCTPDEndpoint::onMctpEndpointChange(sdbusplus::message_t& msg) |
| 170 | { |
| 171 | auto [iface, changed, _] = |
| 172 | msg.unpack<std::string, std::map<std::string, BasicVariantType>, |
| 173 | std::vector<std::string>>(); |
| 174 | if (iface != mctpdEndpointControlInterface) |
| 175 | { |
| 176 | return; |
| 177 | } |
| 178 | |
| 179 | auto it = changed.find("Connectivity"); |
| 180 | if (it == changed.end()) |
| 181 | { |
| 182 | return; |
| 183 | } |
| 184 | |
| 185 | updateEndpointConnectivity(std::get<std::string>(it->second)); |
| 186 | } |
| 187 | |
| 188 | void MCTPDEndpoint::updateEndpointConnectivity(const std::string& connectivity) |
| 189 | { |
| 190 | if (connectivity == "Degraded") |
| 191 | { |
| 192 | if (notifyDegraded) |
| 193 | { |
| 194 | notifyDegraded(shared_from_this()); |
| 195 | } |
| 196 | } |
| 197 | else if (connectivity == "Available") |
| 198 | { |
| 199 | if (notifyAvailable) |
| 200 | { |
| 201 | notifyAvailable(shared_from_this()); |
| 202 | } |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | debug("Unrecognised connectivity state: '{CONNECTIVITY_STATE}'", |
| 207 | "CONNECTIVITY_STATE", connectivity); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | int MCTPDEndpoint::network() const |
| 212 | { |
| 213 | return mctp.network; |
| 214 | } |
| 215 | |
| 216 | uint8_t MCTPDEndpoint::eid() const |
| 217 | { |
| 218 | return mctp.eid; |
| 219 | } |
| 220 | |
| 221 | void MCTPDEndpoint::subscribe(Event&& degraded, Event&& available, |
| 222 | Event&& removed) |
| 223 | { |
| 224 | const auto matchSpec = |
| 225 | sdbusplus::bus::match::rules::propertiesChangedNamespace( |
| 226 | objpath.str, mctpdEndpointControlInterface); |
| 227 | |
| 228 | this->notifyDegraded = std::move(degraded); |
| 229 | this->notifyAvailable = std::move(available); |
| 230 | this->notifyRemoved = std::move(removed); |
| 231 | |
| 232 | try |
| 233 | { |
| 234 | connectivityMatch.emplace( |
| 235 | static_cast<sdbusplus::bus_t&>(*connection), matchSpec, |
| 236 | [weak{weak_from_this()}, |
| 237 | path{objpath.str}](sdbusplus::message_t& msg) { |
| 238 | if (auto self = weak.lock()) |
| 239 | { |
| 240 | self->onMctpEndpointChange(msg); |
| 241 | } |
| 242 | else |
| 243 | { |
| 244 | info( |
| 245 | "The endpoint for the device at inventory path '{INVENTORY_PATH}' was destroyed concurrent to the removal of its state change match", |
| 246 | "INVENTORY_PATH", path); |
| 247 | } |
| 248 | }); |
| 249 | connection->async_method_call( |
| 250 | [weak{weak_from_this()}, |
| 251 | path{objpath.str}](const boost::system::error_code& ec, |
| 252 | const std::variant<std::string>& value) { |
| 253 | if (ec) |
| 254 | { |
| 255 | debug( |
| 256 | "Failed to get current connectivity state: {ERROR_MESSAGE}", |
| 257 | "ERROR_MESSAGE", ec.message(), "ERROR_CATEGORY", |
| 258 | ec.category().name(), "ERROR_CODE", ec.value()); |
| 259 | return; |
| 260 | } |
| 261 | |
| 262 | if (auto self = weak.lock()) |
| 263 | { |
| 264 | const std::string& connectivity = |
| 265 | std::get<std::string>(value); |
| 266 | self->updateEndpointConnectivity(connectivity); |
| 267 | } |
| 268 | else |
| 269 | { |
| 270 | info( |
| 271 | "The endpoint for the device at inventory path '{INVENTORY_PATH}' was destroyed concurrent to the completion of its connectivity state query", |
| 272 | "INVENTORY_PATH", path); |
| 273 | } |
| 274 | }, |
| 275 | mctpdBusName, objpath.str, "org.freedesktop.DBus.Properties", "Get", |
| 276 | mctpdEndpointControlInterface, "Connectivity"); |
| 277 | } |
| 278 | catch (const sdbusplus::exception::SdBusError& err) |
| 279 | { |
| 280 | this->notifyDegraded = nullptr; |
| 281 | this->notifyAvailable = nullptr; |
| 282 | this->notifyRemoved = nullptr; |
| 283 | std::throw_with_nested( |
| 284 | MCTPException("Failed to register connectivity signal match")); |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void MCTPDEndpoint::remove() |
| 289 | { |
| 290 | connection->async_method_call( |
| 291 | [self{shared_from_this()}](const boost::system::error_code& ec) { |
| 292 | if (ec) |
| 293 | { |
| 294 | debug("Failed to remove endpoint @ [ {MCTP_ENDPOINT} ]", |
| 295 | "MCTP_ENDPOINT", self->describe()); |
| 296 | return; |
| 297 | } |
| 298 | }, |
| 299 | mctpdBusName, objpath.str, mctpdEndpointControlInterface, "Remove"); |
| 300 | } |
| 301 | |
| 302 | void MCTPDEndpoint::removed() |
| 303 | { |
| 304 | if (notifyRemoved) |
| 305 | { |
| 306 | notifyRemoved(shared_from_this()); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | std::string MCTPDEndpoint::describe() const |
| 311 | { |
| 312 | return std::format("network: {}, EID: {} | {}", mctp.network, mctp.eid, |
| 313 | dev->describe()); |
| 314 | } |
| 315 | |
| 316 | std::shared_ptr<MCTPDevice> MCTPDEndpoint::device() const |
| 317 | { |
| 318 | return dev; |
| 319 | } |
| 320 | |
Patrick Williams | 556e04b | 2025-02-01 08:22:22 -0500 | [diff] [blame^] | 321 | std::optional<SensorBaseConfigMap> I2CMCTPDDevice::match( |
| 322 | const SensorData& config) |
Andrew Jeffery | 275f7c3 | 2024-01-31 12:41:14 +1030 | [diff] [blame] | 323 | { |
| 324 | auto iface = config.find(configInterfaceName(configType)); |
| 325 | if (iface == config.end()) |
| 326 | { |
| 327 | return std::nullopt; |
| 328 | } |
| 329 | return iface->second; |
| 330 | } |
| 331 | |
| 332 | bool I2CMCTPDDevice::match(const std::set<std::string>& interfaces) |
| 333 | { |
| 334 | return interfaces.contains(configInterfaceName(configType)); |
| 335 | } |
| 336 | |
| 337 | std::shared_ptr<I2CMCTPDDevice> I2CMCTPDDevice::from( |
| 338 | const std::shared_ptr<sdbusplus::asio::connection>& connection, |
| 339 | const SensorBaseConfigMap& iface) |
| 340 | { |
| 341 | auto mType = iface.find("Type"); |
| 342 | if (mType == iface.end()) |
| 343 | { |
| 344 | throw std::invalid_argument( |
| 345 | "No 'Type' member found for provided configuration object"); |
| 346 | } |
| 347 | |
| 348 | auto type = std::visit(VariantToStringVisitor(), mType->second); |
| 349 | if (type != configType) |
| 350 | { |
| 351 | throw std::invalid_argument("Not an SMBus device"); |
| 352 | } |
| 353 | |
| 354 | auto mAddress = iface.find("Address"); |
| 355 | auto mBus = iface.find("Bus"); |
| 356 | auto mName = iface.find("Name"); |
| 357 | if (mAddress == iface.end() || mBus == iface.end() || mName == iface.end()) |
| 358 | { |
| 359 | throw std::invalid_argument( |
| 360 | "Configuration object violates MCTPI2CTarget schema"); |
| 361 | } |
| 362 | |
| 363 | auto sAddress = std::visit(VariantToStringVisitor(), mAddress->second); |
| 364 | std::uint8_t address{}; |
| 365 | auto [aptr, aec] = std::from_chars( |
| 366 | sAddress.data(), sAddress.data() + sAddress.size(), address); |
| 367 | if (aec != std::errc{}) |
| 368 | { |
| 369 | throw std::invalid_argument("Bad device address"); |
| 370 | } |
| 371 | |
| 372 | auto sBus = std::visit(VariantToStringVisitor(), mBus->second); |
| 373 | int bus{}; |
| 374 | auto [bptr, |
| 375 | bec] = std::from_chars(sBus.data(), sBus.data() + sBus.size(), bus); |
| 376 | if (bec != std::errc{}) |
| 377 | { |
| 378 | throw std::invalid_argument("Bad bus index"); |
| 379 | } |
| 380 | |
| 381 | try |
| 382 | { |
| 383 | return std::make_shared<I2CMCTPDDevice>(connection, bus, address); |
| 384 | } |
| 385 | catch (const MCTPException& ex) |
| 386 | { |
| 387 | warning( |
| 388 | "Failed to create I2CMCTPDDevice at [ bus: {I2C_BUS}, address: {I2C_ADDRESS} ]: {EXCEPTION}", |
| 389 | "I2C_BUS", bus, "I2C_ADDRESS", address, "EXCEPTION", ex); |
| 390 | return {}; |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | std::string I2CMCTPDDevice::interfaceFromBus(int bus) |
| 395 | { |
| 396 | std::filesystem::path netdir = |
| 397 | std::format("/sys/bus/i2c/devices/i2c-{}/net", bus); |
| 398 | std::error_code ec; |
| 399 | std::filesystem::directory_iterator it(netdir, ec); |
| 400 | if (ec || it == std::filesystem::end(it)) |
| 401 | { |
| 402 | error("No net device associated with I2C bus {I2C_BUS} at {NET_DEVICE}", |
| 403 | "I2C_BUS", bus, "NET_DEVICE", netdir); |
| 404 | throw MCTPException("Bus is not configured as an MCTP interface"); |
| 405 | } |
| 406 | |
| 407 | return it->path().filename(); |
| 408 | } |