blob: 2d75d9d28335d2ff3cf62ddb1a3a0344a9d7c07c [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 <Utils.hpp>
18#include <boost/algorithm/string/predicate.hpp>
James Feist24f02f22019-04-15 11:05:39 -070019#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070020#include <fstream>
21#include <regex>
22#include <sdbusplus/asio/connection.hpp>
James Feist82bac4c2019-03-11 11:16:53 -070023#include <sdbusplus/asio/object_server.hpp>
James Feist6714a252018-09-10 15:26:18 -070024#include <sdbusplus/bus/match.hpp>
25
James Feistcf3bce62019-01-08 10:07:19 -080026namespace fs = std::filesystem;
James Feist6714a252018-09-10 15:26:18 -070027
James Feist6ef20402019-01-07 16:45:08 -080028static bool powerStatusOn = false;
James Feistfc94b212019-02-06 16:14:51 -080029static bool biosHasPost = false;
James Feist58295ad2019-05-30 15:01:41 -070030
James Feist6ef20402019-01-07 16:45:08 -080031static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
James Feist52497fd2019-06-07 13:01:33 -070032static std::unique_ptr<sdbusplus::bus::match::match> postMatch = nullptr;
James Feist71d31b22019-01-02 16:57:54 -080033
James Feist6714a252018-09-10 15:26:18 -070034bool getSensorConfiguration(
35 const std::string& type,
36 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
37 ManagedObjectType& resp, bool useCache)
38{
39 static ManagedObjectType managedObj;
40
41 if (!useCache)
42 {
43 managedObj.clear();
44 sdbusplus::message::message getManagedObjects =
45 dbusConnection->new_method_call(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070046 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
James Feist6714a252018-09-10 15:26:18 -070047 "GetManagedObjects");
48 bool err = false;
49 try
50 {
51 sdbusplus::message::message reply =
52 dbusConnection->call(getManagedObjects);
Yoo, Jae Hyun0e022052018-10-15 14:05:37 -070053 reply.read(managedObj);
James Feist6714a252018-09-10 15:26:18 -070054 }
55 catch (const sdbusplus::exception::exception&)
56 {
57 err = true;
58 }
59
60 if (err)
61 {
62 std::cerr << "Error communicating to entity manager\n";
63 return false;
64 }
65 }
66 for (const auto& pathPair : managedObj)
67 {
68 std::vector<boost::container::flat_map<std::string, BasicVariantType>>
69 sensorData;
70 bool correctType = false;
71 for (const auto& entry : pathPair.second)
72 {
73 if (boost::starts_with(entry.first, type))
74 {
75 correctType = true;
76 break;
77 }
78 }
79 if (correctType)
80 {
81 resp.emplace(pathPair);
82 }
83 }
84 return true;
85}
86
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070087bool findFiles(const fs::path dirPath, const std::string& matchString,
88 std::vector<fs::path>& foundPaths, unsigned int symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -070089{
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070090 if (!fs::exists(dirPath))
James Feist6714a252018-09-10 15:26:18 -070091 return false;
92
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070093 std::regex search(matchString);
James Feist6714a252018-09-10 15:26:18 -070094 std::smatch match;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 for (auto& p : fs::recursive_directory_iterator(dirPath))
James Feist6714a252018-09-10 15:26:18 -070096 {
97 std::string path = p.path().string();
98 if (!is_directory(p))
99 {
100 if (std::regex_search(path, match, search))
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 foundPaths.emplace_back(p.path());
James Feist6714a252018-09-10 15:26:18 -0700102 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 else if (is_symlink(p) && symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700104 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700105 findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1);
James Feist6714a252018-09-10 15:26:18 -0700106 }
107 }
108 return true;
109}
110
James Feist71d31b22019-01-02 16:57:54 -0800111bool isPowerOn(void)
James Feist6714a252018-09-10 15:26:18 -0700112{
James Feist71d31b22019-01-02 16:57:54 -0800113 if (!powerMatch)
James Feist6714a252018-09-10 15:26:18 -0700114 {
James Feist71d31b22019-01-02 16:57:54 -0800115 throw std::runtime_error("Power Match Not Created");
James Feist6714a252018-09-10 15:26:18 -0700116 }
James Feist71d31b22019-01-02 16:57:54 -0800117 return powerStatusOn;
118}
119
James Feistfc94b212019-02-06 16:14:51 -0800120bool hasBiosPost(void)
121{
James Feist52497fd2019-06-07 13:01:33 -0700122 if (!postMatch)
James Feistfc94b212019-02-06 16:14:51 -0800123 {
James Feist52497fd2019-06-07 13:01:33 -0700124 throw std::runtime_error("Post Match Not Created");
James Feistfc94b212019-02-06 16:14:51 -0800125 }
126 return biosHasPost;
127}
128
James Feist71d31b22019-01-02 16:57:54 -0800129void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
130{
James Feist43d32fe2019-09-04 10:35:20 -0700131 static boost::asio::steady_timer timer(conn->get_io_context());
James Feist6714a252018-09-10 15:26:18 -0700132 // create a match for powergood changes, first time do a method call to
James Feist71d31b22019-01-02 16:57:54 -0800133 // cache the correct value
James Feist52497fd2019-06-07 13:01:33 -0700134 if (powerMatch)
135 {
136 return;
137 }
James Feist6714a252018-09-10 15:26:18 -0700138
139 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
140 static_cast<sdbusplus::bus::bus&>(*conn),
James Feist52497fd2019-06-07 13:01:33 -0700141 "type='signal',interface='" + std::string(properties::interface) +
142 "',path='" + std::string(power::path) + "',arg0='" +
143 std::string(power::interface) + "'",
144 [](sdbusplus::message::message& message) {
145 std::string objectName;
146 boost::container::flat_map<std::string, std::variant<std::string>>
147 values;
148 message.read(objectName, values);
149 auto findState = values.find(power::property);
150 if (findState != values.end())
James Feist6714a252018-09-10 15:26:18 -0700151 {
James Feist43d32fe2019-09-04 10:35:20 -0700152 bool on = boost::ends_with(
James Feist52497fd2019-06-07 13:01:33 -0700153 std::get<std::string>(findState->second), "Running");
James Feist43d32fe2019-09-04 10:35:20 -0700154 if (!on)
155 {
156 timer.cancel();
157 powerStatusOn = false;
158 return;
159 }
160 // on comes too quickly
161 timer.expires_after(std::chrono::seconds(10));
162 timer.async_wait([](boost::system::error_code ec) {
163 if (ec == boost::asio::error::operation_aborted)
164 {
165 return;
166 }
167 else if (ec)
168 {
169 std::cerr << "Timer error " << ec.message() << "\n";
170 return;
171 }
172 powerStatusOn = true;
173 });
James Feist6714a252018-09-10 15:26:18 -0700174 }
James Feist52497fd2019-06-07 13:01:33 -0700175 });
176
177 postMatch = std::make_unique<sdbusplus::bus::match::match>(
178 static_cast<sdbusplus::bus::bus&>(*conn),
179 "type='signal',interface='" + std::string(properties::interface) +
180 "',path='" + std::string(post::path) + "',arg0='" +
181 std::string(post::interface) + "'",
182 [](sdbusplus::message::message& message) {
183 std::string objectName;
184 boost::container::flat_map<std::string, std::variant<std::string>>
185 values;
186 message.read(objectName, values);
187 auto findState = values.find(post::property);
188 if (findState != values.end())
189 {
190 biosHasPost =
191 std::get<std::string>(findState->second) != "Inactive";
192 }
193 });
James Feistfc94b212019-02-06 16:14:51 -0800194
195 conn->async_method_call(
196 [](boost::system::error_code ec,
James Feist52497fd2019-06-07 13:01:33 -0700197 const std::variant<std::string>& state) {
James Feistfc94b212019-02-06 16:14:51 -0800198 if (ec)
199 {
James Feistcee4ef22019-04-04 11:04:08 -0700200 // we commonly come up before power control, we'll capture the
201 // property change later
James Feistfc94b212019-02-06 16:14:51 -0800202 return;
203 }
James Feist52497fd2019-06-07 13:01:33 -0700204 powerStatusOn =
205 boost::ends_with(std::get<std::string>(state), "Running");
James Feistfc94b212019-02-06 16:14:51 -0800206 },
James Feist52497fd2019-06-07 13:01:33 -0700207 power::busname, power::path, properties::interface, properties::get,
208 power::interface, power::property);
209
210 conn->async_method_call(
211 [](boost::system::error_code ec,
212 const std::variant<std::string>& state) {
213 if (ec)
214 {
215 // we commonly come up before power control, we'll capture the
216 // property change later
217 return;
218 }
219 biosHasPost = std::get<std::string>(state) != "Inactive";
220 },
221 post::busname, post::path, properties::interface, properties::get,
222 post::interface, post::property);
James Feist3f0e8762018-11-27 11:30:42 -0800223}
James Feist87d713a2018-12-06 16:06:24 -0800224
225// replaces limits if MinReading and MaxReading are found.
226void findLimits(std::pair<double, double>& limits,
227 const SensorBaseConfiguration* data)
228{
229 if (!data)
230 {
231 return;
232 }
233 auto maxFind = data->second.find("MaxReading");
234 auto minFind = data->second.find("MinReading");
235
236 if (minFind != data->second.end())
237 {
James Feist3eb82622019-02-08 13:10:22 -0800238 limits.first = std::visit(VariantToDoubleVisitor(), minFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800239 }
240 if (maxFind != data->second.end())
241 {
James Feist3eb82622019-02-08 13:10:22 -0800242 limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800243 }
James Feistfc94b212019-02-06 16:14:51 -0800244}
James Feist82bac4c2019-03-11 11:16:53 -0700245
246void createAssociation(
247 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
248 const std::string& path)
249{
250 if (association)
251 {
James Feistd2543f82019-04-09 11:10:06 -0700252 std::filesystem::path p(path);
253
James Feist82bac4c2019-03-11 11:16:53 -0700254 std::vector<Association> associations;
James Feistd2543f82019-04-09 11:10:06 -0700255 associations.push_back(
James Feistd8bd5622019-06-26 12:09:05 -0700256 Association("chassis", "all_sensors", p.parent_path().string()));
James Feist2adc95c2019-09-30 14:55:28 -0700257 association->register_property("Associations", associations);
James Feist82bac4c2019-03-11 11:16:53 -0700258 association->initialize();
259 }
James Feist43d32fe2019-09-04 10:35:20 -0700260}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800261
262void setInventoryAssociation(
263 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
264 const std::string& path, const std::string& chassisPath)
265{
266 if (association)
267 {
268 std::filesystem::path p(path);
269
270 std::vector<Association> associations;
271 associations.push_back(
272 Association("inventory", "sensors", p.parent_path().string()));
273 associations.push_back(
274 Association("chassis", "all_sensors", chassisPath));
James Feist2adc95c2019-09-30 14:55:28 -0700275 association->register_property("Associations", associations);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800276 association->initialize();
277 }
278}
279
280void createInventoryAssoc(
281 std::shared_ptr<sdbusplus::asio::connection> conn,
282 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
283 const std::string& path)
284{
285 if (!association)
286 {
287 return;
288 }
289 conn->async_method_call(
290 [association, path](const boost::system::error_code ec,
291 const std::vector<std::string>& ret) {
292 if (ec)
293 {
294 std::cerr << "Error calling mapper\n";
295 return;
296 }
297 for (const auto& object : ret)
298 {
299 setInventoryAssociation(association, path, object);
300 }
301 },
302 mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths",
303 "/xyz/openbmc_project/inventory/system", 2,
304 std::array<std::string, 1>{
305 "xyz.openbmc_project.Inventory.Item.System"});
306}