blob: d65fb83fa89d04560e658a3d8d077f1265a54699 [file] [log] [blame]
James Feist6714a252018-09-10 15:26:18 -07001#pragma once
James Feist87d713a2018-12-06 16:06:24 -08002#include "VariantVisitors.hpp"
3
James Feist6714a252018-09-10 15:26:18 -07004#include <boost/container/flat_map.hpp>
James Feist24f02f22019-04-15 11:05:39 -07005#include <filesystem>
James Feist6714a252018-09-10 15:26:18 -07006#include <iostream>
7#include <regex>
8#include <sdbusplus/asio/connection.hpp>
James Feist82bac4c2019-03-11 11:16:53 -07009#include <sdbusplus/asio/object_server.hpp>
James Feist6714a252018-09-10 15:26:18 -070010#include <sdbusplus/message/types.hpp>
11
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070012const constexpr char* jsonStore = "/var/configuration/flattened.json";
13const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
14const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
15const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070016
17using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080018 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 Feist6714a252018-09-10 15:26:18 -070020
21using 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>>>;
26using SensorData = boost::container::flat_map<
27 std::string, boost::container::flat_map<std::string, BasicVariantType>>;
28
James Feista5e58722019-04-22 14:43:11 -070029using GetSubTreeType = std::vector<
30 std::pair<std::string,
31 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feist87d713a2018-12-06 16:06:24 -080032using SensorBaseConfiguration =
33 std::pair<std::string,
34 boost::container::flat_map<std::string, BasicVariantType>>;
35
James Feistcf3bce62019-01-08 10:07:19 -080036bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070037 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080038 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070039 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080040bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080041bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080042void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070043bool getSensorConfiguration(
44 const std::string& type,
45 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080046 ManagedObjectType& resp, bool useCache = false);
47
James Feist82bac4c2019-03-11 11:16:53 -070048void createAssociation(
49 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
50 const std::string& path);
51
James Feist87d713a2018-12-06 16:06:24 -080052// replaces limits if MinReading and MaxReading are found.
53void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080054 const SensorBaseConfiguration* data);
55
James Feistfc94b212019-02-06 16:14:51 -080056enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080057{
58 on,
James Feistfc94b212019-02-06 16:14:51 -080059 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080060 always
61};
62
James Feista5e58722019-04-22 14:43:11 -070063namespace mapper
64{
65constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
66constexpr const char* path = "/xyz/openbmc_project/object_mapper";
67constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
68constexpr const char* subtree = "GetSubTree";
69} // namespace mapper
70
71namespace properties
72{
73constexpr const char* interface = "org.freedesktop.DBus.Properties";
74constexpr const char* get = "Get";
75} // namespace properties
76
James Feist40a72142018-12-21 10:09:53 -080077template <typename T>
78inline 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 Feist3eb82622019-02-08 13:10:22 -080090 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -080091 }
James Feist6ef20402019-01-07 16:45:08 -080092 else if constexpr (std::is_unsigned_v<T>)
93 {
James Feist3eb82622019-02-08 13:10:22 -080094 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -080095 }
James Feist40a72142018-12-21 10:09:53 -080096 else if constexpr (std::is_same_v<T, std::string>)
97 {
James Feist3eb82622019-02-08 13:10:22 -080098 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -080099 }
100 else
101 {
102 static_assert("Type Not Implemented");
103 }
104}
James Feistfc94b212019-02-06 16:14:51 -0800105
106inline 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}