blob: 473d8a18617ddeb283388ad59710c102e54ba68b [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 Feist38fb5982020-05-28 10:09:54 -070025#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27#include <sdbusplus/bus/match.hpp>
28
James Feist24f02f22019-04-15 11:05:39 -070029#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070030#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <functional>
32#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040033#include <optional>
James Feist6714a252018-09-10 15:26:18 -070034#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070035#include <string>
36#include <variant>
37#include <vector>
James Feist6714a252018-09-10 15:26:18 -070038
39static constexpr bool DEBUG = false;
40
James Feistcf3bce62019-01-08 10:07:19 -080041namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080042
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070043static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070044 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070045static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070046
James Feistb83bb2b2019-05-31 12:31:23 -070047static boost::container::flat_map<size_t, bool> cpuPresence;
48
James Feist08aec6f2019-01-30 16:17:04 -080049// filter out adc from any other voltage sensor
50bool isAdc(const fs::path& parentPath)
51{
52 fs::path namePath = parentPath / "name";
53
54 std::ifstream nameFile(namePath);
55 if (!nameFile.good())
56 {
57 std::cerr << "Failure reading " << namePath.string() << "\n";
58 return false;
59 }
60
61 std::string name;
62 std::getline(nameFile, name);
63
64 return name == "iio_hwmon";
65}
66
James Feist6714a252018-09-10 15:26:18 -070067void createSensors(
68 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080069 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070070 sensors,
71 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070072 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -070073 sensorsChanged)
74{
James Feistc71c1192019-09-18 14:31:33 -070075 auto getter = std::make_shared<GetSensorConfiguration>(
76 dbusConnection,
77 std::move([&io, &objectServer, &sensors, &dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070078 sensorsChanged](
James Feistc71c1192019-09-18 14:31:33 -070079 const ManagedObjectType& sensorConfigurations) {
80 bool firstScan = sensorsChanged == nullptr;
81 std::vector<fs::path> paths;
82 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)",
83 paths))
James Feist08aec6f2019-01-30 16:17:04 -080084 {
James Feistc71c1192019-09-18 14:31:33 -070085 std::cerr << "No temperature sensors in system\n";
86 return;
87 }
88
89 // iterate through all found adc sensors, and try to match them with
90 // configuration
91 for (auto& path : paths)
92 {
93 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080094 {
James Feistc71c1192019-09-18 14:31:33 -070095 continue;
James Feist08aec6f2019-01-30 16:17:04 -080096 }
James Feistc71c1192019-09-18 14:31:33 -070097 std::smatch match;
98 std::string pathStr = path.string();
James Feist08aec6f2019-01-30 16:17:04 -080099
James Feistc71c1192019-09-18 14:31:33 -0700100 std::regex_search(pathStr, match, inputRegex);
101 std::string indexStr = *(match.begin() + 1);
James Feist08aec6f2019-01-30 16:17:04 -0800102
James Feistc71c1192019-09-18 14:31:33 -0700103 auto directory = path.parent_path();
104 // convert to 0 based
105 size_t index = std::stoul(indexStr) - 1;
James Feist08aec6f2019-01-30 16:17:04 -0800106
James Feistc71c1192019-09-18 14:31:33 -0700107 const SensorData* sensorData = nullptr;
108 const std::string* interfacePath = nullptr;
109 const std::pair<
110 std::string,
111 boost::container::flat_map<std::string, BasicVariantType>>*
112 baseConfiguration;
113 for (const std::pair<sdbusplus::message::object_path,
114 SensorData>& sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700115 {
James Feistc71c1192019-09-18 14:31:33 -0700116 // clear it out each loop
117 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700118
James Feistc71c1192019-09-18 14:31:33 -0700119 // find base configuration
120 for (const char* type : sensorTypes)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700121 {
James Feistc71c1192019-09-18 14:31:33 -0700122 auto sensorBase = sensor.second.find(type);
123 if (sensorBase != sensor.second.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700124 {
James Feistc71c1192019-09-18 14:31:33 -0700125 baseConfiguration = &(*sensorBase);
126 break;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700127 }
128 }
James Feistc71c1192019-09-18 14:31:33 -0700129 if (baseConfiguration == nullptr)
130 {
131 continue;
132 }
133 auto findIndex = baseConfiguration->second.find("Index");
134 if (findIndex == baseConfiguration->second.end())
135 {
136 std::cerr << "Base configuration missing Index"
137 << baseConfiguration->first << "\n";
138 continue;
139 }
140
141 unsigned int number = std::visit(
142 VariantToUnsignedIntVisitor(), findIndex->second);
143
144 if (number != index)
145 {
146 continue;
147 }
148
149 sensorData = &(sensor.second);
150 interfacePath = &(sensor.first.str);
151 break;
152 }
153 if (sensorData == nullptr)
154 {
155 std::cerr << "failed to find match for " << path.string()
156 << "\n";
157 continue;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700158 }
159
James Feistc71c1192019-09-18 14:31:33 -0700160 if (baseConfiguration == nullptr)
161 {
162 std::cerr << "error finding base configuration for"
163 << path.string() << "\n";
164 continue;
165 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700166
James Feistc71c1192019-09-18 14:31:33 -0700167 auto findSensorName = baseConfiguration->second.find("Name");
168 if (findSensorName == baseConfiguration->second.end())
169 {
170 std::cerr << "could not determine configuration name for "
171 << path.string() << "\n";
172 continue;
173 }
174 std::string sensorName =
175 std::get<std::string>(findSensorName->second);
176
177 // on rescans, only update sensors we were signaled by
178 auto findSensor = sensors.find(sensorName);
179 if (!firstScan && findSensor != sensors.end())
180 {
181 bool found = false;
182 for (auto it = sensorsChanged->begin();
183 it != sensorsChanged->end(); it++)
184 {
Wludzik, Jozef9a445472020-06-24 12:11:09 +0200185 if (findSensor->second &&
186 boost::ends_with(*it, findSensor->second->name))
James Feistc71c1192019-09-18 14:31:33 -0700187 {
188 sensorsChanged->erase(it);
189 findSensor->second = nullptr;
190 found = true;
191 break;
192 }
193 }
194 if (!found)
195 {
196 continue;
197 }
198 }
199 std::vector<thresholds::Threshold> sensorThresholds;
200 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
201 {
202 std::cerr << "error populating thresholds for "
203 << sensorName << "\n";
204 }
205
206 auto findScaleFactor =
207 baseConfiguration->second.find("ScaleFactor");
208 float scaleFactor = 1.0;
209 if (findScaleFactor != baseConfiguration->second.end())
210 {
211 scaleFactor = std::visit(VariantToFloatVisitor(),
212 findScaleFactor->second);
Zhikui Ren937eb542020-10-12 13:42:08 -0700213 // scaleFactor is used in division
214 if (scaleFactor == 0.0f)
215 {
216 scaleFactor = 1.0;
217 }
James Feistc71c1192019-09-18 14:31:33 -0700218 }
219
220 auto findPowerOn = baseConfiguration->second.find("PowerState");
221 PowerState readState = PowerState::always;
222 if (findPowerOn != baseConfiguration->second.end())
223 {
224 std::string powerState = std::visit(
225 VariantToStringVisitor(), findPowerOn->second);
226 setReadState(powerState, readState);
227 }
228
229 auto findCPU = baseConfiguration->second.find("CPURequired");
230 if (findCPU != baseConfiguration->second.end())
231 {
232 size_t index =
233 std::visit(VariantToIntVisitor(), findCPU->second);
234 auto presenceFind = cpuPresence.find(index);
235 if (presenceFind == cpuPresence.end())
236 {
237 continue; // no such cpu
238 }
239 if (!presenceFind->second)
240 {
241 continue; // cpu not installed
242 }
243 }
244
245 auto& sensor = sensors[sensorName];
246 sensor = nullptr;
247
248 std::optional<BridgeGpio> bridgeGpio;
249 for (const SensorBaseConfiguration& suppConfig : *sensorData)
250 {
251 if (suppConfig.first.find("BridgeGpio") !=
252 std::string::npos)
253 {
254 auto findName = suppConfig.second.find("Name");
255 if (findName != suppConfig.second.end())
256 {
257 std::string gpioName = std::visit(
258 VariantToStringVisitor(), findName->second);
259
260 int polarity = gpiod::line::ACTIVE_HIGH;
261 auto findPolarity =
262 suppConfig.second.find("Polarity");
263 if (findPolarity != suppConfig.second.end())
264 {
265 if (std::string("Low") ==
266 std::visit(VariantToStringVisitor(),
267 findPolarity->second))
268 {
269 polarity = gpiod::line::ACTIVE_LOW;
270 }
271 }
272 bridgeGpio = BridgeGpio(gpioName, polarity);
273 }
274
275 break;
276 }
277 }
278
Yong Li1afda6b2020-04-09 19:24:13 +0800279 sensor = std::make_shared<ADCSensor>(
James Feistc71c1192019-09-18 14:31:33 -0700280 path.string(), objectServer, dbusConnection, io, sensorName,
281 std::move(sensorThresholds), scaleFactor, readState,
282 *interfacePath, std::move(bridgeGpio));
Yong Li1afda6b2020-04-09 19:24:13 +0800283 sensor->setupRead();
James Feistc71c1192019-09-18 14:31:33 -0700284 }
285 }));
286
287 getter->getConfiguration(
288 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700289}
290
James Feistb6c0b912019-07-09 12:21:44 -0700291int main()
James Feist6714a252018-09-10 15:26:18 -0700292{
293 boost::asio::io_service io;
294 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
295 systemBus->request_name("xyz.openbmc_project.ADCSensor");
296 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800297 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700298 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700299 auto sensorsChanged =
300 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700301
302 io.post([&]() {
303 createSensors(io, objectServer, sensors, systemBus, nullptr);
304 });
305
306 boost::asio::deadline_timer filterTimer(io);
307 std::function<void(sdbusplus::message::message&)> eventHandler =
308 [&](sdbusplus::message::message& message) {
309 if (message.is_method_error())
310 {
311 std::cerr << "callback method error\n";
312 return;
313 }
314 sensorsChanged->insert(message.get_path());
315 // this implicitly cancels the timer
316 filterTimer.expires_from_now(boost::posix_time::seconds(1));
317
318 filterTimer.async_wait([&](const boost::system::error_code& ec) {
319 if (ec == boost::asio::error::operation_aborted)
320 {
321 /* we were canceled*/
322 return;
323 }
324 else if (ec)
325 {
326 std::cerr << "timer error\n";
327 return;
328 }
329 createSensors(io, objectServer, sensors, systemBus,
330 sensorsChanged);
331 });
332 };
333
James Feistb83bb2b2019-05-31 12:31:23 -0700334 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
335 [&](sdbusplus::message::message& message) {
336 std::string path = message.get_path();
337 boost::to_lower(path);
338
339 if (path.rfind("cpu") == std::string::npos)
340 {
341 return; // not interested
342 }
343 size_t index = 0;
344 try
345 {
346 index = std::stoi(path.substr(path.size() - 1));
347 }
348 catch (std::invalid_argument&)
349 {
350 std::cerr << "Found invalid path " << path << "\n";
351 return;
352 }
353
354 std::string objectName;
355 boost::container::flat_map<std::string, std::variant<bool>> values;
356 message.read(objectName, values);
357 auto findPresence = values.find("Present");
358 if (findPresence != values.end())
359 {
360 cpuPresence[index] = std::get<bool>(findPresence->second);
361 }
362
363 // this implicitly cancels the timer
364 filterTimer.expires_from_now(boost::posix_time::seconds(1));
365
366 filterTimer.async_wait([&](const boost::system::error_code& ec) {
367 if (ec == boost::asio::error::operation_aborted)
368 {
369 /* we were canceled*/
370 return;
371 }
372 else if (ec)
373 {
374 std::cerr << "timer error\n";
375 return;
376 }
James Feist5591cf082020-07-15 16:44:54 -0700377 createSensors(io, objectServer, sensors, systemBus, nullptr);
James Feistb83bb2b2019-05-31 12:31:23 -0700378 });
379 };
380
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700381 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700382 {
383 auto match = std::make_unique<sdbusplus::bus::match::match>(
384 static_cast<sdbusplus::bus::bus&>(*systemBus),
385 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700386 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700387 eventHandler);
388 matches.emplace_back(std::move(match));
389 }
James Feistb83bb2b2019-05-31 12:31:23 -0700390 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
391 static_cast<sdbusplus::bus::bus&>(*systemBus),
392 "type='signal',member='PropertiesChanged',path_namespace='" +
393 std::string(cpuInventoryPath) +
394 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
395 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700396
397 io.run();
398}