blob: ee096eab83be51bd69be3626be6aaf37c82e67ca [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
17#include <unistd.h>
18
Ed Tanous8a57ec02020-10-09 12:46:52 -070019#include <HwmonTempSensor.hpp>
James Feist8086aba2020-08-25 16:00:59 -070020#include <boost/asio/read_until.hpp>
James Feist38fb5982020-05-28 10:09:54 -070021#include <sdbusplus/asio/connection.hpp>
22#include <sdbusplus/asio/object_server.hpp>
23
Zev Weissd1c8f442022-07-29 11:54:41 -070024#include <charconv>
James Feist6714a252018-09-10 15:26:18 -070025#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070026#include <istream>
James Feist6714a252018-09-10 15:26:18 -070027#include <limits>
Patrick Venture96e97db2019-10-31 13:44:38 -070028#include <memory>
James Feist6714a252018-09-10 15:26:18 -070029#include <string>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <vector>
James Feist6714a252018-09-10 15:26:18 -070031
Bruce Mitchelle5fc3a52021-08-10 11:50:25 -050032// Temperatures are read in milli degrees Celsius, we need degrees Celsius.
33// Pressures are read in kilopascal, we need Pascals. On D-Bus for Open BMC
34// we use the International System of Units without prefixes.
35// Links to the kernel documentation:
36// https://www.kernel.org/doc/Documentation/hwmon/sysfs-interface
37// https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-bus-iio
38// For IIO RAW sensors we get a raw_value, an offset, and scale to compute
39// the value = (raw_value + offset) * scale
James Feistce3fca42018-11-21 12:58:24 -080040
James Feist6714a252018-09-10 15:26:18 -070041HwmonTempSensor::HwmonTempSensor(
James Feistd8705872019-02-08 13:26:09 -080042 const std::string& path, const std::string& objectType,
43 sdbusplus::asio::object_server& objectServer,
44 std::shared_ptr<sdbusplus::asio::connection>& conn,
45 boost::asio::io_service& io, const std::string& sensorName,
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050046 std::vector<thresholds::Threshold>&& thresholdsIn,
47 const struct SensorParams& thisSensorParameters, const float pollRate,
James Feistf9b01b62020-01-29 15:21:58 -080048 const std::string& sensorConfiguration, const PowerState powerState) :
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050049 Sensor(boost::replace_all_copy(sensorName, " ", "_"),
50 std::move(thresholdsIn), sensorConfiguration, objectType, false,
51 false, thisSensorParameters.maxValue, thisSensorParameters.minValue,
52 conn, powerState),
Zev Weissd1c8f442022-07-29 11:54:41 -070053 objServer(objectServer),
54 inputDev(io, path, boost::asio::random_access_file::read_only),
55 waitTimer(io), path(path), offsetValue(thisSensorParameters.offsetValue),
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050056 scaleValue(thisSensorParameters.scaleValue),
Ed Tanous8a57ec02020-10-09 12:46:52 -070057 sensorPollMs(static_cast<unsigned int>(pollRate * 1000))
James Feist6714a252018-09-10 15:26:18 -070058{
James Feist251c7822018-09-12 12:54:15 -070059 sensorInterface = objectServer.add_interface(
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050060 "/xyz/openbmc_project/sensors/" + thisSensorParameters.typeName + "/" +
61 name,
James Feist251c7822018-09-12 12:54:15 -070062 "xyz.openbmc_project.Sensor.Value");
63
Jayashree Dhanapal56678082022-01-04 17:27:20 +053064 for (const auto& threshold : thresholds)
James Feist6714a252018-09-10 15:26:18 -070065 {
Jayashree Dhanapal56678082022-01-04 17:27:20 +053066 std::string interface = thresholds::getInterface(threshold.level);
67 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
68 objectServer.add_interface("/xyz/openbmc_project/sensors/" +
69 thisSensorParameters.typeName + "/" +
70 name,
71 interface);
James Feist6714a252018-09-10 15:26:18 -070072 }
Bruce Mitchell544e7dc2021-07-29 18:05:49 -050073 association = objectServer.add_interface("/xyz/openbmc_project/sensors/" +
74 thisSensorParameters.typeName +
75 "/" + name,
76 association::interface);
Andrei Kartashev39287412022-02-04 16:04:47 +030077 setInitialProperties(thisSensorParameters.units);
James Feist6714a252018-09-10 15:26:18 -070078}
79
80HwmonTempSensor::~HwmonTempSensor()
81{
82 // close the input dev to cancel async operations
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070083 inputDev.close();
84 waitTimer.cancel();
Jayashree Dhanapal56678082022-01-04 17:27:20 +053085 for (const auto& iface : thresholdInterfaces)
86 {
87 objServer.remove_interface(iface);
88 }
James Feist251c7822018-09-12 12:54:15 -070089 objServer.remove_interface(sensorInterface);
James Feist078f2322019-03-08 11:09:05 -080090 objServer.remove_interface(association);
James Feist6714a252018-09-10 15:26:18 -070091}
92
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093void HwmonTempSensor::setupRead(void)
James Feist6714a252018-09-10 15:26:18 -070094{
Konstantin Aladysheva7345942021-04-28 17:09:02 +030095 if (!readingStateGood())
96 {
97 markAvailable(false);
98 updateValue(std::numeric_limits<double>::quiet_NaN());
99 restartRead();
100 return;
101 }
Yong Lif3fd1912020-03-25 21:35:23 +0800102
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300103 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Zev Weissd1c8f442022-07-29 11:54:41 -0700104 inputDev.async_read_some_at(
105 0, boost::asio::buffer(readBuf),
106 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
Ed Tanousbb679322022-05-16 16:10:00 -0700107 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
108 if (self)
109 {
Zev Weissd1c8f442022-07-29 11:54:41 -0700110 self->handleResponse(ec, bytesRead);
Ed Tanousbb679322022-05-16 16:10:00 -0700111 }
Zev Weissd1c8f442022-07-29 11:54:41 -0700112 });
James Feist6714a252018-09-10 15:26:18 -0700113}
114
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300115void HwmonTempSensor::restartRead()
116{
117 std::weak_ptr<HwmonTempSensor> weakRef = weak_from_this();
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700118 waitTimer.expires_from_now(std::chrono::milliseconds(sensorPollMs));
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300119 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
120 if (ec == boost::asio::error::operation_aborted)
121 {
122 return; // we're being canceled
123 }
124 std::shared_ptr<HwmonTempSensor> self = weakRef.lock();
125 if (!self)
126 {
127 return;
128 }
129 self->setupRead();
130 });
131}
132
Zev Weissd1c8f442022-07-29 11:54:41 -0700133void HwmonTempSensor::handleResponse(const boost::system::error_code& err,
134 size_t bytesRead)
James Feist6714a252018-09-10 15:26:18 -0700135{
Yong Lif3fd1912020-03-25 21:35:23 +0800136 if ((err == boost::system::errc::bad_file_descriptor) ||
137 (err == boost::asio::error::misc_errors::not_found))
James Feist6714a252018-09-10 15:26:18 -0700138 {
Zhikui Rend3da1282020-09-11 17:02:01 -0700139 std::cerr << "Hwmon temp sensor " << name << " removed " << path
140 << "\n";
James Feist6714a252018-09-10 15:26:18 -0700141 return; // we're being destroyed
142 }
Zev Weissd1c8f442022-07-29 11:54:41 -0700143
James Feist6714a252018-09-10 15:26:18 -0700144 if (!err)
145 {
Zev Weissd1c8f442022-07-29 11:54:41 -0700146 const char* bufEnd = readBuf.data() + bytesRead;
147 int nvalue = 0;
148 std::from_chars_result ret =
149 std::from_chars(readBuf.data(), bufEnd, nvalue);
150 if (ret.ec != std::errc())
James Feist6714a252018-09-10 15:26:18 -0700151 {
James Feist961bf092020-07-01 16:38:12 -0700152 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700153 }
Zev Weissd1c8f442022-07-29 11:54:41 -0700154 else
155 {
156 updateValue((nvalue + offsetValue) * scaleValue);
157 }
James Feist6714a252018-09-10 15:26:18 -0700158 }
159 else
160 {
James Feist961bf092020-07-01 16:38:12 -0700161 incrementError();
James Feist6714a252018-09-10 15:26:18 -0700162 }
James Feistdc6c55f2018-10-31 12:53:20 -0700163
Konstantin Aladysheva7345942021-04-28 17:09:02 +0300164 restartRead();
James Feist6714a252018-09-10 15:26:18 -0700165}
166
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700167void HwmonTempSensor::checkThresholds(void)
James Feist6714a252018-09-10 15:26:18 -0700168{
James Feist251c7822018-09-12 12:54:15 -0700169 thresholds::checkThresholds(this);
Patrick Ventureca44b2f2019-10-31 11:02:26 -0700170}