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 | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 21 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 22 | #include <fstream> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 23 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | #include <regex> |
| 25 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 26 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 27 | #include <sdbusplus/bus/match.hpp> |
Patrick Venture | 96e97db | 2019-10-31 13:44:38 -0700 | [diff] [blame] | 28 | #include <stdexcept> |
| 29 | #include <string> |
| 30 | #include <utility> |
| 31 | #include <variant> |
| 32 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 33 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 34 | namespace fs = std::filesystem; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 35 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 36 | static bool powerStatusOn = false; |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 37 | static bool biosHasPost = false; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 38 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 39 | static std::unique_ptr<sdbusplus::bus::match::match> powerMatch = nullptr; |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 40 | static std::unique_ptr<sdbusplus::bus::match::match> postMatch = nullptr; |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 41 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 42 | bool getSensorConfiguration( |
| 43 | const std::string& type, |
| 44 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 45 | ManagedObjectType& resp, bool useCache) |
| 46 | { |
| 47 | static ManagedObjectType managedObj; |
| 48 | |
| 49 | if (!useCache) |
| 50 | { |
| 51 | managedObj.clear(); |
| 52 | sdbusplus::message::message getManagedObjects = |
| 53 | dbusConnection->new_method_call( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 54 | entityManagerName, "/", "org.freedesktop.DBus.ObjectManager", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 55 | "GetManagedObjects"); |
| 56 | bool err = false; |
| 57 | try |
| 58 | { |
| 59 | sdbusplus::message::message reply = |
| 60 | dbusConnection->call(getManagedObjects); |
Yoo, Jae Hyun | 0e02205 | 2018-10-15 14:05:37 -0700 | [diff] [blame] | 61 | reply.read(managedObj); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 62 | } |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 63 | catch (const sdbusplus::exception::exception& e) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 64 | { |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 65 | std::cerr << "While calling GetManagedObjects on service:" |
| 66 | << entityManagerName << " exception name:" << e.name() |
| 67 | << "and description:" << e.description() |
| 68 | << " was thrown\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 69 | err = true; |
| 70 | } |
| 71 | |
| 72 | if (err) |
| 73 | { |
| 74 | std::cerr << "Error communicating to entity manager\n"; |
| 75 | return false; |
| 76 | } |
| 77 | } |
| 78 | for (const auto& pathPair : managedObj) |
| 79 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 80 | bool correctType = false; |
| 81 | for (const auto& entry : pathPair.second) |
| 82 | { |
| 83 | if (boost::starts_with(entry.first, type)) |
| 84 | { |
| 85 | correctType = true; |
| 86 | break; |
| 87 | } |
| 88 | } |
| 89 | if (correctType) |
| 90 | { |
| 91 | resp.emplace(pathPair); |
| 92 | } |
| 93 | } |
| 94 | return true; |
| 95 | } |
| 96 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 97 | bool findFiles(const fs::path dirPath, const std::string& matchString, |
| 98 | std::vector<fs::path>& foundPaths, unsigned int symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 99 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 100 | if (!fs::exists(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 101 | return false; |
| 102 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 103 | std::regex search(matchString); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 104 | std::smatch match; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 105 | for (auto& p : fs::recursive_directory_iterator(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 106 | { |
| 107 | std::string path = p.path().string(); |
| 108 | if (!is_directory(p)) |
| 109 | { |
| 110 | if (std::regex_search(path, match, search)) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 111 | foundPaths.emplace_back(p.path()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 112 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 113 | else if (is_symlink(p) && symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 114 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 115 | findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | return true; |
| 119 | } |
| 120 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 121 | bool isPowerOn(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 122 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 123 | if (!powerMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 124 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 125 | throw std::runtime_error("Power Match Not Created"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 126 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 127 | return powerStatusOn; |
| 128 | } |
| 129 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 130 | bool hasBiosPost(void) |
| 131 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 132 | if (!postMatch) |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 133 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 134 | throw std::runtime_error("Post Match Not Created"); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 135 | } |
| 136 | return biosHasPost; |
| 137 | } |
| 138 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 139 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 140 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 141 | static boost::asio::steady_timer timer(conn->get_io_context()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 142 | // 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] | 143 | // cache the correct value |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 144 | if (powerMatch) |
| 145 | { |
| 146 | return; |
| 147 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 148 | |
| 149 | powerMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 150 | static_cast<sdbusplus::bus::bus&>(*conn), |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 151 | "type='signal',interface='" + std::string(properties::interface) + |
| 152 | "',path='" + std::string(power::path) + "',arg0='" + |
| 153 | std::string(power::interface) + "'", |
| 154 | [](sdbusplus::message::message& message) { |
| 155 | std::string objectName; |
| 156 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 157 | values; |
| 158 | message.read(objectName, values); |
| 159 | auto findState = values.find(power::property); |
| 160 | if (findState != values.end()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 161 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 162 | bool on = boost::ends_with( |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 163 | std::get<std::string>(findState->second), "Running"); |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 164 | if (!on) |
| 165 | { |
| 166 | timer.cancel(); |
| 167 | powerStatusOn = false; |
| 168 | return; |
| 169 | } |
| 170 | // on comes too quickly |
| 171 | timer.expires_after(std::chrono::seconds(10)); |
| 172 | timer.async_wait([](boost::system::error_code ec) { |
| 173 | if (ec == boost::asio::error::operation_aborted) |
| 174 | { |
| 175 | return; |
| 176 | } |
| 177 | else if (ec) |
| 178 | { |
| 179 | std::cerr << "Timer error " << ec.message() << "\n"; |
| 180 | return; |
| 181 | } |
| 182 | powerStatusOn = true; |
| 183 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 184 | } |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 185 | }); |
| 186 | |
| 187 | postMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 188 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 189 | "type='signal',interface='" + std::string(properties::interface) + |
| 190 | "',path='" + std::string(post::path) + "',arg0='" + |
| 191 | std::string(post::interface) + "'", |
| 192 | [](sdbusplus::message::message& message) { |
| 193 | std::string objectName; |
| 194 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 195 | values; |
| 196 | message.read(objectName, values); |
| 197 | auto findState = values.find(post::property); |
| 198 | if (findState != values.end()) |
| 199 | { |
| 200 | biosHasPost = |
| 201 | std::get<std::string>(findState->second) != "Inactive"; |
| 202 | } |
| 203 | }); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 204 | |
| 205 | conn->async_method_call( |
| 206 | [](boost::system::error_code ec, |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 207 | const std::variant<std::string>& state) { |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 208 | if (ec) |
| 209 | { |
James Feist | cee4ef2 | 2019-04-04 11:04:08 -0700 | [diff] [blame] | 210 | // we commonly come up before power control, we'll capture the |
| 211 | // property change later |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 212 | return; |
| 213 | } |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 214 | powerStatusOn = |
| 215 | boost::ends_with(std::get<std::string>(state), "Running"); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 216 | }, |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 217 | power::busname, power::path, properties::interface, properties::get, |
| 218 | power::interface, power::property); |
| 219 | |
| 220 | conn->async_method_call( |
| 221 | [](boost::system::error_code ec, |
| 222 | const std::variant<std::string>& state) { |
| 223 | if (ec) |
| 224 | { |
| 225 | // we commonly come up before power control, we'll capture the |
| 226 | // property change later |
| 227 | return; |
| 228 | } |
| 229 | biosHasPost = std::get<std::string>(state) != "Inactive"; |
| 230 | }, |
| 231 | post::busname, post::path, properties::interface, properties::get, |
| 232 | post::interface, post::property); |
James Feist | 3f0e876 | 2018-11-27 11:30:42 -0800 | [diff] [blame] | 233 | } |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 234 | |
| 235 | // replaces limits if MinReading and MaxReading are found. |
| 236 | void findLimits(std::pair<double, double>& limits, |
| 237 | const SensorBaseConfiguration* data) |
| 238 | { |
| 239 | if (!data) |
| 240 | { |
| 241 | return; |
| 242 | } |
| 243 | auto maxFind = data->second.find("MaxReading"); |
| 244 | auto minFind = data->second.find("MinReading"); |
| 245 | |
| 246 | if (minFind != data->second.end()) |
| 247 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 248 | limits.first = std::visit(VariantToDoubleVisitor(), minFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 249 | } |
| 250 | if (maxFind != data->second.end()) |
| 251 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 252 | limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 253 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 254 | } |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 255 | |
| 256 | void createAssociation( |
| 257 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
| 258 | const std::string& path) |
| 259 | { |
| 260 | if (association) |
| 261 | { |
James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 262 | std::filesystem::path p(path); |
| 263 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 264 | std::vector<Association> associations; |
James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 265 | associations.push_back( |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 266 | Association("chassis", "all_sensors", p.parent_path().string())); |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 267 | association->register_property("Associations", associations); |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 268 | association->initialize(); |
| 269 | } |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 270 | } |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 271 | |
| 272 | void setInventoryAssociation( |
| 273 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, |
| 274 | const std::string& path, const std::string& chassisPath) |
| 275 | { |
| 276 | if (association) |
| 277 | { |
| 278 | std::filesystem::path p(path); |
| 279 | |
| 280 | std::vector<Association> associations; |
| 281 | associations.push_back( |
| 282 | Association("inventory", "sensors", p.parent_path().string())); |
| 283 | associations.push_back( |
| 284 | Association("chassis", "all_sensors", chassisPath)); |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 285 | association->register_property("Associations", associations); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 286 | association->initialize(); |
| 287 | } |
| 288 | } |
| 289 | |
| 290 | void createInventoryAssoc( |
| 291 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 292 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, |
| 293 | const std::string& path) |
| 294 | { |
| 295 | if (!association) |
| 296 | { |
| 297 | return; |
| 298 | } |
| 299 | conn->async_method_call( |
| 300 | [association, path](const boost::system::error_code ec, |
| 301 | const std::vector<std::string>& ret) { |
| 302 | if (ec) |
| 303 | { |
| 304 | std::cerr << "Error calling mapper\n"; |
| 305 | return; |
| 306 | } |
| 307 | for (const auto& object : ret) |
| 308 | { |
| 309 | setInventoryAssociation(association, path, object); |
| 310 | } |
| 311 | }, |
| 312 | mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths", |
| 313 | "/xyz/openbmc_project/inventory/system", 2, |
| 314 | std::array<std::string, 1>{ |
| 315 | "xyz.openbmc_project.Inventory.Item.System"}); |
| 316 | } |