James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | #pragma once |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 2 | #include "VariantVisitors.hpp" |
| 3 | |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 4 | #include <boost/algorithm/string/predicate.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 5 | #include <boost/container/flat_map.hpp> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 6 | #include <filesystem> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame^] | 7 | #include <functional> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 8 | #include <iostream> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame^] | 9 | #include <memory> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 10 | #include <regex> |
| 11 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 12 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 13 | #include <sdbusplus/message/types.hpp> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame^] | 14 | #include <string> |
| 15 | #include <tuple> |
| 16 | #include <utility> |
| 17 | #include <variant> |
| 18 | #include <vector> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 20 | constexpr const char* gpioPath = "/sys/class/gpio/"; |
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>; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 32 | |
| 33 | using ManagedObjectType = boost::container::flat_map< |
| 34 | sdbusplus::message::object_path, |
| 35 | boost::container::flat_map< |
| 36 | std::string, |
| 37 | boost::container::flat_map<std::string, BasicVariantType>>>; |
| 38 | using SensorData = boost::container::flat_map< |
| 39 | std::string, boost::container::flat_map<std::string, BasicVariantType>>; |
| 40 | |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 41 | using GetSubTreeType = std::vector< |
| 42 | std::pair<std::string, |
| 43 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 44 | using SensorBaseConfiguration = |
| 45 | std::pair<std::string, |
| 46 | boost::container::flat_map<std::string, BasicVariantType>>; |
| 47 | |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 48 | using Association = std::tuple<std::string, std::string, std::string>; |
| 49 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [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, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 53 | unsigned 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, |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 60 | ManagedObjectType& resp, bool useCache = false); |
| 61 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 62 | void createAssociation( |
| 63 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
| 64 | const std::string& path); |
| 65 | |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 66 | // replaces limits if MinReading and MaxReading are found. |
| 67 | void findLimits(std::pair<double, double>& limits, |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 68 | const SensorBaseConfiguration* data); |
| 69 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 70 | enum class PowerState |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 71 | { |
| 72 | on, |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 73 | biosPost, |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 74 | always |
| 75 | }; |
| 76 | |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 77 | namespace mapper |
| 78 | { |
| 79 | constexpr const char* busName = "xyz.openbmc_project.ObjectMapper"; |
| 80 | constexpr const char* path = "/xyz/openbmc_project/object_mapper"; |
| 81 | constexpr const char* interface = "xyz.openbmc_project.ObjectMapper"; |
| 82 | constexpr const char* subtree = "GetSubTree"; |
| 83 | } // namespace mapper |
| 84 | |
| 85 | namespace properties |
| 86 | { |
| 87 | constexpr const char* interface = "org.freedesktop.DBus.Properties"; |
| 88 | constexpr const char* get = "Get"; |
| 89 | } // namespace properties |
| 90 | |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 91 | namespace power |
| 92 | { |
| 93 | const static constexpr char* busname = "xyz.openbmc_project.State.Host"; |
| 94 | const static constexpr char* interface = "xyz.openbmc_project.State.Host"; |
| 95 | const static constexpr char* path = "/xyz/openbmc_project/state/host0"; |
| 96 | const static constexpr char* property = "CurrentHostState"; |
| 97 | } // namespace power |
| 98 | namespace post |
| 99 | { |
| 100 | const static constexpr char* busname = |
| 101 | "xyz.openbmc_project.State.OperatingSystem"; |
| 102 | const static constexpr char* interface = |
| 103 | "xyz.openbmc_project.State.OperatingSystem.Status"; |
| 104 | const static constexpr char* path = "/xyz/openbmc_project/state/os"; |
| 105 | const static constexpr char* property = "OperatingSystemState"; |
| 106 | } // namespace post |
| 107 | |
James Feist | 2adc95c | 2019-09-30 14:55:28 -0700 | [diff] [blame] | 108 | namespace association |
| 109 | { |
| 110 | const static constexpr char* interface = |
| 111 | "xyz.openbmc_project.Association.Definitions"; |
| 112 | } // namespace association |
| 113 | |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 114 | template <typename T> |
| 115 | inline T loadVariant( |
| 116 | const boost::container::flat_map<std::string, BasicVariantType>& data, |
| 117 | const std::string& key) |
| 118 | { |
| 119 | auto it = data.find(key); |
| 120 | if (it == data.end()) |
| 121 | { |
| 122 | std::cerr << "Configuration missing " << key << "\n"; |
| 123 | throw std::invalid_argument("Key Missing"); |
| 124 | } |
| 125 | if constexpr (std::is_same_v<T, double>) |
| 126 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 127 | return std::visit(VariantToDoubleVisitor(), it->second); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 128 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 129 | else if constexpr (std::is_unsigned_v<T>) |
| 130 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 131 | return std::visit(VariantToUnsignedIntVisitor(), it->second); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 132 | } |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 133 | else if constexpr (std::is_same_v<T, std::string>) |
| 134 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 135 | return std::visit(VariantToStringVisitor(), it->second); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 136 | } |
| 137 | else |
| 138 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 139 | static_assert(!std::is_same_v<T, T>, "Type Not Implemented"); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 140 | } |
| 141 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 142 | |
| 143 | inline void setReadState(const std::string& str, PowerState& val) |
| 144 | { |
| 145 | |
| 146 | if (str == "On") |
| 147 | { |
| 148 | val = PowerState::on; |
| 149 | } |
| 150 | else if (str == "BiosPost") |
| 151 | { |
| 152 | val = PowerState::biosPost; |
| 153 | } |
| 154 | else if (str == "Always") |
| 155 | { |
| 156 | val = PowerState::always; |
| 157 | } |
| 158 | } |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame] | 159 | |
| 160 | void createInventoryAssoc( |
| 161 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 162 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, |
| 163 | const std::string& path); |
James Feist | c71c119 | 2019-09-18 14:31:33 -0700 | [diff] [blame] | 164 | |
| 165 | struct GetSensorConfiguration |
| 166 | : std::enable_shared_from_this<GetSensorConfiguration> |
| 167 | { |
| 168 | GetSensorConfiguration( |
| 169 | std::shared_ptr<sdbusplus::asio::connection> connection, |
| 170 | std::function<void(ManagedObjectType& resp)>&& callbackFunc) : |
| 171 | dbusConnection(connection), |
| 172 | callback(std::move(callbackFunc)) |
| 173 | { |
| 174 | } |
| 175 | void getConfiguration(const std::vector<std::string>& interfaces) |
| 176 | { |
| 177 | std::shared_ptr<GetSensorConfiguration> self = shared_from_this(); |
| 178 | dbusConnection->async_method_call( |
| 179 | [self, interfaces](const boost::system::error_code ec, |
| 180 | const GetSubTreeType& ret) { |
| 181 | if (ec) |
| 182 | { |
| 183 | std::cerr << "Error calling mapper\n"; |
| 184 | return; |
| 185 | } |
| 186 | for (const auto& [path, objDict] : ret) |
| 187 | { |
| 188 | if (objDict.empty()) |
| 189 | { |
| 190 | return; |
| 191 | } |
| 192 | const std::string& owner = objDict.begin()->first; |
| 193 | |
| 194 | for (const std::string& interface : objDict.begin()->second) |
| 195 | { |
| 196 | // anything that starts with a requested configuration |
| 197 | // is good |
| 198 | if (std::find_if( |
| 199 | interfaces.begin(), interfaces.end(), |
| 200 | [interface](const std::string& possible) { |
| 201 | return boost::starts_with(interface, |
| 202 | possible); |
| 203 | }) == interfaces.end()) |
| 204 | { |
| 205 | continue; |
| 206 | } |
| 207 | |
| 208 | self->dbusConnection->async_method_call( |
| 209 | [self, path, interface]( |
| 210 | const boost::system::error_code ec, |
| 211 | boost::container::flat_map< |
| 212 | std::string, BasicVariantType>& data) { |
| 213 | if (ec) |
| 214 | { |
| 215 | std::cerr << "Error getting " << path |
| 216 | << "\n"; |
| 217 | return; |
| 218 | } |
| 219 | self->respData[path][interface] = |
| 220 | std::move(data); |
| 221 | }, |
| 222 | owner, path, "org.freedesktop.DBus.Properties", |
| 223 | "GetAll", interface); |
| 224 | } |
| 225 | } |
| 226 | }, |
| 227 | mapper::busName, mapper::path, mapper::interface, mapper::subtree, |
| 228 | "/", 0, interfaces); |
| 229 | } |
| 230 | |
| 231 | ~GetSensorConfiguration() |
| 232 | { |
| 233 | callback(respData); |
| 234 | } |
| 235 | |
| 236 | std::shared_ptr<sdbusplus::asio::connection> dbusConnection; |
| 237 | std::function<void(ManagedObjectType& resp)> callback; |
| 238 | ManagedObjectType respData; |
| 239 | }; |