blob: 366933045a268f5ad594679a9a5bf9d5e4b78c5b [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/replace.hpp>
22#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/bus/match.hpp>
26
James Feist24f02f22019-04-15 11:05:39 -070027#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070028#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <functional>
30#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040031#include <optional>
James Feist6714a252018-09-10 15:26:18 -070032#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <string>
34#include <variant>
35#include <vector>
James Feist6714a252018-09-10 15:26:18 -070036
Jeff Lind9cd7042020-11-20 15:49:28 +080037static constexpr float pollRateDefault = 0.5;
Zev Weissf72eb832021-06-25 05:55:08 +000038static constexpr float gpioBridgeSetupTimeDefault = 0.02;
James Feist6714a252018-09-10 15:26:18 -070039
James Feistcf3bce62019-01-08 10:07:19 -080040namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080041
Brandon Kim66558232021-11-09 16:53:08 -080042static constexpr auto sensorTypes{
43 std::to_array<const char*>({"xyz.openbmc_project.Configuration.ADC"})};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070044static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070045
James Feistb83bb2b2019-05-31 12:31:23 -070046static boost::container::flat_map<size_t, bool> cpuPresence;
47
Matt Spinler6ad74d92022-03-01 10:56:46 -060048enum class UpdateType
49{
50 init,
51 cpuPresenceChange
52};
53
James Feist08aec6f2019-01-30 16:17:04 -080054// filter out adc from any other voltage sensor
55bool isAdc(const fs::path& parentPath)
56{
57 fs::path namePath = parentPath / "name";
58
59 std::ifstream nameFile(namePath);
60 if (!nameFile.good())
61 {
62 std::cerr << "Failure reading " << namePath.string() << "\n";
63 return false;
64 }
65
66 std::string name;
67 std::getline(nameFile, name);
68
69 return name == "iio_hwmon";
70}
71
James Feist6714a252018-09-10 15:26:18 -070072void createSensors(
73 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080074 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070075 sensors,
76 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070077 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Spinler6ad74d92022-03-01 10:56:46 -060078 sensorsChanged,
79 UpdateType updateType)
James Feist6714a252018-09-10 15:26:18 -070080{
James Feistc71c1192019-09-18 14:31:33 -070081 auto getter = std::make_shared<GetSensorConfiguration>(
82 dbusConnection,
Matt Spinler6ad74d92022-03-01 10:56:46 -060083 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
84 updateType](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -070085 bool firstScan = sensorsChanged == nullptr;
86 std::vector<fs::path> paths;
87 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
88 {
89 std::cerr << "No adc sensors in system\n";
90 return;
91 }
92
93 // iterate through all found adc sensors, and try to match them with
94 // configuration
95 for (auto& path : paths)
96 {
97 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080098 {
Ed Tanousbb679322022-05-16 16:10:00 -070099 continue;
James Feistc71c1192019-09-18 14:31:33 -0700100 }
Ed Tanousbb679322022-05-16 16:10:00 -0700101 std::smatch match;
102 std::string pathStr = path.string();
James Feistc71c1192019-09-18 14:31:33 -0700103
Ed Tanousbb679322022-05-16 16:10:00 -0700104 std::regex_search(pathStr, match, inputRegex);
105 std::string indexStr = *(match.begin() + 1);
106
107 auto directory = path.parent_path();
108 // convert to 0 based
109 size_t index = std::stoul(indexStr) - 1;
110
111 const SensorData* sensorData = nullptr;
112 const std::string* interfacePath = nullptr;
Zev Weissafd15042022-07-18 12:28:40 -0700113 const std::pair<std::string, SensorBaseConfigMap>*
Ed Tanousbb679322022-05-16 16:10:00 -0700114 baseConfiguration = nullptr;
Zev Weiss9148f382022-08-12 18:21:01 -0700115 for (const auto& [path, cfgData] : sensorConfigurations)
James Feistc71c1192019-09-18 14:31:33 -0700116 {
Ed Tanousbb679322022-05-16 16:10:00 -0700117 // clear it out each loop
118 baseConfiguration = nullptr;
119
120 // find base configuration
121 for (const char* type : sensorTypes)
James Feist08aec6f2019-01-30 16:17:04 -0800122 {
Zev Weiss9148f382022-08-12 18:21:01 -0700123 auto sensorBase = cfgData.find(type);
124 if (sensorBase != cfgData.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700125 {
Ed Tanousbb679322022-05-16 16:10:00 -0700126 baseConfiguration = &(*sensorBase);
James Feistc71c1192019-09-18 14:31:33 -0700127 break;
128 }
129 }
Ed Tanousbb679322022-05-16 16:10:00 -0700130 if (baseConfiguration == nullptr)
131 {
132 continue;
133 }
134 auto findIndex = baseConfiguration->second.find("Index");
135 if (findIndex == baseConfiguration->second.end())
136 {
137 std::cerr << "Base configuration missing Index"
138 << baseConfiguration->first << "\n";
139 continue;
140 }
James Feistc71c1192019-09-18 14:31:33 -0700141
Ed Tanousbb679322022-05-16 16:10:00 -0700142 unsigned int number = std::visit(VariantToUnsignedIntVisitor(),
143 findIndex->second);
144
145 if (number != index)
146 {
147 continue;
148 }
149
Zev Weiss9148f382022-08-12 18:21:01 -0700150 sensorData = &cfgData;
151 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700152 break;
James Feistc71c1192019-09-18 14:31:33 -0700153 }
Ed Tanousbb679322022-05-16 16:10:00 -0700154 if (sensorData == nullptr)
155 {
156 std::cerr << "failed to find match for " << path.string()
157 << "\n";
158 continue;
159 }
160
161 if (baseConfiguration == nullptr)
162 {
163 std::cerr << "error finding base configuration for"
164 << path.string() << "\n";
165 continue;
166 }
167
168 auto findSensorName = baseConfiguration->second.find("Name");
169 if (findSensorName == baseConfiguration->second.end())
170 {
171 std::cerr << "could not determine configuration name for "
172 << path.string() << "\n";
173 continue;
174 }
175 std::string sensorName =
176 std::get<std::string>(findSensorName->second);
177
178 // on rescans, only update sensors we were signaled by
179 auto findSensor = sensors.find(sensorName);
180 if (!firstScan && findSensor != sensors.end())
181 {
182 bool found = false;
183 for (auto it = sensorsChanged->begin();
184 it != sensorsChanged->end(); it++)
185 {
186 if (findSensor->second &&
Zev Weiss6c106d62022-08-17 20:50:00 -0700187 it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700188 {
189 sensorsChanged->erase(it);
190 findSensor->second = nullptr;
191 found = true;
192 break;
193 }
194 }
195 if (!found)
196 {
197 continue;
198 }
199 }
200
201 auto findCPU = baseConfiguration->second.find("CPURequired");
202 if (findCPU != baseConfiguration->second.end())
203 {
204 size_t index =
205 std::visit(VariantToIntVisitor(), findCPU->second);
206 auto presenceFind = cpuPresence.find(index);
207 if (presenceFind == cpuPresence.end())
208 {
209 continue; // no such cpu
210 }
211 if (!presenceFind->second)
212 {
213 continue; // cpu not installed
214 }
215 }
216 else if (updateType == UpdateType::cpuPresenceChange)
217 {
218 continue;
219 }
220
221 std::vector<thresholds::Threshold> sensorThresholds;
222 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
223 {
224 std::cerr << "error populating thresholds for " << sensorName
225 << "\n";
226 }
227
228 auto findScaleFactor =
229 baseConfiguration->second.find("ScaleFactor");
230 float scaleFactor = 1.0;
231 if (findScaleFactor != baseConfiguration->second.end())
232 {
233 scaleFactor = std::visit(VariantToFloatVisitor(),
234 findScaleFactor->second);
235 // scaleFactor is used in division
Ed Tanous2049bd22022-07-09 07:20:26 -0700236 if (scaleFactor == 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700237 {
238 scaleFactor = 1.0;
239 }
240 }
241
242 auto findPollRate = baseConfiguration->second.find("PollRate");
243 float pollRate = pollRateDefault;
244 if (findPollRate != baseConfiguration->second.end())
245 {
246 pollRate =
247 std::visit(VariantToFloatVisitor(), findPollRate->second);
Ed Tanous2049bd22022-07-09 07:20:26 -0700248 if (pollRate <= 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700249 {
250 pollRate = pollRateDefault; // polling time too short
251 }
252 }
253
Zev Weissa4d27682022-07-19 15:30:36 -0700254 PowerState readState = getPowerState(baseConfiguration->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700255
256 auto& sensor = sensors[sensorName];
257 sensor = nullptr;
258
259 std::optional<BridgeGpio> bridgeGpio;
Zev Weiss9148f382022-08-12 18:21:01 -0700260 for (const auto& [key, cfgMap] : *sensorData)
Ed Tanousbb679322022-05-16 16:10:00 -0700261 {
Zev Weiss9148f382022-08-12 18:21:01 -0700262 if (key.find("BridgeGpio") != std::string::npos)
Ed Tanousbb679322022-05-16 16:10:00 -0700263 {
Zev Weiss9148f382022-08-12 18:21:01 -0700264 auto findName = cfgMap.find("Name");
265 if (findName != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700266 {
267 std::string gpioName = std::visit(
268 VariantToStringVisitor(), findName->second);
269
270 int polarity = gpiod::line::ACTIVE_HIGH;
Zev Weiss9148f382022-08-12 18:21:01 -0700271 auto findPolarity = cfgMap.find("Polarity");
272 if (findPolarity != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700273 {
274 if (std::string("Low") ==
275 std::visit(VariantToStringVisitor(),
276 findPolarity->second))
277 {
278 polarity = gpiod::line::ACTIVE_LOW;
279 }
280 }
281
282 float setupTime = gpioBridgeSetupTimeDefault;
Zev Weiss9148f382022-08-12 18:21:01 -0700283 auto findSetupTime = cfgMap.find("SetupTime");
284 if (findSetupTime != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700285 {
286 setupTime = std::visit(VariantToFloatVisitor(),
287 findSetupTime->second);
288 }
289
290 bridgeGpio = BridgeGpio(gpioName, polarity, setupTime);
291 }
292
293 break;
294 }
295 }
296
297 sensor = std::make_shared<ADCSensor>(
298 path.string(), objectServer, dbusConnection, io, sensorName,
299 std::move(sensorThresholds), scaleFactor, pollRate, readState,
300 *interfacePath, std::move(bridgeGpio));
301 sensor->setupRead();
302 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700303 });
James Feistc71c1192019-09-18 14:31:33 -0700304
305 getter->getConfiguration(
306 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700307}
308
James Feistb6c0b912019-07-09 12:21:44 -0700309int main()
James Feist6714a252018-09-10 15:26:18 -0700310{
311 boost::asio::io_service io;
312 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
313 systemBus->request_name("xyz.openbmc_project.ADCSensor");
314 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800315 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist5591cf082020-07-15 16:44:54 -0700316 auto sensorsChanged =
317 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700318
319 io.post([&]() {
Matt Spinler6ad74d92022-03-01 10:56:46 -0600320 createSensors(io, objectServer, sensors, systemBus, nullptr,
321 UpdateType::init);
James Feist6714a252018-09-10 15:26:18 -0700322 });
323
324 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500325 std::function<void(sdbusplus::message_t&)> eventHandler =
326 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700327 if (message.is_method_error())
328 {
329 std::cerr << "callback method error\n";
330 return;
331 }
332 sensorsChanged->insert(message.get_path());
333 // this implicitly cancels the timer
334 filterTimer.expires_from_now(boost::posix_time::seconds(1));
335
336 filterTimer.async_wait([&](const boost::system::error_code& ec) {
337 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700338 {
Ed Tanousbb679322022-05-16 16:10:00 -0700339 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700340 return;
341 }
Ed Tanousbb679322022-05-16 16:10:00 -0700342 if (ec)
343 {
344 std::cerr << "timer error\n";
345 return;
346 }
347 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
348 UpdateType::init);
349 });
350 };
James Feist6714a252018-09-10 15:26:18 -0700351
Patrick Williams92f8f512022-07-22 19:26:55 -0500352 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
353 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700354 std::string path = message.get_path();
355 boost::to_lower(path);
James Feistb83bb2b2019-05-31 12:31:23 -0700356
Ed Tanousbb679322022-05-16 16:10:00 -0700357 if (path.rfind("cpu") == std::string::npos)
358 {
359 return; // not interested
360 }
361 size_t index = 0;
362 try
363 {
364 index = std::stoi(path.substr(path.size() - 1));
365 }
366 catch (const std::invalid_argument&)
367 {
368 std::cerr << "Found invalid path " << path << "\n";
369 return;
370 }
371
372 std::string objectName;
373 boost::container::flat_map<std::string, std::variant<bool>> values;
374 message.read(objectName, values);
375 auto findPresence = values.find("Present");
376 if (findPresence != values.end())
377 {
378 cpuPresence[index] = std::get<bool>(findPresence->second);
379 }
380
381 // this implicitly cancels the timer
382 filterTimer.expires_from_now(boost::posix_time::seconds(1));
383
384 filterTimer.async_wait([&](const boost::system::error_code& ec) {
385 if (ec == boost::asio::error::operation_aborted)
James Feistb83bb2b2019-05-31 12:31:23 -0700386 {
Ed Tanousbb679322022-05-16 16:10:00 -0700387 /* we were canceled*/
James Feistb83bb2b2019-05-31 12:31:23 -0700388 return;
389 }
Ed Tanousbb679322022-05-16 16:10:00 -0700390 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700391 {
Ed Tanousbb679322022-05-16 16:10:00 -0700392 std::cerr << "timer error\n";
393 return;
James Feistb83bb2b2019-05-31 12:31:23 -0700394 }
Ed Tanousbb679322022-05-16 16:10:00 -0700395 createSensors(io, objectServer, sensors, systemBus, nullptr,
396 UpdateType::cpuPresenceChange);
397 });
398 };
James Feistb83bb2b2019-05-31 12:31:23 -0700399
Zev Weiss214d9712022-08-12 12:54:31 -0700400 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
401 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Patrick Williams92f8f512022-07-22 19:26:55 -0500402 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
403 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistb83bb2b2019-05-31 12:31:23 -0700404 "type='signal',member='PropertiesChanged',path_namespace='" +
405 std::string(cpuInventoryPath) +
406 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
407 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700408
Bruce Lee1263c3d2021-06-04 15:16:33 +0800409 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700410 io.run();
411}