blob: c972cf3f58b7bcfbbbff0281e010a4eae1789ac9 [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;
41
James Feistcf3bce62019-01-08 10:07:19 -080042namespace fs = std::filesystem;
Josh Lehan8fb0a012020-06-26 19:28:16 -070043static constexpr std::array<const char*, 11> sensorTypes = {
Alex Qiu8b3f7d42020-01-06 13:54:42 -080044 "xyz.openbmc_project.Configuration.EMC1413",
45 "xyz.openbmc_project.Configuration.MAX31725",
46 "xyz.openbmc_project.Configuration.MAX31730",
Jason Ling16e7af12020-06-05 17:41:01 -070047 "xyz.openbmc_project.Configuration.MAX6581",
Josh Lehan3840d0a2020-05-13 16:13:45 -070048 "xyz.openbmc_project.Configuration.MAX6654",
Josh Lehan8fb0a012020-06-26 19:28:16 -070049 "xyz.openbmc_project.Configuration.SBTSI",
John Wang55ab2af2019-04-18 14:22:31 +080050 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070051 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080052 "xyz.openbmc_project.Configuration.TMP421",
53 "xyz.openbmc_project.Configuration.TMP441",
54 "xyz.openbmc_project.Configuration.TMP75"};
James Feist6714a252018-09-10 15:26:18 -070055
56void createSensors(
57 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080058 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070059 sensors,
60 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070061 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -070062 sensorsChanged)
63{
James Feistdf515152019-09-18 16:40:40 -070064 auto getter = std::make_shared<GetSensorConfiguration>(
65 dbusConnection,
66 std::move([&io, &objectServer, &sensors, &dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070067 sensorsChanged](
James Feistdf515152019-09-18 16:40:40 -070068 const ManagedObjectType& sensorConfigurations) {
69 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070070
James Feistdf515152019-09-18 16:40:40 -070071 std::vector<fs::path> paths;
72 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
73 paths))
James Feist37266ca2018-10-15 15:56:28 -070074 {
James Feistdf515152019-09-18 16:40:40 -070075 std::cerr << "No temperature sensors in system\n";
76 return;
77 }
78
79 boost::container::flat_set<std::string> directories;
80
81 // iterate through all found temp sensors, and try to match them
82 // with configuration
83 for (auto& path : paths)
84 {
85 std::smatch match;
86 const std::string& pathStr = path.string();
87 auto directory = path.parent_path();
88
89 auto ret = directories.insert(directory.string());
90 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070091 {
James Feistdf515152019-09-18 16:40:40 -070092 continue; // already searched this path
93 }
94
95 fs::path device = directory / "device";
96 std::string deviceName = fs::canonical(device).stem();
97 auto findHyphen = deviceName.find("-");
98 if (findHyphen == std::string::npos)
99 {
100 std::cerr << "found bad device " << deviceName << "\n";
101 continue;
102 }
103 std::string busStr = deviceName.substr(0, findHyphen);
104 std::string addrStr = deviceName.substr(findHyphen + 1);
105
106 size_t bus = 0;
107 size_t addr = 0;
108 try
109 {
110 bus = std::stoi(busStr);
111 addr = std::stoi(addrStr, 0, 16);
112 }
113 catch (std::invalid_argument&)
114 {
115 continue;
116 }
117 const SensorData* sensorData = nullptr;
118 const std::string* interfacePath = nullptr;
119 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800120 const SensorBaseConfiguration* baseConfiguration = nullptr;
121 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700122
123 for (const std::pair<sdbusplus::message::object_path,
124 SensorData>& sensor : sensorConfigurations)
125 {
126 sensorData = &(sensor.second);
127 for (const char* type : sensorTypes)
128 {
129 auto sensorBase = sensorData->find(type);
130 if (sensorBase != sensorData->end())
131 {
132 baseConfiguration = &(*sensorBase);
133 sensorType = type;
134 break;
135 }
136 }
137 if (baseConfiguration == nullptr)
138 {
139 std::cerr << "error finding base configuration for "
140 << deviceName << "\n";
141 continue;
142 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800143 baseConfigMap = &baseConfiguration->second;
144 auto configurationBus = baseConfigMap->find("Bus");
145 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700146
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800147 if (configurationBus == baseConfigMap->end() ||
148 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700149 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800150 std::cerr << "error finding bus or address in "
151 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700152 continue;
153 }
154
155 if (std::get<uint64_t>(configurationBus->second) != bus ||
156 std::get<uint64_t>(configurationAddress->second) !=
157 addr)
158 {
159 continue;
160 }
161
162 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700163 break;
164 }
James Feistdf515152019-09-18 16:40:40 -0700165 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700166 {
James Feistdf515152019-09-18 16:40:40 -0700167 std::cerr << "failed to find match for " << deviceName
168 << "\n";
169 continue;
James Feist6714a252018-09-10 15:26:18 -0700170 }
James Feist6714a252018-09-10 15:26:18 -0700171
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800172 auto findSensorName = baseConfigMap->find("Name");
173 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700174 {
175 std::cerr << "could not determine configuration name for "
176 << deviceName << "\n";
177 continue;
178 }
179 std::string sensorName =
180 std::get<std::string>(findSensorName->second);
181 // on rescans, only update sensors we were signaled by
182 auto findSensor = sensors.find(sensorName);
183 if (!firstScan && findSensor != sensors.end())
184 {
185 bool found = false;
186 for (auto it = sensorsChanged->begin();
187 it != sensorsChanged->end(); it++)
188 {
189 if (boost::ends_with(*it, findSensor->second->name))
190 {
191 sensorsChanged->erase(it);
192 findSensor->second = nullptr;
193 found = true;
194 break;
195 }
196 }
197 if (!found)
198 {
199 continue;
200 }
201 }
202 std::vector<thresholds::Threshold> sensorThresholds;
203 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
204 {
205 std::cerr << "error populating thresholds for "
206 << sensorName << "\n";
207 }
James Feistf9b01b62020-01-29 15:21:58 -0800208 auto findPowerOn = baseConfiguration->second.find("PowerState");
209 PowerState readState = PowerState::always;
210 if (findPowerOn != baseConfiguration->second.end())
211 {
212 std::string powerState = std::visit(
213 VariantToStringVisitor(), findPowerOn->second);
214 setReadState(powerState, readState);
215 }
Jason Ling100c20b2020-08-11 14:50:33 -0700216
217 auto permitSet = getPermitSet(*baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800218 auto& sensor = sensors[sensorName];
219 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700220 auto hwmonFile = getFullHwmonFilePath(directory.string(),
221 "temp1", permitSet);
222 if (hwmonFile)
223 {
224 sensor = std::make_shared<HwmonTempSensor>(
225 *hwmonFile, sensorType, objectServer, dbusConnection,
226 io, sensorName, std::move(sensorThresholds),
227 *interfacePath, readState);
228 sensor->setupRead();
229 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800230 // Looking for keys like "Name1" for temp2_input,
231 // "Name2" for temp3_input, etc.
232 int i = 0;
233 while (true)
James Feistdf515152019-09-18 16:40:40 -0700234 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800235 ++i;
236 auto findKey =
Jason Ling8b8bcc82020-08-04 19:33:22 -0700237 baseConfigMap->find("Name" + std::to_string(i));
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800238 if (findKey == baseConfigMap->end())
239 {
240 break;
241 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800242 std::string sensorName =
243 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700244 hwmonFile = getFullHwmonFilePath(
245 directory.string(), "temp" + std::to_string(i + 1),
246 permitSet);
247 if (hwmonFile)
248 {
249 auto& sensor = sensors[sensorName];
250 sensor = nullptr;
251 sensor = std::make_shared<HwmonTempSensor>(
252 *hwmonFile, sensorType, objectServer,
253 dbusConnection, io, sensorName,
254 std::vector<thresholds::Threshold>(),
255 *interfacePath, readState);
256 sensor->setupRead();
257 }
James Feistdf515152019-09-18 16:40:40 -0700258 }
James Feistdf515152019-09-18 16:40:40 -0700259 }
260 }));
261 getter->getConfiguration(
262 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700263}
264
James Feistb6c0b912019-07-09 12:21:44 -0700265int main()
James Feist6714a252018-09-10 15:26:18 -0700266{
267 boost::asio::io_service io;
268 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
269 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
270 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800271 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700272 sensors;
273 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700274 auto sensorsChanged =
275 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700276
277 io.post([&]() {
278 createSensors(io, objectServer, sensors, systemBus, nullptr);
279 });
280
281 boost::asio::deadline_timer filterTimer(io);
282 std::function<void(sdbusplus::message::message&)> eventHandler =
283 [&](sdbusplus::message::message& message) {
284 if (message.is_method_error())
285 {
286 std::cerr << "callback method error\n";
287 return;
288 }
289 sensorsChanged->insert(message.get_path());
290 // this implicitly cancels the timer
291 filterTimer.expires_from_now(boost::posix_time::seconds(1));
292
293 filterTimer.async_wait([&](const boost::system::error_code& ec) {
294 if (ec == boost::asio::error::operation_aborted)
295 {
296 /* we were canceled*/
297 return;
298 }
299 else if (ec)
300 {
301 std::cerr << "timer error\n";
302 return;
303 }
304 createSensors(io, objectServer, sensors, systemBus,
305 sensorsChanged);
306 });
307 };
308
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700309 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700310 {
311 auto match = std::make_unique<sdbusplus::bus::match::match>(
312 static_cast<sdbusplus::bus::bus&>(*systemBus),
313 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700314 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700315 eventHandler);
316 matches.emplace_back(std::move(match));
317 }
318
319 io.run();
320}