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 | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 4 | #include <boost/container/flat_map.hpp> |
James Feist | 24f02f2 | 2019-04-15 11:05:39 -0700 | [diff] [blame] | 5 | #include <filesystem> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 6 | #include <iostream> |
| 7 | #include <regex> |
| 8 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 9 | #include <sdbusplus/asio/object_server.hpp> |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 10 | #include <sdbusplus/message/types.hpp> |
| 11 | |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 12 | constexpr const char* gpioPath = "/sys/class/gpio/"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 13 | const constexpr char* jsonStore = "/var/configuration/flattened.json"; |
| 14 | const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory"; |
| 15 | const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager"; |
James Feist | 58295ad | 2019-05-30 15:01:41 -0700 | [diff] [blame] | 16 | |
| 17 | constexpr const char* cpuInventoryPath = |
| 18 | "/xyz/openbmc_project/inventory/system/chassis/motherboard"; |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 19 | const std::regex illegalDbusRegex("[^A-Za-z0-9_]"); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 | |
| 21 | using BasicVariantType = |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 22 | std::variant<std::vector<std::string>, std::string, int64_t, uint64_t, |
| 23 | double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>; |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 24 | |
| 25 | using ManagedObjectType = boost::container::flat_map< |
| 26 | sdbusplus::message::object_path, |
| 27 | boost::container::flat_map< |
| 28 | std::string, |
| 29 | boost::container::flat_map<std::string, BasicVariantType>>>; |
| 30 | using SensorData = boost::container::flat_map< |
| 31 | std::string, boost::container::flat_map<std::string, BasicVariantType>>; |
| 32 | |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 33 | using GetSubTreeType = std::vector< |
| 34 | std::pair<std::string, |
| 35 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 36 | using SensorBaseConfiguration = |
| 37 | std::pair<std::string, |
| 38 | boost::container::flat_map<std::string, BasicVariantType>>; |
| 39 | |
James Feist | d8bd562 | 2019-06-26 12:09:05 -0700 | [diff] [blame] | 40 | using Association = std::tuple<std::string, std::string, std::string>; |
| 41 | |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 42 | bool findFiles(const std::filesystem::path dirPath, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 43 | const std::string& matchString, |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 44 | std::vector<std::filesystem::path>& foundPaths, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 45 | unsigned int symlinkDepth = 1); |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 46 | bool isPowerOn(void); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 47 | bool hasBiosPost(void); |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 48 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 49 | bool getSensorConfiguration( |
| 50 | const std::string& type, |
| 51 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 52 | ManagedObjectType& resp, bool useCache = false); |
| 53 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 54 | void createAssociation( |
| 55 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
| 56 | const std::string& path); |
| 57 | |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 58 | // replaces limits if MinReading and MaxReading are found. |
| 59 | void findLimits(std::pair<double, double>& limits, |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 60 | const SensorBaseConfiguration* data); |
| 61 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 62 | enum class PowerState |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 63 | { |
| 64 | on, |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 65 | biosPost, |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 66 | always |
| 67 | }; |
| 68 | |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 69 | namespace mapper |
| 70 | { |
| 71 | constexpr const char* busName = "xyz.openbmc_project.ObjectMapper"; |
| 72 | constexpr const char* path = "/xyz/openbmc_project/object_mapper"; |
| 73 | constexpr const char* interface = "xyz.openbmc_project.ObjectMapper"; |
| 74 | constexpr const char* subtree = "GetSubTree"; |
| 75 | } // namespace mapper |
| 76 | |
| 77 | namespace properties |
| 78 | { |
| 79 | constexpr const char* interface = "org.freedesktop.DBus.Properties"; |
| 80 | constexpr const char* get = "Get"; |
| 81 | } // namespace properties |
| 82 | |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 83 | namespace power |
| 84 | { |
| 85 | const static constexpr char* busname = "xyz.openbmc_project.State.Host"; |
| 86 | const static constexpr char* interface = "xyz.openbmc_project.State.Host"; |
| 87 | const static constexpr char* path = "/xyz/openbmc_project/state/host0"; |
| 88 | const static constexpr char* property = "CurrentHostState"; |
| 89 | } // namespace power |
| 90 | namespace post |
| 91 | { |
| 92 | const static constexpr char* busname = |
| 93 | "xyz.openbmc_project.State.OperatingSystem"; |
| 94 | const static constexpr char* interface = |
| 95 | "xyz.openbmc_project.State.OperatingSystem.Status"; |
| 96 | const static constexpr char* path = "/xyz/openbmc_project/state/os"; |
| 97 | const static constexpr char* property = "OperatingSystemState"; |
| 98 | } // namespace post |
| 99 | |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 100 | template <typename T> |
| 101 | inline T loadVariant( |
| 102 | const boost::container::flat_map<std::string, BasicVariantType>& data, |
| 103 | const std::string& key) |
| 104 | { |
| 105 | auto it = data.find(key); |
| 106 | if (it == data.end()) |
| 107 | { |
| 108 | std::cerr << "Configuration missing " << key << "\n"; |
| 109 | throw std::invalid_argument("Key Missing"); |
| 110 | } |
| 111 | if constexpr (std::is_same_v<T, double>) |
| 112 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 113 | return std::visit(VariantToDoubleVisitor(), it->second); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 114 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 115 | else if constexpr (std::is_unsigned_v<T>) |
| 116 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 117 | return std::visit(VariantToUnsignedIntVisitor(), it->second); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 118 | } |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 119 | else if constexpr (std::is_same_v<T, std::string>) |
| 120 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 121 | return std::visit(VariantToStringVisitor(), it->second); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 122 | } |
| 123 | else |
| 124 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame] | 125 | static_assert(!std::is_same_v<T, T>, "Type Not Implemented"); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 126 | } |
| 127 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 128 | |
| 129 | inline void setReadState(const std::string& str, PowerState& val) |
| 130 | { |
| 131 | |
| 132 | if (str == "On") |
| 133 | { |
| 134 | val = PowerState::on; |
| 135 | } |
| 136 | else if (str == "BiosPost") |
| 137 | { |
| 138 | val = PowerState::biosPost; |
| 139 | } |
| 140 | else if (str == "Always") |
| 141 | { |
| 142 | val = PowerState::always; |
| 143 | } |
| 144 | } |
Cheng C Yang | 5580f2f | 2019-09-19 09:01:47 +0800 | [diff] [blame^] | 145 | |
| 146 | void createInventoryAssoc( |
| 147 | std::shared_ptr<sdbusplus::asio::connection> conn, |
| 148 | std::shared_ptr<sdbusplus::asio::dbus_interface> association, |
| 149 | const std::string& path); |