blob: 105330c3602ca98b7766cefffd5f044770f9450e [file] [log] [blame]
James Feist3cb5fec2018-01-23 14:41:51 -08001/*
2// Copyright (c) 2017 Intel Corporation
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15*/
Brad Bishope45d8c72022-05-25 15:12:53 -040016/// \file utils.hpp
James Feist3cb5fec2018-01-23 14:41:51 -080017
18#pragma once
James Feist481c5d52019-08-13 14:40:40 -070019
James Feist1df06a42019-04-11 14:23:04 -070020#include <boost/container/flat_map.hpp>
James Feistb4383f42018-08-06 16:54:10 -070021#include <nlohmann/json.hpp>
James Feist1df06a42019-04-11 14:23:04 -070022#include <sdbusplus/asio/connection.hpp>
James Feist68500ff2018-08-08 15:40:42 -070023#include <sdbusplus/exception.hpp>
James Feist3cb5fec2018-01-23 14:41:51 -080024
James Feist8c505da2020-05-28 10:06:33 -070025#include <filesystem>
Johnathan Mantey9b867872020-10-13 15:00:51 -070026
Andrew Jefferyeab49292022-04-05 14:42:20 +093027using DBusValueVariant =
James Feist481c5d52019-08-13 14:40:40 -070028 std::variant<std::string, int64_t, uint64_t, double, int32_t, uint32_t,
Brad Bishopcd1868e2020-08-28 17:58:52 -040029 int16_t, uint16_t, uint8_t, bool, std::vector<uint8_t>>;
Andrew Jeffery1983d2f2022-04-05 14:55:13 +093030using DBusInterface = boost::container::flat_map<std::string, DBusValueVariant>;
31using DBusObject = boost::container::flat_map<std::string, DBusInterface>;
32using MapperGetSubTreeResponse =
33 boost::container::flat_map<std::string, DBusObject>;
James Feist481c5d52019-08-13 14:40:40 -070034
James Feista465ccc2019-02-08 12:51:01 -080035bool findFiles(const std::filesystem::path& dirPath,
36 const std::string& matchString,
37 std::vector<std::filesystem::path>& foundPaths);
Andrew Jefferya9c58922021-06-01 09:28:59 +093038bool findFiles(const std::vector<std::filesystem::path>&& dirPaths,
39 const std::string& matchString,
40 std::vector<std::filesystem::path>& foundPaths);
James Feistb4383f42018-08-06 16:54:10 -070041
Nikhil Potaded8884f12019-03-27 13:27:13 -070042bool getI2cDevicePaths(
43 const std::filesystem::path& dirPath,
44 boost::container::flat_map<size_t, std::filesystem::path>& busPaths);
45
James Feist68500ff2018-08-08 15:40:42 -070046struct DBusInternalError final : public sdbusplus::exception_t
47{
James Feista465ccc2019-02-08 12:51:01 -080048 const char* name() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070049 {
50 return "org.freedesktop.DBus.Error.Failed";
Patrick Williamscca78562021-09-08 15:43:23 -050051 }
James Feista465ccc2019-02-08 12:51:01 -080052 const char* description() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070053 {
54 return "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050055 }
James Feista465ccc2019-02-08 12:51:01 -080056 const char* what() const noexcept override
James Feist68500ff2018-08-08 15:40:42 -070057 {
58 return "org.freedesktop.DBus.Error.Failed: "
59 "internal error";
Patrick Williamscca78562021-09-08 15:43:23 -050060 }
61
62 int get_errno() const noexcept override
63 {
64 return EACCES;
65 }
James Feist68500ff2018-08-08 15:40:42 -070066};
James Feist1df06a42019-04-11 14:23:04 -070067
James Feist1ffa4a42020-04-22 18:27:17 -070068inline bool deviceHasLogging(const nlohmann::json& json)
69{
70 auto logging = json.find("Logging");
71 if (logging != json.end())
72 {
Ed Tanous3013fb42022-07-09 08:27:06 -070073 const auto* ptr = logging->get_ptr<const std::string*>();
74 if (ptr != nullptr)
James Feist1ffa4a42020-04-22 18:27:17 -070075 {
76 if (*ptr == "Off")
77 {
78 return false;
79 }
80 }
81 }
82 return true;
83}
Brad Bishop3cb8a602020-08-25 17:40:54 -040084
85/// \brief Match a Dbus property against a probe statement.
86/// \param probe the probe statement to match against.
87/// \param dbusValue the property value being matched to a probe.
88/// \return true if the dbusValue matched the probe otherwise false.
Andrew Jefferyeab49292022-04-05 14:42:20 +093089bool matchProbe(const nlohmann::json& probe, const DBusValueVariant& dbusValue);