blob: 767459b8fda9eba66347068cd7a901a392d7a77d [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;
Patrick Venture3546adb2019-08-14 18:35:29 -070042static constexpr std::array<const char*, 7> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070043 "xyz.openbmc_project.Configuration.TMP75",
John Wang55ab2af2019-04-18 14:22:31 +080044 "xyz.openbmc_project.Configuration.TMP421",
Patrick Venture7fa475d2019-08-10 08:49:01 -070045 "xyz.openbmc_project.Configuration.TMP441",
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",
Patrick Venturebd1a9d52019-08-14 18:10:26 -070048 "xyz.openbmc_project.Configuration.EMC1413",
49 "xyz.openbmc_project.Configuration.MAX31725"};
James Feist6714a252018-09-10 15:26:18 -070050
51void createSensors(
52 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
53 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>&
54 sensors,
55 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
56 const std::unique_ptr<boost::container::flat_set<std::string>>&
57 sensorsChanged)
58{
James Feistdf515152019-09-18 16:40:40 -070059 auto getter = std::make_shared<GetSensorConfiguration>(
60 dbusConnection,
61 std::move([&io, &objectServer, &sensors, &dbusConnection,
62 &sensorsChanged](
63 const ManagedObjectType& sensorConfigurations) {
64 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070065
James Feistdf515152019-09-18 16:40:40 -070066 std::vector<fs::path> paths;
67 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
68 paths))
James Feist37266ca2018-10-15 15:56:28 -070069 {
James Feistdf515152019-09-18 16:40:40 -070070 std::cerr << "No temperature sensors in system\n";
71 return;
72 }
73
74 boost::container::flat_set<std::string> directories;
75
76 // iterate through all found temp sensors, and try to match them
77 // with configuration
78 for (auto& path : paths)
79 {
80 std::smatch match;
81 const std::string& pathStr = path.string();
82 auto directory = path.parent_path();
83
84 auto ret = directories.insert(directory.string());
85 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070086 {
James Feistdf515152019-09-18 16:40:40 -070087 continue; // already searched this path
88 }
89
90 fs::path device = directory / "device";
91 std::string deviceName = fs::canonical(device).stem();
92 auto findHyphen = deviceName.find("-");
93 if (findHyphen == std::string::npos)
94 {
95 std::cerr << "found bad device " << deviceName << "\n";
96 continue;
97 }
98 std::string busStr = deviceName.substr(0, findHyphen);
99 std::string addrStr = deviceName.substr(findHyphen + 1);
100
101 size_t bus = 0;
102 size_t addr = 0;
103 try
104 {
105 bus = std::stoi(busStr);
106 addr = std::stoi(addrStr, 0, 16);
107 }
108 catch (std::invalid_argument&)
109 {
110 continue;
111 }
112 const SensorData* sensorData = nullptr;
113 const std::string* interfacePath = nullptr;
114 const char* sensorType = nullptr;
115 const std::pair<
116 std::string,
117 boost::container::flat_map<std::string, BasicVariantType>>*
118 baseConfiguration = nullptr;
119
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 }
140 auto configurationBus =
141 baseConfiguration->second.find("Bus");
142 auto configurationAddress =
143 baseConfiguration->second.find("Address");
144
145 if (configurationBus == baseConfiguration->second.end() ||
146 configurationAddress == baseConfiguration->second.end())
147 {
148 std::cerr
149 << "error finding bus or address in configuration";
150 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
James Feistdf515152019-09-18 16:40:40 -0700170 auto findSensorName = baseConfiguration->second.find("Name");
171 if (findSensorName == baseConfiguration->second.end())
172 {
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 }
206 auto& sensor1 = sensors[sensorName];
207 sensor1 = nullptr;
208 sensor1 = std::make_unique<HwmonTempSensor>(
209 directory.string() + "/temp1_input", sensorType,
210 objectServer, dbusConnection, io, sensorName,
211 std::move(sensorThresholds), *interfacePath);
212 auto findSecondName = baseConfiguration->second.find("Name1");
213 if (findSecondName == baseConfiguration->second.end())
214 {
215 continue;
216 }
217 sensorName = std::get<std::string>(findSecondName->second);
218 auto& sensor2 = sensors[sensorName];
219 sensor2 = nullptr;
220 sensor2 = std::make_unique<HwmonTempSensor>(
221 directory.string() + "/temp2_input", sensorType,
222 objectServer, dbusConnection, io, sensorName,
223 std::vector<thresholds::Threshold>(), *interfacePath);
224 }
225 }));
226 getter->getConfiguration(
227 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700228}
229
James Feistb6c0b912019-07-09 12:21:44 -0700230int main()
James Feist6714a252018-09-10 15:26:18 -0700231{
232 boost::asio::io_service io;
233 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
234 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
235 sdbusplus::asio::object_server objectServer(systemBus);
236 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>
237 sensors;
238 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
239 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
240 std::make_unique<boost::container::flat_set<std::string>>();
241
242 io.post([&]() {
243 createSensors(io, objectServer, sensors, systemBus, nullptr);
244 });
245
246 boost::asio::deadline_timer filterTimer(io);
247 std::function<void(sdbusplus::message::message&)> eventHandler =
248 [&](sdbusplus::message::message& message) {
249 if (message.is_method_error())
250 {
251 std::cerr << "callback method error\n";
252 return;
253 }
254 sensorsChanged->insert(message.get_path());
255 // this implicitly cancels the timer
256 filterTimer.expires_from_now(boost::posix_time::seconds(1));
257
258 filterTimer.async_wait([&](const boost::system::error_code& ec) {
259 if (ec == boost::asio::error::operation_aborted)
260 {
261 /* we were canceled*/
262 return;
263 }
264 else if (ec)
265 {
266 std::cerr << "timer error\n";
267 return;
268 }
269 createSensors(io, objectServer, sensors, systemBus,
270 sensorsChanged);
271 });
272 };
273
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700274 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700275 {
276 auto match = std::make_unique<sdbusplus::bus::match::match>(
277 static_cast<sdbusplus::bus::bus&>(*systemBus),
278 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700279 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700280 eventHandler);
281 matches.emplace_back(std::move(match));
282 }
283
284 io.run();
285}