blob: 4f595c9dbac774c597df7561e780ad19c94d2b5a [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 Feist52497fd2019-06-07 13:01:33 -070081namespace power
82{
83const static constexpr char* busname = "xyz.openbmc_project.State.Host";
84const static constexpr char* interface = "xyz.openbmc_project.State.Host";
85const static constexpr char* path = "/xyz/openbmc_project/state/host0";
86const static constexpr char* property = "CurrentHostState";
87} // namespace power
88namespace post
89{
90const static constexpr char* busname =
91 "xyz.openbmc_project.State.OperatingSystem";
92const static constexpr char* interface =
93 "xyz.openbmc_project.State.OperatingSystem.Status";
94const static constexpr char* path = "/xyz/openbmc_project/state/os";
95const static constexpr char* property = "OperatingSystemState";
96} // namespace post
97
James Feist40a72142018-12-21 10:09:53 -080098template <typename T>
99inline T loadVariant(
100 const boost::container::flat_map<std::string, BasicVariantType>& data,
101 const std::string& key)
102{
103 auto it = data.find(key);
104 if (it == data.end())
105 {
106 std::cerr << "Configuration missing " << key << "\n";
107 throw std::invalid_argument("Key Missing");
108 }
109 if constexpr (std::is_same_v<T, double>)
110 {
James Feist3eb82622019-02-08 13:10:22 -0800111 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800112 }
James Feist6ef20402019-01-07 16:45:08 -0800113 else if constexpr (std::is_unsigned_v<T>)
114 {
James Feist3eb82622019-02-08 13:10:22 -0800115 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800116 }
James Feist40a72142018-12-21 10:09:53 -0800117 else if constexpr (std::is_same_v<T, std::string>)
118 {
James Feist3eb82622019-02-08 13:10:22 -0800119 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800120 }
121 else
122 {
James Feist52497fd2019-06-07 13:01:33 -0700123 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800124 }
125}
James Feistfc94b212019-02-06 16:14:51 -0800126
127inline void setReadState(const std::string& str, PowerState& val)
128{
129
130 if (str == "On")
131 {
132 val = PowerState::on;
133 }
134 else if (str == "BiosPost")
135 {
136 val = PowerState::biosPost;
137 }
138 else if (str == "Always")
139 {
140 val = PowerState::always;
141 }
142}