blob: ccdca8fb45a13bff2b738d1ccff1377b47385296 [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
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103017#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/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 Lin016097c2023-09-08 10:03:38 +080038static constexpr bool debug = false;
Jeff Lind9cd7042020-11-20 15:49:28 +080039static constexpr float pollRateDefault = 0.5;
Zev Weissf72eb832021-06-25 05:55:08 +000040static constexpr float gpioBridgeSetupTimeDefault = 0.02;
James Feist6714a252018-09-10 15:26:18 -070041
James Feistcf3bce62019-01-08 10:07:19 -080042namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080043
Zev Weiss054aad82022-08-18 01:37:34 -070044static constexpr auto sensorTypes{std::to_array<const char*>({"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(
Ed Tanous1f978632023-02-28 18:16:39 -080074 boost::asio::io_context& 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;
Zev Weiss9148f382022-08-12 18:21:01 -0700116 for (const auto& [path, cfgData] : sensorConfigurations)
James Feistc71c1192019-09-18 14:31:33 -0700117 {
Ed Tanousbb679322022-05-16 16:10:00 -0700118 // clear it out each loop
119 baseConfiguration = nullptr;
120
121 // find base configuration
122 for (const char* type : sensorTypes)
James Feist08aec6f2019-01-30 16:17:04 -0800123 {
Zev Weiss054aad82022-08-18 01:37:34 -0700124 auto sensorBase = cfgData.find(configInterfaceName(type));
Zev Weiss9148f382022-08-12 18:21:01 -0700125 if (sensorBase != cfgData.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700126 {
Ed Tanousbb679322022-05-16 16:10:00 -0700127 baseConfiguration = &(*sensorBase);
James Feistc71c1192019-09-18 14:31:33 -0700128 break;
129 }
130 }
Ed Tanousbb679322022-05-16 16:10:00 -0700131 if (baseConfiguration == nullptr)
132 {
133 continue;
134 }
135 auto findIndex = baseConfiguration->second.find("Index");
136 if (findIndex == baseConfiguration->second.end())
137 {
138 std::cerr << "Base configuration missing Index"
139 << baseConfiguration->first << "\n";
140 continue;
141 }
James Feistc71c1192019-09-18 14:31:33 -0700142
Ed Tanousbb679322022-05-16 16:10:00 -0700143 unsigned int number = std::visit(VariantToUnsignedIntVisitor(),
144 findIndex->second);
145
146 if (number != index)
147 {
148 continue;
149 }
150
Zev Weiss9148f382022-08-12 18:21:01 -0700151 sensorData = &cfgData;
152 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700153 break;
James Feistc71c1192019-09-18 14:31:33 -0700154 }
Ed Tanousbb679322022-05-16 16:10:00 -0700155 if (sensorData == nullptr)
156 {
Jeff Lin016097c2023-09-08 10:03:38 +0800157 if constexpr (debug)
158 {
159 std::cerr << "failed to find match for " << path.string()
160 << "\n";
161 }
Ed Tanousbb679322022-05-16 16:10:00 -0700162 continue;
163 }
164
165 if (baseConfiguration == nullptr)
166 {
167 std::cerr << "error finding base configuration for"
168 << path.string() << "\n";
169 continue;
170 }
171
172 auto findSensorName = baseConfiguration->second.find("Name");
173 if (findSensorName == baseConfiguration->second.end())
174 {
175 std::cerr << "could not determine configuration name for "
176 << path.string() << "\n";
177 continue;
178 }
179 std::string sensorName =
180 std::get<std::string>(findSensorName->second);
181
182 // on rescans, only update sensors we were signaled by
183 auto findSensor = sensors.find(sensorName);
184 if (!firstScan && findSensor != sensors.end())
185 {
186 bool found = false;
187 for (auto it = sensorsChanged->begin();
188 it != sensorsChanged->end(); it++)
189 {
190 if (findSensor->second &&
Zev Weiss6c106d62022-08-17 20:50:00 -0700191 it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700192 {
193 sensorsChanged->erase(it);
194 findSensor->second = nullptr;
195 found = true;
196 break;
197 }
198 }
199 if (!found)
200 {
201 continue;
202 }
203 }
204
205 auto findCPU = baseConfiguration->second.find("CPURequired");
206 if (findCPU != baseConfiguration->second.end())
207 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500208 size_t index = std::visit(VariantToIntVisitor(),
209 findCPU->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700210 auto presenceFind = cpuPresence.find(index);
211 if (presenceFind == cpuPresence.end())
212 {
213 continue; // no such cpu
214 }
215 if (!presenceFind->second)
216 {
217 continue; // cpu not installed
218 }
219 }
220 else if (updateType == UpdateType::cpuPresenceChange)
221 {
222 continue;
223 }
224
225 std::vector<thresholds::Threshold> sensorThresholds;
226 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
227 {
228 std::cerr << "error populating thresholds for " << sensorName
229 << "\n";
230 }
231
232 auto findScaleFactor =
233 baseConfiguration->second.find("ScaleFactor");
234 float scaleFactor = 1.0;
235 if (findScaleFactor != baseConfiguration->second.end())
236 {
237 scaleFactor = std::visit(VariantToFloatVisitor(),
238 findScaleFactor->second);
239 // scaleFactor is used in division
Ed Tanous2049bd22022-07-09 07:20:26 -0700240 if (scaleFactor == 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700241 {
242 scaleFactor = 1.0;
243 }
244 }
245
Patrick Williams779c96a2023-05-10 07:50:42 -0500246 float pollRate = getPollRate(baseConfiguration->second,
247 pollRateDefault);
Zev Weissa4d27682022-07-19 15:30:36 -0700248 PowerState readState = getPowerState(baseConfiguration->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700249
250 auto& sensor = sensors[sensorName];
251 sensor = nullptr;
252
253 std::optional<BridgeGpio> bridgeGpio;
Zev Weiss9148f382022-08-12 18:21:01 -0700254 for (const auto& [key, cfgMap] : *sensorData)
Ed Tanousbb679322022-05-16 16:10:00 -0700255 {
Zev Weiss9148f382022-08-12 18:21:01 -0700256 if (key.find("BridgeGpio") != std::string::npos)
Ed Tanousbb679322022-05-16 16:10:00 -0700257 {
Zev Weiss9148f382022-08-12 18:21:01 -0700258 auto findName = cfgMap.find("Name");
259 if (findName != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700260 {
261 std::string gpioName = std::visit(
262 VariantToStringVisitor(), findName->second);
263
264 int polarity = gpiod::line::ACTIVE_HIGH;
Zev Weiss9148f382022-08-12 18:21:01 -0700265 auto findPolarity = cfgMap.find("Polarity");
266 if (findPolarity != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700267 {
268 if (std::string("Low") ==
269 std::visit(VariantToStringVisitor(),
270 findPolarity->second))
271 {
272 polarity = gpiod::line::ACTIVE_LOW;
273 }
274 }
275
276 float setupTime = gpioBridgeSetupTimeDefault;
Zev Weiss9148f382022-08-12 18:21:01 -0700277 auto findSetupTime = cfgMap.find("SetupTime");
278 if (findSetupTime != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700279 {
280 setupTime = std::visit(VariantToFloatVisitor(),
281 findSetupTime->second);
282 }
283
284 bridgeGpio = BridgeGpio(gpioName, polarity, setupTime);
285 }
286
287 break;
288 }
289 }
290
291 sensor = std::make_shared<ADCSensor>(
292 path.string(), objectServer, dbusConnection, io, sensorName,
293 std::move(sensorThresholds), scaleFactor, pollRate, readState,
294 *interfacePath, std::move(bridgeGpio));
295 sensor->setupRead();
296 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700297 });
James Feistc71c1192019-09-18 14:31:33 -0700298
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{
Ed Tanous1f978632023-02-28 18:16:39 -0800305 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700306 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700307 sdbusplus::asio::object_server objectServer(systemBus, true);
308 objectServer.add_manager("/xyz/openbmc_project/sensors");
309
James Feist6714a252018-09-10 15:26:18 -0700310 systemBus->request_name("xyz.openbmc_project.ADCSensor");
Yong Li1afda6b2020-04-09 19:24:13 +0800311 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist5591cf082020-07-15 16:44:54 -0700312 auto sensorsChanged =
313 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700314
Ed Tanous83db50c2023-03-01 10:20:24 -0800315 boost::asio::post(io, [&]() {
Matt Spinler6ad74d92022-03-01 10:56:46 -0600316 createSensors(io, objectServer, sensors, systemBus, nullptr,
317 UpdateType::init);
James Feist6714a252018-09-10 15:26:18 -0700318 });
319
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700320 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500321 std::function<void(sdbusplus::message_t&)> eventHandler =
322 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700323 if (message.is_method_error())
324 {
325 std::cerr << "callback method error\n";
326 return;
327 }
328 sensorsChanged->insert(message.get_path());
329 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800330 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700331
332 filterTimer.async_wait([&](const boost::system::error_code& ec) {
333 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700334 {
Ed Tanousbb679322022-05-16 16:10:00 -0700335 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700336 return;
337 }
Ed Tanousbb679322022-05-16 16:10:00 -0700338 if (ec)
339 {
340 std::cerr << "timer error\n";
341 return;
342 }
343 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
344 UpdateType::init);
345 });
346 };
James Feist6714a252018-09-10 15:26:18 -0700347
Matt Spinlerd0c21d52022-11-17 13:55:48 -0600348 boost::asio::steady_timer cpuFilterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500349 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
350 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700351 std::string path = message.get_path();
352 boost::to_lower(path);
James Feistb83bb2b2019-05-31 12:31:23 -0700353
George Liu79f5ebc2022-11-28 09:42:50 +0800354 sdbusplus::message::object_path cpuPath(path);
355 std::string cpuName = cpuPath.filename();
356 if (!cpuName.starts_with("cpu"))
Ed Tanousbb679322022-05-16 16:10:00 -0700357 {
358 return; // not interested
359 }
360 size_t index = 0;
361 try
362 {
363 index = std::stoi(path.substr(path.size() - 1));
364 }
365 catch (const std::invalid_argument&)
366 {
367 std::cerr << "Found invalid path " << path << "\n";
368 return;
369 }
370
371 std::string objectName;
372 boost::container::flat_map<std::string, std::variant<bool>> values;
373 message.read(objectName, values);
374 auto findPresence = values.find("Present");
375 if (findPresence != values.end())
376 {
377 cpuPresence[index] = std::get<bool>(findPresence->second);
378 }
379
380 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800381 cpuFilterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700382
Matt Spinlerd0c21d52022-11-17 13:55:48 -0600383 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700384 if (ec == boost::asio::error::operation_aborted)
James Feistb83bb2b2019-05-31 12:31:23 -0700385 {
Ed Tanousbb679322022-05-16 16:10:00 -0700386 /* we were canceled*/
James Feistb83bb2b2019-05-31 12:31:23 -0700387 return;
388 }
Ed Tanousbb679322022-05-16 16:10:00 -0700389 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700390 {
Ed Tanousbb679322022-05-16 16:10:00 -0700391 std::cerr << "timer error\n";
392 return;
James Feistb83bb2b2019-05-31 12:31:23 -0700393 }
Ed Tanousbb679322022-05-16 16:10:00 -0700394 createSensors(io, objectServer, sensors, systemBus, nullptr,
395 UpdateType::cpuPresenceChange);
396 });
397 };
James Feistb83bb2b2019-05-31 12:31:23 -0700398
Zev Weiss214d9712022-08-12 12:54:31 -0700399 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
400 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Patrick Williams92f8f512022-07-22 19:26:55 -0500401 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
402 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistb83bb2b2019-05-31 12:31:23 -0700403 "type='signal',member='PropertiesChanged',path_namespace='" +
404 std::string(cpuInventoryPath) +
405 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
406 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700407
Bruce Lee1263c3d2021-06-04 15:16:33 +0800408 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700409 io.run();
410}