blob: dbbaf75b6fe36e126d668975dba2b1d2c21fc928 [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
38void createSensors(
39 boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer,
40 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>>&
41 sensors,
42 std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
43 const std::unique_ptr<boost::container::flat_set<std::string>>&
44 sensorsChanged)
45{
46 bool firstScan = sensorsChanged == nullptr;
47 // use new data the first time, then refresh
48 ManagedObjectType sensorConfigurations;
49 bool useCache = false;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070050 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -070051 {
52 if (!getSensorConfiguration(type, dbusConnection, sensorConfigurations,
53 useCache))
54 {
55 std::cerr << "error communicating to entity manager\n";
56 return;
57 }
58 useCache = true;
59 }
60 std::vector<fs::path> paths;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070061 if (!findFiles(fs::path("/sys/class/hwmon"), R"(in\d+_input)", paths))
James Feist6714a252018-09-10 15:26:18 -070062 {
63 std::cerr << "No temperature sensors in system\n";
64 return;
65 }
66
67 // iterate through all found adc sensors, and try to match them with
68 // configuration
69 for (auto& path : paths)
70 {
71 std::smatch match;
72 std::string pathStr = path.string();
73
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070074 std::regex_search(pathStr, match, inputRegex);
James Feist6714a252018-09-10 15:26:18 -070075 std::string indexStr = *(match.begin() + 1);
76
77 auto directory = path.parent_path();
78 // convert to 0 based
79 size_t index = std::stoul(indexStr) - 1;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070080 auto oemNamePath =
James Feist6714a252018-09-10 15:26:18 -070081 directory.string() + R"(/of_node/oemname)" + std::to_string(index);
82
83 if (DEBUG)
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070084 {
85 std::cout << "Checking path " << oemNamePath << "\n";
86 }
87 std::ifstream nameFile(oemNamePath);
James Feist6714a252018-09-10 15:26:18 -070088 if (!nameFile.good())
89 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090 std::cerr << "Failure reading " << oemNamePath << "\n";
James Feist6714a252018-09-10 15:26:18 -070091 continue;
92 }
93 std::string oemName;
94 std::getline(nameFile, oemName);
95 nameFile.close();
96 if (!oemName.size())
97 {
98 // shouldn't have an empty name file
99 continue;
100 }
101 oemName.pop_back(); // remove trailing null
102
103 const SensorData* sensorData = nullptr;
104 const std::string* interfacePath = nullptr;
105 for (const std::pair<sdbusplus::message::object_path, SensorData>&
106 sensor : sensorConfigurations)
107 {
108 if (!boost::ends_with(sensor.first.str, oemName))
109 {
110 continue;
111 }
112 sensorData = &(sensor.second);
113 interfacePath = &(sensor.first.str);
114 break;
115 }
116 if (sensorData == nullptr)
117 {
118 std::cerr << "failed to find match for " << oemName << "\n";
119 continue;
120 }
121 const std::pair<std::string, boost::container::flat_map<
122 std::string, BasicVariantType>>*
123 baseConfiguration = nullptr;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700124 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700125 {
126 auto sensorBase = sensorData->find(type);
127 if (sensorBase != sensorData->end())
128 {
129 baseConfiguration = &(*sensorBase);
130 break;
131 }
132 }
133
134 if (baseConfiguration == nullptr)
135 {
136 std::cerr << "error finding base configuration for" << oemName
137 << "\n";
138 continue;
139 }
140
141 auto findSensorName = baseConfiguration->second.find("Name");
142 if (findSensorName == baseConfiguration->second.end())
143 {
144 std::cerr << "could not determine configuration name for "
145 << oemName << "\n";
146 continue;
147 }
148 std::string sensorName =
149 sdbusplus::message::variant_ns::get<std::string>(
150 findSensorName->second);
151
152 // on rescans, only update sensors we were signaled by
153 auto findSensor = sensors.find(sensorName);
154 if (!firstScan && findSensor != sensors.end())
155 {
156 bool found = false;
157 for (auto it = sensorsChanged->begin(); it != sensorsChanged->end();
158 it++)
159 {
160 if (boost::ends_with(*it, findSensor->second->name))
161 {
162 sensorsChanged->erase(it);
163 findSensor->second = nullptr;
164 found = true;
165 break;
166 }
167 }
168 if (!found)
169 {
170 continue;
171 }
172 }
173 std::vector<thresholds::Threshold> sensorThresholds;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700174 if (!parseThresholdsFromConfig(*sensorData, sensorThresholds))
James Feist6714a252018-09-10 15:26:18 -0700175 {
176 std::cerr << "error populating thresholds for " << sensorName
177 << "\n";
178 }
179
James Feist757c9db2018-09-12 12:27:25 -0700180 auto findScaleFactor = baseConfiguration->second.find("ScaleFactor");
James Feist6714a252018-09-10 15:26:18 -0700181 float scaleFactor = 1.0;
182 if (findScaleFactor != baseConfiguration->second.end())
183 {
Yoo, Jae Hyun50938052018-10-17 18:19:02 -0700184 scaleFactor = variant_ns::visit(VariantToFloatVisitor(),
185 findScaleFactor->second);
James Feist6714a252018-09-10 15:26:18 -0700186 }
James Feist71d31b22019-01-02 16:57:54 -0800187
188 auto findPowerOn = baseConfiguration->second.find("PowerState");
189 PowerState readState = PowerState::always;
190 if (findPowerOn != baseConfiguration->second.end())
191 {
192 std::string powerState = variant_ns::visit(VariantToStringVisitor(),
193 findPowerOn->second);
194 if (powerState == "On")
195 {
196 readState = PowerState::on;
197 };
198 }
199
James Feist6714a252018-09-10 15:26:18 -0700200 sensors[sensorName] = std::make_unique<ADCSensor>(
201 path.string(), objectServer, dbusConnection, io, sensorName,
James Feist71d31b22019-01-02 16:57:54 -0800202 std::move(sensorThresholds), scaleFactor, readState,
203 *interfacePath);
James Feist6714a252018-09-10 15:26:18 -0700204 }
205}
206
207int main(int argc, char** argv)
208{
209 boost::asio::io_service io;
210 auto systemBus = std::make_shared<sdbusplus::asio::connection>(io);
211 systemBus->request_name("xyz.openbmc_project.ADCSensor");
212 sdbusplus::asio::object_server objectServer(systemBus);
213 boost::container::flat_map<std::string, std::unique_ptr<ADCSensor>> sensors;
214 std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
215 std::unique_ptr<boost::container::flat_set<std::string>> sensorsChanged =
216 std::make_unique<boost::container::flat_set<std::string>>();
217
218 io.post([&]() {
219 createSensors(io, objectServer, sensors, systemBus, nullptr);
220 });
221
222 boost::asio::deadline_timer filterTimer(io);
223 std::function<void(sdbusplus::message::message&)> eventHandler =
224 [&](sdbusplus::message::message& message) {
225 if (message.is_method_error())
226 {
227 std::cerr << "callback method error\n";
228 return;
229 }
230 sensorsChanged->insert(message.get_path());
231 // this implicitly cancels the timer
232 filterTimer.expires_from_now(boost::posix_time::seconds(1));
233
234 filterTimer.async_wait([&](const boost::system::error_code& ec) {
235 if (ec == boost::asio::error::operation_aborted)
236 {
237 /* we were canceled*/
238 return;
239 }
240 else if (ec)
241 {
242 std::cerr << "timer error\n";
243 return;
244 }
245 createSensors(io, objectServer, sensors, systemBus,
246 sensorsChanged);
247 });
248 };
249
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700250 for (const char* type : sensorTypes)
James Feist6714a252018-09-10 15:26:18 -0700251 {
252 auto match = std::make_unique<sdbusplus::bus::match::match>(
253 static_cast<sdbusplus::bus::bus&>(*systemBus),
254 "type='signal',member='PropertiesChanged',path_namespace='" +
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700255 std::string(inventoryPath) + "',arg0namespace='" + type + "'",
James Feist6714a252018-09-10 15:26:18 -0700256 eventHandler);
257 matches.emplace_back(std::move(match));
258 }
259
260 io.run();
261}