xref: /openbmc/entity-manager/src/utils.hpp (revision 4e1142d6f418f48ea260132ebb5a4995b2310c90)
1 // SPDX-License-Identifier: Apache-2.0
2 // SPDX-FileCopyrightText: Copyright 2017 Intel Corporation
3 
4 #pragma once
5 
6 #include <boost/container/flat_map.hpp>
7 #include <nlohmann/json.hpp>
8 #include <sdbusplus/asio/connection.hpp>
9 #include <sdbusplus/exception.hpp>
10 
11 #include <filesystem>
12 
13 using DBusValueVariant =
14     std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
15                  int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
16 using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>;
17 using DBusObject = boost::container::flat_map<std::string, DBusInterface>;
18 using MapperGetSubTreeResponse =
19     boost::container::flat_map<std::string, DBusObject>;
20 
21 bool findFiles(const std::filesystem::path& dirPath,
22                const std::string& matchString,
23                std::vector<std::filesystem::path>& foundPaths);
24 bool findFiles(const std::vector<std::filesystem::path>&& dirPaths,
25                const std::string& matchString,
26                std::vector<std::filesystem::path>& foundPaths);
27 
28 bool getI2cDevicePaths(
29     const std::filesystem::path& dirPath,
30     boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
31 
32 struct DBusInternalError final : public sdbusplus::exception_t
33 {
34     const char* name() const noexcept override
35     {
36         return "org.freedesktop.DBus.Error.Failed";
37     }
38     const char* description() const noexcept override
39     {
40         return "internal error";
41     }
42     const char* what() const noexcept override
43     {
44         return "org.freedesktop.DBus.Error.Failed: "
45                "internal error";
46     }
47 
48     int get_errno() const noexcept override
49     {
50         return EACCES;
51     }
52 };
53 
54 inline bool deviceHasLogging(const nlohmann::json& json)
55 {
56     auto logging = json.find("Logging");
57     if (logging != json.end())
58     {
59         const auto* ptr = logging->get_ptr<const std::string*>();
60         if (ptr != nullptr)
61         {
62             if (*ptr == "Off")
63             {
64                 return false;
65             }
66         }
67     }
68     return true;
69 }
70 
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.
75 bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);
76