blob: 4fdca29772824b6edc7bf7edd701570305f9cf8d [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 <ADCSensor.hpp>
18#include <Utils.hpp>
19#include <VariantVisitors.hpp>
James Feistb83bb2b2019-05-31 12:31:23 -070020#include <boost/algorithm/string/case_conv.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/container/flat_set.hpp>
James Feist24f02f22019-04-15 11:05:39 -070024#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070025#include <fstream>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040026#include <optional>
James Feist6714a252018-09-10 15:26:18 -070027#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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070035static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070036 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070037static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070038
James Feistb83bb2b2019-05-31 12:31:23 -070039static boost::container::flat_map<size_t, bool> cpuPresence;
40
James Feist08aec6f2019-01-30 16:17:04 -080041// filter out adc from any other voltage sensor
42bool isAdc(const fs::path& parentPath)
43{
44 fs::path namePath = parentPath / "name";
45
46 std::ifstream nameFile(namePath);
47 if (!nameFile.good())
48 {
49 std::cerr << "Failure reading " << namePath.string() << "\n";
50 return false;
51 }
52
53 std::string name;
54 std::getline(nameFile, name);
55
56 return name == "iio_hwmon";
57}
58
James Feist6714a252018-09-10 15:26:18 -070059void createSensors(
60 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
61 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
62 sensors,
63 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
64 const std::unique_ptr<boost::container::flat_set<std::string>>&
65 sensorsChanged)
66{
67 bool firstScan = sensorsChanged == nullptr;
68 // use new data the first time, then refresh
69 ManagedObjectType sensorConfigurations;
70 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070072 {
73 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
74 useCache))
75 {
76 std::cerr << "error communicating to entity manager\n";
77 return;
78 }
79 useCache = true;
80 }
81 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070083 {
84 std::cerr << "No temperature sensors in system\n";
85 return;
86 }
87
88 // iterate through all found adc sensors, and try to match them with
89 // configuration
90 for (auto& path : paths)
91 {
James Feist08aec6f2019-01-30 16:17:04 -080092 if (!isAdc(path.parent_path()))
93 {
94 continue;
95 }
James Feist6714a252018-09-10 15:26:18 -070096 std::smatch match;
97 std::string pathStr = path.string();
98
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -0700100 std::string indexStr = *(match.begin() + 1);
101
102 auto directory = path.parent_path();
103 // convert to 0 based
104 size_t index = std::stoul(indexStr) - 1;
James Feist6714a252018-09-10 15:26:18 -0700105
106 const SensorData* sensorData = nullptr;
107 const std::string* interfacePath = nullptr;
James Feist08aec6f2019-01-30 16:17:04 -0800108 const std::pair<std::string, boost::container::flat_map<
109 std::string, BasicVariantType>>*
110 baseConfiguration;
James Feist6714a252018-09-10 15:26:18 -0700111 for (const std::pair<sdbusplus::message::object_path, SensorData>&
112 sensor : sensorConfigurations)
113 {
James Feist08aec6f2019-01-30 16:17:04 -0800114 // clear it out each loop
115 baseConfiguration = nullptr;
116
117 // find base configuration
118 for (const char* type : sensorTypes)
119 {
120 auto sensorBase = sensor.second.find(type);
121 if (sensorBase != sensor.second.end())
122 {
123 baseConfiguration = &(*sensorBase);
124 break;
125 }
126 }
127 if (baseConfiguration == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700128 {
129 continue;
130 }
James Feist08aec6f2019-01-30 16:17:04 -0800131 auto findIndex = baseConfiguration->second.find("Index");
132 if (findIndex == baseConfiguration->second.end())
133 {
134 std::cerr << "Base configuration missing Index"
135 << baseConfiguration->first << "\n";
136 continue;
137 }
138
James Feist3eb82622019-02-08 13:10:22 -0800139 unsigned int number =
140 std::visit(VariantToUnsignedIntVisitor(), findIndex->second);
James Feist08aec6f2019-01-30 16:17:04 -0800141
142 if (number != index)
143 {
144 continue;
145 }
146
James Feist6714a252018-09-10 15:26:18 -0700147 sensorData = &(sensor.second);
148 interfacePath = &(sensor.first.str);
149 break;
150 }
151 if (sensorData == nullptr)
152 {
James Feist08aec6f2019-01-30 16:17:04 -0800153 std::cerr << "failed to find match for " << path.string() << "\n";
James Feist6714a252018-09-10 15:26:18 -0700154 continue;
155 }
James Feist6714a252018-09-10 15:26:18 -0700156
157 if (baseConfiguration == nullptr)
158 {
James Feist08aec6f2019-01-30 16:17:04 -0800159 std::cerr << "error finding base configuration for" << path.string()
James Feist6714a252018-09-10 15:26:18 -0700160 << "\n";
161 continue;
162 }
163
164 auto findSensorName = baseConfiguration->second.find("Name");
165 if (findSensorName == baseConfiguration->second.end())
166 {
167 std::cerr << "could not determine configuration name for "
James Feist08aec6f2019-01-30 16:17:04 -0800168 << path.string() << "\n";
James Feist6714a252018-09-10 15:26:18 -0700169 continue;
170 }
James Feist3eb82622019-02-08 13:10:22 -0800171 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700172
173 // on rescans, only update sensors we were signaled by
174 auto findSensor = sensors.find(sensorName);
175 if (!firstScan && findSensor != sensors.end())
176 {
177 bool found = false;
178 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
179 it++)
180 {
181 if (boost::ends_with(*it, findSensor->second->name))
182 {
183 sensorsChanged->erase(it);
184 findSensor->second = nullptr;
185 found = true;
186 break;
187 }
188 }
189 if (!found)
190 {
191 continue;
192 }
193 }
194 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700196 {
197 std::cerr << "error populating thresholds for " << sensorName
198 << "\n";
199 }
200
James Feist757c9db2018-09-12 12:27:25 -0700201 auto findScaleFactor = baseConfiguration->second.find("ScaleFactor");
James Feist6714a252018-09-10 15:26:18 -0700202 float scaleFactor = 1.0;
203 if (findScaleFactor != baseConfiguration->second.end())
204 {
James Feist3eb82622019-02-08 13:10:22 -0800205 scaleFactor =
206 std::visit(VariantToFloatVisitor(), findScaleFactor->second);
James Feist6714a252018-09-10 15:26:18 -0700207 }
James Feist71d31b22019-01-02 16:57:54 -0800208
209 auto findPowerOn = baseConfiguration->second.find("PowerState");
210 PowerState readState = PowerState::always;
211 if (findPowerOn != baseConfiguration->second.end())
212 {
James Feist3eb82622019-02-08 13:10:22 -0800213 std::string powerState =
214 std::visit(VariantToStringVisitor(), findPowerOn->second);
James Feistfc94b212019-02-06 16:14:51 -0800215 setReadState(powerState, readState);
James Feist71d31b22019-01-02 16:57:54 -0800216 }
217
James Feistb83bb2b2019-05-31 12:31:23 -0700218 auto findCPU = baseConfiguration->second.find("CPURequired");
219 if (findCPU != baseConfiguration->second.end())
220 {
221 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
222 auto presenceFind = cpuPresence.find(index);
223 if (presenceFind == cpuPresence.end())
224 {
225 continue; // no such cpu
226 }
227 if (!presenceFind->second)
228 {
229 continue; // cpu not installed
230 }
231 }
232
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -0700233 auto& sensor = sensors[sensorName];
234 sensor = nullptr;
235
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700236 std::optional<BridgeGpio> bridgeGpio;
237 for (const SensorBaseConfiguration& suppConfig : *sensorData)
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400238 {
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700239 if (suppConfig.first.find("BridgeGpio") != std::string::npos)
240 {
241 auto findName = suppConfig.second.find("Name");
242 if (findName != suppConfig.second.end())
243 {
244 std::string gpioName =
245 std::visit(VariantToStringVisitor(), findName->second);
246
247 int polarity = gpiod::line::ACTIVE_HIGH;
248 auto findPolarity = suppConfig.second.find("Polarity");
249 if (findPolarity != suppConfig.second.end())
250 {
251 if (std::string("Low") ==
252 std::visit(VariantToStringVisitor(),
253 findPolarity->second))
254 {
255 polarity = gpiod::line::ACTIVE_LOW;
256 }
257 }
258 bridgeGpio = BridgeGpio(gpioName, polarity);
259 }
260
261 break;
262 }
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400263 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700264
James Feistb83bb2b2019-05-31 12:31:23 -0700265 sensor = std::make_unique<ADCSensor>(
James Feist6714a252018-09-10 15:26:18 -0700266 path.string(), objectServer, dbusConnection, io, sensorName,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400267 std::move(sensorThresholds), scaleFactor, readState, *interfacePath,
Jae Hyun Yoo1cbd1c62019-07-29 15:02:43 -0700268 std::move(bridgeGpio));
James Feist6714a252018-09-10 15:26:18 -0700269 }
270}
271
James Feistb6c0b912019-07-09 12:21:44 -0700272int main()
James Feist6714a252018-09-10 15:26:18 -0700273{
274 boost::asio::io_service io;
275 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
276 systemBus->request_name("xyz.openbmc_project.ADCSensor");
277 sdbusplus::asio::object_server objectServer(systemBus);
278 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
279 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
280 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
281 std::make_unique<boost::container::flat_set<std::string>>();
282
283 io.post([&]() {
284 createSensors(io, objectServer, sensors, systemBus, nullptr);
285 });
286
287 boost::asio::deadline_timer filterTimer(io);
288 std::function<void(sdbusplus::message::message&)> eventHandler =
289 [&](sdbusplus::message::message& message) {
290 if (message.is_method_error())
291 {
292 std::cerr << "callback method error\n";
293 return;
294 }
295 sensorsChanged->insert(message.get_path());
296 // this implicitly cancels the timer
297 filterTimer.expires_from_now(boost::posix_time::seconds(1));
298
299 filterTimer.async_wait([&](const boost::system::error_code& ec) {
300 if (ec == boost::asio::error::operation_aborted)
301 {
302 /* we were canceled*/
303 return;
304 }
305 else if (ec)
306 {
307 std::cerr << "timer error\n";
308 return;
309 }
310 createSensors(io, objectServer, sensors, systemBus,
311 sensorsChanged);
312 });
313 };
314
James Feistb83bb2b2019-05-31 12:31:23 -0700315 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
316 [&](sdbusplus::message::message& message) {
317 std::string path = message.get_path();
318 boost::to_lower(path);
319
320 if (path.rfind("cpu") == std::string::npos)
321 {
322 return; // not interested
323 }
324 size_t index = 0;
325 try
326 {
327 index = std::stoi(path.substr(path.size() - 1));
328 }
329 catch (std::invalid_argument&)
330 {
331 std::cerr << "Found invalid path " << path << "\n";
332 return;
333 }
334
335 std::string objectName;
336 boost::container::flat_map<std::string, std::variant<bool>> values;
337 message.read(objectName, values);
338 auto findPresence = values.find("Present");
339 if (findPresence != values.end())
340 {
341 cpuPresence[index] = std::get<bool>(findPresence->second);
342 }
343
344 // this implicitly cancels the timer
345 filterTimer.expires_from_now(boost::posix_time::seconds(1));
346
347 filterTimer.async_wait([&](const boost::system::error_code& ec) {
348 if (ec == boost::asio::error::operation_aborted)
349 {
350 /* we were canceled*/
351 return;
352 }
353 else if (ec)
354 {
355 std::cerr << "timer error\n";
356 return;
357 }
358 createSensors(io, objectServer, sensors, systemBus, {});
359 });
360 };
361
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700362 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700363 {
364 auto match = std::make_unique<sdbusplus::bus::match::match>(
365 static_cast<sdbusplus::bus::bus&>(*systemBus),
366 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700367 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700368 eventHandler);
369 matches.emplace_back(std::move(match));
370 }
James Feistb83bb2b2019-05-31 12:31:23 -0700371 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
372 static_cast<sdbusplus::bus::bus&>(*systemBus),
373 "type='signal',member='PropertiesChanged',path_namespace='" +
374 std::string(cpuInventoryPath) +
375 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
376 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700377
378 io.run();
379}