blob: 48ed692bc6c4c5d07ab4b6d4e9041ff2ac787aeb [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
James Feistcf3bce62019-01-08 10:07:19 -080017#include "filesystem.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <PwmSensor.hpp>
20#include <TachSensor.hpp>
21#include <Utils.hpp>
22#include <VariantVisitors.hpp>
23#include <boost/algorithm/string/predicate.hpp>
24#include <boost/algorithm/string/replace.hpp>
25#include <boost/container/flat_set.hpp>
26#include <boost/lexical_cast.hpp>
James Feist6714a252018-09-10 15:26:18 -070027#include <fstream>
28#include <regex>
29#include <sdbusplus/asio/connection.hpp>
30#include <sdbusplus/asio/object_server.hpp>
31
32static constexpr bool DEBUG = false;
33
James Feistcf3bce62019-01-08 10:07:19 -080034namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080035
James Feist95b079b2018-11-21 09:28:00 -080036static constexpr std::array<const char*, 2> sensorTypes = {
37 "xyz.openbmc_project.Configuration.AspeedFan",
38 "xyz.openbmc_project.Configuration.I2CFan"};
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,
46 i2c
47};
48
James Feistdc6c55f2018-10-31 12:53:20 -070049// todo: power supply fan redundancy
James Feist95b079b2018-11-21 09:28:00 -080050std::shared_ptr<RedundancySensor> systemRedundancy = nullptr;
51
52FanTypes getFanType(const fs::path& parentPath)
53{
54 fs::path linkPath = parentPath / "device";
55 std::string canonical = fs::read_symlink(linkPath);
56 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller"))
57 {
58 return FanTypes::aspeed;
59 }
60 // todo: will we need to support other types?
61 return FanTypes::i2c;
62}
James Feistdc6c55f2018-10-31 12:53:20 -070063
James Feist6714a252018-09-10 15:26:18 -070064void createSensors(
65 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
66 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
67 tachSensors,
68 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
69 pwmSensors,
70 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
71 const std::unique_ptr<boost::container::flat_set<std::string>>&
72 sensorsChanged)
73{
74 bool firstScan = sensorsChanged == nullptr;
75 // use new data the first time, then refresh
76 ManagedObjectType sensorConfigurations;
77 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070078 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070079 {
80 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
81 useCache))
82 {
83 std::cerr << "error communicating to entity manager\n";
84 return;
85 }
86 useCache = true;
87 }
88 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070090 {
91 std::cerr << "No temperature sensors in system\n";
92 return;
93 }
94
95 // iterate through all found fan sensors, and try to match them with
96 // configuration
James Feist95b079b2018-11-21 09:28:00 -080097 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -070098 {
99 std::smatch match;
100 std::string pathStr = path.string();
101
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700102 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -0700103 std::string indexStr = *(match.begin() + 1);
104
105 auto directory = path.parent_path();
James Feist95b079b2018-11-21 09:28:00 -0800106 FanTypes fanType = getFanType(directory);
107 size_t bus = 0;
108 size_t address = 0;
109 if (fanType == FanTypes::i2c)
110 {
111 std::string link =
112 fs::read_symlink(directory / "device").filename();
113
114 size_t findDash = link.find("-");
115 if (findDash == std::string::npos || link.size() <= findDash + 1)
116 {
117 std::cerr << "Error finding device from symlink";
118 }
119 bus = std::stoi(link.substr(0, findDash));
120 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
121 }
James Feist6714a252018-09-10 15:26:18 -0700122 // convert to 0 based
123 size_t index = std::stoul(indexStr) - 1;
124
125 const char* baseType;
126 const SensorData* sensorData = nullptr;
127 const std::string* interfacePath = nullptr;
James Feist87d713a2018-12-06 16:06:24 -0800128 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700129 for (const std::pair<sdbusplus::message::object_path, SensorData>&
130 sensor : sensorConfigurations)
131 {
132 // find the base of the configuration to see if indexes match
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700133 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700134 {
135 auto sensorBaseFind = sensor.second.find(type);
136 if (sensorBaseFind != sensor.second.end())
137 {
138 baseConfiguration = &(*sensorBaseFind);
139 interfacePath = &(sensor.first.str);
140 baseType = type;
141 break;
142 }
143 }
144 if (baseConfiguration == nullptr)
145 {
146 continue;
147 }
James Feist6714a252018-09-10 15:26:18 -0700148 auto findIndex = baseConfiguration->second.find("Index");
149 if (findIndex == baseConfiguration->second.end())
150 {
151 std::cerr << baseConfiguration->first << " missing index\n";
152 continue;
153 }
James Feist3eb82622019-02-08 13:10:22 -0800154 unsigned int configIndex =
155 std::visit(VariantToUnsignedIntVisitor(), findIndex->second);
James Feist6714a252018-09-10 15:26:18 -0700156 if (configIndex != index)
157 {
158 continue;
159 }
James Feist95b079b2018-11-21 09:28:00 -0800160 if (fanType == FanTypes::aspeed)
James Feist6714a252018-09-10 15:26:18 -0700161 {
James Feist95b079b2018-11-21 09:28:00 -0800162 // there will be only 1 aspeed sensor object in sysfs, we found
163 // the fan
James Feist6714a252018-09-10 15:26:18 -0700164 sensorData = &(sensor.second);
165 break;
166 }
James Feist95b079b2018-11-21 09:28:00 -0800167 else if (baseType == "xyz.openbmc_project.Configuration.I2CFan")
168 {
169 auto findBus = baseConfiguration->second.find("Bus");
170 auto findAddress = baseConfiguration->second.find("Address");
171 if (findBus == baseConfiguration->second.end() ||
172 findAddress == baseConfiguration->second.end())
173 {
174 std::cerr << baseConfiguration->first
175 << " missing bus or address\n";
176 continue;
177 }
James Feist3eb82622019-02-08 13:10:22 -0800178 unsigned int configBus =
179 std::visit(VariantToUnsignedIntVisitor(), findBus->second);
180 unsigned int configAddress = std::visit(
James Feist95b079b2018-11-21 09:28:00 -0800181 VariantToUnsignedIntVisitor(), findAddress->second);
182
183 if (configBus == bus && configAddress == configAddress)
184 {
185 sensorData = &(sensor.second);
186 break;
187 }
188 }
James Feist6714a252018-09-10 15:26:18 -0700189 }
190 if (sensorData == nullptr)
191 {
192 std::cerr << "failed to find match for " << path.string() << "\n";
193 continue;
194 }
195
196 auto findSensorName = baseConfiguration->second.find("Name");
197 if (findSensorName == baseConfiguration->second.end())
198 {
199 std::cerr << "could not determine configuration name for "
200 << path.string() << "\n";
201 continue;
202 }
James Feist3eb82622019-02-08 13:10:22 -0800203 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700204 // on rescans, only update sensors we were signaled by
205 auto findSensor = tachSensors.find(sensorName);
206 if (!firstScan && findSensor != tachSensors.end())
207 {
208 bool found = false;
209 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
210 it++)
211 {
212 if (boost::ends_with(*it, findSensor->second->name))
213 {
214 sensorsChanged->erase(it);
215 findSensor->second = nullptr;
216 found = true;
217 break;
218 }
219 }
220 if (!found)
221 {
222 continue;
223 }
224 }
225 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700226 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700227 {
228 std::cerr << "error populating thresholds for " << sensorName
229 << "\n";
230 }
231
James Feist7bc2bab2018-10-26 14:09:45 -0700232 auto presenceConfig =
233 sensorData->find(baseType + std::string(".Presence"));
234
235 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
236
237 // presence sensors are optional
238 if (presenceConfig != sensorData->end())
239 {
240 auto findIndex = presenceConfig->second.find("Index");
241 auto findPolarity = presenceConfig->second.find("Polarity");
242
243 if (findIndex == presenceConfig->second.end() ||
244 findPolarity == presenceConfig->second.end())
245 {
246 std::cerr << "Malformed Presence Configuration\n";
247 }
248 else
249 {
James Feist3eb82622019-02-08 13:10:22 -0800250 size_t index = std::get<uint64_t>(findIndex->second);
James Feist7bc2bab2018-10-26 14:09:45 -0700251 bool inverted =
James Feist3eb82622019-02-08 13:10:22 -0800252 std::get<std::string>(findPolarity->second) == "Low";
James Feist7bc2bab2018-10-26 14:09:45 -0700253 presenceSensor =
254 std::make_unique<PresenceSensor>(index, inverted, io);
255 }
256 }
James Feist95b079b2018-11-21 09:28:00 -0800257 std::shared_ptr<RedundancySensor> redundancy;
258 if (fanType == FanTypes::aspeed)
259 {
260 redundancy = systemRedundancy;
261 }
James Feist7bc2bab2018-10-26 14:09:45 -0700262
James Feist87d713a2018-12-06 16:06:24 -0800263 constexpr double defaultMaxReading = 25000;
264 constexpr double defaultMinReading = 0;
265 auto limits = std::make_pair(defaultMinReading, defaultMaxReading);
266
267 findLimits(limits, baseConfiguration);
James Feist6714a252018-09-10 15:26:18 -0700268 tachSensors[sensorName] = std::make_unique<TachSensor>(
James Feistce3fca42018-11-21 12:58:24 -0800269 path.string(), baseType, objectServer, dbusConnection,
James Feist95b079b2018-11-21 09:28:00 -0800270 std::move(presenceSensor), redundancy, io, sensorName,
James Feist87d713a2018-12-06 16:06:24 -0800271 std::move(sensorThresholds), *interfacePath, limits);
James Feist6714a252018-09-10 15:26:18 -0700272 }
273 std::vector<fs::path> pwms;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700274 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700275 {
276 std::cerr << "No pwm in system\n";
277 return;
278 }
279 for (const fs::path& pwm : pwms)
280 {
James Feist95b079b2018-11-21 09:28:00 -0800281 if (pwmSensors.find(pwm) != pwmSensors.end())
282 {
283 continue;
284 }
James Feist6714a252018-09-10 15:26:18 -0700285 // only add new elements
286 pwmSensors.insert(std::pair<std::string, std::unique_ptr<PwmSensor>>(
287 pwm.string(),
288 std::make_unique<PwmSensor>(pwm.string(), objectServer)));
289 }
290}
291
James Feistdc6c55f2018-10-31 12:53:20 -0700292void createRedundancySensor(
293 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
294 sensors,
295 std::shared_ptr<sdbusplus::asio::connection> conn,
296 sdbusplus::asio::object_server& objectServer)
297{
298
299 conn->async_method_call(
300 [&objectServer, &sensors](boost::system::error_code& ec,
301 const ManagedObjectType managedObj) {
302 if (ec)
303 {
304 std::cerr << "Error calling entity manager \n";
305 return;
306 }
307 for (const auto& pathPair : managedObj)
308 {
309 for (const auto& interfacePair : pathPair.second)
310 {
311 if (interfacePair.first == redundancyConfiguration)
312 {
313 // currently only support one
314 auto findCount =
315 interfacePair.second.find("AllowedFailures");
316 if (findCount == interfacePair.second.end())
317 {
318 std::cerr << "Malformed redundancy record \n";
319 return;
320 }
321 std::vector<std::string> sensorList;
322
323 for (const auto& sensor : sensors)
324 {
325 sensorList.push_back(
326 "/xyz/openbmc_project/sensors/fan_tach/" +
327 sensor.second->name);
328 }
329 systemRedundancy = std::make_unique<RedundancySensor>(
James Feist3eb82622019-02-08 13:10:22 -0800330 std::get<uint64_t>(findCount->second), sensorList,
331 objectServer);
James Feistdc6c55f2018-10-31 12:53:20 -0700332
333 return;
334 }
335 }
336 }
337 },
338 "xyz.openbmc_project.EntityManager", "/",
339 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
340}
341
James Feist6714a252018-09-10 15:26:18 -0700342int main(int argc, char** argv)
343{
344 boost::asio::io_service io;
345 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
346 systemBus->request_name("xyz.openbmc_project.FanSensor");
347 sdbusplus::asio::object_server objectServer(systemBus);
348 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
349 tachSensors;
350 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
351 pwmSensors;
352 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
353 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
354 std::make_unique<boost::container::flat_set<std::string>>();
355
356 io.post([&]() {
357 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
358 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700359 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700360 });
361
362 boost::asio::deadline_timer filterTimer(io);
363 std::function<void(sdbusplus::message::message&)> eventHandler =
364 [&](sdbusplus::message::message& message) {
365 if (message.is_method_error())
366 {
367 std::cerr << "callback method error\n";
368 return;
369 }
370 sensorsChanged->insert(message.get_path());
371 // this implicitly cancels the timer
372 filterTimer.expires_from_now(boost::posix_time::seconds(1));
373
374 filterTimer.async_wait([&](const boost::system::error_code& ec) {
375 if (ec == boost::asio::error::operation_aborted)
376 {
377 /* we were canceled*/
378 return;
379 }
380 else if (ec)
381 {
382 std::cerr << "timer error\n";
383 return;
384 }
385 createSensors(io, objectServer, tachSensors, pwmSensors,
386 systemBus, sensorsChanged);
387 });
388 };
389
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700390 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700391 {
392 auto match = std::make_unique<sdbusplus::bus::match::match>(
393 static_cast<sdbusplus::bus::bus&>(*systemBus),
394 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700395 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700396 eventHandler);
397 matches.emplace_back(std::move(match));
398 }
399
James Feistdc6c55f2018-10-31 12:53:20 -0700400 // redundancy sensor
401 std::function<void(sdbusplus::message::message&)> redundancyHandler =
402 [&tachSensors, &systemBus,
403 &objectServer](sdbusplus::message::message& message) {
404 createRedundancySensor(tachSensors, systemBus, objectServer);
405 };
406 auto match = std::make_unique<sdbusplus::bus::match::match>(
407 static_cast<sdbusplus::bus::bus&>(*systemBus),
408 "type='signal',member='PropertiesChanged',path_namespace='" +
409 std::string(inventoryPath) + "',arg0namespace='" +
410 redundancyConfiguration + "'",
411 redundancyHandler);
412 matches.emplace_back(std::move(match));
413
James Feist6714a252018-09-10 15:26:18 -0700414 io.run();
415}