blob: 3a3a083e1ba1faba5471e10749f3c684427fa3a5 [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>;
James Feist481c5d52019-08-13 14:40:40 -070021
James Feista465ccc2019-02-08 12:51:01 -080022bool findFiles(const std::filesystem::path& dirPath,
23 const std::string& matchString,
24 std::vector<std::filesystem::path>& foundPaths);
Andrew Jefferya9c58922021-06-01 09:28:59 +093025bool findFiles(const std::vector<std::filesystem::path>&& dirPaths,
26 const std::string& matchString,
27 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070028
Nikhil Potaded8884f12019-03-27 13:27:13 -070029bool getI2cDevicePaths(
30 const std::filesystem::path& dirPath,
31 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
32
James Feist68500ff2018-08-08 15:40:42 -070033struct DBusInternalError final : public sdbusplus::exception_t
34{
James Feista465ccc2019-02-08 12:51:01 -080035 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070036 {
37 return "org.freedesktop.DBus.Error.Failed";
Patrick Williamscca78562021-09-08 15:43:23 -050038 }
James Feista465ccc2019-02-08 12:51:01 -080039 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070040 {
41 return "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050042 }
James Feista465ccc2019-02-08 12:51:01 -080043 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070044 {
45 return "org.freedesktop.DBus.Error.Failed: "
46 "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050047 }
48
49 int get_errno() const noexcept override
50 {
51 return EACCES;
52 }
James Feist68500ff2018-08-08 15:40:42 -070053};
James Feist1df06a42019-04-11 14:23:04 -070054
James Feist1ffa4a42020-04-22 18:27:17 -070055inline bool deviceHasLogging(const nlohmann::json& json)
56{
57 auto logging = json.find("Logging");
58 if (logging != json.end())
59 {
Ed Tanous3013fb42022-07-09 08:27:06 -070060 const auto* ptr = logging->get_ptr<const std::string*>();
61 if (ptr != nullptr)
James Feist1ffa4a42020-04-22 18:27:17 -070062 {
63 if (*ptr == "Off")
64 {
65 return false;
66 }
67 }
68 }
69 return true;
70}
Brad Bishop3cb8a602020-08-25 17:40:54 -040071
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.
Andrew Jefferyeab49292022-04-05 14:42:20 +093076bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);
Alexander Hansen5df916f2025-09-26 10:31:36 -040077
78template <typename T>
79std::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}