blob: 121621d882f21184a2d27c167021980a0d710af7 [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>
25#include <experimental/filesystem>
26#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
33namespace fs = std::experimental::filesystem;
Yoo, Jae Hyun50938052018-10-17 18:19:02 -070034namespace variant_ns = sdbusplus::message::variant_ns;
James Feist95b079b2018-11-21 09:28:00 -080035static constexpr std::array<const char*, 2> sensorTypes = {
36 "xyz.openbmc_project.Configuration.AspeedFan",
37 "xyz.openbmc_project.Configuration.I2CFan"};
James Feistdc6c55f2018-10-31 12:53:20 -070038constexpr const char* redundancyConfiguration =
39 "xyz.openbmc_project.Configuration.FanRedundancy";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070040static std::regex inputRegex(R"(fan(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070041
James Feist95b079b2018-11-21 09:28:00 -080042enum class FanTypes
43{
44 aspeed,
45 i2c
46};
47
James Feistdc6c55f2018-10-31 12:53:20 -070048// todo: power supply fan redundancy
James Feist95b079b2018-11-21 09:28:00 -080049std::shared_ptr<RedundancySensor> systemRedundancy = nullptr;
50
51FanTypes getFanType(const fs::path& parentPath)
52{
53 fs::path linkPath = parentPath / "device";
54 std::string canonical = fs::read_symlink(linkPath);
55 if (boost::ends_with(canonical, "1e786000.pwm-tacho-controller"))
56 {
57 return FanTypes::aspeed;
58 }
59 // todo: will we need to support other types?
60 return FanTypes::i2c;
61}
James Feistdc6c55f2018-10-31 12:53:20 -070062
James Feist6714a252018-09-10 15:26:18 -070063void createSensors(
64 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
65 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
66 tachSensors,
67 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>&
68 pwmSensors,
69 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
70 const std::unique_ptr<boost::container::flat_set<std::string>>&
71 sensorsChanged)
72{
73 bool firstScan = sensorsChanged == nullptr;
74 // use new data the first time, then refresh
75 ManagedObjectType sensorConfigurations;
76 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070077 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070078 {
79 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
80 useCache))
81 {
82 std::cerr << "error communicating to entity manager\n";
83 return;
84 }
85 useCache = true;
86 }
87 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070088 if (!findFiles(fs::path("/sys/class/hwmon"), R"(fan\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070089 {
90 std::cerr << "No temperature sensors in system\n";
91 return;
92 }
93
94 // iterate through all found fan sensors, and try to match them with
95 // configuration
James Feist95b079b2018-11-21 09:28:00 -080096 for (const auto& path : paths)
James Feist6714a252018-09-10 15:26:18 -070097 {
98 std::smatch match;
99 std::string pathStr = path.string();
100
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -0700102 std::string indexStr = *(match.begin() + 1);
103
104 auto directory = path.parent_path();
James Feist95b079b2018-11-21 09:28:00 -0800105 FanTypes fanType = getFanType(directory);
106 size_t bus = 0;
107 size_t address = 0;
108 if (fanType == FanTypes::i2c)
109 {
110 std::string link =
111 fs::read_symlink(directory / "device").filename();
112
113 size_t findDash = link.find("-");
114 if (findDash == std::string::npos || link.size() <= findDash + 1)
115 {
116 std::cerr << "Error finding device from symlink";
117 }
118 bus = std::stoi(link.substr(0, findDash));
119 address = std::stoi(link.substr(findDash + 1), nullptr, 16);
120 }
James Feist6714a252018-09-10 15:26:18 -0700121 // convert to 0 based
122 size_t index = std::stoul(indexStr) - 1;
123
124 const char* baseType;
125 const SensorData* sensorData = nullptr;
126 const std::string* interfacePath = nullptr;
James Feist87d713a2018-12-06 16:06:24 -0800127 const SensorBaseConfiguration* baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700128 for (const std::pair<sdbusplus::message::object_path, SensorData>&
129 sensor : sensorConfigurations)
130 {
131 // find the base of the configuration to see if indexes match
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700132 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700133 {
134 auto sensorBaseFind = sensor.second.find(type);
135 if (sensorBaseFind != sensor.second.end())
136 {
137 baseConfiguration = &(*sensorBaseFind);
138 interfacePath = &(sensor.first.str);
139 baseType = type;
140 break;
141 }
142 }
143 if (baseConfiguration == nullptr)
144 {
145 continue;
146 }
James Feist6714a252018-09-10 15:26:18 -0700147 auto findIndex = baseConfiguration->second.find("Index");
148 if (findIndex == baseConfiguration->second.end())
149 {
150 std::cerr << baseConfiguration->first << " missing index\n";
151 continue;
152 }
Yoo, Jae Hyun50938052018-10-17 18:19:02 -0700153 unsigned int configIndex = variant_ns::visit(
James Feist6714a252018-09-10 15:26:18 -0700154 VariantToUnsignedIntVisitor(), findIndex->second);
James Feist6714a252018-09-10 15:26:18 -0700155 if (configIndex != index)
156 {
157 continue;
158 }
James Feist95b079b2018-11-21 09:28:00 -0800159 if (fanType == FanTypes::aspeed)
James Feist6714a252018-09-10 15:26:18 -0700160 {
James Feist95b079b2018-11-21 09:28:00 -0800161 // there will be only 1 aspeed sensor object in sysfs, we found
162 // the fan
James Feist6714a252018-09-10 15:26:18 -0700163 sensorData = &(sensor.second);
164 break;
165 }
James Feist95b079b2018-11-21 09:28:00 -0800166 else if (baseType == "xyz.openbmc_project.Configuration.I2CFan")
167 {
168 auto findBus = baseConfiguration->second.find("Bus");
169 auto findAddress = baseConfiguration->second.find("Address");
170 if (findBus == baseConfiguration->second.end() ||
171 findAddress == baseConfiguration->second.end())
172 {
173 std::cerr << baseConfiguration->first
174 << " missing bus or address\n";
175 continue;
176 }
177 unsigned int configBus = variant_ns::visit(
178 VariantToUnsignedIntVisitor(), findBus->second);
179 unsigned int configAddress = variant_ns::visit(
180 VariantToUnsignedIntVisitor(), findAddress->second);
181
182 if (configBus == bus && configAddress == configAddress)
183 {
184 sensorData = &(sensor.second);
185 break;
186 }
187 }
James Feist6714a252018-09-10 15:26:18 -0700188 }
189 if (sensorData == nullptr)
190 {
191 std::cerr << "failed to find match for " << path.string() << "\n";
192 continue;
193 }
194
195 auto findSensorName = baseConfiguration->second.find("Name");
196 if (findSensorName == baseConfiguration->second.end())
197 {
198 std::cerr << "could not determine configuration name for "
199 << path.string() << "\n";
200 continue;
201 }
202 std::string sensorName =
203 sdbusplus::message::variant_ns::get<std::string>(
204 findSensorName->second);
205 // on rescans, only update sensors we were signaled by
206 auto findSensor = tachSensors.find(sensorName);
207 if (!firstScan && findSensor != tachSensors.end())
208 {
209 bool found = false;
210 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
211 it++)
212 {
213 if (boost::ends_with(*it, findSensor->second->name))
214 {
215 sensorsChanged->erase(it);
216 findSensor->second = nullptr;
217 found = true;
218 break;
219 }
220 }
221 if (!found)
222 {
223 continue;
224 }
225 }
226 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700227 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700228 {
229 std::cerr << "error populating thresholds for " << sensorName
230 << "\n";
231 }
232
James Feist7bc2bab2018-10-26 14:09:45 -0700233 auto presenceConfig =
234 sensorData->find(baseType + std::string(".Presence"));
235
236 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
237
238 // presence sensors are optional
239 if (presenceConfig != sensorData->end())
240 {
241 auto findIndex = presenceConfig->second.find("Index");
242 auto findPolarity = presenceConfig->second.find("Polarity");
243
244 if (findIndex == presenceConfig->second.end() ||
245 findPolarity == presenceConfig->second.end())
246 {
247 std::cerr << "Malformed Presence Configuration\n";
248 }
249 else
250 {
251 size_t index = variant_ns::get<uint64_t>(findIndex->second);
252 bool inverted =
253 variant_ns::get<std::string>(findPolarity->second) == "Low";
254 presenceSensor =
255 std::make_unique<PresenceSensor>(index, inverted, io);
256 }
257 }
James Feist95b079b2018-11-21 09:28:00 -0800258 std::shared_ptr<RedundancySensor> redundancy;
259 if (fanType == FanTypes::aspeed)
260 {
261 redundancy = systemRedundancy;
262 }
James Feist7bc2bab2018-10-26 14:09:45 -0700263
James Feist87d713a2018-12-06 16:06:24 -0800264 constexpr double defaultMaxReading = 25000;
265 constexpr double defaultMinReading = 0;
266 auto limits = std::make_pair(defaultMinReading, defaultMaxReading);
267
268 findLimits(limits, baseConfiguration);
James Feist6714a252018-09-10 15:26:18 -0700269 tachSensors[sensorName] = std::make_unique<TachSensor>(
James Feistce3fca42018-11-21 12:58:24 -0800270 path.string(), baseType, objectServer, dbusConnection,
James Feist95b079b2018-11-21 09:28:00 -0800271 std::move(presenceSensor), redundancy, io, sensorName,
James Feist87d713a2018-12-06 16:06:24 -0800272 std::move(sensorThresholds), *interfacePath, limits);
James Feist6714a252018-09-10 15:26:18 -0700273 }
274 std::vector<fs::path> pwms;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700275 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700276 {
277 std::cerr << "No pwm in system\n";
278 return;
279 }
280 for (const fs::path& pwm : pwms)
281 {
James Feist95b079b2018-11-21 09:28:00 -0800282 if (pwmSensors.find(pwm) != pwmSensors.end())
283 {
284 continue;
285 }
James Feist6714a252018-09-10 15:26:18 -0700286 // only add new elements
287 pwmSensors.insert(std::pair<std::string, std::unique_ptr<PwmSensor>>(
288 pwm.string(),
289 std::make_unique<PwmSensor>(pwm.string(), objectServer)));
290 }
291}
292
James Feistdc6c55f2018-10-31 12:53:20 -0700293void createRedundancySensor(
294 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
295 sensors,
296 std::shared_ptr<sdbusplus::asio::connection> conn,
297 sdbusplus::asio::object_server& objectServer)
298{
299
300 conn->async_method_call(
301 [&objectServer, &sensors](boost::system::error_code& ec,
302 const ManagedObjectType managedObj) {
303 if (ec)
304 {
305 std::cerr << "Error calling entity manager \n";
306 return;
307 }
308 for (const auto& pathPair : managedObj)
309 {
310 for (const auto& interfacePair : pathPair.second)
311 {
312 if (interfacePair.first == redundancyConfiguration)
313 {
314 // currently only support one
315 auto findCount =
316 interfacePair.second.find("AllowedFailures");
317 if (findCount == interfacePair.second.end())
318 {
319 std::cerr << "Malformed redundancy record \n";
320 return;
321 }
322 std::vector<std::string> sensorList;
323
324 for (const auto& sensor : sensors)
325 {
326 sensorList.push_back(
327 "/xyz/openbmc_project/sensors/fan_tach/" +
328 sensor.second->name);
329 }
330 systemRedundancy = std::make_unique<RedundancySensor>(
331 variant_ns::get<uint64_t>(findCount->second),
332 sensorList, objectServer);
333
334 return;
335 }
336 }
337 }
338 },
339 "xyz.openbmc_project.EntityManager", "/",
340 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
341}
342
James Feist6714a252018-09-10 15:26:18 -0700343int main(int argc, char** argv)
344{
345 boost::asio::io_service io;
346 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
347 systemBus->request_name("xyz.openbmc_project.FanSensor");
348 sdbusplus::asio::object_server objectServer(systemBus);
349 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
350 tachSensors;
351 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
352 pwmSensors;
353 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
354 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
355 std::make_unique<boost::container::flat_set<std::string>>();
356
357 io.post([&]() {
358 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
359 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700360 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700361 });
362
363 boost::asio::deadline_timer filterTimer(io);
364 std::function<void(sdbusplus::message::message&)> eventHandler =
365 [&](sdbusplus::message::message& message) {
366 if (message.is_method_error())
367 {
368 std::cerr << "callback method error\n";
369 return;
370 }
371 sensorsChanged->insert(message.get_path());
372 // this implicitly cancels the timer
373 filterTimer.expires_from_now(boost::posix_time::seconds(1));
374
375 filterTimer.async_wait([&](const boost::system::error_code& ec) {
376 if (ec == boost::asio::error::operation_aborted)
377 {
378 /* we were canceled*/
379 return;
380 }
381 else if (ec)
382 {
383 std::cerr << "timer error\n";
384 return;
385 }
386 createSensors(io, objectServer, tachSensors, pwmSensors,
387 systemBus, sensorsChanged);
388 });
389 };
390
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700391 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700392 {
393 auto match = std::make_unique<sdbusplus::bus::match::match>(
394 static_cast<sdbusplus::bus::bus&>(*systemBus),
395 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700396 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700397 eventHandler);
398 matches.emplace_back(std::move(match));
399 }
400
James Feistdc6c55f2018-10-31 12:53:20 -0700401 // redundancy sensor
402 std::function<void(sdbusplus::message::message&)> redundancyHandler =
403 [&tachSensors, &systemBus,
404 &objectServer](sdbusplus::message::message& message) {
405 createRedundancySensor(tachSensors, systemBus, objectServer);
406 };
407 auto match = std::make_unique<sdbusplus::bus::match::match>(
408 static_cast<sdbusplus::bus::bus&>(*systemBus),
409 "type='signal',member='PropertiesChanged',path_namespace='" +
410 std::string(inventoryPath) + "',arg0namespace='" +
411 redundancyConfiguration + "'",
412 redundancyHandler);
413 matches.emplace_back(std::move(match));
414
James Feist6714a252018-09-10 15:26:18 -0700415 io.run();
416}