blob: ef9ef44d7284abb322bc01b585c474b12065da57 [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
Matt Spinler6ad74d92022-03-01 10:56:46 -060049enum class UpdateType
50{
51 init,
52 cpuPresenceChange
53};
54
James Feist08aec6f2019-01-30 16:17:04 -080055// filter out adc from any other voltage sensor
56bool isAdc(const fs::path& parentPath)
57{
58 fs::path namePath = parentPath / "name";
59
60 std::ifstream nameFile(namePath);
61 if (!nameFile.good())
62 {
63 std::cerr << "Failure reading " << namePath.string() << "\n";
64 return false;
65 }
66
67 std::string name;
68 std::getline(nameFile, name);
69
70 return name == "iio_hwmon";
71}
72
James Feist6714a252018-09-10 15:26:18 -070073void createSensors(
74 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080075 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070076 sensors,
77 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070078 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Spinler6ad74d92022-03-01 10:56:46 -060079 sensorsChanged,
80 UpdateType updateType)
James Feist6714a252018-09-10 15:26:18 -070081{
James Feistc71c1192019-09-18 14:31:33 -070082 auto getter = std::make_shared<GetSensorConfiguration>(
83 dbusConnection,
Matt Spinler6ad74d92022-03-01 10:56:46 -060084 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
85 updateType](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -070086 bool firstScan = sensorsChanged == nullptr;
87 std::vector<fs::path> paths;
88 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
89 {
90 std::cerr << "No adc sensors in system\n";
91 return;
92 }
93
94 // iterate through all found adc sensors, and try to match them with
95 // configuration
96 for (auto& path : paths)
97 {
98 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080099 {
Ed Tanousbb679322022-05-16 16:10:00 -0700100 continue;
James Feistc71c1192019-09-18 14:31:33 -0700101 }
Ed Tanousbb679322022-05-16 16:10:00 -0700102 std::smatch match;
103 std::string pathStr = path.string();
James Feistc71c1192019-09-18 14:31:33 -0700104
Ed Tanousbb679322022-05-16 16:10:00 -0700105 std::regex_search(pathStr, match, inputRegex);
106 std::string indexStr = *(match.begin() + 1);
107
108 auto directory = path.parent_path();
109 // convert to 0 based
110 size_t index = std::stoul(indexStr) - 1;
111
112 const SensorData* sensorData = nullptr;
113 const std::string* interfacePath = nullptr;
114 const std::pair<std::string, boost::container::flat_map<
115 std::string, BasicVariantType>>*
116 baseConfiguration = nullptr;
117 for (const std::pair<sdbusplus::message::object_path, SensorData>&
118 sensor : sensorConfigurations)
James Feistc71c1192019-09-18 14:31:33 -0700119 {
Ed Tanousbb679322022-05-16 16:10:00 -0700120 // clear it out each loop
121 baseConfiguration = nullptr;
122
123 // find base configuration
124 for (const char* type : sensorTypes)
James Feist08aec6f2019-01-30 16:17:04 -0800125 {
Ed Tanousbb679322022-05-16 16:10:00 -0700126 auto sensorBase = sensor.second.find(type);
127 if (sensorBase != sensor.second.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700128 {
Ed Tanousbb679322022-05-16 16:10:00 -0700129 baseConfiguration = &(*sensorBase);
James Feistc71c1192019-09-18 14:31:33 -0700130 break;
131 }
132 }
Ed Tanousbb679322022-05-16 16:10:00 -0700133 if (baseConfiguration == nullptr)
134 {
135 continue;
136 }
137 auto findIndex = baseConfiguration->second.find("Index");
138 if (findIndex == baseConfiguration->second.end())
139 {
140 std::cerr << "Base configuration missing Index"
141 << baseConfiguration->first << "\n";
142 continue;
143 }
James Feistc71c1192019-09-18 14:31:33 -0700144
Ed Tanousbb679322022-05-16 16:10:00 -0700145 unsigned int number = std::visit(VariantToUnsignedIntVisitor(),
146 findIndex->second);
147
148 if (number != index)
149 {
150 continue;
151 }
152
153 sensorData = &(sensor.second);
154 interfacePath = &(sensor.first.str);
155 break;
James Feistc71c1192019-09-18 14:31:33 -0700156 }
Ed Tanousbb679322022-05-16 16:10:00 -0700157 if (sensorData == nullptr)
158 {
159 std::cerr << "failed to find match for " << path.string()
160 << "\n";
161 continue;
162 }
163
164 if (baseConfiguration == nullptr)
165 {
166 std::cerr << "error finding base configuration for"
167 << path.string() << "\n";
168 continue;
169 }
170
171 auto findSensorName = baseConfiguration->second.find("Name");
172 if (findSensorName == baseConfiguration->second.end())
173 {
174 std::cerr << "could not determine configuration name for "
175 << path.string() << "\n";
176 continue;
177 }
178 std::string sensorName =
179 std::get<std::string>(findSensorName->second);
180
181 // on rescans, only update sensors we were signaled by
182 auto findSensor = sensors.find(sensorName);
183 if (!firstScan && findSensor != sensors.end())
184 {
185 bool found = false;
186 for (auto it = sensorsChanged->begin();
187 it != sensorsChanged->end(); it++)
188 {
189 if (findSensor->second &&
190 boost::ends_with(*it, findSensor->second->name))
191 {
192 sensorsChanged->erase(it);
193 findSensor->second = nullptr;
194 found = true;
195 break;
196 }
197 }
198 if (!found)
199 {
200 continue;
201 }
202 }
203
204 auto findCPU = baseConfiguration->second.find("CPURequired");
205 if (findCPU != baseConfiguration->second.end())
206 {
207 size_t index =
208 std::visit(VariantToIntVisitor(), findCPU->second);
209 auto presenceFind = cpuPresence.find(index);
210 if (presenceFind == cpuPresence.end())
211 {
212 continue; // no such cpu
213 }
214 if (!presenceFind->second)
215 {
216 continue; // cpu not installed
217 }
218 }
219 else if (updateType == UpdateType::cpuPresenceChange)
220 {
221 continue;
222 }
223
224 std::vector<thresholds::Threshold> sensorThresholds;
225 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
226 {
227 std::cerr << "error populating thresholds for " << sensorName
228 << "\n";
229 }
230
231 auto findScaleFactor =
232 baseConfiguration->second.find("ScaleFactor");
233 float scaleFactor = 1.0;
234 if (findScaleFactor != baseConfiguration->second.end())
235 {
236 scaleFactor = std::visit(VariantToFloatVisitor(),
237 findScaleFactor->second);
238 // scaleFactor is used in division
Ed Tanous2049bd22022-07-09 07:20:26 -0700239 if (scaleFactor == 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700240 {
241 scaleFactor = 1.0;
242 }
243 }
244
245 auto findPollRate = baseConfiguration->second.find("PollRate");
246 float pollRate = pollRateDefault;
247 if (findPollRate != baseConfiguration->second.end())
248 {
249 pollRate =
250 std::visit(VariantToFloatVisitor(), findPollRate->second);
Ed Tanous2049bd22022-07-09 07:20:26 -0700251 if (pollRate <= 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700252 {
253 pollRate = pollRateDefault; // polling time too short
254 }
255 }
256
257 auto findPowerOn = baseConfiguration->second.find("PowerState");
258 PowerState readState = PowerState::always;
259 if (findPowerOn != baseConfiguration->second.end())
260 {
261 std::string powerState =
262 std::visit(VariantToStringVisitor(), findPowerOn->second);
263 setReadState(powerState, readState);
264 }
265
266 auto& sensor = sensors[sensorName];
267 sensor = nullptr;
268
269 std::optional<BridgeGpio> bridgeGpio;
270 for (const SensorBaseConfiguration& suppConfig : *sensorData)
271 {
272 if (suppConfig.first.find("BridgeGpio") != std::string::npos)
273 {
274 auto findName = suppConfig.second.find("Name");
275 if (findName != suppConfig.second.end())
276 {
277 std::string gpioName = std::visit(
278 VariantToStringVisitor(), findName->second);
279
280 int polarity = gpiod::line::ACTIVE_HIGH;
281 auto findPolarity = suppConfig.second.find("Polarity");
282 if (findPolarity != suppConfig.second.end())
283 {
284 if (std::string("Low") ==
285 std::visit(VariantToStringVisitor(),
286 findPolarity->second))
287 {
288 polarity = gpiod::line::ACTIVE_LOW;
289 }
290 }
291
292 float setupTime = gpioBridgeSetupTimeDefault;
293 auto findSetupTime =
294 suppConfig.second.find("SetupTime");
295 if (findSetupTime != suppConfig.second.end())
296 {
297 setupTime = std::visit(VariantToFloatVisitor(),
298 findSetupTime->second);
299 }
300
301 bridgeGpio = BridgeGpio(gpioName, polarity, setupTime);
302 }
303
304 break;
305 }
306 }
307
308 sensor = std::make_shared<ADCSensor>(
309 path.string(), objectServer, dbusConnection, io, sensorName,
310 std::move(sensorThresholds), scaleFactor, pollRate, readState,
311 *interfacePath, std::move(bridgeGpio));
312 sensor->setupRead();
313 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700314 });
James Feistc71c1192019-09-18 14:31:33 -0700315
316 getter->getConfiguration(
317 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700318}
319
James Feistb6c0b912019-07-09 12:21:44 -0700320int main()
James Feist6714a252018-09-10 15:26:18 -0700321{
322 boost::asio::io_service io;
323 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
324 systemBus->request_name("xyz.openbmc_project.ADCSensor");
325 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800326 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700327 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700328 auto sensorsChanged =
329 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700330
331 io.post([&]() {
Matt Spinler6ad74d92022-03-01 10:56:46 -0600332 createSensors(io, objectServer, sensors, systemBus, nullptr,
333 UpdateType::init);
James Feist6714a252018-09-10 15:26:18 -0700334 });
335
336 boost::asio::deadline_timer filterTimer(io);
337 std::function<void(sdbusplus::message::message&)> eventHandler =
338 [&](sdbusplus::message::message& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700339 if (message.is_method_error())
340 {
341 std::cerr << "callback method error\n";
342 return;
343 }
344 sensorsChanged->insert(message.get_path());
345 // this implicitly cancels the timer
346 filterTimer.expires_from_now(boost::posix_time::seconds(1));
347
348 filterTimer.async_wait([&](const boost::system::error_code& ec) {
349 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700350 {
Ed Tanousbb679322022-05-16 16:10:00 -0700351 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700352 return;
353 }
Ed Tanousbb679322022-05-16 16:10:00 -0700354 if (ec)
355 {
356 std::cerr << "timer error\n";
357 return;
358 }
359 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
360 UpdateType::init);
361 });
362 };
James Feist6714a252018-09-10 15:26:18 -0700363
James Feistb83bb2b2019-05-31 12:31:23 -0700364 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
365 [&](sdbusplus::message::message& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700366 std::string path = message.get_path();
367 boost::to_lower(path);
James Feistb83bb2b2019-05-31 12:31:23 -0700368
Ed Tanousbb679322022-05-16 16:10:00 -0700369 if (path.rfind("cpu") == std::string::npos)
370 {
371 return; // not interested
372 }
373 size_t index = 0;
374 try
375 {
376 index = std::stoi(path.substr(path.size() - 1));
377 }
378 catch (const std::invalid_argument&)
379 {
380 std::cerr << "Found invalid path " << path << "\n";
381 return;
382 }
383
384 std::string objectName;
385 boost::container::flat_map<std::string, std::variant<bool>> values;
386 message.read(objectName, values);
387 auto findPresence = values.find("Present");
388 if (findPresence != values.end())
389 {
390 cpuPresence[index] = std::get<bool>(findPresence->second);
391 }
392
393 // this implicitly cancels the timer
394 filterTimer.expires_from_now(boost::posix_time::seconds(1));
395
396 filterTimer.async_wait([&](const boost::system::error_code& ec) {
397 if (ec == boost::asio::error::operation_aborted)
James Feistb83bb2b2019-05-31 12:31:23 -0700398 {
Ed Tanousbb679322022-05-16 16:10:00 -0700399 /* we were canceled*/
James Feistb83bb2b2019-05-31 12:31:23 -0700400 return;
401 }
Ed Tanousbb679322022-05-16 16:10:00 -0700402 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700403 {
Ed Tanousbb679322022-05-16 16:10:00 -0700404 std::cerr << "timer error\n";
405 return;
James Feistb83bb2b2019-05-31 12:31:23 -0700406 }
Ed Tanousbb679322022-05-16 16:10:00 -0700407 createSensors(io, objectServer, sensors, systemBus, nullptr,
408 UpdateType::cpuPresenceChange);
409 });
410 };
James Feistb83bb2b2019-05-31 12:31:23 -0700411
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700412 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700413 {
414 auto match = std::make_unique<sdbusplus::bus::match::match>(
415 static_cast<sdbusplus::bus::bus&>(*systemBus),
416 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700417 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700418 eventHandler);
419 matches.emplace_back(std::move(match));
420 }
James Feistb83bb2b2019-05-31 12:31:23 -0700421 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
422 static_cast<sdbusplus::bus::bus&>(*systemBus),
423 "type='signal',member='PropertiesChanged',path_namespace='" +
424 std::string(cpuInventoryPath) +
425 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
426 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700427
Bruce Lee1263c3d2021-06-04 15:16:33 +0800428 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700429 io.run();
430}