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