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