James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 17 | #include "filesystem.hpp" |
| 18 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | #include <Utils.hpp> |
| 20 | #include <boost/algorithm/string/predicate.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 21 | #include <fstream> |
| 22 | #include <regex> |
| 23 | #include <sdbusplus/asio/connection.hpp> |
| 24 | #include <sdbusplus/bus/match.hpp> |
| 25 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 26 | namespace fs = std::filesystem; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 27 | const static constexpr char* powerInterfaceName = |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | "xyz.openbmc_project.Chassis.Control.Power"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 29 | const static constexpr char* powerObjectName = |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 30 | "/xyz/openbmc_project/Chassis/Control/Power0"; |
| 31 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 32 | static bool powerStatusOn = false; |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 33 | static bool biosHasPost = false; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 34 | static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr; |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 35 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 36 | bool 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 Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 48 | entityManagerName, "/", "org.freedesktop.DBus.ObjectManager", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 49 | "GetManagedObjects"); |
| 50 | bool err = false; |
| 51 | try |
| 52 | { |
| 53 | sdbusplus::message::message reply = |
| 54 | dbusConnection->call(getManagedObjects); |
Yoo, Jae Hyun | 0e02205 | 2018-10-15 14:05:37 -0700 | [diff] [blame] | 55 | reply.read(managedObj); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | } |
| 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 Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 89 | bool findFiles(const fs::path dirPath, const std::string& matchString, |
| 90 | std::vector<fs::path>& foundPaths, unsigned int symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 91 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 92 | if (!fs::exists(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 93 | return false; |
| 94 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 95 | std::regex search(matchString); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 96 | std::smatch match; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 97 | for (auto& p : fs::recursive_directory_iterator(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 98 | { |
| 99 | std::string path = p.path().string(); |
| 100 | if (!is_directory(p)) |
| 101 | { |
| 102 | if (std::regex_search(path, match, search)) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 103 | foundPaths.emplace_back(p.path()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 104 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 105 | else if (is_symlink(p) && symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 106 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 107 | findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | return true; |
| 111 | } |
| 112 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 113 | bool isPowerOn(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 114 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 115 | if (!powerMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 116 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 117 | throw std::runtime_error("Power Match Not Created"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 118 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 119 | return powerStatusOn; |
| 120 | } |
| 121 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 122 | bool hasBiosPost(void) |
| 123 | { |
| 124 | if (!powerMatch) |
| 125 | { |
| 126 | throw std::runtime_error("Power Match Not Created"); |
| 127 | } |
| 128 | return biosHasPost; |
| 129 | } |
| 130 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 131 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 132 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 133 | // create a match for powergood changes, first time do a method call to |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 134 | // cache the correct value |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 135 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
James Feist | 3f0e876 | 2018-11-27 11:30:42 -0800 | [diff] [blame] | 136 | [](sdbusplus::message::message& message) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 137 | std::string objectName; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 138 | boost::container::flat_map<std::string, std::variant<int32_t, bool>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 139 | values; |
| 140 | message.read(objectName, values); |
| 141 | auto findPgood = values.find("pgood"); |
| 142 | if (findPgood != values.end()) |
| 143 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 144 | powerStatusOn = std::get<int32_t>(findPgood->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 145 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 146 | auto findPostComplete = values.find("post_complete"); |
| 147 | if (findPostComplete != values.end()) |
| 148 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 149 | biosHasPost = std::get<bool>(findPostComplete->second); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 150 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 151 | }; |
| 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 Feist | e86810b | 2019-02-05 13:13:06 -0800 | [diff] [blame] | 157 | "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 158 | eventHandler); |
| 159 | |
| 160 | conn->async_method_call( |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 161 | [](boost::system::error_code ec, const std::variant<int32_t>& pgood) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 162 | if (ec) |
| 163 | { |
| 164 | std::cerr << "Error getting initial power status\n"; |
| 165 | return; |
| 166 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 167 | powerStatusOn = std::get<int32_t>(pgood); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 169 | powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties", |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 170 | "Get", powerInterfaceName, "pgood"); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 171 | |
| 172 | conn->async_method_call( |
| 173 | [](boost::system::error_code ec, |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 174 | const std::variant<int32_t>& postComplete) { |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 175 | if (ec) |
| 176 | { |
| 177 | std::cerr << "Error getting initial post status\n"; |
| 178 | return; |
| 179 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 180 | biosHasPost = std::get<int32_t>(postComplete); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 181 | }, |
| 182 | powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties", |
| 183 | "Get", powerInterfaceName, "post_complete"); |
James Feist | 3f0e876 | 2018-11-27 11:30:42 -0800 | [diff] [blame] | 184 | } |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 185 | |
| 186 | // replaces limits if MinReading and MaxReading are found. |
| 187 | void 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 Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 199 | limits.first = std::visit(VariantToDoubleVisitor(), minFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 200 | } |
| 201 | if (maxFind != data->second.end()) |
| 202 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 203 | limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 204 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 205 | } |