blob: 4565db05a775b98ae9574e94ed61b0ef3d66c2bc [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 Feist1df06a42019-04-11 14:23:04 -07006#include <boost/container/flat_map.hpp>
James Feistb4383f42018-08-06 16:54:10 -07007#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -07008#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -07009#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080010
Alexander Hansen5df916f2025-09-26 10:31:36 -040011#include <charconv>
James Feist8c505da2020-05-28 10:06:33 -070012#include <filesystem>
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>>;
Andrew Jeffery1983d2f2022-04-05 14:55:13 +093017using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>;
18using DBusObject = boost::container::flat_map<std::string, DBusInterface>;
19using MapperGetSubTreeResponse =
20 boost::container::flat_map<std::string, DBusObject>;
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);
Andrew Jefferya9c58922021-06-01 09:28:59 +093027bool findFiles(const std::vector<std::filesystem::path>&& dirPaths,
28 const std::string& matchString,
29 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070030
Nikhil Potaded8884f12019-03-27 13:27:13 -070031bool getI2cDevicePaths(
32 const std::filesystem::path& dirPath,
33 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
34
James Feist68500ff2018-08-08 15:40:42 -070035struct DBusInternalError final : public sdbusplus::exception_t
36{
James Feista465ccc2019-02-08 12:51:01 -080037 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070038 {
39 return "org.freedesktop.DBus.Error.Failed";
Patrick Williamscca78562021-09-08 15:43:23 -050040 }
James Feista465ccc2019-02-08 12:51:01 -080041 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070042 {
43 return "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050044 }
James Feista465ccc2019-02-08 12:51:01 -080045 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070046 {
47 return "org.freedesktop.DBus.Error.Failed: "
48 "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050049 }
50
51 int get_errno() const noexcept override
52 {
53 return EACCES;
54 }
James Feist68500ff2018-08-08 15:40:42 -070055};
James Feist1df06a42019-04-11 14:23:04 -070056
James Feist1ffa4a42020-04-22 18:27:17 -070057inline bool deviceHasLogging(const nlohmann::json& json)
58{
59 auto logging = json.find("Logging");
60 if (logging != json.end())
61 {
Ed Tanous3013fb42022-07-09 08:27:06 -070062 const auto* ptr = logging->get_ptr<const std::string*>();
63 if (ptr != nullptr)
James Feist1ffa4a42020-04-22 18:27:17 -070064 {
65 if (*ptr == "Off")
66 {
67 return false;
68 }
69 }
70 }
71 return true;
72}
Brad Bishop3cb8a602020-08-25 17:40:54 -040073
74/// \brief Match a Dbus property against a probe statement.
75/// \param probe the probe statement to match against.
76/// \param dbusValue the property value being matched to a probe.
77/// \return true if the dbusValue matched the probe otherwise false.
Andrew Jefferyeab49292022-04-05 14:42:20 +093078bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);
Alexander Hansen5df916f2025-09-26 10:31:36 -040079
Ed Tanousc5a2af92025-08-25 08:58:25 +080080inline char asciiToLower(char c)
81{
82 // Converts a character to lower case without relying on std::locale
83 if ('A' <= c && c <= 'Z')
84 {
85 c -= static_cast<char>('A' - 'a');
86 }
87 return c;
88}
89
90template <typename T>
91auto iFindFirst(T&& str, std::string_view sub)
92{
93 return std::ranges::search(str, sub, [](char a, char b) {
94 return asciiToLower(a) == asciiToLower(b);
95 });
96}
George Liuce8d1d02025-08-25 08:58:25 +080097
Alexander Hansen5df916f2025-09-26 10:31:36 -040098template <typename T>
99std::from_chars_result fromCharsWrapper(const std::string_view& str, T& out,
100 bool& fullMatch, int base = 10)
101{
102 auto result = std::from_chars(
103 str.data(), std::next(str.begin(), str.size()), out, base);
104
105 fullMatch = result.ptr == std::next(str.begin(), str.size());
106
107 return result;
108}