blob: 8ce56b5ae8cf7226229b25ff5410bf947632eab9 [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
17#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 Feist24f02f22019-04-15 11:05:39 -070024#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070025#include <fstream>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040026#include <optional>
James Feist6714a252018-09-10 15:26:18 -070027#include <regex>
28#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30
31static constexpr bool DEBUG = false;
32
James Feistcf3bce62019-01-08 10:07:19 -080033namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080034
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070035static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070036 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070037static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070038
James Feistb83bb2b2019-05-31 12:31:23 -070039static boost::container::flat_map<size_t, bool> cpuPresence;
40
James Feist08aec6f2019-01-30 16:17:04 -080041// filter out adc from any other voltage sensor
42bool isAdc(const fs::path& parentPath)
43{
44 fs::path namePath = parentPath / "name";
45
46 std::ifstream nameFile(namePath);
47 if (!nameFile.good())
48 {
49 std::cerr << "Failure reading " << namePath.string() << "\n";
50 return false;
51 }
52
53 std::string name;
54 std::getline(nameFile, name);
55
56 return name == "iio_hwmon";
57}
58
James Feist6714a252018-09-10 15:26:18 -070059void createSensors(
60 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
61 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
62 sensors,
63 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
64 const std::unique_ptr<boost::container::flat_set<std::string>>&
65 sensorsChanged)
66{
James Feistc71c1192019-09-18 14:31:33 -070067 auto getter = std::make_shared<GetSensorConfiguration>(
68 dbusConnection,
69 std::move([&io, &objectServer, &sensors, &dbusConnection,
70 &sensorsChanged](
71 const ManagedObjectType& sensorConfigurations) {
72 bool firstScan = sensorsChanged == nullptr;
73 std::vector<fs::path> paths;
74 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)",
75 paths))
James Feist08aec6f2019-01-30 16:17:04 -080076 {
James Feistc71c1192019-09-18 14:31:33 -070077 std::cerr << "No temperature sensors in system\n";
78 return;
79 }
80
81 // iterate through all found adc sensors, and try to match them with
82 // configuration
83 for (auto& path : paths)
84 {
85 if (!isAdc(path.parent_path()))
James Feist08aec6f2019-01-30 16:17:04 -080086 {
James Feistc71c1192019-09-18 14:31:33 -070087 continue;
James Feist08aec6f2019-01-30 16:17:04 -080088 }
James Feistc71c1192019-09-18 14:31:33 -070089 std::smatch match;
90 std::string pathStr = path.string();
James Feist08aec6f2019-01-30 16:17:04 -080091
James Feistc71c1192019-09-18 14:31:33 -070092 std::regex_search(pathStr, match, inputRegex);
93 std::string indexStr = *(match.begin() + 1);
James Feist08aec6f2019-01-30 16:17:04 -080094
James Feistc71c1192019-09-18 14:31:33 -070095 auto directory = path.parent_path();
96 // convert to 0 based
97 size_t index = std::stoul(indexStr) - 1;
James Feist08aec6f2019-01-30 16:17:04 -080098
James Feistc71c1192019-09-18 14:31:33 -070099 const SensorData* sensorData = nullptr;
100 const std::string* interfacePath = nullptr;
101 const std::pair<
102 std::string,
103 boost::container::flat_map<std::string, BasicVariantType>>*
104 baseConfiguration;
105 for (const std::pair<sdbusplus::message::object_path,
106 SensorData>& sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700107 {
James Feistc71c1192019-09-18 14:31:33 -0700108 // clear it out each loop
109 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700110
James Feistc71c1192019-09-18 14:31:33 -0700111 // find base configuration
112 for (const char* type : sensorTypes)
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700113 {
James Feistc71c1192019-09-18 14:31:33 -0700114 auto sensorBase = sensor.second.find(type);
115 if (sensorBase != sensor.second.end())
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700116 {
James Feistc71c1192019-09-18 14:31:33 -0700117 baseConfiguration = &(*sensorBase);
118 break;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700119 }
120 }
James Feistc71c1192019-09-18 14:31:33 -0700121 if (baseConfiguration == nullptr)
122 {
123 continue;
124 }
125 auto findIndex = baseConfiguration->second.find("Index");
126 if (findIndex == baseConfiguration->second.end())
127 {
128 std::cerr << "Base configuration missing Index"
129 << baseConfiguration->first << "\n";
130 continue;
131 }
132
133 unsigned int number = std::visit(
134 VariantToUnsignedIntVisitor(), findIndex->second);
135
136 if (number != index)
137 {
138 continue;
139 }
140
141 sensorData = &(sensor.second);
142 interfacePath = &(sensor.first.str);
143 break;
144 }
145 if (sensorData == nullptr)
146 {
147 std::cerr << "failed to find match for " << path.string()
148 << "\n";
149 continue;
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700150 }
151
James Feistc71c1192019-09-18 14:31:33 -0700152 if (baseConfiguration == nullptr)
153 {
154 std::cerr << "error finding base configuration for"
155 << path.string() << "\n";
156 continue;
157 }
Jae Hyun Yoo12bbae02019-07-22 17:24:09 -0700158
James Feistc71c1192019-09-18 14:31:33 -0700159 auto findSensorName = baseConfiguration->second.find("Name");
160 if (findSensorName == baseConfiguration->second.end())
161 {
162 std::cerr << "could not determine configuration name for "
163 << path.string() << "\n";
164 continue;
165 }
166 std::string sensorName =
167 std::get<std::string>(findSensorName->second);
168
169 // on rescans, only update sensors we were signaled by
170 auto findSensor = sensors.find(sensorName);
171 if (!firstScan && findSensor != sensors.end())
172 {
173 bool found = false;
174 for (auto it = sensorsChanged->begin();
175 it != sensorsChanged->end(); it++)
176 {
177 if (boost::ends_with(*it, findSensor->second->name))
178 {
179 sensorsChanged->erase(it);
180 findSensor->second = nullptr;
181 found = true;
182 break;
183 }
184 }
185 if (!found)
186 {
187 continue;
188 }
189 }
190 std::vector<thresholds::Threshold> sensorThresholds;
191 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
192 {
193 std::cerr << "error populating thresholds for "
194 << sensorName << "\n";
195 }
196
197 auto findScaleFactor =
198 baseConfiguration->second.find("ScaleFactor");
199 float scaleFactor = 1.0;
200 if (findScaleFactor != baseConfiguration->second.end())
201 {
202 scaleFactor = std::visit(VariantToFloatVisitor(),
203 findScaleFactor->second);
204 }
205
206 auto findPowerOn = baseConfiguration->second.find("PowerState");
207 PowerState readState = PowerState::always;
208 if (findPowerOn != baseConfiguration->second.end())
209 {
210 std::string powerState = std::visit(
211 VariantToStringVisitor(), findPowerOn->second);
212 setReadState(powerState, readState);
213 }
214
215 auto findCPU = baseConfiguration->second.find("CPURequired");
216 if (findCPU != baseConfiguration->second.end())
217 {
218 size_t index =
219 std::visit(VariantToIntVisitor(), findCPU->second);
220 auto presenceFind = cpuPresence.find(index);
221 if (presenceFind == cpuPresence.end())
222 {
223 continue; // no such cpu
224 }
225 if (!presenceFind->second)
226 {
227 continue; // cpu not installed
228 }
229 }
230
231 auto& sensor = sensors[sensorName];
232 sensor = nullptr;
233
234 std::optional<BridgeGpio> bridgeGpio;
235 for (const SensorBaseConfiguration& suppConfig : *sensorData)
236 {
237 if (suppConfig.first.find("BridgeGpio") !=
238 std::string::npos)
239 {
240 auto findName = suppConfig.second.find("Name");
241 if (findName != suppConfig.second.end())
242 {
243 std::string gpioName = std::visit(
244 VariantToStringVisitor(), findName->second);
245
246 int polarity = gpiod::line::ACTIVE_HIGH;
247 auto findPolarity =
248 suppConfig.second.find("Polarity");
249 if (findPolarity != suppConfig.second.end())
250 {
251 if (std::string("Low") ==
252 std::visit(VariantToStringVisitor(),
253 findPolarity->second))
254 {
255 polarity = gpiod::line::ACTIVE_LOW;
256 }
257 }
258 bridgeGpio = BridgeGpio(gpioName, polarity);
259 }
260
261 break;
262 }
263 }
264
265 sensor = std::make_unique<ADCSensor>(
266 path.string(), objectServer, dbusConnection, io, sensorName,
267 std::move(sensorThresholds), scaleFactor, readState,
268 *interfacePath, std::move(bridgeGpio));
269 }
270 }));
271
272 getter->getConfiguration(
273 std::vector<std::string>{sensorTypes.begin(), sensorTypes.end()});
James Feist6714a252018-09-10 15:26:18 -0700274}
275
James Feistb6c0b912019-07-09 12:21:44 -0700276int main()
James Feist6714a252018-09-10 15:26:18 -0700277{
278 boost::asio::io_service io;
279 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
280 systemBus->request_name("xyz.openbmc_project.ADCSensor");
281 sdbusplus::asio::object_server objectServer(systemBus);
282 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
283 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
284 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
285 std::make_unique<boost::container::flat_set<std::string>>();
286
287 io.post([&]() {
288 createSensors(io, objectServer, sensors, systemBus, nullptr);
289 });
290
291 boost::asio::deadline_timer filterTimer(io);
292 std::function<void(sdbusplus::message::message&)> eventHandler =
293 [&](sdbusplus::message::message& message) {
294 if (message.is_method_error())
295 {
296 std::cerr << "callback method error\n";
297 return;
298 }
299 sensorsChanged->insert(message.get_path());
300 // this implicitly cancels the timer
301 filterTimer.expires_from_now(boost::posix_time::seconds(1));
302
303 filterTimer.async_wait([&](const boost::system::error_code& ec) {
304 if (ec == boost::asio::error::operation_aborted)
305 {
306 /* we were canceled*/
307 return;
308 }
309 else if (ec)
310 {
311 std::cerr << "timer error\n";
312 return;
313 }
314 createSensors(io, objectServer, sensors, systemBus,
315 sensorsChanged);
316 });
317 };
318
James Feistb83bb2b2019-05-31 12:31:23 -0700319 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
320 [&](sdbusplus::message::message& message) {
321 std::string path = message.get_path();
322 boost::to_lower(path);
323
324 if (path.rfind("cpu") == std::string::npos)
325 {
326 return; // not interested
327 }
328 size_t index = 0;
329 try
330 {
331 index = std::stoi(path.substr(path.size() - 1));
332 }
333 catch (std::invalid_argument&)
334 {
335 std::cerr << "Found invalid path " << path << "\n";
336 return;
337 }
338
339 std::string objectName;
340 boost::container::flat_map<std::string, std::variant<bool>> values;
341 message.read(objectName, values);
342 auto findPresence = values.find("Present");
343 if (findPresence != values.end())
344 {
345 cpuPresence[index] = std::get<bool>(findPresence->second);
346 }
347
348 // this implicitly cancels the timer
349 filterTimer.expires_from_now(boost::posix_time::seconds(1));
350
351 filterTimer.async_wait([&](const boost::system::error_code& ec) {
352 if (ec == boost::asio::error::operation_aborted)
353 {
354 /* we were canceled*/
355 return;
356 }
357 else if (ec)
358 {
359 std::cerr << "timer error\n";
360 return;
361 }
362 createSensors(io, objectServer, sensors, systemBus, {});
363 });
364 };
365
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700366 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700367 {
368 auto match = std::make_unique<sdbusplus::bus::match::match>(
369 static_cast<sdbusplus::bus::bus&>(*systemBus),
370 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700371 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700372 eventHandler);
373 matches.emplace_back(std::move(match));
374 }
James Feistb83bb2b2019-05-31 12:31:23 -0700375 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
376 static_cast<sdbusplus::bus::bus&>(*systemBus),
377 "type='signal',member='PropertiesChanged',path_namespace='" +
378 std::string(cpuInventoryPath) +
379 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
380 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700381
382 io.run();
383}