blob: 723ff0142d0030deab1492c59617fdc77979c870 [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>
James Feist38fb5982020-05-28 10:09:54 -070030#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070032#include <sdbusplus/bus.hpp>
James Feist38fb5982020-05-28 10:09:54 -070033#include <sdbusplus/bus/match.hpp>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070034#include <sdbusplus/message.hpp>
35#include <sdbusplus/message/native_types.hpp>
James Feist38fb5982020-05-28 10:09:54 -070036
Ed Tanouseacbfdd2024-04-04 12:00:24 -070037#include <array>
38#include <chrono>
39#include <cstddef>
James Feist24f02f22019-04-15 11:05:39 -070040#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070041#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070042#include <functional>
Ed Tanouseacbfdd2024-04-04 12:00:24 -070043#include <iostream>
Patrick Venture96e97db2019-10-31 13:44:38 -070044#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
James Feistcf3bce62019-01-08 10:07:19 -080057namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080058
Zev Weiss054aad82022-08-18 01:37:34 -070059static constexpr auto sensorTypes{std::to_array<const char*>({"ADC"})};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070060static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070061
James Feistb83bb2b2019-05-31 12:31:23 -070062static boost::container::flat_map<size_t, bool> cpuPresence;
63
Matt Spinler6ad74d92022-03-01 10:56:46 -060064enum class UpdateType
65{
66 init,
67 cpuPresenceChange
68};
69
James Feist08aec6f2019-01-30 16:17:04 -080070// filter out adc from any other voltage sensor
71bool isAdc(const fs::path& parentPath)
72{
73 fs::path namePath = parentPath / "name";
74
75 std::ifstream nameFile(namePath);
76 if (!nameFile.good())
77 {
78 std::cerr << "Failure reading " << namePath.string() << "\n";
79 return false;
80 }
81
82 std::string name;
83 std::getline(nameFile, name);
84
85 return name == "iio_hwmon";
86}
87
James Feist6714a252018-09-10 15:26:18 -070088void createSensors(
Ed Tanous1f978632023-02-28 18:16:39 -080089 boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080090 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070091 sensors,
92 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070093 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Spinler6ad74d92022-03-01 10:56:46 -060094 sensorsChanged,
95 UpdateType updateType)
James Feist6714a252018-09-10 15:26:18 -070096{
James Feistc71c1192019-09-18 14:31:33 -070097 auto getter = std::make_shared<GetSensorConfiguration>(
98 dbusConnection,
Matt Spinler6ad74d92022-03-01 10:56:46 -060099 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
100 updateType](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -0700101 bool firstScan = sensorsChanged == nullptr;
102 std::vector<fs::path> paths;
103 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
104 {
105 std::cerr << "No adc sensors in system\n";
106 return;
107 }
108
109 // iterate through all found adc sensors, and try to match them with
110 // configuration
111 for (auto& path : paths)
112 {
113 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -0800114 {
Ed Tanousbb679322022-05-16 16:10:00 -0700115 continue;
James Feistc71c1192019-09-18 14:31:33 -0700116 }
Ed Tanousbb679322022-05-16 16:10:00 -0700117 std::smatch match;
118 std::string pathStr = path.string();
James Feistc71c1192019-09-18 14:31:33 -0700119
Ed Tanousbb679322022-05-16 16:10:00 -0700120 std::regex_search(pathStr, match, inputRegex);
121 std::string indexStr = *(match.begin() + 1);
122
Ed Tanousbb679322022-05-16 16:10:00 -0700123 // convert to 0 based
124 size_t index = std::stoul(indexStr) - 1;
125
126 const SensorData* sensorData = nullptr;
127 const std::string* interfacePath = nullptr;
Zev Weissafd15042022-07-18 12:28:40 -0700128 const std::pair<std::string, SensorBaseConfigMap>*
Ed Tanousbb679322022-05-16 16:10:00 -0700129 baseConfiguration = nullptr;
Zev Weiss9148f382022-08-12 18:21:01 -0700130 for (const auto& [path, cfgData] : sensorConfigurations)
James Feistc71c1192019-09-18 14:31:33 -0700131 {
Ed Tanousbb679322022-05-16 16:10:00 -0700132 // clear it out each loop
133 baseConfiguration = nullptr;
134
135 // find base configuration
136 for (const char* type : sensorTypes)
James Feist08aec6f2019-01-30 16:17:04 -0800137 {
Zev Weiss054aad82022-08-18 01:37:34 -0700138 auto sensorBase = cfgData.find(configInterfaceName(type));
Zev Weiss9148f382022-08-12 18:21:01 -0700139 if (sensorBase != cfgData.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700140 {
Ed Tanousbb679322022-05-16 16:10:00 -0700141 baseConfiguration = &(*sensorBase);
James Feistc71c1192019-09-18 14:31:33 -0700142 break;
143 }
144 }
Ed Tanousbb679322022-05-16 16:10:00 -0700145 if (baseConfiguration == nullptr)
146 {
147 continue;
148 }
149 auto findIndex = baseConfiguration->second.find("Index");
150 if (findIndex == baseConfiguration->second.end())
151 {
152 std::cerr << "Base configuration missing Index"
153 << baseConfiguration->first << "\n";
154 continue;
155 }
James Feistc71c1192019-09-18 14:31:33 -0700156
Ed Tanousbb679322022-05-16 16:10:00 -0700157 unsigned int number = std::visit(VariantToUnsignedIntVisitor(),
158 findIndex->second);
159
160 if (number != index)
161 {
162 continue;
163 }
164
Zev Weiss9148f382022-08-12 18:21:01 -0700165 sensorData = &cfgData;
166 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700167 break;
James Feistc71c1192019-09-18 14:31:33 -0700168 }
Ed Tanousbb679322022-05-16 16:10:00 -0700169 if (sensorData == nullptr)
170 {
Jeff Lin016097c2023-09-08 10:03:38 +0800171 if constexpr (debug)
172 {
173 std::cerr << "failed to find match for " << path.string()
174 << "\n";
175 }
Ed Tanousbb679322022-05-16 16:10:00 -0700176 continue;
177 }
178
179 if (baseConfiguration == nullptr)
180 {
181 std::cerr << "error finding base configuration for"
182 << path.string() << "\n";
183 continue;
184 }
185
186 auto findSensorName = baseConfiguration->second.find("Name");
187 if (findSensorName == baseConfiguration->second.end())
188 {
189 std::cerr << "could not determine configuration name for "
190 << path.string() << "\n";
191 continue;
192 }
193 std::string sensorName =
194 std::get<std::string>(findSensorName->second);
195
196 // on rescans, only update sensors we were signaled by
197 auto findSensor = sensors.find(sensorName);
198 if (!firstScan && findSensor != sensors.end())
199 {
200 bool found = false;
201 for (auto it = sensorsChanged->begin();
202 it != sensorsChanged->end(); it++)
203 {
204 if (findSensor->second &&
Zev Weiss6c106d62022-08-17 20:50:00 -0700205 it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700206 {
207 sensorsChanged->erase(it);
208 findSensor->second = nullptr;
209 found = true;
210 break;
211 }
212 }
213 if (!found)
214 {
215 continue;
216 }
217 }
218
219 auto findCPU = baseConfiguration->second.find("CPURequired");
220 if (findCPU != baseConfiguration->second.end())
221 {
Patrick Williams779c96a2023-05-10 07:50:42 -0500222 size_t index = std::visit(VariantToIntVisitor(),
223 findCPU->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700224 auto presenceFind = cpuPresence.find(index);
225 if (presenceFind == cpuPresence.end())
226 {
227 continue; // no such cpu
228 }
229 if (!presenceFind->second)
230 {
231 continue; // cpu not installed
232 }
233 }
234 else if (updateType == UpdateType::cpuPresenceChange)
235 {
236 continue;
237 }
238
239 std::vector<thresholds::Threshold> sensorThresholds;
240 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
241 {
242 std::cerr << "error populating thresholds for " << sensorName
243 << "\n";
244 }
245
246 auto findScaleFactor =
247 baseConfiguration->second.find("ScaleFactor");
248 float scaleFactor = 1.0;
249 if (findScaleFactor != baseConfiguration->second.end())
250 {
251 scaleFactor = std::visit(VariantToFloatVisitor(),
252 findScaleFactor->second);
253 // scaleFactor is used in division
Ed Tanous2049bd22022-07-09 07:20:26 -0700254 if (scaleFactor == 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700255 {
256 scaleFactor = 1.0;
257 }
258 }
259
Patrick Williams779c96a2023-05-10 07:50:42 -0500260 float pollRate = getPollRate(baseConfiguration->second,
261 pollRateDefault);
Zev Weissa4d27682022-07-19 15:30:36 -0700262 PowerState readState = getPowerState(baseConfiguration->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700263
264 auto& sensor = sensors[sensorName];
265 sensor = nullptr;
266
267 std::optional<BridgeGpio> bridgeGpio;
Zev Weiss9148f382022-08-12 18:21:01 -0700268 for (const auto& [key, cfgMap] : *sensorData)
Ed Tanousbb679322022-05-16 16:10:00 -0700269 {
Zev Weiss9148f382022-08-12 18:21:01 -0700270 if (key.find("BridgeGpio") != std::string::npos)
Ed Tanousbb679322022-05-16 16:10:00 -0700271 {
Zev Weiss9148f382022-08-12 18:21:01 -0700272 auto findName = cfgMap.find("Name");
273 if (findName != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700274 {
275 std::string gpioName = std::visit(
276 VariantToStringVisitor(), findName->second);
277
278 int polarity = gpiod::line::ACTIVE_HIGH;
Zev Weiss9148f382022-08-12 18:21:01 -0700279 auto findPolarity = cfgMap.find("Polarity");
280 if (findPolarity != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700281 {
282 if (std::string("Low") ==
283 std::visit(VariantToStringVisitor(),
284 findPolarity->second))
285 {
286 polarity = gpiod::line::ACTIVE_LOW;
287 }
288 }
289
290 float setupTime = gpioBridgeSetupTimeDefault;
Zev Weiss9148f382022-08-12 18:21:01 -0700291 auto findSetupTime = cfgMap.find("SetupTime");
292 if (findSetupTime != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700293 {
294 setupTime = std::visit(VariantToFloatVisitor(),
295 findSetupTime->second);
296 }
297
298 bridgeGpio = BridgeGpio(gpioName, polarity, setupTime);
299 }
300
301 break;
302 }
303 }
304
305 sensor = std::make_shared<ADCSensor>(
306 path.string(), objectServer, dbusConnection, io, sensorName,
307 std::move(sensorThresholds), scaleFactor, pollRate, readState,
308 *interfacePath, std::move(bridgeGpio));
309 sensor->setupRead();
310 }
Patrick Williams597e8422023-10-20 11:19:01 -0500311 });
James Feistc71c1192019-09-18 14:31:33 -0700312
313 getter->getConfiguration(
314 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700315}
316
James Feistb6c0b912019-07-09 12:21:44 -0700317int main()
James Feist6714a252018-09-10 15:26:18 -0700318{
Ed Tanous1f978632023-02-28 18:16:39 -0800319 boost::asio::io_context io;
James Feist6714a252018-09-10 15:26:18 -0700320 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700321 sdbusplus::asio::object_server objectServer(systemBus, true);
322 objectServer.add_manager("/xyz/openbmc_project/sensors");
323
James Feist6714a252018-09-10 15:26:18 -0700324 systemBus->request_name("xyz.openbmc_project.ADCSensor");
Yong Li1afda6b2020-04-09 19:24:13 +0800325 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist5591cf082020-07-15 16:44:54 -0700326 auto sensorsChanged =
327 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700328
Ed Tanous83db50c2023-03-01 10:20:24 -0800329 boost::asio::post(io, [&]() {
Matt Spinler6ad74d92022-03-01 10:56:46 -0600330 createSensors(io, objectServer, sensors, systemBus, nullptr,
331 UpdateType::init);
James Feist6714a252018-09-10 15:26:18 -0700332 });
333
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700334 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500335 std::function<void(sdbusplus::message_t&)> eventHandler =
336 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700337 if (message.is_method_error())
338 {
339 std::cerr << "callback method error\n";
340 return;
341 }
342 sensorsChanged->insert(message.get_path());
343 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800344 filterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700345
346 filterTimer.async_wait([&](const boost::system::error_code& ec) {
347 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700348 {
Ed Tanousbb679322022-05-16 16:10:00 -0700349 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700350 return;
351 }
Ed Tanousbb679322022-05-16 16:10:00 -0700352 if (ec)
353 {
354 std::cerr << "timer error\n";
355 return;
356 }
357 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
358 UpdateType::init);
359 });
360 };
James Feist6714a252018-09-10 15:26:18 -0700361
Matt Spinlerd0c21d52022-11-17 13:55:48 -0600362 boost::asio::steady_timer cpuFilterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500363 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
364 [&](sdbusplus::message_t& 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
George Liu79f5ebc2022-11-28 09:42:50 +0800368 sdbusplus::message::object_path cpuPath(path);
369 std::string cpuName = cpuPath.filename();
370 if (!cpuName.starts_with("cpu"))
Ed Tanousbb679322022-05-16 16:10:00 -0700371 {
372 return; // not interested
373 }
374 size_t index = 0;
375 try
376 {
377 index = std::stoi(path.substr(path.size() - 1));
378 }
379 catch (const std::invalid_argument&)
380 {
381 std::cerr << "Found invalid path " << path << "\n";
382 return;
383 }
384
385 std::string objectName;
386 boost::container::flat_map<std::string, std::variant<bool>> values;
387 message.read(objectName, values);
388 auto findPresence = values.find("Present");
389 if (findPresence != values.end())
390 {
391 cpuPresence[index] = std::get<bool>(findPresence->second);
392 }
393
394 // this implicitly cancels the timer
Ed Tanous83db50c2023-03-01 10:20:24 -0800395 cpuFilterTimer.expires_after(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700396
Matt Spinlerd0c21d52022-11-17 13:55:48 -0600397 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700398 if (ec == boost::asio::error::operation_aborted)
James Feistb83bb2b2019-05-31 12:31:23 -0700399 {
Ed Tanousbb679322022-05-16 16:10:00 -0700400 /* we were canceled*/
James Feistb83bb2b2019-05-31 12:31:23 -0700401 return;
402 }
Ed Tanousbb679322022-05-16 16:10:00 -0700403 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700404 {
Ed Tanousbb679322022-05-16 16:10:00 -0700405 std::cerr << "timer error\n";
406 return;
James Feistb83bb2b2019-05-31 12:31:23 -0700407 }
Ed Tanousbb679322022-05-16 16:10:00 -0700408 createSensors(io, objectServer, sensors, systemBus, nullptr,
409 UpdateType::cpuPresenceChange);
410 });
411 };
James Feistb83bb2b2019-05-31 12:31:23 -0700412
Zev Weiss214d9712022-08-12 12:54:31 -0700413 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
414 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Patrick Williams92f8f512022-07-22 19:26:55 -0500415 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
416 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistb83bb2b2019-05-31 12:31:23 -0700417 "type='signal',member='PropertiesChanged',path_namespace='" +
418 std::string(cpuInventoryPath) +
419 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
420 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700421
Bruce Lee1263c3d2021-06-04 15:16:33 +0800422 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700423 io.run();
424}