blob: 481b33dea97cef569bc7bc22c3966249f1a267c6 [file] [log] [blame]
James Feist5b4aa862018-08-16 14:07:01 -07001/*
2 // Copyright (c) 2018 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 */
16#pragma once
17
James Feist5b4aa862018-08-16 14:07:01 -070018#include <sdbusplus/message.hpp>
19
Manojkiran Eda17a897d2020-09-12 15:31:58 +053020#include <filesystem>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050021#include <regex>
Ed Tanous40681292022-02-22 10:11:43 -080022#include <variant>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050023
James Feist5b4aa862018-08-16 14:07:01 -070024namespace dbus
25{
26
27namespace utility
28{
29
Ed Tanousd1a64812021-12-13 12:14:05 -080030// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080031using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080032 std::vector<std::tuple<std::string, std::string, std::string>>,
33 std::vector<std::string>,
34 std::vector<double>,
35 std::string,
36 int64_t,
37 uint64_t,
38 double,
39 int32_t,
40 uint32_t,
41 int16_t,
42 uint16_t,
43 uint8_t,
44 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080045 sdbusplus::message::unix_fd,
46 std::vector<uint32_t>,
47 std::vector<uint16_t>,
48 sdbusplus::message::object_path,
49 std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>,
50 std::vector<std::tuple<std::string, std::string>>,
51 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
52 std::vector<std::tuple<uint32_t, size_t>>,
53 std::vector<std::tuple<sdbusplus::message::object_path, std::string,
54 std::string, std::string>>
55 >;
James Feist5b4aa862018-08-16 14:07:01 -070056
Ed Tanousd1a64812021-12-13 12:14:05 -080057// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080058using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
59using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010060using ManagedObjectType =
61 std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070062
James Feist73df0db2019-03-25 15:29:35 -070063using ManagedItem = std::pair<
64 sdbusplus::message::object_path,
Ed Tanous630c3172022-02-22 12:02:23 -080065 std::vector<std::pair<
66 std::string, std::vector<std::pair<std::string, DbusVariantType>>>>>;
James Feist73df0db2019-03-25 15:29:35 -070067
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060068// Map of service name to list of interfaces
69using MapperServiceMap =
70 std::vector<std::pair<std::string, std::vector<std::string>>>;
71
72// Map of object paths to MapperServiceMaps
73using MapperGetSubTreeResponse =
74 std::vector<std::pair<std::string, MapperServiceMap>>;
75
James Feist5b4aa862018-08-16 14:07:01 -070076inline void escapePathForDbus(std::string& path)
77{
78 const std::regex reg("[^A-Za-z0-9_/]");
79 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
80}
81
82// gets the string N strings deep into a path
83// i.e. /0th/1st/2nd/3rd
84inline bool getNthStringFromPath(const std::string& path, int index,
85 std::string& result)
86{
Manojkiran Eda17a897d2020-09-12 15:31:58 +053087 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -070088 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +053089 return false;
90 }
91
92 std::filesystem::path p1(path);
93 int count = -1;
94 for (auto const& element : p1)
95 {
96 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -070097 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +053098 ++count;
James Feist5b4aa862018-08-16 14:07:01 -070099 if (count == index)
100 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530101 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700102 break;
103 }
104 }
105 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800106 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700107}
108
Ratan Gupta22c33712019-05-03 21:50:28 +0530109template <typename Callback>
110inline void checkDbusPathExists(const std::string& path, Callback&& callback)
111{
112 using GetObjectType =
113 std::vector<std::pair<std::string, std::vector<std::string>>>;
114
115 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800116 [callback{std::forward<Callback>(callback)}](
117 const boost::system::error_code ec,
118 const GetObjectType& objectNames) {
Ed Tanous26f69762022-01-25 09:49:11 -0800119 callback(!ec && !objectNames.empty());
Ratan Gupta22c33712019-05-03 21:50:28 +0530120 },
121 "xyz.openbmc_project.ObjectMapper",
122 "/xyz/openbmc_project/object_mapper",
123 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
124 std::array<std::string, 0>());
125}
126
James Feist5b4aa862018-08-16 14:07:01 -0700127} // namespace utility
128} // namespace dbus