| 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 | 8aeffd9 | 2020-09-14 12:08:28 -0700 | [diff] [blame] | 140 | static void | 
|  | 141 | getPowerStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn, | 
|  | 142 | size_t retries = 2) | 
|  | 143 | { | 
|  | 144 | conn->async_method_call( | 
|  | 145 | [conn, retries](boost::system::error_code ec, | 
|  | 146 | const std::variant<std::string>& state) { | 
|  | 147 | if (ec) | 
|  | 148 | { | 
|  | 149 | if (retries) | 
|  | 150 | { | 
|  | 151 | auto timer = std::make_shared<boost::asio::steady_timer>( | 
|  | 152 | conn->get_io_context()); | 
|  | 153 | timer->expires_after(std::chrono::seconds(15)); | 
|  | 154 | timer->async_wait( | 
|  | 155 | [timer, conn, retries](boost::system::error_code) { | 
|  | 156 | getPowerStatus(conn, retries - 1); | 
|  | 157 | }); | 
|  | 158 | return; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | // we commonly come up before power control, we'll capture the | 
|  | 162 | // property change later | 
|  | 163 | std::cerr << "error getting power status " << ec.message() | 
|  | 164 | << "\n"; | 
|  | 165 | return; | 
|  | 166 | } | 
|  | 167 | powerStatusOn = | 
|  | 168 | boost::ends_with(std::get<std::string>(state), "Running"); | 
|  | 169 | }, | 
|  | 170 | power::busname, power::path, properties::interface, properties::get, | 
|  | 171 | power::interface, power::property); | 
|  | 172 | } | 
|  | 173 |  | 
|  | 174 | static void | 
|  | 175 | getPostStatus(const std::shared_ptr<sdbusplus::asio::connection>& conn, | 
|  | 176 | size_t retries = 2) | 
|  | 177 | { | 
|  | 178 | conn->async_method_call( | 
|  | 179 | [conn, retries](boost::system::error_code ec, | 
|  | 180 | const std::variant<std::string>& state) { | 
|  | 181 | if (ec) | 
|  | 182 | { | 
|  | 183 | if (retries) | 
|  | 184 | { | 
|  | 185 | auto timer = std::make_shared<boost::asio::steady_timer>( | 
|  | 186 | conn->get_io_context()); | 
|  | 187 | timer->expires_after(std::chrono::seconds(15)); | 
|  | 188 | timer->async_wait( | 
|  | 189 | [timer, conn, retries](boost::system::error_code) { | 
|  | 190 | getPostStatus(conn, retries - 1); | 
|  | 191 | }); | 
|  | 192 | return; | 
|  | 193 | } | 
|  | 194 | // we commonly come up before power control, we'll capture the | 
|  | 195 | // property change later | 
|  | 196 | std::cerr << "error getting post status " << ec.message() | 
|  | 197 | << "\n"; | 
|  | 198 | return; | 
|  | 199 | } | 
|  | 200 | biosHasPost = std::get<std::string>(state) != "Inactive"; | 
|  | 201 | }, | 
|  | 202 | post::busname, post::path, properties::interface, properties::get, | 
|  | 203 | post::interface, post::property); | 
|  | 204 | } | 
|  | 205 |  | 
| James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 206 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn) | 
|  | 207 | { | 
| James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 208 | static boost::asio::steady_timer timer(conn->get_io_context()); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 209 | // 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] | 210 | // cache the correct value | 
| James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 211 | if (powerMatch) | 
|  | 212 | { | 
|  | 213 | return; | 
|  | 214 | } | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 215 |  | 
|  | 216 | powerMatch = std::make_unique<sdbusplus::bus::match::match>( | 
|  | 217 | static_cast<sdbusplus::bus::bus&>(*conn), | 
| James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 218 | "type='signal',interface='" + std::string(properties::interface) + | 
|  | 219 | "',path='" + std::string(power::path) + "',arg0='" + | 
|  | 220 | std::string(power::interface) + "'", | 
|  | 221 | [](sdbusplus::message::message& message) { | 
|  | 222 | std::string objectName; | 
|  | 223 | boost::container::flat_map<std::string, std::variant<std::string>> | 
|  | 224 | values; | 
|  | 225 | message.read(objectName, values); | 
|  | 226 | auto findState = values.find(power::property); | 
|  | 227 | if (findState != values.end()) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 228 | { | 
| James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 229 | bool on = boost::ends_with( | 
| James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 230 | std::get<std::string>(findState->second), "Running"); | 
| James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 231 | if (!on) | 
|  | 232 | { | 
|  | 233 | timer.cancel(); | 
|  | 234 | powerStatusOn = false; | 
|  | 235 | return; | 
|  | 236 | } | 
|  | 237 | // on comes too quickly | 
|  | 238 | timer.expires_after(std::chrono::seconds(10)); | 
|  | 239 | timer.async_wait([](boost::system::error_code ec) { | 
|  | 240 | if (ec == boost::asio::error::operation_aborted) | 
|  | 241 | { | 
|  | 242 | return; | 
|  | 243 | } | 
|  | 244 | else if (ec) | 
|  | 245 | { | 
|  | 246 | std::cerr << "Timer error " << ec.message() << "\n"; | 
|  | 247 | return; | 
|  | 248 | } | 
|  | 249 | powerStatusOn = true; | 
|  | 250 | }); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 251 | } | 
| James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 252 | }); | 
|  | 253 |  | 
|  | 254 | postMatch = std::make_unique<sdbusplus::bus::match::match>( | 
|  | 255 | static_cast<sdbusplus::bus::bus&>(*conn), | 
|  | 256 | "type='signal',interface='" + std::string(properties::interface) + | 
|  | 257 | "',path='" + std::string(post::path) + "',arg0='" + | 
|  | 258 | std::string(post::interface) + "'", | 
|  | 259 | [](sdbusplus::message::message& message) { | 
|  | 260 | std::string objectName; | 
|  | 261 | boost::container::flat_map<std::string, std::variant<std::string>> | 
|  | 262 | values; | 
|  | 263 | message.read(objectName, values); | 
|  | 264 | auto findState = values.find(post::property); | 
|  | 265 | if (findState != values.end()) | 
|  | 266 | { | 
|  | 267 | biosHasPost = | 
|  | 268 | std::get<std::string>(findState->second) != "Inactive"; | 
|  | 269 | } | 
|  | 270 | }); | 
| James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 271 |  | 
| James Feist | 8aeffd9 | 2020-09-14 12:08:28 -0700 | [diff] [blame] | 272 | getPowerStatus(conn); | 
|  | 273 | getPostStatus(conn); | 
| James Feist | 3f0e876 | 2018-11-27 11:30:42 -0800 | [diff] [blame] | 274 | } | 
| James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 275 |  | 
|  | 276 | // replaces limits if MinReading and MaxReading are found. | 
|  | 277 | void findLimits(std::pair<double, double>& limits, | 
|  | 278 | const SensorBaseConfiguration* data) | 
|  | 279 | { | 
|  | 280 | if (!data) | 
|  | 281 | { | 
|  | 282 | return; | 
|  | 283 | } | 
|  | 284 | auto maxFind = data->second.find("MaxReading"); | 
|  | 285 | auto minFind = data->second.find("MinReading"); | 
|  | 286 |  | 
|  | 287 | if (minFind != data->second.end()) | 
|  | 288 | { | 
| James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 289 | limits.first = std::visit(VariantToDoubleVisitor(), minFind->second); | 
| James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 290 | } | 
|  | 291 | if (maxFind != data->second.end()) | 
|  | 292 | { | 
| James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 293 | limits.second = std::visit(VariantToDoubleVisitor(), maxFind->second); | 
| James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 294 | } | 
| James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 295 | } | 
| James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 296 |  | 
|  | 297 | void createAssociation( | 
|  | 298 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, | 
|  | 299 | const std::string& path) | 
|  | 300 | { | 
|  | 301 | if (association) | 
|  | 302 | { | 
| James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 303 | std::filesystem::path p(path); | 
|  | 304 |  | 
| James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 305 | std::vector<Association> associations; | 
| James Feist | d2543f8 | 2019-04-09 11:10:06 -0700 | [diff] [blame] | 306 | associations.push_back( | 
| James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 307 | Association("chassis", "all_sensors", p.parent_path().string())); | 
| James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 308 | association->register_property("Associations", associations); | 
| James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 309 | association->initialize(); | 
|  | 310 | } | 
| James Feist | 43d32fe | 2019-09-04 10:35:20 -0700 | [diff] [blame] | 311 | } | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 312 |  | 
|  | 313 | void setInventoryAssociation( | 
|  | 314 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 315 | const std::string& path, | 
|  | 316 | const std::vector<std::string>& chassisPaths = std::vector<std::string>()) | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 317 | { | 
|  | 318 | if (association) | 
|  | 319 | { | 
|  | 320 | std::filesystem::path p(path); | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 321 | std::vector<Association> associations; | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 322 | std::string objPath(p.parent_path().string()); | 
|  | 323 |  | 
|  | 324 | associations.push_back(Association("inventory", "sensors", objPath)); | 
|  | 325 | associations.push_back(Association("chassis", "all_sensors", objPath)); | 
|  | 326 |  | 
|  | 327 | for (const std::string& chassisPath : chassisPaths) | 
|  | 328 | { | 
|  | 329 | associations.push_back( | 
|  | 330 | Association("chassis", "all_sensors", chassisPath)); | 
|  | 331 | } | 
|  | 332 |  | 
| James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 333 | association->register_property("Associations", associations); | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 334 | association->initialize(); | 
|  | 335 | } | 
|  | 336 | } | 
|  | 337 |  | 
|  | 338 | void createInventoryAssoc( | 
|  | 339 | std::shared_ptr<sdbusplus::asio::connection> conn, | 
|  | 340 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, | 
|  | 341 | const std::string& path) | 
|  | 342 | { | 
|  | 343 | if (!association) | 
|  | 344 | { | 
|  | 345 | return; | 
|  | 346 | } | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 347 |  | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 348 | conn->async_method_call( | 
|  | 349 | [association, path](const boost::system::error_code ec, | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 350 | const std::vector<std::string>& invSysObjPaths) { | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 351 | if (ec) | 
|  | 352 | { | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 353 | // In case of error, set the default associations and | 
|  | 354 | // initialize the association Interface. | 
|  | 355 | setInventoryAssociation(association, path); | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 356 | return; | 
|  | 357 | } | 
| AppaRao Puli | c82213c | 2020-02-27 01:24:58 +0530 | [diff] [blame] | 358 | setInventoryAssociation(association, path, invSysObjPaths); | 
| Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 359 | }, | 
|  | 360 | mapper::busName, mapper::path, mapper::interface, "GetSubTreePaths", | 
|  | 361 | "/xyz/openbmc_project/inventory/system", 2, | 
|  | 362 | std::array<std::string, 1>{ | 
|  | 363 | "xyz.openbmc_project.Inventory.Item.System"}); | 
|  | 364 | } | 
| Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 365 |  | 
|  | 366 | std::optional<double> readFile(const std::string& thresholdFile, | 
|  | 367 | const double& scaleFactor) | 
|  | 368 | { | 
|  | 369 | std::string line; | 
|  | 370 | std::ifstream labelFile(thresholdFile); | 
|  | 371 | if (labelFile.good()) | 
|  | 372 | { | 
|  | 373 | std::getline(labelFile, line); | 
|  | 374 | labelFile.close(); | 
|  | 375 |  | 
|  | 376 | try | 
|  | 377 | { | 
|  | 378 | return std::stod(line) / scaleFactor; | 
|  | 379 | } | 
|  | 380 | catch (const std::invalid_argument&) | 
|  | 381 | { | 
|  | 382 | return std::nullopt; | 
|  | 383 | } | 
|  | 384 | } | 
|  | 385 | return std::nullopt; | 
|  | 386 | } | 
|  | 387 |  | 
|  | 388 | std::optional<std::tuple<std::string, std::string, std::string>> | 
|  | 389 | splitFileName(const std::filesystem::path& filePath) | 
|  | 390 | { | 
|  | 391 | if (filePath.has_filename()) | 
|  | 392 | { | 
|  | 393 | const auto fileName = filePath.filename().string(); | 
| Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 394 |  | 
| Zbigniew Kurzynski | dd64800 | 2020-07-10 16:44:16 +0200 | [diff] [blame] | 395 | size_t numberPos = std::strcspn(fileName.c_str(), "1234567890"); | 
|  | 396 | size_t itemPos = std::strcspn(fileName.c_str(), "_"); | 
|  | 397 |  | 
|  | 398 | if (numberPos > 0 && itemPos > numberPos && fileName.size() > itemPos) | 
| Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 399 | { | 
| Zbigniew Kurzynski | dd64800 | 2020-07-10 16:44:16 +0200 | [diff] [blame] | 400 | return std::make_optional( | 
|  | 401 | std::make_tuple(fileName.substr(0, numberPos), | 
|  | 402 | fileName.substr(numberPos, itemPos - numberPos), | 
|  | 403 | fileName.substr(itemPos + 1, fileName.size()))); | 
| Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 404 | } | 
|  | 405 | } | 
|  | 406 | return std::nullopt; | 
| Zbigniew Kurzynski | dd64800 | 2020-07-10 16:44:16 +0200 | [diff] [blame] | 407 | } |