Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 1 | /* |
| 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & |
| 3 | * AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0 |
| 4 | */ |
| 5 | |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 6 | #include "MctpRequester.hpp" |
| 7 | #include "OcpMctpVdm.hpp" |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 8 | #include "Utils.hpp" |
| 9 | |
Harshit Aghera | 11b9c1a | 2025-04-29 17:34:25 +0530 | [diff] [blame^] | 10 | #include <GpuDevice.hpp> |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 11 | #include <boost/asio/error.hpp> |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 12 | #include <boost/asio/io_context.hpp> |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 13 | #include <boost/asio/post.hpp> |
| 14 | #include <boost/asio/steady_timer.hpp> |
| 15 | #include <boost/container/flat_map.hpp> |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 16 | #include <sdbusplus/asio/connection.hpp> |
| 17 | #include <sdbusplus/asio/object_server.hpp> |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 18 | #include <sdbusplus/bus.hpp> |
| 19 | #include <sdbusplus/bus/match.hpp> |
| 20 | #include <sdbusplus/message.hpp> |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 21 | |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 22 | #include <array> |
| 23 | #include <chrono> |
| 24 | #include <functional> |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <string> |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Harshit Aghera | 11b9c1a | 2025-04-29 17:34:25 +0530 | [diff] [blame^] | 29 | /** |
| 30 | * @brief Global map of GPU devices keyed by their paths |
| 31 | * @details Stores all discovered GPU devices in the system for management |
| 32 | * and tracking throughout the application lifecycle |
| 33 | */ |
| 34 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevice; |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 35 | |
| 36 | /** |
Harshit Aghera | 11b9c1a | 2025-04-29 17:34:25 +0530 | [diff] [blame^] | 37 | * @brief Callback function executed when configuration timer expires |
| 38 | * @details Triggers sensor creation or reconfiguration process when |
| 39 | * configuration changes are detected in the system. If the timer was canceled |
| 40 | * due to application shutdown or other reasons, the function returns early. |
| 41 | * |
| 42 | * @param io Boost ASIO I/O context for scheduling asynchronous operations |
| 43 | * @param objectServer D-Bus object server for exposing sensor interfaces |
| 44 | * @param dbusConnection D-Bus connection for system communication |
| 45 | * @param mctpRequester MCTP requester for GPU communication protocol |
| 46 | * @param ec Boost ASIO error code indicating success or failure reason |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 47 | */ |
| 48 | void configTimerExpiryCallback( |
| 49 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 50 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 51 | mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec) |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 52 | { |
| 53 | if (ec == boost::asio::error::operation_aborted) |
| 54 | { |
| 55 | return; // we're being canceled |
| 56 | } |
Harshit Aghera | 11b9c1a | 2025-04-29 17:34:25 +0530 | [diff] [blame^] | 57 | createSensors(io, objectServer, gpuDevice, dbusConnection, mctpRequester); |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 58 | } |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 59 | |
| 60 | int main() |
| 61 | { |
| 62 | boost::asio::io_context io; |
| 63 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 64 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 65 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
| 66 | systemBus->request_name("xyz.openbmc_project.GpuSensor"); |
| 67 | |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 68 | mctp::MctpRequester mctpRequester(io, |
| 69 | ocp::accelerator_management::messageType); |
| 70 | |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 71 | boost::asio::post(io, [&]() { |
Harshit Aghera | 11b9c1a | 2025-04-29 17:34:25 +0530 | [diff] [blame^] | 72 | createSensors(io, objectServer, gpuDevice, systemBus, mctpRequester); |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 73 | }); |
| 74 | |
| 75 | boost::asio::steady_timer configTimer(io); |
| 76 | |
| 77 | std::function<void(sdbusplus::message_t&)> eventHandler = |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 78 | [&configTimer, &io, &objectServer, &systemBus, |
| 79 | &mctpRequester](sdbusplus::message_t&) { |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 80 | configTimer.expires_after(std::chrono::seconds(1)); |
| 81 | // create a timer because normally multiple properties change |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 82 | configTimer.async_wait(std::bind_front( |
| 83 | configTimerExpiryCallback, std::ref(io), std::ref(objectServer), |
| 84 | std::ref(systemBus), std::ref(mctpRequester))); |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 88 | setupPropertiesChangedMatches( |
| 89 | *systemBus, std::to_array<const char*>({sensorType}), eventHandler); |
| 90 | |
| 91 | // Watch for entity-manager to remove configuration interfaces |
| 92 | // so the corresponding sensors can be removed. |
| 93 | auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>( |
| 94 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| 95 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 96 | std::string(inventoryPath) + "/'", |
Harshit Aghera | 11b9c1a | 2025-04-29 17:34:25 +0530 | [diff] [blame^] | 97 | [](sdbusplus::message_t& msg) { interfaceRemoved(msg, gpuDevice); }); |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 98 | |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 99 | io.run(); |
| 100 | return 0; |
| 101 | } |