blob: 4c372d6c1a205492d2dae53d88f5d215b9b1e445 [file] [log] [blame]
Nikhil Potadeb669b6b2019-03-13 10:52:21 -07001/*
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 Jefferye73bd0a2023-01-25 10:39:57 +103017#include "NVMeSensor.hpp"
James Feist38fb5982020-05-28 10:09:54 -070018
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#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 Potadeb669b6b2019-03-13 10:52:21 -070035
36static constexpr double maxReading = 127;
37static constexpr double minReading = 0;
38
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070039NVMeSensor::NVMeSensor(sdbusplus::asio::object_server& objectServer,
Ed Tanous1f978632023-02-28 18:16:39 -080040 boost::asio::io_context& /*unused*/,
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070041 std::shared_ptr<sdbusplus::asio::connection>& conn,
42 const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080043 std::vector<thresholds::Threshold>&& thresholdsIn,
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070044 const std::string& sensorConfiguration,
Nnamdi Ajah06cd9882023-02-15 13:21:32 +010045 const int busNumber, const uint8_t slaveAddr) :
Zhikui Renda98f092021-11-01 09:41:08 -070046 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Zev Weiss054aad82022-08-18 01:37:34 -070047 NVMeSensor::sensorType, false, false, maxReading, minReading, conn,
Andrew Jeffery0c23fc32021-12-08 16:48:30 +103048 PowerState::on),
Nnamdi Ajah06cd9882023-02-15 13:21:32 +010049 bus(busNumber), address(slaveAddr), objServer(objectServer)
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070050{
Andrew Jeffery25e20bd2022-03-15 22:26:04 +103051 if (bus < 0)
52 {
53 throw std::invalid_argument("Invalid bus: Bus ID must not be negative");
54 }
55
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070056 sensorInterface = objectServer.add_interface(
57 "/xyz/openbmc_project/sensors/temperature/" + name,
58 "xyz.openbmc_project.Sensor.Value");
59
Jayashree Dhanapal56678082022-01-04 17:27:20 +053060 for (const auto& threshold : thresholds)
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070061 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053062 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 Potadeb669b6b2019-03-13 10:52:21 -070066 }
67 association = objectServer.add_interface(
68 "/xyz/openbmc_project/sensors/temperature/" + name,
69 association::interface);
70
Andrei Kartashev39287412022-02-04 16:04:47 +030071 setInitialProperties(sensor_paths::unitDegreesC);
Patrick Rudolph4d970582023-10-16 17:06:54 +020072 // Mark as unavailable until the first packet has been received over NVMe
73 // MI.
74 markAvailable(false);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070075}
76
77NVMeSensor::~NVMeSensor()
78{
79 // close the input dev to cancel async operations
Jayashree Dhanapal56678082022-01-04 17:27:20 +053080 for (const auto& iface : thresholdInterfaces)
81 {
82 objServer.remove_interface(iface);
83 }
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070084 objServer.remove_interface(sensorInterface);
85 objServer.remove_interface(association);
86}
87
Andrew Jeffery14108bb2022-03-21 15:00:16 +103088bool 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 Tanous201a1012024-04-03 18:07:28 -0700103void NVMeSensor::checkThresholds()
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700104{
Nikhil Potadeb669b6b2019-03-13 10:52:21 -0700105 thresholds::checkThresholds(this);
106}