blob: 2b1d7c15ac69116b41ef0e1d42c57a1a70574116 [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;
John Wang55ab2af2019-04-18 14:22:31 +080031static constexpr std::array<const char*, 4> 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",
34 "xyz.openbmc_project.Configuration.TMP112",
35 "xyz.openbmc_project.Configuration.EMC1413"};
James Feist6714a252018-09-10 15:26:18 -070036
37void createSensors(
38 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
39 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>&
40 sensors,
41 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
42 const std::unique_ptr<boost::container::flat_set<std::string>>&
43 sensorsChanged)
44{
45 bool firstScan = sensorsChanged == nullptr;
46 // use new data the first time, then refresh
47 ManagedObjectType sensorConfigurations;
48 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070049 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070050 {
51 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
52 useCache))
53 {
54 std::cerr << "error communicating to entity manager\n";
55 return;
56 }
57 useCache = true;
58 }
59 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070060 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070061 {
62 std::cerr << "No temperature sensors in system\n";
63 return;
64 }
65
James Feist37266ca2018-10-15 15:56:28 -070066 boost::container::flat_set<std::string> directories;
67
James Feist6714a252018-09-10 15:26:18 -070068 // iterate through all found temp sensors, and try to match them with
69 // configuration
70 for (auto& path : paths)
71 {
72 std::smatch match;
James Feist37266ca2018-10-15 15:56:28 -070073 const std::string& pathStr = path.string();
James Feist6714a252018-09-10 15:26:18 -070074 auto directory = path.parent_path();
James Feist6714a252018-09-10 15:26:18 -070075
James Feist37266ca2018-10-15 15:56:28 -070076 auto ret = directories.insert(directory.string());
77 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -070078 {
James Feist37266ca2018-10-15 15:56:28 -070079 continue; // already searched this path
80 }
81
82 auto device = fs::path(directory / "device");
83 std::string deviceName = fs::canonical(device).stem();
84 auto findHyphen = deviceName.find("-");
85 if (findHyphen == std::string::npos)
86 {
87 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -070088 continue;
89 }
James Feist37266ca2018-10-15 15:56:28 -070090 std::string busStr = deviceName.substr(0, findHyphen);
91 std::string addrStr = deviceName.substr(findHyphen + 1);
92
93 size_t bus = 0;
94 size_t addr = 0;
95 try
James Feist6714a252018-09-10 15:26:18 -070096 {
James Feist37266ca2018-10-15 15:56:28 -070097 bus = std::stoi(busStr);
98 addr = std::stoi(addrStr, 0, 16);
99 }
100 catch (std::invalid_argument)
101 {
James Feist6714a252018-09-10 15:26:18 -0700102 continue;
103 }
James Feist6714a252018-09-10 15:26:18 -0700104 const SensorData* sensorData = nullptr;
105 const std::string* interfacePath = nullptr;
James Feist37266ca2018-10-15 15:56:28 -0700106 const char* sensorType = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700107 const std::pair<std::string, boost::container::flat_map<
108 std::string, BasicVariantType>>*
109 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700110
James Feist37266ca2018-10-15 15:56:28 -0700111 for (const std::pair<sdbusplus::message::object_path, SensorData>&
112 sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700113 {
James Feist37266ca2018-10-15 15:56:28 -0700114 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700115 for (const char* type : sensorTypes)
James Feist37266ca2018-10-15 15:56:28 -0700116 {
117 auto sensorBase = sensorData->find(type);
118 if (sensorBase != sensorData->end())
119 {
120 baseConfiguration = &(*sensorBase);
121 sensorType = type;
122 break;
123 }
124 }
125 if (baseConfiguration == nullptr)
126 {
127 std::cerr << "error finding base configuration for "
128 << deviceName << "\n";
129 continue;
130 }
131 auto configurationBus = baseConfiguration->second.find("Bus");
132 auto configurationAddress =
133 baseConfiguration->second.find("Address");
134
135 if (configurationBus == baseConfiguration->second.end() ||
136 configurationAddress == baseConfiguration->second.end())
137 {
138 std::cerr << "error finding bus or address in configuration";
139 continue;
140 }
141
James Feist3eb82622019-02-08 13:10:22 -0800142 if (std::get<uint64_t>(configurationBus->second) != bus ||
143 std::get<uint64_t>(configurationAddress->second) != addr)
James Feist37266ca2018-10-15 15:56:28 -0700144 {
145 continue;
146 }
147
148 interfacePath = &(sensor.first.str);
149 break;
150 }
151 if (interfacePath == nullptr)
152 {
153 std::cerr << "failed to find match for " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700154 continue;
155 }
156
157 auto findSensorName = baseConfiguration->second.find("Name");
158 if (findSensorName == baseConfiguration->second.end())
159 {
160 std::cerr << "could not determine configuration name for "
James Feist37266ca2018-10-15 15:56:28 -0700161 << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700162 continue;
163 }
James Feist3eb82622019-02-08 13:10:22 -0800164 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700165 // on rescans, only update sensors we were signaled by
166 auto findSensor = sensors.find(sensorName);
167 if (!firstScan && findSensor != sensors.end())
168 {
169 bool found = false;
170 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
171 it++)
172 {
173 if (boost::ends_with(*it, findSensor->second->name))
174 {
175 sensorsChanged->erase(it);
176 findSensor->second = nullptr;
177 found = true;
178 break;
179 }
180 }
181 if (!found)
182 {
183 continue;
184 }
185 }
186 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700187 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700188 {
189 std::cerr << "error populating thresholds for " << sensorName
190 << "\n";
191 }
192
193 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
James Feist37266ca2018-10-15 15:56:28 -0700194 directory.string() + "/temp1_input", sensorType, objectServer,
195 dbusConnection, io, sensorName, std::move(sensorThresholds),
196 *interfacePath);
197 auto findSecondName = baseConfiguration->second.find("Name1");
198 if (findSecondName == baseConfiguration->second.end())
199 {
200 continue;
201 }
202
James Feist3eb82622019-02-08 13:10:22 -0800203 sensorName = std::get<std::string>(findSecondName->second);
James Feist37266ca2018-10-15 15:56:28 -0700204 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
205 directory.string() + "/temp2_input", sensorType, objectServer,
206 dbusConnection, io, sensorName,
207 std::vector<thresholds::Threshold>(), *interfacePath);
James Feist6714a252018-09-10 15:26:18 -0700208 }
209}
210
211int main(int argc, char** argv)
212{
213 boost::asio::io_service io;
214 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
215 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
216 sdbusplus::asio::object_server objectServer(systemBus);
217 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>
218 sensors;
219 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
220 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
221 std::make_unique<boost::container::flat_set<std::string>>();
222
223 io.post([&]() {
224 createSensors(io, objectServer, sensors, systemBus, nullptr);
225 });
226
227 boost::asio::deadline_timer filterTimer(io);
228 std::function<void(sdbusplus::message::message&)> eventHandler =
229 [&](sdbusplus::message::message& message) {
230 if (message.is_method_error())
231 {
232 std::cerr << "callback method error\n";
233 return;
234 }
235 sensorsChanged->insert(message.get_path());
236 // this implicitly cancels the timer
237 filterTimer.expires_from_now(boost::posix_time::seconds(1));
238
239 filterTimer.async_wait([&](const boost::system::error_code& ec) {
240 if (ec == boost::asio::error::operation_aborted)
241 {
242 /* we were canceled*/
243 return;
244 }
245 else if (ec)
246 {
247 std::cerr << "timer error\n";
248 return;
249 }
250 createSensors(io, objectServer, sensors, systemBus,
251 sensorsChanged);
252 });
253 };
254
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700255 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700256 {
257 auto match = std::make_unique<sdbusplus::bus::match::match>(
258 static_cast<sdbusplus::bus::bus&>(*systemBus),
259 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700260 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700261 eventHandler);
262 matches.emplace_back(std::move(match));
263 }
264
265 io.run();
266}