xref: /openbmc/dbus-sensors/src/NVMeSensor.cpp (revision eacbfdd1)
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 
17 #include "NVMeSensor.hpp"
18 
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>
35 
36 static constexpr double maxReading = 127;
37 static constexpr double minReading = 0;
38 
NVMeSensor(sdbusplus::asio::object_server & objectServer,boost::asio::io_context &,std::shared_ptr<sdbusplus::asio::connection> & conn,const std::string & sensorName,std::vector<thresholds::Threshold> && thresholdsIn,const std::string & sensorConfiguration,const int busNumber,const uint8_t slaveAddr)39 NVMeSensor::NVMeSensor(sdbusplus::asio::object_server& objectServer,
40                        boost::asio::io_context& /*unused*/,
41                        std::shared_ptr<sdbusplus::asio::connection>& conn,
42                        const std::string& sensorName,
43                        std::vector<thresholds::Threshold>&& thresholdsIn,
44                        const std::string& sensorConfiguration,
45                        const int busNumber, const uint8_t slaveAddr) :
46     Sensor(escapeName(sensorName), std::move(thresholdsIn), sensorConfiguration,
47            NVMeSensor::sensorType, false, false, maxReading, minReading, conn,
48            PowerState::on),
49     bus(busNumber), address(slaveAddr), objServer(objectServer)
50 {
51     if (bus < 0)
52     {
53         throw std::invalid_argument("Invalid bus: Bus ID must not be negative");
54     }
55 
56     sensorInterface = objectServer.add_interface(
57         "/xyz/openbmc_project/sensors/temperature/" + name,
58         "xyz.openbmc_project.Sensor.Value");
59 
60     for (const auto& threshold : thresholds)
61     {
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);
66     }
67     association = objectServer.add_interface(
68         "/xyz/openbmc_project/sensors/temperature/" + name,
69         association::interface);
70 
71     setInitialProperties(sensor_paths::unitDegreesC);
72     // Mark as unavailable until the first packet has been received over NVMe
73     // MI.
74     markAvailable(false);
75 }
76 
~NVMeSensor()77 NVMeSensor::~NVMeSensor()
78 {
79     // close the input dev to cancel async operations
80     for (const auto& iface : thresholdInterfaces)
81     {
82         objServer.remove_interface(iface);
83     }
84     objServer.remove_interface(sensorInterface);
85     objServer.remove_interface(association);
86 }
87 
sample()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 
checkThresholds()103 void NVMeSensor::checkThresholds()
104 {
105     thresholds::checkThresholds(this);
106 }
107