blob: a7b1d7f479c8f2125eee3101f4c45321ab7507ec [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"
7#include "Utils.hpp"
8
9#include <boost/asio/error.hpp>
Harshit Aghera2c024682025-04-21 19:09:02 +053010#include <boost/asio/io_context.hpp>
Harshit Agheraacd375a2025-04-21 19:50:10 +053011#include <boost/asio/post.hpp>
12#include <boost/asio/steady_timer.hpp>
13#include <boost/container/flat_map.hpp>
14#include <phosphor-logging/lg2.hpp>
Harshit Aghera2c024682025-04-21 19:09:02 +053015#include <sdbusplus/asio/connection.hpp>
16#include <sdbusplus/asio/object_server.hpp>
Harshit Agheraacd375a2025-04-21 19:50:10 +053017#include <sdbusplus/bus.hpp>
18#include <sdbusplus/bus/match.hpp>
19#include <sdbusplus/message.hpp>
Harshit Aghera2c024682025-04-21 19:09:02 +053020
Harshit Agheraacd375a2025-04-21 19:50:10 +053021#include <array>
22#include <chrono>
23#include <functional>
Harshit Aghera2c024682025-04-21 19:09:02 +053024#include <memory>
25#include <string>
Harshit Agheraacd375a2025-04-21 19:50:10 +053026#include <vector>
27
28boost::container::flat_map<std::string, std::shared_ptr<GpuTempSensor>> sensors;
29
30/**
31 * @brief config timer expiry callback
32 * @param io Boost ASIO I/O context
33 * @param objectServer D-Bus object server
34 * @param dbusConnection D-Bus connection
35 * @param ec Boost ASIO error code
36 */
37void configTimerExpiryCallback(
38 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
39 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
40 const boost::system::error_code& ec)
41{
42 if (ec == boost::asio::error::operation_aborted)
43 {
44 return; // we're being canceled
45 }
46 createSensors(io, objectServer, sensors, dbusConnection);
47 if (sensors.empty())
48 {
49 lg2::info("Configuration not detected");
50 }
51}
Harshit Aghera2c024682025-04-21 19:09:02 +053052
53int main()
54{
55 boost::asio::io_context io;
56 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
57 sdbusplus::asio::object_server objectServer(systemBus, true);
58 objectServer.add_manager("/xyz/openbmc_project/sensors");
59 systemBus->request_name("xyz.openbmc_project.GpuSensor");
60
Harshit Agheraacd375a2025-04-21 19:50:10 +053061 boost::asio::post(io, [&]() {
62 createSensors(io, objectServer, sensors, systemBus);
63 });
64
65 boost::asio::steady_timer configTimer(io);
66
67 std::function<void(sdbusplus::message_t&)> eventHandler =
68 [&configTimer, &io, &objectServer, &systemBus](sdbusplus::message_t&) {
69 configTimer.expires_after(std::chrono::seconds(1));
70 // create a timer because normally multiple properties change
71 configTimer.async_wait(
72 std::bind_front(configTimerExpiryCallback, std::ref(io),
73 std::ref(objectServer), std::ref(systemBus)));
74 };
75
76 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
77 setupPropertiesChangedMatches(
78 *systemBus, std::to_array<const char*>({sensorType}), eventHandler);
79
80 // Watch for entity-manager to remove configuration interfaces
81 // so the corresponding sensors can be removed.
82 auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
83 static_cast<sdbusplus::bus_t&>(*systemBus),
84 "type='signal',member='InterfacesRemoved',arg0path='" +
85 std::string(inventoryPath) + "/'",
86 [](sdbusplus::message_t& msg) { interfaceRemoved(msg, sensors); });
87
Harshit Aghera2c024682025-04-21 19:09:02 +053088 io.run();
89 return 0;
90}