blob: 9d29895364ef63f50dc8fbaaa424a5dc8f2db7f4 [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070017#include <ADCSensor.hpp>
18#include <Utils.hpp>
19#include <VariantVisitors.hpp>
James Feistb83bb2b2019-05-31 12:31:23 -070020#include <boost/algorithm/string/case_conv.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
James Feist24f02f22019-04-15 11:05:39 -070028#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070029#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070030#include <functional>
31#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040032#include <optional>
James Feist6714a252018-09-10 15:26:18 -070033#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <string>
35#include <variant>
36#include <vector>
James Feist6714a252018-09-10 15:26:18 -070037
Ed Tanous8a57ec02020-10-09 12:46:52 -070038static constexpr bool debug = false;
Jeff Lind9cd7042020-11-20 15:49:28 +080039static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070040
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
Jeff Lind9cd7042020-11-20 15:49:28 +0800220 auto findPollRate = baseConfiguration->second.find("PollRate");
221 float pollRate = pollRateDefault;
222 if (findPollRate != baseConfiguration->second.end())
223 {
224 pollRate = std::visit(VariantToFloatVisitor(),
225 findPollRate->second);
226 if (pollRate <= 0.0f)
227 {
228 pollRate = pollRateDefault; // polling time too short
229 }
230 }
231
James Feistc71c1192019-09-18 14:31:33 -0700232 auto findPowerOn = baseConfiguration->second.find("PowerState");
233 PowerState readState = PowerState::always;
234 if (findPowerOn != baseConfiguration->second.end())
235 {
236 std::string powerState = std::visit(
237 VariantToStringVisitor(), findPowerOn->second);
238 setReadState(powerState, readState);
239 }
240
241 auto findCPU = baseConfiguration->second.find("CPURequired");
242 if (findCPU != baseConfiguration->second.end())
243 {
244 size_t index =
245 std::visit(VariantToIntVisitor(), findCPU->second);
246 auto presenceFind = cpuPresence.find(index);
247 if (presenceFind == cpuPresence.end())
248 {
249 continue; // no such cpu
250 }
251 if (!presenceFind->second)
252 {
253 continue; // cpu not installed
254 }
255 }
256
257 auto& sensor = sensors[sensorName];
258 sensor = nullptr;
259
260 std::optional<BridgeGpio> bridgeGpio;
261 for (const SensorBaseConfiguration& suppConfig : *sensorData)
262 {
263 if (suppConfig.first.find("BridgeGpio") !=
264 std::string::npos)
265 {
266 auto findName = suppConfig.second.find("Name");
267 if (findName != suppConfig.second.end())
268 {
269 std::string gpioName = std::visit(
270 VariantToStringVisitor(), findName->second);
271
272 int polarity = gpiod::line::ACTIVE_HIGH;
273 auto findPolarity =
274 suppConfig.second.find("Polarity");
275 if (findPolarity != suppConfig.second.end())
276 {
277 if (std::string("Low") ==
278 std::visit(VariantToStringVisitor(),
279 findPolarity->second))
280 {
281 polarity = gpiod::line::ACTIVE_LOW;
282 }
283 }
284 bridgeGpio = BridgeGpio(gpioName, polarity);
285 }
286
287 break;
288 }
289 }
290
Yong Li1afda6b2020-04-09 19:24:13 +0800291 sensor = std::make_shared<ADCSensor>(
James Feistc71c1192019-09-18 14:31:33 -0700292 path.string(), objectServer, dbusConnection, io, sensorName,
Jeff Lind9cd7042020-11-20 15:49:28 +0800293 std::move(sensorThresholds), scaleFactor, pollRate,
294 readState, *interfacePath, std::move(bridgeGpio));
Yong Li1afda6b2020-04-09 19:24:13 +0800295 sensor->setupRead();
James Feistc71c1192019-09-18 14:31:33 -0700296 }
297 }));
298
299 getter->getConfiguration(
300 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700301}
302
James Feistb6c0b912019-07-09 12:21:44 -0700303int main()
James Feist6714a252018-09-10 15:26:18 -0700304{
305 boost::asio::io_service io;
306 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
307 systemBus->request_name("xyz.openbmc_project.ADCSensor");
308 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800309 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700310 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700311 auto sensorsChanged =
312 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700313
314 io.post([&]() {
315 createSensors(io, objectServer, sensors, systemBus, nullptr);
316 });
317
318 boost::asio::deadline_timer filterTimer(io);
319 std::function<void(sdbusplus::message::message&)> eventHandler =
320 [&](sdbusplus::message::message& message) {
321 if (message.is_method_error())
322 {
323 std::cerr << "callback method error\n";
324 return;
325 }
326 sensorsChanged->insert(message.get_path());
327 // this implicitly cancels the timer
328 filterTimer.expires_from_now(boost::posix_time::seconds(1));
329
330 filterTimer.async_wait([&](const boost::system::error_code& ec) {
331 if (ec == boost::asio::error::operation_aborted)
332 {
333 /* we were canceled*/
334 return;
335 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700336 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700337 {
338 std::cerr << "timer error\n";
339 return;
340 }
341 createSensors(io, objectServer, sensors, systemBus,
342 sensorsChanged);
343 });
344 };
345
James Feistb83bb2b2019-05-31 12:31:23 -0700346 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
347 [&](sdbusplus::message::message& message) {
348 std::string path = message.get_path();
349 boost::to_lower(path);
350
351 if (path.rfind("cpu") == std::string::npos)
352 {
353 return; // not interested
354 }
355 size_t index = 0;
356 try
357 {
358 index = std::stoi(path.substr(path.size() - 1));
359 }
360 catch (std::invalid_argument&)
361 {
362 std::cerr << "Found invalid path " << path << "\n";
363 return;
364 }
365
366 std::string objectName;
367 boost::container::flat_map<std::string, std::variant<bool>> values;
368 message.read(objectName, values);
369 auto findPresence = values.find("Present");
370 if (findPresence != values.end())
371 {
372 cpuPresence[index] = std::get<bool>(findPresence->second);
373 }
374
375 // this implicitly cancels the timer
376 filterTimer.expires_from_now(boost::posix_time::seconds(1));
377
378 filterTimer.async_wait([&](const boost::system::error_code& ec) {
379 if (ec == boost::asio::error::operation_aborted)
380 {
381 /* we were canceled*/
382 return;
383 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700384 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700385 {
386 std::cerr << "timer error\n";
387 return;
388 }
James Feist5591cf082020-07-15 16:44:54 -0700389 createSensors(io, objectServer, sensors, systemBus, nullptr);
James Feistb83bb2b2019-05-31 12:31:23 -0700390 });
391 };
392
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700393 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700394 {
395 auto match = std::make_unique<sdbusplus::bus::match::match>(
396 static_cast<sdbusplus::bus::bus&>(*systemBus),
397 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700398 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700399 eventHandler);
400 matches.emplace_back(std::move(match));
401 }
James Feistb83bb2b2019-05-31 12:31:23 -0700402 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
403 static_cast<sdbusplus::bus::bus&>(*systemBus),
404 "type='signal',member='PropertiesChanged',path_namespace='" +
405 std::string(cpuInventoryPath) +
406 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
407 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700408
Bruce Lee1263c3d2021-06-04 15:16:33 +0800409 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700410 io.run();
411}