blob: 254a11a41a98d86791cebb3952b9d617e466301b [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 Agheraacd375a2025-04-21 19:50:10 +05306#include "GpuSensor.hpp"
Harshit Agheraa3f24f42025-04-21 20:04:56 +05307#include "MctpRequester.hpp"
8#include "OcpMctpVdm.hpp"
Harshit Agheraacd375a2025-04-21 19:50:10 +05309#include "Utils.hpp"
10
11#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>
16#include <phosphor-logging/lg2.hpp>
Harshit Aghera2c024682025-04-21 19:09:02 +053017#include <sdbusplus/asio/connection.hpp>
18#include <sdbusplus/asio/object_server.hpp>
Harshit Agheraacd375a2025-04-21 19:50:10 +053019#include <sdbusplus/bus.hpp>
20#include <sdbusplus/bus/match.hpp>
21#include <sdbusplus/message.hpp>
Harshit Aghera2c024682025-04-21 19:09:02 +053022
Harshit Agheraacd375a2025-04-21 19:50:10 +053023#include <array>
24#include <chrono>
25#include <functional>
Harshit Aghera2c024682025-04-21 19:09:02 +053026#include <memory>
27#include <string>
Harshit Agheraacd375a2025-04-21 19:50:10 +053028#include <vector>
29
30boost::container::flat_map<std::string, std::shared_ptr<GpuTempSensor>> sensors;
31
32/**
33 * @brief config timer expiry callback
34 * @param io Boost ASIO I/O context
35 * @param objectServer D-Bus object server
36 * @param dbusConnection D-Bus connection
Harshit Agheraa3f24f42025-04-21 20:04:56 +053037 * @param mctpRequester MCTP requester for GPU communication
Harshit Agheraacd375a2025-04-21 19:50:10 +053038 * @param ec Boost ASIO error code
39 */
40void configTimerExpiryCallback(
41 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
42 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
Harshit Agheraa3f24f42025-04-21 20:04:56 +053043 mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec)
Harshit Agheraacd375a2025-04-21 19:50:10 +053044{
45 if (ec == boost::asio::error::operation_aborted)
46 {
47 return; // we're being canceled
48 }
Harshit Agheraa3f24f42025-04-21 20:04:56 +053049 createSensors(io, objectServer, sensors, dbusConnection, mctpRequester);
Harshit Agheraacd375a2025-04-21 19:50:10 +053050 if (sensors.empty())
51 {
52 lg2::info("Configuration not detected");
53 }
54}
Harshit Aghera2c024682025-04-21 19:09:02 +053055
56int main()
57{
58 boost::asio::io_context io;
59 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
60 sdbusplus::asio::object_server objectServer(systemBus, true);
61 objectServer.add_manager("/xyz/openbmc_project/sensors");
62 systemBus->request_name("xyz.openbmc_project.GpuSensor");
63
Harshit Agheraa3f24f42025-04-21 20:04:56 +053064 mctp::MctpRequester mctpRequester(io,
65 ocp::accelerator_management::messageType);
66
Harshit Agheraacd375a2025-04-21 19:50:10 +053067 boost::asio::post(io, [&]() {
Harshit Agheraa3f24f42025-04-21 20:04:56 +053068 createSensors(io, objectServer, sensors, systemBus, mctpRequester);
Harshit Agheraacd375a2025-04-21 19:50:10 +053069 });
70
71 boost::asio::steady_timer configTimer(io);
72
73 std::function<void(sdbusplus::message_t&)> eventHandler =
Harshit Agheraa3f24f42025-04-21 20:04:56 +053074 [&configTimer, &io, &objectServer, &systemBus,
75 &mctpRequester](sdbusplus::message_t&) {
Harshit Agheraacd375a2025-04-21 19:50:10 +053076 configTimer.expires_after(std::chrono::seconds(1));
77 // create a timer because normally multiple properties change
Harshit Agheraa3f24f42025-04-21 20:04:56 +053078 configTimer.async_wait(std::bind_front(
79 configTimerExpiryCallback, std::ref(io), std::ref(objectServer),
80 std::ref(systemBus), std::ref(mctpRequester)));
Harshit Agheraacd375a2025-04-21 19:50:10 +053081 };
82
83 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
84 setupPropertiesChangedMatches(
85 *systemBus, std::to_array<const char*>({sensorType}), eventHandler);
86
87 // Watch for entity-manager to remove configuration interfaces
88 // so the corresponding sensors can be removed.
89 auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
90 static_cast<sdbusplus::bus_t&>(*systemBus),
91 "type='signal',member='InterfacesRemoved',arg0path='" +
92 std::string(inventoryPath) + "/'",
93 [](sdbusplus::message_t& msg) { interfaceRemoved(msg, sensors); });
94
Harshit Aghera2c024682025-04-21 19:09:02 +053095 io.run();
96 return 0;
97}