blob: d9d59f7c1f33b89dc22f983a3561b88e0c6f8e08 [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
Nan Zhou80d37e72022-06-21 17:46:14 +000018#include "dbus_singleton.hpp"
19
James Feist5b4aa862018-08-16 14:07:01 -070020#include <sdbusplus/message.hpp>
21
Nan Zhou80d37e72022-06-21 17:46:14 +000022#include <array>
Manojkiran Eda17a897d2020-09-12 15:31:58 +053023#include <filesystem>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050024#include <regex>
Nan Zhou80d37e72022-06-21 17:46:14 +000025#include <string>
26#include <tuple>
Ed Tanous40681292022-02-22 10:11:43 -080027#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000028#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029
James Feist5b4aa862018-08-16 14:07:01 -070030namespace dbus
31{
32
33namespace utility
34{
35
Ed Tanousd1a64812021-12-13 12:14:05 -080036// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080037using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080038 std::vector<std::tuple<std::string, std::string, std::string>>,
39 std::vector<std::string>,
40 std::vector<double>,
41 std::string,
42 int64_t,
43 uint64_t,
44 double,
45 int32_t,
46 uint32_t,
47 int16_t,
48 uint16_t,
49 uint8_t,
50 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080051 sdbusplus::message::unix_fd,
52 std::vector<uint32_t>,
53 std::vector<uint16_t>,
54 sdbusplus::message::object_path,
55 std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>,
56 std::vector<std::tuple<std::string, std::string>>,
57 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
58 std::vector<std::tuple<uint32_t, size_t>>,
59 std::vector<std::tuple<sdbusplus::message::object_path, std::string,
60 std::string, std::string>>
61 >;
James Feist5b4aa862018-08-16 14:07:01 -070062
Ed Tanousd1a64812021-12-13 12:14:05 -080063// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080064using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
65using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010066using ManagedObjectType =
67 std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070068
James Feist73df0db2019-03-25 15:29:35 -070069using ManagedItem = std::pair<
70 sdbusplus::message::object_path,
Ed Tanous630c3172022-02-22 12:02:23 -080071 std::vector<std::pair<
72 std::string, std::vector<std::pair<std::string, DbusVariantType>>>>>;
James Feist73df0db2019-03-25 15:29:35 -070073
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060074// Map of service name to list of interfaces
75using MapperServiceMap =
76 std::vector<std::pair<std::string, std::vector<std::string>>>;
77
78// Map of object paths to MapperServiceMaps
79using MapperGetSubTreeResponse =
80 std::vector<std::pair<std::string, MapperServiceMap>>;
81
Ed Tanousb9d36b42022-02-26 21:42:46 -080082using MapperGetObject =
83 std::vector<std::pair<std::string, std::vector<std::string>>>;
84
85using MapperGetAncestorsResponse = std::vector<
86 std::pair<std::string,
87 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
88
89using MapperGetSubTreePathsResponse = std::vector<std::string>;
90
James Feist5b4aa862018-08-16 14:07:01 -070091inline void escapePathForDbus(std::string& path)
92{
93 const std::regex reg("[^A-Za-z0-9_/]");
94 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
95}
96
97// gets the string N strings deep into a path
98// i.e. /0th/1st/2nd/3rd
99inline bool getNthStringFromPath(const std::string& path, int index,
100 std::string& result)
101{
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530102 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -0700103 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530104 return false;
105 }
106
107 std::filesystem::path p1(path);
108 int count = -1;
109 for (auto const& element : p1)
110 {
111 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -0700112 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530113 ++count;
James Feist5b4aa862018-08-16 14:07:01 -0700114 if (count == index)
115 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530116 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700117 break;
118 }
119 }
120 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800121 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700122}
123
Ratan Gupta22c33712019-05-03 21:50:28 +0530124template <typename Callback>
125inline void checkDbusPathExists(const std::string& path, Callback&& callback)
126{
Ratan Gupta22c33712019-05-03 21:50:28 +0530127 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800128 [callback{std::forward<Callback>(callback)}](
129 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800130 const dbus::utility::MapperGetObject& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700131 callback(!ec && !objectNames.empty());
Ratan Gupta22c33712019-05-03 21:50:28 +0530132 },
133 "xyz.openbmc_project.ObjectMapper",
134 "/xyz/openbmc_project/object_mapper",
135 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
136 std::array<std::string, 0>());
137}
138
James Feist5b4aa862018-08-16 14:07:01 -0700139} // namespace utility
140} // namespace dbus