blob: 7c5b60abbe5c57a1b0a1e934da64923d3aaec33a [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 <PwmSensor.hpp>
18#include <TachSensor.hpp>
19#include <Utils.hpp>
20#include <VariantVisitors.hpp>
21#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/container/flat_set.hpp>
24#include <boost/lexical_cast.hpp>
James Feist24f02f22019-04-15 11:05:39 -070025#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070026#include <fstream>
27#include <regex>
28#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30
31static constexpr bool DEBUG = false;
32
James Feistcf3bce62019-01-08 10:07:19 -080033namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080034
Peter Lundgren8843b622019-09-12 10:33:41 -070035static constexpr std::array<const char*, 3> sensorTypes = {
James Feist95b079b2018-11-21 09:28:00 -080036 "xyz.openbmc_project.Configuration.AspeedFan",
Peter Lundgren8843b622019-09-12 10:33:41 -070037 "xyz.openbmc_project.Configuration.I2CFan",
38 "xyz.openbmc_project.Configuration.NuvotonFan"};
James Feistdc6c55f2018-10-31 12:53:20 -070039constexpr const char* redundancyConfiguration =
40 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070041static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070042
James Feist95b079b2018-11-21 09:28:00 -080043enum class FanTypes
44{
45 aspeed,
Peter Lundgren8843b622019-09-12 10:33:41 -070046 i2c,
47 nuvoton
James Feist95b079b2018-11-21 09:28:00 -080048};
49
James Feistdc6c55f2018-10-31 12:53:20 -070050// todo: power supply fan redundancy
James Feist7b18b1e2019-05-14 13:42:09 -070051std::optional<RedundancySensor> systemRedundancy;
James Feist95b079b2018-11-21 09:28:00 -080052
53FanTypes getFanType(const fs::path& parentPath)
54{
55 fs::path linkPath = parentPath / "device";
56 std::string canonical = fs::read_symlink(linkPath);
57 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller"))
58 {
59 return FanTypes::aspeed;
60 }
Peter Lundgren8843b622019-09-12 10:33:41 -070061 else if (boost::ends_with(canonical, "f0103000.pwm-fan-controller"))
62 {
63 return FanTypes::nuvoton;
64 }
James Feist95b079b2018-11-21 09:28:00 -080065 // todo: will we need to support other types?
66 return FanTypes::i2c;
67}
James Feistdc6c55f2018-10-31 12:53:20 -070068
James Feist6714a252018-09-10 15:26:18 -070069void createSensors(
70 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
71 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
72 tachSensors,
73 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
74 pwmSensors,
75 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
76 const std::unique_ptr<boost::container::flat_set<std::string>>&
77 sensorsChanged)
78{
James Feist6714a252018-09-10 15:26:18 -070079
James Feistde5e9702019-09-18 16:13:02 -070080 auto getter = std::make_shared<GetSensorConfiguration>(
81 dbusConnection,
82 std::move([&io, &objectServer, &tachSensors, &pwmSensors,
83 &dbusConnection, &sensorsChanged](
84 const ManagedObjectType& sensorConfigurations) {
85 bool firstScan = sensorsChanged == nullptr;
86 std::vector<fs::path> paths;
87 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)",
88 paths))
James Feist95b079b2018-11-21 09:28:00 -080089 {
James Feistde5e9702019-09-18 16:13:02 -070090 std::cerr << "No temperature sensors in system\n";
91 return;
James Feist95b079b2018-11-21 09:28:00 -080092 }
James Feist6714a252018-09-10 15:26:18 -070093
James Feistde5e9702019-09-18 16:13:02 -070094 std::vector<std::pair<uint8_t, std::string>> pwmNumbers;
95
96 // iterate through all found fan sensors, and try to match them with
97 // configuration
98 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -070099 {
James Feistde5e9702019-09-18 16:13:02 -0700100 std::smatch match;
101 std::string pathStr = path.string();
102
103 std::regex_search(pathStr, match, inputRegex);
104 std::string indexStr = *(match.begin() + 1);
105
106 auto directory = path.parent_path();
107 FanTypes fanType = getFanType(directory);
108 size_t bus = 0;
109 size_t address = 0;
110 if (fanType == FanTypes::i2c)
James Feist6714a252018-09-10 15:26:18 -0700111 {
James Feistde5e9702019-09-18 16:13:02 -0700112 std::string link =
113 fs::read_symlink(directory / "device").filename();
114
115 size_t findDash = link.find("-");
116 if (findDash == std::string::npos ||
117 link.size() <= findDash + 1)
118 {
119 std::cerr << "Error finding device from symlink";
120 }
121 bus = std::stoi(link.substr(0, findDash));
122 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
James Feist6714a252018-09-10 15:26:18 -0700123 }
James Feistde5e9702019-09-18 16:13:02 -0700124 // convert to 0 based
125 size_t index = std::stoul(indexStr) - 1;
126
127 const char* baseType;
128 const SensorData* sensorData = nullptr;
129 const std::string* interfacePath = nullptr;
130 const SensorBaseConfiguration* baseConfiguration = nullptr;
131 for (const std::pair<sdbusplus::message::object_path,
132 SensorData>& sensor : sensorConfigurations)
James Feist95b079b2018-11-21 09:28:00 -0800133 {
James Feistde5e9702019-09-18 16:13:02 -0700134 // find the base of the configuration to see if indexes
135 // match
136 for (const char* type : sensorTypes)
137 {
138 auto sensorBaseFind = sensor.second.find(type);
139 if (sensorBaseFind != sensor.second.end())
140 {
141 baseConfiguration = &(*sensorBaseFind);
142 interfacePath = &(sensor.first.str);
143 baseType = type;
144 break;
145 }
146 }
147 if (baseConfiguration == nullptr)
148 {
149 continue;
150 }
151 auto findIndex = baseConfiguration->second.find("Index");
152 if (findIndex == baseConfiguration->second.end())
153 {
154 std::cerr << baseConfiguration->first
155 << " missing index\n";
156 continue;
157 }
158 unsigned int configIndex = std::visit(
159 VariantToUnsignedIntVisitor(), findIndex->second);
160 if (configIndex != index)
161 {
162 continue;
163 }
164 if (fanType == FanTypes::aspeed ||
165 fanType == FanTypes::nuvoton)
166 {
167 // there will be only 1 aspeed or nuvoton sensor object
168 // in sysfs, we found the fan
169 sensorData = &(sensor.second);
170 break;
171 }
172 else if (baseType ==
173 std::string(
174 "xyz.openbmc_project.Configuration.I2CFan"))
175 {
176 auto findBus = baseConfiguration->second.find("Bus");
177 auto findAddress =
178 baseConfiguration->second.find("Address");
179 if (findBus == baseConfiguration->second.end() ||
180 findAddress == baseConfiguration->second.end())
181 {
182 std::cerr << baseConfiguration->first
183 << " missing bus or address\n";
184 continue;
185 }
186 unsigned int configBus = std::visit(
187 VariantToUnsignedIntVisitor(), findBus->second);
188 unsigned int configAddress = std::visit(
189 VariantToUnsignedIntVisitor(), findAddress->second);
190
191 if (configBus == bus && configAddress == address)
192 {
193 sensorData = &(sensor.second);
194 break;
195 }
196 }
197 }
198 if (sensorData == nullptr)
199 {
200 std::cerr << "failed to find match for " << path.string()
201 << "\n";
James Feist95b079b2018-11-21 09:28:00 -0800202 continue;
203 }
James Feist95b079b2018-11-21 09:28:00 -0800204
James Feistde5e9702019-09-18 16:13:02 -0700205 auto findSensorName = baseConfiguration->second.find("Name");
206 if (findSensorName == baseConfiguration->second.end())
James Feist95b079b2018-11-21 09:28:00 -0800207 {
James Feistde5e9702019-09-18 16:13:02 -0700208 std::cerr << "could not determine configuration name for "
209 << path.string() << "\n";
210 continue;
211 }
212 std::string sensorName =
213 std::get<std::string>(findSensorName->second);
214 // on rescans, only update sensors we were signaled by
215 auto findSensor = tachSensors.find(sensorName);
216 if (!firstScan && findSensor != tachSensors.end())
217 {
218 bool found = false;
219 for (auto it = sensorsChanged->begin();
220 it != sensorsChanged->end(); it++)
221 {
222 if (boost::ends_with(*it, findSensor->second->name))
223 {
224 sensorsChanged->erase(it);
225 findSensor->second = nullptr;
226 found = true;
227 break;
228 }
229 }
230 if (!found)
231 {
232 continue;
233 }
234 }
235 std::vector<thresholds::Threshold> sensorThresholds;
236 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
237 {
238 std::cerr << "error populating thresholds for "
239 << sensorName << "\n";
240 }
241
242 auto presenceConfig =
243 sensorData->find(baseType + std::string(".Presence"));
244
245 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
246
247 // presence sensors are optional
248 if (presenceConfig != sensorData->end())
249 {
250 auto findIndex = presenceConfig->second.find("Index");
251 auto findPolarity = presenceConfig->second.find("Polarity");
252
253 if (findIndex == presenceConfig->second.end() ||
254 findPolarity == presenceConfig->second.end())
255 {
256 std::cerr << "Malformed Presence Configuration\n";
257 }
258 else
259 {
260 size_t index = std::get<uint64_t>(findIndex->second);
261 bool inverted = std::get<std::string>(
262 findPolarity->second) == "Low";
263 presenceSensor = std::make_unique<PresenceSensor>(
264 index, inverted, io, sensorName);
265 }
266 }
267 std::optional<RedundancySensor>* redundancy = nullptr;
268 if (fanType == FanTypes::aspeed)
269 {
270 redundancy = &systemRedundancy;
271 }
272
273 constexpr double defaultMaxReading = 25000;
274 constexpr double defaultMinReading = 0;
275 auto limits =
276 std::make_pair(defaultMinReading, defaultMaxReading);
277
278 findLimits(limits, baseConfiguration);
279 tachSensors[sensorName] = std::make_unique<TachSensor>(
280 path.string(), baseType, objectServer, dbusConnection,
281 std::move(presenceSensor), redundancy, io, sensorName,
282 std::move(sensorThresholds), *interfacePath, limits);
283
284 auto connector =
285 sensorData->find(baseType + std::string(".Connector"));
286 if (connector != sensorData->end())
287 {
288 auto findPwm = connector->second.find("Pwm");
289 if (findPwm == connector->second.end())
290 {
291 std::cerr << "Connector Missing PWM!\n";
292 continue;
293 }
294
295 size_t pwm = std::visit(VariantToUnsignedIntVisitor(),
296 findPwm->second);
297 pwmNumbers.emplace_back(pwm, *interfacePath);
James Feist95b079b2018-11-21 09:28:00 -0800298 }
299 }
James Feistde5e9702019-09-18 16:13:02 -0700300 std::vector<fs::path> pwms;
301 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+$)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700302 {
James Feistde5e9702019-09-18 16:13:02 -0700303 std::cerr << "No pwm in system\n";
304 return;
305 }
306 for (const fs::path& pwm : pwms)
307 {
308 if (pwmSensors.find(pwm) != pwmSensors.end())
James Feist6714a252018-09-10 15:26:18 -0700309 {
James Feistde5e9702019-09-18 16:13:02 -0700310 continue;
James Feist6714a252018-09-10 15:26:18 -0700311 }
James Feistde5e9702019-09-18 16:13:02 -0700312 const std::string* path = nullptr;
313 for (const auto& [index, configPath] : pwmNumbers)
314 {
315 if (boost::ends_with(pwm.string(),
316 std::to_string(index + 1)))
317 {
318 path = &configPath;
319 break;
320 }
321 }
322
323 if (path == nullptr)
324 {
325 continue;
326 }
327
328 // only add new elements
329 const std::string& sysPath = pwm.string();
330 const std::string& pwmName =
331 "Pwm_" + sysPath.substr(sysPath.find_last_of("pwm") + 1);
332 pwmSensors.insert(
333 std::pair<std::string, std::unique_ptr<PwmSensor>>(
334 sysPath, std::make_unique<PwmSensor>(
335 pwmName, sysPath, objectServer, *path)));
James Feist6714a252018-09-10 15:26:18 -0700336 }
James Feistde5e9702019-09-18 16:13:02 -0700337 }));
338 getter->getConfiguration(
339 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700340}
341
James Feistdc6c55f2018-10-31 12:53:20 -0700342void createRedundancySensor(
343 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
344 sensors,
345 std::shared_ptr<sdbusplus::asio::connection> conn,
346 sdbusplus::asio::object_server& objectServer)
347{
348
349 conn->async_method_call(
350 [&objectServer, &sensors](boost::system::error_code& ec,
351 const ManagedObjectType managedObj) {
352 if (ec)
353 {
354 std::cerr << "Error calling entity manager \n";
355 return;
356 }
357 for (const auto& pathPair : managedObj)
358 {
359 for (const auto& interfacePair : pathPair.second)
360 {
361 if (interfacePair.first == redundancyConfiguration)
362 {
363 // currently only support one
364 auto findCount =
365 interfacePair.second.find("AllowedFailures");
366 if (findCount == interfacePair.second.end())
367 {
368 std::cerr << "Malformed redundancy record \n";
369 return;
370 }
371 std::vector<std::string> sensorList;
372
373 for (const auto& sensor : sensors)
374 {
375 sensorList.push_back(
376 "/xyz/openbmc_project/sensors/fan_tach/" +
377 sensor.second->name);
378 }
James Feist7b18b1e2019-05-14 13:42:09 -0700379 systemRedundancy.reset();
380 systemRedundancy.emplace(RedundancySensor(
James Feist3eb82622019-02-08 13:10:22 -0800381 std::get<uint64_t>(findCount->second), sensorList,
James Feist7b18b1e2019-05-14 13:42:09 -0700382 objectServer, pathPair.first));
James Feistdc6c55f2018-10-31 12:53:20 -0700383
384 return;
385 }
386 }
387 }
388 },
389 "xyz.openbmc_project.EntityManager", "/",
390 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
391}
392
James Feistb6c0b912019-07-09 12:21:44 -0700393int main()
James Feist6714a252018-09-10 15:26:18 -0700394{
395 boost::asio::io_service io;
396 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
397 systemBus->request_name("xyz.openbmc_project.FanSensor");
398 sdbusplus::asio::object_server objectServer(systemBus);
399 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
400 tachSensors;
401 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
402 pwmSensors;
403 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
404 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
405 std::make_unique<boost::container::flat_set<std::string>>();
406
407 io.post([&]() {
408 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
409 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700410 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700411 });
412
413 boost::asio::deadline_timer filterTimer(io);
414 std::function<void(sdbusplus::message::message&)> eventHandler =
415 [&](sdbusplus::message::message& message) {
416 if (message.is_method_error())
417 {
418 std::cerr << "callback method error\n";
419 return;
420 }
421 sensorsChanged->insert(message.get_path());
422 // this implicitly cancels the timer
423 filterTimer.expires_from_now(boost::posix_time::seconds(1));
424
425 filterTimer.async_wait([&](const boost::system::error_code& ec) {
426 if (ec == boost::asio::error::operation_aborted)
427 {
428 /* we were canceled*/
429 return;
430 }
431 else if (ec)
432 {
433 std::cerr << "timer error\n";
434 return;
435 }
436 createSensors(io, objectServer, tachSensors, pwmSensors,
437 systemBus, sensorsChanged);
438 });
439 };
440
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700441 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700442 {
443 auto match = std::make_unique<sdbusplus::bus::match::match>(
444 static_cast<sdbusplus::bus::bus&>(*systemBus),
445 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700446 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700447 eventHandler);
448 matches.emplace_back(std::move(match));
449 }
450
James Feistdc6c55f2018-10-31 12:53:20 -0700451 // redundancy sensor
452 std::function<void(sdbusplus::message::message&)> redundancyHandler =
453 [&tachSensors, &systemBus,
James Feistb6c0b912019-07-09 12:21:44 -0700454 &objectServer](sdbusplus::message::message&) {
James Feistdc6c55f2018-10-31 12:53:20 -0700455 createRedundancySensor(tachSensors, systemBus, objectServer);
456 };
457 auto match = std::make_unique<sdbusplus::bus::match::match>(
458 static_cast<sdbusplus::bus::bus&>(*systemBus),
459 "type='signal',member='PropertiesChanged',path_namespace='" +
460 std::string(inventoryPath) + "',arg0namespace='" +
461 redundancyConfiguration + "'",
James Feistb6c0b912019-07-09 12:21:44 -0700462 std::move(redundancyHandler));
James Feistdc6c55f2018-10-31 12:53:20 -0700463 matches.emplace_back(std::move(match));
464
James Feist6714a252018-09-10 15:26:18 -0700465 io.run();
466}