blob: f3abc7f4aed0500cb681f8878499a20d56a40277 [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
James Feist58295ad2019-05-30 15:01:41 -070012constexpr const char* gpioPath = "/sys/class/gpio/";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070013const constexpr char* jsonStore = "/var/configuration/flattened.json";
14const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
15const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070016
17constexpr const char* cpuInventoryPath =
18 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070019const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070020
21using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080022 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
23 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
James Feist6714a252018-09-10 15:26:18 -070024
25using ManagedObjectType = boost::container::flat_map<
26 sdbusplus::message::object_path,
27 boost::container::flat_map<
28 std::string,
29 boost::container::flat_map<std::string, BasicVariantType>>>;
30using SensorData = boost::container::flat_map<
31 std::string, boost::container::flat_map<std::string, BasicVariantType>>;
32
James Feista5e58722019-04-22 14:43:11 -070033using GetSubTreeType = std::vector<
34 std::pair<std::string,
35 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feist87d713a2018-12-06 16:06:24 -080036using SensorBaseConfiguration =
37 std::pair<std::string,
38 boost::container::flat_map<std::string, BasicVariantType>>;
39
James Feistcf3bce62019-01-08 10:07:19 -080040bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070041 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080042 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070043 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080044bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080045bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080046void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070047bool getSensorConfiguration(
48 const std::string& type,
49 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080050 ManagedObjectType& resp, bool useCache = false);
51
James Feist82bac4c2019-03-11 11:16:53 -070052void createAssociation(
53 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
54 const std::string& path);
55
James Feist87d713a2018-12-06 16:06:24 -080056// replaces limits if MinReading and MaxReading are found.
57void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080058 const SensorBaseConfiguration* data);
59
James Feistfc94b212019-02-06 16:14:51 -080060enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080061{
62 on,
James Feistfc94b212019-02-06 16:14:51 -080063 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080064 always
65};
66
James Feista5e58722019-04-22 14:43:11 -070067namespace mapper
68{
69constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
70constexpr const char* path = "/xyz/openbmc_project/object_mapper";
71constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
72constexpr const char* subtree = "GetSubTree";
73} // namespace mapper
74
75namespace properties
76{
77constexpr const char* interface = "org.freedesktop.DBus.Properties";
78constexpr const char* get = "Get";
79} // namespace properties
80
James Feist40a72142018-12-21 10:09:53 -080081template <typename T>
82inline T loadVariant(
83 const boost::container::flat_map<std::string, BasicVariantType>& data,
84 const std::string& key)
85{
86 auto it = data.find(key);
87 if (it == data.end())
88 {
89 std::cerr << "Configuration missing " << key << "\n";
90 throw std::invalid_argument("Key Missing");
91 }
92 if constexpr (std::is_same_v<T, double>)
93 {
James Feist3eb82622019-02-08 13:10:22 -080094 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -080095 }
James Feist6ef20402019-01-07 16:45:08 -080096 else if constexpr (std::is_unsigned_v<T>)
97 {
James Feist3eb82622019-02-08 13:10:22 -080098 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -080099 }
James Feist40a72142018-12-21 10:09:53 -0800100 else if constexpr (std::is_same_v<T, std::string>)
101 {
James Feist3eb82622019-02-08 13:10:22 -0800102 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800103 }
104 else
105 {
106 static_assert("Type Not Implemented");
107 }
108}
James Feistfc94b212019-02-06 16:14:51 -0800109
110inline void setReadState(const std::string& str, PowerState& val)
111{
112
113 if (str == "On")
114 {
115 val = PowerState::on;
116 }
117 else if (str == "BiosPost")
118 {
119 val = PowerState::biosPost;
120 }
121 else if (str == "Always")
122 {
123 val = PowerState::always;
124 }
125}