blob: 79eab208ad72cbdad39592f815a1648310a73c04 [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;
Gilbert Chen381636e2020-11-03 01:39:56 +080043static constexpr std::array<const char*, 13> sensorTypes = {
44 "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",
Josh Lehan8fb0a012020-06-26 19:28:16 -070051 "xyz.openbmc_project.Configuration.SBTSI",
John Wang55ab2af2019-04-18 14:22:31 +080052 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070053 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080054 "xyz.openbmc_project.Configuration.TMP421",
55 "xyz.openbmc_project.Configuration.TMP441",
56 "xyz.openbmc_project.Configuration.TMP75"};
James Feist6714a252018-09-10 15:26:18 -070057
58void createSensors(
59 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080060 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070061 sensors,
62 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070063 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -070064 sensorsChanged)
65{
James Feistdf515152019-09-18 16:40:40 -070066 auto getter = std::make_shared<GetSensorConfiguration>(
67 dbusConnection,
68 std::move([&io, &objectServer, &sensors, &dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070069 sensorsChanged](
James Feistdf515152019-09-18 16:40:40 -070070 const ManagedObjectType& sensorConfigurations) {
71 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070072
James Feistdf515152019-09-18 16:40:40 -070073 std::vector<fs::path> paths;
74 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
75 paths))
James Feist37266ca2018-10-15 15:56:28 -070076 {
James Feistdf515152019-09-18 16:40:40 -070077 std::cerr << "No temperature sensors in system\n";
78 return;
79 }
80
81 boost::container::flat_set<std::string> directories;
82
83 // iterate through all found temp sensors, and try to match them
84 // with configuration
85 for (auto& path : paths)
86 {
87 std::smatch match;
88 const std::string& pathStr = path.string();
89 auto directory = path.parent_path();
90
91 auto ret = directories.insert(directory.string());
92 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070093 {
James Feistdf515152019-09-18 16:40:40 -070094 continue; // already searched this path
95 }
96
97 fs::path device = directory / "device";
98 std::string deviceName = fs::canonical(device).stem();
Ed Tanous8a57ec02020-10-09 12:46:52 -070099 auto findHyphen = deviceName.find('-');
James Feistdf515152019-09-18 16:40:40 -0700100 if (findHyphen == std::string::npos)
101 {
102 std::cerr << "found bad device " << deviceName << "\n";
103 continue;
104 }
105 std::string busStr = deviceName.substr(0, findHyphen);
106 std::string addrStr = deviceName.substr(findHyphen + 1);
107
108 size_t bus = 0;
109 size_t addr = 0;
110 try
111 {
112 bus = std::stoi(busStr);
Ed Tanous8a57ec02020-10-09 12:46:52 -0700113 addr = std::stoi(addrStr, nullptr, 16);
James Feistdf515152019-09-18 16:40:40 -0700114 }
115 catch (std::invalid_argument&)
116 {
117 continue;
118 }
119 const SensorData* sensorData = nullptr;
120 const std::string* interfacePath = nullptr;
121 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800122 const SensorBaseConfiguration* baseConfiguration = nullptr;
123 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700124
125 for (const std::pair<sdbusplus::message::object_path,
126 SensorData>& sensor : sensorConfigurations)
127 {
128 sensorData = &(sensor.second);
129 for (const char* type : sensorTypes)
130 {
131 auto sensorBase = sensorData->find(type);
132 if (sensorBase != sensorData->end())
133 {
134 baseConfiguration = &(*sensorBase);
135 sensorType = type;
136 break;
137 }
138 }
139 if (baseConfiguration == nullptr)
140 {
141 std::cerr << "error finding base configuration for "
142 << deviceName << "\n";
143 continue;
144 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800145 baseConfigMap = &baseConfiguration->second;
146 auto configurationBus = baseConfigMap->find("Bus");
147 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700148
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800149 if (configurationBus == baseConfigMap->end() ||
150 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700151 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800152 std::cerr << "error finding bus or address in "
153 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700154 continue;
155 }
156
157 if (std::get<uint64_t>(configurationBus->second) != bus ||
158 std::get<uint64_t>(configurationAddress->second) !=
159 addr)
160 {
161 continue;
162 }
163
164 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700165 break;
166 }
James Feistdf515152019-09-18 16:40:40 -0700167 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700168 {
James Feistdf515152019-09-18 16:40:40 -0700169 std::cerr << "failed to find match for " << deviceName
170 << "\n";
171 continue;
James Feist6714a252018-09-10 15:26:18 -0700172 }
James Feist6714a252018-09-10 15:26:18 -0700173
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800174 auto findSensorName = baseConfigMap->find("Name");
175 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700176 {
177 std::cerr << "could not determine configuration name for "
178 << deviceName << "\n";
179 continue;
180 }
181 std::string sensorName =
182 std::get<std::string>(findSensorName->second);
183 // on rescans, only update sensors we were signaled by
184 auto findSensor = sensors.find(sensorName);
185 if (!firstScan && findSensor != sensors.end())
186 {
187 bool found = false;
188 for (auto it = sensorsChanged->begin();
189 it != sensorsChanged->end(); it++)
190 {
191 if (boost::ends_with(*it, findSensor->second->name))
192 {
193 sensorsChanged->erase(it);
194 findSensor->second = nullptr;
195 found = true;
196 break;
197 }
198 }
199 if (!found)
200 {
201 continue;
202 }
203 }
204 std::vector<thresholds::Threshold> sensorThresholds;
205 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
206 {
207 std::cerr << "error populating thresholds for "
208 << sensorName << "\n";
209 }
Jeff Lin87bc67f2020-12-04 20:58:01 +0800210
211 auto findPollRate = baseConfiguration->second.find("PollRate");
212 float pollRate = pollRateDefault;
213 if (findPollRate != baseConfiguration->second.end())
214 {
215 pollRate = std::visit(VariantToFloatVisitor(),
216 findPollRate->second);
217 if (pollRate <= 0.0f)
218 {
219 pollRate = pollRateDefault; // polling time too short
220 }
221 }
222
James Feistf9b01b62020-01-29 15:21:58 -0800223 auto findPowerOn = baseConfiguration->second.find("PowerState");
224 PowerState readState = PowerState::always;
225 if (findPowerOn != baseConfiguration->second.end())
226 {
227 std::string powerState = std::visit(
228 VariantToStringVisitor(), findPowerOn->second);
229 setReadState(powerState, readState);
230 }
Jason Ling100c20b2020-08-11 14:50:33 -0700231
232 auto permitSet = getPermitSet(*baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800233 auto& sensor = sensors[sensorName];
234 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700235 auto hwmonFile = getFullHwmonFilePath(directory.string(),
236 "temp1", permitSet);
237 if (hwmonFile)
238 {
239 sensor = std::make_shared<HwmonTempSensor>(
240 *hwmonFile, sensorType, objectServer, dbusConnection,
Jeff Lin87bc67f2020-12-04 20:58:01 +0800241 io, sensorName, std::move(sensorThresholds), pollRate,
Jason Ling100c20b2020-08-11 14:50:33 -0700242 *interfacePath, readState);
243 sensor->setupRead();
244 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800245 // Looking for keys like "Name1" for temp2_input,
246 // "Name2" for temp3_input, etc.
247 int i = 0;
248 while (true)
James Feistdf515152019-09-18 16:40:40 -0700249 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800250 ++i;
251 auto findKey =
Jason Ling8b8bcc82020-08-04 19:33:22 -0700252 baseConfigMap->find("Name" + std::to_string(i));
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800253 if (findKey == baseConfigMap->end())
254 {
255 break;
256 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800257 std::string sensorName =
258 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700259 hwmonFile = getFullHwmonFilePath(
260 directory.string(), "temp" + std::to_string(i + 1),
261 permitSet);
262 if (hwmonFile)
263 {
264 auto& sensor = sensors[sensorName];
265 sensor = nullptr;
266 sensor = std::make_shared<HwmonTempSensor>(
267 *hwmonFile, sensorType, objectServer,
268 dbusConnection, io, sensorName,
Jeff Lin87bc67f2020-12-04 20:58:01 +0800269 std::vector<thresholds::Threshold>(), pollRate,
Jason Ling100c20b2020-08-11 14:50:33 -0700270 *interfacePath, readState);
271 sensor->setupRead();
272 }
James Feistdf515152019-09-18 16:40:40 -0700273 }
James Feistdf515152019-09-18 16:40:40 -0700274 }
275 }));
276 getter->getConfiguration(
277 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700278}
279
James Feistb6c0b912019-07-09 12:21:44 -0700280int main()
James Feist6714a252018-09-10 15:26:18 -0700281{
282 boost::asio::io_service io;
283 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
284 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
285 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800286 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700287 sensors;
288 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700289 auto sensorsChanged =
290 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700291
292 io.post([&]() {
293 createSensors(io, objectServer, sensors, systemBus, nullptr);
294 });
295
296 boost::asio::deadline_timer filterTimer(io);
297 std::function<void(sdbusplus::message::message&)> eventHandler =
298 [&](sdbusplus::message::message& message) {
299 if (message.is_method_error())
300 {
301 std::cerr << "callback method error\n";
302 return;
303 }
304 sensorsChanged->insert(message.get_path());
305 // this implicitly cancels the timer
306 filterTimer.expires_from_now(boost::posix_time::seconds(1));
307
308 filterTimer.async_wait([&](const boost::system::error_code& ec) {
309 if (ec == boost::asio::error::operation_aborted)
310 {
311 /* we were canceled*/
312 return;
313 }
Ed Tanous8a57ec02020-10-09 12:46:52 -0700314 if (ec)
James Feist6714a252018-09-10 15:26:18 -0700315 {
316 std::cerr << "timer error\n";
317 return;
318 }
319 createSensors(io, objectServer, sensors, systemBus,
320 sensorsChanged);
321 });
322 };
323
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700324 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700325 {
326 auto match = std::make_unique<sdbusplus::bus::match::match>(
327 static_cast<sdbusplus::bus::bus&>(*systemBus),
328 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700329 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700330 eventHandler);
331 matches.emplace_back(std::move(match));
332 }
333
334 io.run();
335}