blob: fd5e7af08194dd20fa1785d616fc007afbcd5173 [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;
Yoo, Jae Hyun50938052018-10-17 18:19:02 -070035namespace variant_ns = sdbusplus::message::variant_ns;
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 }
Yoo, Jae Hyun50938052018-10-17 18:19:02 -0700154 unsigned int configIndex = variant_ns::visit(
James Feist6714a252018-09-10 15:26:18 -0700155 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 }
178 unsigned int configBus = variant_ns::visit(
179 VariantToUnsignedIntVisitor(), findBus->second);
180 unsigned int configAddress = variant_ns::visit(
181 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 }
203 std::string sensorName =
204 sdbusplus::message::variant_ns::get<std::string>(
205 findSensorName->second);
206 // on rescans, only update sensors we were signaled by
207 auto findSensor = tachSensors.find(sensorName);
208 if (!firstScan && findSensor != tachSensors.end())
209 {
210 bool found = false;
211 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
212 it++)
213 {
214 if (boost::ends_with(*it, findSensor->second->name))
215 {
216 sensorsChanged->erase(it);
217 findSensor->second = nullptr;
218 found = true;
219 break;
220 }
221 }
222 if (!found)
223 {
224 continue;
225 }
226 }
227 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700228 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700229 {
230 std::cerr << "error populating thresholds for " << sensorName
231 << "\n";
232 }
233
James Feist7bc2bab2018-10-26 14:09:45 -0700234 auto presenceConfig =
235 sensorData->find(baseType + std::string(".Presence"));
236
237 std::unique_ptr<PresenceSensor> presenceSensor(nullptr);
238
239 // presence sensors are optional
240 if (presenceConfig != sensorData->end())
241 {
242 auto findIndex = presenceConfig->second.find("Index");
243 auto findPolarity = presenceConfig->second.find("Polarity");
244
245 if (findIndex == presenceConfig->second.end() ||
246 findPolarity == presenceConfig->second.end())
247 {
248 std::cerr << "Malformed Presence Configuration\n";
249 }
250 else
251 {
252 size_t index = variant_ns::get<uint64_t>(findIndex->second);
253 bool inverted =
254 variant_ns::get<std::string>(findPolarity->second) == "Low";
255 presenceSensor =
256 std::make_unique<PresenceSensor>(index, inverted, io);
257 }
258 }
James Feist95b079b2018-11-21 09:28:00 -0800259 std::shared_ptr<RedundancySensor> redundancy;
260 if (fanType == FanTypes::aspeed)
261 {
262 redundancy = systemRedundancy;
263 }
James Feist7bc2bab2018-10-26 14:09:45 -0700264
James Feist87d713a2018-12-06 16:06:24 -0800265 constexpr double defaultMaxReading = 25000;
266 constexpr double defaultMinReading = 0;
267 auto limits = std::make_pair(defaultMinReading, defaultMaxReading);
268
269 findLimits(limits, baseConfiguration);
James Feist6714a252018-09-10 15:26:18 -0700270 tachSensors[sensorName] = std::make_unique<TachSensor>(
James Feistce3fca42018-11-21 12:58:24 -0800271 path.string(), baseType, objectServer, dbusConnection,
James Feist95b079b2018-11-21 09:28:00 -0800272 std::move(presenceSensor), redundancy, io, sensorName,
James Feist87d713a2018-12-06 16:06:24 -0800273 std::move(sensorThresholds), *interfacePath, limits);
James Feist6714a252018-09-10 15:26:18 -0700274 }
275 std::vector<fs::path> pwms;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700276 if (!findFiles(fs::path("/sys/class/hwmon"), R"(pwm\d+)", pwms))
James Feist6714a252018-09-10 15:26:18 -0700277 {
278 std::cerr << "No pwm in system\n";
279 return;
280 }
281 for (const fs::path& pwm : pwms)
282 {
James Feist95b079b2018-11-21 09:28:00 -0800283 if (pwmSensors.find(pwm) != pwmSensors.end())
284 {
285 continue;
286 }
James Feist6714a252018-09-10 15:26:18 -0700287 // only add new elements
288 pwmSensors.insert(std::pair<std::string, std::unique_ptr<PwmSensor>>(
289 pwm.string(),
290 std::make_unique<PwmSensor>(pwm.string(), objectServer)));
291 }
292}
293
James Feistdc6c55f2018-10-31 12:53:20 -0700294void createRedundancySensor(
295 const boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>&
296 sensors,
297 std::shared_ptr<sdbusplus::asio::connection> conn,
298 sdbusplus::asio::object_server& objectServer)
299{
300
301 conn->async_method_call(
302 [&objectServer, &sensors](boost::system::error_code& ec,
303 const ManagedObjectType managedObj) {
304 if (ec)
305 {
306 std::cerr << "Error calling entity manager \n";
307 return;
308 }
309 for (const auto& pathPair : managedObj)
310 {
311 for (const auto& interfacePair : pathPair.second)
312 {
313 if (interfacePair.first == redundancyConfiguration)
314 {
315 // currently only support one
316 auto findCount =
317 interfacePair.second.find("AllowedFailures");
318 if (findCount == interfacePair.second.end())
319 {
320 std::cerr << "Malformed redundancy record \n";
321 return;
322 }
323 std::vector<std::string> sensorList;
324
325 for (const auto& sensor : sensors)
326 {
327 sensorList.push_back(
328 "/xyz/openbmc_project/sensors/fan_tach/" +
329 sensor.second->name);
330 }
331 systemRedundancy = std::make_unique<RedundancySensor>(
332 variant_ns::get<uint64_t>(findCount->second),
333 sensorList, objectServer);
334
335 return;
336 }
337 }
338 }
339 },
340 "xyz.openbmc_project.EntityManager", "/",
341 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
342}
343
James Feist6714a252018-09-10 15:26:18 -0700344int main(int argc, char** argv)
345{
346 boost::asio::io_service io;
347 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
348 systemBus->request_name("xyz.openbmc_project.FanSensor");
349 sdbusplus::asio::object_server objectServer(systemBus);
350 boost::container::flat_map<std::string, std::unique_ptr<TachSensor>>
351 tachSensors;
352 boost::container::flat_map<std::string, std::unique_ptr<PwmSensor>>
353 pwmSensors;
354 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
355 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
356 std::make_unique<boost::container::flat_set<std::string>>();
357
358 io.post([&]() {
359 createSensors(io, objectServer, tachSensors, pwmSensors, systemBus,
360 nullptr);
James Feistdc6c55f2018-10-31 12:53:20 -0700361 createRedundancySensor(tachSensors, systemBus, objectServer);
James Feist6714a252018-09-10 15:26:18 -0700362 });
363
364 boost::asio::deadline_timer filterTimer(io);
365 std::function<void(sdbusplus::message::message&)> eventHandler =
366 [&](sdbusplus::message::message& message) {
367 if (message.is_method_error())
368 {
369 std::cerr << "callback method error\n";
370 return;
371 }
372 sensorsChanged->insert(message.get_path());
373 // this implicitly cancels the timer
374 filterTimer.expires_from_now(boost::posix_time::seconds(1));
375
376 filterTimer.async_wait([&](const boost::system::error_code& ec) {
377 if (ec == boost::asio::error::operation_aborted)
378 {
379 /* we were canceled*/
380 return;
381 }
382 else if (ec)
383 {
384 std::cerr << "timer error\n";
385 return;
386 }
387 createSensors(io, objectServer, tachSensors, pwmSensors,
388 systemBus, sensorsChanged);
389 });
390 };
391
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700392 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700393 {
394 auto match = std::make_unique<sdbusplus::bus::match::match>(
395 static_cast<sdbusplus::bus::bus&>(*systemBus),
396 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700397 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700398 eventHandler);
399 matches.emplace_back(std::move(match));
400 }
401
James Feistdc6c55f2018-10-31 12:53:20 -0700402 // redundancy sensor
403 std::function<void(sdbusplus::message::message&)> redundancyHandler =
404 [&tachSensors, &systemBus,
405 &objectServer](sdbusplus::message::message& message) {
406 createRedundancySensor(tachSensors, systemBus, objectServer);
407 };
408 auto match = std::make_unique<sdbusplus::bus::match::match>(
409 static_cast<sdbusplus::bus::bus&>(*systemBus),
410 "type='signal',member='PropertiesChanged',path_namespace='" +
411 std::string(inventoryPath) + "',arg0namespace='" +
412 redundancyConfiguration + "'",
413 redundancyHandler);
414 matches.emplace_back(std::move(match));
415
James Feist6714a252018-09-10 15:26:18 -0700416 io.run();
417}