blob: 8cfba87433b25360528199eeb97d8a5fea2980fa [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 <ADCSensor.hpp>
18#include <Utils.hpp>
19#include <VariantVisitors.hpp>
20#include <boost/algorithm/string/predicate.hpp>
21#include <boost/algorithm/string/replace.hpp>
22#include <boost/container/flat_set.hpp>
James Feist24f02f22019-04-15 11:05:39 -070023#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070024#include <fstream>
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -040025#include <optional>
James Feist6714a252018-09-10 15:26:18 -070026#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;
James Feist3eb82622019-02-08 13:10:22 -080033
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
James Feist3eb82622019-02-08 13:10:22 -0800136 unsigned int number =
137 std::visit(VariantToUnsignedIntVisitor(), findIndex->second);
James Feist08aec6f2019-01-30 16:17:04 -0800138
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 }
James Feist3eb82622019-02-08 13:10:22 -0800168 std::string sensorName = std::get<std::string>(findSensorName->second);
James Feist6714a252018-09-10 15:26:18 -0700169
170 // on rescans, only update sensors we were signaled by
171 auto findSensor = sensors.find(sensorName);
172 if (!firstScan && findSensor != sensors.end())
173 {
174 bool found = false;
175 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
176 it++)
177 {
178 if (boost::ends_with(*it, findSensor->second->name))
179 {
180 sensorsChanged->erase(it);
181 findSensor->second = nullptr;
182 found = true;
183 break;
184 }
185 }
186 if (!found)
187 {
188 continue;
189 }
190 }
191 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700192 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700193 {
194 std::cerr << "error populating thresholds for " << sensorName
195 << "\n";
196 }
197
James Feist757c9db2018-09-12 12:27:25 -0700198 auto findScaleFactor = baseConfiguration->second.find("ScaleFactor");
James Feist6714a252018-09-10 15:26:18 -0700199 float scaleFactor = 1.0;
200 if (findScaleFactor != baseConfiguration->second.end())
201 {
James Feist3eb82622019-02-08 13:10:22 -0800202 scaleFactor =
203 std::visit(VariantToFloatVisitor(), findScaleFactor->second);
James Feist6714a252018-09-10 15:26:18 -0700204 }
James Feist71d31b22019-01-02 16:57:54 -0800205
206 auto findPowerOn = baseConfiguration->second.find("PowerState");
207 PowerState readState = PowerState::always;
208 if (findPowerOn != baseConfiguration->second.end())
209 {
James Feist3eb82622019-02-08 13:10:22 -0800210 std::string powerState =
211 std::visit(VariantToStringVisitor(), findPowerOn->second);
James Feistfc94b212019-02-06 16:14:51 -0800212 setReadState(powerState, readState);
James Feist71d31b22019-01-02 16:57:54 -0800213 }
214
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400215 auto findBridgeGpio = baseConfiguration->second.find("BridgeGpio");
216 std::optional<int> gpioNum;
217
218 if (findBridgeGpio != baseConfiguration->second.end())
219 {
220 int gpioPin =
221 std::visit(VariantToIntVisitor(), findBridgeGpio->second);
222 gpioNum = static_cast<std::optional<int>>(gpioPin);
223 }
224
James Feist6714a252018-09-10 15:26:18 -0700225 sensors[sensorName] = std::make_unique<ADCSensor>(
226 path.string(), objectServer, dbusConnection, io, sensorName,
Zhu, Yungea5b1bbc2019-04-09 19:49:36 -0400227 std::move(sensorThresholds), scaleFactor, readState, *interfacePath,
228 gpioNum);
James Feist6714a252018-09-10 15:26:18 -0700229 }
230}
231
232int main(int argc, char** argv)
233{
234 boost::asio::io_service io;
235 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
236 systemBus->request_name("xyz.openbmc_project.ADCSensor");
237 sdbusplus::asio::object_server objectServer(systemBus);
238 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
239 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
240 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
241 std::make_unique<boost::container::flat_set<std::string>>();
242
243 io.post([&]() {
244 createSensors(io, objectServer, sensors, systemBus, nullptr);
245 });
246
247 boost::asio::deadline_timer filterTimer(io);
248 std::function<void(sdbusplus::message::message&)> eventHandler =
249 [&](sdbusplus::message::message& message) {
250 if (message.is_method_error())
251 {
252 std::cerr << "callback method error\n";
253 return;
254 }
255 sensorsChanged->insert(message.get_path());
256 // this implicitly cancels the timer
257 filterTimer.expires_from_now(boost::posix_time::seconds(1));
258
259 filterTimer.async_wait([&](const boost::system::error_code& ec) {
260 if (ec == boost::asio::error::operation_aborted)
261 {
262 /* we were canceled*/
263 return;
264 }
265 else if (ec)
266 {
267 std::cerr << "timer error\n";
268 return;
269 }
270 createSensors(io, objectServer, sensors, systemBus,
271 sensorsChanged);
272 });
273 };
274
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700275 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700276 {
277 auto match = std::make_unique<sdbusplus::bus::match::match>(
278 static_cast<sdbusplus::bus::bus&>(*systemBus),
279 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700280 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700281 eventHandler);
282 matches.emplace_back(std::move(match));
283 }
284
285 io.run();
286}