blob: cc8e7cd72ac3ff40ac080363432c2833256868aa [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,
72 const std::unique_ptr<boost::container::flat_set<std::string>>&
73 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,
78 &sensorsChanged](
79 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 {
185 if (boost::ends_with(*it, findSensor->second->name))
186 {
187 sensorsChanged->erase(it);
188 findSensor->second = nullptr;
189 found = true;
190 break;
191 }
192 }
193 if (!found)
194 {
195 continue;
196 }
197 }
198 std::vector<thresholds::Threshold> sensorThresholds;
199 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
200 {
201 std::cerr << "error populating thresholds for "
202 << sensorName << "\n";
203 }
204
205 auto findScaleFactor =
206 baseConfiguration->second.find("ScaleFactor");
207 float scaleFactor = 1.0;
208 if (findScaleFactor != baseConfiguration->second.end())
209 {
210 scaleFactor = std::visit(VariantToFloatVisitor(),
211 findScaleFactor->second);
212 }
213
214 auto findPowerOn = baseConfiguration->second.find("PowerState");
215 PowerState readState = PowerState::always;
216 if (findPowerOn != baseConfiguration->second.end())
217 {
218 std::string powerState = std::visit(
219 VariantToStringVisitor(), findPowerOn->second);
220 setReadState(powerState, readState);
221 }
222
223 auto findCPU = baseConfiguration->second.find("CPURequired");
224 if (findCPU != baseConfiguration->second.end())
225 {
226 size_t index =
227 std::visit(VariantToIntVisitor(), findCPU->second);
228 auto presenceFind = cpuPresence.find(index);
229 if (presenceFind == cpuPresence.end())
230 {
231 continue; // no such cpu
232 }
233 if (!presenceFind->second)
234 {
235 continue; // cpu not installed
236 }
237 }
238
239 auto& sensor = sensors[sensorName];
240 sensor = nullptr;
241
242 std::optional<BridgeGpio> bridgeGpio;
243 for (const SensorBaseConfiguration& suppConfig : *sensorData)
244 {
245 if (suppConfig.first.find("BridgeGpio") !=
246 std::string::npos)
247 {
248 auto findName = suppConfig.second.find("Name");
249 if (findName != suppConfig.second.end())
250 {
251 std::string gpioName = std::visit(
252 VariantToStringVisitor(), findName->second);
253
254 int polarity = gpiod::line::ACTIVE_HIGH;
255 auto findPolarity =
256 suppConfig.second.find("Polarity");
257 if (findPolarity != suppConfig.second.end())
258 {
259 if (std::string("Low") ==
260 std::visit(VariantToStringVisitor(),
261 findPolarity->second))
262 {
263 polarity = gpiod::line::ACTIVE_LOW;
264 }
265 }
266 bridgeGpio = BridgeGpio(gpioName, polarity);
267 }
268
269 break;
270 }
271 }
272
Yong Li1afda6b2020-04-09 19:24:13 +0800273 sensor = std::make_shared<ADCSensor>(
James Feistc71c1192019-09-18 14:31:33 -0700274 path.string(), objectServer, dbusConnection, io, sensorName,
275 std::move(sensorThresholds), scaleFactor, readState,
276 *interfacePath, std::move(bridgeGpio));
Yong Li1afda6b2020-04-09 19:24:13 +0800277 sensor->setupRead();
James Feistc71c1192019-09-18 14:31:33 -0700278 }
279 }));
280
281 getter->getConfiguration(
282 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700283}
284
James Feistb6c0b912019-07-09 12:21:44 -0700285int main()
James Feist6714a252018-09-10 15:26:18 -0700286{
287 boost::asio::io_service io;
288 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
289 systemBus->request_name("xyz.openbmc_project.ADCSensor");
290 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800291 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700292 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
293 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
294 std::make_unique<boost::container::flat_set<std::string>>();
295
296 io.post([&]() {
297 createSensors(io, objectServer, sensors, systemBus, nullptr);
298 });
299
300 boost::asio::deadline_timer filterTimer(io);
301 std::function<void(sdbusplus::message::message&)> eventHandler =
302 [&](sdbusplus::message::message& message) {
303 if (message.is_method_error())
304 {
305 std::cerr << "callback method error\n";
306 return;
307 }
308 sensorsChanged->insert(message.get_path());
309 // this implicitly cancels the timer
310 filterTimer.expires_from_now(boost::posix_time::seconds(1));
311
312 filterTimer.async_wait([&](const boost::system::error_code& ec) {
313 if (ec == boost::asio::error::operation_aborted)
314 {
315 /* we were canceled*/
316 return;
317 }
318 else if (ec)
319 {
320 std::cerr << "timer error\n";
321 return;
322 }
323 createSensors(io, objectServer, sensors, systemBus,
324 sensorsChanged);
325 });
326 };
327
James Feistb83bb2b2019-05-31 12:31:23 -0700328 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
329 [&](sdbusplus::message::message& message) {
330 std::string path = message.get_path();
331 boost::to_lower(path);
332
333 if (path.rfind("cpu") == std::string::npos)
334 {
335 return; // not interested
336 }
337 size_t index = 0;
338 try
339 {
340 index = std::stoi(path.substr(path.size() - 1));
341 }
342 catch (std::invalid_argument&)
343 {
344 std::cerr << "Found invalid path " << path << "\n";
345 return;
346 }
347
348 std::string objectName;
349 boost::container::flat_map<std::string, std::variant<bool>> values;
350 message.read(objectName, values);
351 auto findPresence = values.find("Present");
352 if (findPresence != values.end())
353 {
354 cpuPresence[index] = std::get<bool>(findPresence->second);
355 }
356
357 // this implicitly cancels the timer
358 filterTimer.expires_from_now(boost::posix_time::seconds(1));
359
360 filterTimer.async_wait([&](const boost::system::error_code& ec) {
361 if (ec == boost::asio::error::operation_aborted)
362 {
363 /* we were canceled*/
364 return;
365 }
366 else if (ec)
367 {
368 std::cerr << "timer error\n";
369 return;
370 }
371 createSensors(io, objectServer, sensors, systemBus, {});
372 });
373 };
374
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700375 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700376 {
377 auto match = std::make_unique<sdbusplus::bus::match::match>(
378 static_cast<sdbusplus::bus::bus&>(*systemBus),
379 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700380 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700381 eventHandler);
382 matches.emplace_back(std::move(match));
383 }
James Feistb83bb2b2019-05-31 12:31:23 -0700384 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
385 static_cast<sdbusplus::bus::bus&>(*systemBus),
386 "type='signal',member='PropertiesChanged',path_namespace='" +
387 std::string(cpuInventoryPath) +
388 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
389 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700390
391 io.run();
392}