blob: a7316e9c0bbec71c83e3f69bfb6c2df751e1e723 [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;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080042static constexpr std::array<const char*, 8> sensorTypes = {
43 "xyz.openbmc_project.Configuration.EMC1413",
44 "xyz.openbmc_project.Configuration.MAX31725",
45 "xyz.openbmc_project.Configuration.MAX31730",
John Wang55ab2af2019-04-18 14:22:31 +080046 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070047 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080048 "xyz.openbmc_project.Configuration.TMP421",
49 "xyz.openbmc_project.Configuration.TMP441",
50 "xyz.openbmc_project.Configuration.TMP75"};
James Feist6714a252018-09-10 15:26:18 -070051
52void createSensors(
53 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080054 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070055 sensors,
56 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
57 const std::unique_ptr<boost::container::flat_set<std::string>>&
58 sensorsChanged)
59{
James Feistdf515152019-09-18 16:40:40 -070060 auto getter = std::make_shared<GetSensorConfiguration>(
61 dbusConnection,
62 std::move([&io, &objectServer, &sensors, &dbusConnection,
63 &sensorsChanged](
64 const ManagedObjectType& sensorConfigurations) {
65 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070066
James Feistdf515152019-09-18 16:40:40 -070067 std::vector<fs::path> paths;
68 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
69 paths))
James Feist37266ca2018-10-15 15:56:28 -070070 {
James Feistdf515152019-09-18 16:40:40 -070071 std::cerr << "No temperature sensors in system\n";
72 return;
73 }
74
75 boost::container::flat_set<std::string> directories;
76
77 // iterate through all found temp sensors, and try to match them
78 // with configuration
79 for (auto& path : paths)
80 {
81 std::smatch match;
82 const std::string& pathStr = path.string();
83 auto directory = path.parent_path();
84
85 auto ret = directories.insert(directory.string());
86 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070087 {
James Feistdf515152019-09-18 16:40:40 -070088 continue; // already searched this path
89 }
90
91 fs::path device = directory / "device";
92 std::string deviceName = fs::canonical(device).stem();
93 auto findHyphen = deviceName.find("-");
94 if (findHyphen == std::string::npos)
95 {
96 std::cerr << "found bad device " << deviceName << "\n";
97 continue;
98 }
99 std::string busStr = deviceName.substr(0, findHyphen);
100 std::string addrStr = deviceName.substr(findHyphen + 1);
101
102 size_t bus = 0;
103 size_t addr = 0;
104 try
105 {
106 bus = std::stoi(busStr);
107 addr = std::stoi(addrStr, 0, 16);
108 }
109 catch (std::invalid_argument&)
110 {
111 continue;
112 }
113 const SensorData* sensorData = nullptr;
114 const std::string* interfacePath = nullptr;
115 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800116 const SensorBaseConfiguration* baseConfiguration = nullptr;
117 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700118
119 for (const std::pair<sdbusplus::message::object_path,
120 SensorData>& sensor : sensorConfigurations)
121 {
122 sensorData = &(sensor.second);
123 for (const char* type : sensorTypes)
124 {
125 auto sensorBase = sensorData->find(type);
126 if (sensorBase != sensorData->end())
127 {
128 baseConfiguration = &(*sensorBase);
129 sensorType = type;
130 break;
131 }
132 }
133 if (baseConfiguration == nullptr)
134 {
135 std::cerr << "error finding base configuration for "
136 << deviceName << "\n";
137 continue;
138 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800139 baseConfigMap = &baseConfiguration->second;
140 auto configurationBus = baseConfigMap->find("Bus");
141 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700142
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800143 if (configurationBus == baseConfigMap->end() ||
144 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700145 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800146 std::cerr << "error finding bus or address in "
147 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700148 continue;
149 }
150
151 if (std::get<uint64_t>(configurationBus->second) != bus ||
152 std::get<uint64_t>(configurationAddress->second) !=
153 addr)
154 {
155 continue;
156 }
157
158 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700159 break;
160 }
James Feistdf515152019-09-18 16:40:40 -0700161 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700162 {
James Feistdf515152019-09-18 16:40:40 -0700163 std::cerr << "failed to find match for " << deviceName
164 << "\n";
165 continue;
James Feist6714a252018-09-10 15:26:18 -0700166 }
James Feist6714a252018-09-10 15:26:18 -0700167
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800168 auto findSensorName = baseConfigMap->find("Name");
169 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700170 {
171 std::cerr << "could not determine configuration name for "
172 << deviceName << "\n";
173 continue;
174 }
175 std::string sensorName =
176 std::get<std::string>(findSensorName->second);
177 // on rescans, only update sensors we were signaled by
178 auto findSensor = sensors.find(sensorName);
179 if (!firstScan && findSensor != sensors.end())
180 {
181 bool found = false;
182 for (auto it = sensorsChanged->begin();
183 it != sensorsChanged->end(); it++)
184 {
185 if (boost::ends_with(*it, findSensor->second->name))
186 {
187 sensorsChanged->erase(it);
188 findSensor->second = nullptr;
189 found = true;
190 break;
191 }
192 }
193 if (!found)
194 {
195 continue;
196 }
197 }
198 std::vector<thresholds::Threshold> sensorThresholds;
199 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
200 {
201 std::cerr << "error populating thresholds for "
202 << sensorName << "\n";
203 }
James Feistf9b01b62020-01-29 15:21:58 -0800204 auto findPowerOn = baseConfiguration->second.find("PowerState");
205 PowerState readState = PowerState::always;
206 if (findPowerOn != baseConfiguration->second.end())
207 {
208 std::string powerState = std::visit(
209 VariantToStringVisitor(), findPowerOn->second);
210 setReadState(powerState, readState);
211 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800212 auto& sensor = sensors[sensorName];
213 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800214 sensor = std::make_shared<HwmonTempSensor>(
James Feistdf515152019-09-18 16:40:40 -0700215 directory.string() + "/temp1_input", sensorType,
216 objectServer, dbusConnection, io, sensorName,
James Feistf9b01b62020-01-29 15:21:58 -0800217 std::move(sensorThresholds), *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800218 sensor->setupRead();
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800219 // Looking for keys like "Name1" for temp2_input,
220 // "Name2" for temp3_input, etc.
221 int i = 0;
222 while (true)
James Feistdf515152019-09-18 16:40:40 -0700223 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800224 ++i;
225 auto findKey =
226 baseConfigMap->find("Name" + std::string(1, '0' + i));
227 if (findKey == baseConfigMap->end())
228 {
229 break;
230 }
231
232 std::string sensorName =
233 std::get<std::string>(findKey->second);
234 auto& sensor = sensors[sensorName];
235 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800236 sensor = std::make_shared<HwmonTempSensor>(
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800237 directory.string() + "/temp" + std::string(1, '1' + i) +
238 "_input",
239 sensorType, objectServer, dbusConnection, io,
240 sensorName, std::vector<thresholds::Threshold>(),
James Feistf9b01b62020-01-29 15:21:58 -0800241 *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800242 sensor->setupRead();
James Feistdf515152019-09-18 16:40:40 -0700243 }
James Feistdf515152019-09-18 16:40:40 -0700244 }
245 }));
246 getter->getConfiguration(
247 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700248}
249
James Feistb6c0b912019-07-09 12:21:44 -0700250int main()
James Feist6714a252018-09-10 15:26:18 -0700251{
252 boost::asio::io_service io;
253 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
254 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
255 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800256 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700257 sensors;
258 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
259 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
260 std::make_unique<boost::container::flat_set<std::string>>();
261
262 io.post([&]() {
263 createSensors(io, objectServer, sensors, systemBus, nullptr);
264 });
265
266 boost::asio::deadline_timer filterTimer(io);
267 std::function<void(sdbusplus::message::message&)> eventHandler =
268 [&](sdbusplus::message::message& message) {
269 if (message.is_method_error())
270 {
271 std::cerr << "callback method error\n";
272 return;
273 }
274 sensorsChanged->insert(message.get_path());
275 // this implicitly cancels the timer
276 filterTimer.expires_from_now(boost::posix_time::seconds(1));
277
278 filterTimer.async_wait([&](const boost::system::error_code& ec) {
279 if (ec == boost::asio::error::operation_aborted)
280 {
281 /* we were canceled*/
282 return;
283 }
284 else if (ec)
285 {
286 std::cerr << "timer error\n";
287 return;
288 }
289 createSensors(io, objectServer, sensors, systemBus,
290 sensorsChanged);
291 });
292 };
293
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700294 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700295 {
296 auto match = std::make_unique<sdbusplus::bus::match::match>(
297 static_cast<sdbusplus::bus::bus&>(*systemBus),
298 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700299 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700300 eventHandler);
301 matches.emplace_back(std::move(match));
302 }
303
304 io.run();
305}