blob: 4fff81aeb03720b549529a6faa88b894bbdd198c [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;
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -070032namespace variant_ns = sdbusplus::message::variant_ns;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070033static constexpr std::array<const char*, 1> sensorTypes = {
James Feist139cb572018-09-10 15:26:18 -070034 "xyz.openbmc_project.Configuration.ADC"};
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070035static std::regex inputRegex(R"(in(\d+)_input)");
James Feist139cb572018-09-10 15:26:18 -070036
37void createSensors(
38 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
39 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
40 sensors,
41 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
42 const std::unique_ptr<boost::container::flat_set<std::string>>&
43 sensorsChanged)
44{
45 bool firstScan = sensorsChanged == nullptr;
46 // use new data the first time, then refresh
47 ManagedObjectType sensorConfigurations;
48 bool useCache = false;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070049 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -070050 {
51 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
52 useCache))
53 {
54 std::cerr << "error communicating to entity manager\n";
55 return;
56 }
57 useCache = true;
58 }
59 std::vector<fs::path> paths;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070060 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
James Feist139cb572018-09-10 15:26:18 -070061 {
62 std::cerr << "No temperature sensors in system\n";
63 return;
64 }
65
66 // iterate through all found adc sensors, and try to match them with
67 // configuration
68 for (auto& path : paths)
69 {
70 std::smatch match;
71 std::string pathStr = path.string();
72
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070073 std::regex_search(pathStr, match, inputRegex);
James Feist139cb572018-09-10 15:26:18 -070074 std::string indexStr = *(match.begin() + 1);
75
76 auto directory = path.parent_path();
77 // convert to 0 based
78 size_t index = std::stoul(indexStr) - 1;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070079 auto oemNamePath =
James Feist139cb572018-09-10 15:26:18 -070080 directory.string() + R"(/of_node/oemname)" + std::to_string(index);
81
82 if (DEBUG)
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070083 {
84 std::cout << "Checking path " << oemNamePath << "\n";
85 }
86 std::ifstream nameFile(oemNamePath);
James Feist139cb572018-09-10 15:26:18 -070087 if (!nameFile.good())
88 {
Jae Hyun Yoof78ec412018-10-25 10:42:39 -070089 std::cerr << "Failure reading " << oemNamePath << "\n";
James Feist139cb572018-09-10 15:26:18 -070090 continue;
91 }
92 std::string oemName;
93 std::getline(nameFile, oemName);
94 nameFile.close();
95 if (!oemName.size())
96 {
97 // shouldn't have an empty name file
98 continue;
99 }
100 oemName.pop_back(); // remove trailing null
101
102 const SensorData* sensorData = nullptr;
103 const std::string* interfacePath = nullptr;
104 for (const std::pair<sdbusplus::message::object_path, SensorData>&
105 sensor : sensorConfigurations)
106 {
107 if (!boost::ends_with(sensor.first.str, oemName))
108 {
109 continue;
110 }
111 sensorData = &(sensor.second);
112 interfacePath = &(sensor.first.str);
113 break;
114 }
115 if (sensorData == nullptr)
116 {
117 std::cerr << "failed to find match for " << oemName << "\n";
118 continue;
119 }
120 const std::pair<std::string, boost::container::flat_map<
121 std::string, BasicVariantType>>*
122 baseConfiguration = nullptr;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700123 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -0700124 {
125 auto sensorBase = sensorData->find(type);
126 if (sensorBase != sensorData->end())
127 {
128 baseConfiguration = &(*sensorBase);
129 break;
130 }
131 }
132
133 if (baseConfiguration == nullptr)
134 {
135 std::cerr << "error finding base configuration for" << oemName
136 << "\n";
137 continue;
138 }
139
140 auto findSensorName = baseConfiguration->second.find("Name");
141 if (findSensorName == baseConfiguration->second.end())
142 {
143 std::cerr << "could not determine configuration name for "
144 << oemName << "\n";
145 continue;
146 }
147 std::string sensorName =
148 sdbusplus::message::variant_ns::get<std::string>(
149 findSensorName->second);
150
151 // on rescans, only update sensors we were signaled by
152 auto findSensor = sensors.find(sensorName);
153 if (!firstScan && findSensor != sensors.end())
154 {
155 bool found = false;
156 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
157 it++)
158 {
159 if (boost::ends_with(*it, findSensor->second->name))
160 {
161 sensorsChanged->erase(it);
162 findSensor->second = nullptr;
163 found = true;
164 break;
165 }
166 }
167 if (!found)
168 {
169 continue;
170 }
171 }
172 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700173 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist139cb572018-09-10 15:26:18 -0700174 {
175 std::cerr << "error populating thresholds for " << sensorName
176 << "\n";
177 }
178
James Feistb9d9b332018-09-12 12:27:25 -0700179 auto findScaleFactor = baseConfiguration->second.find("ScaleFactor");
James Feist139cb572018-09-10 15:26:18 -0700180 float scaleFactor = 1.0;
181 if (findScaleFactor != baseConfiguration->second.end())
182 {
Yoo, Jae Hyun625429b2018-10-17 18:19:02 -0700183 scaleFactor = variant_ns::visit(VariantToFloatVisitor(),
184 findScaleFactor->second);
James Feist139cb572018-09-10 15:26:18 -0700185 }
186 sensors[sensorName] = std::make_unique<ADCSensor>(
187 path.string(), objectServer, dbusConnection, io, sensorName,
188 std::move(sensorThresholds), scaleFactor, *interfacePath);
189 }
190}
191
192int main(int argc, char** argv)
193{
194 boost::asio::io_service io;
195 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
196 systemBus->request_name("xyz.openbmc_project.ADCSensor");
197 sdbusplus::asio::object_server objectServer(systemBus);
198 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
199 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
200 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
201 std::make_unique<boost::container::flat_set<std::string>>();
202
203 io.post([&]() {
204 createSensors(io, objectServer, sensors, systemBus, nullptr);
205 });
206
207 boost::asio::deadline_timer filterTimer(io);
208 std::function<void(sdbusplus::message::message&)> eventHandler =
209 [&](sdbusplus::message::message& message) {
210 if (message.is_method_error())
211 {
212 std::cerr << "callback method error\n";
213 return;
214 }
215 sensorsChanged->insert(message.get_path());
216 // this implicitly cancels the timer
217 filterTimer.expires_from_now(boost::posix_time::seconds(1));
218
219 filterTimer.async_wait([&](const boost::system::error_code& ec) {
220 if (ec == boost::asio::error::operation_aborted)
221 {
222 /* we were canceled*/
223 return;
224 }
225 else if (ec)
226 {
227 std::cerr << "timer error\n";
228 return;
229 }
230 createSensors(io, objectServer, sensors, systemBus,
231 sensorsChanged);
232 });
233 };
234
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700235 for (const char* type : sensorTypes)
James Feist139cb572018-09-10 15:26:18 -0700236 {
237 auto match = std::make_unique<sdbusplus::bus::match::match>(
238 static_cast<sdbusplus::bus::bus&>(*systemBus),
239 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoof78ec412018-10-25 10:42:39 -0700240 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist139cb572018-09-10 15:26:18 -0700241 eventHandler);
242 matches.emplace_back(std::move(match));
243 }
244
245 io.run();
246}