blob: 872ca614e466bc9f4bc9bcc4f57093f393baa774 [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>
Ed Tanousd1a64812021-12-13 12:14:05 -080019#include <sdbusplus/utility/dedup_variant.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070020
Manojkiran Eda17a897d2020-09-12 15:31:58 +053021#include <filesystem>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050022#include <regex>
23
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
31using DbusVariantType = sdbusplus::utility::dedup_variant_t<
32 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,
45 size_t,
46 sdbusplus::message::unix_fd,
47 std::vector<uint32_t>,
48 std::vector<uint16_t>,
49 sdbusplus::message::object_path,
50 std::tuple<uint64_t, std::vector<std::tuple<std::string, std::string, double, uint64_t>>>,
51 std::vector<std::tuple<std::string, std::string>>,
52 std::vector<std::tuple<uint32_t, std::vector<uint32_t>>>,
53 std::vector<std::tuple<uint32_t, size_t>>,
54 std::vector<std::tuple<sdbusplus::message::object_path, std::string,
55 std::string, std::string>>
56 >;
James Feist5b4aa862018-08-16 14:07:01 -070057
Ed Tanousd1a64812021-12-13 12:14:05 -080058// clang-format on
Ed Tanous711ac7a2021-12-20 09:34:41 -080059using DBusPropertiesMap = std::vector<std::pair<std::string, DbusVariantType>>;
60using DBusInteracesMap = std::vector<std::pair<std::string, DBusPropertiesMap>>;
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010061using ManagedObjectType =
62 std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070063
James Feist73df0db2019-03-25 15:29:35 -070064using ManagedItem = std::pair<
65 sdbusplus::message::object_path,
66 boost::container::flat_map<
67 std::string, boost::container::flat_map<std::string, DbusVariantType>>>;
68
James Feist5b4aa862018-08-16 14:07:01 -070069inline void escapePathForDbus(std::string& path)
70{
71 const std::regex reg("[^A-Za-z0-9_/]");
72 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
73}
74
75// gets the string N strings deep into a path
76// i.e. /0th/1st/2nd/3rd
77inline bool getNthStringFromPath(const std::string& path, int index,
78 std::string& result)
79{
Manojkiran Eda17a897d2020-09-12 15:31:58 +053080 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -070081 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +053082 return false;
83 }
84
85 std::filesystem::path p1(path);
86 int count = -1;
87 for (auto const& element : p1)
88 {
89 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -070090 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +053091 ++count;
James Feist5b4aa862018-08-16 14:07:01 -070092 if (count == index)
93 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +053094 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -070095 break;
96 }
97 }
98 }
99 if (count < index)
100 {
101 return false;
102 }
Manojkiran Eda17a897d2020-09-12 15:31:58 +0530103
James Feist5b4aa862018-08-16 14:07:01 -0700104 return true;
105}
106
Ratan Gupta22c33712019-05-03 21:50:28 +0530107template <typename Callback>
108inline void checkDbusPathExists(const std::string& path, Callback&& callback)
109{
110 using GetObjectType =
111 std::vector<std::pair<std::string, std::vector<std::string>>>;
112
113 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800114 [callback{std::forward<Callback>(callback)}](
115 const boost::system::error_code ec,
116 const GetObjectType& objectNames) {
Ed Tanous81ce6092020-12-17 16:54:55 +0000117 callback(!ec && objectNames.size() != 0);
Ratan Gupta22c33712019-05-03 21:50:28 +0530118 },
119 "xyz.openbmc_project.ObjectMapper",
120 "/xyz/openbmc_project/object_mapper",
121 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
122 std::array<std::string, 0>());
123}
124
James Feist5b4aa862018-08-16 14:07:01 -0700125} // namespace utility
126} // namespace dbus