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