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