blob: ea0610126ab506eec35c2567729caf9ad7904c5e [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
George Liua4eb7612023-02-11 15:49:46 +080021#include <sdbusplus/asio/property.hpp>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000022#include <sdbusplus/message/native_types.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070023
Nan Zhou80d37e72022-06-21 17:46:14 +000024#include <array>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000025#include <cstddef>
26#include <cstdint>
Manojkiran Eda17a897d2020-09-12 15:31:58 +053027#include <filesystem>
George Liu21384832022-11-09 11:21:15 +080028#include <functional>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050029#include <regex>
George Liu21384832022-11-09 11:21:15 +080030#include <span>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000031#include <sstream>
Nan Zhou80d37e72022-06-21 17:46:14 +000032#include <string>
George Liu7a1dbc42022-12-07 16:03:22 +080033#include <string_view>
Nan Zhou80d37e72022-06-21 17:46:14 +000034#include <tuple>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000035#include <utility>
Ed Tanous40681292022-02-22 10:11:43 -080036#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000037#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050038
Nan Zhoud5c80ad2022-07-11 01:16:31 +000039// IWYU pragma: no_include <stddef.h>
40// IWYU pragma: no_include <stdint.h>
41// IWYU pragma: no_include <boost/system/detail/error_code.hpp>
42
James Feist5b4aa862018-08-16 14:07:01 -070043namespace dbus
44{
45
46namespace utility
47{
48
Ed Tanousd1a64812021-12-13 12:14:05 -080049// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080050using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080051 std::vector<std::tuple<std::string, std::string, std::string>>,
52 std::vector<std::string>,
53 std::vector<double>,
54 std::string,
55 int64_t,
56 uint64_t,
57 double,
58 int32_t,
59 uint32_t,
60 int16_t,
61 uint16_t,
62 uint8_t,
63 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080064 sdbusplus::message::unix_fd,
65 std::vector<uint32_t>,
66 std::vector<uint16_t>,
67 sdbusplus::message::object_path,
68 std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>,
69 std::vector<std::tuple<std::string, std::string>>,
70 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
71 std::vector<std::tuple<uint32_t, size_t>>,
72 std::vector<std::tuple<sdbusplus::message::object_path, std::string,
73 std::string, std::string>>
74 >;
James Feist5b4aa862018-08-16 14:07:01 -070075
Ed Tanousd1a64812021-12-13 12:14:05 -080076// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080077using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
78using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010079using ManagedObjectType =
80 std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070081
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060082// Map of service name to list of interfaces
83using MapperServiceMap =
84 std::vector<std::pair<std::string, std::vector<std::string>>>;
85
86// Map of object paths to MapperServiceMaps
87using MapperGetSubTreeResponse =
88 std::vector<std::pair<std::string, MapperServiceMap>>;
89
Ed Tanousb9d36b42022-02-26 21:42:46 -080090using MapperGetObject =
91 std::vector<std::pair<std::string, std::vector<std::string>>>;
92
93using MapperGetAncestorsResponse = std::vector<
94 std::pair<std::string,
95 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
96
97using MapperGetSubTreePathsResponse = std::vector<std::string>;
98
George Liua4eb7612023-02-11 15:49:46 +080099using MapperEndPoints = std::vector<std::string>;
100
James Feist5b4aa862018-08-16 14:07:01 -0700101inline void escapePathForDbus(std::string& path)
102{
103 const std::regex reg("[^A-Za-z0-9_/]");
104 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
105}
106
107// gets the string N strings deep into a path
108// i.e. /0th/1st/2nd/3rd
109inline bool getNthStringFromPath(const std::string& path, int index,
110 std::string& result)
111{
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530112 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -0700113 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530114 return false;
115 }
116
117 std::filesystem::path p1(path);
118 int count = -1;
119 for (auto const& element : p1)
120 {
121 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -0700122 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530123 ++count;
James Feist5b4aa862018-08-16 14:07:01 -0700124 if (count == index)
125 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530126 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700127 break;
128 }
129 }
130 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800131 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700132}
133
Ratan Gupta22c33712019-05-03 21:50:28 +0530134template <typename Callback>
135inline void checkDbusPathExists(const std::string& path, Callback&& callback)
136{
Ratan Gupta22c33712019-05-03 21:50:28 +0530137 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800138 [callback{std::forward<Callback>(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800139 const boost::system::error_code& ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800140 const dbus::utility::MapperGetObject& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700141 callback(!ec && !objectNames.empty());
Ratan Gupta22c33712019-05-03 21:50:28 +0530142 },
143 "xyz.openbmc_project.ObjectMapper",
144 "/xyz/openbmc_project/object_mapper",
145 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
146 std::array<std::string, 0>());
147}
148
George Liu21384832022-11-09 11:21:15 +0800149inline void
George Liue99073f2022-12-09 11:06:16 +0800150 getSubTree(const std::string& path, int32_t depth,
151 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800152 std::function<void(const boost::system::error_code&,
153 const MapperGetSubTreeResponse&)>&& callback)
154{
155 crow::connections::systemBus->async_method_call(
156 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800157 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800158 const MapperGetSubTreeResponse& subtree) { callback(ec, subtree); },
159 "xyz.openbmc_project.ObjectMapper",
160 "/xyz/openbmc_project/object_mapper",
George Liue99073f2022-12-09 11:06:16 +0800161 "xyz.openbmc_project.ObjectMapper", "GetSubTree", path, depth,
162 interfaces);
George Liu21384832022-11-09 11:21:15 +0800163}
164
165inline void getSubTreePaths(
George Liu7a1dbc42022-12-07 16:03:22 +0800166 const std::string& path, int32_t depth,
167 std::span<const std::string_view> interfaces,
George Liu21384832022-11-09 11:21:15 +0800168 std::function<void(const boost::system::error_code&,
169 const MapperGetSubTreePathsResponse&)>&& callback)
170{
171 crow::connections::systemBus->async_method_call(
172 [callback{std::move(callback)}](
Ed Tanous5e7e2dc2023-02-16 10:37:01 -0800173 const boost::system::error_code& ec,
George Liu21384832022-11-09 11:21:15 +0800174 const MapperGetSubTreePathsResponse& subtreePaths) {
175 callback(ec, subtreePaths);
176 },
177 "xyz.openbmc_project.ObjectMapper",
178 "/xyz/openbmc_project/object_mapper",
George Liu7a1dbc42022-12-07 16:03:22 +0800179 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", path, depth,
George Liu21384832022-11-09 11:21:15 +0800180 interfaces);
181}
182
George Liu2b731192023-01-11 16:27:13 +0800183inline void
184 getDbusObject(const std::string& path,
185 std::span<const std::string_view> interfaces,
186 std::function<void(const boost::system::error_code&,
187 const MapperGetObject&)>&& callback)
188{
189 crow::connections::systemBus->async_method_call(
190 [callback{std::move(callback)}](const boost::system::error_code& ec,
191 const MapperGetObject& object) {
192 callback(ec, object);
193 },
194 "xyz.openbmc_project.ObjectMapper",
195 "/xyz/openbmc_project/object_mapper",
196 "xyz.openbmc_project.ObjectMapper", "GetObject", path, interfaces);
197}
198
George Liua4eb7612023-02-11 15:49:46 +0800199inline void getAssociationEndPoints(
200 const std::string& path,
201 std::function<void(const boost::system::error_code&,
202 const MapperEndPoints&)>&& callback)
203{
204 sdbusplus::asio::getProperty<MapperEndPoints>(
205 *crow::connections::systemBus, "xyz.openbmc_project.ObjectMapper", path,
206 "xyz.openbmc_project.Association", "endpoints", std::move(callback));
207}
208
James Feist5b4aa862018-08-16 14:07:01 -0700209} // namespace utility
210} // namespace dbus