blob: 9474caa1bcff1ca96547a8d22f26c4bc47f0aebe [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>
26#include <regex>
27#include <sdbusplus/asio/connection.hpp>
28#include <sdbusplus/asio/object_server.hpp>
29
30static constexpr bool DEBUG = false;
31
James Feistcf3bce62019-01-08 10:07:19 -080032namespace fs = std::filesystem;
Yoo, Jae Hyun50938052018-10-17 18:19:02 -070033namespace variant_ns = sdbusplus::message::variant_ns;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070034static constexpr std::array<const char*, 1> sensorTypes = {
James Feist6714a252018-09-10 15:26:18 -070035 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070036static std::regex inputRegex(R"(in(\d+)_input)");
James Feist6714a252018-09-10 15:26:18 -070037
James Feist08aec6f2019-01-30 16:17:04 -080038// filter out adc from any other voltage sensor
39bool isAdc(const fs::path& parentPath)
40{
41 fs::path namePath = parentPath / "name";
42
43 std::ifstream nameFile(namePath);
44 if (!nameFile.good())
45 {
46 std::cerr << "Failure reading " << namePath.string() << "\n";
47 return false;
48 }
49
50 std::string name;
51 std::getline(nameFile, name);
52
53 return name == "iio_hwmon";
54}
55
James Feist6714a252018-09-10 15:26:18 -070056void createSensors(
57 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
58 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
59 sensors,
60 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
61 const std::unique_ptr<boost::container::flat_set<std::string>>&
62 sensorsChanged)
63{
64 bool firstScan = sensorsChanged == nullptr;
65 // use new data the first time, then refresh
66 ManagedObjectType sensorConfigurations;
67 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070068 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070069 {
70 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
71 useCache))
72 {
73 std::cerr << "error communicating to entity manager\n";
74 return;
75 }
76 useCache = true;
77 }
78 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070079 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070080 {
81 std::cerr << "No temperature sensors in system\n";
82 return;
83 }
84
85 // iterate through all found adc sensors, and try to match them with
86 // configuration
87 for (auto& path : paths)
88 {
James Feist08aec6f2019-01-30 16:17:04 -080089 if (!isAdc(path.parent_path()))
90 {
91 continue;
92 }
James Feist6714a252018-09-10 15:26:18 -070093 std::smatch match;
94 std::string pathStr = path.string();
95
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070096 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -070097 std::string indexStr = *(match.begin() + 1);
98
99 auto directory = path.parent_path();
100 // convert to 0 based
101 size_t index = std::stoul(indexStr) - 1;
James Feist6714a252018-09-10 15:26:18 -0700102
103 const SensorData* sensorData = nullptr;
104 const std::string* interfacePath = nullptr;
James Feist08aec6f2019-01-30 16:17:04 -0800105 const std::pair<std::string, boost::container::flat_map<
106 std::string, BasicVariantType>>*
107 baseConfiguration;
James Feist6714a252018-09-10 15:26:18 -0700108 for (const std::pair<sdbusplus::message::object_path, SensorData>&
109 sensor : sensorConfigurations)
110 {
James Feist08aec6f2019-01-30 16:17:04 -0800111 // clear it out each loop
112 baseConfiguration = nullptr;
113
114 // find base configuration
115 for (const char* type : sensorTypes)
116 {
117 auto sensorBase = sensor.second.find(type);
118 if (sensorBase != sensor.second.end())
119 {
120 baseConfiguration = &(*sensorBase);
121 break;
122 }
123 }
124 if (baseConfiguration == nullptr)
James Feist6714a252018-09-10 15:26:18 -0700125 {
126 continue;
127 }
James Feist08aec6f2019-01-30 16:17:04 -0800128 auto findIndex = baseConfiguration->second.find("Index");
129 if (findIndex == baseConfiguration->second.end())
130 {
131 std::cerr << "Base configuration missing Index"
132 << baseConfiguration->first << "\n";
133 continue;
134 }
135
136 unsigned int number = sdbusplus::message::variant_ns::visit(
137 VariantToUnsignedIntVisitor(), findIndex->second);
138
139 if (number != index)
140 {
141 continue;
142 }
143
James Feist6714a252018-09-10 15:26:18 -0700144 sensorData = &(sensor.second);
145 interfacePath = &(sensor.first.str);
146 break;
147 }
148 if (sensorData == nullptr)
149 {
James Feist08aec6f2019-01-30 16:17:04 -0800150 std::cerr << "failed to find match for " << path.string() << "\n";
James Feist6714a252018-09-10 15:26:18 -0700151 continue;
152 }
James Feist6714a252018-09-10 15:26:18 -0700153
154 if (baseConfiguration == nullptr)
155 {
James Feist08aec6f2019-01-30 16:17:04 -0800156 std::cerr << "error finding base configuration for" << path.string()
James Feist6714a252018-09-10 15:26:18 -0700157 << "\n";
158 continue;
159 }
160
161 auto findSensorName = baseConfiguration->second.find("Name");
162 if (findSensorName == baseConfiguration->second.end())
163 {
164 std::cerr << "could not determine configuration name for "
James Feist08aec6f2019-01-30 16:17:04 -0800165 << path.string() << "\n";
James Feist6714a252018-09-10 15:26:18 -0700166 continue;
167 }
168 std::string sensorName =
169 sdbusplus::message::variant_ns::get<std::string>(
170 findSensorName->second);
171
172 // on rescans, only update sensors we were signaled by
173 auto findSensor = sensors.find(sensorName);
174 if (!firstScan && findSensor != sensors.end())
175 {
176 bool found = false;
177 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
178 it++)
179 {
180 if (boost::ends_with(*it, findSensor->second->name))
181 {
182 sensorsChanged->erase(it);
183 findSensor->second = nullptr;
184 found = true;
185 break;
186 }
187 }
188 if (!found)
189 {
190 continue;
191 }
192 }
193 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700194 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700195 {
196 std::cerr << "error populating thresholds for " << sensorName
197 << "\n";
198 }
199
James Feist757c9db2018-09-12 12:27:25 -0700200 auto findScaleFactor = baseConfiguration->second.find("ScaleFactor");
James Feist6714a252018-09-10 15:26:18 -0700201 float scaleFactor = 1.0;
202 if (findScaleFactor != baseConfiguration->second.end())
203 {
Yoo, Jae Hyun50938052018-10-17 18:19:02 -0700204 scaleFactor = variant_ns::visit(VariantToFloatVisitor(),
205 findScaleFactor->second);
James Feist6714a252018-09-10 15:26:18 -0700206 }
James Feist71d31b22019-01-02 16:57:54 -0800207
208 auto findPowerOn = baseConfiguration->second.find("PowerState");
209 PowerState readState = PowerState::always;
210 if (findPowerOn != baseConfiguration->second.end())
211 {
212 std::string powerState = variant_ns::visit(VariantToStringVisitor(),
213 findPowerOn->second);
214 if (powerState == "On")
215 {
216 readState = PowerState::on;
217 };
218 }
219
James Feist6714a252018-09-10 15:26:18 -0700220 sensors[sensorName] = std::make_unique<ADCSensor>(
221 path.string(), objectServer, dbusConnection, io, sensorName,
James Feist71d31b22019-01-02 16:57:54 -0800222 std::move(sensorThresholds), scaleFactor, readState,
223 *interfacePath);
James Feist6714a252018-09-10 15:26:18 -0700224 }
225}
226
227int main(int argc, char** argv)
228{
229 boost::asio::io_service io;
230 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
231 systemBus->request_name("xyz.openbmc_project.ADCSensor");
232 sdbusplus::asio::object_server objectServer(systemBus);
233 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
234 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
235 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
236 std::make_unique<boost::container::flat_set<std::string>>();
237
238 io.post([&]() {
239 createSensors(io, objectServer, sensors, systemBus, nullptr);
240 });
241
242 boost::asio::deadline_timer filterTimer(io);
243 std::function<void(sdbusplus::message::message&)> eventHandler =
244 [&](sdbusplus::message::message& message) {
245 if (message.is_method_error())
246 {
247 std::cerr << "callback method error\n";
248 return;
249 }
250 sensorsChanged->insert(message.get_path());
251 // this implicitly cancels the timer
252 filterTimer.expires_from_now(boost::posix_time::seconds(1));
253
254 filterTimer.async_wait([&](const boost::system::error_code& ec) {
255 if (ec == boost::asio::error::operation_aborted)
256 {
257 /* we were canceled*/
258 return;
259 }
260 else if (ec)
261 {
262 std::cerr << "timer error\n";
263 return;
264 }
265 createSensors(io, objectServer, sensors, systemBus,
266 sensorsChanged);
267 });
268 };
269
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700270 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700271 {
272 auto match = std::make_unique<sdbusplus::bus::match::match>(
273 static_cast<sdbusplus::bus::bus&>(*systemBus),
274 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700275 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700276 eventHandler);
277 matches.emplace_back(std::move(match));
278 }
279
280 io.run();
281}