blob: f11a52092fa5365ced008f6d72ecbf2d5338f304 [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
Patrick Ventureca44b2f2019-10-31 11:02:26 -070017#include "Utils.hpp"
18
James Feist6714a252018-09-10 15:26:18 -070019#include <boost/algorithm/string/predicate.hpp>
Patrick Venture96e97db2019-10-31 13:44:38 -070020#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -070021#include <sdbusplus/asio/connection.hpp>
22#include <sdbusplus/asio/object_server.hpp>
23#include <sdbusplus/bus/match.hpp>
24
James Feist24f02f22019-04-15 11:05:39 -070025#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -070026#include <fstream>
Patrick Venture96e97db2019-10-31 13:44:38 -070027#include <memory>
James Feist6714a252018-09-10 15:26:18 -070028#include <regex>
Patrick Venture96e97db2019-10-31 13:44:38 -070029#include <stdexcept>
30#include <string>
31#include <utility>
32#include <variant>
33#include <vector>
James Feist6714a252018-09-10 15:26:18 -070034
James Feistcf3bce62019-01-08 10:07:19 -080035namespace fs = std::filesystem;
James Feist6714a252018-09-10 15:26:18 -070036
James Feist6ef20402019-01-07 16:45:08 -080037static bool powerStatusOn = false;
James Feistfc94b212019-02-06 16:14:51 -080038static bool biosHasPost = false;
James Feist58295ad2019-05-30 15:01:41 -070039
James Feist6ef20402019-01-07 16:45:08 -080040static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
James Feist52497fd2019-06-07 13:01:33 -070041static std::unique_ptr<sdbusplus::bus::match::match> postMatch = nullptr;
James Feist71d31b22019-01-02 16:57:54 -080042
James Feist6714a252018-09-10 15:26:18 -070043bool getSensorConfiguration(
44 const std::string& type,
45 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
46 ManagedObjectType& resp, bool useCache)
47{
48 static ManagedObjectType managedObj;
49
50 if (!useCache)
51 {
52 managedObj.clear();
53 sdbusplus::message::message getManagedObjects =
54 dbusConnection->new_method_call(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070055 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
James Feist6714a252018-09-10 15:26:18 -070056 "GetManagedObjects");
57 bool err = false;
58 try
59 {
60 sdbusplus::message::message reply =
61 dbusConnection->call(getManagedObjects);
Yoo, Jae Hyun0e022052018-10-15 14:05:37 -070062 reply.read(managedObj);
James Feist6714a252018-09-10 15:26:18 -070063 }
Jason Ling5747fab2019-10-02 16:46:23 -070064 catch (const sdbusplus::exception::exception& e)
James Feist6714a252018-09-10 15:26:18 -070065 {
Jason Ling5747fab2019-10-02 16:46:23 -070066 std::cerr << "While calling GetManagedObjects on service:"
67 << entityManagerName << " exception name:" << e.name()
68 << "and description:" << e.description()
69 << " was thrown\n";
James Feist6714a252018-09-10 15:26:18 -070070 err = true;
71 }
72
73 if (err)
74 {
75 std::cerr << "Error communicating to entity manager\n";
76 return false;
77 }
78 }
79 for (const auto& pathPair : managedObj)
80 {
James Feist6714a252018-09-10 15:26:18 -070081 bool correctType = false;
82 for (const auto& entry : pathPair.second)
83 {
84 if (boost::starts_with(entry.first, type))
85 {
86 correctType = true;
87 break;
88 }
89 }
90 if (correctType)
91 {
92 resp.emplace(pathPair);
93 }
94 }
95 return true;
96}
97
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070098bool findFiles(const fs::path dirPath, const std::string& matchString,
99 std::vector<fs::path>& foundPaths, unsigned int symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700100{
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700101 if (!fs::exists(dirPath))
James Feist6714a252018-09-10 15:26:18 -0700102 return false;
103
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700104 std::regex search(matchString);
James Feist6714a252018-09-10 15:26:18 -0700105 std::smatch match;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700106 for (auto& p : fs::recursive_directory_iterator(dirPath))
James Feist6714a252018-09-10 15:26:18 -0700107 {
108 std::string path = p.path().string();
109 if (!is_directory(p))
110 {
111 if (std::regex_search(path, match, search))
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700112 foundPaths.emplace_back(p.path());
James Feist6714a252018-09-10 15:26:18 -0700113 }
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700114 else if (is_symlink(p) && symlinkDepth)
James Feist6714a252018-09-10 15:26:18 -0700115 {
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700116 findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1);
James Feist6714a252018-09-10 15:26:18 -0700117 }
118 }
119 return true;
120}
121
James Feist71d31b22019-01-02 16:57:54 -0800122bool isPowerOn(void)
James Feist6714a252018-09-10 15:26:18 -0700123{
James Feist71d31b22019-01-02 16:57:54 -0800124 if (!powerMatch)
James Feist6714a252018-09-10 15:26:18 -0700125 {
James Feist71d31b22019-01-02 16:57:54 -0800126 throw std::runtime_error("Power Match Not Created");
James Feist6714a252018-09-10 15:26:18 -0700127 }
James Feist71d31b22019-01-02 16:57:54 -0800128 return powerStatusOn;
129}
130
James Feistfc94b212019-02-06 16:14:51 -0800131bool hasBiosPost(void)
132{
James Feist52497fd2019-06-07 13:01:33 -0700133 if (!postMatch)
James Feistfc94b212019-02-06 16:14:51 -0800134 {
James Feist52497fd2019-06-07 13:01:33 -0700135 throw std::runtime_error("Post Match Not Created");
James Feistfc94b212019-02-06 16:14:51 -0800136 }
137 return biosHasPost;
138}
139
James Feist71d31b22019-01-02 16:57:54 -0800140void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
141{
James Feist43d32fe2019-09-04 10:35:20 -0700142 static boost::asio::steady_timer timer(conn->get_io_context());
James Feist6714a252018-09-10 15:26:18 -0700143 // create a match for powergood changes, first time do a method call to
James Feist71d31b22019-01-02 16:57:54 -0800144 // cache the correct value
James Feist52497fd2019-06-07 13:01:33 -0700145 if (powerMatch)
146 {
147 return;
148 }
James Feist6714a252018-09-10 15:26:18 -0700149
150 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
151 static_cast<sdbusplus::bus::bus&>(*conn),
James Feist52497fd2019-06-07 13:01:33 -0700152 "type='signal',interface='" + std::string(properties::interface) +
153 "',path='" + std::string(power::path) + "',arg0='" +
154 std::string(power::interface) + "'",
155 [](sdbusplus::message::message& message) {
156 std::string objectName;
157 boost::container::flat_map<std::string, std::variant<std::string>>
158 values;
159 message.read(objectName, values);
160 auto findState = values.find(power::property);
161 if (findState != values.end())
James Feist6714a252018-09-10 15:26:18 -0700162 {
James Feist43d32fe2019-09-04 10:35:20 -0700163 bool on = boost::ends_with(
James Feist52497fd2019-06-07 13:01:33 -0700164 std::get<std::string>(findState->second), "Running");
James Feist43d32fe2019-09-04 10:35:20 -0700165 if (!on)
166 {
167 timer.cancel();
168 powerStatusOn = false;
169 return;
170 }
171 // on comes too quickly
172 timer.expires_after(std::chrono::seconds(10));
173 timer.async_wait([](boost::system::error_code ec) {
174 if (ec == boost::asio::error::operation_aborted)
175 {
176 return;
177 }
178 else if (ec)
179 {
180 std::cerr << "Timer error " << ec.message() << "\n";
181 return;
182 }
183 powerStatusOn = true;
184 });
James Feist6714a252018-09-10 15:26:18 -0700185 }
James Feist52497fd2019-06-07 13:01:33 -0700186 });
187
188 postMatch = std::make_unique<sdbusplus::bus::match::match>(
189 static_cast<sdbusplus::bus::bus&>(*conn),
190 "type='signal',interface='" + std::string(properties::interface) +
191 "',path='" + std::string(post::path) + "',arg0='" +
192 std::string(post::interface) + "'",
193 [](sdbusplus::message::message& message) {
194 std::string objectName;
195 boost::container::flat_map<std::string, std::variant<std::string>>
196 values;
197 message.read(objectName, values);
198 auto findState = values.find(post::property);
199 if (findState != values.end())
200 {
201 biosHasPost =
202 std::get<std::string>(findState->second) != "Inactive";
203 }
204 });
James Feistfc94b212019-02-06 16:14:51 -0800205
206 conn->async_method_call(
207 [](boost::system::error_code ec,
James Feist52497fd2019-06-07 13:01:33 -0700208 const std::variant<std::string>& state) {
James Feistfc94b212019-02-06 16:14:51 -0800209 if (ec)
210 {
James Feistcee4ef22019-04-04 11:04:08 -0700211 // we commonly come up before power control, we'll capture the
212 // property change later
James Feistfc94b212019-02-06 16:14:51 -0800213 return;
214 }
James Feist52497fd2019-06-07 13:01:33 -0700215 powerStatusOn =
216 boost::ends_with(std::get<std::string>(state), "Running");
James Feistfc94b212019-02-06 16:14:51 -0800217 },
James Feist52497fd2019-06-07 13:01:33 -0700218 power::busname, power::path, properties::interface, properties::get,
219 power::interface, power::property);
220
221 conn->async_method_call(
222 [](boost::system::error_code ec,
223 const std::variant<std::string>& state) {
224 if (ec)
225 {
226 // we commonly come up before power control, we'll capture the
227 // property change later
228 return;
229 }
230 biosHasPost = std::get<std::string>(state) != "Inactive";
231 },
232 post::busname, post::path, properties::interface, properties::get,
233 post::interface, post::property);
James Feist3f0e8762018-11-27 11:30:42 -0800234}
James Feist87d713a2018-12-06 16:06:24 -0800235
236// replaces limits if MinReading and MaxReading are found.
237void findLimits(std::pair<double, double>& limits,
238 const SensorBaseConfiguration* data)
239{
240 if (!data)
241 {
242 return;
243 }
244 auto maxFind = data->second.find("MaxReading");
245 auto minFind = data->second.find("MinReading");
246
247 if (minFind != data->second.end())
248 {
James Feist3eb82622019-02-08 13:10:22 -0800249 limits.first = std::visit(VariantToDoubleVisitor(), minFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800250 }
251 if (maxFind != data->second.end())
252 {
James Feist3eb82622019-02-08 13:10:22 -0800253 limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800254 }
James Feistfc94b212019-02-06 16:14:51 -0800255}
James Feist82bac4c2019-03-11 11:16:53 -0700256
257void createAssociation(
258 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
259 const std::string& path)
260{
261 if (association)
262 {
James Feistd2543f82019-04-09 11:10:06 -0700263 std::filesystem::path p(path);
264
James Feist82bac4c2019-03-11 11:16:53 -0700265 std::vector<Association> associations;
James Feistd2543f82019-04-09 11:10:06 -0700266 associations.push_back(
James Feistd8bd5622019-06-26 12:09:05 -0700267 Association("chassis", "all_sensors", p.parent_path().string()));
James Feist2adc95c2019-09-30 14:55:28 -0700268 association->register_property("Associations", associations);
James Feist82bac4c2019-03-11 11:16:53 -0700269 association->initialize();
270 }
James Feist43d32fe2019-09-04 10:35:20 -0700271}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800272
273void setInventoryAssociation(
274 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
AppaRao Pulic82213c2020-02-27 01:24:58 +0530275 const std::string& path,
276 const std::vector<std::string>& chassisPaths = std::vector<std::string>())
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800277{
278 if (association)
279 {
280 std::filesystem::path p(path);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800281 std::vector<Association> associations;
AppaRao Pulic82213c2020-02-27 01:24:58 +0530282 std::string objPath(p.parent_path().string());
283
284 associations.push_back(Association("inventory", "sensors", objPath));
285 associations.push_back(Association("chassis", "all_sensors", objPath));
286
287 for (const std::string& chassisPath : chassisPaths)
288 {
289 associations.push_back(
290 Association("chassis", "all_sensors", chassisPath));
291 }
292
James Feist2adc95c2019-09-30 14:55:28 -0700293 association->register_property("Associations", associations);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800294 association->initialize();
295 }
296}
297
298void createInventoryAssoc(
299 std::shared_ptr<sdbusplus::asio::connection> conn,
300 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
301 const std::string& path)
302{
303 if (!association)
304 {
305 return;
306 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530307
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800308 conn->async_method_call(
309 [association, path](const boost::system::error_code ec,
AppaRao Pulic82213c2020-02-27 01:24:58 +0530310 const std::vector<std::string>& invSysObjPaths) {
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800311 if (ec)
312 {
AppaRao Pulic82213c2020-02-27 01:24:58 +0530313 // In case of error, set the default associations and
314 // initialize the association Interface.
315 setInventoryAssociation(association, path);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800316 return;
317 }
AppaRao Pulic82213c2020-02-27 01:24:58 +0530318 setInventoryAssociation(association, path, invSysObjPaths);
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800319 },
320 mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths",
321 "/xyz/openbmc_project/inventory/system", 2,
322 std::array<std::string, 1>{
323 "xyz.openbmc_project.Inventory.Item.System"});
324}
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200325
326std::optional<double> readFile(const std::string& thresholdFile,
327 const double& scaleFactor)
328{
329 std::string line;
330 std::ifstream labelFile(thresholdFile);
331 if (labelFile.good())
332 {
333 std::getline(labelFile, line);
334 labelFile.close();
335
336 try
337 {
338 return std::stod(line) / scaleFactor;
339 }
340 catch (const std::invalid_argument&)
341 {
342 return std::nullopt;
343 }
344 }
345 return std::nullopt;
346}
347
348std::optional<std::tuple<std::string, std::string, std::string>>
349 splitFileName(const std::filesystem::path& filePath)
350{
351 if (filePath.has_filename())
352 {
353 const auto fileName = filePath.filename().string();
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200354
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200355 size_t numberPos = std::strcspn(fileName.c_str(), "1234567890");
356 size_t itemPos = std::strcspn(fileName.c_str(), "_");
357
358 if (numberPos > 0 && itemPos > numberPos && fileName.size() > itemPos)
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200359 {
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200360 return std::make_optional(
361 std::make_tuple(fileName.substr(0, numberPos),
362 fileName.substr(numberPos, itemPos - numberPos),
363 fileName.substr(itemPos + 1, fileName.size())));
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200364 }
365 }
366 return std::nullopt;
Zbigniew Kurzynskidd648002020-07-10 16:44:16 +0200367}