| Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 1 | /* | 
|  | 2 | * SPDX-FileCopyrightText: Copyright (c) 2024-2025 NVIDIA CORPORATION & | 
|  | 3 | * AFFILIATES. All rights reserved. | 
|  | 4 | * SPDX-License-Identifier: Apache-2.0 | 
|  | 5 | */ | 
|  | 6 |  | 
|  | 7 | #include "NvidiaSmaDevice.hpp" | 
|  | 8 |  | 
|  | 9 | #include "NvidiaDeviceDiscovery.hpp" | 
|  | 10 | #include "NvidiaGpuSensor.hpp" | 
|  | 11 | #include "Thresholds.hpp" | 
|  | 12 | #include "Utils.hpp" | 
|  | 13 |  | 
|  | 14 | #include <bits/basic_string.h> | 
|  | 15 |  | 
|  | 16 | #include <MctpRequester.hpp> | 
|  | 17 | #include <boost/asio/io_context.hpp> | 
|  | 18 | #include <phosphor-logging/lg2.hpp> | 
|  | 19 | #include <sdbusplus/asio/connection.hpp> | 
|  | 20 | #include <sdbusplus/asio/object_server.hpp> | 
|  | 21 |  | 
|  | 22 | #include <chrono> | 
|  | 23 | #include <cstdint> | 
|  | 24 | #include <memory> | 
|  | 25 | #include <string> | 
|  | 26 | #include <vector> | 
|  | 27 |  | 
|  | 28 | SmaDevice::SmaDevice(const SensorConfigs& configs, const std::string& name, | 
|  | 29 | const std::string& path, | 
|  | 30 | const std::shared_ptr<sdbusplus::asio::connection>& conn, | 
|  | 31 | uint8_t eid, boost::asio::io_context& io, | 
|  | 32 | mctp::MctpRequester& mctpRequester, | 
|  | 33 | sdbusplus::asio::object_server& objectServer) : | 
|  | 34 | eid(eid), sensorPollMs(std::chrono::milliseconds{configs.pollRate}), | 
|  | 35 | waitTimer(io, std::chrono::steady_clock::duration(0)), | 
|  | 36 | mctpRequester(mctpRequester), conn(conn), objectServer(objectServer), | 
|  | 37 | configs(configs), name(escapeName(name)), path(path) | 
| Marc Olberding | fd4a377 | 2025-09-24 16:31:02 -0700 | [diff] [blame^] | 38 | {} | 
|  | 39 |  | 
|  | 40 | void SmaDevice::init() | 
| Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 41 | { | 
|  | 42 | makeSensors(); | 
|  | 43 | } | 
|  | 44 |  | 
|  | 45 | void SmaDevice::makeSensors() | 
|  | 46 | { | 
|  | 47 | tempSensor = std::make_shared<NvidiaGpuTempSensor>( | 
|  | 48 | conn, mctpRequester, name + "_TEMP_0", path, eid, smaTempSensorId, | 
|  | 49 | objectServer, std::vector<thresholds::Threshold>{}); | 
|  | 50 |  | 
|  | 51 | lg2::info("Added MCA {NAME} Sensors with chassis path: {PATH}.", "NAME", | 
|  | 52 | name, "PATH", path); | 
|  | 53 |  | 
|  | 54 | read(); | 
|  | 55 | } | 
|  | 56 |  | 
|  | 57 | void SmaDevice::read() | 
|  | 58 | { | 
|  | 59 | tempSensor->update(); | 
|  | 60 |  | 
|  | 61 | waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs)); | 
| Marc Olberding | fd4a377 | 2025-09-24 16:31:02 -0700 | [diff] [blame^] | 62 | waitTimer.async_wait( | 
|  | 63 | [weak{weak_from_this()}](const boost::system::error_code& ec) { | 
|  | 64 | std::shared_ptr<SmaDevice> self = weak.lock(); | 
|  | 65 | if (!self) | 
|  | 66 | { | 
|  | 67 | lg2::error("Invalid SmaDevice reference"); | 
|  | 68 | return; | 
|  | 69 | } | 
|  | 70 | if (ec) | 
|  | 71 | { | 
|  | 72 | return; | 
|  | 73 | } | 
|  | 74 | self->read(); | 
|  | 75 | }); | 
| Harshit Aghera | 8951c87 | 2025-06-25 15:25:33 +0530 | [diff] [blame] | 76 | } |