blob: 526532118b0a8abea745f3497137a2ff42010d2e [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;
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
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,
James Feist5591cf082020-07-15 16:44:54 -070071 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -070072 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,
James Feist5591cf082020-07-15 16:44:54 -070077 sensorsChanged](
James Feistc71c1192019-09-18 14:31:33 -070078 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 {
Wludzik, Jozef9a445472020-06-24 12:11:09 +0200184 if (findSensor->second &&
185 boost::ends_with(*it, findSensor->second->name))
James Feistc71c1192019-09-18 14:31:33 -0700186 {
187 sensorsChanged->erase(it);
188 findSensor->second = nullptr;
189 found = true;
190 break;
191 }
192 }
193 if (!found)
194 {
195 continue;
196 }
197 }
198 std::vector<thresholds::Threshold> sensorThresholds;
199 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
200 {
201 std::cerr << "error populating thresholds for "
202 << sensorName << "\n";
203 }
204
205 auto findScaleFactor =
206 baseConfiguration->second.find("ScaleFactor");
207 float scaleFactor = 1.0;
208 if (findScaleFactor != baseConfiguration->second.end())
209 {
210 scaleFactor = std::visit(VariantToFloatVisitor(),
211 findScaleFactor->second);
Zhikui Ren937eb542020-10-12 13:42:08 -0700212 // scaleFactor is used in division
213 if (scaleFactor == 0.0f)
214 {
215 scaleFactor = 1.0;
216 }
James Feistc71c1192019-09-18 14:31:33 -0700217 }
218
219 auto findPowerOn = baseConfiguration->second.find("PowerState");
220 PowerState readState = PowerState::always;
221 if (findPowerOn != baseConfiguration->second.end())
222 {
223 std::string powerState = std::visit(
224 VariantToStringVisitor(), findPowerOn->second);
225 setReadState(powerState, readState);
226 }
227
228 auto findCPU = baseConfiguration->second.find("CPURequired");
229 if (findCPU != baseConfiguration->second.end())
230 {
231 size_t index =
232 std::visit(VariantToIntVisitor(), findCPU->second);
233 auto presenceFind = cpuPresence.find(index);
234 if (presenceFind == cpuPresence.end())
235 {
236 continue; // no such cpu
237 }
238 if (!presenceFind->second)
239 {
240 continue; // cpu not installed
241 }
242 }
243
244 auto& sensor = sensors[sensorName];
245 sensor = nullptr;
246
247 std::optional<BridgeGpio> bridgeGpio;
248 for (const SensorBaseConfiguration& suppConfig : *sensorData)
249 {
250 if (suppConfig.first.find("BridgeGpio") !=
251 std::string::npos)
252 {
253 auto findName = suppConfig.second.find("Name");
254 if (findName != suppConfig.second.end())
255 {
256 std::string gpioName = std::visit(
257 VariantToStringVisitor(), findName->second);
258
259 int polarity = gpiod::line::ACTIVE_HIGH;
260 auto findPolarity =
261 suppConfig.second.find("Polarity");
262 if (findPolarity != suppConfig.second.end())
263 {
264 if (std::string("Low") ==
265 std::visit(VariantToStringVisitor(),
266 findPolarity->second))
267 {
268 polarity = gpiod::line::ACTIVE_LOW;
269 }
270 }
271 bridgeGpio = BridgeGpio(gpioName, polarity);
272 }
273
274 break;
275 }
276 }
277
Yong Li1afda6b2020-04-09 19:24:13 +0800278 sensor = std::make_shared<ADCSensor>(
James Feistc71c1192019-09-18 14:31:33 -0700279 path.string(), objectServer, dbusConnection, io, sensorName,
280 std::move(sensorThresholds), scaleFactor, readState,
281 *interfacePath, std::move(bridgeGpio));
Yong Li1afda6b2020-04-09 19:24:13 +0800282 sensor->setupRead();
James Feistc71c1192019-09-18 14:31:33 -0700283 }
284 }));
285
286 getter->getConfiguration(
287 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700288}
289
James Feistb6c0b912019-07-09 12:21:44 -0700290int main()
James Feist6714a252018-09-10 15:26:18 -0700291{
292 boost::asio::io_service io;
293 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
294 systemBus->request_name("xyz.openbmc_project.ADCSensor");
295 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800296 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist6714a252018-09-10 15:26:18 -0700297 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700298 auto sensorsChanged =
299 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700300
301 io.post([&]() {
302 createSensors(io, objectServer, sensors, systemBus, nullptr);
303 });
304
305 boost::asio::deadline_timer filterTimer(io);
306 std::function<void(sdbusplus::message::message&)> eventHandler =
307 [&](sdbusplus::message::message& message) {
308 if (message.is_method_error())
309 {
310 std::cerr << "callback method error\n";
311 return;
312 }
313 sensorsChanged->insert(message.get_path());
314 // this implicitly cancels the timer
315 filterTimer.expires_from_now(boost::posix_time::seconds(1));
316
317 filterTimer.async_wait([&](const boost::system::error_code& ec) {
318 if (ec == boost::asio::error::operation_aborted)
319 {
320 /* we were canceled*/
321 return;
322 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700323 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700324 {
325 std::cerr << "timer error\n";
326 return;
327 }
328 createSensors(io, objectServer, sensors, systemBus,
329 sensorsChanged);
330 });
331 };
332
James Feistb83bb2b2019-05-31 12:31:23 -0700333 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
334 [&](sdbusplus::message::message& message) {
335 std::string path = message.get_path();
336 boost::to_lower(path);
337
338 if (path.rfind("cpu") == std::string::npos)
339 {
340 return; // not interested
341 }
342 size_t index = 0;
343 try
344 {
345 index = std::stoi(path.substr(path.size() - 1));
346 }
347 catch (std::invalid_argument&)
348 {
349 std::cerr << "Found invalid path " << path << "\n";
350 return;
351 }
352
353 std::string objectName;
354 boost::container::flat_map<std::string, std::variant<bool>> values;
355 message.read(objectName, values);
356 auto findPresence = values.find("Present");
357 if (findPresence != values.end())
358 {
359 cpuPresence[index] = std::get<bool>(findPresence->second);
360 }
361
362 // this implicitly cancels the timer
363 filterTimer.expires_from_now(boost::posix_time::seconds(1));
364
365 filterTimer.async_wait([&](const boost::system::error_code& ec) {
366 if (ec == boost::asio::error::operation_aborted)
367 {
368 /* we were canceled*/
369 return;
370 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700371 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700372 {
373 std::cerr << "timer error\n";
374 return;
375 }
James Feist5591cf082020-07-15 16:44:54 -0700376 createSensors(io, objectServer, sensors, systemBus, nullptr);
James Feistb83bb2b2019-05-31 12:31:23 -0700377 });
378 };
379
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700380 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700381 {
382 auto match = std::make_unique<sdbusplus::bus::match::match>(
383 static_cast<sdbusplus::bus::bus&>(*systemBus),
384 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700385 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700386 eventHandler);
387 matches.emplace_back(std::move(match));
388 }
James Feistb83bb2b2019-05-31 12:31:23 -0700389 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
390 static_cast<sdbusplus::bus::bus&>(*systemBus),
391 "type='signal',member='PropertiesChanged',path_namespace='" +
392 std::string(cpuInventoryPath) +
393 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
394 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700395
396 io.run();
397}