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 | |
Patrick Venture | ca44b2f | 2019-10-31 11:02:26 -0700 | [diff] [blame] | 17 | #include "Utils.hpp" |
| 18 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | #include <boost/algorithm/string/predicate.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 20 | #include <boost/container/flat_map.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame^] | 21 | #include <sdbusplus/asio/connection.hpp> |
| 22 | #include <sdbusplus/asio/object_server.hpp> |
| 23 | #include <sdbusplus/bus/match.hpp> |
| 24 | |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 25 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 26 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 27 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | #include <regex> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 29 | #include <stdexcept> |
| 30 | #include <string> |
| 31 | #include <utility> |
| 32 | #include <variant> |
| 33 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 34 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 35 | namespace fs = std::filesystem; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 36 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 37 | static bool powerStatusOn = false; |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 38 | static bool biosHasPost = false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 39 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 40 | static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr; |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 41 | static std::unique_ptr<sdbusplus::bus::match::match> postMatch = nullptr; |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 42 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 43 | bool 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 Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 55 | entityManagerName, "/", "org.freedesktop.DBus.ObjectManager", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 56 | "GetManagedObjects"); |
| 57 | bool err = false; |
| 58 | try |
| 59 | { |
| 60 | sdbusplus::message::message reply = |
| 61 | dbusConnection->call(getManagedObjects); |
Yoo, Jae Hyun | 0e02205 | 2018-10-15 14:05:37 -0700 | [diff] [blame] | 62 | reply.read(managedObj); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 63 | } |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 64 | catch (const sdbusplus::exception::exception& e) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 65 | { |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 66 | std::cerr << "While calling GetManagedObjects on service:" |
| 67 | << entityManagerName << " exception name:" << e.name() |
| 68 | << "and description:" << e.description() |
| 69 | << " was thrown\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 70 | 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 Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 81 | 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 Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 98 | bool findFiles(const fs::path dirPath, const std::string& matchString, |
| 99 | std::vector<fs::path>& foundPaths, unsigned int symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 100 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 101 | if (!fs::exists(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 102 | return false; |
| 103 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 104 | std::regex search(matchString); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 105 | std::smatch match; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 106 | for (auto& p : fs::recursive_directory_iterator(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 107 | { |
| 108 | std::string path = p.path().string(); |
| 109 | if (!is_directory(p)) |
| 110 | { |
| 111 | if (std::regex_search(path, match, search)) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 112 | foundPaths.emplace_back(p.path()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 113 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 114 | else if (is_symlink(p) && symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 115 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 116 | findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 117 | } |
| 118 | } |
| 119 | return true; |
| 120 | } |
| 121 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 122 | bool isPowerOn(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 123 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 124 | if (!powerMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 125 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 126 | throw std::runtime_error("Power Match Not Created"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 128 | return powerStatusOn; |
| 129 | } |
| 130 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 131 | bool hasBiosPost(void) |
| 132 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 133 | if (!postMatch) |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 134 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 135 | throw std::runtime_error("Post Match Not Created"); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 136 | } |
| 137 | return biosHasPost; |
| 138 | } |
| 139 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 140 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 141 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 142 | static boost::asio::steady_timer timer(conn->get_io_context()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 143 | // 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] | 144 | // cache the correct value |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 145 | if (powerMatch) |
| 146 | { |
| 147 | return; |
| 148 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 149 | |
| 150 | powerMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 151 | static_cast<sdbusplus::bus::bus&>(*conn), |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 152 | "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 Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 162 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 163 | bool on = boost::ends_with( |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 164 | std::get<std::string>(findState->second), "Running"); |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 165 | 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 Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 185 | } |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 186 | }); |
| 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 Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 205 | |
| 206 | conn->async_method_call( |
| 207 | [](boost::system::error_code ec, |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 208 | const std::variant<std::string>& state) { |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 209 | if (ec) |
| 210 | { |
James Feist | cee4ef2 | 2019-04-04 11:04:08 -0700 | [diff] [blame] | 211 | // we commonly come up before power control, we'll capture the |
| 212 | // property change later |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 213 | return; |
| 214 | } |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 215 | powerStatusOn = |
| 216 | boost::ends_with(std::get<std::string>(state), "Running"); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 217 | }, |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 218 | 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 Feist | 3f0e876 | 2018-11-27 11:30:42 -0800 | [diff] [blame] | 234 | } |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 235 | |
| 236 | // replaces limits if MinReading and MaxReading are found. |
| 237 | void 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 Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 249 | limits.first = std::visit(VariantToDoubleVisitor(), minFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 250 | } |
| 251 | if (maxFind != data->second.end()) |
| 252 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 253 | limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 254 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 255 | } |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 256 | |
| 257 | void createAssociation( |
| 258 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
| 259 | const std::string& path) |
| 260 | { |
| 261 | if (association) |
| 262 | { |
James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 263 | std::filesystem::path p(path); |
| 264 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 265 | std::vector<Association> associations; |
James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 266 | associations.push_back( |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 267 | Association("chassis", "all_sensors", p.parent_path().string())); |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 268 | association->register_property("Associations", associations); |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 269 | association->initialize(); |
| 270 | } |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 271 | } |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 272 | |
| 273 | void setInventoryAssociation( |
| 274 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 275 | const std::string& path, |
| 276 | const std::vector<std::string>& chassisPaths = std::vector<std::string>()) |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 277 | { |
| 278 | if (association) |
| 279 | { |
| 280 | std::filesystem::path p(path); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 281 | std::vector<Association> associations; |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 282 | 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 Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 293 | association->register_property("Associations", associations); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 294 | association->initialize(); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | void 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 Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 307 | |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 308 | conn->async_method_call( |
| 309 | [association, path](const boost::system::error_code ec, |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 310 | const std::vector<std::string>& invSysObjPaths) { |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 311 | if (ec) |
| 312 | { |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 313 | // In case of error, set the default associations and |
| 314 | // initialize the association Interface. |
| 315 | setInventoryAssociation(association, path); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 316 | return; |
| 317 | } |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 318 | setInventoryAssociation(association, path, invSysObjPaths); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 319 | }, |
| 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 | } |