blob: 94f23c0ed28c425b31c9188d5cd551d85cdca1d3 [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 Lehan3840d0a2020-05-13 16:13:45 -070043static constexpr std::array<const char*, 9> 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",
Josh Lehan3840d0a2020-05-13 16:13:45 -070047 "xyz.openbmc_project.Configuration.MAX6654",
John Wang55ab2af2019-04-18 14:22:31 +080048 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070049 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080050 "xyz.openbmc_project.Configuration.TMP421",
51 "xyz.openbmc_project.Configuration.TMP441",
52 "xyz.openbmc_project.Configuration.TMP75"};
James Feist6714a252018-09-10 15:26:18 -070053
54void createSensors(
55 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080056 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070057 sensors,
58 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
59 const std::unique_ptr<boost::container::flat_set<std::string>>&
60 sensorsChanged)
61{
James Feistdf515152019-09-18 16:40:40 -070062 auto getter = std::make_shared<GetSensorConfiguration>(
63 dbusConnection,
64 std::move([&io, &objectServer, &sensors, &dbusConnection,
65 &sensorsChanged](
66 const ManagedObjectType& sensorConfigurations) {
67 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070068
James Feistdf515152019-09-18 16:40:40 -070069 std::vector<fs::path> paths;
70 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
71 paths))
James Feist37266ca2018-10-15 15:56:28 -070072 {
James Feistdf515152019-09-18 16:40:40 -070073 std::cerr << "No temperature sensors in system\n";
74 return;
75 }
76
77 boost::container::flat_set<std::string> directories;
78
79 // iterate through all found temp sensors, and try to match them
80 // with configuration
81 for (auto& path : paths)
82 {
83 std::smatch match;
84 const std::string& pathStr = path.string();
85 auto directory = path.parent_path();
86
87 auto ret = directories.insert(directory.string());
88 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070089 {
James Feistdf515152019-09-18 16:40:40 -070090 continue; // already searched this path
91 }
92
93 fs::path device = directory / "device";
94 std::string deviceName = fs::canonical(device).stem();
95 auto findHyphen = deviceName.find("-");
96 if (findHyphen == std::string::npos)
97 {
98 std::cerr << "found bad device " << deviceName << "\n";
99 continue;
100 }
101 std::string busStr = deviceName.substr(0, findHyphen);
102 std::string addrStr = deviceName.substr(findHyphen + 1);
103
104 size_t bus = 0;
105 size_t addr = 0;
106 try
107 {
108 bus = std::stoi(busStr);
109 addr = std::stoi(addrStr, 0, 16);
110 }
111 catch (std::invalid_argument&)
112 {
113 continue;
114 }
115 const SensorData* sensorData = nullptr;
116 const std::string* interfacePath = nullptr;
117 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800118 const SensorBaseConfiguration* baseConfiguration = nullptr;
119 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700120
121 for (const std::pair<sdbusplus::message::object_path,
122 SensorData>& sensor : sensorConfigurations)
123 {
124 sensorData = &(sensor.second);
125 for (const char* type : sensorTypes)
126 {
127 auto sensorBase = sensorData->find(type);
128 if (sensorBase != sensorData->end())
129 {
130 baseConfiguration = &(*sensorBase);
131 sensorType = type;
132 break;
133 }
134 }
135 if (baseConfiguration == nullptr)
136 {
137 std::cerr << "error finding base configuration for "
138 << deviceName << "\n";
139 continue;
140 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800141 baseConfigMap = &baseConfiguration->second;
142 auto configurationBus = baseConfigMap->find("Bus");
143 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700144
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800145 if (configurationBus == baseConfigMap->end() ||
146 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700147 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800148 std::cerr << "error finding bus or address in "
149 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700150 continue;
151 }
152
153 if (std::get<uint64_t>(configurationBus->second) != bus ||
154 std::get<uint64_t>(configurationAddress->second) !=
155 addr)
156 {
157 continue;
158 }
159
160 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700161 break;
162 }
James Feistdf515152019-09-18 16:40:40 -0700163 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700164 {
James Feistdf515152019-09-18 16:40:40 -0700165 std::cerr << "failed to find match for " << deviceName
166 << "\n";
167 continue;
James Feist6714a252018-09-10 15:26:18 -0700168 }
James Feist6714a252018-09-10 15:26:18 -0700169
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800170 auto findSensorName = baseConfigMap->find("Name");
171 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700172 {
173 std::cerr << "could not determine configuration name for "
174 << deviceName << "\n";
175 continue;
176 }
177 std::string sensorName =
178 std::get<std::string>(findSensorName->second);
179 // on rescans, only update sensors we were signaled by
180 auto findSensor = sensors.find(sensorName);
181 if (!firstScan && findSensor != sensors.end())
182 {
183 bool found = false;
184 for (auto it = sensorsChanged->begin();
185 it != sensorsChanged->end(); it++)
186 {
187 if (boost::ends_with(*it, findSensor->second->name))
188 {
189 sensorsChanged->erase(it);
190 findSensor->second = nullptr;
191 found = true;
192 break;
193 }
194 }
195 if (!found)
196 {
197 continue;
198 }
199 }
200 std::vector<thresholds::Threshold> sensorThresholds;
201 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
202 {
203 std::cerr << "error populating thresholds for "
204 << sensorName << "\n";
205 }
James Feistf9b01b62020-01-29 15:21:58 -0800206 auto findPowerOn = baseConfiguration->second.find("PowerState");
207 PowerState readState = PowerState::always;
208 if (findPowerOn != baseConfiguration->second.end())
209 {
210 std::string powerState = std::visit(
211 VariantToStringVisitor(), findPowerOn->second);
212 setReadState(powerState, readState);
213 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800214 auto& sensor = sensors[sensorName];
215 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800216 sensor = std::make_shared<HwmonTempSensor>(
James Feistdf515152019-09-18 16:40:40 -0700217 directory.string() + "/temp1_input", sensorType,
218 objectServer, dbusConnection, io, sensorName,
James Feistf9b01b62020-01-29 15:21:58 -0800219 std::move(sensorThresholds), *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800220 sensor->setupRead();
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800221 // Looking for keys like "Name1" for temp2_input,
222 // "Name2" for temp3_input, etc.
223 int i = 0;
224 while (true)
James Feistdf515152019-09-18 16:40:40 -0700225 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800226 ++i;
227 auto findKey =
228 baseConfigMap->find("Name" + std::string(1, '0' + i));
229 if (findKey == baseConfigMap->end())
230 {
231 break;
232 }
233
234 std::string sensorName =
235 std::get<std::string>(findKey->second);
236 auto& sensor = sensors[sensorName];
237 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800238 sensor = std::make_shared<HwmonTempSensor>(
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800239 directory.string() + "/temp" + std::string(1, '1' + i) +
240 "_input",
241 sensorType, objectServer, dbusConnection, io,
242 sensorName, std::vector<thresholds::Threshold>(),
James Feistf9b01b62020-01-29 15:21:58 -0800243 *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800244 sensor->setupRead();
James Feistdf515152019-09-18 16:40:40 -0700245 }
James Feistdf515152019-09-18 16:40:40 -0700246 }
247 }));
248 getter->getConfiguration(
249 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700250}
251
James Feistb6c0b912019-07-09 12:21:44 -0700252int main()
James Feist6714a252018-09-10 15:26:18 -0700253{
254 boost::asio::io_service io;
255 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
256 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
257 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800258 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700259 sensors;
260 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
261 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
262 std::make_unique<boost::container::flat_set<std::string>>();
263
264 io.post([&]() {
265 createSensors(io, objectServer, sensors, systemBus, nullptr);
266 });
267
268 boost::asio::deadline_timer filterTimer(io);
269 std::function<void(sdbusplus::message::message&)> eventHandler =
270 [&](sdbusplus::message::message& message) {
271 if (message.is_method_error())
272 {
273 std::cerr << "callback method error\n";
274 return;
275 }
276 sensorsChanged->insert(message.get_path());
277 // this implicitly cancels the timer
278 filterTimer.expires_from_now(boost::posix_time::seconds(1));
279
280 filterTimer.async_wait([&](const boost::system::error_code& ec) {
281 if (ec == boost::asio::error::operation_aborted)
282 {
283 /* we were canceled*/
284 return;
285 }
286 else if (ec)
287 {
288 std::cerr << "timer error\n";
289 return;
290 }
291 createSensors(io, objectServer, sensors, systemBus,
292 sensorsChanged);
293 });
294 };
295
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700296 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700297 {
298 auto match = std::make_unique<sdbusplus::bus::match::match>(
299 static_cast<sdbusplus::bus::bus&>(*systemBus),
300 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700301 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700302 eventHandler);
303 matches.emplace_back(std::move(match));
304 }
305
306 io.run();
307}