blob: 4f259dcbf81a0f850888f7934de08eba082a3857 [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
Ed Tanous8a57ec02020-10-09 12:46:52 -070039static constexpr bool debug = false;
Jeff Lin87bc67f2020-12-04 20:58:01 +080040static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070041
James Feistcf3bce62019-01-08 10:07:19 -080042namespace fs = std::filesystem;
Oskar Senft5770a6f2021-09-07 13:42:47 -040043static constexpr std::array<const char*, 17> sensorTypes = {
Gilbert Chen381636e2020-11-03 01:39:56 +080044 "xyz.openbmc_project.Configuration.EMC1412",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080045 "xyz.openbmc_project.Configuration.EMC1413",
Gilbert Chen381636e2020-11-03 01:39:56 +080046 "xyz.openbmc_project.Configuration.EMC1414",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080047 "xyz.openbmc_project.Configuration.MAX31725",
48 "xyz.openbmc_project.Configuration.MAX31730",
Jason Ling16e7af12020-06-05 17:41:01 -070049 "xyz.openbmc_project.Configuration.MAX6581",
Josh Lehan3840d0a2020-05-13 16:13:45 -070050 "xyz.openbmc_project.Configuration.MAX6654",
Oskar Senft5770a6f2021-09-07 13:42:47 -040051 "xyz.openbmc_project.Configuration.NCT7802",
Josh Lehan8fb0a012020-06-26 19:28:16 -070052 "xyz.openbmc_project.Configuration.SBTSI",
Konstantin Aladyshev5a3b3f82021-04-01 17:27:48 +030053 "xyz.openbmc_project.Configuration.LM95234",
John Wang55ab2af2019-04-18 14:22:31 +080054 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070055 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080056 "xyz.openbmc_project.Configuration.TMP421",
57 "xyz.openbmc_project.Configuration.TMP441",
Konstantin Aladyshev34fc75a2021-04-01 17:19:17 +030058 "xyz.openbmc_project.Configuration.LM75A",
Zev Weiss7ea918f2021-03-24 22:46:02 +000059 "xyz.openbmc_project.Configuration.TMP75",
60 "xyz.openbmc_project.Configuration.W83773G"};
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,
72 std::move([&io, &objectServer, &sensors, &dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070073 sensorsChanged](
James Feistdf515152019-09-18 16:40:40 -070074 const ManagedObjectType& sensorConfigurations) {
75 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070076
James Feistdf515152019-09-18 16:40:40 -070077 std::vector<fs::path> paths;
78 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
79 paths))
James Feist37266ca2018-10-15 15:56:28 -070080 {
James Feistdf515152019-09-18 16:40:40 -070081 std::cerr << "No temperature sensors in system\n";
82 return;
83 }
84
85 boost::container::flat_set<std::string> directories;
86
87 // iterate through all found temp sensors, and try to match them
88 // with configuration
89 for (auto& path : paths)
90 {
91 std::smatch match;
92 const std::string& pathStr = path.string();
93 auto directory = path.parent_path();
94
95 auto ret = directories.insert(directory.string());
96 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070097 {
James Feistdf515152019-09-18 16:40:40 -070098 continue; // already searched this path
99 }
100
101 fs::path device = directory / "device";
102 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -0700103 auto findHyphen = deviceName.find('-');
James Feistdf515152019-09-18 16:40:40 -0700104 if (findHyphen == std::string::npos)
105 {
106 std::cerr << "found bad device " << deviceName << "\n";
107 continue;
108 }
109 std::string busStr = deviceName.substr(0, findHyphen);
110 std::string addrStr = deviceName.substr(findHyphen + 1);
111
112 size_t bus = 0;
113 size_t addr = 0;
114 try
115 {
116 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700117 addr = std::stoi(addrStr, nullptr, 16);
James Feistdf515152019-09-18 16:40:40 -0700118 }
119 catch (std::invalid_argument&)
120 {
121 continue;
122 }
123 const SensorData* sensorData = nullptr;
124 const std::string* interfacePath = nullptr;
125 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800126 const SensorBaseConfiguration* baseConfiguration = nullptr;
127 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700128
129 for (const std::pair<sdbusplus::message::object_path,
130 SensorData>& sensor : sensorConfigurations)
131 {
132 sensorData = &(sensor.second);
133 for (const char* type : sensorTypes)
134 {
135 auto sensorBase = sensorData->find(type);
136 if (sensorBase != sensorData->end())
137 {
138 baseConfiguration = &(*sensorBase);
139 sensorType = type;
140 break;
141 }
142 }
143 if (baseConfiguration == nullptr)
144 {
145 std::cerr << "error finding base configuration for "
146 << deviceName << "\n";
147 continue;
148 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800149 baseConfigMap = &baseConfiguration->second;
150 auto configurationBus = baseConfigMap->find("Bus");
151 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700152
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800153 if (configurationBus == baseConfigMap->end() ||
154 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700155 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800156 std::cerr << "error finding bus or address in "
157 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700158 continue;
159 }
160
161 if (std::get<uint64_t>(configurationBus->second) != bus ||
162 std::get<uint64_t>(configurationAddress->second) !=
163 addr)
164 {
165 continue;
166 }
167
168 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700169 break;
170 }
James Feistdf515152019-09-18 16:40:40 -0700171 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700172 {
James Feistdf515152019-09-18 16:40:40 -0700173 std::cerr << "failed to find match for " << deviceName
174 << "\n";
175 continue;
James Feist6714a252018-09-10 15:26:18 -0700176 }
James Feist6714a252018-09-10 15:26:18 -0700177
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800178 auto findSensorName = baseConfigMap->find("Name");
179 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700180 {
181 std::cerr << "could not determine configuration name for "
182 << deviceName << "\n";
183 continue;
184 }
185 std::string sensorName =
186 std::get<std::string>(findSensorName->second);
187 // on rescans, only update sensors we were signaled by
188 auto findSensor = sensors.find(sensorName);
189 if (!firstScan && findSensor != sensors.end())
190 {
191 bool found = false;
Bruce Mitchelld653b752021-08-23 13:09:00 -0500192 auto it = sensorsChanged->begin();
193 while (it != sensorsChanged->end())
James Feistdf515152019-09-18 16:40:40 -0700194 {
195 if (boost::ends_with(*it, findSensor->second->name))
196 {
Bruce Mitchelld653b752021-08-23 13:09:00 -0500197 it = sensorsChanged->erase(it);
James Feistdf515152019-09-18 16:40:40 -0700198 findSensor->second = nullptr;
199 found = true;
200 break;
201 }
Bruce Mitchelld653b752021-08-23 13:09:00 -0500202 ++it;
James Feistdf515152019-09-18 16:40:40 -0700203 }
204 if (!found)
205 {
206 continue;
207 }
208 }
Matt Spinler5636d522021-03-17 14:52:18 -0500209
James Feistdf515152019-09-18 16:40:40 -0700210 std::vector<thresholds::Threshold> sensorThresholds;
Matt Spinler5636d522021-03-17 14:52:18 -0500211 int index = 1;
212
213 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds,
214 nullptr, &index))
James Feistdf515152019-09-18 16:40:40 -0700215 {
216 std::cerr << "error populating thresholds for "
Matt Spinler5636d522021-03-17 14:52:18 -0500217 << sensorName << " index 1\n";
James Feistdf515152019-09-18 16:40:40 -0700218 }
Jeff Lin87bc67f2020-12-04 20:58:01 +0800219
220 auto findPollRate = baseConfiguration->second.find("PollRate");
221 float pollRate = pollRateDefault;
222 if (findPollRate != baseConfiguration->second.end())
223 {
224 pollRate = std::visit(VariantToFloatVisitor(),
225 findPollRate->second);
226 if (pollRate <= 0.0f)
227 {
228 pollRate = pollRateDefault; // polling time too short
229 }
230 }
231
James Feistf9b01b62020-01-29 15:21:58 -0800232 auto findPowerOn = baseConfiguration->second.find("PowerState");
233 PowerState readState = PowerState::always;
234 if (findPowerOn != baseConfiguration->second.end())
235 {
236 std::string powerState = std::visit(
237 VariantToStringVisitor(), findPowerOn->second);
238 setReadState(powerState, readState);
239 }
Jason Ling100c20b2020-08-11 14:50:33 -0700240
241 auto permitSet = getPermitSet(*baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800242 auto& sensor = sensors[sensorName];
243 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700244 auto hwmonFile = getFullHwmonFilePath(directory.string(),
245 "temp1", permitSet);
246 if (hwmonFile)
247 {
248 sensor = std::make_shared<HwmonTempSensor>(
249 *hwmonFile, sensorType, objectServer, dbusConnection,
Jeff Lin87bc67f2020-12-04 20:58:01 +0800250 io, sensorName, std::move(sensorThresholds), pollRate,
Jason Ling100c20b2020-08-11 14:50:33 -0700251 *interfacePath, readState);
252 sensor->setupRead();
253 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800254 // Looking for keys like "Name1" for temp2_input,
255 // "Name2" for temp3_input, etc.
256 int i = 0;
257 while (true)
James Feistdf515152019-09-18 16:40:40 -0700258 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800259 ++i;
260 auto findKey =
Jason Ling8b8bcc82020-08-04 19:33:22 -0700261 baseConfigMap->find("Name" + std::to_string(i));
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800262 if (findKey == baseConfigMap->end())
263 {
264 break;
265 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800266 std::string sensorName =
267 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700268 hwmonFile = getFullHwmonFilePath(
269 directory.string(), "temp" + std::to_string(i + 1),
270 permitSet);
271 if (hwmonFile)
272 {
Matt Spinler5636d522021-03-17 14:52:18 -0500273 // To look up thresholds for these additional sensors,
274 // match on the Index property in the threshold data
275 // where the index comes from the sysfs file we're on,
276 // i.e. index = 2 for temp2_input.
277 int index = i + 1;
278 std::vector<thresholds::Threshold> thresholds;
279
280 if (!parseThresholdsFromConfig(*sensorData, thresholds,
281 nullptr, &index))
282 {
283 std::cerr << "error populating thresholds for "
284 << sensorName << " index " << index
285 << "\n";
286 }
287
Jason Ling100c20b2020-08-11 14:50:33 -0700288 auto& sensor = sensors[sensorName];
289 sensor = nullptr;
290 sensor = std::make_shared<HwmonTempSensor>(
291 *hwmonFile, sensorType, objectServer,
292 dbusConnection, io, sensorName,
Matt Spinler5636d522021-03-17 14:52:18 -0500293 std::move(thresholds), pollRate, *interfacePath,
294 readState);
Jason Ling100c20b2020-08-11 14:50:33 -0700295 sensor->setupRead();
296 }
James Feistdf515152019-09-18 16:40:40 -0700297 }
James Feistdf515152019-09-18 16:40:40 -0700298 }
299 }));
300 getter->getConfiguration(
301 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700302}
303
James Feistb6c0b912019-07-09 12:21:44 -0700304int main()
James Feist6714a252018-09-10 15:26:18 -0700305{
306 boost::asio::io_service io;
307 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
308 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
309 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800310 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700311 sensors;
312 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700313 auto sensorsChanged =
314 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700315
316 io.post([&]() {
317 createSensors(io, objectServer, sensors, systemBus, nullptr);
318 });
319
320 boost::asio::deadline_timer filterTimer(io);
321 std::function<void(sdbusplus::message::message&)> eventHandler =
322 [&](sdbusplus::message::message& message) {
323 if (message.is_method_error())
324 {
325 std::cerr << "callback method error\n";
326 return;
327 }
328 sensorsChanged->insert(message.get_path());
329 // this implicitly cancels the timer
330 filterTimer.expires_from_now(boost::posix_time::seconds(1));
331
332 filterTimer.async_wait([&](const boost::system::error_code& ec) {
333 if (ec == boost::asio::error::operation_aborted)
334 {
335 /* we were canceled*/
336 return;
337 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700338 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700339 {
340 std::cerr << "timer error\n";
341 return;
342 }
343 createSensors(io, objectServer, sensors, systemBus,
344 sensorsChanged);
345 });
346 };
347
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700348 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700349 {
350 auto match = std::make_unique<sdbusplus::bus::match::match>(
351 static_cast<sdbusplus::bus::bus&>(*systemBus),
352 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700353 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700354 eventHandler);
355 matches.emplace_back(std::move(match));
356 }
357
Bruce Lee1263c3d2021-06-04 15:16:33 +0800358 setupManufacturingModeMatch(*systemBus);
James Feist6714a252018-09-10 15:26:18 -0700359 io.run();
360}