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 | |
| 17 | #include <Utils.hpp> |
| 18 | #include <boost/algorithm/string/predicate.hpp> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 19 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | #include <fstream> |
| 21 | #include <regex> |
| 22 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 23 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 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 | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame^] | 34 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 35 | static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr; |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 36 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | bool getSensorConfiguration( |
| 38 | const std::string& type, |
| 39 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 40 | ManagedObjectType& resp, bool useCache) |
| 41 | { |
| 42 | static ManagedObjectType managedObj; |
| 43 | |
| 44 | if (!useCache) |
| 45 | { |
| 46 | managedObj.clear(); |
| 47 | sdbusplus::message::message getManagedObjects = |
| 48 | dbusConnection->new_method_call( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 49 | entityManagerName, "/", "org.freedesktop.DBus.ObjectManager", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 50 | "GetManagedObjects"); |
| 51 | bool err = false; |
| 52 | try |
| 53 | { |
| 54 | sdbusplus::message::message reply = |
| 55 | dbusConnection->call(getManagedObjects); |
Yoo, Jae Hyun | 0e02205 | 2018-10-15 14:05:37 -0700 | [diff] [blame] | 56 | reply.read(managedObj); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | } |
| 58 | catch (const sdbusplus::exception::exception&) |
| 59 | { |
| 60 | err = true; |
| 61 | } |
| 62 | |
| 63 | if (err) |
| 64 | { |
| 65 | std::cerr << "Error communicating to entity manager\n"; |
| 66 | return false; |
| 67 | } |
| 68 | } |
| 69 | for (const auto& pathPair : managedObj) |
| 70 | { |
| 71 | std::vector<boost::container::flat_map<std::string, BasicVariantType>> |
| 72 | sensorData; |
| 73 | bool correctType = false; |
| 74 | for (const auto& entry : pathPair.second) |
| 75 | { |
| 76 | if (boost::starts_with(entry.first, type)) |
| 77 | { |
| 78 | correctType = true; |
| 79 | break; |
| 80 | } |
| 81 | } |
| 82 | if (correctType) |
| 83 | { |
| 84 | resp.emplace(pathPair); |
| 85 | } |
| 86 | } |
| 87 | return true; |
| 88 | } |
| 89 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 90 | bool findFiles(const fs::path dirPath, const std::string& matchString, |
| 91 | std::vector<fs::path>& foundPaths, unsigned int symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 92 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 93 | if (!fs::exists(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | return false; |
| 95 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 96 | std::regex search(matchString); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 97 | std::smatch match; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 98 | for (auto& p : fs::recursive_directory_iterator(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 99 | { |
| 100 | std::string path = p.path().string(); |
| 101 | if (!is_directory(p)) |
| 102 | { |
| 103 | if (std::regex_search(path, match, search)) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 104 | foundPaths.emplace_back(p.path()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 106 | else if (is_symlink(p) && symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 107 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 108 | findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 109 | } |
| 110 | } |
| 111 | return true; |
| 112 | } |
| 113 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 114 | bool isPowerOn(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 115 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 116 | if (!powerMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 118 | throw std::runtime_error("Power Match Not Created"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 119 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 120 | return powerStatusOn; |
| 121 | } |
| 122 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 123 | bool hasBiosPost(void) |
| 124 | { |
| 125 | if (!powerMatch) |
| 126 | { |
| 127 | throw std::runtime_error("Power Match Not Created"); |
| 128 | } |
| 129 | return biosHasPost; |
| 130 | } |
| 131 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 132 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 133 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 134 | // 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] | 135 | // cache the correct value |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 136 | std::function<void(sdbusplus::message::message & message)> eventHandler = |
James Feist | 3f0e876 | 2018-11-27 11:30:42 -0800 | [diff] [blame] | 137 | [](sdbusplus::message::message& message) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 138 | std::string objectName; |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 139 | boost::container::flat_map<std::string, std::variant<int32_t, bool>> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 140 | values; |
| 141 | message.read(objectName, values); |
| 142 | auto findPgood = values.find("pgood"); |
| 143 | if (findPgood != values.end()) |
| 144 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 145 | powerStatusOn = std::get<int32_t>(findPgood->second); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 146 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 147 | auto findPostComplete = values.find("post_complete"); |
| 148 | if (findPostComplete != values.end()) |
| 149 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 150 | biosHasPost = std::get<bool>(findPostComplete->second); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 151 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 152 | }; |
| 153 | |
| 154 | powerMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 155 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 156 | "type='signal',interface='org.freedesktop.DBus.Properties',path_" |
| 157 | "namespace='/xyz/openbmc_project/Chassis/Control/" |
James Feist | e86810b | 2019-02-05 13:13:06 -0800 | [diff] [blame] | 158 | "Power0',arg0='xyz.openbmc_project.Chassis.Control.Power'", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 159 | eventHandler); |
| 160 | |
| 161 | conn->async_method_call( |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 162 | [](boost::system::error_code ec, const std::variant<int32_t>& pgood) { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 163 | if (ec) |
| 164 | { |
James Feist | c9445d8 | 2019-04-04 08:40:21 -0700 | [diff] [blame] | 165 | // we commonly come up before power control, we'll capture the |
| 166 | // property change later |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 167 | return; |
| 168 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 169 | powerStatusOn = std::get<int32_t>(pgood); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 170 | }, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 171 | powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties", |
James Feist | dc6c55f | 2018-10-31 12:53:20 -0700 | [diff] [blame] | 172 | "Get", powerInterfaceName, "pgood"); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 173 | |
| 174 | conn->async_method_call( |
| 175 | [](boost::system::error_code ec, |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 176 | const std::variant<int32_t>& postComplete) { |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 177 | if (ec) |
| 178 | { |
James Feist | cee4ef2 | 2019-04-04 11:04:08 -0700 | [diff] [blame] | 179 | // we commonly come up before power control, we'll capture the |
| 180 | // property change later |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 181 | return; |
| 182 | } |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 183 | biosHasPost = std::get<int32_t>(postComplete); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 184 | }, |
| 185 | powerInterfaceName, powerObjectName, "org.freedesktop.DBus.Properties", |
| 186 | "Get", powerInterfaceName, "post_complete"); |
James Feist | 3f0e876 | 2018-11-27 11:30:42 -0800 | [diff] [blame] | 187 | } |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 188 | |
| 189 | // replaces limits if MinReading and MaxReading are found. |
| 190 | void findLimits(std::pair<double, double>& limits, |
| 191 | const SensorBaseConfiguration* data) |
| 192 | { |
| 193 | if (!data) |
| 194 | { |
| 195 | return; |
| 196 | } |
| 197 | auto maxFind = data->second.find("MaxReading"); |
| 198 | auto minFind = data->second.find("MinReading"); |
| 199 | |
| 200 | if (minFind != data->second.end()) |
| 201 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 202 | limits.first = std::visit(VariantToDoubleVisitor(), minFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 203 | } |
| 204 | if (maxFind != data->second.end()) |
| 205 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 206 | limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 207 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 208 | } |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 209 | |
| 210 | void createAssociation( |
| 211 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
| 212 | const std::string& path) |
| 213 | { |
| 214 | if (association) |
| 215 | { |
James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 216 | std::filesystem::path p(path); |
| 217 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 218 | using Association = std::tuple<std::string, std::string, std::string>; |
| 219 | std::vector<Association> associations; |
James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 220 | associations.push_back( |
| 221 | Association("inventory", "sensors", p.parent_path().string())); |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 222 | association->register_property("associations", associations); |
| 223 | association->initialize(); |
| 224 | } |
| 225 | } |