blob: 9024eb9cb3961010b53e6e3aa4033ceecc00cb98 [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);
213 }
214
215 auto findPowerOn = baseConfiguration->second.find("PowerState");
216 PowerState readState = PowerState::always;
217 if (findPowerOn != baseConfiguration->second.end())
218 {
219 std::string powerState = std::visit(
220 VariantToStringVisitor(), findPowerOn->second);
221 setReadState(powerState, readState);
222 }
223
224 auto findCPU = baseConfiguration->second.find("CPURequired");
225 if (findCPU != baseConfiguration->second.end())
226 {
227 size_t index =
228 std::visit(VariantToIntVisitor(), findCPU->second);
229 auto presenceFind = cpuPresence.find(index);
230 if (presenceFind == cpuPresence.end())
231 {
232 continue; // no such cpu
233 }
234 if (!presenceFind->second)
235 {
236 continue; // cpu not installed
237 }
238 }
239
240 auto& sensor = sensors[sensorName];
241 sensor = nullptr;
242
243 std::optional<BridgeGpio> bridgeGpio;
244 for (const SensorBaseConfiguration& suppConfig : *sensorData)
245 {
246 if (suppConfig.first.find("BridgeGpio") !=
247 std::string::npos)
248 {
249 auto findName = suppConfig.second.find("Name");
250 if (findName != suppConfig.second.end())
251 {
252 std::string gpioName = std::visit(
253 VariantToStringVisitor(), findName->second);
254
255 int polarity = gpiod::line::ACTIVE_HIGH;
256 auto findPolarity =
257 suppConfig.second.find("Polarity");
258 if (findPolarity != suppConfig.second.end())
259 {
260 if (std::string("Low") ==
261 std::visit(VariantToStringVisitor(),
262 findPolarity->second))
263 {
264 polarity = gpiod::line::ACTIVE_LOW;
265 }
266 }
267 bridgeGpio = BridgeGpio(gpioName, polarity);
268 }
269
270 break;
271 }
272 }
273
Yong Li1afda6b2020-04-09 19:24:13 +0800274 sensor = std::make_shared<ADCSensor>(
James Feistc71c1192019-09-18 14:31:33 -0700275 path.string(), objectServer, dbusConnection, io, sensorName,
276 std::move(sensorThresholds), scaleFactor, readState,
277 *interfacePath, std::move(bridgeGpio));
Yong Li1afda6b2020-04-09 19:24:13 +0800278 sensor->setupRead();
James Feistc71c1192019-09-18 14:31:33 -0700279 }
280 }));
281
282 getter->getConfiguration(
283 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700284}
285
James Feistb6c0b912019-07-09 12:21:44 -0700286int main()
James Feist6714a252018-09-10 15:26:18 -0700287{
288 boost::asio::io_service io;
289 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
290 systemBus->request_name("xyz.openbmc_project.ADCSensor");
291 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800292 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700293 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700294 auto sensorsChanged =
295 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700296
297 io.post([&]() {
298 createSensors(io, objectServer, sensors, systemBus, nullptr);
299 });
300
301 boost::asio::deadline_timer filterTimer(io);
302 std::function<void(sdbusplus::message::message&)> eventHandler =
303 [&](sdbusplus::message::message& message) {
304 if (message.is_method_error())
305 {
306 std::cerr << "callback method error\n";
307 return;
308 }
309 sensorsChanged->insert(message.get_path());
310 // this implicitly cancels the timer
311 filterTimer.expires_from_now(boost::posix_time::seconds(1));
312
313 filterTimer.async_wait([&](const boost::system::error_code& ec) {
314 if (ec == boost::asio::error::operation_aborted)
315 {
316 /* we were canceled*/
317 return;
318 }
319 else if (ec)
320 {
321 std::cerr << "timer error\n";
322 return;
323 }
324 createSensors(io, objectServer, sensors, systemBus,
325 sensorsChanged);
326 });
327 };
328
James Feistb83bb2b2019-05-31 12:31:23 -0700329 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
330 [&](sdbusplus::message::message& message) {
331 std::string path = message.get_path();
332 boost::to_lower(path);
333
334 if (path.rfind("cpu") == std::string::npos)
335 {
336 return; // not interested
337 }
338 size_t index = 0;
339 try
340 {
341 index = std::stoi(path.substr(path.size() - 1));
342 }
343 catch (std::invalid_argument&)
344 {
345 std::cerr << "Found invalid path " << path << "\n";
346 return;
347 }
348
349 std::string objectName;
350 boost::container::flat_map<std::string, std::variant<bool>> values;
351 message.read(objectName, values);
352 auto findPresence = values.find("Present");
353 if (findPresence != values.end())
354 {
355 cpuPresence[index] = std::get<bool>(findPresence->second);
356 }
357
358 // this implicitly cancels the timer
359 filterTimer.expires_from_now(boost::posix_time::seconds(1));
360
361 filterTimer.async_wait([&](const boost::system::error_code& ec) {
362 if (ec == boost::asio::error::operation_aborted)
363 {
364 /* we were canceled*/
365 return;
366 }
367 else if (ec)
368 {
369 std::cerr << "timer error\n";
370 return;
371 }
James Feist5591cf082020-07-15 16:44:54 -0700372 createSensors(io, objectServer, sensors, systemBus, nullptr);
James Feistb83bb2b2019-05-31 12:31:23 -0700373 });
374 };
375
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700376 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700377 {
378 auto match = std::make_unique<sdbusplus::bus::match::match>(
379 static_cast<sdbusplus::bus::bus&>(*systemBus),
380 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700381 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700382 eventHandler);
383 matches.emplace_back(std::move(match));
384 }
James Feistb83bb2b2019-05-31 12:31:23 -0700385 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
386 static_cast<sdbusplus::bus::bus&>(*systemBus),
387 "type='signal',member='PropertiesChanged',path_namespace='" +
388 std::string(cpuInventoryPath) +
389 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
390 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700391
392 io.run();
393}