blob: d62c9a68b4902819b6bf70fef2cff0985c94bf2f [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
Zev Weiss054aad82022-08-18 01:37:34 -070042static constexpr auto sensorTypes{std::to_array<const char*>({"ADC"})};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070043static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070044
James Feistb83bb2b2019-05-31 12:31:23 -070045static boost::container::flat_map<size_t, bool> cpuPresence;
46
Matt Spinler6ad74d92022-03-01 10:56:46 -060047enum class UpdateType
48{
49 init,
50 cpuPresenceChange
51};
52
James Feist08aec6f2019-01-30 16:17:04 -080053// filter out adc from any other voltage sensor
54bool isAdc(const fs::path& parentPath)
55{
56 fs::path namePath = parentPath / "name";
57
58 std::ifstream nameFile(namePath);
59 if (!nameFile.good())
60 {
61 std::cerr << "Failure reading " << namePath.string() << "\n";
62 return false;
63 }
64
65 std::string name;
66 std::getline(nameFile, name);
67
68 return name == "iio_hwmon";
69}
70
James Feist6714a252018-09-10 15:26:18 -070071void createSensors(
72 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080073 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070074 sensors,
75 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070076 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Spinler6ad74d92022-03-01 10:56:46 -060077 sensorsChanged,
78 UpdateType updateType)
James Feist6714a252018-09-10 15:26:18 -070079{
James Feistc71c1192019-09-18 14:31:33 -070080 auto getter = std::make_shared<GetSensorConfiguration>(
81 dbusConnection,
Matt Spinler6ad74d92022-03-01 10:56:46 -060082 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
83 updateType](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -070084 bool firstScan = sensorsChanged == nullptr;
85 std::vector<fs::path> paths;
86 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
87 {
88 std::cerr << "No adc sensors in system\n";
89 return;
90 }
91
92 // iterate through all found adc sensors, and try to match them with
93 // configuration
94 for (auto& path : paths)
95 {
96 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080097 {
Ed Tanousbb679322022-05-16 16:10:00 -070098 continue;
James Feistc71c1192019-09-18 14:31:33 -070099 }
Ed Tanousbb679322022-05-16 16:10:00 -0700100 std::smatch match;
101 std::string pathStr = path.string();
James Feistc71c1192019-09-18 14:31:33 -0700102
Ed Tanousbb679322022-05-16 16:10:00 -0700103 std::regex_search(pathStr, match, inputRegex);
104 std::string indexStr = *(match.begin() + 1);
105
106 auto directory = path.parent_path();
107 // convert to 0 based
108 size_t index = std::stoul(indexStr) - 1;
109
110 const SensorData* sensorData = nullptr;
111 const std::string* interfacePath = nullptr;
Zev Weissafd15042022-07-18 12:28:40 -0700112 const std::pair<std::string, SensorBaseConfigMap>*
Ed Tanousbb679322022-05-16 16:10:00 -0700113 baseConfiguration = nullptr;
Zev Weiss9148f382022-08-12 18:21:01 -0700114 for (const auto& [path, cfgData] : sensorConfigurations)
James Feistc71c1192019-09-18 14:31:33 -0700115 {
Ed Tanousbb679322022-05-16 16:10:00 -0700116 // clear it out each loop
117 baseConfiguration = nullptr;
118
119 // find base configuration
120 for (const char* type : sensorTypes)
James Feist08aec6f2019-01-30 16:17:04 -0800121 {
Zev Weiss054aad82022-08-18 01:37:34 -0700122 auto sensorBase = cfgData.find(configInterfaceName(type));
Zev Weiss9148f382022-08-12 18:21:01 -0700123 if (sensorBase != cfgData.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700124 {
Ed Tanousbb679322022-05-16 16:10:00 -0700125 baseConfiguration = &(*sensorBase);
James Feistc71c1192019-09-18 14:31:33 -0700126 break;
127 }
128 }
Ed Tanousbb679322022-05-16 16:10:00 -0700129 if (baseConfiguration == nullptr)
130 {
131 continue;
132 }
133 auto findIndex = baseConfiguration->second.find("Index");
134 if (findIndex == baseConfiguration->second.end())
135 {
136 std::cerr << "Base configuration missing Index"
137 << baseConfiguration->first << "\n";
138 continue;
139 }
James Feistc71c1192019-09-18 14:31:33 -0700140
Ed Tanousbb679322022-05-16 16:10:00 -0700141 unsigned int number = std::visit(VariantToUnsignedIntVisitor(),
142 findIndex->second);
143
144 if (number != index)
145 {
146 continue;
147 }
148
Zev Weiss9148f382022-08-12 18:21:01 -0700149 sensorData = &cfgData;
150 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700151 break;
James Feistc71c1192019-09-18 14:31:33 -0700152 }
Ed Tanousbb679322022-05-16 16:10:00 -0700153 if (sensorData == nullptr)
154 {
155 std::cerr << "failed to find match for " << path.string()
156 << "\n";
157 continue;
158 }
159
160 if (baseConfiguration == nullptr)
161 {
162 std::cerr << "error finding base configuration for"
163 << path.string() << "\n";
164 continue;
165 }
166
167 auto findSensorName = baseConfiguration->second.find("Name");
168 if (findSensorName == baseConfiguration->second.end())
169 {
170 std::cerr << "could not determine configuration name for "
171 << path.string() << "\n";
172 continue;
173 }
174 std::string sensorName =
175 std::get<std::string>(findSensorName->second);
176
177 // on rescans, only update sensors we were signaled by
178 auto findSensor = sensors.find(sensorName);
179 if (!firstScan && findSensor != sensors.end())
180 {
181 bool found = false;
182 for (auto it = sensorsChanged->begin();
183 it != sensorsChanged->end(); it++)
184 {
185 if (findSensor->second &&
Zev Weiss6c106d62022-08-17 20:50:00 -0700186 it->ends_with(findSensor->second->name))
Ed Tanousbb679322022-05-16 16:10:00 -0700187 {
188 sensorsChanged->erase(it);
189 findSensor->second = nullptr;
190 found = true;
191 break;
192 }
193 }
194 if (!found)
195 {
196 continue;
197 }
198 }
199
200 auto findCPU = baseConfiguration->second.find("CPURequired");
201 if (findCPU != baseConfiguration->second.end())
202 {
203 size_t index =
204 std::visit(VariantToIntVisitor(), findCPU->second);
205 auto presenceFind = cpuPresence.find(index);
206 if (presenceFind == cpuPresence.end())
207 {
208 continue; // no such cpu
209 }
210 if (!presenceFind->second)
211 {
212 continue; // cpu not installed
213 }
214 }
215 else if (updateType == UpdateType::cpuPresenceChange)
216 {
217 continue;
218 }
219
220 std::vector<thresholds::Threshold> sensorThresholds;
221 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
222 {
223 std::cerr << "error populating thresholds for " << sensorName
224 << "\n";
225 }
226
227 auto findScaleFactor =
228 baseConfiguration->second.find("ScaleFactor");
229 float scaleFactor = 1.0;
230 if (findScaleFactor != baseConfiguration->second.end())
231 {
232 scaleFactor = std::visit(VariantToFloatVisitor(),
233 findScaleFactor->second);
234 // scaleFactor is used in division
Ed Tanous2049bd22022-07-09 07:20:26 -0700235 if (scaleFactor == 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700236 {
237 scaleFactor = 1.0;
238 }
239 }
240
Zev Weiss8569bf22022-10-11 15:37:44 -0700241 float pollRate =
242 getPollRate(baseConfiguration->second, pollRateDefault);
Zev Weissa4d27682022-07-19 15:30:36 -0700243 PowerState readState = getPowerState(baseConfiguration->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700244
245 auto& sensor = sensors[sensorName];
246 sensor = nullptr;
247
248 std::optional<BridgeGpio> bridgeGpio;
Zev Weiss9148f382022-08-12 18:21:01 -0700249 for (const auto& [key, cfgMap] : *sensorData)
Ed Tanousbb679322022-05-16 16:10:00 -0700250 {
Zev Weiss9148f382022-08-12 18:21:01 -0700251 if (key.find("BridgeGpio") != std::string::npos)
Ed Tanousbb679322022-05-16 16:10:00 -0700252 {
Zev Weiss9148f382022-08-12 18:21:01 -0700253 auto findName = cfgMap.find("Name");
254 if (findName != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700255 {
256 std::string gpioName = std::visit(
257 VariantToStringVisitor(), findName->second);
258
259 int polarity = gpiod::line::ACTIVE_HIGH;
Zev Weiss9148f382022-08-12 18:21:01 -0700260 auto findPolarity = cfgMap.find("Polarity");
261 if (findPolarity != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700262 {
263 if (std::string("Low") ==
264 std::visit(VariantToStringVisitor(),
265 findPolarity->second))
266 {
267 polarity = gpiod::line::ACTIVE_LOW;
268 }
269 }
270
271 float setupTime = gpioBridgeSetupTimeDefault;
Zev Weiss9148f382022-08-12 18:21:01 -0700272 auto findSetupTime = cfgMap.find("SetupTime");
273 if (findSetupTime != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700274 {
275 setupTime = std::visit(VariantToFloatVisitor(),
276 findSetupTime->second);
277 }
278
279 bridgeGpio = BridgeGpio(gpioName, polarity, setupTime);
280 }
281
282 break;
283 }
284 }
285
286 sensor = std::make_shared<ADCSensor>(
287 path.string(), objectServer, dbusConnection, io, sensorName,
288 std::move(sensorThresholds), scaleFactor, pollRate, readState,
289 *interfacePath, std::move(bridgeGpio));
290 sensor->setupRead();
291 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700292 });
James Feistc71c1192019-09-18 14:31:33 -0700293
294 getter->getConfiguration(
295 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700296}
297
James Feistb6c0b912019-07-09 12:21:44 -0700298int main()
James Feist6714a252018-09-10 15:26:18 -0700299{
300 boost::asio::io_service io;
301 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
Ed Tanous14ed5e92022-07-12 15:50:23 -0700302 sdbusplus::asio::object_server objectServer(systemBus, true);
303 objectServer.add_manager("/xyz/openbmc_project/sensors");
304
James Feist6714a252018-09-10 15:26:18 -0700305 systemBus->request_name("xyz.openbmc_project.ADCSensor");
Yong Li1afda6b2020-04-09 19:24:13 +0800306 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist5591cf082020-07-15 16:44:54 -0700307 auto sensorsChanged =
308 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700309
310 io.post([&]() {
Matt Spinler6ad74d92022-03-01 10:56:46 -0600311 createSensors(io, objectServer, sensors, systemBus, nullptr,
312 UpdateType::init);
James Feist6714a252018-09-10 15:26:18 -0700313 });
314
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700315 boost::asio::steady_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500316 std::function<void(sdbusplus::message_t&)> eventHandler =
317 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700318 if (message.is_method_error())
319 {
320 std::cerr << "callback method error\n";
321 return;
322 }
323 sensorsChanged->insert(message.get_path());
324 // this implicitly cancels the timer
Ed Tanous9b4a20e2022-09-06 08:47:11 -0700325 filterTimer.expires_from_now(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700326
327 filterTimer.async_wait([&](const boost::system::error_code& ec) {
328 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700329 {
Ed Tanousbb679322022-05-16 16:10:00 -0700330 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700331 return;
332 }
Ed Tanousbb679322022-05-16 16:10:00 -0700333 if (ec)
334 {
335 std::cerr << "timer error\n";
336 return;
337 }
338 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
339 UpdateType::init);
340 });
341 };
James Feist6714a252018-09-10 15:26:18 -0700342
Matt Spinlerd0c21d52022-11-17 13:55:48 -0600343 boost::asio::steady_timer cpuFilterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500344 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
345 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700346 std::string path = message.get_path();
347 boost::to_lower(path);
James Feistb83bb2b2019-05-31 12:31:23 -0700348
Ed Tanousbb679322022-05-16 16:10:00 -0700349 if (path.rfind("cpu") == std::string::npos)
350 {
351 return; // not interested
352 }
353 size_t index = 0;
354 try
355 {
356 index = std::stoi(path.substr(path.size() - 1));
357 }
358 catch (const std::invalid_argument&)
359 {
360 std::cerr << "Found invalid path " << path << "\n";
361 return;
362 }
363
364 std::string objectName;
365 boost::container::flat_map<std::string, std::variant<bool>> values;
366 message.read(objectName, values);
367 auto findPresence = values.find("Present");
368 if (findPresence != values.end())
369 {
370 cpuPresence[index] = std::get<bool>(findPresence->second);
371 }
372
373 // this implicitly cancels the timer
Matt Spinlerd0c21d52022-11-17 13:55:48 -0600374 cpuFilterTimer.expires_from_now(std::chrono::seconds(1));
Ed Tanousbb679322022-05-16 16:10:00 -0700375
Matt Spinlerd0c21d52022-11-17 13:55:48 -0600376 cpuFilterTimer.async_wait([&](const boost::system::error_code& ec) {
Ed Tanousbb679322022-05-16 16:10:00 -0700377 if (ec == boost::asio::error::operation_aborted)
James Feistb83bb2b2019-05-31 12:31:23 -0700378 {
Ed Tanousbb679322022-05-16 16:10:00 -0700379 /* we were canceled*/
James Feistb83bb2b2019-05-31 12:31:23 -0700380 return;
381 }
Ed Tanousbb679322022-05-16 16:10:00 -0700382 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700383 {
Ed Tanousbb679322022-05-16 16:10:00 -0700384 std::cerr << "timer error\n";
385 return;
James Feistb83bb2b2019-05-31 12:31:23 -0700386 }
Ed Tanousbb679322022-05-16 16:10:00 -0700387 createSensors(io, objectServer, sensors, systemBus, nullptr,
388 UpdateType::cpuPresenceChange);
389 });
390 };
James Feistb83bb2b2019-05-31 12:31:23 -0700391
Zev Weiss214d9712022-08-12 12:54:31 -0700392 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
393 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Patrick Williams92f8f512022-07-22 19:26:55 -0500394 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
395 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistb83bb2b2019-05-31 12:31:23 -0700396 "type='signal',member='PropertiesChanged',path_namespace='" +
397 std::string(cpuInventoryPath) +
398 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
399 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700400
Bruce Lee1263c3d2021-06-04 15:16:33 +0800401 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700402 io.run();
403}