blob: 1b56d938fd9277a11b010099df640f561c9741f6 [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>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <functional>
28#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040029#include <optional>
James Feist6714a252018-09-10 15:26:18 -070030#include <regex>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <sdbusplus/bus/match.hpp>
34#include <string>
35#include <variant>
36#include <vector>
James Feist6714a252018-09-10 15:26:18 -070037
38static constexpr bool DEBUG = false;
39
James Feistcf3bce62019-01-08 10:07:19 -080040namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080041
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070043 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070044static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070045
James Feistb83bb2b2019-05-31 12:31:23 -070046static boost::container::flat_map<size_t, bool> cpuPresence;
47
James Feist08aec6f2019-01-30 16:17:04 -080048// filter out adc from any other voltage sensor
49bool isAdc(const fs::path& parentPath)
50{
51 fs::path namePath = parentPath / "name";
52
53 std::ifstream nameFile(namePath);
54 if (!nameFile.good())
55 {
56 std::cerr << "Failure reading " << namePath.string() << "\n";
57 return false;
58 }
59
60 std::string name;
61 std::getline(nameFile, name);
62
63 return name == "iio_hwmon";
64}
65
James Feist6714a252018-09-10 15:26:18 -070066void createSensors(
67 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
68 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
69 sensors,
70 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
71 const std::unique_ptr<boost::container::flat_set<std::string>>&
72 sensorsChanged)
73{
James Feistc71c1192019-09-18 14:31:33 -070074 auto getter = std::make_shared<GetSensorConfiguration>(
75 dbusConnection,
76 std::move([&io, &objectServer, &sensors, &dbusConnection,
77 &sensorsChanged](
78 const ManagedObjectType& sensorConfigurations) {
79 bool firstScan = sensorsChanged == nullptr;
80 std::vector<fs::path> paths;
81 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)",
82 paths))
James Feist08aec6f2019-01-30 16:17:04 -080083 {
James Feistc71c1192019-09-18 14:31:33 -070084 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 {
92 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080093 {
James Feistc71c1192019-09-18 14:31:33 -070094 continue;
James Feist08aec6f2019-01-30 16:17:04 -080095 }
James Feistc71c1192019-09-18 14:31:33 -070096 std::smatch match;
97 std::string pathStr = path.string();
James Feist08aec6f2019-01-30 16:17:04 -080098
James Feistc71c1192019-09-18 14:31:33 -070099 std::regex_search(pathStr, match, inputRegex);
100 std::string indexStr = *(match.begin() + 1);
James Feist08aec6f2019-01-30 16:17:04 -0800101
James Feistc71c1192019-09-18 14:31:33 -0700102 auto directory = path.parent_path();
103 // convert to 0 based
104 size_t index = std::stoul(indexStr) - 1;
James Feist08aec6f2019-01-30 16:17:04 -0800105
James Feistc71c1192019-09-18 14:31:33 -0700106 const SensorData* sensorData = nullptr;
107 const std::string* interfacePath = nullptr;
108 const std::pair<
109 std::string,
110 boost::container::flat_map<std::string, BasicVariantType>>*
111 baseConfiguration;
112 for (const std::pair<sdbusplus::message::object_path,
113 SensorData>& sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700114 {
James Feistc71c1192019-09-18 14:31:33 -0700115 // clear it out each loop
116 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700117
James Feistc71c1192019-09-18 14:31:33 -0700118 // find base configuration
119 for (const char* type : sensorTypes)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700120 {
James Feistc71c1192019-09-18 14:31:33 -0700121 auto sensorBase = sensor.second.find(type);
122 if (sensorBase != sensor.second.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700123 {
James Feistc71c1192019-09-18 14:31:33 -0700124 baseConfiguration = &(*sensorBase);
125 break;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700126 }
127 }
James Feistc71c1192019-09-18 14:31:33 -0700128 if (baseConfiguration == nullptr)
129 {
130 continue;
131 }
132 auto findIndex = baseConfiguration->second.find("Index");
133 if (findIndex == baseConfiguration->second.end())
134 {
135 std::cerr << "Base configuration missing Index"
136 << baseConfiguration->first << "\n";
137 continue;
138 }
139
140 unsigned int number = std::visit(
141 VariantToUnsignedIntVisitor(), findIndex->second);
142
143 if (number != index)
144 {
145 continue;
146 }
147
148 sensorData = &(sensor.second);
149 interfacePath = &(sensor.first.str);
150 break;
151 }
152 if (sensorData == nullptr)
153 {
154 std::cerr << "failed to find match for " << path.string()
155 << "\n";
156 continue;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700157 }
158
James Feistc71c1192019-09-18 14:31:33 -0700159 if (baseConfiguration == nullptr)
160 {
161 std::cerr << "error finding base configuration for"
162 << path.string() << "\n";
163 continue;
164 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700165
James Feistc71c1192019-09-18 14:31:33 -0700166 auto findSensorName = baseConfiguration->second.find("Name");
167 if (findSensorName == baseConfiguration->second.end())
168 {
169 std::cerr << "could not determine configuration name for "
170 << path.string() << "\n";
171 continue;
172 }
173 std::string sensorName =
174 std::get<std::string>(findSensorName->second);
175
176 // on rescans, only update sensors we were signaled by
177 auto findSensor = sensors.find(sensorName);
178 if (!firstScan && findSensor != sensors.end())
179 {
180 bool found = false;
181 for (auto it = sensorsChanged->begin();
182 it != sensorsChanged->end(); it++)
183 {
184 if (boost::ends_with(*it, findSensor->second->name))
185 {
186 sensorsChanged->erase(it);
187 findSensor->second = nullptr;
188 found = true;
189 break;
190 }
191 }
192 if (!found)
193 {
194 continue;
195 }
196 }
197 std::vector<thresholds::Threshold> sensorThresholds;
198 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
199 {
200 std::cerr << "error populating thresholds for "
201 << sensorName << "\n";
202 }
203
204 auto findScaleFactor =
205 baseConfiguration->second.find("ScaleFactor");
206 float scaleFactor = 1.0;
207 if (findScaleFactor != baseConfiguration->second.end())
208 {
209 scaleFactor = std::visit(VariantToFloatVisitor(),
210 findScaleFactor->second);
211 }
212
213 auto findPowerOn = baseConfiguration->second.find("PowerState");
214 PowerState readState = PowerState::always;
215 if (findPowerOn != baseConfiguration->second.end())
216 {
217 std::string powerState = std::visit(
218 VariantToStringVisitor(), findPowerOn->second);
219 setReadState(powerState, readState);
220 }
221
222 auto findCPU = baseConfiguration->second.find("CPURequired");
223 if (findCPU != baseConfiguration->second.end())
224 {
225 size_t index =
226 std::visit(VariantToIntVisitor(), findCPU->second);
227 auto presenceFind = cpuPresence.find(index);
228 if (presenceFind == cpuPresence.end())
229 {
230 continue; // no such cpu
231 }
232 if (!presenceFind->second)
233 {
234 continue; // cpu not installed
235 }
236 }
237
238 auto& sensor = sensors[sensorName];
239 sensor = nullptr;
240
241 std::optional<BridgeGpio> bridgeGpio;
242 for (const SensorBaseConfiguration& suppConfig : *sensorData)
243 {
244 if (suppConfig.first.find("BridgeGpio") !=
245 std::string::npos)
246 {
247 auto findName = suppConfig.second.find("Name");
248 if (findName != suppConfig.second.end())
249 {
250 std::string gpioName = std::visit(
251 VariantToStringVisitor(), findName->second);
252
253 int polarity = gpiod::line::ACTIVE_HIGH;
254 auto findPolarity =
255 suppConfig.second.find("Polarity");
256 if (findPolarity != suppConfig.second.end())
257 {
258 if (std::string("Low") ==
259 std::visit(VariantToStringVisitor(),
260 findPolarity->second))
261 {
262 polarity = gpiod::line::ACTIVE_LOW;
263 }
264 }
265 bridgeGpio = BridgeGpio(gpioName, polarity);
266 }
267
268 break;
269 }
270 }
271
272 sensor = std::make_unique<ADCSensor>(
273 path.string(), objectServer, dbusConnection, io, sensorName,
274 std::move(sensorThresholds), scaleFactor, readState,
275 *interfacePath, std::move(bridgeGpio));
276 }
277 }));
278
279 getter->getConfiguration(
280 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700281}
282
James Feistb6c0b912019-07-09 12:21:44 -0700283int main()
James Feist6714a252018-09-10 15:26:18 -0700284{
285 boost::asio::io_service io;
286 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
287 systemBus->request_name("xyz.openbmc_project.ADCSensor");
288 sdbusplus::asio::object_server objectServer(systemBus);
289 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
290 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
291 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
292 std::make_unique<boost::container::flat_set<std::string>>();
293
294 io.post([&]() {
295 createSensors(io, objectServer, sensors, systemBus, nullptr);
296 });
297
298 boost::asio::deadline_timer filterTimer(io);
299 std::function<void(sdbusplus::message::message&)> eventHandler =
300 [&](sdbusplus::message::message& message) {
301 if (message.is_method_error())
302 {
303 std::cerr << "callback method error\n";
304 return;
305 }
306 sensorsChanged->insert(message.get_path());
307 // this implicitly cancels the timer
308 filterTimer.expires_from_now(boost::posix_time::seconds(1));
309
310 filterTimer.async_wait([&](const boost::system::error_code& ec) {
311 if (ec == boost::asio::error::operation_aborted)
312 {
313 /* we were canceled*/
314 return;
315 }
316 else if (ec)
317 {
318 std::cerr << "timer error\n";
319 return;
320 }
321 createSensors(io, objectServer, sensors, systemBus,
322 sensorsChanged);
323 });
324 };
325
James Feistb83bb2b2019-05-31 12:31:23 -0700326 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
327 [&](sdbusplus::message::message& message) {
328 std::string path = message.get_path();
329 boost::to_lower(path);
330
331 if (path.rfind("cpu") == std::string::npos)
332 {
333 return; // not interested
334 }
335 size_t index = 0;
336 try
337 {
338 index = std::stoi(path.substr(path.size() - 1));
339 }
340 catch (std::invalid_argument&)
341 {
342 std::cerr << "Found invalid path " << path << "\n";
343 return;
344 }
345
346 std::string objectName;
347 boost::container::flat_map<std::string, std::variant<bool>> values;
348 message.read(objectName, values);
349 auto findPresence = values.find("Present");
350 if (findPresence != values.end())
351 {
352 cpuPresence[index] = std::get<bool>(findPresence->second);
353 }
354
355 // this implicitly cancels the timer
356 filterTimer.expires_from_now(boost::posix_time::seconds(1));
357
358 filterTimer.async_wait([&](const boost::system::error_code& ec) {
359 if (ec == boost::asio::error::operation_aborted)
360 {
361 /* we were canceled*/
362 return;
363 }
364 else if (ec)
365 {
366 std::cerr << "timer error\n";
367 return;
368 }
369 createSensors(io, objectServer, sensors, systemBus, {});
370 });
371 };
372
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700373 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700374 {
375 auto match = std::make_unique<sdbusplus::bus::match::match>(
376 static_cast<sdbusplus::bus::bus&>(*systemBus),
377 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700378 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700379 eventHandler);
380 matches.emplace_back(std::move(match));
381 }
James Feistb83bb2b2019-05-31 12:31:23 -0700382 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
383 static_cast<sdbusplus::bus::bus&>(*systemBus),
384 "type='signal',member='PropertiesChanged',path_namespace='" +
385 std::string(cpuInventoryPath) +
386 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
387 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700388
389 io.run();
390}