blob: be7be9805796f08aece49c96646d5d0712c5657a [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 Lehan8fb0a012020-06-26 19:28:16 -070043static constexpr std::array<const char*, 11> 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",
Josh Lehan8fb0a012020-06-26 19:28:16 -070049 "xyz.openbmc_project.Configuration.SBTSI",
John Wang55ab2af2019-04-18 14:22:31 +080050 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070051 "xyz.openbmc_project.Configuration.TMP175",
Alex Qiu8b3f7d42020-01-06 13:54:42 -080052 "xyz.openbmc_project.Configuration.TMP421",
53 "xyz.openbmc_project.Configuration.TMP441",
54 "xyz.openbmc_project.Configuration.TMP75"};
James Feist6714a252018-09-10 15:26:18 -070055
56void createSensors(
57 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
Yong Lif3fd1912020-03-25 21:35:23 +080058 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>&
James Feist6714a252018-09-10 15:26:18 -070059 sensors,
60 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070061 const std::shared_ptr<boost::container::flat_set<std::string>>&
James Feist6714a252018-09-10 15:26:18 -070062 sensorsChanged)
63{
James Feistdf515152019-09-18 16:40:40 -070064 auto getter = std::make_shared<GetSensorConfiguration>(
65 dbusConnection,
66 std::move([&io, &objectServer, &sensors, &dbusConnection,
James Feist5591cf082020-07-15 16:44:54 -070067 sensorsChanged](
James Feistdf515152019-09-18 16:40:40 -070068 const ManagedObjectType& sensorConfigurations) {
69 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070070
James Feistdf515152019-09-18 16:40:40 -070071 std::vector<fs::path> paths;
72 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
73 paths))
James Feist37266ca2018-10-15 15:56:28 -070074 {
James Feistdf515152019-09-18 16:40:40 -070075 std::cerr << "No temperature sensors in system\n";
76 return;
77 }
78
79 boost::container::flat_set<std::string> directories;
80
81 // iterate through all found temp sensors, and try to match them
82 // with configuration
83 for (auto& path : paths)
84 {
85 std::smatch match;
86 const std::string& pathStr = path.string();
87 auto directory = path.parent_path();
88
89 auto ret = directories.insert(directory.string());
90 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070091 {
James Feistdf515152019-09-18 16:40:40 -070092 continue; // already searched this path
93 }
94
95 fs::path device = directory / "device";
96 std::string deviceName = fs::canonical(device).stem();
97 auto findHyphen = deviceName.find("-");
98 if (findHyphen == std::string::npos)
99 {
100 std::cerr << "found bad device " << deviceName << "\n";
101 continue;
102 }
103 std::string busStr = deviceName.substr(0, findHyphen);
104 std::string addrStr = deviceName.substr(findHyphen + 1);
105
106 size_t bus = 0;
107 size_t addr = 0;
108 try
109 {
110 bus = std::stoi(busStr);
111 addr = std::stoi(addrStr, 0, 16);
112 }
113 catch (std::invalid_argument&)
114 {
115 continue;
116 }
117 const SensorData* sensorData = nullptr;
118 const std::string* interfacePath = nullptr;
119 const char* sensorType = nullptr;
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800120 const SensorBaseConfiguration* baseConfiguration = nullptr;
121 const SensorBaseConfigMap* baseConfigMap = nullptr;
James Feistdf515152019-09-18 16:40:40 -0700122
123 for (const std::pair<sdbusplus::message::object_path,
124 SensorData>& sensor : sensorConfigurations)
125 {
126 sensorData = &(sensor.second);
127 for (const char* type : sensorTypes)
128 {
129 auto sensorBase = sensorData->find(type);
130 if (sensorBase != sensorData->end())
131 {
132 baseConfiguration = &(*sensorBase);
133 sensorType = type;
134 break;
135 }
136 }
137 if (baseConfiguration == nullptr)
138 {
139 std::cerr << "error finding base configuration for "
140 << deviceName << "\n";
141 continue;
142 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800143 baseConfigMap = &baseConfiguration->second;
144 auto configurationBus = baseConfigMap->find("Bus");
145 auto configurationAddress = baseConfigMap->find("Address");
James Feistdf515152019-09-18 16:40:40 -0700146
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800147 if (configurationBus == baseConfigMap->end() ||
148 configurationAddress == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700149 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800150 std::cerr << "error finding bus or address in "
151 "configuration\n";
James Feistdf515152019-09-18 16:40:40 -0700152 continue;
153 }
154
155 if (std::get<uint64_t>(configurationBus->second) != bus ||
156 std::get<uint64_t>(configurationAddress->second) !=
157 addr)
158 {
159 continue;
160 }
161
162 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700163 break;
164 }
James Feistdf515152019-09-18 16:40:40 -0700165 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700166 {
James Feistdf515152019-09-18 16:40:40 -0700167 std::cerr << "failed to find match for " << deviceName
168 << "\n";
169 continue;
James Feist6714a252018-09-10 15:26:18 -0700170 }
James Feist6714a252018-09-10 15:26:18 -0700171
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800172 auto findSensorName = baseConfigMap->find("Name");
173 if (findSensorName == baseConfigMap->end())
James Feistdf515152019-09-18 16:40:40 -0700174 {
175 std::cerr << "could not determine configuration name for "
176 << deviceName << "\n";
177 continue;
178 }
179 std::string sensorName =
180 std::get<std::string>(findSensorName->second);
181 // on rescans, only update sensors we were signaled by
182 auto findSensor = sensors.find(sensorName);
183 if (!firstScan && findSensor != sensors.end())
184 {
185 bool found = false;
186 for (auto it = sensorsChanged->begin();
187 it != sensorsChanged->end(); it++)
188 {
189 if (boost::ends_with(*it, findSensor->second->name))
190 {
191 sensorsChanged->erase(it);
192 findSensor->second = nullptr;
193 found = true;
194 break;
195 }
196 }
197 if (!found)
198 {
199 continue;
200 }
201 }
202 std::vector<thresholds::Threshold> sensorThresholds;
203 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
204 {
205 std::cerr << "error populating thresholds for "
206 << sensorName << "\n";
207 }
James Feistf9b01b62020-01-29 15:21:58 -0800208 auto findPowerOn = baseConfiguration->second.find("PowerState");
209 PowerState readState = PowerState::always;
210 if (findPowerOn != baseConfiguration->second.end())
211 {
212 std::string powerState = std::visit(
213 VariantToStringVisitor(), findPowerOn->second);
214 setReadState(powerState, readState);
215 }
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800216 auto& sensor = sensors[sensorName];
217 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800218 sensor = std::make_shared<HwmonTempSensor>(
James Feistdf515152019-09-18 16:40:40 -0700219 directory.string() + "/temp1_input", sensorType,
220 objectServer, dbusConnection, io, sensorName,
James Feistf9b01b62020-01-29 15:21:58 -0800221 std::move(sensorThresholds), *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800222 sensor->setupRead();
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800223 // Looking for keys like "Name1" for temp2_input,
224 // "Name2" for temp3_input, etc.
225 int i = 0;
226 while (true)
James Feistdf515152019-09-18 16:40:40 -0700227 {
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800228 ++i;
229 auto findKey =
Jason Ling8b8bcc82020-08-04 19:33:22 -0700230 baseConfigMap->find("Name" + std::to_string(i));
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800231 if (findKey == baseConfigMap->end())
232 {
233 break;
234 }
235
236 std::string sensorName =
237 std::get<std::string>(findKey->second);
238 auto& sensor = sensors[sensorName];
239 sensor = nullptr;
Yong Lif3fd1912020-03-25 21:35:23 +0800240 sensor = std::make_shared<HwmonTempSensor>(
Jason Ling8b8bcc82020-08-04 19:33:22 -0700241 directory.string() + "/temp" + std::to_string(i + 1) +
Alex Qiu8b3f7d42020-01-06 13:54:42 -0800242 "_input",
243 sensorType, objectServer, dbusConnection, io,
244 sensorName, std::vector<thresholds::Threshold>(),
James Feistf9b01b62020-01-29 15:21:58 -0800245 *interfacePath, readState);
Yong Lif3fd1912020-03-25 21:35:23 +0800246 sensor->setupRead();
James Feistdf515152019-09-18 16:40:40 -0700247 }
James Feistdf515152019-09-18 16:40:40 -0700248 }
249 }));
250 getter->getConfiguration(
251 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700252}
253
James Feistb6c0b912019-07-09 12:21:44 -0700254int main()
James Feist6714a252018-09-10 15:26:18 -0700255{
256 boost::asio::io_service io;
257 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
258 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
259 sdbusplus::asio::object_server objectServer(systemBus);
Yong Lif3fd1912020-03-25 21:35:23 +0800260 boost::container::flat_map<std::string, std::shared_ptr<HwmonTempSensor>>
James Feist6714a252018-09-10 15:26:18 -0700261 sensors;
262 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
James Feist5591cf082020-07-15 16:44:54 -0700263 auto sensorsChanged =
264 std::make_shared<boost::container::flat_set<std::string>>();
James Feist6714a252018-09-10 15:26:18 -0700265
266 io.post([&]() {
267 createSensors(io, objectServer, sensors, systemBus, nullptr);
268 });
269
270 boost::asio::deadline_timer filterTimer(io);
271 std::function<void(sdbusplus::message::message&)> eventHandler =
272 [&](sdbusplus::message::message& message) {
273 if (message.is_method_error())
274 {
275 std::cerr << "callback method error\n";
276 return;
277 }
278 sensorsChanged->insert(message.get_path());
279 // this implicitly cancels the timer
280 filterTimer.expires_from_now(boost::posix_time::seconds(1));
281
282 filterTimer.async_wait([&](const boost::system::error_code& ec) {
283 if (ec == boost::asio::error::operation_aborted)
284 {
285 /* we were canceled*/
286 return;
287 }
288 else if (ec)
289 {
290 std::cerr << "timer error\n";
291 return;
292 }
293 createSensors(io, objectServer, sensors, systemBus,
294 sensorsChanged);
295 });
296 };
297
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700298 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700299 {
300 auto match = std::make_unique<sdbusplus::bus::match::match>(
301 static_cast<sdbusplus::bus::bus&>(*systemBus),
302 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700303 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700304 eventHandler);
305 matches.emplace_back(std::move(match));
306 }
307
308 io.run();
309}