blob: e11fae7c26f3f06094b40b98f5d5dabaec007dcf [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
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070019#include <iostream>
20
21static constexpr double maxReading = 127;
22static constexpr double minReading = 0;
23
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070024NVMeSensor::NVMeSensor(sdbusplus::asio::object_server& objectServer,
Ed Tanous1f978632023-02-28 18:16:39 -080025 boost::asio::io_context& /*unused*/,
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070026 std::shared_ptr<sdbusplus::asio::connection>& conn,
27 const std::string& sensorName,
Jeff Lin7b7a9de2021-02-22 11:16:27 +080028 std::vector<thresholds::Threshold>&& thresholdsIn,
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070029 const std::string& sensorConfiguration,
30 const int busNumber) :
Zhikui Renda98f092021-11-01 09:41:08 -070031 Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
Zev Weiss054aad82022-08-18 01:37:34 -070032 NVMeSensor::sensorType, false, false, maxReading, minReading, conn,
Andrew Jeffery0c23fc32021-12-08 16:48:30 +103033 PowerState::on),
Ed Tanousb429f312022-06-27 16:09:53 -070034 bus(busNumber), objServer(objectServer)
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070035{
Andrew Jeffery25e20bd2022-03-15 22:26:04 +103036 if (bus < 0)
37 {
38 throw std::invalid_argument("Invalid bus: Bus ID must not be negative");
39 }
40
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070041 sensorInterface = objectServer.add_interface(
42 "/xyz/openbmc_project/sensors/temperature/" + name,
43 "xyz.openbmc_project.Sensor.Value");
44
Jayashree Dhanapal56678082022-01-04 17:27:20 +053045 for (const auto& threshold : thresholds)
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070046 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053047 std::string interface = thresholds::getInterface(threshold.level);
48 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
49 objectServer.add_interface(
50 "/xyz/openbmc_project/sensors/temperature/" + name, interface);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070051 }
52 association = objectServer.add_interface(
53 "/xyz/openbmc_project/sensors/temperature/" + name,
54 association::interface);
55
Andrei Kartashev39287412022-02-04 16:04:47 +030056 setInitialProperties(sensor_paths::unitDegreesC);
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070057}
58
59NVMeSensor::~NVMeSensor()
60{
61 // close the input dev to cancel async operations
Jayashree Dhanapal56678082022-01-04 17:27:20 +053062 for (const auto& iface : thresholdInterfaces)
63 {
64 objServer.remove_interface(iface);
65 }
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070066 objServer.remove_interface(sensorInterface);
67 objServer.remove_interface(association);
68}
69
Andrew Jeffery14108bb2022-03-21 15:00:16 +103070bool NVMeSensor::sample()
71{
72 if (inError())
73 {
74 if (scanDelay == 0)
75 {
76 scanDelay = scanDelayTicks;
77 }
78
79 scanDelay--;
80 }
81
82 return scanDelay == 0;
83}
84
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070085void NVMeSensor::checkThresholds(void)
86{
Nikhil Potadeb669b6b2019-03-13 10:52:21 -070087 thresholds::checkThresholds(this);
88}