blob: df3572256a45c98eb5cee06f4898fd39c1aaded7 [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "ADCSensor.hpp"
18#include "Utils.hpp"
19#include "VariantVisitors.hpp"
20
James Feistb83bb2b2019-05-31 12:31:23 -070021#include <boost/algorithm/string/case_conv.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/algorithm/string/predicate.hpp>
23#include <boost/algorithm/string/replace.hpp>
24#include <boost/container/flat_set.hpp>
James Feist24f02f22019-04-15 11:05:39 -070025#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070026#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <functional>
28#include <memory>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040029#include <optional>
James Feist6714a252018-09-10 15:26:18 -070030#include <regex>
31#include <sdbusplus/asio/connection.hpp>
32#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <sdbusplus/bus/match.hpp>
34#include <string>
35#include <variant>
36#include <vector>
James Feist6714a252018-09-10 15:26:18 -070037
38static constexpr bool DEBUG = false;
39
James Feistcf3bce62019-01-08 10:07:19 -080040namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080041
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070042static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070043 "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
James Feist08aec6f2019-01-30 16:17:04 -080048// filter out adc from any other voltage sensor
49bool isAdc(const fs::path& parentPath)
50{
51 fs::path namePath = parentPath / "name";
52
53 std::ifstream nameFile(namePath);
54 if (!nameFile.good())
55 {
56 std::cerr << "Failure reading " << namePath.string() << "\n";
57 return false;
58 }
59
60 std::string name;
61 std::getline(nameFile, name);
62
63 return name == "iio_hwmon";
64}
65
James Feist6714a252018-09-10 15:26:18 -070066void createSensors(
67 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080068 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070069 sensors,
70 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
71 const std::unique_ptr<boost::container::flat_set<std::string>>&
72 sensorsChanged)
73{
James Feistc71c1192019-09-18 14:31:33 -070074 auto getter = std::make_shared<GetSensorConfiguration>(
75 dbusConnection,
76 std::move([&io, &objectServer, &sensors, &dbusConnection,
77 &sensorsChanged](
78 const ManagedObjectType& sensorConfigurations) {
79 bool firstScan = sensorsChanged == nullptr;
80 std::vector<fs::path> paths;
81 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)",
82 paths))
James Feist08aec6f2019-01-30 16:17:04 -080083 {
James Feistc71c1192019-09-18 14:31:33 -070084 std::cerr << "No temperature sensors in system\n";
85 return;
86 }
87
88 // iterate through all found adc sensors, and try to match them with
89 // configuration
90 for (auto& path : paths)
91 {
92 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080093 {
James Feistc71c1192019-09-18 14:31:33 -070094 continue;
James Feist08aec6f2019-01-30 16:17:04 -080095 }
James Feistc71c1192019-09-18 14:31:33 -070096 std::smatch match;
97 std::string pathStr = path.string();
James Feist08aec6f2019-01-30 16:17:04 -080098
James Feistc71c1192019-09-18 14:31:33 -070099 std::regex_search(pathStr, match, inputRegex);
100 std::string indexStr = *(match.begin() + 1);
James Feist08aec6f2019-01-30 16:17:04 -0800101
James Feistc71c1192019-09-18 14:31:33 -0700102 auto directory = path.parent_path();
103 // convert to 0 based
104 size_t index = std::stoul(indexStr) - 1;
James Feist08aec6f2019-01-30 16:17:04 -0800105
James Feistc71c1192019-09-18 14:31:33 -0700106 const SensorData* sensorData = nullptr;
107 const std::string* interfacePath = nullptr;
108 const std::pair<
109 std::string,
110 boost::container::flat_map<std::string, BasicVariantType>>*
111 baseConfiguration;
112 for (const std::pair<sdbusplus::message::object_path,
113 SensorData>& sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700114 {
James Feistc71c1192019-09-18 14:31:33 -0700115 // clear it out each loop
116 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700117
James Feistc71c1192019-09-18 14:31:33 -0700118 // find base configuration
119 for (const char* type : sensorTypes)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700120 {
James Feistc71c1192019-09-18 14:31:33 -0700121 auto sensorBase = sensor.second.find(type);
122 if (sensorBase != sensor.second.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700123 {
James Feistc71c1192019-09-18 14:31:33 -0700124 baseConfiguration = &(*sensorBase);
125 break;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700126 }
127 }
James Feistc71c1192019-09-18 14:31:33 -0700128 if (baseConfiguration == nullptr)
129 {
130 continue;
131 }
132 auto findIndex = baseConfiguration->second.find("Index");
133 if (findIndex == baseConfiguration->second.end())
134 {
135 std::cerr << "Base configuration missing Index"
136 << baseConfiguration->first << "\n";
137 continue;
138 }
139
140 unsigned int number = std::visit(
141 VariantToUnsignedIntVisitor(), findIndex->second);
142
143 if (number != index)
144 {
145 continue;
146 }
147
148 sensorData = &(sensor.second);
149 interfacePath = &(sensor.first.str);
150 break;
151 }
152 if (sensorData == nullptr)
153 {
154 std::cerr << "failed to find match for " << path.string()
155 << "\n";
156 continue;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700157 }
158
James Feistc71c1192019-09-18 14:31:33 -0700159 if (baseConfiguration == nullptr)
160 {
161 std::cerr << "error finding base configuration for"
162 << path.string() << "\n";
163 continue;
164 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700165
James Feistc71c1192019-09-18 14:31:33 -0700166 auto findSensorName = baseConfiguration->second.find("Name");
167 if (findSensorName == baseConfiguration->second.end())
168 {
169 std::cerr << "could not determine configuration name for "
170 << path.string() << "\n";
171 continue;
172 }
173 std::string sensorName =
174 std::get<std::string>(findSensorName->second);
175
176 // on rescans, only update sensors we were signaled by
177 auto findSensor = sensors.find(sensorName);
178 if (!firstScan && findSensor != sensors.end())
179 {
180 bool found = false;
181 for (auto it = sensorsChanged->begin();
182 it != sensorsChanged->end(); it++)
183 {
184 if (boost::ends_with(*it, findSensor->second->name))
185 {
186 sensorsChanged->erase(it);
187 findSensor->second = nullptr;
188 found = true;
189 break;
190 }
191 }
192 if (!found)
193 {
194 continue;
195 }
196 }
197 std::vector<thresholds::Threshold> sensorThresholds;
198 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
199 {
200 std::cerr << "error populating thresholds for "
201 << sensorName << "\n";
202 }
203
204 auto findScaleFactor =
205 baseConfiguration->second.find("ScaleFactor");
206 float scaleFactor = 1.0;
207 if (findScaleFactor != baseConfiguration->second.end())
208 {
209 scaleFactor = std::visit(VariantToFloatVisitor(),
210 findScaleFactor->second);
211 }
212
213 auto findPowerOn = baseConfiguration->second.find("PowerState");
214 PowerState readState = PowerState::always;
215 if (findPowerOn != baseConfiguration->second.end())
216 {
217 std::string powerState = std::visit(
218 VariantToStringVisitor(), findPowerOn->second);
219 setReadState(powerState, readState);
220 }
221
222 auto findCPU = baseConfiguration->second.find("CPURequired");
223 if (findCPU != baseConfiguration->second.end())
224 {
225 size_t index =
226 std::visit(VariantToIntVisitor(), findCPU->second);
227 auto presenceFind = cpuPresence.find(index);
228 if (presenceFind == cpuPresence.end())
229 {
230 continue; // no such cpu
231 }
232 if (!presenceFind->second)
233 {
234 continue; // cpu not installed
235 }
236 }
237
238 auto& sensor = sensors[sensorName];
239 sensor = nullptr;
240
241 std::optional<BridgeGpio> bridgeGpio;
242 for (const SensorBaseConfiguration& suppConfig : *sensorData)
243 {
244 if (suppConfig.first.find("BridgeGpio") !=
245 std::string::npos)
246 {
247 auto findName = suppConfig.second.find("Name");
248 if (findName != suppConfig.second.end())
249 {
250 std::string gpioName = std::visit(
251 VariantToStringVisitor(), findName->second);
252
253 int polarity = gpiod::line::ACTIVE_HIGH;
254 auto findPolarity =
255 suppConfig.second.find("Polarity");
256 if (findPolarity != suppConfig.second.end())
257 {
258 if (std::string("Low") ==
259 std::visit(VariantToStringVisitor(),
260 findPolarity->second))
261 {
262 polarity = gpiod::line::ACTIVE_LOW;
263 }
264 }
265 bridgeGpio = BridgeGpio(gpioName, polarity);
266 }
267
268 break;
269 }
270 }
271
Yong Li1afda6b2020-04-09 19:24:13 +0800272 sensor = std::make_shared<ADCSensor>(
James Feistc71c1192019-09-18 14:31:33 -0700273 path.string(), objectServer, dbusConnection, io, sensorName,
274 std::move(sensorThresholds), scaleFactor, readState,
275 *interfacePath, std::move(bridgeGpio));
Yong Li1afda6b2020-04-09 19:24:13 +0800276 sensor->setupRead();
James Feistc71c1192019-09-18 14:31:33 -0700277 }
278 }));
279
280 getter->getConfiguration(
281 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700282}
283
James Feistb6c0b912019-07-09 12:21:44 -0700284int main()
James Feist6714a252018-09-10 15:26:18 -0700285{
286 boost::asio::io_service io;
287 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
288 systemBus->request_name("xyz.openbmc_project.ADCSensor");
289 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800290 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700291 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
292 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
293 std::make_unique<boost::container::flat_set<std::string>>();
294
295 io.post([&]() {
296 createSensors(io, objectServer, sensors, systemBus, nullptr);
297 });
298
299 boost::asio::deadline_timer filterTimer(io);
300 std::function<void(sdbusplus::message::message&)> eventHandler =
301 [&](sdbusplus::message::message& message) {
302 if (message.is_method_error())
303 {
304 std::cerr << "callback method error\n";
305 return;
306 }
307 sensorsChanged->insert(message.get_path());
308 // this implicitly cancels the timer
309 filterTimer.expires_from_now(boost::posix_time::seconds(1));
310
311 filterTimer.async_wait([&](const boost::system::error_code& ec) {
312 if (ec == boost::asio::error::operation_aborted)
313 {
314 /* we were canceled*/
315 return;
316 }
317 else if (ec)
318 {
319 std::cerr << "timer error\n";
320 return;
321 }
322 createSensors(io, objectServer, sensors, systemBus,
323 sensorsChanged);
324 });
325 };
326
James Feistb83bb2b2019-05-31 12:31:23 -0700327 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
328 [&](sdbusplus::message::message& message) {
329 std::string path = message.get_path();
330 boost::to_lower(path);
331
332 if (path.rfind("cpu") == std::string::npos)
333 {
334 return; // not interested
335 }
336 size_t index = 0;
337 try
338 {
339 index = std::stoi(path.substr(path.size() - 1));
340 }
341 catch (std::invalid_argument&)
342 {
343 std::cerr << "Found invalid path " << path << "\n";
344 return;
345 }
346
347 std::string objectName;
348 boost::container::flat_map<std::string, std::variant<bool>> values;
349 message.read(objectName, values);
350 auto findPresence = values.find("Present");
351 if (findPresence != values.end())
352 {
353 cpuPresence[index] = std::get<bool>(findPresence->second);
354 }
355
356 // this implicitly cancels the timer
357 filterTimer.expires_from_now(boost::posix_time::seconds(1));
358
359 filterTimer.async_wait([&](const boost::system::error_code& ec) {
360 if (ec == boost::asio::error::operation_aborted)
361 {
362 /* we were canceled*/
363 return;
364 }
365 else if (ec)
366 {
367 std::cerr << "timer error\n";
368 return;
369 }
370 createSensors(io, objectServer, sensors, systemBus, {});
371 });
372 };
373
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700374 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700375 {
376 auto match = std::make_unique<sdbusplus::bus::match::match>(
377 static_cast<sdbusplus::bus::bus&>(*systemBus),
378 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700379 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700380 eventHandler);
381 matches.emplace_back(std::move(match));
382 }
James Feistb83bb2b2019-05-31 12:31:23 -0700383 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
384 static_cast<sdbusplus::bus::bus&>(*systemBus),
385 "type='signal',member='PropertiesChanged',path_namespace='" +
386 std::string(cpuInventoryPath) +
387 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
388 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700389
390 io.run();
391}