blob: 4f35a7ff435d8a954f4b9fa14cb007eba834c009 [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/predicate.hpp>
22#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
Ed Tanous8a57ec02020-10-09 12:46:52 -070038static 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
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070044static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070045 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070046static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070047
James Feistb83bb2b2019-05-31 12:31:23 -070048static boost::container::flat_map<size_t, bool> cpuPresence;
49
James Feist08aec6f2019-01-30 16:17:04 -080050// filter out adc from any other voltage sensor
51bool isAdc(const fs::path& parentPath)
52{
53 fs::path namePath = parentPath / "name";
54
55 std::ifstream nameFile(namePath);
56 if (!nameFile.good())
57 {
58 std::cerr << "Failure reading " << namePath.string() << "\n";
59 return false;
60 }
61
62 std::string name;
63 std::getline(nameFile, name);
64
65 return name == "iio_hwmon";
66}
67
James Feist6714a252018-09-10 15:26:18 -070068void createSensors(
69 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080070 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070071 sensors,
72 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070073 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -070074 sensorsChanged)
75{
James Feistc71c1192019-09-18 14:31:33 -070076 auto getter = std::make_shared<GetSensorConfiguration>(
77 dbusConnection,
78 std::move([&io, &objectServer, &sensors, &dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070079 sensorsChanged](
James Feistc71c1192019-09-18 14:31:33 -070080 const ManagedObjectType& sensorConfigurations) {
81 bool firstScan = sensorsChanged == nullptr;
82 std::vector<fs::path> paths;
83 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)",
84 paths))
James Feist08aec6f2019-01-30 16:17:04 -080085 {
James Feistc71c1192019-09-18 14:31:33 -070086 std::cerr << "No temperature sensors in system\n";
87 return;
88 }
89
90 // iterate through all found adc sensors, and try to match them with
91 // configuration
92 for (auto& path : paths)
93 {
94 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080095 {
James Feistc71c1192019-09-18 14:31:33 -070096 continue;
James Feist08aec6f2019-01-30 16:17:04 -080097 }
James Feistc71c1192019-09-18 14:31:33 -070098 std::smatch match;
99 std::string pathStr = path.string();
James Feist08aec6f2019-01-30 16:17:04 -0800100
James Feistc71c1192019-09-18 14:31:33 -0700101 std::regex_search(pathStr, match, inputRegex);
102 std::string indexStr = *(match.begin() + 1);
James Feist08aec6f2019-01-30 16:17:04 -0800103
James Feistc71c1192019-09-18 14:31:33 -0700104 auto directory = path.parent_path();
105 // convert to 0 based
106 size_t index = std::stoul(indexStr) - 1;
James Feist08aec6f2019-01-30 16:17:04 -0800107
James Feistc71c1192019-09-18 14:31:33 -0700108 const SensorData* sensorData = nullptr;
109 const std::string* interfacePath = nullptr;
110 const std::pair<
111 std::string,
112 boost::container::flat_map<std::string, BasicVariantType>>*
113 baseConfiguration;
114 for (const std::pair<sdbusplus::message::object_path,
115 SensorData>& sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700116 {
James Feistc71c1192019-09-18 14:31:33 -0700117 // clear it out each loop
118 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700119
James Feistc71c1192019-09-18 14:31:33 -0700120 // find base configuration
121 for (const char* type : sensorTypes)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700122 {
James Feistc71c1192019-09-18 14:31:33 -0700123 auto sensorBase = sensor.second.find(type);
124 if (sensorBase != sensor.second.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700125 {
James Feistc71c1192019-09-18 14:31:33 -0700126 baseConfiguration = &(*sensorBase);
127 break;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700128 }
129 }
James Feistc71c1192019-09-18 14:31:33 -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 }
141
142 unsigned int number = std::visit(
143 VariantToUnsignedIntVisitor(), findIndex->second);
144
145 if (number != index)
146 {
147 continue;
148 }
149
150 sensorData = &(sensor.second);
151 interfacePath = &(sensor.first.str);
152 break;
153 }
154 if (sensorData == nullptr)
155 {
156 std::cerr << "failed to find match for " << path.string()
157 << "\n";
158 continue;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700159 }
160
James Feistc71c1192019-09-18 14:31:33 -0700161 if (baseConfiguration == nullptr)
162 {
163 std::cerr << "error finding base configuration for"
164 << path.string() << "\n";
165 continue;
166 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700167
James Feistc71c1192019-09-18 14:31:33 -0700168 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 {
Wludzik, Jozef9a445472020-06-24 12:11:09 +0200186 if (findSensor->second &&
187 boost::ends_with(*it, findSensor->second->name))
James Feistc71c1192019-09-18 14:31:33 -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 std::vector<thresholds::Threshold> sensorThresholds;
201 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
202 {
203 std::cerr << "error populating thresholds for "
204 << sensorName << "\n";
205 }
206
207 auto findScaleFactor =
208 baseConfiguration->second.find("ScaleFactor");
209 float scaleFactor = 1.0;
210 if (findScaleFactor != baseConfiguration->second.end())
211 {
212 scaleFactor = std::visit(VariantToFloatVisitor(),
213 findScaleFactor->second);
Zhikui Ren937eb542020-10-12 13:42:08 -0700214 // scaleFactor is used in division
215 if (scaleFactor == 0.0f)
216 {
217 scaleFactor = 1.0;
218 }
James Feistc71c1192019-09-18 14:31:33 -0700219 }
220
Jeff Lind9cd7042020-11-20 15:49:28 +0800221 auto findPollRate = baseConfiguration->second.find("PollRate");
222 float pollRate = pollRateDefault;
223 if (findPollRate != baseConfiguration->second.end())
224 {
225 pollRate = std::visit(VariantToFloatVisitor(),
226 findPollRate->second);
227 if (pollRate <= 0.0f)
228 {
229 pollRate = pollRateDefault; // polling time too short
230 }
231 }
232
James Feistc71c1192019-09-18 14:31:33 -0700233 auto findPowerOn = baseConfiguration->second.find("PowerState");
234 PowerState readState = PowerState::always;
235 if (findPowerOn != baseConfiguration->second.end())
236 {
237 std::string powerState = std::visit(
238 VariantToStringVisitor(), findPowerOn->second);
239 setReadState(powerState, readState);
240 }
241
242 auto findCPU = baseConfiguration->second.find("CPURequired");
243 if (findCPU != baseConfiguration->second.end())
244 {
245 size_t index =
246 std::visit(VariantToIntVisitor(), findCPU->second);
247 auto presenceFind = cpuPresence.find(index);
248 if (presenceFind == cpuPresence.end())
249 {
250 continue; // no such cpu
251 }
252 if (!presenceFind->second)
253 {
254 continue; // cpu not installed
255 }
256 }
257
258 auto& sensor = sensors[sensorName];
259 sensor = nullptr;
260
261 std::optional<BridgeGpio> bridgeGpio;
262 for (const SensorBaseConfiguration& suppConfig : *sensorData)
263 {
264 if (suppConfig.first.find("BridgeGpio") !=
265 std::string::npos)
266 {
267 auto findName = suppConfig.second.find("Name");
268 if (findName != suppConfig.second.end())
269 {
270 std::string gpioName = std::visit(
271 VariantToStringVisitor(), findName->second);
272
273 int polarity = gpiod::line::ACTIVE_HIGH;
274 auto findPolarity =
275 suppConfig.second.find("Polarity");
276 if (findPolarity != suppConfig.second.end())
277 {
278 if (std::string("Low") ==
279 std::visit(VariantToStringVisitor(),
280 findPolarity->second))
281 {
282 polarity = gpiod::line::ACTIVE_LOW;
283 }
284 }
Zev Weissf72eb832021-06-25 05:55:08 +0000285
286 float setupTime = gpioBridgeSetupTimeDefault;
287 auto findSetupTime =
288 suppConfig.second.find("SetupTime");
289 if (findSetupTime != suppConfig.second.end())
290 {
291 setupTime = std::visit(VariantToFloatVisitor(),
292 findSetupTime->second);
293 }
294
295 bridgeGpio =
296 BridgeGpio(gpioName, polarity, setupTime);
James Feistc71c1192019-09-18 14:31:33 -0700297 }
298
299 break;
300 }
301 }
302
Yong Li1afda6b2020-04-09 19:24:13 +0800303 sensor = std::make_shared<ADCSensor>(
James Feistc71c1192019-09-18 14:31:33 -0700304 path.string(), objectServer, dbusConnection, io, sensorName,
Jeff Lind9cd7042020-11-20 15:49:28 +0800305 std::move(sensorThresholds), scaleFactor, pollRate,
306 readState, *interfacePath, std::move(bridgeGpio));
Yong Li1afda6b2020-04-09 19:24:13 +0800307 sensor->setupRead();
James Feistc71c1192019-09-18 14:31:33 -0700308 }
309 }));
310
311 getter->getConfiguration(
312 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700313}
314
James Feistb6c0b912019-07-09 12:21:44 -0700315int main()
James Feist6714a252018-09-10 15:26:18 -0700316{
317 boost::asio::io_service io;
318 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
319 systemBus->request_name("xyz.openbmc_project.ADCSensor");
320 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800321 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700322 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700323 auto sensorsChanged =
324 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700325
326 io.post([&]() {
327 createSensors(io, objectServer, sensors, systemBus, nullptr);
328 });
329
330 boost::asio::deadline_timer filterTimer(io);
331 std::function<void(sdbusplus::message::message&)> eventHandler =
332 [&](sdbusplus::message::message& message) {
333 if (message.is_method_error())
334 {
335 std::cerr << "callback method error\n";
336 return;
337 }
338 sensorsChanged->insert(message.get_path());
339 // this implicitly cancels the timer
340 filterTimer.expires_from_now(boost::posix_time::seconds(1));
341
342 filterTimer.async_wait([&](const boost::system::error_code& ec) {
343 if (ec == boost::asio::error::operation_aborted)
344 {
345 /* we were canceled*/
346 return;
347 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700348 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700349 {
350 std::cerr << "timer error\n";
351 return;
352 }
353 createSensors(io, objectServer, sensors, systemBus,
354 sensorsChanged);
355 });
356 };
357
James Feistb83bb2b2019-05-31 12:31:23 -0700358 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
359 [&](sdbusplus::message::message& message) {
360 std::string path = message.get_path();
361 boost::to_lower(path);
362
363 if (path.rfind("cpu") == std::string::npos)
364 {
365 return; // not interested
366 }
367 size_t index = 0;
368 try
369 {
370 index = std::stoi(path.substr(path.size() - 1));
371 }
372 catch (std::invalid_argument&)
373 {
374 std::cerr << "Found invalid path " << path << "\n";
375 return;
376 }
377
378 std::string objectName;
379 boost::container::flat_map<std::string, std::variant<bool>> values;
380 message.read(objectName, values);
381 auto findPresence = values.find("Present");
382 if (findPresence != values.end())
383 {
384 cpuPresence[index] = std::get<bool>(findPresence->second);
385 }
386
387 // this implicitly cancels the timer
388 filterTimer.expires_from_now(boost::posix_time::seconds(1));
389
390 filterTimer.async_wait([&](const boost::system::error_code& ec) {
391 if (ec == boost::asio::error::operation_aborted)
392 {
393 /* we were canceled*/
394 return;
395 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700396 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700397 {
398 std::cerr << "timer error\n";
399 return;
400 }
James Feist5591cf082020-07-15 16:44:54 -0700401 createSensors(io, objectServer, sensors, systemBus, nullptr);
James Feistb83bb2b2019-05-31 12:31:23 -0700402 });
403 };
404
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700405 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700406 {
407 auto match = std::make_unique<sdbusplus::bus::match::match>(
408 static_cast<sdbusplus::bus::bus&>(*systemBus),
409 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700410 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700411 eventHandler);
412 matches.emplace_back(std::move(match));
413 }
James Feistb83bb2b2019-05-31 12:31:23 -0700414 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
415 static_cast<sdbusplus::bus::bus&>(*systemBus),
416 "type='signal',member='PropertiesChanged',path_namespace='" +
417 std::string(cpuInventoryPath) +
418 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
419 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700420
Bruce Lee1263c3d2021-06-04 15:16:33 +0800421 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700422 io.run();
423}