| 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 | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 6 | #include <nlohmann/json.hpp> |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 7 | #include <sdbusplus/asio/connection.hpp> |
| James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 8 | #include <sdbusplus/exception.hpp> |
| James Feist | 3cb5fec | 2018-01-23 14:41:51 -0800 | [diff] [blame] | 9 | |
| Alexander Hansen | 5df916f | 2025-09-26 10:31:36 -0400 | [diff] [blame] | 10 | #include <charconv> |
| James Feist | 8c505da | 2020-05-28 10:06:33 -0700 | [diff] [blame] | 11 | #include <filesystem> |
| Ed Tanous | dbf95b2 | 2025-10-13 11:38:35 -0700 | [diff] [blame] | 12 | #include <flat_map> |
| Johnathan Mantey | 9b86787 | 2020-10-13 15:00:51 -0700 | [diff] [blame] | 13 | |
| Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 14 | using DBusValueVariant = |
| James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 15 | 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] | 16 | int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>; |
| Ed Tanous | dbf95b2 | 2025-10-13 11:38:35 -0700 | [diff] [blame] | 17 | using DBusInterface = std::flat_map<std::string, DBusValueVariant, std::less<>>; |
| 18 | using DBusObject = std::flat_map<std::string, DBusInterface, std::less<>>; |
| Andrew Jeffery | 1983d2f | 2022-04-05 14:55:13 +0930 | [diff] [blame] | 19 | using MapperGetSubTreeResponse = |
| Ed Tanous | dbf95b2 | 2025-10-13 11:38:35 -0700 | [diff] [blame] | 20 | std::flat_map<std::string, DBusObject, std::less<>>; |
| George Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 21 | using FirstIndex = size_t; |
| 22 | using LastIndex = size_t; |
| James Feist | 481c5d5 | 2019-08-13 14:40:40 -0700 | [diff] [blame] | 23 | |
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 24 | bool findFiles(const std::filesystem::path& dirPath, |
| 25 | const std::string& matchString, |
| 26 | std::vector<std::filesystem::path>& foundPaths); |
| Alexander Hansen | 8290ca4 | 2025-08-04 15:27:22 +0200 | [diff] [blame] | 27 | bool findFiles(const std::vector<std::filesystem::path>& dirPaths, |
| Andrew Jeffery | a9c5892 | 2021-06-01 09:28:59 +0930 | [diff] [blame] | 28 | const std::string& matchString, |
| 29 | std::vector<std::filesystem::path>& foundPaths); |
| James Feist | b4383f4 | 2018-08-06 16:54:10 -0700 | [diff] [blame] | 30 | |
| Ed Tanous | dbf95b2 | 2025-10-13 11:38:35 -0700 | [diff] [blame] | 31 | bool getI2cDevicePaths(const std::filesystem::path& dirPath, |
| 32 | std::flat_map<size_t, std::filesystem::path>& busPaths); |
| Nikhil Potade | d8884f1 | 2019-03-27 13:27:13 -0700 | [diff] [blame] | 33 | |
| James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 34 | struct DBusInternalError final : public sdbusplus::exception_t |
| 35 | { |
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 36 | const char* name() const noexcept override |
| James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 37 | { |
| 38 | return "org.freedesktop.DBus.Error.Failed"; |
| Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 39 | } |
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 40 | const char* description() const noexcept override |
| James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 41 | { |
| 42 | return "internal error"; |
| Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 43 | } |
| James Feist | a465ccc | 2019-02-08 12:51:01 -0800 | [diff] [blame] | 44 | const char* what() const noexcept override |
| James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 45 | { |
| 46 | return "org.freedesktop.DBus.Error.Failed: " |
| 47 | "internal error"; |
| Patrick Williams | cca7856 | 2021-09-08 15:43:23 -0500 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | int get_errno() const noexcept override |
| 51 | { |
| 52 | return EACCES; |
| 53 | } |
| James Feist | 68500ff | 2018-08-08 15:40:42 -0700 | [diff] [blame] | 54 | }; |
| James Feist | 1df06a4 | 2019-04-11 14:23:04 -0700 | [diff] [blame] | 55 | |
| James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 56 | inline bool deviceHasLogging(const nlohmann::json& json) |
| 57 | { |
| 58 | auto logging = json.find("Logging"); |
| 59 | if (logging != json.end()) |
| 60 | { |
| Ed Tanous | 3013fb4 | 2022-07-09 08:27:06 -0700 | [diff] [blame] | 61 | const auto* ptr = logging->get_ptr<const std::string*>(); |
| 62 | if (ptr != nullptr) |
| James Feist | 1ffa4a4 | 2020-04-22 18:27:17 -0700 | [diff] [blame] | 63 | { |
| 64 | if (*ptr == "Off") |
| 65 | { |
| 66 | return false; |
| 67 | } |
| 68 | } |
| 69 | } |
| 70 | return true; |
| 71 | } |
| Brad Bishop | 3cb8a60 | 2020-08-25 17:40:54 -0400 | [diff] [blame] | 72 | |
| 73 | /// \brief Match a Dbus property against a probe statement. |
| 74 | /// \param probe the probe statement to match against. |
| 75 | /// \param dbusValue the property value being matched to a probe. |
| 76 | /// \return true if the dbusValue matched the probe otherwise false. |
| Andrew Jeffery | eab4929 | 2022-04-05 14:42:20 +0930 | [diff] [blame] | 77 | bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue); |
| Alexander Hansen | 5df916f | 2025-09-26 10:31:36 -0400 | [diff] [blame] | 78 | |
| Ed Tanous | c5a2af9 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 79 | inline char asciiToLower(char c) |
| 80 | { |
| 81 | // Converts a character to lower case without relying on std::locale |
| 82 | if ('A' <= c && c <= 'Z') |
| 83 | { |
| 84 | c -= static_cast<char>('A' - 'a'); |
| 85 | } |
| 86 | return c; |
| 87 | } |
| 88 | |
| 89 | template <typename T> |
| 90 | auto iFindFirst(T&& str, std::string_view sub) |
| 91 | { |
| 92 | return std::ranges::search(str, sub, [](char a, char b) { |
| 93 | return asciiToLower(a) == asciiToLower(b); |
| 94 | }); |
| 95 | } |
| George Liu | ce8d1d0 | 2025-08-25 08:58:25 +0800 | [diff] [blame] | 96 | |
| George Liu | ecf1a31 | 2025-08-25 10:43:12 +0800 | [diff] [blame] | 97 | std::vector<std::string> split(std::string_view str, char delim); |
| 98 | |
| George Liu | 5a61ec8 | 2025-08-25 11:16:44 +0800 | [diff] [blame] | 99 | void iReplaceAll(std::string& str, std::string_view search, |
| 100 | std::string_view replace); |
| 101 | |
| 102 | void replaceAll(std::string& str, std::string_view search, |
| 103 | std::string_view replace); |
| 104 | |
| George Liu | fd977ec | 2025-08-25 11:36:44 +0800 | [diff] [blame] | 105 | std::string toLowerCopy(std::string_view str); |
| 106 | |
| Alexander Hansen | 5df916f | 2025-09-26 10:31:36 -0400 | [diff] [blame] | 107 | template <typename T> |
| 108 | std::from_chars_result fromCharsWrapper(const std::string_view& str, T& out, |
| 109 | bool& fullMatch, int base = 10) |
| 110 | { |
| 111 | auto result = std::from_chars( |
| 112 | str.data(), std::next(str.begin(), str.size()), out, base); |
| 113 | |
| 114 | fullMatch = result.ptr == std::next(str.begin(), str.size()); |
| 115 | |
| 116 | return result; |
| 117 | } |