blob: d97ff9cf468a1a429389138bcff3b80517677bac [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 Feistc71c1192019-09-18 14:31:33 -07004#include <boost/algorithm/string/predicate.hpp>
James Feist6714a252018-09-10 15:26:18 -07005#include <boost/container/flat_map.hpp>
James Feist24f02f22019-04-15 11:05:39 -07006#include <filesystem>
Patrick Venturefd6ba732019-10-31 14:27:39 -07007#include <functional>
James Feist6714a252018-09-10 15:26:18 -07008#include <iostream>
Patrick Venturefd6ba732019-10-31 14:27:39 -07009#include <memory>
James Feist6714a252018-09-10 15:26:18 -070010#include <regex>
11#include <sdbusplus/asio/connection.hpp>
James Feist82bac4c2019-03-11 11:16:53 -070012#include <sdbusplus/asio/object_server.hpp>
James Feist6714a252018-09-10 15:26:18 -070013#include <sdbusplus/message/types.hpp>
Patrick Venturefd6ba732019-10-31 14:27:39 -070014#include <string>
15#include <tuple>
16#include <utility>
17#include <variant>
18#include <vector>
James Feist6714a252018-09-10 15:26:18 -070019
James Feist58295ad2019-05-30 15:01:41 -070020constexpr const char* gpioPath = "/sys/class/gpio/";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070021const constexpr char* jsonStore = "/var/configuration/flattened.json";
22const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
23const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070024
25constexpr const char* cpuInventoryPath =
26 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070027const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070028
29using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080030 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
31 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080032using SensorBaseConfigMap =
33 boost::container::flat_map<std::string, BasicVariantType>;
34using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
35using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
36using ManagedObjectType =
37 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070038
James Feista5e58722019-04-22 14:43:11 -070039using GetSubTreeType = std::vector<
40 std::pair<std::string,
41 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070042using Association = std::tuple<std::string, std::string, std::string>;
43
James Feistcf3bce62019-01-08 10:07:19 -080044bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070045 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080046 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070047 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080048bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080049bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080050void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070051bool getSensorConfiguration(
52 const std::string& type,
53 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080054 ManagedObjectType& resp, bool useCache = false);
55
James Feist82bac4c2019-03-11 11:16:53 -070056void createAssociation(
57 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
58 const std::string& path);
59
James Feist87d713a2018-12-06 16:06:24 -080060// replaces limits if MinReading and MaxReading are found.
61void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080062 const SensorBaseConfiguration* data);
63
James Feistfc94b212019-02-06 16:14:51 -080064enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080065{
66 on,
James Feistfc94b212019-02-06 16:14:51 -080067 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080068 always
69};
70
James Feista5e58722019-04-22 14:43:11 -070071namespace mapper
72{
73constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
74constexpr const char* path = "/xyz/openbmc_project/object_mapper";
75constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
76constexpr const char* subtree = "GetSubTree";
77} // namespace mapper
78
79namespace properties
80{
81constexpr const char* interface = "org.freedesktop.DBus.Properties";
82constexpr const char* get = "Get";
83} // namespace properties
84
James Feist52497fd2019-06-07 13:01:33 -070085namespace power
86{
87const static constexpr char* busname = "xyz.openbmc_project.State.Host";
88const static constexpr char* interface = "xyz.openbmc_project.State.Host";
89const static constexpr char* path = "/xyz/openbmc_project/state/host0";
90const static constexpr char* property = "CurrentHostState";
91} // namespace power
92namespace post
93{
94const static constexpr char* busname =
95 "xyz.openbmc_project.State.OperatingSystem";
96const static constexpr char* interface =
97 "xyz.openbmc_project.State.OperatingSystem.Status";
98const static constexpr char* path = "/xyz/openbmc_project/state/os";
99const static constexpr char* property = "OperatingSystemState";
100} // namespace post
101
James Feist2adc95c2019-09-30 14:55:28 -0700102namespace association
103{
104const static constexpr char* interface =
105 "xyz.openbmc_project.Association.Definitions";
106} // namespace association
107
James Feist40a72142018-12-21 10:09:53 -0800108template <typename T>
109inline T loadVariant(
110 const boost::container::flat_map<std::string, BasicVariantType>& data,
111 const std::string& key)
112{
113 auto it = data.find(key);
114 if (it == data.end())
115 {
116 std::cerr << "Configuration missing " << key << "\n";
117 throw std::invalid_argument("Key Missing");
118 }
119 if constexpr (std::is_same_v<T, double>)
120 {
James Feist3eb82622019-02-08 13:10:22 -0800121 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800122 }
James Feist6ef20402019-01-07 16:45:08 -0800123 else if constexpr (std::is_unsigned_v<T>)
124 {
James Feist3eb82622019-02-08 13:10:22 -0800125 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800126 }
James Feist40a72142018-12-21 10:09:53 -0800127 else if constexpr (std::is_same_v<T, std::string>)
128 {
James Feist3eb82622019-02-08 13:10:22 -0800129 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800130 }
131 else
132 {
James Feist52497fd2019-06-07 13:01:33 -0700133 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800134 }
135}
James Feistfc94b212019-02-06 16:14:51 -0800136
137inline void setReadState(const std::string& str, PowerState& val)
138{
139
140 if (str == "On")
141 {
142 val = PowerState::on;
143 }
144 else if (str == "BiosPost")
145 {
146 val = PowerState::biosPost;
147 }
148 else if (str == "Always")
149 {
150 val = PowerState::always;
151 }
152}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800153
154void createInventoryAssoc(
155 std::shared_ptr<sdbusplus::asio::connection> conn,
156 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
157 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700158
159struct GetSensorConfiguration
160 : std::enable_shared_from_this<GetSensorConfiguration>
161{
162 GetSensorConfiguration(
163 std::shared_ptr<sdbusplus::asio::connection> connection,
164 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
165 dbusConnection(connection),
166 callback(std::move(callbackFunc))
167 {
168 }
169 void getConfiguration(const std::vector<std::string>& interfaces)
170 {
171 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
172 dbusConnection->async_method_call(
173 [self, interfaces](const boost::system::error_code ec,
174 const GetSubTreeType& ret) {
175 if (ec)
176 {
177 std::cerr << "Error calling mapper\n";
178 return;
179 }
180 for (const auto& [path, objDict] : ret)
181 {
182 if (objDict.empty())
183 {
184 return;
185 }
186 const std::string& owner = objDict.begin()->first;
187
188 for (const std::string& interface : objDict.begin()->second)
189 {
190 // anything that starts with a requested configuration
191 // is good
192 if (std::find_if(
193 interfaces.begin(), interfaces.end(),
194 [interface](const std::string& possible) {
195 return boost::starts_with(interface,
196 possible);
197 }) == interfaces.end())
198 {
199 continue;
200 }
201
202 self->dbusConnection->async_method_call(
203 [self, path, interface](
204 const boost::system::error_code ec,
205 boost::container::flat_map<
206 std::string, BasicVariantType>& data) {
207 if (ec)
208 {
209 std::cerr << "Error getting " << path
210 << "\n";
211 return;
212 }
213 self->respData[path][interface] =
214 std::move(data);
215 },
216 owner, path, "org.freedesktop.DBus.Properties",
217 "GetAll", interface);
218 }
219 }
220 },
221 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
222 "/", 0, interfaces);
223 }
224
225 ~GetSensorConfiguration()
226 {
227 callback(respData);
228 }
229
230 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
231 std::function<void(ManagedObjectType& resp)> callback;
232 ManagedObjectType respData;
233};