blob: 019226fb4fc9e3c6ba8249754b9029939ed2f68a [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
Jeff Lind9cd7042020-11-20 15:49:28 +080038static constexpr float pollRateDefault = 0.5;
Zev Weissf72eb832021-06-25 05:55:08 +000039static constexpr float gpioBridgeSetupTimeDefault = 0.02;
James Feist6714a252018-09-10 15:26:18 -070040
James Feistcf3bce62019-01-08 10:07:19 -080041namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080042
Brandon Kim66558232021-11-09 16:53:08 -080043static constexpr auto sensorTypes{
44 std::to_array<const char*>({"xyz.openbmc_project.Configuration.ADC"})};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070045static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070046
James Feistb83bb2b2019-05-31 12:31:23 -070047static boost::container::flat_map<size_t, bool> cpuPresence;
48
Matt Spinler6ad74d92022-03-01 10:56:46 -060049enum class UpdateType
50{
51 init,
52 cpuPresenceChange
53};
54
James Feist08aec6f2019-01-30 16:17:04 -080055// filter out adc from any other voltage sensor
56bool isAdc(const fs::path& parentPath)
57{
58 fs::path namePath = parentPath / "name";
59
60 std::ifstream nameFile(namePath);
61 if (!nameFile.good())
62 {
63 std::cerr << "Failure reading " << namePath.string() << "\n";
64 return false;
65 }
66
67 std::string name;
68 std::getline(nameFile, name);
69
70 return name == "iio_hwmon";
71}
72
James Feist6714a252018-09-10 15:26:18 -070073void createSensors(
74 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Li1afda6b2020-04-09 19:24:13 +080075 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>>&
James Feist6714a252018-09-10 15:26:18 -070076 sensors,
77 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070078 const std::shared_ptr<boost::container::flat_set<std::string>>&
Matt Spinler6ad74d92022-03-01 10:56:46 -060079 sensorsChanged,
80 UpdateType updateType)
James Feist6714a252018-09-10 15:26:18 -070081{
James Feistc71c1192019-09-18 14:31:33 -070082 auto getter = std::make_shared<GetSensorConfiguration>(
83 dbusConnection,
Matt Spinler6ad74d92022-03-01 10:56:46 -060084 [&io, &objectServer, &sensors, &dbusConnection, sensorsChanged,
85 updateType](const ManagedObjectType& sensorConfigurations) {
Ed Tanousbb679322022-05-16 16:10:00 -070086 bool firstScan = sensorsChanged == nullptr;
87 std::vector<fs::path> paths;
88 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
89 {
90 std::cerr << "No adc sensors in system\n";
91 return;
92 }
93
94 // iterate through all found adc sensors, and try to match them with
95 // configuration
96 for (auto& path : paths)
97 {
98 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080099 {
Ed Tanousbb679322022-05-16 16:10:00 -0700100 continue;
James Feistc71c1192019-09-18 14:31:33 -0700101 }
Ed Tanousbb679322022-05-16 16:10:00 -0700102 std::smatch match;
103 std::string pathStr = path.string();
James Feistc71c1192019-09-18 14:31:33 -0700104
Ed Tanousbb679322022-05-16 16:10:00 -0700105 std::regex_search(pathStr, match, inputRegex);
106 std::string indexStr = *(match.begin() + 1);
107
108 auto directory = path.parent_path();
109 // convert to 0 based
110 size_t index = std::stoul(indexStr) - 1;
111
112 const SensorData* sensorData = nullptr;
113 const std::string* interfacePath = nullptr;
Zev Weissafd15042022-07-18 12:28:40 -0700114 const std::pair<std::string, SensorBaseConfigMap>*
Ed Tanousbb679322022-05-16 16:10:00 -0700115 baseConfiguration = nullptr;
Zev Weiss9148f382022-08-12 18:21:01 -0700116 for (const auto& [path, cfgData] : sensorConfigurations)
James Feistc71c1192019-09-18 14:31:33 -0700117 {
Ed Tanousbb679322022-05-16 16:10:00 -0700118 // clear it out each loop
119 baseConfiguration = nullptr;
120
121 // find base configuration
122 for (const char* type : sensorTypes)
James Feist08aec6f2019-01-30 16:17:04 -0800123 {
Zev Weiss9148f382022-08-12 18:21:01 -0700124 auto sensorBase = cfgData.find(type);
125 if (sensorBase != cfgData.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700126 {
Ed Tanousbb679322022-05-16 16:10:00 -0700127 baseConfiguration = &(*sensorBase);
James Feistc71c1192019-09-18 14:31:33 -0700128 break;
129 }
130 }
Ed Tanousbb679322022-05-16 16:10:00 -0700131 if (baseConfiguration == nullptr)
132 {
133 continue;
134 }
135 auto findIndex = baseConfiguration->second.find("Index");
136 if (findIndex == baseConfiguration->second.end())
137 {
138 std::cerr << "Base configuration missing Index"
139 << baseConfiguration->first << "\n";
140 continue;
141 }
James Feistc71c1192019-09-18 14:31:33 -0700142
Ed Tanousbb679322022-05-16 16:10:00 -0700143 unsigned int number = std::visit(VariantToUnsignedIntVisitor(),
144 findIndex->second);
145
146 if (number != index)
147 {
148 continue;
149 }
150
Zev Weiss9148f382022-08-12 18:21:01 -0700151 sensorData = &cfgData;
152 interfacePath = &path.str;
Ed Tanousbb679322022-05-16 16:10:00 -0700153 break;
James Feistc71c1192019-09-18 14:31:33 -0700154 }
Ed Tanousbb679322022-05-16 16:10:00 -0700155 if (sensorData == nullptr)
156 {
157 std::cerr << "failed to find match for " << path.string()
158 << "\n";
159 continue;
160 }
161
162 if (baseConfiguration == nullptr)
163 {
164 std::cerr << "error finding base configuration for"
165 << path.string() << "\n";
166 continue;
167 }
168
169 auto findSensorName = baseConfiguration->second.find("Name");
170 if (findSensorName == baseConfiguration->second.end())
171 {
172 std::cerr << "could not determine configuration name for "
173 << path.string() << "\n";
174 continue;
175 }
176 std::string sensorName =
177 std::get<std::string>(findSensorName->second);
178
179 // on rescans, only update sensors we were signaled by
180 auto findSensor = sensors.find(sensorName);
181 if (!firstScan && findSensor != sensors.end())
182 {
183 bool found = false;
184 for (auto it = sensorsChanged->begin();
185 it != sensorsChanged->end(); it++)
186 {
187 if (findSensor->second &&
188 boost::ends_with(*it, findSensor->second->name))
189 {
190 sensorsChanged->erase(it);
191 findSensor->second = nullptr;
192 found = true;
193 break;
194 }
195 }
196 if (!found)
197 {
198 continue;
199 }
200 }
201
202 auto findCPU = baseConfiguration->second.find("CPURequired");
203 if (findCPU != baseConfiguration->second.end())
204 {
205 size_t index =
206 std::visit(VariantToIntVisitor(), findCPU->second);
207 auto presenceFind = cpuPresence.find(index);
208 if (presenceFind == cpuPresence.end())
209 {
210 continue; // no such cpu
211 }
212 if (!presenceFind->second)
213 {
214 continue; // cpu not installed
215 }
216 }
217 else if (updateType == UpdateType::cpuPresenceChange)
218 {
219 continue;
220 }
221
222 std::vector<thresholds::Threshold> sensorThresholds;
223 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
224 {
225 std::cerr << "error populating thresholds for " << sensorName
226 << "\n";
227 }
228
229 auto findScaleFactor =
230 baseConfiguration->second.find("ScaleFactor");
231 float scaleFactor = 1.0;
232 if (findScaleFactor != baseConfiguration->second.end())
233 {
234 scaleFactor = std::visit(VariantToFloatVisitor(),
235 findScaleFactor->second);
236 // scaleFactor is used in division
Ed Tanous2049bd22022-07-09 07:20:26 -0700237 if (scaleFactor == 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700238 {
239 scaleFactor = 1.0;
240 }
241 }
242
243 auto findPollRate = baseConfiguration->second.find("PollRate");
244 float pollRate = pollRateDefault;
245 if (findPollRate != baseConfiguration->second.end())
246 {
247 pollRate =
248 std::visit(VariantToFloatVisitor(), findPollRate->second);
Ed Tanous2049bd22022-07-09 07:20:26 -0700249 if (pollRate <= 0.0F)
Ed Tanousbb679322022-05-16 16:10:00 -0700250 {
251 pollRate = pollRateDefault; // polling time too short
252 }
253 }
254
Zev Weissa4d27682022-07-19 15:30:36 -0700255 PowerState readState = getPowerState(baseConfiguration->second);
Ed Tanousbb679322022-05-16 16:10:00 -0700256
257 auto& sensor = sensors[sensorName];
258 sensor = nullptr;
259
260 std::optional<BridgeGpio> bridgeGpio;
Zev Weiss9148f382022-08-12 18:21:01 -0700261 for (const auto& [key, cfgMap] : *sensorData)
Ed Tanousbb679322022-05-16 16:10:00 -0700262 {
Zev Weiss9148f382022-08-12 18:21:01 -0700263 if (key.find("BridgeGpio") != std::string::npos)
Ed Tanousbb679322022-05-16 16:10:00 -0700264 {
Zev Weiss9148f382022-08-12 18:21:01 -0700265 auto findName = cfgMap.find("Name");
266 if (findName != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700267 {
268 std::string gpioName = std::visit(
269 VariantToStringVisitor(), findName->second);
270
271 int polarity = gpiod::line::ACTIVE_HIGH;
Zev Weiss9148f382022-08-12 18:21:01 -0700272 auto findPolarity = cfgMap.find("Polarity");
273 if (findPolarity != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700274 {
275 if (std::string("Low") ==
276 std::visit(VariantToStringVisitor(),
277 findPolarity->second))
278 {
279 polarity = gpiod::line::ACTIVE_LOW;
280 }
281 }
282
283 float setupTime = gpioBridgeSetupTimeDefault;
Zev Weiss9148f382022-08-12 18:21:01 -0700284 auto findSetupTime = cfgMap.find("SetupTime");
285 if (findSetupTime != cfgMap.end())
Ed Tanousbb679322022-05-16 16:10:00 -0700286 {
287 setupTime = std::visit(VariantToFloatVisitor(),
288 findSetupTime->second);
289 }
290
291 bridgeGpio = BridgeGpio(gpioName, polarity, setupTime);
292 }
293
294 break;
295 }
296 }
297
298 sensor = std::make_shared<ADCSensor>(
299 path.string(), objectServer, dbusConnection, io, sensorName,
300 std::move(sensorThresholds), scaleFactor, pollRate, readState,
301 *interfacePath, std::move(bridgeGpio));
302 sensor->setupRead();
303 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700304 });
James Feistc71c1192019-09-18 14:31:33 -0700305
306 getter->getConfiguration(
307 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700308}
309
James Feistb6c0b912019-07-09 12:21:44 -0700310int main()
James Feist6714a252018-09-10 15:26:18 -0700311{
312 boost::asio::io_service io;
313 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
314 systemBus->request_name("xyz.openbmc_project.ADCSensor");
315 sdbusplus::asio::object_server objectServer(systemBus);
Yong Li1afda6b2020-04-09 19:24:13 +0800316 boost::container::flat_map<std::string, std::shared_ptr<ADCSensor>> sensors;
James Feist5591cf082020-07-15 16:44:54 -0700317 auto sensorsChanged =
318 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700319
320 io.post([&]() {
Matt Spinler6ad74d92022-03-01 10:56:46 -0600321 createSensors(io, objectServer, sensors, systemBus, nullptr,
322 UpdateType::init);
James Feist6714a252018-09-10 15:26:18 -0700323 });
324
325 boost::asio::deadline_timer filterTimer(io);
Patrick Williams92f8f512022-07-22 19:26:55 -0500326 std::function<void(sdbusplus::message_t&)> eventHandler =
327 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700328 if (message.is_method_error())
329 {
330 std::cerr << "callback method error\n";
331 return;
332 }
333 sensorsChanged->insert(message.get_path());
334 // this implicitly cancels the timer
335 filterTimer.expires_from_now(boost::posix_time::seconds(1));
336
337 filterTimer.async_wait([&](const boost::system::error_code& ec) {
338 if (ec == boost::asio::error::operation_aborted)
James Feist6714a252018-09-10 15:26:18 -0700339 {
Ed Tanousbb679322022-05-16 16:10:00 -0700340 /* we were canceled*/
James Feist6714a252018-09-10 15:26:18 -0700341 return;
342 }
Ed Tanousbb679322022-05-16 16:10:00 -0700343 if (ec)
344 {
345 std::cerr << "timer error\n";
346 return;
347 }
348 createSensors(io, objectServer, sensors, systemBus, sensorsChanged,
349 UpdateType::init);
350 });
351 };
James Feist6714a252018-09-10 15:26:18 -0700352
Patrick Williams92f8f512022-07-22 19:26:55 -0500353 std::function<void(sdbusplus::message_t&)> cpuPresenceHandler =
354 [&](sdbusplus::message_t& message) {
Ed Tanousbb679322022-05-16 16:10:00 -0700355 std::string path = message.get_path();
356 boost::to_lower(path);
James Feistb83bb2b2019-05-31 12:31:23 -0700357
Ed Tanousbb679322022-05-16 16:10:00 -0700358 if (path.rfind("cpu") == std::string::npos)
359 {
360 return; // not interested
361 }
362 size_t index = 0;
363 try
364 {
365 index = std::stoi(path.substr(path.size() - 1));
366 }
367 catch (const std::invalid_argument&)
368 {
369 std::cerr << "Found invalid path " << path << "\n";
370 return;
371 }
372
373 std::string objectName;
374 boost::container::flat_map<std::string, std::variant<bool>> values;
375 message.read(objectName, values);
376 auto findPresence = values.find("Present");
377 if (findPresence != values.end())
378 {
379 cpuPresence[index] = std::get<bool>(findPresence->second);
380 }
381
382 // this implicitly cancels the timer
383 filterTimer.expires_from_now(boost::posix_time::seconds(1));
384
385 filterTimer.async_wait([&](const boost::system::error_code& ec) {
386 if (ec == boost::asio::error::operation_aborted)
James Feistb83bb2b2019-05-31 12:31:23 -0700387 {
Ed Tanousbb679322022-05-16 16:10:00 -0700388 /* we were canceled*/
James Feistb83bb2b2019-05-31 12:31:23 -0700389 return;
390 }
Ed Tanousbb679322022-05-16 16:10:00 -0700391 if (ec)
James Feistb83bb2b2019-05-31 12:31:23 -0700392 {
Ed Tanousbb679322022-05-16 16:10:00 -0700393 std::cerr << "timer error\n";
394 return;
James Feistb83bb2b2019-05-31 12:31:23 -0700395 }
Ed Tanousbb679322022-05-16 16:10:00 -0700396 createSensors(io, objectServer, sensors, systemBus, nullptr,
397 UpdateType::cpuPresenceChange);
398 });
399 };
James Feistb83bb2b2019-05-31 12:31:23 -0700400
Zev Weiss214d9712022-08-12 12:54:31 -0700401 std::vector<std::unique_ptr<sdbusplus::bus::match_t>> matches =
402 setupPropertiesChangedMatches(*systemBus, sensorTypes, eventHandler);
Patrick Williams92f8f512022-07-22 19:26:55 -0500403 matches.emplace_back(std::make_unique<sdbusplus::bus::match_t>(
404 static_cast<sdbusplus::bus_t&>(*systemBus),
James Feistb83bb2b2019-05-31 12:31:23 -0700405 "type='signal',member='PropertiesChanged',path_namespace='" +
406 std::string(cpuInventoryPath) +
407 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
408 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700409
Bruce Lee1263c3d2021-06-04 15:16:33 +0800410 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700411 io.run();
412}