blob: cec0984a59b620ef85eee6cde9aa8cd63b857959 [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;
Zev Weissafd15042022-07-18 12:28:40 -0700114 const std::pair<std::string, SensorBaseConfigMap>*
Ed Tanousbb679322022-05-16 16:10:00 -0700115 baseConfiguration = nullptr;
116 for (const std::pair<sdbusplus::message::object_path, SensorData>&
117 sensor : sensorConfigurations)
James Feistc71c1192019-09-18 14:31:33 -0700118 {
Ed Tanousbb679322022-05-16 16:10:00 -0700119 // clear it out each loop
120 baseConfiguration = nullptr;
121
122 // find base configuration
123 for (const char* type : sensorTypes)
James Feist08aec6f2019-01-30 16:17:04 -0800124 {
Ed Tanousbb679322022-05-16 16:10:00 -0700125 auto sensorBase = sensor.second.find(type);
126 if (sensorBase != sensor.second.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700127 {
Ed Tanousbb679322022-05-16 16:10:00 -0700128 baseConfiguration = &(*sensorBase);
James Feistc71c1192019-09-18 14:31:33 -0700129 break;
130 }
131 }
Ed Tanousbb679322022-05-16 16:10:00 -0700132 if (baseConfiguration == nullptr)
133 {
134 continue;
135 }
136 auto findIndex = baseConfiguration->second.find("Index");
137 if (findIndex == baseConfiguration->second.end())
138 {
139 std::cerr << "Base configuration missing Index"
140 << baseConfiguration->first << "\n";
141 continue;
142 }
James Feistc71c1192019-09-18 14:31:33 -0700143
Ed Tanousbb679322022-05-16 16:10:00 -0700144 unsigned int number = std::visit(VariantToUnsignedIntVisitor(),
145 findIndex->second);
146
147 if (number != index)
148 {
149 continue;
150 }
151
152 sensorData = &(sensor.second);
153 interfacePath = &(sensor.first.str);
154 break;
James Feistc71c1192019-09-18 14:31:33 -0700155 }
Ed Tanousbb679322022-05-16 16:10:00 -0700156 if (sensorData == nullptr)
157 {
158 std::cerr << "failed to find match for " << path.string()
159 << "\n";
160 continue;
161 }
162
163 if (baseConfiguration == nullptr)
164 {
165 std::cerr << "error finding base configuration for"
166 << path.string() << "\n";
167 continue;
168 }
169
170 auto findSensorName = baseConfiguration->second.find("Name");
171 if (findSensorName == baseConfiguration->second.end())
172 {
173 std::cerr << "could not determine configuration name for "
174 << path.string() << "\n";
175 continue;
176 }
177 std::string sensorName =
178 std::get<std::string>(findSensorName->second);
179
180 // on rescans, only update sensors we were signaled by
181 auto findSensor = sensors.find(sensorName);
182 if (!firstScan && findSensor != sensors.end())
183 {
184 bool found = false;
185 for (auto it = sensorsChanged->begin();
186 it != sensorsChanged->end(); it++)
187 {
188 if (findSensor->second &&
189 boost::ends_with(*it, findSensor->second->name))
190 {
191 sensorsChanged->erase(it);
192 findSensor->second = nullptr;
193 found = true;
194 break;
195 }
196 }
197 if (!found)
198 {
199 continue;
200 }
201 }
202
203 auto findCPU = baseConfiguration->second.find("CPURequired");
204 if (findCPU != baseConfiguration->second.end())
205 {
206 size_t index =
207 std::visit(VariantToIntVisitor(), findCPU->second);
208 auto presenceFind = cpuPresence.find(index);
209 if (presenceFind == cpuPresence.end())
210 {
211 continue; // no such cpu
212 }
213 if (!presenceFind->second)
214 {
215 continue; // cpu not installed
216 }
217 }
218 else if (updateType == UpdateType::cpuPresenceChange)
219 {
220 continue;
221 }
222
223 std::vector<thresholds::Threshold> sensorThresholds;
224 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
225 {
226 std::cerr << "error populating thresholds for " << sensorName
227 << "\n";
228 }
229
230 auto findScaleFactor =
231 baseConfiguration->second.find("ScaleFactor");
232 float scaleFactor = 1.0;
233 if (findScaleFactor != baseConfiguration->second.end())
234 {
235 scaleFactor = std::visit(VariantToFloatVisitor(),
236 findScaleFactor->second);
237 // scaleFactor is used in division
Ed Tanous2049bd22022-07-09 07:20:26 -0700238 if (scaleFactor == 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700239 {
240 scaleFactor = 1.0;
241 }
242 }
243
244 auto findPollRate = baseConfiguration->second.find("PollRate");
245 float pollRate = pollRateDefault;
246 if (findPollRate != baseConfiguration->second.end())
247 {
248 pollRate =
249 std::visit(VariantToFloatVisitor(), findPollRate->second);
Ed Tanous2049bd22022-07-09 07:20:26 -0700250 if (pollRate <= 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700251 {
252 pollRate = pollRateDefault; // polling time too short
253 }
254 }
255
256 auto findPowerOn = baseConfiguration->second.find("PowerState");
257 PowerState readState = PowerState::always;
258 if (findPowerOn != baseConfiguration->second.end())
259 {
260 std::string powerState =
261 std::visit(VariantToStringVisitor(), findPowerOn->second);
262 setReadState(powerState, readState);
263 }
264
265 auto& sensor = sensors[sensorName];
266 sensor = nullptr;
267
268 std::optional<BridgeGpio> bridgeGpio;
269 for (const SensorBaseConfiguration& suppConfig : *sensorData)
270 {
271 if (suppConfig.first.find("BridgeGpio") != std::string::npos)
272 {
273 auto findName = suppConfig.second.find("Name");
274 if (findName != suppConfig.second.end())
275 {
276 std::string gpioName = std::visit(
277 VariantToStringVisitor(), findName->second);
278
279 int polarity = gpiod::line::ACTIVE_HIGH;
280 auto findPolarity = suppConfig.second.find("Polarity");
281 if (findPolarity != suppConfig.second.end())
282 {
283 if (std::string("Low") ==
284 std::visit(VariantToStringVisitor(),
285 findPolarity->second))
286 {
287 polarity = gpiod::line::ACTIVE_LOW;
288 }
289 }
290
291 float setupTime = gpioBridgeSetupTimeDefault;
292 auto findSetupTime =
293 suppConfig.second.find("SetupTime");
294 if (findSetupTime != suppConfig.second.end())
295 {
296 setupTime = std::visit(VariantToFloatVisitor(),
297 findSetupTime->second);
298 }
299
300 bridgeGpio = BridgeGpio(gpioName, polarity, setupTime);
301 }
302
303 break;
304 }
305 }
306
307 sensor = std::make_shared<ADCSensor>(
308 path.string(), objectServer, dbusConnection, io, sensorName,
309 std::move(sensorThresholds), scaleFactor, pollRate, readState,
310 *interfacePath, std::move(bridgeGpio));
311 sensor->setupRead();
312 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700313 });
James Feistc71c1192019-09-18 14:31:33 -0700314
315 getter->getConfiguration(
316 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700317}
318
James Feistb6c0b912019-07-09 12:21:44 -0700319int main()
James Feist6714a252018-09-10 15:26:18 -0700320{
321 boost::asio::io_service io;
322 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
323 systemBus->request_name("xyz.openbmc_project.ADCSensor");
324 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800325 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700326 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700327 auto sensorsChanged =
328 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700329
330 io.post([&]() {
Matt Spinler6ad74d92022-03-01 10:56:46 -0600331 createSensors(io, objectServer, sensors, systemBus, nullptr,
332 UpdateType::init);
James Feist6714a252018-09-10 15:26:18 -0700333 });
334
335 boost::asio::deadline_timer filterTimer(io);
336 std::function<void(sdbusplus::message::message&)> eventHandler =
337 [&](sdbusplus::message::message& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700338 if (message.is_method_error())
339 {
340 std::cerr << "callback method error\n";
341 return;
342 }
343 sensorsChanged->insert(message.get_path());
344 // this implicitly cancels the timer
345 filterTimer.expires_from_now(boost::posix_time::seconds(1));
346
347 filterTimer.async_wait([&](const boost::system::error_code& ec) {
348 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700349 {
Ed Tanousbb679322022-05-16 16:10:00 -0700350 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700351 return;
352 }
Ed Tanousbb679322022-05-16 16:10:00 -0700353 if (ec)
354 {
355 std::cerr << "timer error\n";
356 return;
357 }
358 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
359 UpdateType::init);
360 });
361 };
James Feist6714a252018-09-10 15:26:18 -0700362
James Feistb83bb2b2019-05-31 12:31:23 -0700363 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
364 [&](sdbusplus::message::message& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700365 std::string path = message.get_path();
366 boost::to_lower(path);
James Feistb83bb2b2019-05-31 12:31:23 -0700367
Ed Tanousbb679322022-05-16 16:10:00 -0700368 if (path.rfind("cpu") == std::string::npos)
369 {
370 return; // not interested
371 }
372 size_t index = 0;
373 try
374 {
375 index = std::stoi(path.substr(path.size() - 1));
376 }
377 catch (const std::invalid_argument&)
378 {
379 std::cerr << "Found invalid path " << path << "\n";
380 return;
381 }
382
383 std::string objectName;
384 boost::container::flat_map<std::string, std::variant<bool>> values;
385 message.read(objectName, values);
386 auto findPresence = values.find("Present");
387 if (findPresence != values.end())
388 {
389 cpuPresence[index] = std::get<bool>(findPresence->second);
390 }
391
392 // this implicitly cancels the timer
393 filterTimer.expires_from_now(boost::posix_time::seconds(1));
394
395 filterTimer.async_wait([&](const boost::system::error_code& ec) {
396 if (ec == boost::asio::error::operation_aborted)
James Feistb83bb2b2019-05-31 12:31:23 -0700397 {
Ed Tanousbb679322022-05-16 16:10:00 -0700398 /* we were canceled*/
James Feistb83bb2b2019-05-31 12:31:23 -0700399 return;
400 }
Ed Tanousbb679322022-05-16 16:10:00 -0700401 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700402 {
Ed Tanousbb679322022-05-16 16:10:00 -0700403 std::cerr << "timer error\n";
404 return;
James Feistb83bb2b2019-05-31 12:31:23 -0700405 }
Ed Tanousbb679322022-05-16 16:10:00 -0700406 createSensors(io, objectServer, sensors, systemBus, nullptr,
407 UpdateType::cpuPresenceChange);
408 });
409 };
James Feistb83bb2b2019-05-31 12:31:23 -0700410
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700411 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700412 {
413 auto match = std::make_unique<sdbusplus::bus::match::match>(
414 static_cast<sdbusplus::bus::bus&>(*systemBus),
415 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700416 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700417 eventHandler);
418 matches.emplace_back(std::move(match));
419 }
James Feistb83bb2b2019-05-31 12:31:23 -0700420 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
421 static_cast<sdbusplus::bus::bus&>(*systemBus),
422 "type='signal',member='PropertiesChanged',path_namespace='" +
423 std::string(cpuInventoryPath) +
424 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
425 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700426
Bruce Lee1263c3d2021-06-04 15:16:33 +0800427 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700428 io.run();
429}