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 <sdbusplus/asio/connection.hpp>
30 #include <sdbusplus/asio/object_server.hpp>
31
32 #include <charconv>
33 #include <chrono>
34 #include <cstddef>
35 #include <cstdint>
36 #include <iostream>
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 std::cerr << "TachSensor " << name << " removed " << path << "\n";
155 return; // we're being destroyed
156 }
157 bool missing = false;
158 size_t pollTime = pwmPollMs;
159 if (presence)
160 {
161 if (!presence->isPresent())
162 {
163 markAvailable(false);
164 missing = true;
165 pollTime = sensorFailedPollTimeMs;
166 }
167 itemIface->set_property("Present", !missing);
168 }
169
170 if (!missing)
171 {
172 if (!err)
173 {
174 const char* bufEnd = readBuf.data() + bytesRead;
175 int nvalue = 0;
176 std::from_chars_result ret =
177 std::from_chars(readBuf.data(), bufEnd, nvalue);
178 if (ret.ec != std::errc())
179 {
180 incrementError();
181 pollTime = sensorFailedPollTimeMs;
182 }
183 else
184 {
185 updateValue(nvalue);
186 }
187 }
188 else
189 {
190 incrementError();
191 pollTime = sensorFailedPollTimeMs;
192 }
193 }
194
195 restartRead(pollTime);
196 }
197
checkThresholds()198 void TachSensor::checkThresholds()
199 {
200 bool status = thresholds::checkThresholds(this);
201
202 if ((redundancy != nullptr) && *redundancy)
203 {
204 (*redundancy)
205 ->update("/xyz/openbmc_project/sensors/fan_tach/" + name, !status);
206 }
207
208 bool curLed = !status;
209 if (led && ledState != curLed)
210 {
211 ledState = curLed;
212 setLed(dbusConnection, *led, curLed);
213 }
214 }
215
RedundancySensor(size_t count,const std::vector<std::string> & children,sdbusplus::asio::object_server & objectServer,const std::string & sensorConfiguration)216 RedundancySensor::RedundancySensor(size_t count,
217 const std::vector<std::string>& children,
218 sdbusplus::asio::object_server& objectServer,
219 const std::string& sensorConfiguration) :
220 count(count),
221 iface(objectServer.add_interface(
222 "/xyz/openbmc_project/control/FanRedundancy/Tach",
223 "xyz.openbmc_project.Control.FanRedundancy")),
224 association(objectServer.add_interface(
225 "/xyz/openbmc_project/control/FanRedundancy/Tach",
226 association::interface)),
227 objectServer(objectServer)
228 {
229 createAssociation(association, sensorConfiguration);
230 iface->register_property("Collection", children);
231 iface->register_property("Status", std::string("Full"));
232 iface->register_property("AllowedFailures", static_cast<uint8_t>(count));
233 iface->initialize();
234 }
~RedundancySensor()235 RedundancySensor::~RedundancySensor()
236 {
237 objectServer.remove_interface(association);
238 objectServer.remove_interface(iface);
239 }
update(const std::string & name,bool failed)240 void RedundancySensor::update(const std::string& name, bool failed)
241 {
242 statuses[name] = failed;
243 size_t failedCount = 0;
244
245 std::string newState = redundancy::full;
246 for (const auto& [name, status] : statuses)
247 {
248 if (status)
249 {
250 failedCount++;
251 }
252 if (failedCount > count)
253 {
254 newState = redundancy::failed;
255 break;
256 }
257 if (failedCount != 0U)
258 {
259 newState = redundancy::degraded;
260 }
261 }
262 if (state != newState)
263 {
264 if (state == redundancy::full)
265 {
266 logFanRedundancyLost();
267 }
268 else if (newState == redundancy::full)
269 {
270 logFanRedundancyRestored();
271 }
272 state = newState;
273 iface->set_property("Status", state);
274 }
275 }
276