blob: e953a9f0ebaad44f5c07acd4f7684b3586f19bf2 [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
Nan Zhoud5c80ad2022-07-11 01:16:31 +000020#include <boost/system/error_code.hpp> // IWYU pragma: keep
21#include <sdbusplus/message/native_types.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070022
Nan Zhou80d37e72022-06-21 17:46:14 +000023#include <array>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000024#include <cstddef>
25#include <cstdint>
Manojkiran Eda17a897d2020-09-12 15:31:58 +053026#include <filesystem>
George Liu21384832022-11-09 11:21:15 +080027#include <functional>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050028#include <regex>
George Liu21384832022-11-09 11:21:15 +080029#include <span>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000030#include <sstream>
Nan Zhou80d37e72022-06-21 17:46:14 +000031#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080032#include <string_view>
Nan Zhou80d37e72022-06-21 17:46:14 +000033#include <tuple>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000034#include <utility>
Ed Tanous40681292022-02-22 10:11:43 -080035#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000036#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050037
Nan Zhoud5c80ad2022-07-11 01:16:31 +000038// IWYU pragma: no_include <stddef.h>
39// IWYU pragma: no_include <stdint.h>
40// IWYU pragma: no_include <boost/system/detail/error_code.hpp>
41
James Feist5b4aa862018-08-16 14:07:01 -070042namespace dbus
43{
44
45namespace utility
46{
47
Ed Tanousd1a64812021-12-13 12:14:05 -080048// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080049using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080050 std::vector<std::tuple<std::string, std::string, std::string>>,
51 std::vector<std::string>,
52 std::vector<double>,
53 std::string,
54 int64_t,
55 uint64_t,
56 double,
57 int32_t,
58 uint32_t,
59 int16_t,
60 uint16_t,
61 uint8_t,
62 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080063 sdbusplus::message::unix_fd,
64 std::vector<uint32_t>,
65 std::vector<uint16_t>,
66 sdbusplus::message::object_path,
67 std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>,
68 std::vector<std::tuple<std::string, std::string>>,
69 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
70 std::vector<std::tuple<uint32_t, size_t>>,
71 std::vector<std::tuple<sdbusplus::message::object_path, std::string,
72 std::string, std::string>>
73 >;
James Feist5b4aa862018-08-16 14:07:01 -070074
Ed Tanousd1a64812021-12-13 12:14:05 -080075// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080076using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
77using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010078using ManagedObjectType =
79 std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070080
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060081// Map of service name to list of interfaces
82using MapperServiceMap =
83 std::vector<std::pair<std::string, std::vector<std::string>>>;
84
85// Map of object paths to MapperServiceMaps
86using MapperGetSubTreeResponse =
87 std::vector<std::pair<std::string, MapperServiceMap>>;
88
Ed Tanousb9d36b42022-02-26 21:42:46 -080089using MapperGetObject =
90 std::vector<std::pair<std::string, std::vector<std::string>>>;
91
92using MapperGetAncestorsResponse = std::vector<
93 std::pair<std::string,
94 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
95
96using MapperGetSubTreePathsResponse = std::vector<std::string>;
97
James Feist5b4aa862018-08-16 14:07:01 -070098inline void escapePathForDbus(std::string& path)
99{
100 const std::regex reg("[^A-Za-z0-9_/]");
101 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
102}
103
104// gets the string N strings deep into a path
105// i.e. /0th/1st/2nd/3rd
106inline bool getNthStringFromPath(const std::string& path, int index,
107 std::string& result)
108{
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530109 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -0700110 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530111 return false;
112 }
113
114 std::filesystem::path p1(path);
115 int count = -1;
116 for (auto const& element : p1)
117 {
118 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -0700119 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530120 ++count;
James Feist5b4aa862018-08-16 14:07:01 -0700121 if (count == index)
122 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530123 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700124 break;
125 }
126 }
127 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800128 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700129}
130
Ratan Gupta22c33712019-05-03 21:50:28 +0530131template <typename Callback>
132inline void checkDbusPathExists(const std::string& path, Callback&& callback)
133{
Ratan Gupta22c33712019-05-03 21:50:28 +0530134 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800135 [callback{std::forward<Callback>(callback)}](
136 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800137 const dbus::utility::MapperGetObject& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700138 callback(!ec && !objectNames.empty());
Ratan Gupta22c33712019-05-03 21:50:28 +0530139 },
140 "xyz.openbmc_project.ObjectMapper",
141 "/xyz/openbmc_project/object_mapper",
142 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
143 std::array<std::string, 0>());
144}
145
George Liu21384832022-11-09 11:21:15 +0800146inline void
George Liue99073f2022-12-09 11:06:16 +0800147 getSubTree(const std::string& path, int32_t depth,
148 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800149 std::function<void(const boost::system::error_code&,
150 const MapperGetSubTreeResponse&)>&& callback)
151{
152 crow::connections::systemBus->async_method_call(
153 [callback{std::move(callback)}](
154 const boost::system::error_code ec,
155 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
156 "xyz.openbmc_project.ObjectMapper",
157 "/xyz/openbmc_project/object_mapper",
George Liue99073f2022-12-09 11:06:16 +0800158 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, depth,
159 interfaces);
George Liu21384832022-11-09 11:21:15 +0800160}
161
162inline void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800163 const std::string& path, int32_t depth,
164 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800165 std::function<void(const boost::system::error_code&,
166 const MapperGetSubTreePathsResponse&)>&& callback)
167{
168 crow::connections::systemBus->async_method_call(
169 [callback{std::move(callback)}](
170 const boost::system::error_code ec,
171 const MapperGetSubTreePathsResponse& subtreePaths) {
172 callback(ec, subtreePaths);
173 },
174 "xyz.openbmc_project.ObjectMapper",
175 "/xyz/openbmc_project/object_mapper",
George Liu7a1dbc42022-12-07 16:03:22 +0800176 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", path, depth,
George Liu21384832022-11-09 11:21:15 +0800177 interfaces);
178}
179
George Liu2b731192023-01-11 16:27:13 +0800180inline void
181 getDbusObject(const std::string& path,
182 std::span<const std::string_view> interfaces,
183 std::function<void(const boost::system::error_code&,
184 const MapperGetObject&)>&& callback)
185{
186 crow::connections::systemBus->async_method_call(
187 [callback{std::move(callback)}](const boost::system::error_code& ec,
188 const MapperGetObject& object) {
189 callback(ec, object);
190 },
191 "xyz.openbmc_project.ObjectMapper",
192 "/xyz/openbmc_project/object_mapper",
193 "xyz.openbmc_project.ObjectMapper", "GetObject", path, interfaces);
194}
195
James Feist5b4aa862018-08-16 14:07:01 -0700196} // namespace utility
197} // namespace dbus