blob: 1a3b6754126365a788f4ed9d4b4c47b2cb281a8d [file] [log] [blame]
Harshit Aghera82d4a622025-04-21 19:09:02 +05301/*
2 * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
Harshit Aghera560e6af2025-04-21 20:04:56 +05303 * AFFILIATES. All rights reserved.
4 * SPDX-License-Identifier: Apache-2.0
Harshit Aghera82d4a622025-04-21 19:09:02 +05305 */
6
Harshit Aghera560e6af2025-04-21 20:04:56 +05307#include "MctpRequester.hpp"
Harshit Agherad837b562025-04-21 19:50:10 +05308#include "Utils.hpp"
9
Harshit Agherafa2a5b92025-05-22 11:35:39 +053010#include <NvidiaDeviceDiscovery.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>
Harshit Aghera82d4a622025-04-21 19:09:02 +053016#include <sdbusplus/asio/connection.hpp>
17#include <sdbusplus/asio/object_server.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053018#include <sdbusplus/bus.hpp>
19#include <sdbusplus/bus/match.hpp>
20#include <sdbusplus/message.hpp>
Harshit Aghera82d4a622025-04-21 19:09:02 +053021
Harshit Agherad837b562025-04-21 19:50:10 +053022#include <array>
23#include <chrono>
24#include <functional>
Harshit Aghera82d4a622025-04-21 19:09:02 +053025#include <memory>
26#include <string>
Harshit Agherad837b562025-04-21 19:50:10 +053027#include <vector>
28
Harshit Agherafa2a5b92025-05-22 11:35:39 +053029boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevices;
Harshit Agherad837b562025-04-21 19:50:10 +053030
31void configTimerExpiryCallback(
32 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
33 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Harshit Aghera560e6af2025-04-21 20:04:56 +053034 mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec)
Harshit Agherad837b562025-04-21 19:50:10 +053035{
36 if (ec == boost::asio::error::operation_aborted)
37 {
38 return; // we're being canceled
39 }
Harshit Agherafa2a5b92025-05-22 11:35:39 +053040 createSensors(io, objectServer, gpuDevices, dbusConnection, mctpRequester);
Harshit Agherad837b562025-04-21 19:50:10 +053041}
Harshit Aghera82d4a622025-04-21 19:09:02 +053042
43int 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");
Rohit PAId87bf7f2025-06-11 08:52:29 +053049 objectServer.add_manager("/xyz/openbmc_project/inventory");
Harshit Aghera82d4a622025-04-21 19:09:02 +053050 systemBus->request_name("xyz.openbmc_project.GpuSensor");
51
Harshit Aghera560e6af2025-04-21 20:04:56 +053052 mctp::MctpRequester mctpRequester(io);
53
Harshit Agherad837b562025-04-21 19:50:10 +053054 boost::asio::post(io, [&]() {
Harshit Agherafa2a5b92025-05-22 11:35:39 +053055 createSensors(io, objectServer, gpuDevices, systemBus, mctpRequester);
Harshit Agherad837b562025-04-21 19:50:10 +053056 });
57
58 boost::asio::steady_timer configTimer(io);
59
60 std::function<void(sdbusplus::message_t&)> eventHandler =
Harshit Aghera560e6af2025-04-21 20:04:56 +053061 [&configTimer, &io, &objectServer, &systemBus,
62 &mctpRequester](sdbusplus::message_t&) {
Harshit Agherad837b562025-04-21 19:50:10 +053063 configTimer.expires_after(std::chrono::seconds(1));
64 // create a timer because normally multiple properties change
Harshit Aghera560e6af2025-04-21 20:04:56 +053065 configTimer.async_wait(std::bind_front(
66 configTimerExpiryCallback, std::ref(io), std::ref(objectServer),
67 std::ref(systemBus), std::ref(mctpRequester)));
Harshit Agherad837b562025-04-21 19:50:10 +053068 };
69
70 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
71 setupPropertiesChangedMatches(
Harshit Agherafa2a5b92025-05-22 11:35:39 +053072 *systemBus, std::to_array<const char*>({deviceType}), eventHandler);
Harshit Agherad837b562025-04-21 19:50:10 +053073
74 // Watch for entity-manager to remove configuration interfaces
75 // so the corresponding sensors can be removed.
76 auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
77 static_cast<sdbusplus::bus_t&>(*systemBus),
78 sdbusplus::bus::match::rules::interfacesRemovedAtPath(
79 std::string(inventoryPath)),
Harshit Agherafa2a5b92025-05-22 11:35:39 +053080 [](sdbusplus::message_t& msg) { interfaceRemoved(msg, gpuDevices); });
Harshit Agherad837b562025-04-21 19:50:10 +053081
Harshit Aghera82d4a622025-04-21 19:09:02 +053082 io.run();
83 return 0;
84}