blob: 123a3165331589b31553261c3032ed12249beed8 [file] [log] [blame]
Alexander Hansen4e1142d2025-07-25 17:07:27 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Intel Corporation
James Feist3cb5fec2018-01-23 14:41:51 -08003
4#pragma once
James Feist481c5d52019-08-13 14:40:40 -07005
James Feist1df06a42019-04-11 14:23:04 -07006#include <boost/container/flat_map.hpp>
James Feistb4383f42018-08-06 16:54:10 -07007#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -07008#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -07009#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080010
James Feist8c505da2020-05-28 10:06:33 -070011#include <filesystem>
Johnathan Mantey9b867872020-10-13 15:00:51 -070012
Andrew Jefferyeab49292022-04-05 14:42:20 +093013using DBusValueVariant =
James Feist481c5d52019-08-13 14:40:40 -070014 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
Brad Bishopcd1868e2020-08-28 17:58:52 -040015 int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
Andrew Jeffery1983d2f2022-04-05 14:55:13 +093016using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>;
17using DBusObject = boost::container::flat_map<std::string, DBusInterface>;
18using MapperGetSubTreeResponse =
19 boost::container::flat_map<std::string, DBusObject>;
James Feist481c5d52019-08-13 14:40:40 -070020
James Feista465ccc2019-02-08 12:51:01 -080021bool findFiles(const std::filesystem::path& dirPath,
22 const std::string& matchString,
23 std::vector<std::filesystem::path>& foundPaths);
Andrew Jefferya9c58922021-06-01 09:28:59 +093024bool findFiles(const std::vector<std::filesystem::path>&& dirPaths,
25 const std::string& matchString,
26 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070027
Nikhil Potaded8884f12019-03-27 13:27:13 -070028bool getI2cDevicePaths(
29 const std::filesystem::path& dirPath,
30 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
31
James Feist68500ff2018-08-08 15:40:42 -070032struct DBusInternalError final : public sdbusplus::exception_t
33{
James Feista465ccc2019-02-08 12:51:01 -080034 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070035 {
36 return "org.freedesktop.DBus.Error.Failed";
Patrick Williamscca78562021-09-08 15:43:23 -050037 }
James Feista465ccc2019-02-08 12:51:01 -080038 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070039 {
40 return "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050041 }
James Feista465ccc2019-02-08 12:51:01 -080042 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070043 {
44 return "org.freedesktop.DBus.Error.Failed: "
45 "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050046 }
47
48 int get_errno() const noexcept override
49 {
50 return EACCES;
51 }
James Feist68500ff2018-08-08 15:40:42 -070052};
James Feist1df06a42019-04-11 14:23:04 -070053
James Feist1ffa4a42020-04-22 18:27:17 -070054inline bool deviceHasLogging(const nlohmann::json& json)
55{
56 auto logging = json.find("Logging");
57 if (logging != json.end())
58 {
Ed Tanous3013fb42022-07-09 08:27:06 -070059 const auto* ptr = logging->get_ptr<const std::string*>();
60 if (ptr != nullptr)
James Feist1ffa4a42020-04-22 18:27:17 -070061 {
62 if (*ptr == "Off")
63 {
64 return false;
65 }
66 }
67 }
68 return true;
69}
Brad Bishop3cb8a602020-08-25 17:40:54 -040070
71/// \brief Match a Dbus property against a probe statement.
72/// \param probe the probe statement to match against.
73/// \param dbusValue the property value being matched to a probe.
74/// \return true if the dbusValue matched the probe otherwise false.
Andrew Jefferyeab49292022-04-05 14:42:20 +093075bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);