blob: 18e5e1a9bd78e1cfc8453961e1d1ce271ca60923 [file] [log] [blame]
Harshit Aghera82d4a622025-04-21 19:09:02 +05301/*
Ed Tanousb5e823f2025-10-09 20:28:42 -04002 * SPDX-FileCopyrightText: Copyright OpenBMC Authors
Harshit Aghera560e6af2025-04-21 20:04:56 +05303 * SPDX-License-Identifier: Apache-2.0
Harshit Aghera82d4a622025-04-21 19:09:02 +05304 */
5
Harshit Aghera560e6af2025-04-21 20:04:56 +05306#include "MctpRequester.hpp"
Harshit Agherad837b562025-04-21 19:50:10 +05307#include "Utils.hpp"
8
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +05309#include <NvidiaDeviceDiscovery.hpp>
Harshit Aghera8951c872025-06-25 15:25:33 +053010#include <NvidiaSmaDevice.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053011#include <boost/asio/error.hpp>
Harshit Aghera82d4a622025-04-21 19:09:02 +053012#include <boost/asio/io_context.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053013#include <boost/asio/post.hpp>
14#include <boost/asio/steady_timer.hpp>
15#include <boost/container/flat_map.hpp>
Marc Olberdingd0125c92025-10-08 14:37:19 -070016#include <phosphor-logging/lg2.hpp>
Harshit Aghera82d4a622025-04-21 19:09:02 +053017#include <sdbusplus/asio/connection.hpp>
18#include <sdbusplus/asio/object_server.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053019#include <sdbusplus/bus.hpp>
20#include <sdbusplus/bus/match.hpp>
21#include <sdbusplus/message.hpp>
Harshit Aghera82d4a622025-04-21 19:09:02 +053022
Harshit Agherad837b562025-04-21 19:50:10 +053023#include <array>
24#include <chrono>
Marc Olberdingd0125c92025-10-08 14:37:19 -070025#include <cstdlib>
26#include <exception>
Harshit Agherad837b562025-04-21 19:50:10 +053027#include <functional>
Harshit Aghera82d4a622025-04-21 19:09:02 +053028#include <memory>
29#include <string>
Harshit Agherad837b562025-04-21 19:50:10 +053030#include <vector>
31
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053032boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevices;
Harshit Aghera8951c872025-06-25 15:25:33 +053033boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>> smaDevices;
Harshit Agherad837b562025-04-21 19:50:10 +053034
35void configTimerExpiryCallback(
36 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
37 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Harshit Aghera560e6af2025-04-21 20:04:56 +053038 mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec)
Harshit Agherad837b562025-04-21 19:50:10 +053039{
40 if (ec == boost::asio::error::operation_aborted)
41 {
42 return; // we're being canceled
43 }
Harshit Aghera8951c872025-06-25 15:25:33 +053044 createSensors(io, objectServer, gpuDevices, smaDevices, dbusConnection,
45 mctpRequester);
Harshit Agherad837b562025-04-21 19:50:10 +053046}
Harshit Aghera82d4a622025-04-21 19:09:02 +053047
48int 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");
Rohit PAI0a888262025-06-11 08:52:29 +053054 objectServer.add_manager("/xyz/openbmc_project/inventory");
Harshit Aghera82d4a622025-04-21 19:09:02 +053055 systemBus->request_name("xyz.openbmc_project.GpuSensor");
56
Harshit Aghera560e6af2025-04-21 20:04:56 +053057 mctp::MctpRequester mctpRequester(io);
58
Harshit Agherad837b562025-04-21 19:50:10 +053059 boost::asio::post(io, [&]() {
Harshit Aghera8951c872025-06-25 15:25:33 +053060 createSensors(io, objectServer, gpuDevices, smaDevices, systemBus,
61 mctpRequester);
Harshit Agherad837b562025-04-21 19:50:10 +053062 });
63
64 boost::asio::steady_timer configTimer(io);
65
66 std::function<void(sdbusplus::message_t&)> eventHandler =
Harshit Aghera560e6af2025-04-21 20:04:56 +053067 [&configTimer, &io, &objectServer, &systemBus,
68 &mctpRequester](sdbusplus::message_t&) {
Harshit Agherad837b562025-04-21 19:50:10 +053069 configTimer.expires_after(std::chrono::seconds(1));
70 // create a timer because normally multiple properties change
Harshit Aghera560e6af2025-04-21 20:04:56 +053071 configTimer.async_wait(std::bind_front(
72 configTimerExpiryCallback, std::ref(io), std::ref(objectServer),
73 std::ref(systemBus), std::ref(mctpRequester)));
Harshit Agherad837b562025-04-21 19:50:10 +053074 };
75
76 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
77 setupPropertiesChangedMatches(
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053078 *systemBus, std::to_array<const char*>({deviceType}), eventHandler);
Harshit Agherad837b562025-04-21 19:50:10 +053079
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 sdbusplus::bus::match::rules::interfacesRemovedAtPath(
85 std::string(inventoryPath)),
Harshit Aghera8951c872025-06-25 15:25:33 +053086 [](sdbusplus::message_t& msg) {
87 interfaceRemoved(msg, gpuDevices, smaDevices);
88 });
Harshit Agherad837b562025-04-21 19:50:10 +053089
Marc Olberdingd0125c92025-10-08 14:37:19 -070090 try
91 {
92 io.run();
93 }
94 catch (const std::exception& e)
95 {
96 lg2::error("fatal error caught during processing: {MSG}", "MSG",
97 e.what());
98 return EXIT_FAILURE;
99 }
100
Harshit Aghera82d4a622025-04-21 19:09:02 +0530101 return 0;
102}