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 | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 6 | #include "GpuSensor.hpp" |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame^] | 7 | #include "MctpRequester.hpp" |
| 8 | #include "OcpMctpVdm.hpp" |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 9 | #include "Utils.hpp" |
| 10 | |
| 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> |
| 16 | #include <phosphor-logging/lg2.hpp> |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 17 | #include <sdbusplus/asio/connection.hpp> |
| 18 | #include <sdbusplus/asio/object_server.hpp> |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 19 | #include <sdbusplus/bus.hpp> |
| 20 | #include <sdbusplus/bus/match.hpp> |
| 21 | #include <sdbusplus/message.hpp> |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 22 | |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 23 | #include <array> |
| 24 | #include <chrono> |
| 25 | #include <functional> |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 26 | #include <memory> |
| 27 | #include <string> |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 28 | #include <vector> |
| 29 | |
| 30 | boost::container::flat_map<std::string, std::shared_ptr<GpuTempSensor>> sensors; |
| 31 | |
| 32 | /** |
| 33 | * @brief config timer expiry callback |
| 34 | * @param io Boost ASIO I/O context |
| 35 | * @param objectServer D-Bus object server |
| 36 | * @param dbusConnection D-Bus connection |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame^] | 37 | * @param mctpRequester MCTP requester for GPU communication |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 38 | * @param ec Boost ASIO error code |
| 39 | */ |
| 40 | void configTimerExpiryCallback( |
| 41 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 42 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame^] | 43 | mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec) |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 44 | { |
| 45 | if (ec == boost::asio::error::operation_aborted) |
| 46 | { |
| 47 | return; // we're being canceled |
| 48 | } |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame^] | 49 | createSensors(io, objectServer, sensors, dbusConnection, mctpRequester); |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 50 | if (sensors.empty()) |
| 51 | { |
| 52 | lg2::info("Configuration not detected"); |
| 53 | } |
| 54 | } |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 55 | |
| 56 | int main() |
| 57 | { |
| 58 | boost::asio::io_context io; |
| 59 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 60 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 61 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
| 62 | systemBus->request_name("xyz.openbmc_project.GpuSensor"); |
| 63 | |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame^] | 64 | mctp::MctpRequester mctpRequester(io, |
| 65 | ocp::accelerator_management::messageType); |
| 66 | |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 67 | boost::asio::post(io, [&]() { |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame^] | 68 | createSensors(io, objectServer, sensors, systemBus, mctpRequester); |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 69 | }); |
| 70 | |
| 71 | boost::asio::steady_timer configTimer(io); |
| 72 | |
| 73 | std::function<void(sdbusplus::message_t&)> eventHandler = |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame^] | 74 | [&configTimer, &io, &objectServer, &systemBus, |
| 75 | &mctpRequester](sdbusplus::message_t&) { |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 76 | configTimer.expires_after(std::chrono::seconds(1)); |
| 77 | // create a timer because normally multiple properties change |
Harshit Aghera | a3f24f4 | 2025-04-21 20:04:56 +0530 | [diff] [blame^] | 78 | configTimer.async_wait(std::bind_front( |
| 79 | configTimerExpiryCallback, std::ref(io), std::ref(objectServer), |
| 80 | std::ref(systemBus), std::ref(mctpRequester))); |
Harshit Aghera | acd375a | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 81 | }; |
| 82 | |
| 83 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 84 | setupPropertiesChangedMatches( |
| 85 | *systemBus, std::to_array<const char*>({sensorType}), eventHandler); |
| 86 | |
| 87 | // Watch for entity-manager to remove configuration interfaces |
| 88 | // so the corresponding sensors can be removed. |
| 89 | auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>( |
| 90 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| 91 | "type='signal',member='InterfacesRemoved',arg0path='" + |
| 92 | std::string(inventoryPath) + "/'", |
| 93 | [](sdbusplus::message_t& msg) { interfaceRemoved(msg, sensors); }); |
| 94 | |
Harshit Aghera | 2c02468 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 95 | io.run(); |
| 96 | return 0; |
| 97 | } |