blob: 9345b84495e3014bb9406935b62f2eb4f363e019 [file] [log] [blame]
Harshit Aghera2c024682025-04-21 19:09:02 +05301/*
2 * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
3 * AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0
4 */
5
Harshit Agheraa3f24f42025-04-21 20:04:56 +05306#include "MctpRequester.hpp"
7#include "OcpMctpVdm.hpp"
Harshit Agheraacd375a2025-04-21 19:50:10 +05308#include "Utils.hpp"
9
Harshit Aghera11b9c1a2025-04-29 17:34:25 +053010#include <GpuDevice.hpp>
Harshit Agheraacd375a2025-04-21 19:50:10 +053011#include <boost/asio/error.hpp>
Harshit Aghera2c024682025-04-21 19:09:02 +053012#include <boost/asio/io_context.hpp>
Harshit Agheraacd375a2025-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 Aghera2c024682025-04-21 19:09:02 +053016#include <sdbusplus/asio/connection.hpp>
17#include <sdbusplus/asio/object_server.hpp>
Harshit Agheraacd375a2025-04-21 19:50:10 +053018#include <sdbusplus/bus.hpp>
19#include <sdbusplus/bus/match.hpp>
20#include <sdbusplus/message.hpp>
Harshit Aghera2c024682025-04-21 19:09:02 +053021
Harshit Agheraacd375a2025-04-21 19:50:10 +053022#include <array>
23#include <chrono>
24#include <functional>
Harshit Aghera2c024682025-04-21 19:09:02 +053025#include <memory>
26#include <string>
Harshit Agheraacd375a2025-04-21 19:50:10 +053027#include <vector>
28
Harshit Aghera11b9c1a2025-04-29 17:34:25 +053029/**
30 * @brief Global map of GPU devices keyed by their paths
31 * @details Stores all discovered GPU devices in the system for management
32 * and tracking throughout the application lifecycle
33 */
34boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevice;
Harshit Agheraacd375a2025-04-21 19:50:10 +053035
36/**
Harshit Aghera11b9c1a2025-04-29 17:34:25 +053037 * @brief Callback function executed when configuration timer expires
38 * @details Triggers sensor creation or reconfiguration process when
39 * configuration changes are detected in the system. If the timer was canceled
40 * due to application shutdown or other reasons, the function returns early.
41 *
42 * @param io Boost ASIO I/O context for scheduling asynchronous operations
43 * @param objectServer D-Bus object server for exposing sensor interfaces
44 * @param dbusConnection D-Bus connection for system communication
45 * @param mctpRequester MCTP requester for GPU communication protocol
46 * @param ec Boost ASIO error code indicating success or failure reason
Harshit Agheraacd375a2025-04-21 19:50:10 +053047 */
48void configTimerExpiryCallback(
49 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
50 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Harshit Agheraa3f24f42025-04-21 20:04:56 +053051 mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec)
Harshit Agheraacd375a2025-04-21 19:50:10 +053052{
53 if (ec == boost::asio::error::operation_aborted)
54 {
55 return; // we're being canceled
56 }
Harshit Aghera11b9c1a2025-04-29 17:34:25 +053057 createSensors(io, objectServer, gpuDevice, dbusConnection, mctpRequester);
Harshit Agheraacd375a2025-04-21 19:50:10 +053058}
Harshit Aghera2c024682025-04-21 19:09:02 +053059
60int main()
61{
62 boost::asio::io_context io;
63 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
64 sdbusplus::asio::object_server objectServer(systemBus, true);
65 objectServer.add_manager("/xyz/openbmc_project/sensors");
66 systemBus->request_name("xyz.openbmc_project.GpuSensor");
67
Harshit Agheraa3f24f42025-04-21 20:04:56 +053068 mctp::MctpRequester mctpRequester(io,
69 ocp::accelerator_management::messageType);
70
Harshit Agheraacd375a2025-04-21 19:50:10 +053071 boost::asio::post(io, [&]() {
Harshit Aghera11b9c1a2025-04-29 17:34:25 +053072 createSensors(io, objectServer, gpuDevice, systemBus, mctpRequester);
Harshit Agheraacd375a2025-04-21 19:50:10 +053073 });
74
75 boost::asio::steady_timer configTimer(io);
76
77 std::function<void(sdbusplus::message_t&)> eventHandler =
Harshit Agheraa3f24f42025-04-21 20:04:56 +053078 [&configTimer, &io, &objectServer, &systemBus,
79 &mctpRequester](sdbusplus::message_t&) {
Harshit Agheraacd375a2025-04-21 19:50:10 +053080 configTimer.expires_after(std::chrono::seconds(1));
81 // create a timer because normally multiple properties change
Harshit Agheraa3f24f42025-04-21 20:04:56 +053082 configTimer.async_wait(std::bind_front(
83 configTimerExpiryCallback, std::ref(io), std::ref(objectServer),
84 std::ref(systemBus), std::ref(mctpRequester)));
Harshit Agheraacd375a2025-04-21 19:50:10 +053085 };
86
87 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
88 setupPropertiesChangedMatches(
89 *systemBus, std::to_array<const char*>({sensorType}), eventHandler);
90
91 // Watch for entity-manager to remove configuration interfaces
92 // so the corresponding sensors can be removed.
93 auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
94 static_cast<sdbusplus::bus_t&>(*systemBus),
95 "type='signal',member='InterfacesRemoved',arg0path='" +
96 std::string(inventoryPath) + "/'",
Harshit Aghera11b9c1a2025-04-29 17:34:25 +053097 [](sdbusplus::message_t& msg) { interfaceRemoved(msg, gpuDevice); });
Harshit Agheraacd375a2025-04-21 19:50:10 +053098
Harshit Aghera2c024682025-04-21 19:09:02 +053099 io.run();
100 return 0;
101}