blob: eab734968c2091ab0ccc83c69a51f8b820b8fc01 [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
Ed Tanouseacbfdd2024-04-04 12:00:24 -070019#include "DeviceMgmt.hpp"
20#include "Thresholds.hpp"
21#include "Utils.hpp"
22#include "sensor.hpp"
James Feist6714a252018-09-10 15:26:18 -070023
Ed Tanouseacbfdd2024-04-04 12:00:24 -070024#include <boost/algorithm/string/replace.hpp>
25#include <boost/asio/buffer.hpp>
26#include <boost/asio/error.hpp>
27#include <boost/asio/io_context.hpp>
28#include <boost/asio/random_access_file.hpp>
James Feist38fb5982020-05-28 10:09:54 -070029#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31
Zev Weissd1c8f442022-07-29 11:54:41 -070032#include <charconv>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <chrono>
34#include <cstddef>
James Feist6714a252018-09-10 15:26:18 -070035#include <iostream>
36#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070037#include <memory>
James Feist6714a252018-09-10 15:26:18 -070038#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070039#include <system_error>
Zev Weissa1456c42022-07-18 16:59:35 -070040#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070041#include <vector>
James Feist6714a252018-09-10 15:26:18 -070042
Bruce Mitchelle5fc3a52021-08-10 11:50:25 -050043// Temperatures are read in milli degrees Celsius, we need degrees Celsius.
44// Pressures are read in kilopascal, we need Pascals. On D-Bus for Open BMC
45// we use the International System of Units without prefixes.
46// Links to the kernel documentation:
47// https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
48// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
49// For IIO RAW sensors we get a raw_value, an offset, and scale to compute
50// the value = (raw_value + offset) * scale
James Feistce3fca42018-11-21 12:58:24 -080051
James Feist6714a252018-09-10 15:26:18 -070052HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080053 const std::string& path, const std::string& objectType,
54 sdbusplus::asio::object_server& objectServer,
55 std::shared_ptr<sdbusplus::asio::connection>& conn,
Ed Tanous1f978632023-02-28 18:16:39 -080056 boost::asio::io_context& io, const std::string& sensorName,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050057 std::vector<thresholds::Threshold>&& thresholdsIn,
58 const struct SensorParams& thisSensorParameters, const float pollRate,
Zev Weissa1456c42022-07-18 16:59:35 -070059 const std::string& sensorConfiguration, const PowerState powerState,
60 const std::shared_ptr<I2CDevice>& i2cDevice) :
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050061 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
62 std::move(thresholdsIn), sensorConfiguration, objectType, false,
63 false, thisSensorParameters.maxValue, thisSensorParameters.minValue,
64 conn, powerState),
Zev Weissa1456c42022-07-18 16:59:35 -070065 i2cDevice(i2cDevice), objServer(objectServer),
Zev Weissd1c8f442022-07-29 11:54:41 -070066 inputDev(io, path, boost::asio::random_access_file::read_only),
67 waitTimer(io), path(path), offsetValue(thisSensorParameters.offsetValue),
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050068 scaleValue(thisSensorParameters.scaleValue),
Ed Tanous8a57ec02020-10-09 12:46:52 -070069 sensorPollMs(static_cast<unsigned int>(pollRate * 1000))
James Feist6714a252018-09-10 15:26:18 -070070{
James Feist251c7822018-09-12 12:54:15 -070071 sensorInterface = objectServer.add_interface(
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050072 "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" +
73 name,
James Feist251c7822018-09-12 12:54:15 -070074 "xyz.openbmc_project.Sensor.Value");
75
Jayashree Dhanapal56678082022-01-04 17:27:20 +053076 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070077 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053078 std::string interface = thresholds::getInterface(threshold.level);
79 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
Patrick Williams2aaf7172024-08-16 15:20:40 -040080 objectServer.add_interface(
81 "/xyz/openbmc_project/sensors/" +
82 thisSensorParameters.typeName + "/" + name,
83 interface);
James Feist6714a252018-09-10 15:26:18 -070084 }
Patrick Williams2aaf7172024-08-16 15:20:40 -040085 association = objectServer.add_interface(
86 "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" +
87 name,
88 association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030089 setInitialProperties(thisSensorParameters.units);
James Feist6714a252018-09-10 15:26:18 -070090}
91
Zev Weissa1456c42022-07-18 16:59:35 -070092bool HwmonTempSensor::isActive()
James Feist6714a252018-09-10 15:26:18 -070093{
Zev Weissa1456c42022-07-18 16:59:35 -070094 return inputDev.is_open();
95}
96
97void HwmonTempSensor::activate(const std::string& newPath,
98 const std::shared_ptr<I2CDevice>& newI2CDevice)
99{
100 path = newPath;
101 i2cDevice = newI2CDevice;
102 inputDev.open(path, boost::asio::random_access_file::read_only);
103 markAvailable(true);
104 setupRead();
105}
106
107void HwmonTempSensor::deactivate()
108{
109 markAvailable(false);
James Feist6714a252018-09-10 15:26:18 -0700110 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700111 inputDev.close();
112 waitTimer.cancel();
Zev Weissa1456c42022-07-18 16:59:35 -0700113 i2cDevice = nullptr;
114 path = "";
115}
116
117HwmonTempSensor::~HwmonTempSensor()
118{
119 deactivate();
120
Jayashree Dhanapal56678082022-01-04 17:27:20 +0530121 for (const auto& iface : thresholdInterfaces)
122 {
123 objServer.remove_interface(iface);
124 }
James Feist251c7822018-09-12 12:54:15 -0700125 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -0800126 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -0700127}
128
Ed Tanous201a1012024-04-03 18:07:28 -0700129void HwmonTempSensor::setupRead()
James Feist6714a252018-09-10 15:26:18 -0700130{
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300131 if (!readingStateGood())
132 {
133 markAvailable(false);
134 updateValue(std::numeric_limits<double>::quiet_NaN());
135 restartRead();
136 return;
137 }
Yong Lif3fd1912020-03-25 21:35:23 +0800138
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300139 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Zev Weissd1c8f442022-07-29 11:54:41 -0700140 inputDev.async_read_some_at(
141 0, boost::asio::buffer(readBuf),
142 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400143 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
144 if (self)
145 {
146 self->handleResponse(ec, bytesRead);
147 }
148 });
James Feist6714a252018-09-10 15:26:18 -0700149}
150
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300151void HwmonTempSensor::restartRead()
152{
153 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Ed Tanous83db50c2023-03-01 10:20:24 -0800154 waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300155 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
156 if (ec == boost::asio::error::operation_aborted)
157 {
158 return; // we're being canceled
159 }
160 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
161 if (!self)
162 {
163 return;
164 }
165 self->setupRead();
166 });
167}
168
Zev Weissd1c8f442022-07-29 11:54:41 -0700169void HwmonTempSensor::handleResponse(const boost::system::error_code& err,
170 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700171{
Yong Lif3fd1912020-03-25 21:35:23 +0800172 if ((err == boost::system::errc::bad_file_descriptor) ||
173 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700174 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700175 std::cerr << "Hwmon temp sensor " << name << " removed " << path
176 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700177 return; // we're being destroyed
178 }
Zev Weissd1c8f442022-07-29 11:54:41 -0700179
James Feist6714a252018-09-10 15:26:18 -0700180 if (!err)
181 {
Zev Weissd1c8f442022-07-29 11:54:41 -0700182 const char* bufEnd = readBuf.data() + bytesRead;
183 int nvalue = 0;
Patrick Williams2aaf7172024-08-16 15:20:40 -0400184 std::from_chars_result ret =
185 std::from_chars(readBuf.data(), bufEnd, nvalue);
Zev Weissd1c8f442022-07-29 11:54:41 -0700186 if (ret.ec != std::errc())
James Feist6714a252018-09-10 15:26:18 -0700187 {
James Feist961bf092020-07-01 16:38:12 -0700188 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700189 }
Zev Weissd1c8f442022-07-29 11:54:41 -0700190 else
191 {
192 updateValue((nvalue + offsetValue) * scaleValue);
193 }
James Feist6714a252018-09-10 15:26:18 -0700194 }
195 else
196 {
James Feist961bf092020-07-01 16:38:12 -0700197 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700198 }
James Feistdc6c55f2018-10-31 12:53:20 -0700199
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300200 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700201}
202
Ed Tanous201a1012024-04-03 18:07:28 -0700203void HwmonTempSensor::checkThresholds()
James Feist6714a252018-09-10 15:26:18 -0700204{
James Feist251c7822018-09-12 12:54:15 -0700205 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700206}