blob: bd0d64bd55c2dfb156b81dfbd070535a69cd7a85 [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>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050027#include <regex>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000028#include <sstream>
Nan Zhou80d37e72022-06-21 17:46:14 +000029#include <string>
30#include <tuple>
Nan Zhoud5c80ad2022-07-11 01:16:31 +000031#include <utility>
Ed Tanous40681292022-02-22 10:11:43 -080032#include <variant>
Nan Zhou80d37e72022-06-21 17:46:14 +000033#include <vector>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050034
Nan Zhoud5c80ad2022-07-11 01:16:31 +000035// IWYU pragma: no_include <stddef.h>
36// IWYU pragma: no_include <stdint.h>
37// IWYU pragma: no_include <boost/system/detail/error_code.hpp>
38
James Feist5b4aa862018-08-16 14:07:01 -070039namespace dbus
40{
41
42namespace utility
43{
44
Ed Tanousd1a64812021-12-13 12:14:05 -080045// clang-format off
Ed Tanous40681292022-02-22 10:11:43 -080046using DbusVariantType = std::variant<
Ed Tanousd1a64812021-12-13 12:14:05 -080047 std::vector<std::tuple<std::string, std::string, std::string>>,
48 std::vector<std::string>,
49 std::vector<double>,
50 std::string,
51 int64_t,
52 uint64_t,
53 double,
54 int32_t,
55 uint32_t,
56 int16_t,
57 uint16_t,
58 uint8_t,
59 bool,
Ed Tanousd1a64812021-12-13 12:14:05 -080060 sdbusplus::message::unix_fd,
61 std::vector<uint32_t>,
62 std::vector<uint16_t>,
63 sdbusplus::message::object_path,
64 std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>,
65 std::vector<std::tuple<std::string, std::string>>,
66 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
67 std::vector<std::tuple<uint32_t, size_t>>,
68 std::vector<std::tuple<sdbusplus::message::object_path, std::string,
69 std::string, std::string>>
70 >;
James Feist5b4aa862018-08-16 14:07:01 -070071
Ed Tanousd1a64812021-12-13 12:14:05 -080072// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080073using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
74using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010075using ManagedObjectType =
76 std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070077
Shantappa Teekappanavar5df6eda2022-01-18 12:29:28 -060078// Map of service name to list of interfaces
79using MapperServiceMap =
80 std::vector<std::pair<std::string, std::vector<std::string>>>;
81
82// Map of object paths to MapperServiceMaps
83using MapperGetSubTreeResponse =
84 std::vector<std::pair<std::string, MapperServiceMap>>;
85
Ed Tanousb9d36b42022-02-26 21:42:46 -080086using MapperGetObject =
87 std::vector<std::pair<std::string, std::vector<std::string>>>;
88
89using MapperGetAncestorsResponse = std::vector<
90 std::pair<std::string,
91 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
92
93using MapperGetSubTreePathsResponse = std::vector<std::string>;
94
James Feist5b4aa862018-08-16 14:07:01 -070095inline void escapePathForDbus(std::string& path)
96{
97 const std::regex reg("[^A-Za-z0-9_/]");
98 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
99}
100
101// gets the string N strings deep into a path
102// i.e. /0th/1st/2nd/3rd
103inline bool getNthStringFromPath(const std::string& path, int index,
104 std::string& result)
105{
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530106 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -0700107 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530108 return false;
109 }
110
111 std::filesystem::path p1(path);
112 int count = -1;
113 for (auto const& element : p1)
114 {
115 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -0700116 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530117 ++count;
James Feist5b4aa862018-08-16 14:07:01 -0700118 if (count == index)
119 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530120 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -0700121 break;
122 }
123 }
124 }
Ed Tanousdcf2ebc2022-01-25 10:07:45 -0800125 return count >= index;
James Feist5b4aa862018-08-16 14:07:01 -0700126}
127
Ratan Gupta22c33712019-05-03 21:50:28 +0530128template <typename Callback>
129inline void checkDbusPathExists(const std::string& path, Callback&& callback)
130{
Ratan Gupta22c33712019-05-03 21:50:28 +0530131 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800132 [callback{std::forward<Callback>(callback)}](
133 const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -0800134 const dbus::utility::MapperGetObject& objectNames) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700135 callback(!ec && !objectNames.empty());
Ratan Gupta22c33712019-05-03 21:50:28 +0530136 },
137 "xyz.openbmc_project.ObjectMapper",
138 "/xyz/openbmc_project/object_mapper",
139 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
140 std::array<std::string, 0>());
141}
142
James Feist5b4aa862018-08-16 14:07:01 -0700143} // namespace utility
144} // namespace dbus