blob: 4fdbac716c50e2b04dc21517ea2d936f6688e66f [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 Feistd8bd5622019-06-26 12:09:05 -070040using Association = std::tuple<std::string, std::string, std::string>;
41
James Feistcf3bce62019-01-08 10:07:19 -080042bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070043 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080044 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070045 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080046bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080047bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080048void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070049bool getSensorConfiguration(
50 const std::string& type,
51 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080052 ManagedObjectType& resp, bool useCache = false);
53
James Feist82bac4c2019-03-11 11:16:53 -070054void createAssociation(
55 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
56 const std::string& path);
57
James Feist87d713a2018-12-06 16:06:24 -080058// replaces limits if MinReading and MaxReading are found.
59void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080060 const SensorBaseConfiguration* data);
61
James Feistfc94b212019-02-06 16:14:51 -080062enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080063{
64 on,
James Feistfc94b212019-02-06 16:14:51 -080065 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080066 always
67};
68
James Feista5e58722019-04-22 14:43:11 -070069namespace mapper
70{
71constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
72constexpr const char* path = "/xyz/openbmc_project/object_mapper";
73constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
74constexpr const char* subtree = "GetSubTree";
75} // namespace mapper
76
77namespace properties
78{
79constexpr const char* interface = "org.freedesktop.DBus.Properties";
80constexpr const char* get = "Get";
81} // namespace properties
82
James Feist52497fd2019-06-07 13:01:33 -070083namespace power
84{
85const static constexpr char* busname = "xyz.openbmc_project.State.Host";
86const static constexpr char* interface = "xyz.openbmc_project.State.Host";
87const static constexpr char* path = "/xyz/openbmc_project/state/host0";
88const static constexpr char* property = "CurrentHostState";
89} // namespace power
90namespace post
91{
92const static constexpr char* busname =
93 "xyz.openbmc_project.State.OperatingSystem";
94const static constexpr char* interface =
95 "xyz.openbmc_project.State.OperatingSystem.Status";
96const static constexpr char* path = "/xyz/openbmc_project/state/os";
97const static constexpr char* property = "OperatingSystemState";
98} // namespace post
99
James Feist40a72142018-12-21 10:09:53 -0800100template <typename T>
101inline T loadVariant(
102 const boost::container::flat_map<std::string, BasicVariantType>& data,
103 const std::string& key)
104{
105 auto it = data.find(key);
106 if (it == data.end())
107 {
108 std::cerr << "Configuration missing " << key << "\n";
109 throw std::invalid_argument("Key Missing");
110 }
111 if constexpr (std::is_same_v<T, double>)
112 {
James Feist3eb82622019-02-08 13:10:22 -0800113 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800114 }
James Feist6ef20402019-01-07 16:45:08 -0800115 else if constexpr (std::is_unsigned_v<T>)
116 {
James Feist3eb82622019-02-08 13:10:22 -0800117 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800118 }
James Feist40a72142018-12-21 10:09:53 -0800119 else if constexpr (std::is_same_v<T, std::string>)
120 {
James Feist3eb82622019-02-08 13:10:22 -0800121 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800122 }
123 else
124 {
James Feist52497fd2019-06-07 13:01:33 -0700125 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800126 }
127}
James Feistfc94b212019-02-06 16:14:51 -0800128
129inline void setReadState(const std::string& str, PowerState& val)
130{
131
132 if (str == "On")
133 {
134 val = PowerState::on;
135 }
136 else if (str == "BiosPost")
137 {
138 val = PowerState::biosPost;
139 }
140 else if (str == "Always")
141 {
142 val = PowerState::always;
143 }
144}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800145
146void createInventoryAssoc(
147 std::shared_ptr<sdbusplus::asio::connection> conn,
148 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
149 const std::string& path);