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