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> |
| 5 | #include <experimental/filesystem> |
| 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 | |
Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 33 | bool findFiles(const std::experimental::filesystem::path dirPath, |
| 34 | const std::string& matchString, |
| 35 | std::vector<std::experimental::filesystem::path>& foundPaths, |
| 36 | unsigned int symlinkDepth = 1); |
James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 37 | bool isPowerOn(const std::shared_ptr<sdbusplus::asio::connection>& conn); |
| 38 | bool getSensorConfiguration( |
| 39 | const std::string& type, |
| 40 | const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, |
James Feist | 87d713a | 2018-12-06 16:06:24 -0800 | [diff] [blame] | 41 | ManagedObjectType& resp, bool useCache = false); |
| 42 | |
| 43 | // replaces limits if MinReading and MaxReading are found. |
| 44 | void findLimits(std::pair<double, double>& limits, |
James Feist | 40a7214 | 2018-12-21 10:09:53 -0800 | [diff] [blame^] | 45 | const SensorBaseConfiguration* data); |
| 46 | |
| 47 | template <typename T> |
| 48 | inline T loadVariant( |
| 49 | const boost::container::flat_map<std::string, BasicVariantType>& data, |
| 50 | const std::string& key) |
| 51 | { |
| 52 | auto it = data.find(key); |
| 53 | if (it == data.end()) |
| 54 | { |
| 55 | std::cerr << "Configuration missing " << key << "\n"; |
| 56 | throw std::invalid_argument("Key Missing"); |
| 57 | } |
| 58 | if constexpr (std::is_same_v<T, double>) |
| 59 | { |
| 60 | return sdbusplus::message::variant_ns::visit(VariantToDoubleVisitor(), |
| 61 | it->second); |
| 62 | } |
| 63 | else if constexpr (std::is_same_v<T, std::string>) |
| 64 | { |
| 65 | return sdbusplus::message::variant_ns::visit(VariantToStringVisitor(), |
| 66 | it->second); |
| 67 | } |
| 68 | else |
| 69 | { |
| 70 | static_assert("Type Not Implemented"); |
| 71 | } |
| 72 | } |