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