blob: eacfa2ada3eb8d58ae59377ae9ecd6abb952d49f [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;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070031static constexpr std::array<const char*, 2> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070032 "xyz.openbmc_project.Configuration.TMP75",
33 "xyz.openbmc_project.Configuration.TMP421"};
James Feist6714a252018-09-10 15:26:18 -070034
35void createSensors(
36 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
37 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>&
38 sensors,
39 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
40 const std::unique_ptr<boost::container::flat_set<std::string>>&
41 sensorsChanged)
42{
43 bool firstScan = sensorsChanged == nullptr;
44 // use new data the first time, then refresh
45 ManagedObjectType sensorConfigurations;
46 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070047 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070048 {
49 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
50 useCache))
51 {
52 std::cerr << "error communicating to entity manager\n";
53 return;
54 }
55 useCache = true;
56 }
57 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070058 if (!findFiles(fs::path("/sys/class/hwmon"), R"(temp\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070059 {
60 std::cerr << "No temperature sensors in system\n";
61 return;
62 }
63
James Feist37266ca2018-10-15 15:56:28 -070064 boost::container::flat_set<std::string> directories;
65
James Feist6714a252018-09-10 15:26:18 -070066 // iterate through all found temp sensors, and try to match them with
67 // configuration
68 for (auto& path : paths)
69 {
70 std::smatch match;
James Feist37266ca2018-10-15 15:56:28 -070071 const std::string& pathStr = path.string();
James Feist6714a252018-09-10 15:26:18 -070072 auto directory = path.parent_path();
James Feist6714a252018-09-10 15:26:18 -070073
James Feist37266ca2018-10-15 15:56:28 -070074 auto ret = directories.insert(directory.string());
75 if (!ret.second)
James Feist6714a252018-09-10 15:26:18 -070076 {
James Feist37266ca2018-10-15 15:56:28 -070077 continue; // already searched this path
78 }
79
80 auto device = fs::path(directory / "device");
81 std::string deviceName = fs::canonical(device).stem();
82 auto findHyphen = deviceName.find("-");
83 if (findHyphen == std::string::npos)
84 {
85 std::cerr << "found bad device " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -070086 continue;
87 }
James Feist37266ca2018-10-15 15:56:28 -070088 std::string busStr = deviceName.substr(0, findHyphen);
89 std::string addrStr = deviceName.substr(findHyphen + 1);
90
91 size_t bus = 0;
92 size_t addr = 0;
93 try
James Feist6714a252018-09-10 15:26:18 -070094 {
James Feist37266ca2018-10-15 15:56:28 -070095 bus = std::stoi(busStr);
96 addr = std::stoi(addrStr, 0, 16);
97 }
98 catch (std::invalid_argument)
99 {
James Feist6714a252018-09-10 15:26:18 -0700100 continue;
101 }
James Feist6714a252018-09-10 15:26:18 -0700102 const SensorData* sensorData = nullptr;
103 const std::string* interfacePath = nullptr;
James Feist37266ca2018-10-15 15:56:28 -0700104 const char* sensorType = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700105 const std::pair<std::string, boost::container::flat_map<
106 std::string, BasicVariantType>>*
107 baseConfiguration = nullptr;
James Feist6714a252018-09-10 15:26:18 -0700108
James Feist37266ca2018-10-15 15:56:28 -0700109 for (const std::pair<sdbusplus::message::object_path, SensorData>&
110 sensor : sensorConfigurations)
James Feist6714a252018-09-10 15:26:18 -0700111 {
James Feist37266ca2018-10-15 15:56:28 -0700112 sensorData = &(sensor.second);
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700113 for (const char* type : sensorTypes)
James Feist37266ca2018-10-15 15:56:28 -0700114 {
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 = baseConfiguration->second.find("Bus");
130 auto configurationAddress =
131 baseConfiguration->second.find("Address");
132
133 if (configurationBus == baseConfiguration->second.end() ||
134 configurationAddress == baseConfiguration->second.end())
135 {
136 std::cerr << "error finding bus or address in configuration";
137 continue;
138 }
139
James Feist3eb82622019-02-08 13:10:22 -0800140 if (std::get<uint64_t>(configurationBus->second) != bus ||
141 std::get<uint64_t>(configurationAddress->second) != addr)
James Feist37266ca2018-10-15 15:56:28 -0700142 {
143 continue;
144 }
145
146 interfacePath = &(sensor.first.str);
147 break;
148 }
149 if (interfacePath == nullptr)
150 {
151 std::cerr << "failed to find match for " << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700152 continue;
153 }
154
155 auto findSensorName = baseConfiguration->second.find("Name");
156 if (findSensorName == baseConfiguration->second.end())
157 {
158 std::cerr << "could not determine configuration name for "
James Feist37266ca2018-10-15 15:56:28 -0700159 << deviceName << "\n";
James Feist6714a252018-09-10 15:26:18 -0700160 continue;
161 }
James Feist3eb82622019-02-08 13:10:22 -0800162 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700163 // on rescans, only update sensors we were signaled by
164 auto findSensor = sensors.find(sensorName);
165 if (!firstScan && findSensor != sensors.end())
166 {
167 bool found = false;
168 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
169 it++)
170 {
171 if (boost::ends_with(*it, findSensor->second->name))
172 {
173 sensorsChanged->erase(it);
174 findSensor->second = nullptr;
175 found = true;
176 break;
177 }
178 }
179 if (!found)
180 {
181 continue;
182 }
183 }
184 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700185 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700186 {
187 std::cerr << "error populating thresholds for " << sensorName
188 << "\n";
189 }
190
191 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
James Feist37266ca2018-10-15 15:56:28 -0700192 directory.string() + "/temp1_input", sensorType, objectServer,
193 dbusConnection, io, sensorName, std::move(sensorThresholds),
194 *interfacePath);
195 auto findSecondName = baseConfiguration->second.find("Name1");
196 if (findSecondName == baseConfiguration->second.end())
197 {
198 continue;
199 }
200
James Feist3eb82622019-02-08 13:10:22 -0800201 sensorName = std::get<std::string>(findSecondName->second);
James Feist37266ca2018-10-15 15:56:28 -0700202 sensors[sensorName] = std::make_unique<HwmonTempSensor>(
203 directory.string() + "/temp2_input", sensorType, objectServer,
204 dbusConnection, io, sensorName,
205 std::vector<thresholds::Threshold>(), *interfacePath);
James Feist6714a252018-09-10 15:26:18 -0700206 }
207}
208
209int main(int argc, char** argv)
210{
211 boost::asio::io_service io;
212 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
213 systemBus->request_name("xyz.openbmc_project.HwmonTempSensor");
214 sdbusplus::asio::object_server objectServer(systemBus);
215 boost::container::flat_map<std::string, std::unique_ptr<HwmonTempSensor>>
216 sensors;
217 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
218 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
219 std::make_unique<boost::container::flat_set<std::string>>();
220
221 io.post([&]() {
222 createSensors(io, objectServer, sensors, systemBus, nullptr);
223 });
224
225 boost::asio::deadline_timer filterTimer(io);
226 std::function<void(sdbusplus::message::message&)> eventHandler =
227 [&](sdbusplus::message::message& message) {
228 if (message.is_method_error())
229 {
230 std::cerr << "callback method error\n";
231 return;
232 }
233 sensorsChanged->insert(message.get_path());
234 // this implicitly cancels the timer
235 filterTimer.expires_from_now(boost::posix_time::seconds(1));
236
237 filterTimer.async_wait([&](const boost::system::error_code& ec) {
238 if (ec == boost::asio::error::operation_aborted)
239 {
240 /* we were canceled*/
241 return;
242 }
243 else if (ec)
244 {
245 std::cerr << "timer error\n";
246 return;
247 }
248 createSensors(io, objectServer, sensors, systemBus,
249 sensorsChanged);
250 });
251 };
252
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700253 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700254 {
255 auto match = std::make_unique<sdbusplus::bus::match::match>(
256 static_cast<sdbusplus::bus::bus&>(*systemBus),
257 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700258 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700259 eventHandler);
260 matches.emplace_back(std::move(match));
261 }
262
263 io.run();
264}