blob: 7949280cc5c54333ef9153ca0583046b5e6ea714 [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"
Ed Tanouseacbfdd2024-04-04 12:00:24 -070018#include "Thresholds.hpp"
Andrew Jefferye73bd0a2023-01-25 10:39:57 +103019#include "Utils.hpp"
20#include "VariantVisitors.hpp"
21
James Feistb83bb2b2019-05-31 12:31:23 -070022#include <boost/algorithm/string/case_conv.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070023#include <boost/asio/error.hpp>
24#include <boost/asio/io_context.hpp>
25#include <boost/asio/post.hpp>
26#include <boost/asio/steady_timer.hpp>
27#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070028#include <boost/container/flat_set.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070029#include <gpiod.hpp>
George Liud4bc41f2025-02-20 09:21:38 +080030#include <phosphor-logging/lg2.hpp>
James Feist38fb5982020-05-28 10:09:54 -070031#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070033#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070034#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070035#include <sdbusplus/message.hpp>
36#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070037
Ed Tanouseacbfdd2024-04-04 12:00:24 -070038#include <array>
39#include <chrono>
40#include <cstddef>
James Feist24f02f22019-04-15 11:05:39 -070041#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070042#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070043#include <functional>
44#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040045#include <optional>
James Feist6714a252018-09-10 15:26:18 -070046#include <regex>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070047#include <stdexcept>
Patrick Venture96e97db2019-10-31 13:44:38 -070048#include <string>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070049#include <utility>
Patrick Venture96e97db2019-10-31 13:44:38 -070050#include <variant>
51#include <vector>
James Feist6714a252018-09-10 15:26:18 -070052
Jeff Lin016097c2023-09-08 10:03:38 +080053static constexpr bool debug = false;
Jeff Lind9cd7042020-11-20 15:49:28 +080054static constexpr float pollRateDefault = 0.5;
Zev Weissf72eb832021-06-25 05:55:08 +000055static constexpr float gpioBridgeSetupTimeDefault = 0.02;
James Feist6714a252018-09-10 15:26:18 -070056
Zev Weiss054aad82022-08-18 01:37:34 -070057static constexpr auto sensorTypes{std::to_array<const char*>({"ADC"})};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070058static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070059
James Feistb83bb2b2019-05-31 12:31:23 -070060static boost::container::flat_map<size_t, bool> cpuPresence;
61
Matt Spinler6ad74d92022-03-01 10:56:46 -060062enum class UpdateType
63{
64 init,
65 cpuPresenceChange
66};
67
James Feist08aec6f2019-01-30 16:17:04 -080068// filter out adc from any other voltage sensor
Ed Tanous2e466962025-01-30 10:59:49 -080069bool isAdc(const std::filesystem::path& parentPath)
James Feist08aec6f2019-01-30 16:17:04 -080070{
Ed Tanous2e466962025-01-30 10:59:49 -080071 std::filesystem::path namePath = parentPath / "name";
James Feist08aec6f2019-01-30 16:17:04 -080072
73 std::ifstream nameFile(namePath);
74 if (!nameFile.good())
75 {
George Liud4bc41f2025-02-20 09:21:38 +080076 lg2::error("Failure reading '{PATH}'", "PATH", namePath.string());
James Feist08aec6f2019-01-30 16:17:04 -080077 return false;
78 }
79
80 std::string name;
81 std::getline(nameFile, name);
82
83 return name == "iio_hwmon";
84}
85
James Feist6714a252018-09-10 15:26:18 -070086void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -080087 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080088 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070089 sensors,
90 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070091 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Spinler6ad74d92022-03-01 10:56:46 -060092 sensorsChanged,
93 UpdateType updateType)
James Feist6714a252018-09-10 15:26:18 -070094{
James Feistc71c1192019-09-18 14:31:33 -070095 auto getter = std::make_shared<GetSensorConfiguration>(
96 dbusConnection,
Matt Spinler6ad74d92022-03-01 10:56:46 -060097 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
98 updateType](const ManagedObjectType& sensorConfigurations) {
Patrick Williams2aaf7172024-08-16 15:20:40 -040099 bool firstScan = sensorsChanged == nullptr;
Ed Tanous2e466962025-01-30 10:59:49 -0800100 std::vector<std::filesystem::path> paths;
101 if (!findFiles(std::filesystem::path("/sys/class/hwmon"),
102 R"(in\d+_input)", paths))
James Feist08aec6f2019-01-30 16:17:04 -0800103 {
George Liud4bc41f2025-02-20 09:21:38 +0800104 lg2::error("No adc sensors in system");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400105 return;
James Feistc71c1192019-09-18 14:31:33 -0700106 }
107
Patrick Williams2aaf7172024-08-16 15:20:40 -0400108 // iterate through all found adc sensors, and try to match them with
109 // configuration
110 for (auto& path : paths)
James Feistc71c1192019-09-18 14:31:33 -0700111 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400112 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -0800113 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400114 continue;
115 }
116 std::smatch match;
117 std::string pathStr = path.string();
118
119 std::regex_search(pathStr, match, inputRegex);
120 std::string indexStr = *(match.begin() + 1);
121
122 // convert to 0 based
123 size_t index = std::stoul(indexStr) - 1;
124
125 const SensorData* sensorData = nullptr;
126 const std::string* interfacePath = nullptr;
127 const std::pair<std::string, SensorBaseConfigMap>*
128 baseConfiguration = nullptr;
129 for (const auto& [path, cfgData] : sensorConfigurations)
130 {
131 // clear it out each loop
132 baseConfiguration = nullptr;
133
134 // find base configuration
135 for (const char* type : sensorTypes)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700136 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400137 auto sensorBase =
138 cfgData.find(configInterfaceName(type));
139 if (sensorBase != cfgData.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700140 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400141 baseConfiguration = &(*sensorBase);
142 break;
Ed Tanousbb679322022-05-16 16:10:00 -0700143 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400144 }
145 if (baseConfiguration == nullptr)
146 {
147 continue;
148 }
149 auto findIndex = baseConfiguration->second.find("Index");
150 if (findIndex == baseConfiguration->second.end())
151 {
George Liud4bc41f2025-02-20 09:21:38 +0800152 lg2::error(
153 "Base configuration missing Index: '{INTERFACE}'",
154 "INTERFACE", baseConfiguration->first);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400155 continue;
Ed Tanousbb679322022-05-16 16:10:00 -0700156 }
157
Patrick Williams2aaf7172024-08-16 15:20:40 -0400158 unsigned int number = std::visit(
159 VariantToUnsignedIntVisitor(), findIndex->second);
160
161 if (number != index)
162 {
163 continue;
164 }
165
166 sensorData = &cfgData;
167 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700168 break;
169 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400170 if (sensorData == nullptr)
171 {
172 if constexpr (debug)
173 {
George Liud4bc41f2025-02-20 09:21:38 +0800174 lg2::error("failed to find match for '{PATH}'", "PATH",
175 path.string());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400176 }
177 continue;
178 }
Ed Tanousbb679322022-05-16 16:10:00 -0700179
Patrick Williams2aaf7172024-08-16 15:20:40 -0400180 if (baseConfiguration == nullptr)
181 {
George Liud4bc41f2025-02-20 09:21:38 +0800182 lg2::error("error finding base configuration for '{PATH}'",
183 "PATH", path.string());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400184 continue;
185 }
186
187 auto findSensorName = baseConfiguration->second.find("Name");
188 if (findSensorName == baseConfiguration->second.end())
189 {
George Liud4bc41f2025-02-20 09:21:38 +0800190 lg2::error(
191 "could not determine configuration name for '{PATH}'",
192 "PATH", path.string());
Patrick Williams2aaf7172024-08-16 15:20:40 -0400193 continue;
194 }
195 std::string sensorName =
196 std::get<std::string>(findSensorName->second);
197
198 // on rescans, only update sensors we were signaled by
199 auto findSensor = sensors.find(sensorName);
200 if (!firstScan && findSensor != sensors.end())
201 {
202 bool found = false;
203 for (auto it = sensorsChanged->begin();
204 it != sensorsChanged->end(); it++)
205 {
206 if (findSensor->second &&
207 it->ends_with(findSensor->second->name))
208 {
209 sensorsChanged->erase(it);
210 findSensor->second = nullptr;
211 found = true;
212 break;
213 }
214 }
215 if (!found)
216 {
217 continue;
218 }
219 }
220
221 auto findCPU = baseConfiguration->second.find("CPURequired");
222 if (findCPU != baseConfiguration->second.end())
223 {
224 size_t index =
225 std::visit(VariantToIntVisitor(), findCPU->second);
226 auto presenceFind = cpuPresence.find(index);
227 if (presenceFind == cpuPresence.end())
228 {
229 continue; // no such cpu
230 }
231 if (!presenceFind->second)
232 {
233 continue; // cpu not installed
234 }
235 }
236 else if (updateType == UpdateType::cpuPresenceChange)
237 {
238 continue;
239 }
240
241 std::vector<thresholds::Threshold> sensorThresholds;
242 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
243 {
George Liud4bc41f2025-02-20 09:21:38 +0800244 lg2::error("error populating thresholds for '{NAME}'",
245 "NAME", sensorName);
Patrick Williams2aaf7172024-08-16 15:20:40 -0400246 }
247
248 auto findScaleFactor =
249 baseConfiguration->second.find("ScaleFactor");
250 float scaleFactor = 1.0;
251 if (findScaleFactor != baseConfiguration->second.end())
252 {
253 scaleFactor = std::visit(VariantToFloatVisitor(),
254 findScaleFactor->second);
255 // scaleFactor is used in division
256 if (scaleFactor == 0.0F)
257 {
258 scaleFactor = 1.0;
259 }
260 }
261
262 float pollRate =
263 getPollRate(baseConfiguration->second, pollRateDefault);
264 PowerState readState = getPowerState(baseConfiguration->second);
265
266 auto& sensor = sensors[sensorName];
267 sensor = nullptr;
268
269 std::optional<BridgeGpio> bridgeGpio;
270 for (const auto& [key, cfgMap] : *sensorData)
271 {
272 if (key.find("BridgeGpio") != std::string::npos)
273 {
274 auto findName = cfgMap.find("Name");
275 if (findName != cfgMap.end())
276 {
277 std::string gpioName = std::visit(
278 VariantToStringVisitor(), findName->second);
279
280 int polarity = gpiod::line::ACTIVE_HIGH;
281 auto findPolarity = cfgMap.find("Polarity");
282 if (findPolarity != cfgMap.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 = cfgMap.find("SetupTime");
294 if (findSetupTime != cfgMap.end())
295 {
296 setupTime = std::visit(VariantToFloatVisitor(),
297 findSetupTime->second);
298 }
299
300 bridgeGpio =
301 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,
311 readState, *interfacePath, std::move(bridgeGpio));
312 sensor->setupRead();
313 }
314 });
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{
Ed Tanous1f978632023-02-28 18:16:39 -0800322 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700323 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700324 sdbusplus::asio::object_server objectServer(systemBus, true);
325 objectServer.add_manager("/xyz/openbmc_project/sensors");
326
James Feist6714a252018-09-10 15:26:18 -0700327 systemBus->request_name("xyz.openbmc_project.ADCSensor");
Yong Li1afda6b2020-04-09 19:24:13 +0800328 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist5591cf082020-07-15 16:44:54 -0700329 auto sensorsChanged =
330 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700331
Ed Tanous83db50c2023-03-01 10:20:24 -0800332 boost::asio::post(io, [&]() {
Matt Spinler6ad74d92022-03-01 10:56:46 -0600333 createSensors(io, objectServer, sensors, systemBus, nullptr,
334 UpdateType::init);
James Feist6714a252018-09-10 15:26:18 -0700335 });
336
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700337 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500338 std::function<void(sdbusplus::message_t&)> eventHandler =
339 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400340 if (message.is_method_error())
341 {
George Liud4bc41f2025-02-20 09:21:38 +0800342 lg2::error("callback method error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400343 return;
344 }
345 sensorsChanged->insert(message.get_path());
346 // this implicitly cancels the timer
347 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700348
Patrick Williams2aaf7172024-08-16 15:20:40 -0400349 filterTimer.async_wait([&](const boost::system::error_code& ec) {
350 if (ec == boost::asio::error::operation_aborted)
351 {
352 /* we were canceled*/
353 return;
354 }
355 if (ec)
356 {
George Liud4bc41f2025-02-20 09:21:38 +0800357 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400358 return;
359 }
360 createSensors(io, objectServer, sensors, systemBus,
361 sensorsChanged, UpdateType::init);
362 });
363 };
James Feist6714a252018-09-10 15:26:18 -0700364
Matt Spinlerd0c21d52022-11-17 13:55:48 -0600365 boost::asio::steady_timer cpuFilterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500366 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
367 [&](sdbusplus::message_t& message) {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400368 std::string path = message.get_path();
369 boost::to_lower(path);
James Feistb83bb2b2019-05-31 12:31:23 -0700370
Patrick Williams2aaf7172024-08-16 15:20:40 -0400371 sdbusplus::message::object_path cpuPath(path);
372 std::string cpuName = cpuPath.filename();
373 if (!cpuName.starts_with("cpu"))
James Feistb83bb2b2019-05-31 12:31:23 -0700374 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400375 return; // not interested
376 }
377 size_t index = 0;
378 try
379 {
380 index = std::stoi(path.substr(path.size() - 1));
381 }
382 catch (const std::invalid_argument&)
383 {
George Liud4bc41f2025-02-20 09:21:38 +0800384 lg2::error("Found invalid path: '{PATH}'", "PATH", path);
James Feistb83bb2b2019-05-31 12:31:23 -0700385 return;
386 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400387
388 std::string objectName;
389 boost::container::flat_map<std::string, std::variant<bool>> values;
390 message.read(objectName, values);
391 auto findPresence = values.find("Present");
392 if (findPresence != values.end())
James Feistb83bb2b2019-05-31 12:31:23 -0700393 {
Patrick Williams2aaf7172024-08-16 15:20:40 -0400394 cpuPresence[index] = std::get<bool>(findPresence->second);
James Feistb83bb2b2019-05-31 12:31:23 -0700395 }
Patrick Williams2aaf7172024-08-16 15:20:40 -0400396
397 // this implicitly cancels the timer
398 cpuFilterTimer.expires_after(std::chrono::seconds(1));
399
400 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
401 if (ec == boost::asio::error::operation_aborted)
402 {
403 /* we were canceled*/
404 return;
405 }
406 if (ec)
407 {
George Liud4bc41f2025-02-20 09:21:38 +0800408 lg2::error("timer error");
Patrick Williams2aaf7172024-08-16 15:20:40 -0400409 return;
410 }
411 createSensors(io, objectServer, sensors, systemBus, nullptr,
412 UpdateType::cpuPresenceChange);
413 });
414 };
James Feistb83bb2b2019-05-31 12:31:23 -0700415
Zev Weiss214d9712022-08-12 12:54:31 -0700416 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
417 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Patrick Williams92f8f512022-07-22 19:26:55 -0500418 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
419 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistb83bb2b2019-05-31 12:31:23 -0700420 "type='signal',member='PropertiesChanged',path_namespace='" +
421 std::string(cpuInventoryPath) +
422 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
423 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700424
Bruce Lee1263c3d2021-06-04 15:16:33 +0800425 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700426 io.run();
427}