blob: b3fa9692d99a06fad6af0ae69350300f8f27c227 [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 <HwmonTempSensor.hpp>
18#include <Utils.hpp>
James Feist6714a252018-09-10 15:26:18 -070019#include <boost/algorithm/string/predicate.hpp>
20#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070021#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070022#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070023#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/asio/object_server.hpp>
25#include <sdbusplus/bus/match.hpp>
26
27#include <array>
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>
James Feist6714a252018-09-10 15:26:18 -070032#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070033#include <stdexcept>
34#include <string>
35#include <utility>
36#include <variant>
37#include <vector>
James Feist6714a252018-09-10 15:26:18 -070038
Jeff Lin87bc67f2020-12-04 20:58:01 +080039static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070040
James Feistcf3bce62019-01-08 10:07:19 -080041namespace fs = std::filesystem;
Brandon Kim66558232021-11-09 16:53:08 -080042static auto sensorTypes{
43 std::to_array<const char*>({"xyz.openbmc_project.Configuration.EMC1412",
44 "xyz.openbmc_project.Configuration.EMC1413",
45 "xyz.openbmc_project.Configuration.EMC1414",
46 "xyz.openbmc_project.Configuration.MAX31725",
47 "xyz.openbmc_project.Configuration.MAX31730",
48 "xyz.openbmc_project.Configuration.MAX6581",
49 "xyz.openbmc_project.Configuration.MAX6654",
50 "xyz.openbmc_project.Configuration.NCT7802",
51 "xyz.openbmc_project.Configuration.SBTSI",
52 "xyz.openbmc_project.Configuration.LM95234",
53 "xyz.openbmc_project.Configuration.TMP112",
54 "xyz.openbmc_project.Configuration.TMP175",
55 "xyz.openbmc_project.Configuration.TMP421",
56 "xyz.openbmc_project.Configuration.TMP441",
57 "xyz.openbmc_project.Configuration.LM75A",
58 "xyz.openbmc_project.Configuration.TMP75",
hsanganidaf6f562021-11-30 12:17:37 +053059 "xyz.openbmc_project.Configuration.W83773G",
60 "xyz.openbmc_project.Configuration.JC42"})};
James Feist6714a252018-09-10 15:26:18 -070061
62void createSensors(
63 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080064 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070065 sensors,
66 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070067 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -070068 sensorsChanged)
69{
James Feistdf515152019-09-18 16:40:40 -070070 auto getter = std::make_shared<GetSensorConfiguration>(
71 dbusConnection,
Ed Tanous8a17c302021-09-02 15:07:11 -070072 [&io, &objectServer, &sensors, &dbusConnection,
73 sensorsChanged](const ManagedObjectType& sensorConfigurations) {
James Feistdf515152019-09-18 16:40:40 -070074 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070075
James Feistdf515152019-09-18 16:40:40 -070076 std::vector<fs::path> paths;
77 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
78 paths))
James Feist37266ca2018-10-15 15:56:28 -070079 {
James Feistdf515152019-09-18 16:40:40 -070080 std::cerr << "No temperature sensors in system\n";
81 return;
82 }
83
84 boost::container::flat_set<std::string> directories;
85
86 // iterate through all found temp sensors, and try to match them
87 // with configuration
88 for (auto& path : paths)
89 {
90 std::smatch match;
James Feistdf515152019-09-18 16:40:40 -070091 auto directory = path.parent_path();
92
93 auto ret = directories.insert(directory.string());
94 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070095 {
James Feistdf515152019-09-18 16:40:40 -070096 continue; // already searched this path
97 }
98
99 fs::path device = directory / "device";
100 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700101 auto findHyphen = deviceName.find('-');
James Feistdf515152019-09-18 16:40:40 -0700102 if (findHyphen == std::string::npos)
103 {
104 std::cerr << "found bad device " << deviceName << "\n";
105 continue;
106 }
107 std::string busStr = deviceName.substr(0, findHyphen);
108 std::string addrStr = deviceName.substr(findHyphen + 1);
109
110 size_t bus = 0;
111 size_t addr = 0;
112 try
113 {
114 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700115 addr = std::stoi(addrStr, nullptr, 16);
James Feistdf515152019-09-18 16:40:40 -0700116 }
Patrick Williams26601e82021-10-06 12:43:25 -0500117 catch (const std::invalid_argument&)
James Feistdf515152019-09-18 16:40:40 -0700118 {
119 continue;
120 }
121 const SensorData* sensorData = nullptr;
122 const std::string* interfacePath = nullptr;
123 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800124 const SensorBaseConfiguration* baseConfiguration = nullptr;
125 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700126
127 for (const std::pair<sdbusplus::message::object_path,
128 SensorData>& sensor : sensorConfigurations)
129 {
130 sensorData = &(sensor.second);
131 for (const char* type : sensorTypes)
132 {
133 auto sensorBase = sensorData->find(type);
134 if (sensorBase != sensorData->end())
135 {
136 baseConfiguration = &(*sensorBase);
137 sensorType = type;
138 break;
139 }
140 }
141 if (baseConfiguration == nullptr)
142 {
143 std::cerr << "error finding base configuration for "
144 << deviceName << "\n";
145 continue;
146 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800147 baseConfigMap = &baseConfiguration->second;
148 auto configurationBus = baseConfigMap->find("Bus");
149 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700150
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800151 if (configurationBus == baseConfigMap->end() ||
152 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700153 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800154 std::cerr << "error finding bus or address in "
155 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700156 continue;
157 }
158
159 if (std::get<uint64_t>(configurationBus->second) != bus ||
160 std::get<uint64_t>(configurationAddress->second) !=
161 addr)
162 {
163 continue;
164 }
165
166 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700167 break;
168 }
James Feistdf515152019-09-18 16:40:40 -0700169 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700170 {
James Feistdf515152019-09-18 16:40:40 -0700171 std::cerr << "failed to find match for " << deviceName
172 << "\n";
173 continue;
James Feist6714a252018-09-10 15:26:18 -0700174 }
James Feist6714a252018-09-10 15:26:18 -0700175
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800176 auto findSensorName = baseConfigMap->find("Name");
177 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700178 {
179 std::cerr << "could not determine configuration name for "
180 << deviceName << "\n";
181 continue;
182 }
183 std::string sensorName =
184 std::get<std::string>(findSensorName->second);
185 // on rescans, only update sensors we were signaled by
186 auto findSensor = sensors.find(sensorName);
187 if (!firstScan && findSensor != sensors.end())
188 {
189 bool found = false;
Bruce Mitchelld653b752021-08-23 13:09:00 -0500190 auto it = sensorsChanged->begin();
191 while (it != sensorsChanged->end())
James Feistdf515152019-09-18 16:40:40 -0700192 {
193 if (boost::ends_with(*it, findSensor->second->name))
194 {
Bruce Mitchelld653b752021-08-23 13:09:00 -0500195 it = sensorsChanged->erase(it);
James Feistdf515152019-09-18 16:40:40 -0700196 findSensor->second = nullptr;
197 found = true;
198 break;
199 }
Bruce Mitchelld653b752021-08-23 13:09:00 -0500200 ++it;
James Feistdf515152019-09-18 16:40:40 -0700201 }
202 if (!found)
203 {
204 continue;
205 }
206 }
Matt Spinler5636d522021-03-17 14:52:18 -0500207
James Feistdf515152019-09-18 16:40:40 -0700208 std::vector<thresholds::Threshold> sensorThresholds;
Matt Spinler5636d522021-03-17 14:52:18 -0500209 int index = 1;
210
211 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
212 nullptr, &index))
James Feistdf515152019-09-18 16:40:40 -0700213 {
214 std::cerr << "error populating thresholds for "
Matt Spinler5636d522021-03-17 14:52:18 -0500215 << sensorName << " index 1\n";
James Feistdf515152019-09-18 16:40:40 -0700216 }
Jeff Lin87bc67f2020-12-04 20:58:01 +0800217
218 auto findPollRate = baseConfiguration->second.find("PollRate");
219 float pollRate = pollRateDefault;
220 if (findPollRate != baseConfiguration->second.end())
221 {
222 pollRate = std::visit(VariantToFloatVisitor(),
223 findPollRate->second);
224 if (pollRate <= 0.0f)
225 {
226 pollRate = pollRateDefault; // polling time too short
227 }
228 }
229
James Feistf9b01b62020-01-29 15:21:58 -0800230 auto findPowerOn = baseConfiguration->second.find("PowerState");
231 PowerState readState = PowerState::always;
232 if (findPowerOn != baseConfiguration->second.end())
233 {
234 std::string powerState = std::visit(
235 VariantToStringVisitor(), findPowerOn->second);
236 setReadState(powerState, readState);
237 }
Jason Ling100c20b2020-08-11 14:50:33 -0700238
239 auto permitSet = getPermitSet(*baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800240 auto& sensor = sensors[sensorName];
241 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700242 auto hwmonFile = getFullHwmonFilePath(directory.string(),
243 "temp1", permitSet);
244 if (hwmonFile)
245 {
246 sensor = std::make_shared<HwmonTempSensor>(
247 *hwmonFile, sensorType, objectServer, dbusConnection,
Jeff Lin87bc67f2020-12-04 20:58:01 +0800248 io, sensorName, std::move(sensorThresholds), pollRate,
Jason Ling100c20b2020-08-11 14:50:33 -0700249 *interfacePath, readState);
250 sensor->setupRead();
251 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800252 // Looking for keys like "Name1" for temp2_input,
253 // "Name2" for temp3_input, etc.
254 int i = 0;
255 while (true)
James Feistdf515152019-09-18 16:40:40 -0700256 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800257 ++i;
258 auto findKey =
Jason Ling8b8bcc82020-08-04 19:33:22 -0700259 baseConfigMap->find("Name" + std::to_string(i));
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800260 if (findKey == baseConfigMap->end())
261 {
262 break;
263 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800264 std::string sensorName =
265 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700266 hwmonFile = getFullHwmonFilePath(
267 directory.string(), "temp" + std::to_string(i + 1),
268 permitSet);
269 if (hwmonFile)
270 {
Matt Spinler5636d522021-03-17 14:52:18 -0500271 // To look up thresholds for these additional sensors,
272 // match on the Index property in the threshold data
273 // where the index comes from the sysfs file we're on,
274 // i.e. index = 2 for temp2_input.
275 int index = i + 1;
276 std::vector<thresholds::Threshold> thresholds;
277
278 if (!parseThresholdsFromConfig(*sensorData, thresholds,
279 nullptr, &index))
280 {
281 std::cerr << "error populating thresholds for "
282 << sensorName << " index " << index
283 << "\n";
284 }
285
Jason Ling100c20b2020-08-11 14:50:33 -0700286 auto& sensor = sensors[sensorName];
287 sensor = nullptr;
288 sensor = std::make_shared<HwmonTempSensor>(
289 *hwmonFile, sensorType, objectServer,
290 dbusConnection, io, sensorName,
Matt Spinler5636d522021-03-17 14:52:18 -0500291 std::move(thresholds), pollRate, *interfacePath,
292 readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700293 sensor->setupRead();
294 }
James Feistdf515152019-09-18 16:40:40 -0700295 }
James Feistdf515152019-09-18 16:40:40 -0700296 }
Ed Tanous8a17c302021-09-02 15:07:11 -0700297 });
James Feistdf515152019-09-18 16:40:40 -0700298 getter->getConfiguration(
299 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700300}
301
Matt Spinler20bf2c12021-09-14 11:07:07 -0500302void interfaceRemoved(
303 sdbusplus::message::message& message,
304 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
305 sensors)
306{
307 if (message.is_method_error())
308 {
309 std::cerr << "interfacesRemoved callback method error\n";
310 return;
311 }
312
313 sdbusplus::message::object_path path;
314 std::vector<std::string> interfaces;
315
316 message.read(path, interfaces);
317
318 // If the xyz.openbmc_project.Confguration.X interface was removed
319 // for one or more sensors, delete those sensor objects.
320 auto sensorIt = sensors.begin();
321 while (sensorIt != sensors.end())
322 {
323 if ((sensorIt->second->configurationPath == path) &&
324 (std::find(interfaces.begin(), interfaces.end(),
325 sensorIt->second->objectType) != interfaces.end()))
326 {
327 sensorIt = sensors.erase(sensorIt);
328 }
329 else
330 {
331 sensorIt++;
332 }
333 }
334}
335
James Feistb6c0b912019-07-09 12:21:44 -0700336int main()
James Feist6714a252018-09-10 15:26:18 -0700337{
338 boost::asio::io_service io;
339 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
340 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
341 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800342 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700343 sensors;
344 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700345 auto sensorsChanged =
346 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700347
348 io.post([&]() {
349 createSensors(io, objectServer, sensors, systemBus, nullptr);
350 });
351
352 boost::asio::deadline_timer filterTimer(io);
353 std::function<void(sdbusplus::message::message&)> eventHandler =
354 [&](sdbusplus::message::message& message) {
355 if (message.is_method_error())
356 {
357 std::cerr << "callback method error\n";
358 return;
359 }
360 sensorsChanged->insert(message.get_path());
361 // this implicitly cancels the timer
362 filterTimer.expires_from_now(boost::posix_time::seconds(1));
363
364 filterTimer.async_wait([&](const boost::system::error_code& ec) {
365 if (ec == boost::asio::error::operation_aborted)
366 {
367 /* we were canceled*/
368 return;
369 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700370 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700371 {
372 std::cerr << "timer error\n";
373 return;
374 }
375 createSensors(io, objectServer, sensors, systemBus,
376 sensorsChanged);
377 });
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 }
389
Bruce Lee1263c3d2021-06-04 15:16:33 +0800390 setupManufacturingModeMatch(*systemBus);
Matt Spinler20bf2c12021-09-14 11:07:07 -0500391
392 // Watch for entity-manager to remove configuration interfaces
393 // so the corresponding sensors can be removed.
394 auto ifaceRemovedMatch = std::make_unique<sdbusplus::bus::match::match>(
395 static_cast<sdbusplus::bus::bus&>(*systemBus),
396 "type='signal',member='InterfacesRemoved',arg0path='" +
397 std::string(inventoryPath) + "/'",
398 [&sensors](sdbusplus::message::message& msg) {
399 interfaceRemoved(msg, sensors);
400 });
401
402 matches.emplace_back(std::move(ifaceRemovedMatch));
403
James Feist6714a252018-09-10 15:26:18 -0700404 io.run();
405}