182d4a623SHarshit Aghera /*
2b5e823f7SEd Tanous * SPDX-FileCopyrightText: Copyright OpenBMC Authors
3560e6af7SHarshit Aghera * SPDX-License-Identifier: Apache-2.0
482d4a623SHarshit Aghera */
582d4a623SHarshit Aghera
6560e6af7SHarshit Aghera #include "MctpRequester.hpp"
7d837b56cSHarshit Aghera #include "Utils.hpp"
8d837b56cSHarshit Aghera
94ecdfaaaSHarshit Aghera #include <NvidiaDeviceDiscovery.hpp>
10e0b80e1eSHarshit Aghera #include <NvidiaPcieDevice.hpp>
118951c87eSHarshit Aghera #include <NvidiaSmaDevice.hpp>
12d837b56cSHarshit Aghera #include <boost/asio/error.hpp>
1382d4a623SHarshit Aghera #include <boost/asio/io_context.hpp>
14d837b56cSHarshit Aghera #include <boost/asio/post.hpp>
15d837b56cSHarshit Aghera #include <boost/asio/steady_timer.hpp>
16d837b56cSHarshit Aghera #include <boost/container/flat_map.hpp>
17d0125c9cSMarc Olberding #include <phosphor-logging/lg2.hpp>
1882d4a623SHarshit Aghera #include <sdbusplus/asio/connection.hpp>
1982d4a623SHarshit Aghera #include <sdbusplus/asio/object_server.hpp>
20d837b56cSHarshit Aghera #include <sdbusplus/bus.hpp>
21d837b56cSHarshit Aghera #include <sdbusplus/bus/match.hpp>
22d837b56cSHarshit Aghera #include <sdbusplus/message.hpp>
2382d4a623SHarshit Aghera
24d837b56cSHarshit Aghera #include <array>
25d837b56cSHarshit Aghera #include <chrono>
26d0125c9cSMarc Olberding #include <cstdlib>
27d0125c9cSMarc Olberding #include <exception>
28d837b56cSHarshit Aghera #include <functional>
2982d4a623SHarshit Aghera #include <memory>
3082d4a623SHarshit Aghera #include <string>
31d837b56cSHarshit Aghera #include <vector>
32d837b56cSHarshit Aghera
334ecdfaaaSHarshit Aghera boost::container::flat_map<std::string, std::shared_ptr<GpuDevice>> gpuDevices;
348951c87eSHarshit Aghera boost::container::flat_map<std::string, std::shared_ptr<SmaDevice>> smaDevices;
35e0b80e1eSHarshit Aghera boost::container::flat_map<std::string, std::shared_ptr<PcieDevice>>
36e0b80e1eSHarshit Aghera pcieDevices;
37d837b56cSHarshit Aghera
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)38d837b56cSHarshit Aghera void configTimerExpiryCallback(
39d837b56cSHarshit Aghera boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
40d837b56cSHarshit Aghera std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
41560e6af7SHarshit Aghera mctp::MctpRequester& mctpRequester, const boost::system::error_code& ec)
42d837b56cSHarshit Aghera {
43d837b56cSHarshit Aghera if (ec == boost::asio::error::operation_aborted)
44d837b56cSHarshit Aghera {
45d837b56cSHarshit Aghera return; // we're being canceled
46d837b56cSHarshit Aghera }
47e0b80e1eSHarshit Aghera createSensors(io, objectServer, gpuDevices, smaDevices, pcieDevices,
48e0b80e1eSHarshit Aghera dbusConnection, mctpRequester);
49d837b56cSHarshit Aghera }
5082d4a623SHarshit Aghera
main()5182d4a623SHarshit Aghera int main()
5282d4a623SHarshit Aghera {
5382d4a623SHarshit Aghera boost::asio::io_context io;
5482d4a623SHarshit Aghera auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
5582d4a623SHarshit Aghera sdbusplus::asio::object_server objectServer(systemBus, true);
5682d4a623SHarshit Aghera objectServer.add_manager("/xyz/openbmc_project/sensors");
570a88826fSRohit PAI objectServer.add_manager("/xyz/openbmc_project/inventory");
58b341fa2bSHarshit Aghera objectServer.add_manager("/xyz/openbmc_project/software");
59*1180ed47SHarshit Aghera objectServer.add_manager("/xyz/openbmc_project/metric");
6082d4a623SHarshit Aghera systemBus->request_name("xyz.openbmc_project.GpuSensor");
6182d4a623SHarshit Aghera
62560e6af7SHarshit Aghera mctp::MctpRequester mctpRequester(io);
63560e6af7SHarshit Aghera
64d837b56cSHarshit Aghera boost::asio::post(io, [&]() {
65e0b80e1eSHarshit Aghera createSensors(io, objectServer, gpuDevices, smaDevices, pcieDevices,
66e0b80e1eSHarshit Aghera systemBus, mctpRequester);
67d837b56cSHarshit Aghera });
68d837b56cSHarshit Aghera
69d837b56cSHarshit Aghera boost::asio::steady_timer configTimer(io);
70d837b56cSHarshit Aghera
71d837b56cSHarshit Aghera std::function<void(sdbusplus::message_t&)> eventHandler =
72560e6af7SHarshit Aghera [&configTimer, &io, &objectServer, &systemBus,
73560e6af7SHarshit Aghera &mctpRequester](sdbusplus::message_t&) {
74d837b56cSHarshit Aghera configTimer.expires_after(std::chrono::seconds(1));
75d837b56cSHarshit Aghera // create a timer because normally multiple properties change
76560e6af7SHarshit Aghera configTimer.async_wait(std::bind_front(
77560e6af7SHarshit Aghera configTimerExpiryCallback, std::ref(io), std::ref(objectServer),
78560e6af7SHarshit Aghera std::ref(systemBus), std::ref(mctpRequester)));
79d837b56cSHarshit Aghera };
80d837b56cSHarshit Aghera
81d837b56cSHarshit Aghera std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
82d837b56cSHarshit Aghera setupPropertiesChangedMatches(
834ecdfaaaSHarshit Aghera *systemBus, std::to_array<const char*>({deviceType}), eventHandler);
84d837b56cSHarshit Aghera
85d837b56cSHarshit Aghera // Watch for entity-manager to remove configuration interfaces
86d837b56cSHarshit Aghera // so the corresponding sensors can be removed.
87d837b56cSHarshit Aghera auto ifaceRemovedMatch = std::make_shared<sdbusplus::bus::match_t>(
88d837b56cSHarshit Aghera static_cast<sdbusplus::bus_t&>(*systemBus),
89d837b56cSHarshit Aghera sdbusplus::bus::match::rules::interfacesRemovedAtPath(
90d837b56cSHarshit Aghera std::string(inventoryPath)),
918951c87eSHarshit Aghera [](sdbusplus::message_t& msg) {
92e0b80e1eSHarshit Aghera interfaceRemoved(msg, gpuDevices, smaDevices, pcieDevices);
938951c87eSHarshit Aghera });
94d837b56cSHarshit Aghera
95d0125c9cSMarc Olberding try
96d0125c9cSMarc Olberding {
9782d4a623SHarshit Aghera io.run();
98d0125c9cSMarc Olberding }
99d0125c9cSMarc Olberding catch (const std::exception& e)
100d0125c9cSMarc Olberding {
101d0125c9cSMarc Olberding lg2::error("fatal error caught during processing: {MSG}", "MSG",
102d0125c9cSMarc Olberding e.what());
103d0125c9cSMarc Olberding return EXIT_FAILURE;
104d0125c9cSMarc Olberding }
105d0125c9cSMarc Olberding
10682d4a623SHarshit Aghera return 0;
10782d4a623SHarshit Aghera }
108