blob: 0a1963676e928e4d1cbd698d8de43857f4dbda52 [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
James Feistcf3bce62019-01-08 10:07:19 -080017#include "filesystem.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <HwmonTempSensor.hpp>
20#include <Utils.hpp>
21#include <boost/algorithm/string/predicate.hpp>
22#include <boost/algorithm/string/replace.hpp>
23#include <boost/container/flat_set.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <fstream>
25#include <regex>
26#include <sdbusplus/asio/connection.hpp>
27#include <sdbusplus/asio/object_server.hpp>
28
29static constexpr bool DEBUG = false;
30
James Feistcf3bce62019-01-08 10:07:19 -080031namespace fs = std::filesystem;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070032static constexpr std::array<const char*, 2> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070033 "xyz.openbmc_project.Configuration.TMP75",
34 "xyz.openbmc_project.Configuration.TMP421"};
James Feist6714a252018-09-10 15:26:18 -070035
36void createSensors(
37 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
38 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>&
39 sensors,
40 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
41 const std::unique_ptr<boost::container::flat_set<std::string>>&
42 sensorsChanged)
43{
44 bool firstScan = sensorsChanged == nullptr;
45 // use new data the first time, then refresh
46 ManagedObjectType sensorConfigurations;
47 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070048 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070049 {
50 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
51 useCache))
52 {
53 std::cerr << "error communicating to entity manager\n";
54 return;
55 }
56 useCache = true;
57 }
58 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070059 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070060 {
61 std::cerr << "No temperature sensors in system\n";
62 return;
63 }
64
James Feist37266ca2018-10-15 15:56:28 -070065 boost::container::flat_set<std::string> directories;
66
James Feist6714a252018-09-10 15:26:18 -070067 // iterate through all found temp sensors, and try to match them with
68 // configuration
69 for (auto& path : paths)
70 {
71 std::smatch match;
James Feist37266ca2018-10-15 15:56:28 -070072 const std::string& pathStr = path.string();
James Feist6714a252018-09-10 15:26:18 -070073 auto directory = path.parent_path();
James Feist6714a252018-09-10 15:26:18 -070074
James Feist37266ca2018-10-15 15:56:28 -070075 auto ret = directories.insert(directory.string());
76 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -070077 {
James Feist37266ca2018-10-15 15:56:28 -070078 continue; // already searched this path
79 }
80
81 auto device = fs::path(directory / "device");
82 std::string deviceName = fs::canonical(device).stem();
83 auto findHyphen = deviceName.find("-");
84 if (findHyphen == std::string::npos)
85 {
86 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -070087 continue;
88 }
James Feist37266ca2018-10-15 15:56:28 -070089 std::string busStr = deviceName.substr(0, findHyphen);
90 std::string addrStr = deviceName.substr(findHyphen + 1);
91
92 size_t bus = 0;
93 size_t addr = 0;
94 try
James Feist6714a252018-09-10 15:26:18 -070095 {
James Feist37266ca2018-10-15 15:56:28 -070096 bus = std::stoi(busStr);
97 addr = std::stoi(addrStr, 0, 16);
98 }
99 catch (std::invalid_argument)
100 {
James Feist6714a252018-09-10 15:26:18 -0700101 continue;
102 }
James Feist6714a252018-09-10 15:26:18 -0700103 const SensorData* sensorData = nullptr;
104 const std::string* interfacePath = nullptr;
James Feist37266ca2018-10-15 15:56:28 -0700105 const char* sensorType = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700106 const std::pair<std::string, boost::container::flat_map<
107 std::string, BasicVariantType>>*
108 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700109
James Feist37266ca2018-10-15 15:56:28 -0700110 for (const std::pair<sdbusplus::message::object_path, SensorData>&
111 sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700112 {
James Feist37266ca2018-10-15 15:56:28 -0700113 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700114 for (const char* type : sensorTypes)
James Feist37266ca2018-10-15 15:56:28 -0700115 {
116 auto sensorBase = sensorData->find(type);
117 if (sensorBase != sensorData->end())
118 {
119 baseConfiguration = &(*sensorBase);
120 sensorType = type;
121 break;
122 }
123 }
124 if (baseConfiguration == nullptr)
125 {
126 std::cerr << "error finding base configuration for "
127 << deviceName << "\n";
128 continue;
129 }
130 auto configurationBus = 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 << "error finding bus or address in configuration";
138 continue;
139 }
140
141 if (sdbusplus::message::variant_ns::get<uint64_t>(
142 configurationBus->second) != bus ||
143 sdbusplus::message::variant_ns::get<uint64_t>(
144 configurationAddress->second) != addr)
145 {
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 }
165 std::string sensorName =
166 sdbusplus::message::variant_ns::get<std::string>(
167 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(); it != sensorsChanged->end();
174 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;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700190 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700191 {
192 std::cerr << "error populating thresholds for " << sensorName
193 << "\n";
194 }
195
196 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
James Feist37266ca2018-10-15 15:56:28 -0700197 directory.string() + "/temp1_input", sensorType, objectServer,
198 dbusConnection, io, sensorName, std::move(sensorThresholds),
199 *interfacePath);
200 auto findSecondName = baseConfiguration->second.find("Name1");
201 if (findSecondName == baseConfiguration->second.end())
202 {
203 continue;
204 }
205
206 sensorName = sdbusplus::message::variant_ns::get<std::string>(
207 findSecondName->second);
208 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
209 directory.string() + "/temp2_input", sensorType, objectServer,
210 dbusConnection, io, sensorName,
211 std::vector<thresholds::Threshold>(), *interfacePath);
James Feist6714a252018-09-10 15:26:18 -0700212 }
213}
214
215int main(int argc, char** argv)
216{
217 boost::asio::io_service io;
218 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
219 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
220 sdbusplus::asio::object_server objectServer(systemBus);
221 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>
222 sensors;
223 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
224 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
225 std::make_unique<boost::container::flat_set<std::string>>();
226
227 io.post([&]() {
228 createSensors(io, objectServer, sensors, systemBus, nullptr);
229 });
230
231 boost::asio::deadline_timer filterTimer(io);
232 std::function<void(sdbusplus::message::message&)> eventHandler =
233 [&](sdbusplus::message::message& message) {
234 if (message.is_method_error())
235 {
236 std::cerr << "callback method error\n";
237 return;
238 }
239 sensorsChanged->insert(message.get_path());
240 // this implicitly cancels the timer
241 filterTimer.expires_from_now(boost::posix_time::seconds(1));
242
243 filterTimer.async_wait([&](const boost::system::error_code& ec) {
244 if (ec == boost::asio::error::operation_aborted)
245 {
246 /* we were canceled*/
247 return;
248 }
249 else if (ec)
250 {
251 std::cerr << "timer error\n";
252 return;
253 }
254 createSensors(io, objectServer, sensors, systemBus,
255 sensorsChanged);
256 });
257 };
258
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700259 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700260 {
261 auto match = std::make_unique<sdbusplus::bus::match::match>(
262 static_cast<sdbusplus::bus::bus&>(*systemBus),
263 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700264 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700265 eventHandler);
266 matches.emplace_back(std::move(match));
267 }
268
269 io.run();
270}