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 "Utils.hpp" |
| 9 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 10 | #include <NvidiaDeviceDiscovery.hpp> |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 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> |
Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 16 | #include <sdbusplus/asio/connection.hpp> |
| 17 | #include <sdbusplus/asio/object_server.hpp> |
Harshit Aghera | d837b56 | 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 | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 21 | |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 22 | #include <array> |
| 23 | #include <chrono> |
| 24 | #include <functional> |
Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 25 | #include <memory> |
| 26 | #include <string> |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 29 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevices; |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 30 | |
| 31 | void configTimerExpiryCallback( |
| 32 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 33 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 34 | mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec) |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 35 | { |
| 36 | if (ec == boost::asio::error::operation_aborted) |
| 37 | { |
| 38 | return; // we're being canceled |
| 39 | } |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 40 | createSensors(io, objectServer, gpuDevices, dbusConnection, mctpRequester); |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 41 | } |
Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 42 | |
| 43 | int main() |
| 44 | { |
| 45 | boost::asio::io_context io; |
| 46 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 47 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 48 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
| 49 | systemBus->request_name("xyz.openbmc_project.GpuSensor"); |
| 50 | |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 51 | mctp::MctpRequester mctpRequester(io); |
| 52 | |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 53 | boost::asio::post(io, [&]() { |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 54 | createSensors(io, objectServer, gpuDevices, systemBus, mctpRequester); |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 55 | }); |
| 56 | |
| 57 | boost::asio::steady_timer configTimer(io); |
| 58 | |
| 59 | std::function<void(sdbusplus::message_t&)> eventHandler = |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 60 | [&configTimer, &io, &objectServer, &systemBus, |
| 61 | &mctpRequester](sdbusplus::message_t&) { |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 62 | configTimer.expires_after(std::chrono::seconds(1)); |
| 63 | // create a timer because normally multiple properties change |
Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 64 | configTimer.async_wait(std::bind_front( |
| 65 | configTimerExpiryCallback, std::ref(io), std::ref(objectServer), |
| 66 | std::ref(systemBus), std::ref(mctpRequester))); |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 67 | }; |
| 68 | |
| 69 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 70 | setupPropertiesChangedMatches( |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 71 | *systemBus, std::to_array<const char*>({deviceType}), eventHandler); |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 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)), |
Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 79 | [](sdbusplus::message_t& msg) { interfaceRemoved(msg, gpuDevices); }); |
Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 80 | |
Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 81 | io.run(); |
| 82 | return 0; |
| 83 | } |