xref: /openbmc/dbus-sensors/src/hwmon-temp/HwmonTempSensor.cpp (revision 8b06518128bfb46b4cf603bcf1d50114245b68a8)
1d7be555eSGeorge Liu /*
2d7be555eSGeorge Liu // Copyright (c) 2017 Intel Corporation
3d7be555eSGeorge Liu //
4d7be555eSGeorge Liu // Licensed under the Apache License, Version 2.0 (the "License");
5d7be555eSGeorge Liu // you may not use this file except in compliance with the License.
6d7be555eSGeorge Liu // You may obtain a copy of the License at
7d7be555eSGeorge Liu //
8d7be555eSGeorge Liu //      http://www.apache.org/licenses/LICENSE-2.0
9d7be555eSGeorge Liu //
10d7be555eSGeorge Liu // Unless required by applicable law or agreed to in writing, software
11d7be555eSGeorge Liu // distributed under the License is distributed on an "AS IS" BASIS,
12d7be555eSGeorge Liu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d7be555eSGeorge Liu // See the License for the specific language governing permissions and
14d7be555eSGeorge Liu // limitations under the License.
15d7be555eSGeorge Liu */
16d7be555eSGeorge Liu 
17d7be555eSGeorge Liu #include "HwmonTempSensor.hpp"
18d7be555eSGeorge Liu 
19d7be555eSGeorge Liu #include "DeviceMgmt.hpp"
20d7be555eSGeorge Liu #include "Thresholds.hpp"
21d7be555eSGeorge Liu #include "Utils.hpp"
22d7be555eSGeorge Liu #include "sensor.hpp"
23d7be555eSGeorge Liu 
24d7be555eSGeorge Liu #include <boost/algorithm/string/replace.hpp>
25d7be555eSGeorge Liu #include <boost/asio/buffer.hpp>
26d7be555eSGeorge Liu #include <boost/asio/error.hpp>
27d7be555eSGeorge Liu #include <boost/asio/io_context.hpp>
28d7be555eSGeorge Liu #include <boost/asio/random_access_file.hpp>
29*8b065181SGeorge Liu #include <phosphor-logging/lg2.hpp>
30d7be555eSGeorge Liu #include <sdbusplus/asio/connection.hpp>
31d7be555eSGeorge Liu #include <sdbusplus/asio/object_server.hpp>
32d7be555eSGeorge Liu 
33d7be555eSGeorge Liu #include <charconv>
34d7be555eSGeorge Liu #include <chrono>
35d7be555eSGeorge Liu #include <cstddef>
36d7be555eSGeorge Liu #include <limits>
37d7be555eSGeorge Liu #include <memory>
38d7be555eSGeorge Liu #include <string>
39d7be555eSGeorge Liu #include <system_error>
40d7be555eSGeorge Liu #include <utility>
41d7be555eSGeorge Liu #include <vector>
42d7be555eSGeorge Liu 
43d7be555eSGeorge Liu // Temperatures are read in milli degrees Celsius, we need degrees Celsius.
44d7be555eSGeorge Liu // Pressures are read in kilopascal, we need Pascals.  On D-Bus for Open BMC
45d7be555eSGeorge Liu // we use the International System of Units without prefixes.
46d7be555eSGeorge Liu // Links to the kernel documentation:
47d7be555eSGeorge Liu // https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
48d7be555eSGeorge Liu // https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
49d7be555eSGeorge Liu // For IIO RAW sensors we get a raw_value, an offset, and scale to compute
50d7be555eSGeorge Liu // the value = (raw_value + offset) * scale
51d7be555eSGeorge Liu 
HwmonTempSensor(const std::string & path,const std::string & objectType,sdbusplus::asio::object_server & objectServer,std::shared_ptr<sdbusplus::asio::connection> & conn,boost::asio::io_context & io,const std::string & sensorName,std::vector<thresholds::Threshold> && thresholdsIn,const struct SensorParams & thisSensorParameters,const float pollRate,const std::string & sensorConfiguration,const PowerState powerState,const std::shared_ptr<I2CDevice> & i2cDevice)52d7be555eSGeorge Liu HwmonTempSensor::HwmonTempSensor(
53d7be555eSGeorge Liu     const std::string& path, const std::string& objectType,
54d7be555eSGeorge Liu     sdbusplus::asio::object_server& objectServer,
55d7be555eSGeorge Liu     std::shared_ptr<sdbusplus::asio::connection>& conn,
56d7be555eSGeorge Liu     boost::asio::io_context& io, const std::string& sensorName,
57d7be555eSGeorge Liu     std::vector<thresholds::Threshold>&& thresholdsIn,
58d7be555eSGeorge Liu     const struct SensorParams& thisSensorParameters, const float pollRate,
59d7be555eSGeorge Liu     const std::string& sensorConfiguration, const PowerState powerState,
60d7be555eSGeorge Liu     const std::shared_ptr<I2CDevice>& i2cDevice) :
61d7be555eSGeorge Liu     Sensor(boost::replace_all_copy(sensorName, " ", "_"),
62d7be555eSGeorge Liu            std::move(thresholdsIn), sensorConfiguration, objectType, false,
63d7be555eSGeorge Liu            false, thisSensorParameters.maxValue, thisSensorParameters.minValue,
64d7be555eSGeorge Liu            conn, powerState),
65d7be555eSGeorge Liu     i2cDevice(i2cDevice), objServer(objectServer),
66d7be555eSGeorge Liu     inputDev(io, path, boost::asio::random_access_file::read_only),
67d7be555eSGeorge Liu     waitTimer(io), path(path), offsetValue(thisSensorParameters.offsetValue),
68d7be555eSGeorge Liu     scaleValue(thisSensorParameters.scaleValue),
69d7be555eSGeorge Liu     sensorPollMs(static_cast<unsigned int>(pollRate * 1000))
70d7be555eSGeorge Liu {
71d7be555eSGeorge Liu     sensorInterface = objectServer.add_interface(
72d7be555eSGeorge Liu         "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" +
73d7be555eSGeorge Liu             name,
74d7be555eSGeorge Liu         "xyz.openbmc_project.Sensor.Value");
75d7be555eSGeorge Liu 
76d7be555eSGeorge Liu     for (const auto& threshold : thresholds)
77d7be555eSGeorge Liu     {
78d7be555eSGeorge Liu         std::string interface = thresholds::getInterface(threshold.level);
79d7be555eSGeorge Liu         thresholdInterfaces[static_cast<size_t>(threshold.level)] =
80d7be555eSGeorge Liu             objectServer.add_interface(
81d7be555eSGeorge Liu                 "/xyz/openbmc_project/sensors/" +
82d7be555eSGeorge Liu                     thisSensorParameters.typeName + "/" + name,
83d7be555eSGeorge Liu                 interface);
84d7be555eSGeorge Liu     }
85d7be555eSGeorge Liu     association = objectServer.add_interface(
86d7be555eSGeorge Liu         "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" +
87d7be555eSGeorge Liu             name,
88d7be555eSGeorge Liu         association::interface);
89d7be555eSGeorge Liu     setInitialProperties(thisSensorParameters.units);
90d7be555eSGeorge Liu }
91d7be555eSGeorge Liu 
isActive()92d7be555eSGeorge Liu bool HwmonTempSensor::isActive()
93d7be555eSGeorge Liu {
94d7be555eSGeorge Liu     return inputDev.is_open();
95d7be555eSGeorge Liu }
96d7be555eSGeorge Liu 
activate(const std::string & newPath,const std::shared_ptr<I2CDevice> & newI2CDevice)97d7be555eSGeorge Liu void HwmonTempSensor::activate(const std::string& newPath,
98d7be555eSGeorge Liu                                const std::shared_ptr<I2CDevice>& newI2CDevice)
99d7be555eSGeorge Liu {
100d490830bSPotin Lai     if (isActive())
101d490830bSPotin Lai     {
102d490830bSPotin Lai         // Avoid activating an active sensor
103d490830bSPotin Lai         return;
104d490830bSPotin Lai     }
105d7be555eSGeorge Liu     path = newPath;
106d7be555eSGeorge Liu     i2cDevice = newI2CDevice;
107d7be555eSGeorge Liu     inputDev.open(path, boost::asio::random_access_file::read_only);
108d7be555eSGeorge Liu     markAvailable(true);
109d7be555eSGeorge Liu     setupRead();
110d7be555eSGeorge Liu }
111d7be555eSGeorge Liu 
deactivate()112d7be555eSGeorge Liu void HwmonTempSensor::deactivate()
113d7be555eSGeorge Liu {
114d7be555eSGeorge Liu     markAvailable(false);
115d7be555eSGeorge Liu     // close the input dev to cancel async operations
116d7be555eSGeorge Liu     inputDev.close();
117d7be555eSGeorge Liu     waitTimer.cancel();
118d7be555eSGeorge Liu     i2cDevice = nullptr;
119d7be555eSGeorge Liu     path = "";
120d7be555eSGeorge Liu }
121d7be555eSGeorge Liu 
~HwmonTempSensor()122d7be555eSGeorge Liu HwmonTempSensor::~HwmonTempSensor()
123d7be555eSGeorge Liu {
124d7be555eSGeorge Liu     deactivate();
125d7be555eSGeorge Liu 
126d7be555eSGeorge Liu     for (const auto& iface : thresholdInterfaces)
127d7be555eSGeorge Liu     {
128d7be555eSGeorge Liu         objServer.remove_interface(iface);
129d7be555eSGeorge Liu     }
130d7be555eSGeorge Liu     objServer.remove_interface(sensorInterface);
131d7be555eSGeorge Liu     objServer.remove_interface(association);
132d7be555eSGeorge Liu }
133d7be555eSGeorge Liu 
setupRead()134d7be555eSGeorge Liu void HwmonTempSensor::setupRead()
135d7be555eSGeorge Liu {
136d7be555eSGeorge Liu     if (!readingStateGood())
137d7be555eSGeorge Liu     {
138d7be555eSGeorge Liu         markAvailable(false);
139d7be555eSGeorge Liu         updateValue(std::numeric_limits<double>::quiet_NaN());
140d7be555eSGeorge Liu         restartRead();
141d7be555eSGeorge Liu         return;
142d7be555eSGeorge Liu     }
143d7be555eSGeorge Liu 
144d7be555eSGeorge Liu     std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
145d7be555eSGeorge Liu     inputDev.async_read_some_at(
146d7be555eSGeorge Liu         0, boost::asio::buffer(readBuf),
147d7be555eSGeorge Liu         [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
148d7be555eSGeorge Liu             std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
149d7be555eSGeorge Liu             if (self)
150d7be555eSGeorge Liu             {
151d7be555eSGeorge Liu                 self->handleResponse(ec, bytesRead);
152d7be555eSGeorge Liu             }
153d7be555eSGeorge Liu         });
154d7be555eSGeorge Liu }
155d7be555eSGeorge Liu 
restartRead()156d7be555eSGeorge Liu void HwmonTempSensor::restartRead()
157d7be555eSGeorge Liu {
158d7be555eSGeorge Liu     std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
159d7be555eSGeorge Liu     waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
160d7be555eSGeorge Liu     waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
161d7be555eSGeorge Liu         if (ec == boost::asio::error::operation_aborted)
162d7be555eSGeorge Liu         {
163d7be555eSGeorge Liu             return; // we're being canceled
164d7be555eSGeorge Liu         }
165d7be555eSGeorge Liu         std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
166d7be555eSGeorge Liu         if (!self)
167d7be555eSGeorge Liu         {
168d7be555eSGeorge Liu             return;
169d7be555eSGeorge Liu         }
170d7be555eSGeorge Liu         self->setupRead();
171d7be555eSGeorge Liu     });
172d7be555eSGeorge Liu }
173d7be555eSGeorge Liu 
handleResponse(const boost::system::error_code & err,size_t bytesRead)174d7be555eSGeorge Liu void HwmonTempSensor::handleResponse(const boost::system::error_code& err,
175d7be555eSGeorge Liu                                      size_t bytesRead)
176d7be555eSGeorge Liu {
177d7be555eSGeorge Liu     if ((err == boost::system::errc::bad_file_descriptor) ||
178d7be555eSGeorge Liu         (err == boost::asio::error::misc_errors::not_found))
179d7be555eSGeorge Liu     {
180*8b065181SGeorge Liu         lg2::error("Hwmon temp sensor '{NAME}' removed '{PATH}'", "NAME", name,
181*8b065181SGeorge Liu                    "PATH", path);
182d7be555eSGeorge Liu         return; // we're being destroyed
183d7be555eSGeorge Liu     }
184d7be555eSGeorge Liu 
185d7be555eSGeorge Liu     if (!err)
186d7be555eSGeorge Liu     {
187d7be555eSGeorge Liu         const char* bufEnd = readBuf.data() + bytesRead;
188d7be555eSGeorge Liu         int nvalue = 0;
189d7be555eSGeorge Liu         std::from_chars_result ret =
190d7be555eSGeorge Liu             std::from_chars(readBuf.data(), bufEnd, nvalue);
191d7be555eSGeorge Liu         if (ret.ec != std::errc())
192d7be555eSGeorge Liu         {
193d7be555eSGeorge Liu             incrementError();
194d7be555eSGeorge Liu         }
195d7be555eSGeorge Liu         else
196d7be555eSGeorge Liu         {
197d7be555eSGeorge Liu             updateValue((nvalue + offsetValue) * scaleValue);
198d7be555eSGeorge Liu         }
199d7be555eSGeorge Liu     }
200d7be555eSGeorge Liu     else
201d7be555eSGeorge Liu     {
202d7be555eSGeorge Liu         incrementError();
203d7be555eSGeorge Liu     }
204d7be555eSGeorge Liu 
205d7be555eSGeorge Liu     restartRead();
206d7be555eSGeorge Liu }
207d7be555eSGeorge Liu 
checkThresholds()208d7be555eSGeorge Liu void HwmonTempSensor::checkThresholds()
209d7be555eSGeorge Liu {
210d7be555eSGeorge Liu     thresholds::checkThresholds(this);
211d7be555eSGeorge Liu }
212