blob: c164187698ab81ab80a50f46c8c915c7042e2c78 [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
Jason Ling100c20b2020-08-11 14:50:33 -070045std::optional<std::string> openAndRead(const std::string& hwmonFile);
46std::optional<std::string>
47 getFullHwmonFilePath(const std::string& directory,
48 const std::string& hwmonBaseName,
49 const std::set<std::string>& permitSet);
50std::set<std::string> getPermitSet(const SensorBaseConfigMap& config);
James Feistcf3bce62019-01-08 10:07:19 -080051bool findFiles(const std::filesystem::path dirPath,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070052 const std::string& matchString,
James Feistcf3bce62019-01-08 10:07:19 -080053 std::vector<std::filesystem::path>& foundPaths,
Jae Hyun Yoo9ced0a32018-10-25 10:42:39 -070054 unsigned int symlinkDepth = 1);
James Feist71d31b22019-01-02 16:57:54 -080055bool isPowerOn(void);
James Feistfc94b212019-02-06 16:14:51 -080056bool hasBiosPost(void);
James Feist71d31b22019-01-02 16:57:54 -080057void setupPowerMatch(const std::shared_ptr<sdbusplus::asio::connection>& conn);
James Feist6714a252018-09-10 15:26:18 -070058bool getSensorConfiguration(
59 const std::string& type,
60 const std::shared_ptr<sdbusplus::asio::connection>& dbusConnection,
James Feist87d713a2018-12-06 16:06:24 -080061 ManagedObjectType& resp, bool useCache = false);
62
James Feist82bac4c2019-03-11 11:16:53 -070063void createAssociation(
64 std::shared_ptr<sdbusplus::asio::dbus_interface>& association,
65 const std::string& path);
66
James Feist87d713a2018-12-06 16:06:24 -080067// replaces limits if MinReading and MaxReading are found.
68void findLimits(std::pair<double, double>& limits,
James Feist40a72142018-12-21 10:09:53 -080069 const SensorBaseConfiguration* data);
70
James Feistfc94b212019-02-06 16:14:51 -080071enum class PowerState
James Feist6ef20402019-01-07 16:45:08 -080072{
73 on,
James Feistfc94b212019-02-06 16:14:51 -080074 biosPost,
James Feist6ef20402019-01-07 16:45:08 -080075 always
76};
77
James Feista5e58722019-04-22 14:43:11 -070078namespace mapper
79{
80constexpr const char* busName = "xyz.openbmc_project.ObjectMapper";
81constexpr const char* path = "/xyz/openbmc_project/object_mapper";
82constexpr const char* interface = "xyz.openbmc_project.ObjectMapper";
83constexpr const char* subtree = "GetSubTree";
84} // namespace mapper
85
86namespace properties
87{
88constexpr const char* interface = "org.freedesktop.DBus.Properties";
89constexpr const char* get = "Get";
James Feist49a8ccd2020-09-16 16:09:52 -070090constexpr const char* set = "Set";
James Feista5e58722019-04-22 14:43:11 -070091} // namespace properties
92
James Feist52497fd2019-06-07 13:01:33 -070093namespace power
94{
95const static constexpr char* busname = "xyz.openbmc_project.State.Host";
96const static constexpr char* interface = "xyz.openbmc_project.State.Host";
97const static constexpr char* path = "/xyz/openbmc_project/state/host0";
98const static constexpr char* property = "CurrentHostState";
99} // namespace power
100namespace post
101{
102const static constexpr char* busname =
103 "xyz.openbmc_project.State.OperatingSystem";
104const static constexpr char* interface =
105 "xyz.openbmc_project.State.OperatingSystem.Status";
106const static constexpr char* path = "/xyz/openbmc_project/state/os";
107const static constexpr char* property = "OperatingSystemState";
108} // namespace post
109
James Feist2adc95c2019-09-30 14:55:28 -0700110namespace association
111{
112const static constexpr char* interface =
113 "xyz.openbmc_project.Association.Definitions";
114} // namespace association
115
James Feist40a72142018-12-21 10:09:53 -0800116template <typename T>
117inline T loadVariant(
118 const boost::container::flat_map<std::string, BasicVariantType>& data,
119 const std::string& key)
120{
121 auto it = data.find(key);
122 if (it == data.end())
123 {
124 std::cerr << "Configuration missing " << key << "\n";
125 throw std::invalid_argument("Key Missing");
126 }
127 if constexpr (std::is_same_v<T, double>)
128 {
James Feist3eb82622019-02-08 13:10:22 -0800129 return std::visit(VariantToDoubleVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800130 }
James Feist6ef20402019-01-07 16:45:08 -0800131 else if constexpr (std::is_unsigned_v<T>)
132 {
James Feist3eb82622019-02-08 13:10:22 -0800133 return std::visit(VariantToUnsignedIntVisitor(), it->second);
James Feist6ef20402019-01-07 16:45:08 -0800134 }
James Feist40a72142018-12-21 10:09:53 -0800135 else if constexpr (std::is_same_v<T, std::string>)
136 {
James Feist3eb82622019-02-08 13:10:22 -0800137 return std::visit(VariantToStringVisitor(), it->second);
James Feist40a72142018-12-21 10:09:53 -0800138 }
139 else
140 {
James Feist52497fd2019-06-07 13:01:33 -0700141 static_assert(!std::is_same_v<T, T>, "Type Not Implemented");
James Feist40a72142018-12-21 10:09:53 -0800142 }
143}
James Feistfc94b212019-02-06 16:14:51 -0800144
145inline void setReadState(const std::string& str, PowerState& val)
146{
147
148 if (str == "On")
149 {
150 val = PowerState::on;
151 }
152 else if (str == "BiosPost")
153 {
154 val = PowerState::biosPost;
155 }
156 else if (str == "Always")
157 {
158 val = PowerState::always;
159 }
160}
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800161
James Feist49a8ccd2020-09-16 16:09:52 -0700162inline void setLed(std::shared_ptr<sdbusplus::asio::connection> conn,
163 const std::string& name, bool on)
164{
165 conn->async_method_call(
166 [name](const boost::system::error_code ec) {
167 if (ec)
168 {
169 std::cerr << "Failed to set LED " << name << "\n";
170 }
171 },
172 "xyz.openbmc_project.LED.GroupManager",
173 "/xyz/openbmc_project/led/groups/" + name, properties::interface,
174 properties::set, "xyz.openbmc_project.Led.Group", "Asserted",
175 std::variant<bool>(on));
176}
177
Cheng C Yang5580f2f2019-09-19 09:01:47 +0800178void createInventoryAssoc(
179 std::shared_ptr<sdbusplus::asio::connection> conn,
180 std::shared_ptr<sdbusplus::asio::dbus_interface> association,
181 const std::string& path);
James Feistc71c1192019-09-18 14:31:33 -0700182
James Feist38fb5982020-05-28 10:09:54 -0700183struct GetSensorConfiguration :
184 std::enable_shared_from_this<GetSensorConfiguration>
James Feistc71c1192019-09-18 14:31:33 -0700185{
186 GetSensorConfiguration(
187 std::shared_ptr<sdbusplus::asio::connection> connection,
188 std::function<void(ManagedObjectType& resp)>&& callbackFunc) :
189 dbusConnection(connection),
190 callback(std::move(callbackFunc))
James Feist38fb5982020-05-28 10:09:54 -0700191 {}
James Feistf27a55c2020-08-04 14:27:30 -0700192
193 void getPath(const std::string& path, const std::string& interface,
194 const std::string& owner, size_t retries = 5)
James Feistc71c1192019-09-18 14:31:33 -0700195 {
James Feistf27a55c2020-08-04 14:27:30 -0700196 if (retries > 5)
197 {
198 retries = 5;
199 }
200 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
201
202 self->dbusConnection->async_method_call(
203 [self, path, interface, owner,
204 retries](const boost::system::error_code ec,
205 boost::container::flat_map<std::string, BasicVariantType>&
206 data) {
207 if (ec)
208 {
209 std::cerr << "Error getting " << path << ": retries left"
210 << retries - 1 << "\n";
211 if (!retries)
212 {
213 return;
214 }
215 auto timer = std::make_shared<boost::asio::steady_timer>(
216 self->dbusConnection->get_io_context());
217 timer->expires_after(std::chrono::seconds(10));
218 timer->async_wait([self, timer, path, interface, owner,
219 retries](boost::system::error_code ec) {
220 if (ec)
221 {
222 std::cerr << "Timer error!\n";
223 return;
224 }
225 self->getPath(path, interface, owner, retries - 1);
226 });
227 return;
228 }
229
230 self->respData[path][interface] = std::move(data);
231 },
232 owner, path, "org.freedesktop.DBus.Properties", "GetAll",
233 interface);
234 }
235
236 void getConfiguration(const std::vector<std::string>& interfaces,
237 size_t retries = 0)
238 {
239 if (retries > 5)
240 {
241 retries = 5;
242 }
243
James Feistc71c1192019-09-18 14:31:33 -0700244 std::shared_ptr<GetSensorConfiguration> self = shared_from_this();
245 dbusConnection->async_method_call(
James Feistf27a55c2020-08-04 14:27:30 -0700246 [self, interfaces, retries](const boost::system::error_code ec,
247 const GetSubTreeType& ret) {
James Feistc71c1192019-09-18 14:31:33 -0700248 if (ec)
249 {
250 std::cerr << "Error calling mapper\n";
James Feistf27a55c2020-08-04 14:27:30 -0700251 if (!retries)
252 {
253 return;
254 }
255 auto timer = std::make_shared<boost::asio::steady_timer>(
256 self->dbusConnection->get_io_context());
257 timer->expires_after(std::chrono::seconds(10));
258 timer->async_wait([self, timer, interfaces,
259 retries](boost::system::error_code ec) {
260 if (ec)
261 {
262 std::cerr << "Timer error!\n";
263 return;
264 }
265 self->getConfiguration(interfaces, retries - 1);
266 });
267
James Feistc71c1192019-09-18 14:31:33 -0700268 return;
269 }
270 for (const auto& [path, objDict] : ret)
271 {
272 if (objDict.empty())
273 {
274 return;
275 }
276 const std::string& owner = objDict.begin()->first;
277
278 for (const std::string& interface : objDict.begin()->second)
279 {
280 // anything that starts with a requested configuration
281 // is good
282 if (std::find_if(
283 interfaces.begin(), interfaces.end(),
284 [interface](const std::string& possible) {
285 return boost::starts_with(interface,
286 possible);
287 }) == interfaces.end())
288 {
289 continue;
290 }
James Feistf27a55c2020-08-04 14:27:30 -0700291 self->getPath(path, interface, owner);
James Feistc71c1192019-09-18 14:31:33 -0700292 }
293 }
294 },
295 mapper::busName, mapper::path, mapper::interface, mapper::subtree,
296 "/", 0, interfaces);
297 }
298
299 ~GetSensorConfiguration()
300 {
301 callback(respData);
302 }
303
304 std::shared_ptr<sdbusplus::asio::connection> dbusConnection;
305 std::function<void(ManagedObjectType& resp)> callback;
306 ManagedObjectType respData;
307};
Zbigniew Kurzynski63f38662020-06-09 13:02:11 +0200308
309// The common scheme for sysfs files naming is: <type><number>_<item>.
310// This function returns optionally these 3 elements as a tuple.
311std::optional<std::tuple<std::string, std::string, std::string>>
312 splitFileName(const std::filesystem::path& filePath);
313std::optional<double> readFile(const std::string& thresholdFile,
James Feist8086aba2020-08-25 16:00:59 -0700314 const double& scaleFactor);