blob: 9fdbab1e366fbf228902313a7dbe87b55bf76a64 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001/*
2// Copyright (c) 2017 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 "HwmonTempSensor.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <unistd.h>
20
James Feist8086aba2020-08-25 16:00:59 -070021#include <boost/asio/read_until.hpp>
James Feist38fb5982020-05-28 10:09:54 -070022#include <sdbusplus/asio/connection.hpp>
23#include <sdbusplus/asio/object_server.hpp>
24
Zev Weissd1c8f442022-07-29 11:54:41 -070025#include <charconv>
James Feist6714a252018-09-10 15:26:18 -070026#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <istream>
James Feist6714a252018-09-10 15:26:18 -070028#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <memory>
James Feist6714a252018-09-10 15:26:18 -070030#include <string>
Zev Weissa1456c42022-07-18 16:59:35 -070031#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <vector>
James Feist6714a252018-09-10 15:26:18 -070033
Bruce Mitchelle5fc3a52021-08-10 11:50:25 -050034// Temperatures are read in milli degrees Celsius, we need degrees Celsius.
35// Pressures are read in kilopascal, we need Pascals. On D-Bus for Open BMC
36// we use the International System of Units without prefixes.
37// Links to the kernel documentation:
38// https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
39// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
40// For IIO RAW sensors we get a raw_value, an offset, and scale to compute
41// the value = (raw_value + offset) * scale
James Feistce3fca42018-11-21 12:58:24 -080042
James Feist6714a252018-09-10 15:26:18 -070043HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080044 const std::string& path, const std::string& objectType,
45 sdbusplus::asio::object_server& objectServer,
46 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080047 boost::asio::io_context& io, const std::string& sensorName,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050048 std::vector<thresholds::Threshold>&& thresholdsIn,
49 const struct SensorParams& thisSensorParameters, const float pollRate,
Zev Weissa1456c42022-07-18 16:59:35 -070050 const std::string& sensorConfiguration, const PowerState powerState,
51 const std::shared_ptr<I2CDevice>& i2cDevice) :
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050052 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
53 std::move(thresholdsIn), sensorConfiguration, objectType, false,
54 false, thisSensorParameters.maxValue, thisSensorParameters.minValue,
55 conn, powerState),
Zev Weissa1456c42022-07-18 16:59:35 -070056 i2cDevice(i2cDevice), objServer(objectServer),
Zev Weissd1c8f442022-07-29 11:54:41 -070057 inputDev(io, path, boost::asio::random_access_file::read_only),
58 waitTimer(io), path(path), offsetValue(thisSensorParameters.offsetValue),
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050059 scaleValue(thisSensorParameters.scaleValue),
Ed Tanous8a57ec02020-10-09 12:46:52 -070060 sensorPollMs(static_cast<unsigned int>(pollRate * 1000))
James Feist6714a252018-09-10 15:26:18 -070061{
James Feist251c7822018-09-12 12:54:15 -070062 sensorInterface = objectServer.add_interface(
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050063 "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" +
64 name,
James Feist251c7822018-09-12 12:54:15 -070065 "xyz.openbmc_project.Sensor.Value");
66
Jayashree Dhanapal56678082022-01-04 17:27:20 +053067 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070068 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053069 std::string interface = thresholds::getInterface(threshold.level);
70 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
71 objectServer.add_interface("/xyz/openbmc_project/sensors/" +
72 thisSensorParameters.typeName + "/" +
73 name,
74 interface);
James Feist6714a252018-09-10 15:26:18 -070075 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050076 association = objectServer.add_interface("/xyz/openbmc_project/sensors/" +
77 thisSensorParameters.typeName +
78 "/" + name,
79 association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030080 setInitialProperties(thisSensorParameters.units);
James Feist6714a252018-09-10 15:26:18 -070081}
82
Zev Weissa1456c42022-07-18 16:59:35 -070083bool HwmonTempSensor::isActive()
James Feist6714a252018-09-10 15:26:18 -070084{
Zev Weissa1456c42022-07-18 16:59:35 -070085 return inputDev.is_open();
86}
87
88void HwmonTempSensor::activate(const std::string& newPath,
89 const std::shared_ptr<I2CDevice>& newI2CDevice)
90{
91 path = newPath;
92 i2cDevice = newI2CDevice;
93 inputDev.open(path, boost::asio::random_access_file::read_only);
94 markAvailable(true);
95 setupRead();
96}
97
98void HwmonTempSensor::deactivate()
99{
100 markAvailable(false);
James Feist6714a252018-09-10 15:26:18 -0700101 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102 inputDev.close();
103 waitTimer.cancel();
Zev Weissa1456c42022-07-18 16:59:35 -0700104 i2cDevice = nullptr;
105 path = "";
106}
107
108HwmonTempSensor::~HwmonTempSensor()
109{
110 deactivate();
111
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530112 for (const auto& iface : thresholdInterfaces)
113 {
114 objServer.remove_interface(iface);
115 }
James Feist251c7822018-09-12 12:54:15 -0700116 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800117 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700118}
119
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700120void HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -0700121{
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300122 if (!readingStateGood())
123 {
124 markAvailable(false);
125 updateValue(std::numeric_limits<double>::quiet_NaN());
126 restartRead();
127 return;
128 }
Yong Lif3fd1912020-03-25 21:35:23 +0800129
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300130 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Zev Weissd1c8f442022-07-29 11:54:41 -0700131 inputDev.async_read_some_at(
132 0, boost::asio::buffer(readBuf),
133 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
Ed Tanousbb679322022-05-16 16:10:00 -0700134 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
135 if (self)
136 {
Zev Weissd1c8f442022-07-29 11:54:41 -0700137 self->handleResponse(ec, bytesRead);
Ed Tanousbb679322022-05-16 16:10:00 -0700138 }
Zev Weissd1c8f442022-07-29 11:54:41 -0700139 });
James Feist6714a252018-09-10 15:26:18 -0700140}
141
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300142void HwmonTempSensor::restartRead()
143{
144 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700145 waitTimer.expires_from_now(std::chrono::milliseconds(sensorPollMs));
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300146 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
147 if (ec == boost::asio::error::operation_aborted)
148 {
149 return; // we're being canceled
150 }
151 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
152 if (!self)
153 {
154 return;
155 }
156 self->setupRead();
157 });
158}
159
Zev Weissd1c8f442022-07-29 11:54:41 -0700160void HwmonTempSensor::handleResponse(const boost::system::error_code& err,
161 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700162{
Yong Lif3fd1912020-03-25 21:35:23 +0800163 if ((err == boost::system::errc::bad_file_descriptor) ||
164 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700165 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700166 std::cerr << "Hwmon temp sensor " << name << " removed " << path
167 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700168 return; // we're being destroyed
169 }
Zev Weissd1c8f442022-07-29 11:54:41 -0700170
James Feist6714a252018-09-10 15:26:18 -0700171 if (!err)
172 {
Zev Weissd1c8f442022-07-29 11:54:41 -0700173 const char* bufEnd = readBuf.data() + bytesRead;
174 int nvalue = 0;
175 std::from_chars_result ret =
176 std::from_chars(readBuf.data(), bufEnd, nvalue);
177 if (ret.ec != std::errc())
James Feist6714a252018-09-10 15:26:18 -0700178 {
James Feist961bf092020-07-01 16:38:12 -0700179 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700180 }
Zev Weissd1c8f442022-07-29 11:54:41 -0700181 else
182 {
183 updateValue((nvalue + offsetValue) * scaleValue);
184 }
James Feist6714a252018-09-10 15:26:18 -0700185 }
186 else
187 {
James Feist961bf092020-07-01 16:38:12 -0700188 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700189 }
James Feistdc6c55f2018-10-31 12:53:20 -0700190
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300191 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700192}
193
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700195{
James Feist251c7822018-09-12 12:54:15 -0700196 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700197}