1 /*
2 // Copyright (c) 2018 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 "TachSensor.hpp"
18
19 #include "PresenceGpio.hpp"
20 #include "SensorPaths.hpp"
21 #include "Thresholds.hpp"
22 #include "Utils.hpp"
23 #include "sensor.hpp"
24
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>
29 #include <phosphor-logging/lg2.hpp>
30 #include <sdbusplus/asio/connection.hpp>
31 #include <sdbusplus/asio/object_server.hpp>
32
33 #include <charconv>
34 #include <chrono>
35 #include <cstddef>
36 #include <cstdint>
37 #include <memory>
38 #include <optional>
39 #include <string>
40 #include <system_error>
41 #include <utility>
42 #include <vector>
43
44 static constexpr unsigned int pwmPollMs = 500;
45
TachSensor(const std::string & path,const std::string & objectType,sdbusplus::asio::object_server & objectServer,std::shared_ptr<sdbusplus::asio::connection> & conn,std::shared_ptr<PresenceGpio> & presenceGpio,std::optional<RedundancySensor> * redundancy,boost::asio::io_context & io,const std::string & fanName,std::vector<thresholds::Threshold> && thresholdsIn,const std::string & sensorConfiguration,const std::pair<double,double> & limits,const PowerState & powerState,const std::optional<std::string> & ledIn)46 TachSensor::TachSensor(
47 const std::string& path, const std::string& objectType,
48 sdbusplus::asio::object_server& objectServer,
49 std::shared_ptr<sdbusplus::asio::connection>& conn,
50 std::shared_ptr<PresenceGpio>& presenceGpio,
51 std::optional<RedundancySensor>* redundancy, boost::asio::io_context& io,
52 const std::string& fanName,
53 std::vector<thresholds::Threshold>&& thresholdsIn,
54 const std::string& sensorConfiguration,
55 const std::pair<double, double>& limits, const PowerState& powerState,
56 const std::optional<std::string>& ledIn) :
57 Sensor(escapeName(fanName), std::move(thresholdsIn), sensorConfiguration,
58 objectType, false, false, limits.second, limits.first, conn,
59 powerState),
60 objServer(objectServer), redundancy(redundancy), presence(presenceGpio),
61 inputDev(io, path, boost::asio::random_access_file::read_only),
62 waitTimer(io), path(path), led(ledIn)
63 {
64 sensorInterface = objectServer.add_interface(
65 "/xyz/openbmc_project/sensors/fan_tach/" + name,
66 "xyz.openbmc_project.Sensor.Value");
67
68 for (const auto& threshold : thresholds)
69 {
70 std::string interface = thresholds::getInterface(threshold.level);
71 thresholdInterfaces[static_cast<size_t>(threshold.level)] =
72 objectServer.add_interface(
73 "/xyz/openbmc_project/sensors/fan_tach/" + name, interface);
74 }
75 association = objectServer.add_interface(
76 "/xyz/openbmc_project/sensors/fan_tach/" + name,
77 association::interface);
78
79 if (presence)
80 {
81 presence->monitorPresence();
82 itemIface =
83 objectServer.add_interface("/xyz/openbmc_project/inventory/" + name,
84 "xyz.openbmc_project.Inventory.Item");
85 itemIface->register_property("PrettyName",
86 std::string()); // unused property
87 itemIface->register_property("Present", true);
88 itemIface->initialize();
89 itemAssoc = objectServer.add_interface(
90 "/xyz/openbmc_project/inventory/" + name, association::interface);
91 itemAssoc->register_property(
92 "Associations",
93 std::vector<Association>{
94 {"sensors", "inventory",
95 "/xyz/openbmc_project/sensors/fan_tach/" + name}});
96 itemAssoc->initialize();
97 }
98 setInitialProperties(sensor_paths::unitRPMs);
99 }
100
~TachSensor()101 TachSensor::~TachSensor()
102 {
103 // close the input dev to cancel async operations
104 inputDev.close();
105 waitTimer.cancel();
106 for (const auto& iface : thresholdInterfaces)
107 {
108 objServer.remove_interface(iface);
109 }
110 objServer.remove_interface(sensorInterface);
111 objServer.remove_interface(association);
112 objServer.remove_interface(itemIface);
113 objServer.remove_interface(itemAssoc);
114 }
115
setupRead()116 void TachSensor::setupRead()
117 {
118 std::weak_ptr<TachSensor> weakRef = weak_from_this();
119 inputDev.async_read_some_at(
120 0, boost::asio::buffer(readBuf),
121 [weakRef](const boost::system::error_code& ec, std::size_t bytesRead) {
122 std::shared_ptr<TachSensor> self = weakRef.lock();
123 if (self)
124 {
125 self->handleResponse(ec, bytesRead);
126 }
127 });
128 }
129
restartRead(size_t pollTime)130 void TachSensor::restartRead(size_t pollTime)
131 {
132 std::weak_ptr<TachSensor> weakRef = weak_from_this();
133 waitTimer.expires_after(std::chrono::milliseconds(pollTime));
134 waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
135 if (ec == boost::asio::error::operation_aborted)
136 {
137 return; // we're being canceled
138 }
139 std::shared_ptr<TachSensor> self = weakRef.lock();
140 if (!self)
141 {
142 return;
143 }
144 self->setupRead();
145 });
146 }
147
handleResponse(const boost::system::error_code & err,size_t bytesRead)148 void TachSensor::handleResponse(const boost::system::error_code& err,
149 size_t bytesRead)
150 {
151 if ((err == boost::system::errc::bad_file_descriptor) ||
152 (err == boost::asio::error::misc_errors::not_found))
153 {
154 lg2::error("TachSensor '{NAME}' removed '{PATH}'", "NAME", name, "PATH",
155 path);
156 return; // we're being destroyed
157 }
158 bool missing = false;
159 size_t pollTime = pwmPollMs;
160 if (presence)
161 {
162 if (!presence->isPresent())
163 {
164 markAvailable(false);
165 missing = true;
166 pollTime = sensorFailedPollTimeMs;
167 }
168 itemIface->set_property("Present", !missing);
169 }
170
171 if (!missing)
172 {
173 if (!err)
174 {
175 const char* bufEnd = readBuf.data() + bytesRead;
176 int nvalue = 0;
177 std::from_chars_result ret =
178 std::from_chars(readBuf.data(), bufEnd, nvalue);
179 if (ret.ec != std::errc())
180 {
181 incrementError();
182 pollTime = sensorFailedPollTimeMs;
183 }
184 else
185 {
186 updateValue(nvalue);
187 }
188 }
189 else
190 {
191 incrementError();
192 pollTime = sensorFailedPollTimeMs;
193 }
194 }
195
196 restartRead(pollTime);
197 }
198
checkThresholds()199 void TachSensor::checkThresholds()
200 {
201 bool status = thresholds::checkThresholds(this);
202
203 if ((redundancy != nullptr) && *redundancy)
204 {
205 (*redundancy)
206 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
207 }
208
209 bool curLed = !status;
210 if (led && ledState != curLed)
211 {
212 ledState = curLed;
213 setLed(dbusConnection, *led, curLed);
214 }
215 }
216
RedundancySensor(size_t count,const std::vector<std::string> & children,sdbusplus::asio::object_server & objectServer,const std::string & sensorConfiguration)217 RedundancySensor::RedundancySensor(size_t count,
218 const std::vector<std::string>& children,
219 sdbusplus::asio::object_server& objectServer,
220 const std::string& sensorConfiguration) :
221 count(count),
222 iface(objectServer.add_interface(
223 "/xyz/openbmc_project/control/FanRedundancy/Tach",
224 "xyz.openbmc_project.Control.FanRedundancy")),
225 association(objectServer.add_interface(
226 "/xyz/openbmc_project/control/FanRedundancy/Tach",
227 association::interface)),
228 objectServer(objectServer)
229 {
230 createAssociation(association, sensorConfiguration);
231 iface->register_property("Collection", children);
232 iface->register_property("Status", std::string("Full"));
233 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
234 iface->initialize();
235 }
~RedundancySensor()236 RedundancySensor::~RedundancySensor()
237 {
238 objectServer.remove_interface(association);
239 objectServer.remove_interface(iface);
240 }
update(const std::string & name,bool failed)241 void RedundancySensor::update(const std::string& name, bool failed)
242 {
243 statuses[name] = failed;
244 size_t failedCount = 0;
245
246 std::string newState = redundancy::full;
247 for (const auto& [name, status] : statuses)
248 {
249 if (status)
250 {
251 failedCount++;
252 }
253 if (failedCount > count)
254 {
255 newState = redundancy::failed;
256 break;
257 }
258 if (failedCount != 0U)
259 {
260 newState = redundancy::degraded;
261 }
262 }
263 if (state != newState)
264 {
265 if (state == redundancy::full)
266 {
267 logFanRedundancyLost();
268 }
269 else if (newState == redundancy::full)
270 {
271 logFanRedundancyRestored();
272 }
273 state = newState;
274 iface->set_property("Status", state);
275 }
276 }
277