blob: f7a2ebeeaa3f8748deb0994c7231799b9fab4980 [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 }
Jason Ling5747fab2019-10-02 16:46:23 -070055 catch (const sdbusplus::exception::exception& e)
James Feist6714a252018-09-10 15:26:18 -070056 {
Jason Ling5747fab2019-10-02 16:46:23 -070057 std::cerr << "While calling GetManagedObjects on service:"
58 << entityManagerName << " exception name:" << e.name()
59 << "and description:" << e.description()
60 << " was thrown\n";
James Feist6714a252018-09-10 15:26:18 -070061 err = true;
62 }
63
64 if (err)
65 {
66 std::cerr << "Error communicating to entity manager\n";
67 return false;
68 }
69 }
70 for (const auto& pathPair : managedObj)
71 {
James Feist6714a252018-09-10 15:26:18 -070072 bool correctType = false;
73 for (const auto& entry : pathPair.second)
74 {
75 if (boost::starts_with(entry.first, type))
76 {
77 correctType = true;
78 break;
79 }
80 }
81 if (correctType)
82 {
83 resp.emplace(pathPair);
84 }
85 }
86 return true;
87}
88
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070089bool findFiles(const fs::path dirPath, const std::string& matchString,
90 std::vector<fs::path>& foundPaths, unsigned int symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -070091{
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070092 if (!fs::exists(dirPath))
James Feist6714a252018-09-10 15:26:18 -070093 return false;
94
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070095 std::regex search(matchString);
James Feist6714a252018-09-10 15:26:18 -070096 std::smatch match;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070097 for (auto& p : fs::recursive_directory_iterator(dirPath))
James Feist6714a252018-09-10 15:26:18 -070098 {
99 std::string path = p.path().string();
100 if (!is_directory(p))
101 {
102 if (std::regex_search(path, match, search))
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700103 foundPaths.emplace_back(p.path());
James Feist6714a252018-09-10 15:26:18 -0700104 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700105 else if (is_symlink(p) && symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700106 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700107 findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1);
James Feist6714a252018-09-10 15:26:18 -0700108 }
109 }
110 return true;
111}
112
James Feist71d31b22019-01-02 16:57:54 -0800113bool isPowerOn(void)
James Feist6714a252018-09-10 15:26:18 -0700114{
James Feist71d31b22019-01-02 16:57:54 -0800115 if (!powerMatch)
James Feist6714a252018-09-10 15:26:18 -0700116 {
James Feist71d31b22019-01-02 16:57:54 -0800117 throw std::runtime_error("Power Match Not Created");
James Feist6714a252018-09-10 15:26:18 -0700118 }
James Feist71d31b22019-01-02 16:57:54 -0800119 return powerStatusOn;
120}
121
James Feistfc94b212019-02-06 16:14:51 -0800122bool hasBiosPost(void)
123{
James Feist52497fd2019-06-07 13:01:33 -0700124 if (!postMatch)
James Feistfc94b212019-02-06 16:14:51 -0800125 {
James Feist52497fd2019-06-07 13:01:33 -0700126 throw std::runtime_error("Post Match Not Created");
James Feistfc94b212019-02-06 16:14:51 -0800127 }
128 return biosHasPost;
129}
130
James Feist71d31b22019-01-02 16:57:54 -0800131void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
132{
James Feist43d32fe2019-09-04 10:35:20 -0700133 static boost::asio::steady_timer timer(conn->get_io_context());
James Feist6714a252018-09-10 15:26:18 -0700134 // create a match for powergood changes, first time do a method call to
James Feist71d31b22019-01-02 16:57:54 -0800135 // cache the correct value
James Feist52497fd2019-06-07 13:01:33 -0700136 if (powerMatch)
137 {
138 return;
139 }
James Feist6714a252018-09-10 15:26:18 -0700140
141 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
142 static_cast<sdbusplus::bus::bus&>(*conn),
James Feist52497fd2019-06-07 13:01:33 -0700143 "type='signal',interface='" + std::string(properties::interface) +
144 "',path='" + std::string(power::path) + "',arg0='" +
145 std::string(power::interface) + "'",
146 [](sdbusplus::message::message& message) {
147 std::string objectName;
148 boost::container::flat_map<std::string, std::variant<std::string>>
149 values;
150 message.read(objectName, values);
151 auto findState = values.find(power::property);
152 if (findState != values.end())
James Feist6714a252018-09-10 15:26:18 -0700153 {
James Feist43d32fe2019-09-04 10:35:20 -0700154 bool on = boost::ends_with(
James Feist52497fd2019-06-07 13:01:33 -0700155 std::get<std::string>(findState->second), "Running");
James Feist43d32fe2019-09-04 10:35:20 -0700156 if (!on)
157 {
158 timer.cancel();
159 powerStatusOn = false;
160 return;
161 }
162 // on comes too quickly
163 timer.expires_after(std::chrono::seconds(10));
164 timer.async_wait([](boost::system::error_code ec) {
165 if (ec == boost::asio::error::operation_aborted)
166 {
167 return;
168 }
169 else if (ec)
170 {
171 std::cerr << "Timer error " << ec.message() << "\n";
172 return;
173 }
174 powerStatusOn = true;
175 });
James Feist6714a252018-09-10 15:26:18 -0700176 }
James Feist52497fd2019-06-07 13:01:33 -0700177 });
178
179 postMatch = std::make_unique<sdbusplus::bus::match::match>(
180 static_cast<sdbusplus::bus::bus&>(*conn),
181 "type='signal',interface='" + std::string(properties::interface) +
182 "',path='" + std::string(post::path) + "',arg0='" +
183 std::string(post::interface) + "'",
184 [](sdbusplus::message::message& message) {
185 std::string objectName;
186 boost::container::flat_map<std::string, std::variant<std::string>>
187 values;
188 message.read(objectName, values);
189 auto findState = values.find(post::property);
190 if (findState != values.end())
191 {
192 biosHasPost =
193 std::get<std::string>(findState->second) != "Inactive";
194 }
195 });
James Feistfc94b212019-02-06 16:14:51 -0800196
197 conn->async_method_call(
198 [](boost::system::error_code ec,
James Feist52497fd2019-06-07 13:01:33 -0700199 const std::variant<std::string>& state) {
James Feistfc94b212019-02-06 16:14:51 -0800200 if (ec)
201 {
James Feistcee4ef22019-04-04 11:04:08 -0700202 // we commonly come up before power control, we'll capture the
203 // property change later
James Feistfc94b212019-02-06 16:14:51 -0800204 return;
205 }
James Feist52497fd2019-06-07 13:01:33 -0700206 powerStatusOn =
207 boost::ends_with(std::get<std::string>(state), "Running");
James Feistfc94b212019-02-06 16:14:51 -0800208 },
James Feist52497fd2019-06-07 13:01:33 -0700209 power::busname, power::path, properties::interface, properties::get,
210 power::interface, power::property);
211
212 conn->async_method_call(
213 [](boost::system::error_code ec,
214 const std::variant<std::string>& state) {
215 if (ec)
216 {
217 // we commonly come up before power control, we'll capture the
218 // property change later
219 return;
220 }
221 biosHasPost = std::get<std::string>(state) != "Inactive";
222 },
223 post::busname, post::path, properties::interface, properties::get,
224 post::interface, post::property);
James Feist3f0e8762018-11-27 11:30:42 -0800225}
James Feist87d713a2018-12-06 16:06:24 -0800226
227// replaces limits if MinReading and MaxReading are found.
228void findLimits(std::pair<double, double>& limits,
229 const SensorBaseConfiguration* data)
230{
231 if (!data)
232 {
233 return;
234 }
235 auto maxFind = data->second.find("MaxReading");
236 auto minFind = data->second.find("MinReading");
237
238 if (minFind != data->second.end())
239 {
James Feist3eb82622019-02-08 13:10:22 -0800240 limits.first = std::visit(VariantToDoubleVisitor(), minFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800241 }
242 if (maxFind != data->second.end())
243 {
James Feist3eb82622019-02-08 13:10:22 -0800244 limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800245 }
James Feistfc94b212019-02-06 16:14:51 -0800246}
James Feist82bac4c2019-03-11 11:16:53 -0700247
248void createAssociation(
249 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
250 const std::string& path)
251{
252 if (association)
253 {
James Feistd2543f82019-04-09 11:10:06 -0700254 std::filesystem::path p(path);
255
James Feist82bac4c2019-03-11 11:16:53 -0700256 std::vector<Association> associations;
James Feistd2543f82019-04-09 11:10:06 -0700257 associations.push_back(
James Feistd8bd5622019-06-26 12:09:05 -0700258 Association("chassis", "all_sensors", p.parent_path().string()));
James Feist2adc95c2019-09-30 14:55:28 -0700259 association->register_property("Associations", associations);
James Feist82bac4c2019-03-11 11:16:53 -0700260 association->initialize();
261 }
James Feist43d32fe2019-09-04 10:35:20 -0700262}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800263
264void setInventoryAssociation(
265 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
266 const std::string& path, const std::string& chassisPath)
267{
268 if (association)
269 {
270 std::filesystem::path p(path);
271
272 std::vector<Association> associations;
273 associations.push_back(
274 Association("inventory", "sensors", p.parent_path().string()));
275 associations.push_back(
276 Association("chassis", "all_sensors", chassisPath));
James Feist2adc95c2019-09-30 14:55:28 -0700277 association->register_property("Associations", associations);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800278 association->initialize();
279 }
280}
281
282void createInventoryAssoc(
283 std::shared_ptr<sdbusplus::asio::connection> conn,
284 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
285 const std::string& path)
286{
287 if (!association)
288 {
289 return;
290 }
291 conn->async_method_call(
292 [association, path](const boost::system::error_code ec,
293 const std::vector<std::string>& ret) {
294 if (ec)
295 {
296 std::cerr << "Error calling mapper\n";
297 return;
298 }
299 for (const auto& object : ret)
300 {
301 setInventoryAssociation(association, path, object);
302 }
303 },
304 mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths",
305 "/xyz/openbmc_project/inventory/system", 2,
306 std::array<std::string, 1>{
307 "xyz.openbmc_project.Inventory.Item.System"});
308}