xref: /openbmc/entity-manager/src/utils.hpp (revision 5df916f17c178e9633a9df999a6adae5449fc524)
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 <charconv>
12 #include <filesystem>
13 
14 using DBusValueVariant =
15     std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
16                  int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
17 using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>;
18 using DBusObject = boost::container::flat_map<std::string, DBusInterface>;
19 using MapperGetSubTreeResponse =
20     boost::container::flat_map<std::string, DBusObject>;
21 
22 bool findFiles(const std::filesystem::path& dirPath,
23                const std::string& matchString,
24                std::vector<std::filesystem::path>& foundPaths);
25 bool findFiles(const std::vector<std::filesystem::path>&& dirPaths,
26                const std::string& matchString,
27                std::vector<std::filesystem::path>& foundPaths);
28 
29 bool getI2cDevicePaths(
30     const std::filesystem::path& dirPath,
31     boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
32 
33 struct DBusInternalError final : public sdbusplus::exception_t
34 {
nameDBusInternalError35     const char* name() const noexcept override
36     {
37         return "org.freedesktop.DBus.Error.Failed";
38     }
descriptionDBusInternalError39     const char* description() const noexcept override
40     {
41         return "internal error";
42     }
whatDBusInternalError43     const char* what() const noexcept override
44     {
45         return "org.freedesktop.DBus.Error.Failed: "
46                "internal error";
47     }
48 
get_errnoDBusInternalError49     int get_errno() const noexcept override
50     {
51         return EACCES;
52     }
53 };
54 
deviceHasLogging(const nlohmann::json & json)55 inline bool deviceHasLogging(const nlohmann::json& json)
56 {
57     auto logging = json.find("Logging");
58     if (logging != json.end())
59     {
60         const auto* ptr = logging->get_ptr<const std::string*>();
61         if (ptr != nullptr)
62         {
63             if (*ptr == "Off")
64             {
65                 return false;
66             }
67         }
68     }
69     return true;
70 }
71 
72 /// \brief Match a Dbus property against a probe statement.
73 /// \param probe the probe statement to match against.
74 /// \param dbusValue the property value being matched to a probe.
75 /// \return true if the dbusValue matched the probe otherwise false.
76 bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);
77 
78 template <typename T>
fromCharsWrapper(const std::string_view & str,T & out,bool & fullMatch,int base=10)79 std::from_chars_result fromCharsWrapper(const std::string_view& str, T& out,
80                                         bool& fullMatch, int base = 10)
81 {
82     auto result = std::from_chars(
83         str.data(), std::next(str.begin(), str.size()), out, base);
84 
85     fullMatch = result.ptr == std::next(str.begin(), str.size());
86 
87     return result;
88 }
89