blob: 7cb31d0ceff216659ffbfba12128db3ba4d0bf0b [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{
67 bool firstScan = sensorsChanged == nullptr;
68 // use new data the first time, then refresh
69 ManagedObjectType sensorConfigurations;
70 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070071 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070072 {
73 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
74 useCache))
75 {
76 std::cerr << "error communicating to entity manager\n";
77 return;
78 }
79 useCache = true;
80 }
81 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070082 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070083 {
84 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 {
James Feist08aec6f2019-01-30 16:17:04 -080092 if (!isAdc(path.parent_path()))
93 {
94 continue;
95 }
James Feist6714a252018-09-10 15:26:18 -070096 std::smatch match;
97 std::string pathStr = path.string();
98
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070099 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -0700100 std::string indexStr = *(match.begin() + 1);
101
102 auto directory = path.parent_path();
103 // convert to 0 based
104 size_t index = std::stoul(indexStr) - 1;
James Feist6714a252018-09-10 15:26:18 -0700105
106 const SensorData* sensorData = nullptr;
107 const std::string* interfacePath = nullptr;
James Feist08aec6f2019-01-30 16:17:04 -0800108 const std::pair<std::string, boost::container::flat_map<
109 std::string, BasicVariantType>>*
110 baseConfiguration;
James Feist6714a252018-09-10 15:26:18 -0700111 for (const std::pair<sdbusplus::message::object_path, SensorData>&
112 sensor : sensorConfigurations)
113 {
James Feist08aec6f2019-01-30 16:17:04 -0800114 // clear it out each loop
115 baseConfiguration = nullptr;
116
117 // find base configuration
118 for (const char* type : sensorTypes)
119 {
120 auto sensorBase = sensor.second.find(type);
121 if (sensorBase != sensor.second.end())
122 {
123 baseConfiguration = &(*sensorBase);
124 break;
125 }
126 }
127 if (baseConfiguration == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700128 {
129 continue;
130 }
James Feist08aec6f2019-01-30 16:17:04 -0800131 auto findIndex = baseConfiguration->second.find("Index");
132 if (findIndex == baseConfiguration->second.end())
133 {
134 std::cerr << "Base configuration missing Index"
135 << baseConfiguration->first << "\n";
136 continue;
137 }
138
James Feist3eb82622019-02-08 13:10:22 -0800139 unsigned int number =
140 std::visit(VariantToUnsignedIntVisitor(), findIndex->second);
James Feist08aec6f2019-01-30 16:17:04 -0800141
142 if (number != index)
143 {
144 continue;
145 }
146
James Feist6714a252018-09-10 15:26:18 -0700147 sensorData = &(sensor.second);
148 interfacePath = &(sensor.first.str);
149 break;
150 }
151 if (sensorData == nullptr)
152 {
James Feist08aec6f2019-01-30 16:17:04 -0800153 std::cerr << "failed to find match for " << path.string() << "\n";
James Feist6714a252018-09-10 15:26:18 -0700154 continue;
155 }
James Feist6714a252018-09-10 15:26:18 -0700156
157 if (baseConfiguration == nullptr)
158 {
James Feist08aec6f2019-01-30 16:17:04 -0800159 std::cerr << "error finding base configuration for" << path.string()
James Feist6714a252018-09-10 15:26:18 -0700160 << "\n";
161 continue;
162 }
163
164 auto findSensorName = baseConfiguration->second.find("Name");
165 if (findSensorName == baseConfiguration->second.end())
166 {
167 std::cerr << "could not determine configuration name for "
James Feist08aec6f2019-01-30 16:17:04 -0800168 << path.string() << "\n";
James Feist6714a252018-09-10 15:26:18 -0700169 continue;
170 }
James Feist3eb82622019-02-08 13:10:22 -0800171 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700172
173 // on rescans, only update sensors we were signaled by
174 auto findSensor = sensors.find(sensorName);
175 if (!firstScan && findSensor != sensors.end())
176 {
177 bool found = false;
178 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
179 it++)
180 {
181 if (boost::ends_with(*it, findSensor->second->name))
182 {
183 sensorsChanged->erase(it);
184 findSensor->second = nullptr;
185 found = true;
186 break;
187 }
188 }
189 if (!found)
190 {
191 continue;
192 }
193 }
194 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700195 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700196 {
197 std::cerr << "error populating thresholds for " << sensorName
198 << "\n";
199 }
200
James Feist757c9db2018-09-12 12:27:25 -0700201 auto findScaleFactor = baseConfiguration->second.find("ScaleFactor");
James Feist6714a252018-09-10 15:26:18 -0700202 float scaleFactor = 1.0;
203 if (findScaleFactor != baseConfiguration->second.end())
204 {
James Feist3eb82622019-02-08 13:10:22 -0800205 scaleFactor =
206 std::visit(VariantToFloatVisitor(), findScaleFactor->second);
James Feist6714a252018-09-10 15:26:18 -0700207 }
James Feist71d31b22019-01-02 16:57:54 -0800208
209 auto findPowerOn = baseConfiguration->second.find("PowerState");
210 PowerState readState = PowerState::always;
211 if (findPowerOn != baseConfiguration->second.end())
212 {
James Feist3eb82622019-02-08 13:10:22 -0800213 std::string powerState =
214 std::visit(VariantToStringVisitor(), findPowerOn->second);
James Feistfc94b212019-02-06 16:14:51 -0800215 setReadState(powerState, readState);
James Feist71d31b22019-01-02 16:57:54 -0800216 }
217
James Feistb83bb2b2019-05-31 12:31:23 -0700218 auto findCPU = baseConfiguration->second.find("CPURequired");
219 if (findCPU != baseConfiguration->second.end())
220 {
221 size_t index = std::visit(VariantToIntVisitor(), findCPU->second);
222 auto presenceFind = cpuPresence.find(index);
223 if (presenceFind == cpuPresence.end())
224 {
225 continue; // no such cpu
226 }
227 if (!presenceFind->second)
228 {
229 continue; // cpu not installed
230 }
231 }
232
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400233 auto findBridgeGpio = baseConfiguration->second.find("BridgeGpio");
234 std::optional<int> gpioNum;
235
236 if (findBridgeGpio != baseConfiguration->second.end())
237 {
238 int gpioPin =
239 std::visit(VariantToIntVisitor(), findBridgeGpio->second);
240 gpioNum = static_cast<std::optional<int>>(gpioPin);
241 }
James Feistb83bb2b2019-05-31 12:31:23 -0700242 auto& sensor = sensors[sensorName];
243 sensor = nullptr;
244 sensor = std::make_unique<ADCSensor>(
James Feist6714a252018-09-10 15:26:18 -0700245 path.string(), objectServer, dbusConnection, io, sensorName,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400246 std::move(sensorThresholds), scaleFactor, readState, *interfacePath,
247 gpioNum);
James Feist6714a252018-09-10 15:26:18 -0700248 }
249}
250
251int main(int argc, char** argv)
252{
253 boost::asio::io_service io;
254 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
255 systemBus->request_name("xyz.openbmc_project.ADCSensor");
256 sdbusplus::asio::object_server objectServer(systemBus);
257 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
258 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
259 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
260 std::make_unique<boost::container::flat_set<std::string>>();
261
262 io.post([&]() {
263 createSensors(io, objectServer, sensors, systemBus, nullptr);
264 });
265
266 boost::asio::deadline_timer filterTimer(io);
267 std::function<void(sdbusplus::message::message&)> eventHandler =
268 [&](sdbusplus::message::message& message) {
269 if (message.is_method_error())
270 {
271 std::cerr << "callback method error\n";
272 return;
273 }
274 sensorsChanged->insert(message.get_path());
275 // this implicitly cancels the timer
276 filterTimer.expires_from_now(boost::posix_time::seconds(1));
277
278 filterTimer.async_wait([&](const boost::system::error_code& ec) {
279 if (ec == boost::asio::error::operation_aborted)
280 {
281 /* we were canceled*/
282 return;
283 }
284 else if (ec)
285 {
286 std::cerr << "timer error\n";
287 return;
288 }
289 createSensors(io, objectServer, sensors, systemBus,
290 sensorsChanged);
291 });
292 };
293
James Feistb83bb2b2019-05-31 12:31:23 -0700294 std::function<void(sdbusplus::message::message&)> cpuPresenceHandler =
295 [&](sdbusplus::message::message& message) {
296 std::string path = message.get_path();
297 boost::to_lower(path);
298
299 if (path.rfind("cpu") == std::string::npos)
300 {
301 return; // not interested
302 }
303 size_t index = 0;
304 try
305 {
306 index = std::stoi(path.substr(path.size() - 1));
307 }
308 catch (std::invalid_argument&)
309 {
310 std::cerr << "Found invalid path " << path << "\n";
311 return;
312 }
313
314 std::string objectName;
315 boost::container::flat_map<std::string, std::variant<bool>> values;
316 message.read(objectName, values);
317 auto findPresence = values.find("Present");
318 if (findPresence != values.end())
319 {
320 cpuPresence[index] = std::get<bool>(findPresence->second);
321 }
322
323 // this implicitly cancels the timer
324 filterTimer.expires_from_now(boost::posix_time::seconds(1));
325
326 filterTimer.async_wait([&](const boost::system::error_code& ec) {
327 if (ec == boost::asio::error::operation_aborted)
328 {
329 /* we were canceled*/
330 return;
331 }
332 else if (ec)
333 {
334 std::cerr << "timer error\n";
335 return;
336 }
337 createSensors(io, objectServer, sensors, systemBus, {});
338 });
339 };
340
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700341 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700342 {
343 auto match = std::make_unique<sdbusplus::bus::match::match>(
344 static_cast<sdbusplus::bus::bus&>(*systemBus),
345 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700346 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700347 eventHandler);
348 matches.emplace_back(std::move(match));
349 }
James Feistb83bb2b2019-05-31 12:31:23 -0700350 matches.emplace_back(std::make_unique<sdbusplus::bus::match::match>(
351 static_cast<sdbusplus::bus::bus&>(*systemBus),
352 "type='signal',member='PropertiesChanged',path_namespace='" +
353 std::string(cpuInventoryPath) +
354 "',arg0namespace='xyz.openbmc_project.Inventory.Item'",
355 cpuPresenceHandler));
James Feist6714a252018-09-10 15:26:18 -0700356
357 io.run();
358}