blob: 19e3505c2a680a0cc7cb7611ca1d8693771243c0 [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
17#include <HwmonTempSensor.hpp>
18#include <Utils.hpp>
19#include <boost/algorithm/string/predicate.hpp>
20#include <boost/algorithm/string/replace.hpp>
21#include <boost/container/flat_set.hpp>
James Feist24f02f22019-04-15 11:05:39 -070022#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070023#include <fstream>
24#include <regex>
25#include <sdbusplus/asio/connection.hpp>
26#include <sdbusplus/asio/object_server.hpp>
27
28static constexpr bool DEBUG = false;
29
James Feistcf3bce62019-01-08 10:07:19 -080030namespace fs = std::filesystem;
Patrick Venture3546adb2019-08-14 18:35:29 -070031static constexpr std::array<const char*, 7> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070032 "xyz.openbmc_project.Configuration.TMP75",
John Wang55ab2af2019-04-18 14:22:31 +080033 "xyz.openbmc_project.Configuration.TMP421",
Patrick Venture7fa475d2019-08-10 08:49:01 -070034 "xyz.openbmc_project.Configuration.TMP441",
John Wang55ab2af2019-04-18 14:22:31 +080035 "xyz.openbmc_project.Configuration.TMP112",
Patrick Venture3546adb2019-08-14 18:35:29 -070036 "xyz.openbmc_project.Configuration.TMP175",
Patrick Venturebd1a9d52019-08-14 18:10:26 -070037 "xyz.openbmc_project.Configuration.EMC1413",
38 "xyz.openbmc_project.Configuration.MAX31725"};
James Feist6714a252018-09-10 15:26:18 -070039
40void createSensors(
41 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
42 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>&
43 sensors,
44 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
45 const std::unique_ptr<boost::container::flat_set<std::string>>&
46 sensorsChanged)
47{
James Feistdf515152019-09-18 16:40:40 -070048 auto getter = std::make_shared<GetSensorConfiguration>(
49 dbusConnection,
50 std::move([&io, &objectServer, &sensors, &dbusConnection,
51 &sensorsChanged](
52 const ManagedObjectType& sensorConfigurations) {
53 bool firstScan = sensorsChanged == nullptr;
James Feist6714a252018-09-10 15:26:18 -070054
James Feistdf515152019-09-18 16:40:40 -070055 std::vector<fs::path> paths;
56 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)",
57 paths))
James Feist37266ca2018-10-15 15:56:28 -070058 {
James Feistdf515152019-09-18 16:40:40 -070059 std::cerr << "No temperature sensors in system\n";
60 return;
61 }
62
63 boost::container::flat_set<std::string> directories;
64
65 // iterate through all found temp sensors, and try to match them
66 // with configuration
67 for (auto& path : paths)
68 {
69 std::smatch match;
70 const std::string& pathStr = path.string();
71 auto directory = path.parent_path();
72
73 auto ret = directories.insert(directory.string());
74 if (!ret.second)
James Feist37266ca2018-10-15 15:56:28 -070075 {
James Feistdf515152019-09-18 16:40:40 -070076 continue; // already searched this path
77 }
78
79 fs::path device = directory / "device";
80 std::string deviceName = fs::canonical(device).stem();
81 auto findHyphen = deviceName.find("-");
82 if (findHyphen == std::string::npos)
83 {
84 std::cerr << "found bad device " << deviceName << "\n";
85 continue;
86 }
87 std::string busStr = deviceName.substr(0, findHyphen);
88 std::string addrStr = deviceName.substr(findHyphen + 1);
89
90 size_t bus = 0;
91 size_t addr = 0;
92 try
93 {
94 bus = std::stoi(busStr);
95 addr = std::stoi(addrStr, 0, 16);
96 }
97 catch (std::invalid_argument&)
98 {
99 continue;
100 }
101 const SensorData* sensorData = nullptr;
102 const std::string* interfacePath = nullptr;
103 const char* sensorType = nullptr;
104 const std::pair<
105 std::string,
106 boost::container::flat_map<std::string, BasicVariantType>>*
107 baseConfiguration = nullptr;
108
109 for (const std::pair<sdbusplus::message::object_path,
110 SensorData>& sensor : sensorConfigurations)
111 {
112 sensorData = &(sensor.second);
113 for (const char* type : sensorTypes)
114 {
115 auto sensorBase = sensorData->find(type);
116 if (sensorBase != sensorData->end())
117 {
118 baseConfiguration = &(*sensorBase);
119 sensorType = type;
120 break;
121 }
122 }
123 if (baseConfiguration == nullptr)
124 {
125 std::cerr << "error finding base configuration for "
126 << deviceName << "\n";
127 continue;
128 }
129 auto configurationBus =
130 baseConfiguration->second.find("Bus");
131 auto configurationAddress =
132 baseConfiguration->second.find("Address");
133
134 if (configurationBus == baseConfiguration->second.end() ||
135 configurationAddress == baseConfiguration->second.end())
136 {
137 std::cerr
138 << "error finding bus or address in configuration";
139 continue;
140 }
141
142 if (std::get<uint64_t>(configurationBus->second) != bus ||
143 std::get<uint64_t>(configurationAddress->second) !=
144 addr)
145 {
146 continue;
147 }
148
149 interfacePath = &(sensor.first.str);
James Feist37266ca2018-10-15 15:56:28 -0700150 break;
151 }
James Feistdf515152019-09-18 16:40:40 -0700152 if (interfacePath == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700153 {
James Feistdf515152019-09-18 16:40:40 -0700154 std::cerr << "failed to find match for " << deviceName
155 << "\n";
156 continue;
James Feist6714a252018-09-10 15:26:18 -0700157 }
James Feist6714a252018-09-10 15:26:18 -0700158
James Feistdf515152019-09-18 16:40:40 -0700159 auto findSensorName = baseConfiguration->second.find("Name");
160 if (findSensorName == baseConfiguration->second.end())
161 {
162 std::cerr << "could not determine configuration name for "
163 << deviceName << "\n";
164 continue;
165 }
166 std::string sensorName =
167 std::get<std::string>(findSensorName->second);
168 // on rescans, only update sensors we were signaled by
169 auto findSensor = sensors.find(sensorName);
170 if (!firstScan && findSensor != sensors.end())
171 {
172 bool found = false;
173 for (auto it = sensorsChanged->begin();
174 it != sensorsChanged->end(); it++)
175 {
176 if (boost::ends_with(*it, findSensor->second->name))
177 {
178 sensorsChanged->erase(it);
179 findSensor->second = nullptr;
180 found = true;
181 break;
182 }
183 }
184 if (!found)
185 {
186 continue;
187 }
188 }
189 std::vector<thresholds::Threshold> sensorThresholds;
190 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
191 {
192 std::cerr << "error populating thresholds for "
193 << sensorName << "\n";
194 }
195 auto& sensor1 = sensors[sensorName];
196 sensor1 = nullptr;
197 sensor1 = std::make_unique<HwmonTempSensor>(
198 directory.string() + "/temp1_input", sensorType,
199 objectServer, dbusConnection, io, sensorName,
200 std::move(sensorThresholds), *interfacePath);
201 auto findSecondName = baseConfiguration->second.find("Name1");
202 if (findSecondName == baseConfiguration->second.end())
203 {
204 continue;
205 }
206 sensorName = std::get<std::string>(findSecondName->second);
207 auto& sensor2 = sensors[sensorName];
208 sensor2 = nullptr;
209 sensor2 = std::make_unique<HwmonTempSensor>(
210 directory.string() + "/temp2_input", sensorType,
211 objectServer, dbusConnection, io, sensorName,
212 std::vector<thresholds::Threshold>(), *interfacePath);
213 }
214 }));
215 getter->getConfiguration(
216 std::vector<std::string>(sensorTypes.begin(), sensorTypes.end()));
James Feist6714a252018-09-10 15:26:18 -0700217}
218
James Feistb6c0b912019-07-09 12:21:44 -0700219int main()
James Feist6714a252018-09-10 15:26:18 -0700220{
221 boost::asio::io_service io;
222 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
223 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
224 sdbusplus::asio::object_server objectServer(systemBus);
225 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>
226 sensors;
227 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
228 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
229 std::make_unique<boost::container::flat_set<std::string>>();
230
231 io.post([&]() {
232 createSensors(io, objectServer, sensors, systemBus, nullptr);
233 });
234
235 boost::asio::deadline_timer filterTimer(io);
236 std::function<void(sdbusplus::message::message&)> eventHandler =
237 [&](sdbusplus::message::message& message) {
238 if (message.is_method_error())
239 {
240 std::cerr << "callback method error\n";
241 return;
242 }
243 sensorsChanged->insert(message.get_path());
244 // this implicitly cancels the timer
245 filterTimer.expires_from_now(boost::posix_time::seconds(1));
246
247 filterTimer.async_wait([&](const boost::system::error_code& ec) {
248 if (ec == boost::asio::error::operation_aborted)
249 {
250 /* we were canceled*/
251 return;
252 }
253 else if (ec)
254 {
255 std::cerr << "timer error\n";
256 return;
257 }
258 createSensors(io, objectServer, sensors, systemBus,
259 sensorsChanged);
260 });
261 };
262
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700263 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700264 {
265 auto match = std::make_unique<sdbusplus::bus::match::match>(
266 static_cast<sdbusplus::bus::bus&>(*systemBus),
267 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700268 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700269 eventHandler);
270 matches.emplace_back(std::move(match));
271 }
272
273 io.run();
274}