blob: e478bcca636674302b6cba6415d673dcce781cd8 [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;
Jason Ling16e7af12020-06-05 17:41:01 -070043static constexpr std::array<const char*, 10> 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",
John Wang55ab2af2019-04-18 14:22:31 +080049 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070050 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080051 "xyz.openbmc_project.Configuration.TMP421",
52 "xyz.openbmc_project.Configuration.TMP441",
53 "xyz.openbmc_project.Configuration.TMP75"};
James Feist6714a252018-09-10 15:26:18 -070054
55void createSensors(
56 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080057 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070058 sensors,
59 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
60 const std::unique_ptr<boost::container::flat_set<std::string>>&
61 sensorsChanged)
62{
James Feistdf515152019-09-18 16:40:40 -070063 auto getter = std::make_shared<GetSensorConfiguration>(
64 dbusConnection,
65 std::move([&io, &objectServer, &sensors, &dbusConnection,
66 &sensorsChanged](
67 const ManagedObjectType& sensorConfigurations) {
68 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070069
James Feistdf515152019-09-18 16:40:40 -070070 std::vector<fs::path> paths;
71 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
72 paths))
James Feist37266ca2018-10-15 15:56:28 -070073 {
James Feistdf515152019-09-18 16:40:40 -070074 std::cerr << "No temperature sensors in system\n";
75 return;
76 }
77
78 boost::container::flat_set<std::string> directories;
79
80 // iterate through all found temp sensors, and try to match them
81 // with configuration
82 for (auto& path : paths)
83 {
84 std::smatch match;
85 const std::string& pathStr = path.string();
86 auto directory = path.parent_path();
87
88 auto ret = directories.insert(directory.string());
89 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070090 {
James Feistdf515152019-09-18 16:40:40 -070091 continue; // already searched this path
92 }
93
94 fs::path device = directory / "device";
95 std::string deviceName = fs::canonical(device).stem();
96 auto findHyphen = deviceName.find("-");
97 if (findHyphen == std::string::npos)
98 {
99 std::cerr << "found bad device " << deviceName << "\n";
100 continue;
101 }
102 std::string busStr = deviceName.substr(0, findHyphen);
103 std::string addrStr = deviceName.substr(findHyphen + 1);
104
105 size_t bus = 0;
106 size_t addr = 0;
107 try
108 {
109 bus = std::stoi(busStr);
110 addr = std::stoi(addrStr, 0, 16);
111 }
112 catch (std::invalid_argument&)
113 {
114 continue;
115 }
116 const SensorData* sensorData = nullptr;
117 const std::string* interfacePath = nullptr;
118 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800119 const SensorBaseConfiguration* baseConfiguration = nullptr;
120 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700121
122 for (const std::pair<sdbusplus::message::object_path,
123 SensorData>& sensor : sensorConfigurations)
124 {
125 sensorData = &(sensor.second);
126 for (const char* type : sensorTypes)
127 {
128 auto sensorBase = sensorData->find(type);
129 if (sensorBase != sensorData->end())
130 {
131 baseConfiguration = &(*sensorBase);
132 sensorType = type;
133 break;
134 }
135 }
136 if (baseConfiguration == nullptr)
137 {
138 std::cerr << "error finding base configuration for "
139 << deviceName << "\n";
140 continue;
141 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800142 baseConfigMap = &baseConfiguration->second;
143 auto configurationBus = baseConfigMap->find("Bus");
144 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700145
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800146 if (configurationBus == baseConfigMap->end() ||
147 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700148 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800149 std::cerr << "error finding bus or address in "
150 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700151 continue;
152 }
153
154 if (std::get<uint64_t>(configurationBus->second) != bus ||
155 std::get<uint64_t>(configurationAddress->second) !=
156 addr)
157 {
158 continue;
159 }
160
161 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700162 break;
163 }
James Feistdf515152019-09-18 16:40:40 -0700164 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700165 {
James Feistdf515152019-09-18 16:40:40 -0700166 std::cerr << "failed to find match for " << deviceName
167 << "\n";
168 continue;
James Feist6714a252018-09-10 15:26:18 -0700169 }
James Feist6714a252018-09-10 15:26:18 -0700170
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800171 auto findSensorName = baseConfigMap->find("Name");
172 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700173 {
174 std::cerr << "could not determine configuration name for "
175 << deviceName << "\n";
176 continue;
177 }
178 std::string sensorName =
179 std::get<std::string>(findSensorName->second);
180 // on rescans, only update sensors we were signaled by
181 auto findSensor = sensors.find(sensorName);
182 if (!firstScan && findSensor != sensors.end())
183 {
184 bool found = false;
185 for (auto it = sensorsChanged->begin();
186 it != sensorsChanged->end(); it++)
187 {
188 if (boost::ends_with(*it, findSensor->second->name))
189 {
190 sensorsChanged->erase(it);
191 findSensor->second = nullptr;
192 found = true;
193 break;
194 }
195 }
196 if (!found)
197 {
198 continue;
199 }
200 }
201 std::vector<thresholds::Threshold> sensorThresholds;
202 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
203 {
204 std::cerr << "error populating thresholds for "
205 << sensorName << "\n";
206 }
James Feistf9b01b62020-01-29 15:21:58 -0800207 auto findPowerOn = baseConfiguration->second.find("PowerState");
208 PowerState readState = PowerState::always;
209 if (findPowerOn != baseConfiguration->second.end())
210 {
211 std::string powerState = std::visit(
212 VariantToStringVisitor(), findPowerOn->second);
213 setReadState(powerState, readState);
214 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800215 auto& sensor = sensors[sensorName];
216 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800217 sensor = std::make_shared<HwmonTempSensor>(
James Feistdf515152019-09-18 16:40:40 -0700218 directory.string() + "/temp1_input", sensorType,
219 objectServer, dbusConnection, io, sensorName,
James Feistf9b01b62020-01-29 15:21:58 -0800220 std::move(sensorThresholds), *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800221 sensor->setupRead();
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800222 // Looking for keys like "Name1" for temp2_input,
223 // "Name2" for temp3_input, etc.
224 int i = 0;
225 while (true)
James Feistdf515152019-09-18 16:40:40 -0700226 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800227 ++i;
228 auto findKey =
229 baseConfigMap->find("Name" + std::string(1, '0' + i));
230 if (findKey == baseConfigMap->end())
231 {
232 break;
233 }
234
235 std::string sensorName =
236 std::get<std::string>(findKey->second);
237 auto& sensor = sensors[sensorName];
238 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800239 sensor = std::make_shared<HwmonTempSensor>(
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800240 directory.string() + "/temp" + std::string(1, '1' + i) +
241 "_input",
242 sensorType, objectServer, dbusConnection, io,
243 sensorName, std::vector<thresholds::Threshold>(),
James Feistf9b01b62020-01-29 15:21:58 -0800244 *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800245 sensor->setupRead();
James Feistdf515152019-09-18 16:40:40 -0700246 }
James Feistdf515152019-09-18 16:40:40 -0700247 }
248 }));
249 getter->getConfiguration(
250 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700251}
252
James Feistb6c0b912019-07-09 12:21:44 -0700253int main()
James Feist6714a252018-09-10 15:26:18 -0700254{
255 boost::asio::io_service io;
256 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
257 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
258 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800259 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700260 sensors;
261 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
262 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
263 std::make_unique<boost::container::flat_set<std::string>>();
264
265 io.post([&]() {
266 createSensors(io, objectServer, sensors, systemBus, nullptr);
267 });
268
269 boost::asio::deadline_timer filterTimer(io);
270 std::function<void(sdbusplus::message::message&)> eventHandler =
271 [&](sdbusplus::message::message& message) {
272 if (message.is_method_error())
273 {
274 std::cerr << "callback method error\n";
275 return;
276 }
277 sensorsChanged->insert(message.get_path());
278 // this implicitly cancels the timer
279 filterTimer.expires_from_now(boost::posix_time::seconds(1));
280
281 filterTimer.async_wait([&](const boost::system::error_code& ec) {
282 if (ec == boost::asio::error::operation_aborted)
283 {
284 /* we were canceled*/
285 return;
286 }
287 else if (ec)
288 {
289 std::cerr << "timer error\n";
290 return;
291 }
292 createSensors(io, objectServer, sensors, systemBus,
293 sensorsChanged);
294 });
295 };
296
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700297 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700298 {
299 auto match = std::make_unique<sdbusplus::bus::match::match>(
300 static_cast<sdbusplus::bus::bus&>(*systemBus),
301 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700302 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700303 eventHandler);
304 matches.emplace_back(std::move(match));
305 }
306
307 io.run();
308}