blob: 430793a11236517f49ab65fd1ffb75facf79cca4 [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
James Feist3eb82622019-02-08 13:10:22 -0800141 if (std::get<uint64_t>(configurationBus->second) != bus ||
142 std::get<uint64_t>(configurationAddress->second) != addr)
James Feist37266ca2018-10-15 15:56:28 -0700143 {
144 continue;
145 }
146
147 interfacePath = &(sensor.first.str);
148 break;
149 }
150 if (interfacePath == nullptr)
151 {
152 std::cerr << "failed to find match for " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700153 continue;
154 }
155
156 auto findSensorName = baseConfiguration->second.find("Name");
157 if (findSensorName == baseConfiguration->second.end())
158 {
159 std::cerr << "could not determine configuration name for "
James Feist37266ca2018-10-15 15:56:28 -0700160 << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700161 continue;
162 }
James Feist3eb82622019-02-08 13:10:22 -0800163 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700164 // on rescans, only update sensors we were signaled by
165 auto findSensor = sensors.find(sensorName);
166 if (!firstScan && findSensor != sensors.end())
167 {
168 bool found = false;
169 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
170 it++)
171 {
172 if (boost::ends_with(*it, findSensor->second->name))
173 {
174 sensorsChanged->erase(it);
175 findSensor->second = nullptr;
176 found = true;
177 break;
178 }
179 }
180 if (!found)
181 {
182 continue;
183 }
184 }
185 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700186 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700187 {
188 std::cerr << "error populating thresholds for " << sensorName
189 << "\n";
190 }
191
192 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
James Feist37266ca2018-10-15 15:56:28 -0700193 directory.string() + "/temp1_input", sensorType, objectServer,
194 dbusConnection, io, sensorName, std::move(sensorThresholds),
195 *interfacePath);
196 auto findSecondName = baseConfiguration->second.find("Name1");
197 if (findSecondName == baseConfiguration->second.end())
198 {
199 continue;
200 }
201
James Feist3eb82622019-02-08 13:10:22 -0800202 sensorName = std::get<std::string>(findSecondName->second);
James Feist37266ca2018-10-15 15:56:28 -0700203 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
204 directory.string() + "/temp2_input", sensorType, objectServer,
205 dbusConnection, io, sensorName,
206 std::vector<thresholds::Threshold>(), *interfacePath);
James Feist6714a252018-09-10 15:26:18 -0700207 }
208}
209
210int main(int argc, char** argv)
211{
212 boost::asio::io_service io;
213 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
214 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
215 sdbusplus::asio::object_server objectServer(systemBus);
216 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>
217 sensors;
218 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
219 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
220 std::make_unique<boost::container::flat_set<std::string>>();
221
222 io.post([&]() {
223 createSensors(io, objectServer, sensors, systemBus, nullptr);
224 });
225
226 boost::asio::deadline_timer filterTimer(io);
227 std::function<void(sdbusplus::message::message&)> eventHandler =
228 [&](sdbusplus::message::message& message) {
229 if (message.is_method_error())
230 {
231 std::cerr << "callback method error\n";
232 return;
233 }
234 sensorsChanged->insert(message.get_path());
235 // this implicitly cancels the timer
236 filterTimer.expires_from_now(boost::posix_time::seconds(1));
237
238 filterTimer.async_wait([&](const boost::system::error_code& ec) {
239 if (ec == boost::asio::error::operation_aborted)
240 {
241 /* we were canceled*/
242 return;
243 }
244 else if (ec)
245 {
246 std::cerr << "timer error\n";
247 return;
248 }
249 createSensors(io, objectServer, sensors, systemBus,
250 sensorsChanged);
251 });
252 };
253
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700254 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700255 {
256 auto match = std::make_unique<sdbusplus::bus::match::match>(
257 static_cast<sdbusplus::bus::bus&>(*systemBus),
258 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700259 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700260 eventHandler);
261 matches.emplace_back(std::move(match));
262 }
263
264 io.run();
265}