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