xref: /openbmc/entity-manager/src/utils.hpp (revision 59ef1e72fbf8c8eef1d5108b790a30e745cadaee)
1 /*
2 // Copyright (c) 2017 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 /// \file utils.hpp
17 
18 #pragma once
19 
20 #include <boost/container/flat_map.hpp>
21 #include <nlohmann/json.hpp>
22 #include <sdbusplus/asio/connection.hpp>
23 #include <sdbusplus/exception.hpp>
24 
25 #include <filesystem>
26 
27 using DBusValueVariant =
28     std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
29                  int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
30 using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>;
31 using DBusObject = boost::container::flat_map<std::string, DBusInterface>;
32 using MapperGetSubTreeResponse =
33     boost::container::flat_map<std::string, DBusObject>;
34 
35 bool findFiles(const std::filesystem::path& dirPath,
36                const std::string& matchString,
37                std::vector<std::filesystem::path>& foundPaths);
38 bool findFiles(const std::vector<std::filesystem::path>&& dirPaths,
39                const std::string& matchString,
40                std::vector<std::filesystem::path>& foundPaths);
41 
42 bool getI2cDevicePaths(
43     const std::filesystem::path& dirPath,
44     boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
45 
46 struct DBusInternalError final : public sdbusplus::exception_t
47 {
nameDBusInternalError48     const char* name() const noexcept override
49     {
50         return "org.freedesktop.DBus.Error.Failed";
51     }
descriptionDBusInternalError52     const char* description() const noexcept override
53     {
54         return "internal error";
55     }
whatDBusInternalError56     const char* what() const noexcept override
57     {
58         return "org.freedesktop.DBus.Error.Failed: "
59                "internal error";
60     }
61 
get_errnoDBusInternalError62     int get_errno() const noexcept override
63     {
64         return EACCES;
65     }
66 };
67 
deviceHasLogging(const nlohmann::json & json)68 inline bool deviceHasLogging(const nlohmann::json& json)
69 {
70     auto logging = json.find("Logging");
71     if (logging != json.end())
72     {
73         const auto* ptr = logging->get_ptr<const std::string*>();
74         if (ptr != nullptr)
75         {
76             if (*ptr == "Off")
77             {
78                 return false;
79             }
80         }
81     }
82     return true;
83 }
84 
85 /// \brief Match a Dbus property against a probe statement.
86 /// \param probe the probe statement to match against.
87 /// \param dbusValue the property value being matched to a probe.
88 /// \return true if the dbusValue matched the probe otherwise false.
89 bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);
90