blob: 1fac7dd544a820040976db7ae9ae7e3d62181265 [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
Jeff Lind9cd7042020-11-20 15:49:28 +080038static constexpr float pollRateDefault = 0.5;
Zev Weissf72eb832021-06-25 05:55:08 +000039static constexpr float gpioBridgeSetupTimeDefault = 0.02;
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
Brandon Kim66558232021-11-09 16:53:08 -080043static constexpr auto sensorTypes{
44 std::to_array<const char*>({"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,
Ed Tanous8a17c302021-09-02 15:07:11 -070077 [&io, &objectServer, &sensors, &dbusConnection,
78 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
James Feistc71c1192019-09-18 14:31:33 -070079 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 {
krishnar4a040ec42021-10-06 16:44:12 +053084 std::cerr << "No adc sensors in system\n";
James Feistc71c1192019-09-18 14:31:33 -070085 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 {
Wludzik, Jozef9a445472020-06-24 12:11:09 +0200184 if (findSensor->second &&
185 boost::ends_with(*it, findSensor->second->name))
James Feistc71c1192019-09-18 14:31:33 -0700186 {
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);
Zhikui Ren937eb542020-10-12 13:42:08 -0700212 // scaleFactor is used in division
213 if (scaleFactor == 0.0f)
214 {
215 scaleFactor = 1.0;
216 }
James Feistc71c1192019-09-18 14:31:33 -0700217 }
218
Jeff Lind9cd7042020-11-20 15:49:28 +0800219 auto findPollRate = baseConfiguration->second.find("PollRate");
220 float pollRate = pollRateDefault;
221 if (findPollRate != baseConfiguration->second.end())
222 {
223 pollRate = std::visit(VariantToFloatVisitor(),
224 findPollRate->second);
225 if (pollRate <= 0.0f)
226 {
227 pollRate = pollRateDefault; // polling time too short
228 }
229 }
230
James Feistc71c1192019-09-18 14:31:33 -0700231 auto findPowerOn = baseConfiguration->second.find("PowerState");
232 PowerState readState = PowerState::always;
233 if (findPowerOn != baseConfiguration->second.end())
234 {
235 std::string powerState = std::visit(
236 VariantToStringVisitor(), findPowerOn->second);
237 setReadState(powerState, readState);
238 }
239
240 auto findCPU = baseConfiguration->second.find("CPURequired");
241 if (findCPU != baseConfiguration->second.end())
242 {
243 size_t index =
244 std::visit(VariantToIntVisitor(), findCPU->second);
245 auto presenceFind = cpuPresence.find(index);
246 if (presenceFind == cpuPresence.end())
247 {
248 continue; // no such cpu
249 }
250 if (!presenceFind->second)
251 {
252 continue; // cpu not installed
253 }
254 }
255
256 auto& sensor = sensors[sensorName];
257 sensor = nullptr;
258
259 std::optional<BridgeGpio> bridgeGpio;
260 for (const SensorBaseConfiguration& suppConfig : *sensorData)
261 {
262 if (suppConfig.first.find("BridgeGpio") !=
263 std::string::npos)
264 {
265 auto findName = suppConfig.second.find("Name");
266 if (findName != suppConfig.second.end())
267 {
268 std::string gpioName = std::visit(
269 VariantToStringVisitor(), findName->second);
270
271 int polarity = gpiod::line::ACTIVE_HIGH;
272 auto findPolarity =
273 suppConfig.second.find("Polarity");
274 if (findPolarity != suppConfig.second.end())
275 {
276 if (std::string("Low") ==
277 std::visit(VariantToStringVisitor(),
278 findPolarity->second))
279 {
280 polarity = gpiod::line::ACTIVE_LOW;
281 }
282 }
Zev Weissf72eb832021-06-25 05:55:08 +0000283
284 float setupTime = gpioBridgeSetupTimeDefault;
285 auto findSetupTime =
286 suppConfig.second.find("SetupTime");
287 if (findSetupTime != suppConfig.second.end())
288 {
289 setupTime = std::visit(VariantToFloatVisitor(),
290 findSetupTime->second);
291 }
292
293 bridgeGpio =
294 BridgeGpio(gpioName, polarity, setupTime);
James Feistc71c1192019-09-18 14:31:33 -0700295 }
296
297 break;
298 }
299 }
300
Yong Li1afda6b2020-04-09 19:24:13 +0800301 sensor = std::make_shared<ADCSensor>(
James Feistc71c1192019-09-18 14:31:33 -0700302 path.string(), objectServer, dbusConnection, io, sensorName,
Jeff Lind9cd7042020-11-20 15:49:28 +0800303 std::move(sensorThresholds), scaleFactor, pollRate,
304 readState, *interfacePath, std::move(bridgeGpio));
Yong Li1afda6b2020-04-09 19:24:13 +0800305 sensor->setupRead();
James Feistc71c1192019-09-18 14:31:33 -0700306 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700307 });
James Feistc71c1192019-09-18 14:31:33 -0700308
309 getter->getConfiguration(
310 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700311}
312
James Feistb6c0b912019-07-09 12:21:44 -0700313int main()
James Feist6714a252018-09-10 15:26:18 -0700314{
315 boost::asio::io_service io;
316 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
317 systemBus->request_name("xyz.openbmc_project.ADCSensor");
318 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800319 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700320 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700321 auto sensorsChanged =
322 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700323
324 io.post([&]() {
325 createSensors(io, objectServer, sensors, systemBus, nullptr);
326 });
327
328 boost::asio::deadline_timer filterTimer(io);
329 std::function<void(sdbusplus::message::message&)> eventHandler =
330 [&](sdbusplus::message::message& message) {
331 if (message.is_method_error())
332 {
333 std::cerr << "callback method error\n";
334 return;
335 }
336 sensorsChanged->insert(message.get_path());
337 // this implicitly cancels the timer
338 filterTimer.expires_from_now(boost::posix_time::seconds(1));
339
340 filterTimer.async_wait([&](const boost::system::error_code& ec) {
341 if (ec == boost::asio::error::operation_aborted)
342 {
343 /* we were canceled*/
344 return;
345 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700346 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700347 {
348 std::cerr << "timer error\n";
349 return;
350 }
351 createSensors(io, objectServer, sensors, systemBus,
352 sensorsChanged);
353 });
354 };
355
James Feistb83bb2b2019-05-31 12:31:23 -0700356 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
357 [&](sdbusplus::message::message& message) {
358 std::string path = message.get_path();
359 boost::to_lower(path);
360
361 if (path.rfind("cpu") == std::string::npos)
362 {
363 return; // not interested
364 }
365 size_t index = 0;
366 try
367 {
368 index = std::stoi(path.substr(path.size() - 1));
369 }
Patrick Williams26601e82021-10-06 12:43:25 -0500370 catch (const std::invalid_argument&)
James Feistb83bb2b2019-05-31 12:31:23 -0700371 {
372 std::cerr << "Found invalid path " << path << "\n";
373 return;
374 }
375
376 std::string objectName;
377 boost::container::flat_map<std::string, std::variant<bool>> values;
378 message.read(objectName, values);
379 auto findPresence = values.find("Present");
380 if (findPresence != values.end())
381 {
382 cpuPresence[index] = std::get<bool>(findPresence->second);
383 }
384
385 // this implicitly cancels the timer
386 filterTimer.expires_from_now(boost::posix_time::seconds(1));
387
388 filterTimer.async_wait([&](const boost::system::error_code& ec) {
389 if (ec == boost::asio::error::operation_aborted)
390 {
391 /* we were canceled*/
392 return;
393 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700394 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700395 {
396 std::cerr << "timer error\n";
397 return;
398 }
James Feist5591cf082020-07-15 16:44:54 -0700399 createSensors(io, objectServer, sensors, systemBus, nullptr);
James Feistb83bb2b2019-05-31 12:31:23 -0700400 });
401 };
402
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700403 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700404 {
405 auto match = std::make_unique<sdbusplus::bus::match::match>(
406 static_cast<sdbusplus::bus::bus&>(*systemBus),
407 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700408 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700409 eventHandler);
410 matches.emplace_back(std::move(match));
411 }
James Feistb83bb2b2019-05-31 12:31:23 -0700412 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
413 static_cast<sdbusplus::bus::bus&>(*systemBus),
414 "type='signal',member='PropertiesChanged',path_namespace='" +
415 std::string(cpuInventoryPath) +
416 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
417 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700418
Bruce Lee1263c3d2021-06-04 15:16:33 +0800419 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700420 io.run();
421}