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