blob: 53d09c224b45585462ddb6b8b09c4d742ec50b3f [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "ADCSensor.hpp"
18#include "Utils.hpp"
19#include "VariantVisitors.hpp"
20
James Feistb83bb2b2019-05-31 12:31:23 -070021#include <boost/algorithm/string/case_conv.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/algorithm/string/predicate.hpp>
23#include <boost/algorithm/string/replace.hpp>
24#include <boost/container/flat_set.hpp>
James Feist24f02f22019-04-15 11:05:39 -070025#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070026#include <fstream>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040027#include <optional>
James Feist6714a252018-09-10 15:26:18 -070028#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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070036static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070037 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070038static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070039
James Feistb83bb2b2019-05-31 12:31:23 -070040static boost::container::flat_map<size_t, bool> cpuPresence;
41
James Feist08aec6f2019-01-30 16:17:04 -080042// filter out adc from any other voltage sensor
43bool isAdc(const fs::path& parentPath)
44{
45 fs::path namePath = parentPath / "name";
46
47 std::ifstream nameFile(namePath);
48 if (!nameFile.good())
49 {
50 std::cerr << "Failure reading " << namePath.string() << "\n";
51 return false;
52 }
53
54 std::string name;
55 std::getline(nameFile, name);
56
57 return name == "iio_hwmon";
58}
59
James Feist6714a252018-09-10 15:26:18 -070060void createSensors(
61 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
62 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
63 sensors,
64 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
65 const std::unique_ptr<boost::container::flat_set<std::string>>&
66 sensorsChanged)
67{
James Feistc71c1192019-09-18 14:31:33 -070068 auto getter = std::make_shared<GetSensorConfiguration>(
69 dbusConnection,
70 std::move([&io, &objectServer, &sensors, &dbusConnection,
71 &sensorsChanged](
72 const ManagedObjectType& sensorConfigurations) {
73 bool firstScan = sensorsChanged == nullptr;
74 std::vector<fs::path> paths;
75 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)",
76 paths))
James Feist08aec6f2019-01-30 16:17:04 -080077 {
James Feistc71c1192019-09-18 14:31:33 -070078 std::cerr << "No temperature sensors in system\n";
79 return;
80 }
81
82 // iterate through all found adc sensors, and try to match them with
83 // configuration
84 for (auto& path : paths)
85 {
86 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080087 {
James Feistc71c1192019-09-18 14:31:33 -070088 continue;
James Feist08aec6f2019-01-30 16:17:04 -080089 }
James Feistc71c1192019-09-18 14:31:33 -070090 std::smatch match;
91 std::string pathStr = path.string();
James Feist08aec6f2019-01-30 16:17:04 -080092
James Feistc71c1192019-09-18 14:31:33 -070093 std::regex_search(pathStr, match, inputRegex);
94 std::string indexStr = *(match.begin() + 1);
James Feist08aec6f2019-01-30 16:17:04 -080095
James Feistc71c1192019-09-18 14:31:33 -070096 auto directory = path.parent_path();
97 // convert to 0 based
98 size_t index = std::stoul(indexStr) - 1;
James Feist08aec6f2019-01-30 16:17:04 -080099
James Feistc71c1192019-09-18 14:31:33 -0700100 const SensorData* sensorData = nullptr;
101 const std::string* interfacePath = nullptr;
102 const std::pair<
103 std::string,
104 boost::container::flat_map<std::string, BasicVariantType>>*
105 baseConfiguration;
106 for (const std::pair<sdbusplus::message::object_path,
107 SensorData>& sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700108 {
James Feistc71c1192019-09-18 14:31:33 -0700109 // clear it out each loop
110 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700111
James Feistc71c1192019-09-18 14:31:33 -0700112 // find base configuration
113 for (const char* type : sensorTypes)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700114 {
James Feistc71c1192019-09-18 14:31:33 -0700115 auto sensorBase = sensor.second.find(type);
116 if (sensorBase != sensor.second.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700117 {
James Feistc71c1192019-09-18 14:31:33 -0700118 baseConfiguration = &(*sensorBase);
119 break;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700120 }
121 }
James Feistc71c1192019-09-18 14:31:33 -0700122 if (baseConfiguration == nullptr)
123 {
124 continue;
125 }
126 auto findIndex = baseConfiguration->second.find("Index");
127 if (findIndex == baseConfiguration->second.end())
128 {
129 std::cerr << "Base configuration missing Index"
130 << baseConfiguration->first << "\n";
131 continue;
132 }
133
134 unsigned int number = std::visit(
135 VariantToUnsignedIntVisitor(), findIndex->second);
136
137 if (number != index)
138 {
139 continue;
140 }
141
142 sensorData = &(sensor.second);
143 interfacePath = &(sensor.first.str);
144 break;
145 }
146 if (sensorData == nullptr)
147 {
148 std::cerr << "failed to find match for " << path.string()
149 << "\n";
150 continue;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700151 }
152
James Feistc71c1192019-09-18 14:31:33 -0700153 if (baseConfiguration == nullptr)
154 {
155 std::cerr << "error finding base configuration for"
156 << path.string() << "\n";
157 continue;
158 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700159
James Feistc71c1192019-09-18 14:31:33 -0700160 auto findSensorName = baseConfiguration->second.find("Name");
161 if (findSensorName == baseConfiguration->second.end())
162 {
163 std::cerr << "could not determine configuration name for "
164 << path.string() << "\n";
165 continue;
166 }
167 std::string sensorName =
168 std::get<std::string>(findSensorName->second);
169
170 // on rescans, only update sensors we were signaled by
171 auto findSensor = sensors.find(sensorName);
172 if (!firstScan && findSensor != sensors.end())
173 {
174 bool found = false;
175 for (auto it = sensorsChanged->begin();
176 it != sensorsChanged->end(); it++)
177 {
178 if (boost::ends_with(*it, findSensor->second->name))
179 {
180 sensorsChanged->erase(it);
181 findSensor->second = nullptr;
182 found = true;
183 break;
184 }
185 }
186 if (!found)
187 {
188 continue;
189 }
190 }
191 std::vector<thresholds::Threshold> sensorThresholds;
192 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
193 {
194 std::cerr << "error populating thresholds for "
195 << sensorName << "\n";
196 }
197
198 auto findScaleFactor =
199 baseConfiguration->second.find("ScaleFactor");
200 float scaleFactor = 1.0;
201 if (findScaleFactor != baseConfiguration->second.end())
202 {
203 scaleFactor = std::visit(VariantToFloatVisitor(),
204 findScaleFactor->second);
205 }
206
207 auto findPowerOn = baseConfiguration->second.find("PowerState");
208 PowerState readState = PowerState::always;
209 if (findPowerOn != baseConfiguration->second.end())
210 {
211 std::string powerState = std::visit(
212 VariantToStringVisitor(), findPowerOn->second);
213 setReadState(powerState, readState);
214 }
215
216 auto findCPU = baseConfiguration->second.find("CPURequired");
217 if (findCPU != baseConfiguration->second.end())
218 {
219 size_t index =
220 std::visit(VariantToIntVisitor(), findCPU->second);
221 auto presenceFind = cpuPresence.find(index);
222 if (presenceFind == cpuPresence.end())
223 {
224 continue; // no such cpu
225 }
226 if (!presenceFind->second)
227 {
228 continue; // cpu not installed
229 }
230 }
231
232 auto& sensor = sensors[sensorName];
233 sensor = nullptr;
234
235 std::optional<BridgeGpio> bridgeGpio;
236 for (const SensorBaseConfiguration& suppConfig : *sensorData)
237 {
238 if (suppConfig.first.find("BridgeGpio") !=
239 std::string::npos)
240 {
241 auto findName = suppConfig.second.find("Name");
242 if (findName != suppConfig.second.end())
243 {
244 std::string gpioName = std::visit(
245 VariantToStringVisitor(), findName->second);
246
247 int polarity = gpiod::line::ACTIVE_HIGH;
248 auto findPolarity =
249 suppConfig.second.find("Polarity");
250 if (findPolarity != suppConfig.second.end())
251 {
252 if (std::string("Low") ==
253 std::visit(VariantToStringVisitor(),
254 findPolarity->second))
255 {
256 polarity = gpiod::line::ACTIVE_LOW;
257 }
258 }
259 bridgeGpio = BridgeGpio(gpioName, polarity);
260 }
261
262 break;
263 }
264 }
265
266 sensor = std::make_unique<ADCSensor>(
267 path.string(), objectServer, dbusConnection, io, sensorName,
268 std::move(sensorThresholds), scaleFactor, readState,
269 *interfacePath, std::move(bridgeGpio));
270 }
271 }));
272
273 getter->getConfiguration(
274 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700275}
276
James Feistb6c0b912019-07-09 12:21:44 -0700277int main()
James Feist6714a252018-09-10 15:26:18 -0700278{
279 boost::asio::io_service io;
280 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
281 systemBus->request_name("xyz.openbmc_project.ADCSensor");
282 sdbusplus::asio::object_server objectServer(systemBus);
283 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
284 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
285 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
286 std::make_unique<boost::container::flat_set<std::string>>();
287
288 io.post([&]() {
289 createSensors(io, objectServer, sensors, systemBus, nullptr);
290 });
291
292 boost::asio::deadline_timer filterTimer(io);
293 std::function<void(sdbusplus::message::message&)> eventHandler =
294 [&](sdbusplus::message::message& message) {
295 if (message.is_method_error())
296 {
297 std::cerr << "callback method error\n";
298 return;
299 }
300 sensorsChanged->insert(message.get_path());
301 // this implicitly cancels the timer
302 filterTimer.expires_from_now(boost::posix_time::seconds(1));
303
304 filterTimer.async_wait([&](const boost::system::error_code& ec) {
305 if (ec == boost::asio::error::operation_aborted)
306 {
307 /* we were canceled*/
308 return;
309 }
310 else if (ec)
311 {
312 std::cerr << "timer error\n";
313 return;
314 }
315 createSensors(io, objectServer, sensors, systemBus,
316 sensorsChanged);
317 });
318 };
319
James Feistb83bb2b2019-05-31 12:31:23 -0700320 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
321 [&](sdbusplus::message::message& message) {
322 std::string path = message.get_path();
323 boost::to_lower(path);
324
325 if (path.rfind("cpu") == std::string::npos)
326 {
327 return; // not interested
328 }
329 size_t index = 0;
330 try
331 {
332 index = std::stoi(path.substr(path.size() - 1));
333 }
334 catch (std::invalid_argument&)
335 {
336 std::cerr << "Found invalid path " << path << "\n";
337 return;
338 }
339
340 std::string objectName;
341 boost::container::flat_map<std::string, std::variant<bool>> values;
342 message.read(objectName, values);
343 auto findPresence = values.find("Present");
344 if (findPresence != values.end())
345 {
346 cpuPresence[index] = std::get<bool>(findPresence->second);
347 }
348
349 // this implicitly cancels the timer
350 filterTimer.expires_from_now(boost::posix_time::seconds(1));
351
352 filterTimer.async_wait([&](const boost::system::error_code& ec) {
353 if (ec == boost::asio::error::operation_aborted)
354 {
355 /* we were canceled*/
356 return;
357 }
358 else if (ec)
359 {
360 std::cerr << "timer error\n";
361 return;
362 }
363 createSensors(io, objectServer, sensors, systemBus, {});
364 });
365 };
366
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700367 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700368 {
369 auto match = std::make_unique<sdbusplus::bus::match::match>(
370 static_cast<sdbusplus::bus::bus&>(*systemBus),
371 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700372 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700373 eventHandler);
374 matches.emplace_back(std::move(match));
375 }
James Feistb83bb2b2019-05-31 12:31:23 -0700376 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
377 static_cast<sdbusplus::bus::bus&>(*systemBus),
378 "type='signal',member='PropertiesChanged',path_namespace='" +
379 std::string(cpuInventoryPath) +
380 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
381 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700382
383 io.run();
384}