blob: 8a583b93bde28829342775a409700bffa929ac7c [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "HwmonTempSensor.hpp"
18#include "Utils.hpp"
19
James Feist6714a252018-09-10 15:26:18 -070020#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070022#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070023#include <boost/container/flat_set.hpp>
James Feist38fb5982020-05-28 10:09:54 -070024#include <sdbusplus/asio/connection.hpp>
25#include <sdbusplus/asio/object_server.hpp>
26#include <sdbusplus/bus/match.hpp>
27
28#include <array>
James Feist24f02f22019-04-15 11:05:39 -070029#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070030#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070031#include <functional>
32#include <memory>
James Feist6714a252018-09-10 15:26:18 -070033#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070034#include <stdexcept>
35#include <string>
36#include <utility>
37#include <variant>
38#include <vector>
James Feist6714a252018-09-10 15:26:18 -070039
40static constexpr bool DEBUG = false;
Jeff Lin87bc67f2020-12-04 20:58:01 +080041static constexpr float pollRateDefault = 0.5;
James Feist6714a252018-09-10 15:26:18 -070042
James Feistcf3bce62019-01-08 10:07:19 -080043namespace fs = std::filesystem;
Gilbert Chen381636e2020-11-03 01:39:56 +080044static constexpr std::array<const char*, 13> sensorTypes = {
45 "xyz.openbmc_project.Configuration.EMC1412",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080046 "xyz.openbmc_project.Configuration.EMC1413",
Gilbert Chen381636e2020-11-03 01:39:56 +080047 "xyz.openbmc_project.Configuration.EMC1414",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080048 "xyz.openbmc_project.Configuration.MAX31725",
49 "xyz.openbmc_project.Configuration.MAX31730",
Jason Ling16e7af12020-06-05 17:41:01 -070050 "xyz.openbmc_project.Configuration.MAX6581",
Josh Lehan3840d0a2020-05-13 16:13:45 -070051 "xyz.openbmc_project.Configuration.MAX6654",
Josh Lehan8fb0a012020-06-26 19:28:16 -070052 "xyz.openbmc_project.Configuration.SBTSI",
John Wang55ab2af2019-04-18 14:22:31 +080053 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070054 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080055 "xyz.openbmc_project.Configuration.TMP421",
56 "xyz.openbmc_project.Configuration.TMP441",
57 "xyz.openbmc_project.Configuration.TMP75"};
James Feist6714a252018-09-10 15:26:18 -070058
59void createSensors(
60 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080061 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070062 sensors,
63 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070064 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -070065 sensorsChanged)
66{
James Feistdf515152019-09-18 16:40:40 -070067 auto getter = std::make_shared<GetSensorConfiguration>(
68 dbusConnection,
69 std::move([&io, &objectServer, &sensors, &dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070070 sensorsChanged](
James Feistdf515152019-09-18 16:40:40 -070071 const ManagedObjectType& sensorConfigurations) {
72 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070073
James Feistdf515152019-09-18 16:40:40 -070074 std::vector<fs::path> paths;
75 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
76 paths))
James Feist37266ca2018-10-15 15:56:28 -070077 {
James Feistdf515152019-09-18 16:40:40 -070078 std::cerr << "No temperature sensors in system\n";
79 return;
80 }
81
82 boost::container::flat_set<std::string> directories;
83
84 // iterate through all found temp sensors, and try to match them
85 // with configuration
86 for (auto& path : paths)
87 {
88 std::smatch match;
89 const std::string& pathStr = path.string();
90 auto directory = path.parent_path();
91
92 auto ret = directories.insert(directory.string());
93 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070094 {
James Feistdf515152019-09-18 16:40:40 -070095 continue; // already searched this path
96 }
97
98 fs::path device = directory / "device";
99 std::string deviceName = fs::canonical(device).stem();
100 auto findHyphen = deviceName.find("-");
101 if (findHyphen == std::string::npos)
102 {
103 std::cerr << "found bad device " << deviceName << "\n";
104 continue;
105 }
106 std::string busStr = deviceName.substr(0, findHyphen);
107 std::string addrStr = deviceName.substr(findHyphen + 1);
108
109 size_t bus = 0;
110 size_t addr = 0;
111 try
112 {
113 bus = std::stoi(busStr);
114 addr = std::stoi(addrStr, 0, 16);
115 }
116 catch (std::invalid_argument&)
117 {
118 continue;
119 }
120 const SensorData* sensorData = nullptr;
121 const std::string* interfacePath = nullptr;
122 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800123 const SensorBaseConfiguration* baseConfiguration = nullptr;
124 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700125
126 for (const std::pair<sdbusplus::message::object_path,
127 SensorData>& sensor : sensorConfigurations)
128 {
129 sensorData = &(sensor.second);
130 for (const char* type : sensorTypes)
131 {
132 auto sensorBase = sensorData->find(type);
133 if (sensorBase != sensorData->end())
134 {
135 baseConfiguration = &(*sensorBase);
136 sensorType = type;
137 break;
138 }
139 }
140 if (baseConfiguration == nullptr)
141 {
142 std::cerr << "error finding base configuration for "
143 << deviceName << "\n";
144 continue;
145 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800146 baseConfigMap = &baseConfiguration->second;
147 auto configurationBus = baseConfigMap->find("Bus");
148 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700149
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800150 if (configurationBus == baseConfigMap->end() ||
151 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700152 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800153 std::cerr << "error finding bus or address in "
154 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700155 continue;
156 }
157
158 if (std::get<uint64_t>(configurationBus->second) != bus ||
159 std::get<uint64_t>(configurationAddress->second) !=
160 addr)
161 {
162 continue;
163 }
164
165 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700166 break;
167 }
James Feistdf515152019-09-18 16:40:40 -0700168 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700169 {
James Feistdf515152019-09-18 16:40:40 -0700170 std::cerr << "failed to find match for " << deviceName
171 << "\n";
172 continue;
James Feist6714a252018-09-10 15:26:18 -0700173 }
James Feist6714a252018-09-10 15:26:18 -0700174
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800175 auto findSensorName = baseConfigMap->find("Name");
176 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700177 {
178 std::cerr << "could not determine configuration name for "
179 << deviceName << "\n";
180 continue;
181 }
182 std::string sensorName =
183 std::get<std::string>(findSensorName->second);
184 // on rescans, only update sensors we were signaled by
185 auto findSensor = sensors.find(sensorName);
186 if (!firstScan && findSensor != sensors.end())
187 {
188 bool found = false;
189 for (auto it = sensorsChanged->begin();
190 it != sensorsChanged->end(); it++)
191 {
192 if (boost::ends_with(*it, findSensor->second->name))
193 {
194 sensorsChanged->erase(it);
195 findSensor->second = nullptr;
196 found = true;
197 break;
198 }
199 }
200 if (!found)
201 {
202 continue;
203 }
204 }
205 std::vector<thresholds::Threshold> sensorThresholds;
206 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
207 {
208 std::cerr << "error populating thresholds for "
209 << sensorName << "\n";
210 }
Jeff Lin87bc67f2020-12-04 20:58:01 +0800211
212 auto findPollRate = baseConfiguration->second.find("PollRate");
213 float pollRate = pollRateDefault;
214 if (findPollRate != baseConfiguration->second.end())
215 {
216 pollRate = std::visit(VariantToFloatVisitor(),
217 findPollRate->second);
218 if (pollRate <= 0.0f)
219 {
220 pollRate = pollRateDefault; // polling time too short
221 }
222 }
223
James Feistf9b01b62020-01-29 15:21:58 -0800224 auto findPowerOn = baseConfiguration->second.find("PowerState");
225 PowerState readState = PowerState::always;
226 if (findPowerOn != baseConfiguration->second.end())
227 {
228 std::string powerState = std::visit(
229 VariantToStringVisitor(), findPowerOn->second);
230 setReadState(powerState, readState);
231 }
Jason Ling100c20b2020-08-11 14:50:33 -0700232
233 auto permitSet = getPermitSet(*baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800234 auto& sensor = sensors[sensorName];
235 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700236 auto hwmonFile = getFullHwmonFilePath(directory.string(),
237 "temp1", permitSet);
238 if (hwmonFile)
239 {
240 sensor = std::make_shared<HwmonTempSensor>(
241 *hwmonFile, sensorType, objectServer, dbusConnection,
Jeff Lin87bc67f2020-12-04 20:58:01 +0800242 io, sensorName, std::move(sensorThresholds), pollRate,
Jason Ling100c20b2020-08-11 14:50:33 -0700243 *interfacePath, readState);
244 sensor->setupRead();
245 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800246 // Looking for keys like "Name1" for temp2_input,
247 // "Name2" for temp3_input, etc.
248 int i = 0;
249 while (true)
James Feistdf515152019-09-18 16:40:40 -0700250 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800251 ++i;
252 auto findKey =
Jason Ling8b8bcc82020-08-04 19:33:22 -0700253 baseConfigMap->find("Name" + std::to_string(i));
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800254 if (findKey == baseConfigMap->end())
255 {
256 break;
257 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800258 std::string sensorName =
259 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700260 hwmonFile = getFullHwmonFilePath(
261 directory.string(), "temp" + std::to_string(i + 1),
262 permitSet);
263 if (hwmonFile)
264 {
265 auto& sensor = sensors[sensorName];
266 sensor = nullptr;
267 sensor = std::make_shared<HwmonTempSensor>(
268 *hwmonFile, sensorType, objectServer,
269 dbusConnection, io, sensorName,
Jeff Lin87bc67f2020-12-04 20:58:01 +0800270 std::vector<thresholds::Threshold>(), pollRate,
Jason Ling100c20b2020-08-11 14:50:33 -0700271 *interfacePath, readState);
272 sensor->setupRead();
273 }
James Feistdf515152019-09-18 16:40:40 -0700274 }
James Feistdf515152019-09-18 16:40:40 -0700275 }
276 }));
277 getter->getConfiguration(
278 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700279}
280
James Feistb6c0b912019-07-09 12:21:44 -0700281int main()
James Feist6714a252018-09-10 15:26:18 -0700282{
283 boost::asio::io_service io;
284 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
285 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
286 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800287 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700288 sensors;
289 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700290 auto sensorsChanged =
291 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700292
293 io.post([&]() {
294 createSensors(io, objectServer, sensors, systemBus, nullptr);
295 });
296
297 boost::asio::deadline_timer filterTimer(io);
298 std::function<void(sdbusplus::message::message&)> eventHandler =
299 [&](sdbusplus::message::message& message) {
300 if (message.is_method_error())
301 {
302 std::cerr << "callback method error\n";
303 return;
304 }
305 sensorsChanged->insert(message.get_path());
306 // this implicitly cancels the timer
307 filterTimer.expires_from_now(boost::posix_time::seconds(1));
308
309 filterTimer.async_wait([&](const boost::system::error_code& ec) {
310 if (ec == boost::asio::error::operation_aborted)
311 {
312 /* we were canceled*/
313 return;
314 }
315 else if (ec)
316 {
317 std::cerr << "timer error\n";
318 return;
319 }
320 createSensors(io, objectServer, sensors, systemBus,
321 sensorsChanged);
322 });
323 };
324
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700325 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700326 {
327 auto match = std::make_unique<sdbusplus::bus::match::match>(
328 static_cast<sdbusplus::bus::bus&>(*systemBus),
329 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700330 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700331 eventHandler);
332 matches.emplace_back(std::move(match));
333 }
334
335 io.run();
336}