James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 2 | #include <VariantVisitors.hpp> |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 3 | #include <boost/algorithm/string/predicate.hpp> |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 4 | #include <boost/asio/steady_timer.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 5 | #include <boost/container/flat_map.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 6 | #include <sdbusplus/asio/connection.hpp> |
| 7 | #include <sdbusplus/asio/object_server.hpp> |
| 8 | #include <sdbusplus/message/types.hpp> |
| 9 | |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 10 | #include <filesystem> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 11 | #include <functional> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 12 | #include <iostream> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 13 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 14 | #include <regex> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 15 | #include <string> |
| 16 | #include <tuple> |
| 17 | #include <utility> |
| 18 | #include <variant> |
| 19 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 21 | const constexpr char* jsonStore = "/var/configuration/flattened.json"; |
| 22 | const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory"; |
| 23 | const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager"; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 24 | |
| 25 | constexpr const char* cpuInventoryPath = |
| 26 | "/xyz/openbmc_project/inventory/system/chassis/motherboard"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 27 | const std::regex illegalDbusRegex("[^A-Za-z0-9_]"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 28 | |
| 29 | using BasicVariantType = |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 30 | std::variant<std::vector<std::string>, std::string, int64_t, uint64_t, |
| 31 | double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>; |
Alex Qiu | 8b3f7d4 | 2020-01-06 13:54:42 -0800 | [diff] [blame] | 32 | using SensorBaseConfigMap = |
| 33 | boost::container::flat_map<std::string, BasicVariantType>; |
| 34 | using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>; |
| 35 | using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>; |
| 36 | using ManagedObjectType = |
| 37 | boost::container::flat_map<sdbusplus::message::object_path, SensorData>; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 38 | |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 39 | using GetSubTreeType = std::vector< |
| 40 | std::pair<std::string, |
| 41 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 42 | using Association = std::tuple<std::string, std::string, std::string>; |
| 43 | |
Jason Ling | 100c20b | 2020-08-11 14:50:33 -0700 | [diff] [blame] | 44 | std::optional<std::string> openAndRead(const std::string& hwmonFile); |
| 45 | std::optional<std::string> |
| 46 | getFullHwmonFilePath(const std::string& directory, |
| 47 | const std::string& hwmonBaseName, |
| 48 | const std::set<std::string>& permitSet); |
| 49 | std::set<std::string> getPermitSet(const SensorBaseConfigMap& config); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 50 | bool findFiles(const std::filesystem::path& dirPath, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 51 | const std::string& matchString, |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 52 | std::vector<std::filesystem::path>& foundPaths, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 53 | int symlinkDepth = 1); |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 54 | bool isPowerOn(void); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 55 | bool hasBiosPost(void); |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 56 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | bool getSensorConfiguration( |
| 58 | const std::string& type, |
| 59 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 60 | ManagedObjectType& resp, bool useCache); |
| 61 | |
| 62 | bool getSensorConfiguration( |
| 63 | const std::string& type, |
| 64 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
| 65 | ManagedObjectType& resp); |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 66 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 67 | void createAssociation( |
| 68 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
| 69 | const std::string& path); |
| 70 | |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 71 | // replaces limits if MinReading and MaxReading are found. |
| 72 | void findLimits(std::pair<double, double>& limits, |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 73 | const SensorBaseConfiguration* data); |
| 74 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 75 | enum class PowerState |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 76 | { |
| 77 | on, |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 78 | biosPost, |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 79 | always |
| 80 | }; |
| 81 | |
Konstantin Aladyshev | c7a1ae6 | 2021-04-30 08:50:43 +0000 | [diff] [blame^] | 82 | bool readingStateGood(const PowerState& powerState); |
| 83 | |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 84 | namespace mapper |
| 85 | { |
| 86 | constexpr const char* busName = "xyz.openbmc_project.ObjectMapper"; |
| 87 | constexpr const char* path = "/xyz/openbmc_project/object_mapper"; |
| 88 | constexpr const char* interface = "xyz.openbmc_project.ObjectMapper"; |
| 89 | constexpr const char* subtree = "GetSubTree"; |
| 90 | } // namespace mapper |
| 91 | |
| 92 | namespace properties |
| 93 | { |
| 94 | constexpr const char* interface = "org.freedesktop.DBus.Properties"; |
| 95 | constexpr const char* get = "Get"; |
James Feist | 49a8ccd | 2020-09-16 16:09:52 -0700 | [diff] [blame] | 96 | constexpr const char* set = "Set"; |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 97 | } // namespace properties |
| 98 | |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 99 | namespace power |
| 100 | { |
| 101 | const static constexpr char* busname = "xyz.openbmc_project.State.Host"; |
| 102 | const static constexpr char* interface = "xyz.openbmc_project.State.Host"; |
| 103 | const static constexpr char* path = "/xyz/openbmc_project/state/host0"; |
| 104 | const static constexpr char* property = "CurrentHostState"; |
| 105 | } // namespace power |
| 106 | namespace post |
| 107 | { |
| 108 | const static constexpr char* busname = |
| 109 | "xyz.openbmc_project.State.OperatingSystem"; |
| 110 | const static constexpr char* interface = |
| 111 | "xyz.openbmc_project.State.OperatingSystem.Status"; |
| 112 | const static constexpr char* path = "/xyz/openbmc_project/state/os"; |
| 113 | const static constexpr char* property = "OperatingSystemState"; |
| 114 | } // namespace post |
| 115 | |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 116 | namespace association |
| 117 | { |
| 118 | const static constexpr char* interface = |
| 119 | "xyz.openbmc_project.Association.Definitions"; |
| 120 | } // namespace association |
| 121 | |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 122 | template <typename T> |
| 123 | inline T loadVariant( |
| 124 | const boost::container::flat_map<std::string, BasicVariantType>& data, |
| 125 | const std::string& key) |
| 126 | { |
| 127 | auto it = data.find(key); |
| 128 | if (it == data.end()) |
| 129 | { |
| 130 | std::cerr << "Configuration missing " << key << "\n"; |
| 131 | throw std::invalid_argument("Key Missing"); |
| 132 | } |
| 133 | if constexpr (std::is_same_v<T, double>) |
| 134 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 135 | return std::visit(VariantToDoubleVisitor(), it->second); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 136 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 137 | else if constexpr (std::is_unsigned_v<T>) |
| 138 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 139 | return std::visit(VariantToUnsignedIntVisitor(), it->second); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 140 | } |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 141 | else if constexpr (std::is_same_v<T, std::string>) |
| 142 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 143 | return std::visit(VariantToStringVisitor(), it->second); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 144 | } |
| 145 | else |
| 146 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 147 | static_assert(!std::is_same_v<T, T>, "Type Not Implemented"); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 148 | } |
| 149 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 150 | |
| 151 | inline void setReadState(const std::string& str, PowerState& val) |
| 152 | { |
| 153 | |
| 154 | if (str == "On") |
| 155 | { |
| 156 | val = PowerState::on; |
| 157 | } |
| 158 | else if (str == "BiosPost") |
| 159 | { |
| 160 | val = PowerState::biosPost; |
| 161 | } |
| 162 | else if (str == "Always") |
| 163 | { |
| 164 | val = PowerState::always; |
| 165 | } |
| 166 | } |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 167 | |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 168 | inline void setLed(const std::shared_ptr<sdbusplus::asio::connection>& conn, |
James Feist | 49a8ccd | 2020-09-16 16:09:52 -0700 | [diff] [blame] | 169 | const std::string& name, bool on) |
| 170 | { |
| 171 | conn->async_method_call( |
| 172 | [name](const boost::system::error_code ec) { |
| 173 | if (ec) |
| 174 | { |
| 175 | std::cerr << "Failed to set LED " << name << "\n"; |
| 176 | } |
| 177 | }, |
| 178 | "xyz.openbmc_project.LED.GroupManager", |
| 179 | "/xyz/openbmc_project/led/groups/" + name, properties::interface, |
| 180 | properties::set, "xyz.openbmc_project.Led.Group", "Asserted", |
| 181 | std::variant<bool>(on)); |
| 182 | } |
| 183 | |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 184 | void createInventoryAssoc( |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 185 | const std::shared_ptr<sdbusplus::asio::connection>& conn, |
| 186 | const std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 187 | const std::string& path); |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 188 | |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 189 | struct GetSensorConfiguration : |
| 190 | std::enable_shared_from_this<GetSensorConfiguration> |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 191 | { |
| 192 | GetSensorConfiguration( |
| 193 | std::shared_ptr<sdbusplus::asio::connection> connection, |
| 194 | std::function<void(ManagedObjectType& resp)>&& callbackFunc) : |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 195 | dbusConnection(std::move(connection)), |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 196 | callback(std::move(callbackFunc)) |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 197 | {} |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 198 | |
| 199 | void getPath(const std::string& path, const std::string& interface, |
| 200 | const std::string& owner, size_t retries = 5) |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 201 | { |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 202 | if (retries > 5) |
| 203 | { |
| 204 | retries = 5; |
| 205 | } |
| 206 | std::shared_ptr<GetSensorConfiguration> self = shared_from_this(); |
| 207 | |
| 208 | self->dbusConnection->async_method_call( |
| 209 | [self, path, interface, owner, |
| 210 | retries](const boost::system::error_code ec, |
| 211 | boost::container::flat_map<std::string, BasicVariantType>& |
| 212 | data) { |
| 213 | if (ec) |
| 214 | { |
| 215 | std::cerr << "Error getting " << path << ": retries left" |
| 216 | << retries - 1 << "\n"; |
| 217 | if (!retries) |
| 218 | { |
| 219 | return; |
| 220 | } |
| 221 | auto timer = std::make_shared<boost::asio::steady_timer>( |
| 222 | self->dbusConnection->get_io_context()); |
| 223 | timer->expires_after(std::chrono::seconds(10)); |
| 224 | timer->async_wait([self, timer, path, interface, owner, |
| 225 | retries](boost::system::error_code ec) { |
| 226 | if (ec) |
| 227 | { |
| 228 | std::cerr << "Timer error!\n"; |
| 229 | return; |
| 230 | } |
| 231 | self->getPath(path, interface, owner, retries - 1); |
| 232 | }); |
| 233 | return; |
| 234 | } |
| 235 | |
| 236 | self->respData[path][interface] = std::move(data); |
| 237 | }, |
| 238 | owner, path, "org.freedesktop.DBus.Properties", "GetAll", |
| 239 | interface); |
| 240 | } |
| 241 | |
| 242 | void getConfiguration(const std::vector<std::string>& interfaces, |
| 243 | size_t retries = 0) |
| 244 | { |
| 245 | if (retries > 5) |
| 246 | { |
| 247 | retries = 5; |
| 248 | } |
| 249 | |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 250 | std::shared_ptr<GetSensorConfiguration> self = shared_from_this(); |
| 251 | dbusConnection->async_method_call( |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 252 | [self, interfaces, retries](const boost::system::error_code ec, |
| 253 | const GetSubTreeType& ret) { |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 254 | if (ec) |
| 255 | { |
| 256 | std::cerr << "Error calling mapper\n"; |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 257 | if (!retries) |
| 258 | { |
| 259 | return; |
| 260 | } |
| 261 | auto timer = std::make_shared<boost::asio::steady_timer>( |
| 262 | self->dbusConnection->get_io_context()); |
| 263 | timer->expires_after(std::chrono::seconds(10)); |
| 264 | timer->async_wait([self, timer, interfaces, |
| 265 | retries](boost::system::error_code ec) { |
| 266 | if (ec) |
| 267 | { |
| 268 | std::cerr << "Timer error!\n"; |
| 269 | return; |
| 270 | } |
| 271 | self->getConfiguration(interfaces, retries - 1); |
| 272 | }); |
| 273 | |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 274 | return; |
| 275 | } |
| 276 | for (const auto& [path, objDict] : ret) |
| 277 | { |
| 278 | if (objDict.empty()) |
| 279 | { |
| 280 | return; |
| 281 | } |
| 282 | const std::string& owner = objDict.begin()->first; |
| 283 | |
| 284 | for (const std::string& interface : objDict.begin()->second) |
| 285 | { |
| 286 | // anything that starts with a requested configuration |
| 287 | // is good |
| 288 | if (std::find_if( |
| 289 | interfaces.begin(), interfaces.end(), |
| 290 | [interface](const std::string& possible) { |
| 291 | return boost::starts_with(interface, |
| 292 | possible); |
| 293 | }) == interfaces.end()) |
| 294 | { |
| 295 | continue; |
| 296 | } |
James Feist | f27a55c | 2020-08-04 14:27:30 -0700 | [diff] [blame] | 297 | self->getPath(path, interface, owner); |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 298 | } |
| 299 | } |
| 300 | }, |
| 301 | mapper::busName, mapper::path, mapper::interface, mapper::subtree, |
| 302 | "/", 0, interfaces); |
| 303 | } |
| 304 | |
| 305 | ~GetSensorConfiguration() |
| 306 | { |
| 307 | callback(respData); |
| 308 | } |
| 309 | |
| 310 | std::shared_ptr<sdbusplus::asio::connection> dbusConnection; |
| 311 | std::function<void(ManagedObjectType& resp)> callback; |
| 312 | ManagedObjectType respData; |
| 313 | }; |
Zbigniew Kurzynski | 63f3866 | 2020-06-09 13:02:11 +0200 | [diff] [blame] | 314 | |
| 315 | // The common scheme for sysfs files naming is: <type><number>_<item>. |
| 316 | // This function returns optionally these 3 elements as a tuple. |
| 317 | std::optional<std::tuple<std::string, std::string, std::string>> |
| 318 | splitFileName(const std::filesystem::path& filePath); |
| 319 | std::optional<double> readFile(const std::string& thresholdFile, |
James Feist | 8086aba | 2020-08-25 16:00:59 -0700 | [diff] [blame] | 320 | const double& scaleFactor); |
Bruce Lee | 1263c3d | 2021-06-04 15:16:33 +0800 | [diff] [blame] | 321 | void setupManufacturingModeMatch(sdbusplus::asio::connection& conn); |
| 322 | bool getManufacturingMode(); |