blob: b14a6203555a2ae2d0e3beaeddadc104aa1c9d74 [file] [log] [blame]
Alexander Hansen4e1142d2025-07-25 17:07:27 +02001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Intel Corporation
James Feist3cb5fec2018-01-23 14:41:51 -08003
4#pragma once
James Feist481c5d52019-08-13 14:40:40 -07005
James Feistb4383f42018-08-06 16:54:10 -07006#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -07007#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -07008#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -08009
Alexander Hansen5df916f2025-09-26 10:31:36 -040010#include <charconv>
James Feist8c505da2020-05-28 10:06:33 -070011#include <filesystem>
Ed Tanousdbf95b22025-10-13 11:38:35 -070012#include <flat_map>
Johnathan Mantey9b867872020-10-13 15:00:51 -070013
Andrew Jefferyeab49292022-04-05 14:42:20 +093014using DBusValueVariant =
James Feist481c5d52019-08-13 14:40:40 -070015 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
Brad Bishopcd1868e2020-08-28 17:58:52 -040016 int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
Ed Tanousdbf95b22025-10-13 11:38:35 -070017using DBusInterface = std::flat_map<std::string, DBusValueVariant, std::less<>>;
18using DBusObject = std::flat_map<std::string, DBusInterface, std::less<>>;
Andrew Jeffery1983d2f2022-04-05 14:55:13 +093019using MapperGetSubTreeResponse =
Ed Tanousdbf95b22025-10-13 11:38:35 -070020 std::flat_map<std::string, DBusObject, std::less<>>;
George Liuce8d1d02025-08-25 08:58:25 +080021using FirstIndex = size_t;
22using LastIndex = size_t;
James Feist481c5d52019-08-13 14:40:40 -070023
James Feista465ccc2019-02-08 12:51:01 -080024bool findFiles(const std::filesystem::path& dirPath,
25 const std::string& matchString,
26 std::vector<std::filesystem::path>& foundPaths);
Alexander Hansen8290ca42025-08-04 15:27:22 +020027bool findFiles(const std::vector<std::filesystem::path>& dirPaths,
Andrew Jefferya9c58922021-06-01 09:28:59 +093028 const std::string& matchString,
29 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070030
Ed Tanousdbf95b22025-10-13 11:38:35 -070031bool getI2cDevicePaths(const std::filesystem::path& dirPath,
32 std::flat_map<size_t, std::filesystem::path>& busPaths);
Nikhil Potaded8884f12019-03-27 13:27:13 -070033
James Feist68500ff2018-08-08 15:40:42 -070034struct DBusInternalError final : public sdbusplus::exception_t
35{
James Feista465ccc2019-02-08 12:51:01 -080036 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070037 {
38 return "org.freedesktop.DBus.Error.Failed";
Patrick Williamscca78562021-09-08 15:43:23 -050039 }
James Feista465ccc2019-02-08 12:51:01 -080040 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070041 {
42 return "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050043 }
James Feista465ccc2019-02-08 12:51:01 -080044 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070045 {
46 return "org.freedesktop.DBus.Error.Failed: "
47 "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050048 }
49
50 int get_errno() const noexcept override
51 {
52 return EACCES;
53 }
James Feist68500ff2018-08-08 15:40:42 -070054};
James Feist1df06a42019-04-11 14:23:04 -070055
James Feist1ffa4a42020-04-22 18:27:17 -070056inline bool deviceHasLogging(const nlohmann::json& json)
57{
58 auto logging = json.find("Logging");
59 if (logging != json.end())
60 {
Ed Tanous3013fb42022-07-09 08:27:06 -070061 const auto* ptr = logging->get_ptr<const std::string*>();
62 if (ptr != nullptr)
James Feist1ffa4a42020-04-22 18:27:17 -070063 {
64 if (*ptr == "Off")
65 {
66 return false;
67 }
68 }
69 }
70 return true;
71}
Brad Bishop3cb8a602020-08-25 17:40:54 -040072
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 Jefferyeab49292022-04-05 14:42:20 +093077bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);
Alexander Hansen5df916f2025-09-26 10:31:36 -040078
Ed Tanousc5a2af92025-08-25 08:58:25 +080079inline 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
89template <typename T>
90auto 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 Liuce8d1d02025-08-25 08:58:25 +080096
George Liuecf1a312025-08-25 10:43:12 +080097std::vector<std::string> split(std::string_view str, char delim);
98
George Liu5a61ec82025-08-25 11:16:44 +080099void iReplaceAll(std::string& str, std::string_view search,
100 std::string_view replace);
101
102void replaceAll(std::string& str, std::string_view search,
103 std::string_view replace);
104
George Liufd977ec2025-08-25 11:36:44 +0800105std::string toLowerCopy(std::string_view str);
106
Alexander Hansen5df916f2025-09-26 10:31:36 -0400107template <typename T>
108std::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}