blob: 9345b84495e3014bb9406935b62f2eb4f363e019 [file] [log] [blame]
/*
* SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION &
* AFFILIATES. All rights reserved. SPDX-License-Identifier: Apache-2.0
*/
#include "MctpRequester.hpp"
#include "OcpMctpVdm.hpp"
#include "Utils.hpp"
#include <GpuDevice.hpp>
#include <boost/asio/error.hpp>
#include <boost/asio/io_context.hpp>
#include <boost/asio/post.hpp>
#include <boost/asio/steady_timer.hpp>
#include <boost/container/flat_map.hpp>
#include <sdbusplus/asio/connection.hpp>
#include <sdbusplus/asio/object_server.hpp>
#include <sdbusplus/bus.hpp>
#include <sdbusplus/bus/match.hpp>
#include <sdbusplus/message.hpp>
#include <array>
#include <chrono>
#include <functional>
#include <memory>
#include <string>
#include <vector>
/**
* @brief Global map of GPU devices keyed by their paths
* @details Stores all discovered GPU devices in the system for management
* and tracking throughout the application lifecycle
*/
boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevice;
/**
* @brief Callback function executed when configuration timer expires
* @details Triggers sensor creation or reconfiguration process when
* configuration changes are detected in the system. If the timer was canceled
* due to application shutdown or other reasons, the function returns early.
*
* @param io Boost ASIO I/O context for scheduling asynchronous operations
* @param objectServer D-Bus object server for exposing sensor interfaces
* @param dbusConnection D-Bus connection for system communication
* @param mctpRequester MCTP requester for GPU communication protocol
* @param ec Boost ASIO error code indicating success or failure reason
*/
void configTimerExpiryCallback(
boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec)
{
if (ec == boost::asio::error::operation_aborted)
{
return; // we're being canceled
}
createSensors(io, objectServer, gpuDevice, dbusConnection, mctpRequester);
}
int main()
{
boost::asio::io_context io;
auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
sdbusplus::asio::object_server objectServer(systemBus, true);
objectServer.add_manager("/xyz/openbmc_project/sensors");
systemBus->request_name("xyz.openbmc_project.GpuSensor");
mctp::MctpRequester mctpRequester(io,
ocp::accelerator_management::messageType);
boost::asio::post(io, [&]() {
createSensors(io, objectServer, gpuDevice, systemBus, mctpRequester);
});
boost::asio::steady_timer configTimer(io);
std::function<void(sdbusplus::message_t&)> eventHandler =
[&configTimer, &io, &objectServer, &systemBus,
&mctpRequester](sdbusplus::message_t&) {
configTimer.expires_after(std::chrono::seconds(1));
// create a timer because normally multiple properties change
configTimer.async_wait(std::bind_front(
configTimerExpiryCallback, std::ref(io), std::ref(objectServer),
std::ref(systemBus), std::ref(mctpRequester)));
};
std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
setupPropertiesChangedMatches(
*systemBus, std::to_array<const char*>({sensorType}), eventHandler);
// Watch for entity-manager to remove configuration interfaces
// so the corresponding sensors can be removed.
auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
static_cast<sdbusplus::bus_t&>(*systemBus),
"type='signal',member='InterfacesRemoved',arg0path='" +
std::string(inventoryPath) + "/'",
[](sdbusplus::message_t& msg) { interfaceRemoved(msg, gpuDevice); });
io.run();
return 0;
}