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 | |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 43 | /** |
| 44 | * return the contents of a file |
| 45 | * @param[in] hwmonFile - the path to the file to read |
| 46 | * @return the contents of the file as a string or nullopt if the file could not |
| 47 | * be opened. |
| 48 | */ |
| 49 | |
| 50 | std::optional<std::string> openAndRead(const std::string& hwmonFile) |
| 51 | { |
| 52 | std::string fileVal; |
| 53 | std::ifstream fileStream(hwmonFile); |
| 54 | if (!fileStream.is_open()) |
| 55 | { |
| 56 | return std::nullopt; |
| 57 | } |
| 58 | std::getline(fileStream, fileVal); |
| 59 | return fileVal; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * given a hwmon temperature base name if valid return the full path else |
| 64 | * nullopt |
| 65 | * @param[in] directory - the hwmon sysfs directory |
| 66 | * @param[in] permitSet - a set of labels or hwmon basenames to permit. If this |
| 67 | * is empty then *everything* is permitted. |
| 68 | * @return a string to the full path of the file to create a temp sensor with or |
| 69 | * nullopt to indicate that no sensor should be created for this basename. |
| 70 | */ |
| 71 | std::optional<std::string> |
| 72 | getFullHwmonFilePath(const std::string& directory, |
| 73 | const std::string& hwmonBaseName, |
| 74 | const std::set<std::string>& permitSet) |
| 75 | { |
| 76 | std::optional<std::string> result; |
| 77 | std::string filename; |
| 78 | if (permitSet.empty()) |
| 79 | { |
| 80 | result = directory + "/" + hwmonBaseName + "_input"; |
| 81 | return result; |
| 82 | } |
| 83 | filename = directory + "/" + hwmonBaseName + "_label"; |
| 84 | auto searchVal = openAndRead(filename); |
| 85 | if (!searchVal) |
| 86 | { |
| 87 | /* if the hwmon temp doesn't have a corresponding label file |
| 88 | * then use the hwmon temperature base name |
| 89 | */ |
| 90 | searchVal = hwmonBaseName; |
| 91 | } |
| 92 | if (permitSet.find(*searchVal) != permitSet.end()) |
| 93 | { |
| 94 | result = directory + "/" + hwmonBaseName + "_input"; |
| 95 | } |
| 96 | return result; |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * retrieve a set of basenames and labels to allow sensor creation for. |
| 101 | * @param[in] config - a map representing the configuration for a specific |
| 102 | * device |
| 103 | * @return a set of basenames and labels to allow sensor creation for. An empty |
| 104 | * set indicates that everything is permitted. |
| 105 | */ |
| 106 | std::set<std::string> getPermitSet(const SensorBaseConfigMap& config) |
| 107 | { |
| 108 | auto permitAttribute = config.find("Labels"); |
| 109 | std::set<std::string> permitSet; |
| 110 | if (permitAttribute != config.end()) |
| 111 | { |
| 112 | try |
| 113 | { |
| 114 | auto val = |
| 115 | std::get<std::vector<std::string>>(permitAttribute->second); |
| 116 | |
| 117 | permitSet.insert(std::make_move_iterator(val.begin()), |
| 118 | std::make_move_iterator(val.end())); |
| 119 | } |
| 120 | catch (const std::bad_variant_access& err) |
| 121 | { |
| 122 | std::cerr << err.what() |
| 123 | << ":PermitList does not contain a list, wrong " |
| 124 | "variant type.\n"; |
| 125 | } |
| 126 | } |
| 127 | return permitSet; |
| 128 | } |
| 129 | |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 130 | bool getSensorConfiguration( |
| 131 | const std::string& type, |
| 132 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 133 | ManagedObjectType& resp, bool useCache) |
| 134 | { |
| 135 | static ManagedObjectType managedObj; |
| 136 | |
| 137 | if (!useCache) |
| 138 | { |
| 139 | managedObj.clear(); |
| 140 | sdbusplus::message::message getManagedObjects = |
| 141 | dbusConnection->new_method_call( |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 142 | entityManagerName, "/", "org.freedesktop.DBus.ObjectManager", |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 143 | "GetManagedObjects"); |
| 144 | bool err = false; |
| 145 | try |
| 146 | { |
| 147 | sdbusplus::message::message reply = |
| 148 | dbusConnection->call(getManagedObjects); |
Yoo, Jae Hyun | 0e02205 | 2018-10-15 14:05:37 -0700 | [diff] [blame] | 149 | reply.read(managedObj); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 150 | } |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 151 | catch (const sdbusplus::exception::exception& e) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 152 | { |
Jason Ling | 5747fab | 2019-10-02 16:46:23 -0700 | [diff] [blame] | 153 | std::cerr << "While calling GetManagedObjects on service:" |
| 154 | << entityManagerName << " exception name:" << e.name() |
| 155 | << "and description:" << e.description() |
| 156 | << " was thrown\n"; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 157 | err = true; |
| 158 | } |
| 159 | |
| 160 | if (err) |
| 161 | { |
| 162 | std::cerr << "Error communicating to entity manager\n"; |
| 163 | return false; |
| 164 | } |
| 165 | } |
| 166 | for (const auto& pathPair : managedObj) |
| 167 | { |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 168 | bool correctType = false; |
| 169 | for (const auto& entry : pathPair.second) |
| 170 | { |
| 171 | if (boost::starts_with(entry.first, type)) |
| 172 | { |
| 173 | correctType = true; |
| 174 | break; |
| 175 | } |
| 176 | } |
| 177 | if (correctType) |
| 178 | { |
| 179 | resp.emplace(pathPair); |
| 180 | } |
| 181 | } |
| 182 | return true; |
| 183 | } |
| 184 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 185 | bool findFiles(const fs::path dirPath, const std::string& matchString, |
| 186 | std::vector<fs::path>& foundPaths, unsigned int symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 188 | if (!fs::exists(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 189 | return false; |
| 190 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 191 | std::regex search(matchString); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 192 | std::smatch match; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 193 | for (auto& p : fs::recursive_directory_iterator(dirPath)) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 194 | { |
| 195 | std::string path = p.path().string(); |
| 196 | if (!is_directory(p)) |
| 197 | { |
| 198 | if (std::regex_search(path, match, search)) |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 199 | foundPaths.emplace_back(p.path()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 200 | } |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 201 | else if (is_symlink(p) && symlinkDepth) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 202 | { |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 203 | findFiles(p.path(), matchString, foundPaths, symlinkDepth - 1); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 204 | } |
| 205 | } |
| 206 | return true; |
| 207 | } |
| 208 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 209 | bool isPowerOn(void) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 210 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 211 | if (!powerMatch) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 212 | { |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 213 | throw std::runtime_error("Power Match Not Created"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 214 | } |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 215 | return powerStatusOn; |
| 216 | } |
| 217 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 218 | bool hasBiosPost(void) |
| 219 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 220 | if (!postMatch) |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 221 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 222 | throw std::runtime_error("Post Match Not Created"); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 223 | } |
| 224 | return biosHasPost; |
| 225 | } |
| 226 | |
James Feist | 8aeffd9 | 2020-09-14 12:08:28 -0700 | [diff] [blame] | 227 | static void |
| 228 | getPowerStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 229 | size_t retries = 2) |
| 230 | { |
| 231 | conn->async_method_call( |
| 232 | [conn, retries](boost::system::error_code ec, |
| 233 | const std::variant<std::string>& state) { |
| 234 | if (ec) |
| 235 | { |
| 236 | if (retries) |
| 237 | { |
| 238 | auto timer = std::make_shared<boost::asio::steady_timer>( |
| 239 | conn->get_io_context()); |
| 240 | timer->expires_after(std::chrono::seconds(15)); |
| 241 | timer->async_wait( |
| 242 | [timer, conn, retries](boost::system::error_code) { |
| 243 | getPowerStatus(conn, retries - 1); |
| 244 | }); |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | // we commonly come up before power control, we'll capture the |
| 249 | // property change later |
| 250 | std::cerr << "error getting power status " << ec.message() |
| 251 | << "\n"; |
| 252 | return; |
| 253 | } |
| 254 | powerStatusOn = |
| 255 | boost::ends_with(std::get<std::string>(state), "Running"); |
| 256 | }, |
| 257 | power::busname, power::path, properties::interface, properties::get, |
| 258 | power::interface, power::property); |
| 259 | } |
| 260 | |
| 261 | static void |
| 262 | getPostStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 263 | size_t retries = 2) |
| 264 | { |
| 265 | conn->async_method_call( |
| 266 | [conn, retries](boost::system::error_code ec, |
| 267 | const std::variant<std::string>& state) { |
| 268 | if (ec) |
| 269 | { |
| 270 | if (retries) |
| 271 | { |
| 272 | auto timer = std::make_shared<boost::asio::steady_timer>( |
| 273 | conn->get_io_context()); |
| 274 | timer->expires_after(std::chrono::seconds(15)); |
| 275 | timer->async_wait( |
| 276 | [timer, conn, retries](boost::system::error_code) { |
| 277 | getPostStatus(conn, retries - 1); |
| 278 | }); |
| 279 | return; |
| 280 | } |
| 281 | // we commonly come up before power control, we'll capture the |
| 282 | // property change later |
| 283 | std::cerr << "error getting post status " << ec.message() |
| 284 | << "\n"; |
| 285 | return; |
| 286 | } |
| 287 | biosHasPost = std::get<std::string>(state) != "Inactive"; |
| 288 | }, |
| 289 | post::busname, post::path, properties::interface, properties::get, |
| 290 | post::interface, post::property); |
| 291 | } |
| 292 | |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 293 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn) |
| 294 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 295 | static boost::asio::steady_timer timer(conn->get_io_context()); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 296 | // 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] | 297 | // cache the correct value |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 298 | if (powerMatch) |
| 299 | { |
| 300 | return; |
| 301 | } |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 302 | |
| 303 | powerMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 304 | static_cast<sdbusplus::bus::bus&>(*conn), |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 305 | "type='signal',interface='" + std::string(properties::interface) + |
| 306 | "',path='" + std::string(power::path) + "',arg0='" + |
| 307 | std::string(power::interface) + "'", |
| 308 | [](sdbusplus::message::message& message) { |
| 309 | std::string objectName; |
| 310 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 311 | values; |
| 312 | message.read(objectName, values); |
| 313 | auto findState = values.find(power::property); |
| 314 | if (findState != values.end()) |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 315 | { |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 316 | bool on = boost::ends_with( |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 317 | std::get<std::string>(findState->second), "Running"); |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 318 | if (!on) |
| 319 | { |
| 320 | timer.cancel(); |
| 321 | powerStatusOn = false; |
| 322 | return; |
| 323 | } |
| 324 | // on comes too quickly |
| 325 | timer.expires_after(std::chrono::seconds(10)); |
| 326 | timer.async_wait([](boost::system::error_code ec) { |
| 327 | if (ec == boost::asio::error::operation_aborted) |
| 328 | { |
| 329 | return; |
| 330 | } |
| 331 | else if (ec) |
| 332 | { |
| 333 | std::cerr << "Timer error " << ec.message() << "\n"; |
| 334 | return; |
| 335 | } |
| 336 | powerStatusOn = true; |
| 337 | }); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 338 | } |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 339 | }); |
| 340 | |
| 341 | postMatch = std::make_unique<sdbusplus::bus::match::match>( |
| 342 | static_cast<sdbusplus::bus::bus&>(*conn), |
| 343 | "type='signal',interface='" + std::string(properties::interface) + |
| 344 | "',path='" + std::string(post::path) + "',arg0='" + |
| 345 | std::string(post::interface) + "'", |
| 346 | [](sdbusplus::message::message& message) { |
| 347 | std::string objectName; |
| 348 | boost::container::flat_map<std::string, std::variant<std::string>> |
| 349 | values; |
| 350 | message.read(objectName, values); |
| 351 | auto findState = values.find(post::property); |
| 352 | if (findState != values.end()) |
| 353 | { |
| 354 | biosHasPost = |
| 355 | std::get<std::string>(findState->second) != "Inactive"; |
| 356 | } |
| 357 | }); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 358 | |
James Feist | 8aeffd9 | 2020-09-14 12:08:28 -0700 | [diff] [blame] | 359 | getPowerStatus(conn); |
| 360 | getPostStatus(conn); |
James Feist | 3f0e876 | 2018-11-27 11:30:42 -0800 | [diff] [blame] | 361 | } |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 362 | |
| 363 | // replaces limits if MinReading and MaxReading are found. |
| 364 | void findLimits(std::pair<double, double>& limits, |
| 365 | const SensorBaseConfiguration* data) |
| 366 | { |
| 367 | if (!data) |
| 368 | { |
| 369 | return; |
| 370 | } |
| 371 | auto maxFind = data->second.find("MaxReading"); |
| 372 | auto minFind = data->second.find("MinReading"); |
| 373 | |
| 374 | if (minFind != data->second.end()) |
| 375 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 376 | limits.first = std::visit(VariantToDoubleVisitor(), minFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 377 | } |
| 378 | if (maxFind != data->second.end()) |
| 379 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 380 | limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 381 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 382 | } |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 383 | |
| 384 | void createAssociation( |
| 385 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
| 386 | const std::string& path) |
| 387 | { |
| 388 | if (association) |
| 389 | { |
James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 390 | std::filesystem::path p(path); |
| 391 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 392 | std::vector<Association> associations; |
James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 393 | associations.push_back( |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 394 | Association("chassis", "all_sensors", p.parent_path().string())); |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 395 | association->register_property("Associations", associations); |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 396 | association->initialize(); |
| 397 | } |
James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 398 | } |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 399 | |
| 400 | void setInventoryAssociation( |
| 401 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 402 | const std::string& path, |
| 403 | const std::vector<std::string>& chassisPaths = std::vector<std::string>()) |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 404 | { |
| 405 | if (association) |
| 406 | { |
| 407 | std::filesystem::path p(path); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 408 | std::vector<Association> associations; |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 409 | std::string objPath(p.parent_path().string()); |
| 410 | |
| 411 | associations.push_back(Association("inventory", "sensors", objPath)); |
| 412 | associations.push_back(Association("chassis", "all_sensors", objPath)); |
| 413 | |
| 414 | for (const std::string& chassisPath : chassisPaths) |
| 415 | { |
| 416 | associations.push_back( |
| 417 | Association("chassis", "all_sensors", chassisPath)); |
| 418 | } |
| 419 | |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 420 | association->register_property("Associations", associations); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 421 | association->initialize(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | void createInventoryAssoc( |
| 426 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 427 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, |
| 428 | const std::string& path) |
| 429 | { |
| 430 | if (!association) |
| 431 | { |
| 432 | return; |
| 433 | } |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 434 | |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 435 | conn->async_method_call( |
| 436 | [association, path](const boost::system::error_code ec, |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 437 | const std::vector<std::string>& invSysObjPaths) { |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 438 | if (ec) |
| 439 | { |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 440 | // In case of error, set the default associations and |
| 441 | // initialize the association Interface. |
| 442 | setInventoryAssociation(association, path); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 443 | return; |
| 444 | } |
AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 445 | setInventoryAssociation(association, path, invSysObjPaths); |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 446 | }, |
| 447 | mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths", |
| 448 | "/xyz/openbmc_project/inventory/system", 2, |
| 449 | std::array<std::string, 1>{ |
| 450 | "xyz.openbmc_project.Inventory.Item.System"}); |
| 451 | } |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 452 | |
| 453 | std::optional<double> readFile(const std::string& thresholdFile, |
| 454 | const double& scaleFactor) |
| 455 | { |
| 456 | std::string line; |
| 457 | std::ifstream labelFile(thresholdFile); |
| 458 | if (labelFile.good()) |
| 459 | { |
| 460 | std::getline(labelFile, line); |
| 461 | labelFile.close(); |
| 462 | |
| 463 | try |
| 464 | { |
| 465 | return std::stod(line) / scaleFactor; |
| 466 | } |
| 467 | catch (const std::invalid_argument&) |
| 468 | { |
| 469 | return std::nullopt; |
| 470 | } |
| 471 | } |
| 472 | return std::nullopt; |
| 473 | } |
| 474 | |
| 475 | std::optional<std::tuple<std::string, std::string, std::string>> |
| 476 | splitFileName(const std::filesystem::path& filePath) |
| 477 | { |
| 478 | if (filePath.has_filename()) |
| 479 | { |
| 480 | const auto fileName = filePath.filename().string(); |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 481 | |
Zbigniew Kurzynski | dd64800 | 2020-07-10 16:44:16 +0200 | [diff] [blame] | 482 | size_t numberPos = std::strcspn(fileName.c_str(), "1234567890"); |
| 483 | size_t itemPos = std::strcspn(fileName.c_str(), "_"); |
| 484 | |
| 485 | if (numberPos > 0 && itemPos > numberPos && fileName.size() > itemPos) |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 486 | { |
Zbigniew Kurzynski | dd64800 | 2020-07-10 16:44:16 +0200 | [diff] [blame] | 487 | return std::make_optional( |
| 488 | std::make_tuple(fileName.substr(0, numberPos), |
| 489 | fileName.substr(numberPos, itemPos - numberPos), |
| 490 | fileName.substr(itemPos + 1, fileName.size()))); |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 491 | } |
| 492 | } |
| 493 | return std::nullopt; |
Zbigniew Kurzynski | dd64800 | 2020-07-10 16:44:16 +0200 | [diff] [blame] | 494 | } |