blob: 845b976671629642f9bc31821dd60a6e04d4ff21 [file] [log] [blame]
James Feist139cb572018-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>
23#include <experimental/filesystem>
24#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
31namespace fs = std::experimental::filesystem;
32static constexpr std::array<const char*, 1> SENSOR_TYPES = {
33 "xyz.openbmc_project.Configuration.ADC"};
34static std::regex INPUT_REGEX(R"(in(\d+)_input)");
35
36void createSensors(
37 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
38 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
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;
48 for (const char* type : SENSOR_TYPES)
49 {
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;
59 if (!find_files(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
60 {
61 std::cerr << "No temperature sensors in system\n";
62 return;
63 }
64
65 // iterate through all found adc sensors, and try to match them with
66 // configuration
67 for (auto& path : paths)
68 {
69 std::smatch match;
70 std::string pathStr = path.string();
71
72 std::regex_search(pathStr, match, INPUT_REGEX);
73 std::string indexStr = *(match.begin() + 1);
74
75 auto directory = path.parent_path();
76 // convert to 0 based
77 size_t index = std::stoul(indexStr) - 1;
78 auto oem_name_path =
79 directory.string() + R"(/of_node/oemname)" + std::to_string(index);
80
81 if (DEBUG)
82 std::cout << "Checking path " << oem_name_path << "\n";
83 std::ifstream nameFile(oem_name_path);
84 if (!nameFile.good())
85 {
86 std::cerr << "Failure reading " << oem_name_path << "\n";
87 continue;
88 }
89 std::string oemName;
90 std::getline(nameFile, oemName);
91 nameFile.close();
92 if (!oemName.size())
93 {
94 // shouldn't have an empty name file
95 continue;
96 }
97 oemName.pop_back(); // remove trailing null
98
99 const SensorData* sensorData = nullptr;
100 const std::string* interfacePath = nullptr;
101 for (const std::pair<sdbusplus::message::object_path, SensorData>&
102 sensor : sensorConfigurations)
103 {
104 if (!boost::ends_with(sensor.first.str, oemName))
105 {
106 continue;
107 }
108 sensorData = &(sensor.second);
109 interfacePath = &(sensor.first.str);
110 break;
111 }
112 if (sensorData == nullptr)
113 {
114 std::cerr << "failed to find match for " << oemName << "\n";
115 continue;
116 }
117 const std::pair<std::string, boost::container::flat_map<
118 std::string, BasicVariantType>>*
119 baseConfiguration = nullptr;
120 for (const char* type : SENSOR_TYPES)
121 {
122 auto sensorBase = sensorData->find(type);
123 if (sensorBase != sensorData->end())
124 {
125 baseConfiguration = &(*sensorBase);
126 break;
127 }
128 }
129
130 if (baseConfiguration == nullptr)
131 {
132 std::cerr << "error finding base configuration for" << oemName
133 << "\n";
134 continue;
135 }
136
137 auto findSensorName = baseConfiguration->second.find("Name");
138 if (findSensorName == baseConfiguration->second.end())
139 {
140 std::cerr << "could not determine configuration name for "
141 << oemName << "\n";
142 continue;
143 }
144 std::string sensorName =
145 sdbusplus::message::variant_ns::get<std::string>(
146 findSensorName->second);
147
148 // on rescans, only update sensors we were signaled by
149 auto findSensor = sensors.find(sensorName);
150 if (!firstScan && findSensor != sensors.end())
151 {
152 bool found = false;
153 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
154 it++)
155 {
156 if (boost::ends_with(*it, findSensor->second->name))
157 {
158 sensorsChanged->erase(it);
159 findSensor->second = nullptr;
160 found = true;
161 break;
162 }
163 }
164 if (!found)
165 {
166 continue;
167 }
168 }
169 std::vector<thresholds::Threshold> sensorThresholds;
170 if (!ParseThresholdsFromConfig(*sensorData, sensorThresholds))
171 {
172 std::cerr << "error populating thresholds for " << sensorName
173 << "\n";
174 }
175
James Feistb9d9b332018-09-12 12:27:25 -0700176 auto findScaleFactor = baseConfiguration->second.find("ScaleFactor");
James Feist139cb572018-09-10 15:26:18 -0700177 float scaleFactor = 1.0;
178 if (findScaleFactor != baseConfiguration->second.end())
179 {
180 scaleFactor = mapbox::util::apply_visitor(VariantToFloatVisitor(),
181 findScaleFactor->second);
182 }
183 sensors[sensorName] = std::make_unique<ADCSensor>(
184 path.string(), objectServer, dbusConnection, io, sensorName,
185 std::move(sensorThresholds), scaleFactor, *interfacePath);
186 }
187}
188
189int main(int argc, char** argv)
190{
191 boost::asio::io_service io;
192 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
193 systemBus->request_name("xyz.openbmc_project.ADCSensor");
194 sdbusplus::asio::object_server objectServer(systemBus);
195 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
196 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
197 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
198 std::make_unique<boost::container::flat_set<std::string>>();
199
200 io.post([&]() {
201 createSensors(io, objectServer, sensors, systemBus, nullptr);
202 });
203
204 boost::asio::deadline_timer filterTimer(io);
205 std::function<void(sdbusplus::message::message&)> eventHandler =
206 [&](sdbusplus::message::message& message) {
207 if (message.is_method_error())
208 {
209 std::cerr << "callback method error\n";
210 return;
211 }
212 sensorsChanged->insert(message.get_path());
213 // this implicitly cancels the timer
214 filterTimer.expires_from_now(boost::posix_time::seconds(1));
215
216 filterTimer.async_wait([&](const boost::system::error_code& ec) {
217 if (ec == boost::asio::error::operation_aborted)
218 {
219 /* we were canceled*/
220 return;
221 }
222 else if (ec)
223 {
224 std::cerr << "timer error\n";
225 return;
226 }
227 createSensors(io, objectServer, sensors, systemBus,
228 sensorsChanged);
229 });
230 };
231
232 for (const char* type : SENSOR_TYPES)
233 {
234 auto match = std::make_unique<sdbusplus::bus::match::match>(
235 static_cast<sdbusplus::bus::bus&>(*systemBus),
236 "type='signal',member='PropertiesChanged',path_namespace='" +
237 std::string(INVENTORY_PATH) + "',arg0namespace='" + type + "'",
238 eventHandler);
239 matches.emplace_back(std::move(match));
240 }
241
242 io.run();
243}