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