Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 1 | /* |
| 2 | // Copyright (c) 2019 Intel Corporation |
| 3 | // |
| 4 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | // you may not use this file except in compliance with the License. |
| 6 | // You may obtain a copy of the License at |
| 7 | // |
| 8 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | // |
| 10 | // Unless required by applicable law or agreed to in writing, software |
| 11 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | // See the License for the specific language governing permissions and |
| 14 | // limitations under the License. |
| 15 | */ |
| 16 | |
Andrew Jeffery | e73bd0a | 2023-01-25 10:39:57 +1030 | [diff] [blame] | 17 | #include "NVMeSensor.hpp" |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 18 | |
Ed Tanous | eacbfdd | 2024-04-04 12:00:24 -0700 | [diff] [blame] | 19 | #include "SensorPaths.hpp" |
| 20 | #include "Thresholds.hpp" |
| 21 | #include "Utils.hpp" |
| 22 | #include "sensor.hpp" |
| 23 | |
| 24 | #include <boost/asio/io_context.hpp> |
| 25 | #include <sdbusplus/asio/connection.hpp> |
| 26 | #include <sdbusplus/asio/object_server.hpp> |
| 27 | |
| 28 | #include <cstddef> |
| 29 | #include <cstdint> |
| 30 | #include <memory> |
| 31 | #include <stdexcept> |
| 32 | #include <string> |
| 33 | #include <utility> |
| 34 | #include <vector> |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 35 | |
| 36 | static constexpr double maxReading = 127; |
| 37 | static constexpr double minReading = 0; |
| 38 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 39 | NVMeSensor::NVMeSensor(sdbusplus::asio::object_server& objectServer, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 40 | boost::asio::io_context& /*unused*/, |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 41 | std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 42 | const std::string& sensorName, |
Jeff Lin | 7b7a9de | 2021-02-22 11:16:27 +0800 | [diff] [blame] | 43 | std::vector<thresholds::Threshold>&& thresholdsIn, |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 44 | const std::string& sensorConfiguration, |
Nnamdi Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 45 | const int busNumber, const uint8_t slaveAddr) : |
Zhikui Ren | da98f09 | 2021-11-01 09:41:08 -0700 | [diff] [blame] | 46 | Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration, |
Zev Weiss | 054aad8 | 2022-08-18 01:37:34 -0700 | [diff] [blame] | 47 | NVMeSensor::sensorType, false, false, maxReading, minReading, conn, |
Andrew Jeffery | 0c23fc3 | 2021-12-08 16:48:30 +1030 | [diff] [blame] | 48 | PowerState::on), |
Nnamdi Ajah | 06cd988 | 2023-02-15 13:21:32 +0100 | [diff] [blame] | 49 | bus(busNumber), address(slaveAddr), objServer(objectServer) |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 50 | { |
Andrew Jeffery | 25e20bd | 2022-03-15 22:26:04 +1030 | [diff] [blame] | 51 | if (bus < 0) |
| 52 | { |
| 53 | throw std::invalid_argument("Invalid bus: Bus ID must not be negative"); |
| 54 | } |
| 55 | |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 56 | sensorInterface = objectServer.add_interface( |
| 57 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 58 | "xyz.openbmc_project.Sensor.Value"); |
| 59 | |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 60 | for (const auto& threshold : thresholds) |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 61 | { |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 62 | std::string interface = thresholds::getInterface(threshold.level); |
| 63 | thresholdInterfaces[static_cast<size_t>(threshold.level)] = |
| 64 | objectServer.add_interface( |
| 65 | "/xyz/openbmc_project/sensors/temperature/" + name, interface); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 66 | } |
| 67 | association = objectServer.add_interface( |
| 68 | "/xyz/openbmc_project/sensors/temperature/" + name, |
| 69 | association::interface); |
| 70 | |
Andrei Kartashev | 3928741 | 2022-02-04 16:04:47 +0300 | [diff] [blame] | 71 | setInitialProperties(sensor_paths::unitDegreesC); |
Patrick Rudolph | 4d97058 | 2023-10-16 17:06:54 +0200 | [diff] [blame] | 72 | // Mark as unavailable until the first packet has been received over NVMe |
| 73 | // MI. |
| 74 | markAvailable(false); |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | NVMeSensor::~NVMeSensor() |
| 78 | { |
| 79 | // close the input dev to cancel async operations |
Jayashree Dhanapal | 5667808 | 2022-01-04 17:27:20 +0530 | [diff] [blame] | 80 | for (const auto& iface : thresholdInterfaces) |
| 81 | { |
| 82 | objServer.remove_interface(iface); |
| 83 | } |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 84 | objServer.remove_interface(sensorInterface); |
| 85 | objServer.remove_interface(association); |
| 86 | } |
| 87 | |
Andrew Jeffery | 14108bb | 2022-03-21 15:00:16 +1030 | [diff] [blame] | 88 | bool NVMeSensor::sample() |
| 89 | { |
| 90 | if (inError()) |
| 91 | { |
| 92 | if (scanDelay == 0) |
| 93 | { |
| 94 | scanDelay = scanDelayTicks; |
| 95 | } |
| 96 | |
| 97 | scanDelay--; |
| 98 | } |
| 99 | |
| 100 | return scanDelay == 0; |
| 101 | } |
| 102 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 103 | void NVMeSensor::checkThresholds() |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 104 | { |
Nikhil Potade | b669b6b | 2019-03-13 10:52:21 -0700 | [diff] [blame] | 105 | thresholds::checkThresholds(this); |
| 106 | } |