Alexander Hansen | 4e1142d | 2025-07-25 17:07:27 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: Apache-2.0 |
| 2 | // SPDX-FileCopyrightText: Copyright 2017 Intel Corporation |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 3 | |
| 4 | #pragma once |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 5 | |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 6 | #include <boost/container/flat_map.hpp> |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 7 | #include <nlohmann/json.hpp> |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 8 | #include <sdbusplus/asio/connection.hpp> |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 9 | #include <sdbusplus/exception.hpp> |
James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 10 | |
James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 11 | #include <filesystem> |
Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 12 | |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 13 | using DBusValueVariant = |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 14 | std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t, |
Brad Bishop | cd1868e | 2020-08-28 17:58:52 -0400 | [diff] [blame] | 15 | int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>; |
Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 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>; |
James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 20 | |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 21 | bool findFiles(const std::filesystem::path& dirPath, |
| 22 | const std::string& matchString, |
| 23 | std::vector<std::filesystem::path>& foundPaths); |
Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 24 | bool findFiles(const std::vector<std::filesystem::path>&& dirPaths, |
| 25 | const std::string& matchString, |
| 26 | std::vector<std::filesystem::path>& foundPaths); |
James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 27 | |
Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 28 | bool getI2cDevicePaths( |
| 29 | const std::filesystem::path& dirPath, |
| 30 | boost::container::flat_map<size_t, std::filesystem::path>& busPaths); |
| 31 | |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 32 | struct DBusInternalError final : public sdbusplus::exception_t |
| 33 | { |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 34 | const char* name() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 35 | { |
| 36 | return "org.freedesktop.DBus.Error.Failed"; |
Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 37 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 38 | const char* description() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 39 | { |
| 40 | return "internal error"; |
Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 41 | } |
James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 42 | const char* what() const noexcept override |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 43 | { |
| 44 | return "org.freedesktop.DBus.Error.Failed: " |
| 45 | "internal error"; |
Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | int get_errno() const noexcept override |
| 49 | { |
| 50 | return EACCES; |
| 51 | } |
James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 52 | }; |
James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 53 | |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 54 | inline bool deviceHasLogging(const nlohmann::json& json) |
| 55 | { |
| 56 | auto logging = json.find("Logging"); |
| 57 | if (logging != json.end()) |
| 58 | { |
Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 59 | const auto* ptr = logging->get_ptr<const std::string*>(); |
| 60 | if (ptr != nullptr) |
James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 61 | { |
| 62 | if (*ptr == "Off") |
| 63 | { |
| 64 | return false; |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return true; |
| 69 | } |
Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 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. |
Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 75 | bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue); |