blob: 9879c2cc3d62611fedde305fb7b325cec3e4e567 [file] [log] [blame]
Harshit Aghera82d4a622025-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 Agherad837b562025-04-21 19:50:10 +05306#include "NvidiaGpuSensor.hpp"
7#include "Utils.hpp"
8
9#include <boost/asio/error.hpp>
Harshit Aghera82d4a622025-04-21 19:09:02 +053010#include <boost/asio/io_context.hpp>
Harshit Agherad837b562025-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 Aghera82d4a622025-04-21 19:09:02 +053015#include <sdbusplus/asio/connection.hpp>
16#include <sdbusplus/asio/object_server.hpp>
Harshit Agherad837b562025-04-21 19:50:10 +053017#include <sdbusplus/bus.hpp>
18#include <sdbusplus/bus/match.hpp>
19#include <sdbusplus/message.hpp>
Harshit Aghera82d4a622025-04-21 19:09:02 +053020
Harshit Agherad837b562025-04-21 19:50:10 +053021#include <array>
22#include <chrono>
23#include <functional>
Harshit Aghera82d4a622025-04-21 19:09:02 +053024#include <memory>
25#include <string>
Harshit Agherad837b562025-04-21 19:50:10 +053026#include <vector>
27
28boost::container::flat_map<std::string, std::shared_ptr<GpuTempSensor>> sensors;
29
30void configTimerExpiryCallback(
31 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
32 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
33 const boost::system::error_code& ec)
34{
35 if (ec == boost::asio::error::operation_aborted)
36 {
37 return; // we're being canceled
38 }
39 createSensors(io, objectServer, sensors, dbusConnection);
40 if (sensors.empty())
41 {
42 lg2::info("Configuration not detected");
43 }
44}
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 Agherad837b562025-04-21 19:50:10 +053054 boost::asio::post(io, [&]() {
55 createSensors(io, objectServer, sensors, systemBus);
56 });
57
58 boost::asio::steady_timer configTimer(io);
59
60 std::function<void(sdbusplus::message_t&)> eventHandler =
61 [&configTimer, &io, &objectServer, &systemBus](sdbusplus::message_t&) {
62 configTimer.expires_after(std::chrono::seconds(1));
63 // create a timer because normally multiple properties change
64 configTimer.async_wait(
65 std::bind_front(configTimerExpiryCallback, std::ref(io),
66 std::ref(objectServer), std::ref(systemBus)));
67 };
68
69 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
70 setupPropertiesChangedMatches(
71 *systemBus, std::to_array<const char*>({sensorType}), eventHandler);
72
73 // Watch for entity-manager to remove configuration interfaces
74 // so the corresponding sensors can be removed.
75 auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
76 static_cast<sdbusplus::bus_t&>(*systemBus),
77 sdbusplus::bus::match::rules::interfacesRemovedAtPath(
78 std::string(inventoryPath)),
79 [](sdbusplus::message_t& msg) { interfaceRemoved(msg, sensors); });
80
Harshit Aghera82d4a622025-04-21 19:09:02 +053081 io.run();
82 return 0;
83}