blob: ab3544dcfdfafd6410275ef2f93b764e711ab547 [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
Patrick Venture96e97db2019-10-31 13:44:38 -070020#include <array>
James Feist6714a252018-09-10 15:26:18 -070021#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070023#include <boost/container/flat_map.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <boost/container/flat_set.hpp>
James Feist24f02f22019-04-15 11:05:39 -070025#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070026#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <functional>
28#include <memory>
James Feist6714a252018-09-10 15:26:18 -070029#include <regex>
30#include <sdbusplus/asio/connection.hpp>
31#include <sdbusplus/asio/object_server.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070032#include <sdbusplus/bus/match.hpp>
33#include <stdexcept>
34#include <string>
35#include <utility>
36#include <variant>
37#include <vector>
James Feist6714a252018-09-10 15:26:18 -070038
39static constexpr bool DEBUG = false;
40
James Feistcf3bce62019-01-08 10:07:19 -080041namespace fs = std::filesystem;
Josh Lehan3840d0a2020-05-13 16:13:45 -070042static constexpr std::array<const char*, 9> sensorTypes = {
Alex Qiu8b3f7d42020-01-06 13:54:42 -080043 "xyz.openbmc_project.Configuration.EMC1413",
44 "xyz.openbmc_project.Configuration.MAX31725",
45 "xyz.openbmc_project.Configuration.MAX31730",
Josh Lehan3840d0a2020-05-13 16:13:45 -070046 "xyz.openbmc_project.Configuration.MAX6654",
John Wang55ab2af2019-04-18 14:22:31 +080047 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070048 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080049 "xyz.openbmc_project.Configuration.TMP421",
50 "xyz.openbmc_project.Configuration.TMP441",
51 "xyz.openbmc_project.Configuration.TMP75"};
James Feist6714a252018-09-10 15:26:18 -070052
53void createSensors(
54 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080055 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070056 sensors,
57 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
58 const std::unique_ptr<boost::container::flat_set<std::string>>&
59 sensorsChanged)
60{
James Feistdf515152019-09-18 16:40:40 -070061 auto getter = std::make_shared<GetSensorConfiguration>(
62 dbusConnection,
63 std::move([&io, &objectServer, &sensors, &dbusConnection,
64 &sensorsChanged](
65 const ManagedObjectType& sensorConfigurations) {
66 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070067
James Feistdf515152019-09-18 16:40:40 -070068 std::vector<fs::path> paths;
69 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
70 paths))
James Feist37266ca2018-10-15 15:56:28 -070071 {
James Feistdf515152019-09-18 16:40:40 -070072 std::cerr << "No temperature sensors in system\n";
73 return;
74 }
75
76 boost::container::flat_set<std::string> directories;
77
78 // iterate through all found temp sensors, and try to match them
79 // with configuration
80 for (auto& path : paths)
81 {
82 std::smatch match;
83 const std::string& pathStr = path.string();
84 auto directory = path.parent_path();
85
86 auto ret = directories.insert(directory.string());
87 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070088 {
James Feistdf515152019-09-18 16:40:40 -070089 continue; // already searched this path
90 }
91
92 fs::path device = directory / "device";
93 std::string deviceName = fs::canonical(device).stem();
94 auto findHyphen = deviceName.find("-");
95 if (findHyphen == std::string::npos)
96 {
97 std::cerr << "found bad device " << deviceName << "\n";
98 continue;
99 }
100 std::string busStr = deviceName.substr(0, findHyphen);
101 std::string addrStr = deviceName.substr(findHyphen + 1);
102
103 size_t bus = 0;
104 size_t addr = 0;
105 try
106 {
107 bus = std::stoi(busStr);
108 addr = std::stoi(addrStr, 0, 16);
109 }
110 catch (std::invalid_argument&)
111 {
112 continue;
113 }
114 const SensorData* sensorData = nullptr;
115 const std::string* interfacePath = nullptr;
116 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800117 const SensorBaseConfiguration* baseConfiguration = nullptr;
118 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700119
120 for (const std::pair<sdbusplus::message::object_path,
121 SensorData>& sensor : sensorConfigurations)
122 {
123 sensorData = &(sensor.second);
124 for (const char* type : sensorTypes)
125 {
126 auto sensorBase = sensorData->find(type);
127 if (sensorBase != sensorData->end())
128 {
129 baseConfiguration = &(*sensorBase);
130 sensorType = type;
131 break;
132 }
133 }
134 if (baseConfiguration == nullptr)
135 {
136 std::cerr << "error finding base configuration for "
137 << deviceName << "\n";
138 continue;
139 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800140 baseConfigMap = &baseConfiguration->second;
141 auto configurationBus = baseConfigMap->find("Bus");
142 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700143
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800144 if (configurationBus == baseConfigMap->end() ||
145 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700146 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800147 std::cerr << "error finding bus or address in "
148 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700149 continue;
150 }
151
152 if (std::get<uint64_t>(configurationBus->second) != bus ||
153 std::get<uint64_t>(configurationAddress->second) !=
154 addr)
155 {
156 continue;
157 }
158
159 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700160 break;
161 }
James Feistdf515152019-09-18 16:40:40 -0700162 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700163 {
James Feistdf515152019-09-18 16:40:40 -0700164 std::cerr << "failed to find match for " << deviceName
165 << "\n";
166 continue;
James Feist6714a252018-09-10 15:26:18 -0700167 }
James Feist6714a252018-09-10 15:26:18 -0700168
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800169 auto findSensorName = baseConfigMap->find("Name");
170 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700171 {
172 std::cerr << "could not determine configuration name for "
173 << deviceName << "\n";
174 continue;
175 }
176 std::string sensorName =
177 std::get<std::string>(findSensorName->second);
178 // on rescans, only update sensors we were signaled by
179 auto findSensor = sensors.find(sensorName);
180 if (!firstScan && findSensor != sensors.end())
181 {
182 bool found = false;
183 for (auto it = sensorsChanged->begin();
184 it != sensorsChanged->end(); it++)
185 {
186 if (boost::ends_with(*it, findSensor->second->name))
187 {
188 sensorsChanged->erase(it);
189 findSensor->second = nullptr;
190 found = true;
191 break;
192 }
193 }
194 if (!found)
195 {
196 continue;
197 }
198 }
199 std::vector<thresholds::Threshold> sensorThresholds;
200 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
201 {
202 std::cerr << "error populating thresholds for "
203 << sensorName << "\n";
204 }
James Feistf9b01b62020-01-29 15:21:58 -0800205 auto findPowerOn = baseConfiguration->second.find("PowerState");
206 PowerState readState = PowerState::always;
207 if (findPowerOn != baseConfiguration->second.end())
208 {
209 std::string powerState = std::visit(
210 VariantToStringVisitor(), findPowerOn->second);
211 setReadState(powerState, readState);
212 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800213 auto& sensor = sensors[sensorName];
214 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800215 sensor = std::make_shared<HwmonTempSensor>(
James Feistdf515152019-09-18 16:40:40 -0700216 directory.string() + "/temp1_input", sensorType,
217 objectServer, dbusConnection, io, sensorName,
James Feistf9b01b62020-01-29 15:21:58 -0800218 std::move(sensorThresholds), *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800219 sensor->setupRead();
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800220 // Looking for keys like "Name1" for temp2_input,
221 // "Name2" for temp3_input, etc.
222 int i = 0;
223 while (true)
James Feistdf515152019-09-18 16:40:40 -0700224 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800225 ++i;
226 auto findKey =
227 baseConfigMap->find("Name" + std::string(1, '0' + i));
228 if (findKey == baseConfigMap->end())
229 {
230 break;
231 }
232
233 std::string sensorName =
234 std::get<std::string>(findKey->second);
235 auto& sensor = sensors[sensorName];
236 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800237 sensor = std::make_shared<HwmonTempSensor>(
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800238 directory.string() + "/temp" + std::string(1, '1' + i) +
239 "_input",
240 sensorType, objectServer, dbusConnection, io,
241 sensorName, std::vector<thresholds::Threshold>(),
James Feistf9b01b62020-01-29 15:21:58 -0800242 *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800243 sensor->setupRead();
James Feistdf515152019-09-18 16:40:40 -0700244 }
James Feistdf515152019-09-18 16:40:40 -0700245 }
246 }));
247 getter->getConfiguration(
248 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700249}
250
James Feistb6c0b912019-07-09 12:21:44 -0700251int main()
James Feist6714a252018-09-10 15:26:18 -0700252{
253 boost::asio::io_service io;
254 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
255 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
256 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800257 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700258 sensors;
259 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
260 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
261 std::make_unique<boost::container::flat_set<std::string>>();
262
263 io.post([&]() {
264 createSensors(io, objectServer, sensors, systemBus, nullptr);
265 });
266
267 boost::asio::deadline_timer filterTimer(io);
268 std::function<void(sdbusplus::message::message&)> eventHandler =
269 [&](sdbusplus::message::message& message) {
270 if (message.is_method_error())
271 {
272 std::cerr << "callback method error\n";
273 return;
274 }
275 sensorsChanged->insert(message.get_path());
276 // this implicitly cancels the timer
277 filterTimer.expires_from_now(boost::posix_time::seconds(1));
278
279 filterTimer.async_wait([&](const boost::system::error_code& ec) {
280 if (ec == boost::asio::error::operation_aborted)
281 {
282 /* we were canceled*/
283 return;
284 }
285 else if (ec)
286 {
287 std::cerr << "timer error\n";
288 return;
289 }
290 createSensors(io, objectServer, sensors, systemBus,
291 sensorsChanged);
292 });
293 };
294
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700295 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700296 {
297 auto match = std::make_unique<sdbusplus::bus::match::match>(
298 static_cast<sdbusplus::bus::bus&>(*systemBus),
299 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700300 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700301 eventHandler);
302 matches.emplace_back(std::move(match));
303 }
304
305 io.run();
306}