blob: 7e404f510de1e2f01f904c895512b2568449b39e [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 Aghera4ecdfaa2025-05-22 11:35:39 +053010#include <NvidiaDeviceDiscovery.hpp>
Harshit Aghera8951c872025-06-25 15:25:33 +053011#include <NvidiaSmaDevice.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053012#include <boost/asio/error.hpp>
Harshit Aghera82d4a622025-04-21 19:09:02 +053013#include <boost/asio/io_context.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053014#include <boost/asio/post.hpp>
15#include <boost/asio/steady_timer.hpp>
16#include <boost/container/flat_map.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>
25#include <functional>
Harshit Aghera82d4a622025-04-21 19:09:02 +053026#include <memory>
27#include <string>
Harshit Agherad837b562025-04-21 19:50:10 +053028#include <vector>
29
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053030boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevices;
Harshit Aghera8951c872025-06-25 15:25:33 +053031boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>> smaDevices;
Harshit Agherad837b562025-04-21 19:50:10 +053032
33void configTimerExpiryCallback(
34 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
35 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Harshit Aghera560e6af2025-04-21 20:04:56 +053036 mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec)
Harshit Agherad837b562025-04-21 19:50:10 +053037{
38 if (ec == boost::asio::error::operation_aborted)
39 {
40 return; // we're being canceled
41 }
Harshit Aghera8951c872025-06-25 15:25:33 +053042 createSensors(io, objectServer, gpuDevices, smaDevices, dbusConnection,
43 mctpRequester);
Harshit Agherad837b562025-04-21 19:50:10 +053044}
Harshit Aghera82d4a622025-04-21 19:09:02 +053045
46int main()
47{
48 boost::asio::io_context io;
49 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
50 sdbusplus::asio::object_server objectServer(systemBus, true);
51 objectServer.add_manager("/xyz/openbmc_project/sensors");
52 systemBus->request_name("xyz.openbmc_project.GpuSensor");
53
Harshit Aghera560e6af2025-04-21 20:04:56 +053054 mctp::MctpRequester mctpRequester(io);
55
Harshit Agherad837b562025-04-21 19:50:10 +053056 boost::asio::post(io, [&]() {
Harshit Aghera8951c872025-06-25 15:25:33 +053057 createSensors(io, objectServer, gpuDevices, smaDevices, systemBus,
58 mctpRequester);
Harshit Agherad837b562025-04-21 19:50:10 +053059 });
60
61 boost::asio::steady_timer configTimer(io);
62
63 std::function<void(sdbusplus::message_t&)> eventHandler =
Harshit Aghera560e6af2025-04-21 20:04:56 +053064 [&configTimer, &io, &objectServer, &systemBus,
65 &mctpRequester](sdbusplus::message_t&) {
Harshit Agherad837b562025-04-21 19:50:10 +053066 configTimer.expires_after(std::chrono::seconds(1));
67 // create a timer because normally multiple properties change
Harshit Aghera560e6af2025-04-21 20:04:56 +053068 configTimer.async_wait(std::bind_front(
69 configTimerExpiryCallback, std::ref(io), std::ref(objectServer),
70 std::ref(systemBus), std::ref(mctpRequester)));
Harshit Agherad837b562025-04-21 19:50:10 +053071 };
72
73 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
74 setupPropertiesChangedMatches(
Harshit Aghera4ecdfaa2025-05-22 11:35:39 +053075 *systemBus, std::to_array<const char*>({deviceType}), eventHandler);
Harshit Agherad837b562025-04-21 19:50:10 +053076
77 // Watch for entity-manager to remove configuration interfaces
78 // so the corresponding sensors can be removed.
79 auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
80 static_cast<sdbusplus::bus_t&>(*systemBus),
81 sdbusplus::bus::match::rules::interfacesRemovedAtPath(
82 std::string(inventoryPath)),
Harshit Aghera8951c872025-06-25 15:25:33 +053083 [](sdbusplus::message_t& msg) {
84 interfaceRemoved(msg, gpuDevices, smaDevices);
85 });
Harshit Agherad837b562025-04-21 19:50:10 +053086
Harshit Aghera82d4a622025-04-21 19:09:02 +053087 io.run();
88 return 0;
89}