blob: 2c6ce9ef2f8161614d2d4a4a1ec8095be83cc6a5 [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 Feist8086aba2020-08-25 16:00:59 -07005#include <boost/asio/steady_timer.hpp>
James Feist6714a252018-09-10 15:26:18 -07006#include <boost/container/flat_map.hpp>
James Feist38fb5982020-05-28 10:09:54 -07007#include <sdbusplus/asio/connection.hpp>
8#include <sdbusplus/asio/object_server.hpp>
9#include <sdbusplus/message/types.hpp>
10
James Feist24f02f22019-04-15 11:05:39 -070011#include <filesystem>
Patrick Venturefd6ba732019-10-31 14:27:39 -070012#include <functional>
James Feist6714a252018-09-10 15:26:18 -070013#include <iostream>
Patrick Venturefd6ba732019-10-31 14:27:39 -070014#include <memory>
James Feist6714a252018-09-10 15:26:18 -070015#include <regex>
Patrick Venturefd6ba732019-10-31 14:27:39 -070016#include <string>
17#include <tuple>
18#include <utility>
19#include <variant>
20#include <vector>
James Feist6714a252018-09-10 15:26:18 -070021
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070022const constexpr char* jsonStore = "/var/configuration/flattened.json";
23const constexpr char* inventoryPath = "/xyz/openbmc_project/inventory";
24const constexpr char* entityManagerName = "xyz.openbmc_project.EntityManager";
James Feist58295ad2019-05-30 15:01:41 -070025
26constexpr const char* cpuInventoryPath =
27 "/xyz/openbmc_project/inventory/system/chassis/motherboard";
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070028const std::regex illegalDbusRegex("[^A-Za-z0-9_]");
James Feist6714a252018-09-10 15:26:18 -070029
30using BasicVariantType =
James Feist3eb82622019-02-08 13:10:22 -080031 std::variant<std::vector<std::string>, std::string, int64_t, uint64_t,
32 double, int32_t, uint32_t, int16_t, uint16_t, uint8_t, bool>;
Alex Qiu8b3f7d42020-01-06 13:54:42 -080033using SensorBaseConfigMap =
34 boost::container::flat_map<std::string, BasicVariantType>;
35using SensorBaseConfiguration = std::pair<std::string, SensorBaseConfigMap>;
36using SensorData = boost::container::flat_map<std::string, SensorBaseConfigMap>;
37using ManagedObjectType =
38 boost::container::flat_map<sdbusplus::message::object_path, SensorData>;
James Feist6714a252018-09-10 15:26:18 -070039
James Feista5e58722019-04-22 14:43:11 -070040using GetSubTreeType = std::vector<
41 std::pair<std::string,
42 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
James Feistd8bd5622019-06-26 12:09:05 -070043using Association = std::tuple<std::string, std::string, std::string>;
44
James Feistcf3bce62019-01-08 10:07:19 -080045bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070046 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080047 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070048 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080049bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080050bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080051void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070052bool getSensorConfiguration(
53 const std::string& type,
54 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080055 ManagedObjectType& resp, bool useCache = false);
56
James Feist82bac4c2019-03-11 11:16:53 -070057void createAssociation(
58 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
59 const std::string& path);
60
James Feist87d713a2018-12-06 16:06:24 -080061// replaces limits if MinReading and MaxReading are found.
62void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080063 const SensorBaseConfiguration* data);
64
James Feistfc94b212019-02-06 16:14:51 -080065enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080066{
67 on,
James Feistfc94b212019-02-06 16:14:51 -080068 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080069 always
70};
71
James Feista5e58722019-04-22 14:43:11 -070072namespace mapper
73{
74constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
75constexpr const char* path = "/xyz/openbmc_project/object_mapper";
76constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
77constexpr const char* subtree = "GetSubTree";
78} // namespace mapper
79
80namespace properties
81{
82constexpr const char* interface = "org.freedesktop.DBus.Properties";
83constexpr const char* get = "Get";
84} // namespace properties
85
James Feist52497fd2019-06-07 13:01:33 -070086namespace power
87{
88const static constexpr char* busname = "xyz.openbmc_project.State.Host";
89const static constexpr char* interface = "xyz.openbmc_project.State.Host";
90const static constexpr char* path = "/xyz/openbmc_project/state/host0";
91const static constexpr char* property = "CurrentHostState";
92} // namespace power
93namespace post
94{
95const static constexpr char* busname =
96 "xyz.openbmc_project.State.OperatingSystem";
97const static constexpr char* interface =
98 "xyz.openbmc_project.State.OperatingSystem.Status";
99const static constexpr char* path = "/xyz/openbmc_project/state/os";
100const static constexpr char* property = "OperatingSystemState";
101} // namespace post
102
James Feist2adc95c2019-09-30 14:55:28 -0700103namespace association
104{
105const static constexpr char* interface =
106 "xyz.openbmc_project.Association.Definitions";
107} // namespace association
108
James Feist40a72142018-12-21 10:09:53 -0800109template <typename T>
110inline T loadVariant(
111 const boost::container::flat_map<std::string, BasicVariantType>& data,
112 const std::string& key)
113{
114 auto it = data.find(key);
115 if (it == data.end())
116 {
117 std::cerr << "Configuration missing " << key << "\n";
118 throw std::invalid_argument("Key Missing");
119 }
120 if constexpr (std::is_same_v<T, double>)
121 {
James Feist3eb82622019-02-08 13:10:22 -0800122 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800123 }
James Feist6ef20402019-01-07 16:45:08 -0800124 else if constexpr (std::is_unsigned_v<T>)
125 {
James Feist3eb82622019-02-08 13:10:22 -0800126 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800127 }
James Feist40a72142018-12-21 10:09:53 -0800128 else if constexpr (std::is_same_v<T, std::string>)
129 {
James Feist3eb82622019-02-08 13:10:22 -0800130 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800131 }
132 else
133 {
James Feist52497fd2019-06-07 13:01:33 -0700134 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800135 }
136}
James Feistfc94b212019-02-06 16:14:51 -0800137
138inline void setReadState(const std::string& str, PowerState& val)
139{
140
141 if (str == "On")
142 {
143 val = PowerState::on;
144 }
145 else if (str == "BiosPost")
146 {
147 val = PowerState::biosPost;
148 }
149 else if (str == "Always")
150 {
151 val = PowerState::always;
152 }
153}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800154
155void createInventoryAssoc(
156 std::shared_ptr<sdbusplus::asio::connection> conn,
157 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
158 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700159
James Feist38fb5982020-05-28 10:09:54 -0700160struct GetSensorConfiguration :
161 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700162{
163 GetSensorConfiguration(
164 std::shared_ptr<sdbusplus::asio::connection> connection,
165 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
166 dbusConnection(connection),
167 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700168 {}
James Feistf27a55c2020-08-04 14:27:30 -0700169
170 void getPath(const std::string& path, const std::string& interface,
171 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700172 {
James Feistf27a55c2020-08-04 14:27:30 -0700173 if (retries > 5)
174 {
175 retries = 5;
176 }
177 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
178
179 self->dbusConnection->async_method_call(
180 [self, path, interface, owner,
181 retries](const boost::system::error_code ec,
182 boost::container::flat_map<std::string, BasicVariantType>&
183 data) {
184 if (ec)
185 {
186 std::cerr << "Error getting " << path << ": retries left"
187 << retries - 1 << "\n";
188 if (!retries)
189 {
190 return;
191 }
192 auto timer = std::make_shared<boost::asio::steady_timer>(
193 self->dbusConnection->get_io_context());
194 timer->expires_after(std::chrono::seconds(10));
195 timer->async_wait([self, timer, path, interface, owner,
196 retries](boost::system::error_code ec) {
197 if (ec)
198 {
199 std::cerr << "Timer error!\n";
200 return;
201 }
202 self->getPath(path, interface, owner, retries - 1);
203 });
204 return;
205 }
206
207 self->respData[path][interface] = std::move(data);
208 },
209 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
210 interface);
211 }
212
213 void getConfiguration(const std::vector<std::string>& interfaces,
214 size_t retries = 0)
215 {
216 if (retries > 5)
217 {
218 retries = 5;
219 }
220
James Feistc71c1192019-09-18 14:31:33 -0700221 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
222 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700223 [self, interfaces, retries](const boost::system::error_code ec,
224 const GetSubTreeType& ret) {
James Feistc71c1192019-09-18 14:31:33 -0700225 if (ec)
226 {
227 std::cerr << "Error calling mapper\n";
James Feistf27a55c2020-08-04 14:27:30 -0700228 if (!retries)
229 {
230 return;
231 }
232 auto timer = std::make_shared<boost::asio::steady_timer>(
233 self->dbusConnection->get_io_context());
234 timer->expires_after(std::chrono::seconds(10));
235 timer->async_wait([self, timer, interfaces,
236 retries](boost::system::error_code ec) {
237 if (ec)
238 {
239 std::cerr << "Timer error!\n";
240 return;
241 }
242 self->getConfiguration(interfaces, retries - 1);
243 });
244
James Feistc71c1192019-09-18 14:31:33 -0700245 return;
246 }
247 for (const auto& [path, objDict] : ret)
248 {
249 if (objDict.empty())
250 {
251 return;
252 }
253 const std::string& owner = objDict.begin()->first;
254
255 for (const std::string& interface : objDict.begin()->second)
256 {
257 // anything that starts with a requested configuration
258 // is good
259 if (std::find_if(
260 interfaces.begin(), interfaces.end(),
261 [interface](const std::string& possible) {
262 return boost::starts_with(interface,
263 possible);
264 }) == interfaces.end())
265 {
266 continue;
267 }
James Feistf27a55c2020-08-04 14:27:30 -0700268 self->getPath(path, interface, owner);
James Feistc71c1192019-09-18 14:31:33 -0700269 }
270 }
271 },
272 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
273 "/", 0, interfaces);
274 }
275
276 ~GetSensorConfiguration()
277 {
278 callback(respData);
279 }
280
281 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
282 std::function<void(ManagedObjectType& resp)> callback;
283 ManagedObjectType respData;
284};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200285
286// The common scheme for sysfs files naming is: <type><number>_<item>.
287// This function returns optionally these 3 elements as a tuple.
288std::optional<std::tuple<std::string, std::string, std::string>>
289 splitFileName(const std::filesystem::path& filePath);
290std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700291 const double& scaleFactor);