Harshit Aghera | 82d4a62 | 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 | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame^] | 6 | #include "NvidiaGpuSensor.hpp" |
| 7 | #include "Utils.hpp" |
| 8 | |
| 9 | #include <boost/asio/error.hpp> |
Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 10 | #include <boost/asio/io_context.hpp> |
Harshit Aghera | d837b56 | 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 | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 15 | #include <sdbusplus/asio/connection.hpp> |
| 16 | #include <sdbusplus/asio/object_server.hpp> |
Harshit Aghera | d837b56 | 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 | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 20 | |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame^] | 21 | #include <array> |
| 22 | #include <chrono> |
| 23 | #include <functional> |
Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 24 | #include <memory> |
| 25 | #include <string> |
Harshit Aghera | d837b56 | 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 | void configTimerExpiryCallback( |
| 31 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 32 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 33 | const boost::system::error_code& ec) |
| 34 | { |
| 35 | if (ec == boost::asio::error::operation_aborted) |
| 36 | { |
| 37 | return; // we're being canceled |
| 38 | } |
| 39 | createSensors(io, objectServer, sensors, dbusConnection); |
| 40 | if (sensors.empty()) |
| 41 | { |
| 42 | lg2::info("Configuration not detected"); |
| 43 | } |
| 44 | } |
Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 45 | |
| 46 | int main() |
| 47 | { |
| 48 | boost::asio::io_context io; |
| 49 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 50 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 51 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
| 52 | systemBus->request_name("xyz.openbmc_project.GpuSensor"); |
| 53 | |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame^] | 54 | boost::asio::post(io, [&]() { |
| 55 | createSensors(io, objectServer, sensors, systemBus); |
| 56 | }); |
| 57 | |
| 58 | boost::asio::steady_timer configTimer(io); |
| 59 | |
| 60 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| 61 | [&configTimer, &io, &objectServer, &systemBus](sdbusplus::message_t&) { |
| 62 | configTimer.expires_after(std::chrono::seconds(1)); |
| 63 | // create a timer because normally multiple properties change |
| 64 | configTimer.async_wait( |
| 65 | std::bind_front(configTimerExpiryCallback, std::ref(io), |
| 66 | std::ref(objectServer), std::ref(systemBus))); |
| 67 | }; |
| 68 | |
| 69 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 70 | setupPropertiesChangedMatches( |
| 71 | *systemBus, std::to_array<const char*>({sensorType}), eventHandler); |
| 72 | |
| 73 | // Watch for entity-manager to remove configuration interfaces |
| 74 | // so the corresponding sensors can be removed. |
| 75 | auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>( |
| 76 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| 77 | sdbusplus::bus::match::rules::interfacesRemovedAtPath( |
| 78 | std::string(inventoryPath)), |
| 79 | [](sdbusplus::message_t& msg) { interfaceRemoved(msg, sensors); }); |
| 80 | |
Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 81 | io.run(); |
| 82 | return 0; |
| 83 | } |