| 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 | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 11 | #include <NvidiaSmaDevice.hpp> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 12 | #include <boost/asio/error.hpp> |
| Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 13 | #include <boost/asio/io_context.hpp> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 14 | #include <boost/asio/post.hpp> |
| 15 | #include <boost/asio/steady_timer.hpp> |
| 16 | #include <boost/container/flat_map.hpp> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 17 | #include <phosphor-logging/lg2.hpp> |
| Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 18 | #include <sdbusplus/asio/connection.hpp> |
| 19 | #include <sdbusplus/asio/object_server.hpp> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 20 | #include <sdbusplus/bus.hpp> |
| 21 | #include <sdbusplus/bus/match.hpp> |
| 22 | #include <sdbusplus/message.hpp> |
| Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 23 | |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 24 | #include <array> |
| 25 | #include <chrono> |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 26 | #include <cstdlib> |
| 27 | #include <exception> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 28 | #include <functional> |
| Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 29 | #include <memory> |
| 30 | #include <string> |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 31 | #include <vector> |
| 32 | |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 33 | boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevices; |
| Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 34 | boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>> smaDevices; |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 35 | |
| 36 | void configTimerExpiryCallback( |
| 37 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 38 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 39 | mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec) |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 40 | { |
| 41 | if (ec == boost::asio::error::operation_aborted) |
| 42 | { |
| 43 | return; // we're being canceled |
| 44 | } |
| Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 45 | createSensors(io, objectServer, gpuDevices, smaDevices, dbusConnection, |
| 46 | mctpRequester); |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 47 | } |
| Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 48 | |
| 49 | int main() |
| 50 | { |
| 51 | boost::asio::io_context io; |
| 52 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); |
| 53 | sdbusplus::asio::object_server objectServer(systemBus, true); |
| 54 | objectServer.add_manager("/xyz/openbmc_project/sensors"); |
| Rohit PAI | 0a88826 | 2025-06-11 08:52:29 +0530 | [diff] [blame] | 55 | objectServer.add_manager("/xyz/openbmc_project/inventory"); |
| Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 56 | systemBus->request_name("xyz.openbmc_project.GpuSensor"); |
| 57 | |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 58 | mctp::MctpRequester mctpRequester(io); |
| 59 | |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 60 | boost::asio::post(io, [&]() { |
| Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 61 | createSensors(io, objectServer, gpuDevices, smaDevices, systemBus, |
| 62 | mctpRequester); |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 63 | }); |
| 64 | |
| 65 | boost::asio::steady_timer configTimer(io); |
| 66 | |
| 67 | std::function<void(sdbusplus::message_t&)> eventHandler = |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 68 | [&configTimer, &io, &objectServer, &systemBus, |
| 69 | &mctpRequester](sdbusplus::message_t&) { |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 70 | configTimer.expires_after(std::chrono::seconds(1)); |
| 71 | // create a timer because normally multiple properties change |
| Harshit Aghera | 560e6af | 2025-04-21 20:04:56 +0530 | [diff] [blame] | 72 | configTimer.async_wait(std::bind_front( |
| 73 | configTimerExpiryCallback, std::ref(io), std::ref(objectServer), |
| 74 | std::ref(systemBus), std::ref(mctpRequester))); |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 75 | }; |
| 76 | |
| 77 | std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches = |
| 78 | setupPropertiesChangedMatches( |
| Harshit Aghera | 4ecdfaa | 2025-05-22 11:35:39 +0530 | [diff] [blame] | 79 | *systemBus, std::to_array<const char*>({deviceType}), eventHandler); |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 80 | |
| 81 | // Watch for entity-manager to remove configuration interfaces |
| 82 | // so the corresponding sensors can be removed. |
| 83 | auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>( |
| 84 | static_cast<sdbusplus::bus_t&>(*systemBus), |
| 85 | sdbusplus::bus::match::rules::interfacesRemovedAtPath( |
| 86 | std::string(inventoryPath)), |
| Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 87 | [](sdbusplus::message_t& msg) { |
| 88 | interfaceRemoved(msg, gpuDevices, smaDevices); |
| 89 | }); |
| Harshit Aghera | d837b56 | 2025-04-21 19:50:10 +0530 | [diff] [blame] | 90 | |
| Marc Olberding | d0125c9 | 2025-10-08 14:37:19 -0700 | [diff] [blame] | 91 | try |
| 92 | { |
| 93 | io.run(); |
| 94 | } |
| 95 | catch (const std::exception& e) |
| 96 | { |
| 97 | lg2::error("fatal error caught during processing: {MSG}", "MSG", |
| 98 | e.what()); |
| 99 | return EXIT_FAILURE; |
| 100 | } |
| 101 | |
| Harshit Aghera | 82d4a62 | 2025-04-21 19:09:02 +0530 | [diff] [blame] | 102 | return 0; |
| 103 | } |