blob: ff064280cfb56281dec244c109c88674bf7ff629 [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 <Utils.hpp>
20#include <boost/algorithm/string/predicate.hpp>
James Feist6714a252018-09-10 15:26:18 -070021#include <fstream>
22#include <regex>
23#include <sdbusplus/asio/connection.hpp>
24#include <sdbusplus/bus/match.hpp>
25
James Feistcf3bce62019-01-08 10:07:19 -080026namespace fs = std::filesystem;
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070027const static constexpr char* powerInterfaceName =
James Feist6714a252018-09-10 15:26:18 -070028 "xyz.openbmc_project.Chassis.Control.Power";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070029const static constexpr char* powerObjectName =
James Feist6714a252018-09-10 15:26:18 -070030 "/xyz/openbmc_project/Chassis/Control/Power0";
31
James Feist6ef20402019-01-07 16:45:08 -080032static bool powerStatusOn = false;
James Feistfc94b212019-02-06 16:14:51 -080033static bool biosHasPost = false;
James Feist6ef20402019-01-07 16:45:08 -080034static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr;
James Feist71d31b22019-01-02 16:57:54 -080035
James Feist6714a252018-09-10 15:26:18 -070036bool getSensorConfiguration(
37 const std::string& type,
38 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
39 ManagedObjectType& resp, bool useCache)
40{
41 static ManagedObjectType managedObj;
42
43 if (!useCache)
44 {
45 managedObj.clear();
46 sdbusplus::message::message getManagedObjects =
47 dbusConnection->new_method_call(
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070048 entityManagerName, "/", "org.freedesktop.DBus.ObjectManager",
James Feist6714a252018-09-10 15:26:18 -070049 "GetManagedObjects");
50 bool err = false;
51 try
52 {
53 sdbusplus::message::message reply =
54 dbusConnection->call(getManagedObjects);
Yoo, Jae Hyun0e022052018-10-15 14:05:37 -070055 reply.read(managedObj);
James Feist6714a252018-09-10 15:26:18 -070056 }
57 catch (const sdbusplus::exception::exception&)
58 {
59 err = true;
60 }
61
62 if (err)
63 {
64 std::cerr << "Error communicating to entity manager\n";
65 return false;
66 }
67 }
68 for (const auto& pathPair : managedObj)
69 {
70 std::vector<boost::container::flat_map<std::string, BasicVariantType>>
71 sensorData;
72 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{
124 if (!powerMatch)
125 {
126 throw std::runtime_error("Power Match Not Created");
127 }
128 return biosHasPost;
129}
130
James Feist71d31b22019-01-02 16:57:54 -0800131void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn)
132{
James Feist6714a252018-09-10 15:26:18 -0700133 // create a match for powergood changes, first time do a method call to
James Feist71d31b22019-01-02 16:57:54 -0800134 // cache the correct value
James Feist6714a252018-09-10 15:26:18 -0700135 std::function<void(sdbusplus::message::message & message)> eventHandler =
James Feist3f0e8762018-11-27 11:30:42 -0800136 [](sdbusplus::message::message& message) {
James Feist6714a252018-09-10 15:26:18 -0700137 std::string objectName;
James Feist3eb82622019-02-08 13:10:22 -0800138 boost::container::flat_map<std::string, std::variant<int32_t, bool>>
James Feist6714a252018-09-10 15:26:18 -0700139 values;
140 message.read(objectName, values);
141 auto findPgood = values.find("pgood");
142 if (findPgood != values.end())
143 {
James Feist3eb82622019-02-08 13:10:22 -0800144 powerStatusOn = std::get<int32_t>(findPgood->second);
James Feist6714a252018-09-10 15:26:18 -0700145 }
James Feistfc94b212019-02-06 16:14:51 -0800146 auto findPostComplete = values.find("post_complete");
147 if (findPostComplete != values.end())
148 {
James Feist3eb82622019-02-08 13:10:22 -0800149 biosHasPost = std::get<bool>(findPostComplete->second);
James Feistfc94b212019-02-06 16:14:51 -0800150 }
James Feist6714a252018-09-10 15:26:18 -0700151 };
152
153 powerMatch = std::make_unique<sdbusplus::bus::match::match>(
154 static_cast<sdbusplus::bus::bus&>(*conn),
155 "type='signal',interface='org.freedesktop.DBus.Properties',path_"
156 "namespace='/xyz/openbmc_project/Chassis/Control/"
James Feiste86810b2019-02-05 13:13:06 -0800157 "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'",
James Feist6714a252018-09-10 15:26:18 -0700158 eventHandler);
159
160 conn->async_method_call(
James Feist3eb82622019-02-08 13:10:22 -0800161 [](boost::system::error_code ec, const std::variant<int32_t>& pgood) {
James Feist6714a252018-09-10 15:26:18 -0700162 if (ec)
163 {
164 std::cerr << "Error getting initial power status\n";
165 return;
166 }
James Feist3eb82622019-02-08 13:10:22 -0800167 powerStatusOn = std::get<int32_t>(pgood);
James Feist6714a252018-09-10 15:26:18 -0700168 },
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -0700169 powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties",
James Feistdc6c55f2018-10-31 12:53:20 -0700170 "Get", powerInterfaceName, "pgood");
James Feistfc94b212019-02-06 16:14:51 -0800171
172 conn->async_method_call(
173 [](boost::system::error_code ec,
James Feist3eb82622019-02-08 13:10:22 -0800174 const std::variant<int32_t>& postComplete) {
James Feistfc94b212019-02-06 16:14:51 -0800175 if (ec)
176 {
177 std::cerr << "Error getting initial post status\n";
178 return;
179 }
James Feist3eb82622019-02-08 13:10:22 -0800180 biosHasPost = std::get<int32_t>(postComplete);
James Feistfc94b212019-02-06 16:14:51 -0800181 },
182 powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties",
183 "Get", powerInterfaceName, "post_complete");
James Feist3f0e8762018-11-27 11:30:42 -0800184}
James Feist87d713a2018-12-06 16:06:24 -0800185
186// replaces limits if MinReading and MaxReading are found.
187void findLimits(std::pair<double, double>& limits,
188 const SensorBaseConfiguration* data)
189{
190 if (!data)
191 {
192 return;
193 }
194 auto maxFind = data->second.find("MaxReading");
195 auto minFind = data->second.find("MinReading");
196
197 if (minFind != data->second.end())
198 {
James Feist3eb82622019-02-08 13:10:22 -0800199 limits.first = std::visit(VariantToDoubleVisitor(), minFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800200 }
201 if (maxFind != data->second.end())
202 {
James Feist3eb82622019-02-08 13:10:22 -0800203 limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second);
James Feist87d713a2018-12-06 16:06:24 -0800204 }
James Feistfc94b212019-02-06 16:14:51 -0800205}