blob: 1b20aa820cf989745e70581b38d3bb5188eae2f8 [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 Venture7fa475d2019-08-10 08:49:01 -070031static constexpr std::array<const char*, 5> 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",
36 "xyz.openbmc_project.Configuration.EMC1413"};
James Feist6714a252018-09-10 15:26:18 -070037
38void createSensors(
39 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
40 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>&
41 sensors,
42 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
43 const std::unique_ptr<boost::container::flat_set<std::string>>&
44 sensorsChanged)
45{
46 bool firstScan = sensorsChanged == nullptr;
47 // use new data the first time, then refresh
48 ManagedObjectType sensorConfigurations;
49 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070050 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070051 {
52 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
53 useCache))
54 {
55 std::cerr << "error communicating to entity manager\n";
56 return;
57 }
58 useCache = true;
59 }
60 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070061 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070062 {
63 std::cerr << "No temperature sensors in system\n";
64 return;
65 }
66
James Feist37266ca2018-10-15 15:56:28 -070067 boost::container::flat_set<std::string> directories;
68
James Feist6714a252018-09-10 15:26:18 -070069 // iterate through all found temp sensors, and try to match them with
70 // configuration
71 for (auto& path : paths)
72 {
73 std::smatch match;
James Feist37266ca2018-10-15 15:56:28 -070074 const std::string& pathStr = path.string();
James Feist6714a252018-09-10 15:26:18 -070075 auto directory = path.parent_path();
James Feist6714a252018-09-10 15:26:18 -070076
James Feist37266ca2018-10-15 15:56:28 -070077 auto ret = directories.insert(directory.string());
78 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -070079 {
James Feist37266ca2018-10-15 15:56:28 -070080 continue; // already searched this path
81 }
82
James Feistb6c0b912019-07-09 12:21:44 -070083 fs::path device = directory / "device";
James Feist37266ca2018-10-15 15:56:28 -070084 std::string deviceName = fs::canonical(device).stem();
85 auto findHyphen = deviceName.find("-");
86 if (findHyphen == std::string::npos)
87 {
88 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -070089 continue;
90 }
James Feist37266ca2018-10-15 15:56:28 -070091 std::string busStr = deviceName.substr(0, findHyphen);
92 std::string addrStr = deviceName.substr(findHyphen + 1);
93
94 size_t bus = 0;
95 size_t addr = 0;
96 try
James Feist6714a252018-09-10 15:26:18 -070097 {
James Feist37266ca2018-10-15 15:56:28 -070098 bus = std::stoi(busStr);
99 addr = std::stoi(addrStr, 0, 16);
100 }
James Feistb6c0b912019-07-09 12:21:44 -0700101 catch (std::invalid_argument&)
James Feist37266ca2018-10-15 15:56:28 -0700102 {
James Feist6714a252018-09-10 15:26:18 -0700103 continue;
104 }
James Feist6714a252018-09-10 15:26:18 -0700105 const SensorData* sensorData = nullptr;
106 const std::string* interfacePath = nullptr;
James Feist37266ca2018-10-15 15:56:28 -0700107 const char* sensorType = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700108 const std::pair<std::string, boost::container::flat_map<
109 std::string, BasicVariantType>>*
110 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700111
James Feist37266ca2018-10-15 15:56:28 -0700112 for (const std::pair<sdbusplus::message::object_path, SensorData>&
113 sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700114 {
James Feist37266ca2018-10-15 15:56:28 -0700115 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116 for (const char* type : sensorTypes)
James Feist37266ca2018-10-15 15:56:28 -0700117 {
118 auto sensorBase = sensorData->find(type);
119 if (sensorBase != sensorData->end())
120 {
121 baseConfiguration = &(*sensorBase);
122 sensorType = type;
123 break;
124 }
125 }
126 if (baseConfiguration == nullptr)
127 {
128 std::cerr << "error finding base configuration for "
129 << deviceName << "\n";
130 continue;
131 }
132 auto configurationBus = baseConfiguration->second.find("Bus");
133 auto configurationAddress =
134 baseConfiguration->second.find("Address");
135
136 if (configurationBus == baseConfiguration->second.end() ||
137 configurationAddress == baseConfiguration->second.end())
138 {
139 std::cerr << "error finding bus or address in configuration";
140 continue;
141 }
142
James Feist3eb82622019-02-08 13:10:22 -0800143 if (std::get<uint64_t>(configurationBus->second) != bus ||
144 std::get<uint64_t>(configurationAddress->second) != addr)
James Feist37266ca2018-10-15 15:56:28 -0700145 {
146 continue;
147 }
148
149 interfacePath = &(sensor.first.str);
150 break;
151 }
152 if (interfacePath == nullptr)
153 {
154 std::cerr << "failed to find match for " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700155 continue;
156 }
157
158 auto findSensorName = baseConfiguration->second.find("Name");
159 if (findSensorName == baseConfiguration->second.end())
160 {
161 std::cerr << "could not determine configuration name for "
James Feist37266ca2018-10-15 15:56:28 -0700162 << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700163 continue;
164 }
James Feist3eb82622019-02-08 13:10:22 -0800165 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700166 // on rescans, only update sensors we were signaled by
167 auto findSensor = sensors.find(sensorName);
168 if (!firstScan && findSensor != sensors.end())
169 {
170 bool found = false;
171 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
172 it++)
173 {
174 if (boost::ends_with(*it, findSensor->second->name))
175 {
176 sensorsChanged->erase(it);
177 findSensor->second = nullptr;
178 found = true;
179 break;
180 }
181 }
182 if (!found)
183 {
184 continue;
185 }
186 }
187 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700188 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700189 {
190 std::cerr << "error populating thresholds for " << sensorName
191 << "\n";
192 }
193
194 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
James Feist37266ca2018-10-15 15:56:28 -0700195 directory.string() + "/temp1_input", sensorType, objectServer,
196 dbusConnection, io, sensorName, std::move(sensorThresholds),
197 *interfacePath);
198 auto findSecondName = baseConfiguration->second.find("Name1");
199 if (findSecondName == baseConfiguration->second.end())
200 {
201 continue;
202 }
203
James Feist3eb82622019-02-08 13:10:22 -0800204 sensorName = std::get<std::string>(findSecondName->second);
James Feist37266ca2018-10-15 15:56:28 -0700205 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
206 directory.string() + "/temp2_input", sensorType, objectServer,
207 dbusConnection, io, sensorName,
208 std::vector<thresholds::Threshold>(), *interfacePath);
James Feist6714a252018-09-10 15:26:18 -0700209 }
210}
211
James Feistb6c0b912019-07-09 12:21:44 -0700212int main()
James Feist6714a252018-09-10 15:26:18 -0700213{
214 boost::asio::io_service io;
215 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
216 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
217 sdbusplus::asio::object_server objectServer(systemBus);
218 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>
219 sensors;
220 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
221 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
222 std::make_unique<boost::container::flat_set<std::string>>();
223
224 io.post([&]() {
225 createSensors(io, objectServer, sensors, systemBus, nullptr);
226 });
227
228 boost::asio::deadline_timer filterTimer(io);
229 std::function<void(sdbusplus::message::message&)> eventHandler =
230 [&](sdbusplus::message::message& message) {
231 if (message.is_method_error())
232 {
233 std::cerr << "callback method error\n";
234 return;
235 }
236 sensorsChanged->insert(message.get_path());
237 // this implicitly cancels the timer
238 filterTimer.expires_from_now(boost::posix_time::seconds(1));
239
240 filterTimer.async_wait([&](const boost::system::error_code& ec) {
241 if (ec == boost::asio::error::operation_aborted)
242 {
243 /* we were canceled*/
244 return;
245 }
246 else if (ec)
247 {
248 std::cerr << "timer error\n";
249 return;
250 }
251 createSensors(io, objectServer, sensors, systemBus,
252 sensorsChanged);
253 });
254 };
255
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700256 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700257 {
258 auto match = std::make_unique<sdbusplus::bus::match::match>(
259 static_cast<sdbusplus::bus::bus&>(*systemBus),
260 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700261 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700262 eventHandler);
263 matches.emplace_back(std::move(match));
264 }
265
266 io.run();
267}