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 | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 40 | bool findFiles(const std::filesystem::path dirPath, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 41 | const std::string& matchString, |
James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 42 | std::vector<std::filesystem::path>& foundPaths, |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 43 | unsigned int symlinkDepth = 1); |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 44 | bool isPowerOn(void); |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 45 | bool hasBiosPost(void); |
James Feist | 71d31b2 | 2019-01-02 16:57:54 -0800 | [diff] [blame] | 46 | void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 47 | bool getSensorConfiguration( |
| 48 | const std::string& type, |
| 49 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 50 | ManagedObjectType& resp, bool useCache = false); |
| 51 | |
James Feist | 82bac4c | 2019-03-11 11:16:53 -0700 | [diff] [blame] | 52 | void createAssociation( |
| 53 | std::shared_ptr<sdbusplus::asio::dbus_interface>& association, |
| 54 | const std::string& path); |
| 55 | |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 56 | // replaces limits if MinReading and MaxReading are found. |
| 57 | void findLimits(std::pair<double, double>& limits, |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 58 | const SensorBaseConfiguration* data); |
| 59 | |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 60 | enum class PowerState |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 61 | { |
| 62 | on, |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 63 | biosPost, |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 64 | always |
| 65 | }; |
| 66 | |
James Feist | a5e5872 | 2019-04-22 14:43:11 -0700 | [diff] [blame] | 67 | namespace mapper |
| 68 | { |
| 69 | constexpr const char* busName = "xyz.openbmc_project.ObjectMapper"; |
| 70 | constexpr const char* path = "/xyz/openbmc_project/object_mapper"; |
| 71 | constexpr const char* interface = "xyz.openbmc_project.ObjectMapper"; |
| 72 | constexpr const char* subtree = "GetSubTree"; |
| 73 | } // namespace mapper |
| 74 | |
| 75 | namespace properties |
| 76 | { |
| 77 | constexpr const char* interface = "org.freedesktop.DBus.Properties"; |
| 78 | constexpr const char* get = "Get"; |
| 79 | } // namespace properties |
| 80 | |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame^] | 81 | namespace power |
| 82 | { |
| 83 | const static constexpr char* busname = "xyz.openbmc_project.State.Host"; |
| 84 | const static constexpr char* interface = "xyz.openbmc_project.State.Host"; |
| 85 | const static constexpr char* path = "/xyz/openbmc_project/state/host0"; |
| 86 | const static constexpr char* property = "CurrentHostState"; |
| 87 | } // namespace power |
| 88 | namespace post |
| 89 | { |
| 90 | const static constexpr char* busname = |
| 91 | "xyz.openbmc_project.State.OperatingSystem"; |
| 92 | const static constexpr char* interface = |
| 93 | "xyz.openbmc_project.State.OperatingSystem.Status"; |
| 94 | const static constexpr char* path = "/xyz/openbmc_project/state/os"; |
| 95 | const static constexpr char* property = "OperatingSystemState"; |
| 96 | } // namespace post |
| 97 | |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 98 | template <typename T> |
| 99 | inline T loadVariant( |
| 100 | const boost::container::flat_map<std::string, BasicVariantType>& data, |
| 101 | const std::string& key) |
| 102 | { |
| 103 | auto it = data.find(key); |
| 104 | if (it == data.end()) |
| 105 | { |
| 106 | std::cerr << "Configuration missing " << key << "\n"; |
| 107 | throw std::invalid_argument("Key Missing"); |
| 108 | } |
| 109 | if constexpr (std::is_same_v<T, double>) |
| 110 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 111 | return std::visit(VariantToDoubleVisitor(), it->second); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 112 | } |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 113 | else if constexpr (std::is_unsigned_v<T>) |
| 114 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 115 | return std::visit(VariantToUnsignedIntVisitor(), it->second); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 116 | } |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 117 | else if constexpr (std::is_same_v<T, std::string>) |
| 118 | { |
James Feist | 3eb8262 | 2019-02-08 13:10:22 -0800 | [diff] [blame] | 119 | return std::visit(VariantToStringVisitor(), it->second); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 120 | } |
| 121 | else |
| 122 | { |
James Feist | 52497fd | 2019-06-07 13:01:33 -0700 | [diff] [blame^] | 123 | static_assert(!std::is_same_v<T, T>, "Type Not Implemented"); |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame] | 124 | } |
| 125 | } |
James Feist | fc94b21 | 2019-02-06 16:14:51 -0800 | [diff] [blame] | 126 | |
| 127 | inline void setReadState(const std::string& str, PowerState& val) |
| 128 | { |
| 129 | |
| 130 | if (str == "On") |
| 131 | { |
| 132 | val = PowerState::on; |
| 133 | } |
| 134 | else if (str == "BiosPost") |
| 135 | { |
| 136 | val = PowerState::biosPost; |
| 137 | } |
| 138 | else if (str == "Always") |
| 139 | { |
| 140 | val = PowerState::always; |
| 141 | } |
| 142 | } |