blob: b5cbf235502e9ed6817515a309ede914e0bf714b [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;
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();
99 auto findHyphen = deviceName.find("-");
100 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);
113 addr = std::stoi(addrStr, 0, 16);
114 }
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 }
James Feistf9b01b62020-01-29 15:21:58 -0800210 auto findPowerOn = baseConfiguration->second.find("PowerState");
211 PowerState readState = PowerState::always;
212 if (findPowerOn != baseConfiguration->second.end())
213 {
214 std::string powerState = std::visit(
215 VariantToStringVisitor(), findPowerOn->second);
216 setReadState(powerState, readState);
217 }
Jason Ling100c20b2020-08-11 14:50:33 -0700218
219 auto permitSet = getPermitSet(*baseConfigMap);
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800220 auto& sensor = sensors[sensorName];
221 sensor = nullptr;
Jason Ling100c20b2020-08-11 14:50:33 -0700222 auto hwmonFile = getFullHwmonFilePath(directory.string(),
223 "temp1", permitSet);
224 if (hwmonFile)
225 {
226 sensor = std::make_shared<HwmonTempSensor>(
227 *hwmonFile, sensorType, objectServer, dbusConnection,
228 io, sensorName, std::move(sensorThresholds),
229 *interfacePath, readState);
230 sensor->setupRead();
231 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800232 // Looking for keys like "Name1" for temp2_input,
233 // "Name2" for temp3_input, etc.
234 int i = 0;
235 while (true)
James Feistdf515152019-09-18 16:40:40 -0700236 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800237 ++i;
238 auto findKey =
Jason Ling8b8bcc82020-08-04 19:33:22 -0700239 baseConfigMap->find("Name" + std::to_string(i));
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800240 if (findKey == baseConfigMap->end())
241 {
242 break;
243 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800244 std::string sensorName =
245 std::get<std::string>(findKey->second);
Jason Ling100c20b2020-08-11 14:50:33 -0700246 hwmonFile = getFullHwmonFilePath(
247 directory.string(), "temp" + std::to_string(i + 1),
248 permitSet);
249 if (hwmonFile)
250 {
251 auto& sensor = sensors[sensorName];
252 sensor = nullptr;
253 sensor = std::make_shared<HwmonTempSensor>(
254 *hwmonFile, sensorType, objectServer,
255 dbusConnection, io, sensorName,
256 std::vector<thresholds::Threshold>(),
257 *interfacePath, readState);
258 sensor->setupRead();
259 }
James Feistdf515152019-09-18 16:40:40 -0700260 }
James Feistdf515152019-09-18 16:40:40 -0700261 }
262 }));
263 getter->getConfiguration(
264 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700265}
266
James Feistb6c0b912019-07-09 12:21:44 -0700267int main()
James Feist6714a252018-09-10 15:26:18 -0700268{
269 boost::asio::io_service io;
270 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
271 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
272 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800273 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700274 sensors;
275 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700276 auto sensorsChanged =
277 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700278
279 io.post([&]() {
280 createSensors(io, objectServer, sensors, systemBus, nullptr);
281 });
282
283 boost::asio::deadline_timer filterTimer(io);
284 std::function<void(sdbusplus::message::message&)> eventHandler =
285 [&](sdbusplus::message::message& message) {
286 if (message.is_method_error())
287 {
288 std::cerr << "callback method error\n";
289 return;
290 }
291 sensorsChanged->insert(message.get_path());
292 // this implicitly cancels the timer
293 filterTimer.expires_from_now(boost::posix_time::seconds(1));
294
295 filterTimer.async_wait([&](const boost::system::error_code& ec) {
296 if (ec == boost::asio::error::operation_aborted)
297 {
298 /* we were canceled*/
299 return;
300 }
301 else if (ec)
302 {
303 std::cerr << "timer error\n";
304 return;
305 }
306 createSensors(io, objectServer, sensors, systemBus,
307 sensorsChanged);
308 });
309 };
310
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700311 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700312 {
313 auto match = std::make_unique<sdbusplus::bus::match::match>(
314 static_cast<sdbusplus::bus::bus&>(*systemBus),
315 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700316 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700317 eventHandler);
318 matches.emplace_back(std::move(match));
319 }
320
321 io.run();
322}