blob: 61ebd6c4b31439c88f77cf0aca151109b59f2c87 [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 <ADCSensor.hpp>
20#include <Utils.hpp>
21#include <VariantVisitors.hpp>
22#include <boost/algorithm/string/predicate.hpp>
23#include <boost/algorithm/string/replace.hpp>
24#include <boost/container/flat_set.hpp>
James Feist6714a252018-09-10 15:26:18 -070025#include <fstream>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040026#include <optional>
James Feist6714a252018-09-10 15:26:18 -070027#include <regex>
28#include <sdbusplus/asio/connection.hpp>
29#include <sdbusplus/asio/object_server.hpp>
30
31static constexpr bool DEBUG = false;
32
James Feistcf3bce62019-01-08 10:07:19 -080033namespace fs = std::filesystem;
James Feist3eb82622019-02-08 13:10:22 -080034
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070035static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070036 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070037static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070038
James Feist08aec6f2019-01-30 16:17:04 -080039// filter out adc from any other voltage sensor
40bool isAdc(const fs::path& parentPath)
41{
42 fs::path namePath = parentPath / "name";
43
44 std::ifstream nameFile(namePath);
45 if (!nameFile.good())
46 {
47 std::cerr << "Failure reading " << namePath.string() << "\n";
48 return false;
49 }
50
51 std::string name;
52 std::getline(nameFile, name);
53
54 return name == "iio_hwmon";
55}
56
James Feist6714a252018-09-10 15:26:18 -070057void createSensors(
58 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
59 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
60 sensors,
61 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
62 const std::unique_ptr<boost::container::flat_set<std::string>>&
63 sensorsChanged)
64{
65 bool firstScan = sensorsChanged == nullptr;
66 // use new data the first time, then refresh
67 ManagedObjectType sensorConfigurations;
68 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070069 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070070 {
71 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
72 useCache))
73 {
74 std::cerr << "error communicating to entity manager\n";
75 return;
76 }
77 useCache = true;
78 }
79 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070081 {
82 std::cerr << "No temperature sensors in system\n";
83 return;
84 }
85
86 // iterate through all found adc sensors, and try to match them with
87 // configuration
88 for (auto& path : paths)
89 {
James Feist08aec6f2019-01-30 16:17:04 -080090 if (!isAdc(path.parent_path()))
91 {
92 continue;
93 }
James Feist6714a252018-09-10 15:26:18 -070094 std::smatch match;
95 std::string pathStr = path.string();
96
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -070098 std::string indexStr = *(match.begin() + 1);
99
100 auto directory = path.parent_path();
101 // convert to 0 based
102 size_t index = std::stoul(indexStr) - 1;
James Feist6714a252018-09-10 15:26:18 -0700103
104 const SensorData* sensorData = nullptr;
105 const std::string* interfacePath = nullptr;
James Feist08aec6f2019-01-30 16:17:04 -0800106 const std::pair<std::string, boost::container::flat_map<
107 std::string, BasicVariantType>>*
108 baseConfiguration;
James Feist6714a252018-09-10 15:26:18 -0700109 for (const std::pair<sdbusplus::message::object_path, SensorData>&
110 sensor : sensorConfigurations)
111 {
James Feist08aec6f2019-01-30 16:17:04 -0800112 // clear it out each loop
113 baseConfiguration = nullptr;
114
115 // find base configuration
116 for (const char* type : sensorTypes)
117 {
118 auto sensorBase = sensor.second.find(type);
119 if (sensorBase != sensor.second.end())
120 {
121 baseConfiguration = &(*sensorBase);
122 break;
123 }
124 }
125 if (baseConfiguration == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700126 {
127 continue;
128 }
James Feist08aec6f2019-01-30 16:17:04 -0800129 auto findIndex = baseConfiguration->second.find("Index");
130 if (findIndex == baseConfiguration->second.end())
131 {
132 std::cerr << "Base configuration missing Index"
133 << baseConfiguration->first << "\n";
134 continue;
135 }
136
James Feist3eb82622019-02-08 13:10:22 -0800137 unsigned int number =
138 std::visit(VariantToUnsignedIntVisitor(), findIndex->second);
James Feist08aec6f2019-01-30 16:17:04 -0800139
140 if (number != index)
141 {
142 continue;
143 }
144
James Feist6714a252018-09-10 15:26:18 -0700145 sensorData = &(sensor.second);
146 interfacePath = &(sensor.first.str);
147 break;
148 }
149 if (sensorData == nullptr)
150 {
James Feist08aec6f2019-01-30 16:17:04 -0800151 std::cerr << "failed to find match for " << path.string() << "\n";
James Feist6714a252018-09-10 15:26:18 -0700152 continue;
153 }
James Feist6714a252018-09-10 15:26:18 -0700154
155 if (baseConfiguration == nullptr)
156 {
James Feist08aec6f2019-01-30 16:17:04 -0800157 std::cerr << "error finding base configuration for" << path.string()
James Feist6714a252018-09-10 15:26:18 -0700158 << "\n";
159 continue;
160 }
161
162 auto findSensorName = baseConfiguration->second.find("Name");
163 if (findSensorName == baseConfiguration->second.end())
164 {
165 std::cerr << "could not determine configuration name for "
James Feist08aec6f2019-01-30 16:17:04 -0800166 << path.string() << "\n";
James Feist6714a252018-09-10 15:26:18 -0700167 continue;
168 }
James Feist3eb82622019-02-08 13:10:22 -0800169 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700170
171 // on rescans, only update sensors we were signaled by
172 auto findSensor = sensors.find(sensorName);
173 if (!firstScan && findSensor != sensors.end())
174 {
175 bool found = false;
176 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
177 it++)
178 {
179 if (boost::ends_with(*it, findSensor->second->name))
180 {
181 sensorsChanged->erase(it);
182 findSensor->second = nullptr;
183 found = true;
184 break;
185 }
186 }
187 if (!found)
188 {
189 continue;
190 }
191 }
192 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700193 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700194 {
195 std::cerr << "error populating thresholds for " << sensorName
196 << "\n";
197 }
198
James Feist757c9db2018-09-12 12:27:25 -0700199 auto findScaleFactor = baseConfiguration->second.find("ScaleFactor");
James Feist6714a252018-09-10 15:26:18 -0700200 float scaleFactor = 1.0;
201 if (findScaleFactor != baseConfiguration->second.end())
202 {
James Feist3eb82622019-02-08 13:10:22 -0800203 scaleFactor =
204 std::visit(VariantToFloatVisitor(), findScaleFactor->second);
James Feist6714a252018-09-10 15:26:18 -0700205 }
James Feist71d31b22019-01-02 16:57:54 -0800206
207 auto findPowerOn = baseConfiguration->second.find("PowerState");
208 PowerState readState = PowerState::always;
209 if (findPowerOn != baseConfiguration->second.end())
210 {
James Feist3eb82622019-02-08 13:10:22 -0800211 std::string powerState =
212 std::visit(VariantToStringVisitor(), findPowerOn->second);
James Feistfc94b212019-02-06 16:14:51 -0800213 setReadState(powerState, readState);
James Feist71d31b22019-01-02 16:57:54 -0800214 }
215
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400216 auto findBridgeGpio = baseConfiguration->second.find("BridgeGpio");
217 std::optional<int> gpioNum;
218
219 if (findBridgeGpio != baseConfiguration->second.end())
220 {
221 int gpioPin =
222 std::visit(VariantToIntVisitor(), findBridgeGpio->second);
223 gpioNum = static_cast<std::optional<int>>(gpioPin);
224 }
225
James Feist6714a252018-09-10 15:26:18 -0700226 sensors[sensorName] = std::make_unique<ADCSensor>(
227 path.string(), objectServer, dbusConnection, io, sensorName,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400228 std::move(sensorThresholds), scaleFactor, readState, *interfacePath,
229 gpioNum);
James Feist6714a252018-09-10 15:26:18 -0700230 }
231}
232
233int main(int argc, char** argv)
234{
235 boost::asio::io_service io;
236 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
237 systemBus->request_name("xyz.openbmc_project.ADCSensor");
238 sdbusplus::asio::object_server objectServer(systemBus);
239 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
240 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
241 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
242 std::make_unique<boost::container::flat_set<std::string>>();
243
244 io.post([&]() {
245 createSensors(io, objectServer, sensors, systemBus, nullptr);
246 });
247
248 boost::asio::deadline_timer filterTimer(io);
249 std::function<void(sdbusplus::message::message&)> eventHandler =
250 [&](sdbusplus::message::message& message) {
251 if (message.is_method_error())
252 {
253 std::cerr << "callback method error\n";
254 return;
255 }
256 sensorsChanged->insert(message.get_path());
257 // this implicitly cancels the timer
258 filterTimer.expires_from_now(boost::posix_time::seconds(1));
259
260 filterTimer.async_wait([&](const boost::system::error_code& ec) {
261 if (ec == boost::asio::error::operation_aborted)
262 {
263 /* we were canceled*/
264 return;
265 }
266 else if (ec)
267 {
268 std::cerr << "timer error\n";
269 return;
270 }
271 createSensors(io, objectServer, sensors, systemBus,
272 sensorsChanged);
273 });
274 };
275
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700276 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700277 {
278 auto match = std::make_unique<sdbusplus::bus::match::match>(
279 static_cast<sdbusplus::bus::bus&>(*systemBus),
280 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700281 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700282 eventHandler);
283 matches.emplace_back(std::move(match));
284 }
285
286 io.run();
287}