blob: 99270245ff44eebd2da110da0846c32892d25a80 [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>
19
Manojkiran Eda17a897d2020-09-12 15:31:58 +053020#include <filesystem>
Gunnar Mills1214b7e2020-06-04 10:11:30 -050021#include <regex>
22
James Feist5b4aa862018-08-16 14:07:01 -070023namespace dbus
24{
25
26namespace utility
27{
28
Ed Tanousabf2add2019-01-22 16:40:12 -080029using DbusVariantType =
30 std::variant<std::vector<std::tuple<std::string, std::string, std::string>>,
31 std::vector<std::string>, std::vector<double>, std::string,
32 int64_t, uint64_t, double, int32_t, uint32_t, int16_t,
33 uint16_t, uint8_t, bool>;
James Feist5b4aa862018-08-16 14:07:01 -070034
Zbigniew Kurzynski755a33c2020-02-28 14:06:37 +010035using DBusPropertiesMap =
36 boost::container::flat_map<std::string, DbusVariantType>;
37using DBusInteracesMap =
38 boost::container::flat_map<std::string, DBusPropertiesMap>;
39using ManagedObjectType =
40 std::vector<std::pair<sdbusplus::message::object_path, DBusInteracesMap>>;
James Feist5b4aa862018-08-16 14:07:01 -070041
James Feist73df0db2019-03-25 15:29:35 -070042using ManagedItem = std::pair<
43 sdbusplus::message::object_path,
44 boost::container::flat_map<
45 std::string, boost::container::flat_map<std::string, DbusVariantType>>>;
46
James Feist5b4aa862018-08-16 14:07:01 -070047inline void escapePathForDbus(std::string& path)
48{
49 const std::regex reg("[^A-Za-z0-9_/]");
50 std::regex_replace(path.begin(), path.begin(), path.end(), reg, "_");
51}
52
53// gets the string N strings deep into a path
54// i.e. /0th/1st/2nd/3rd
55inline bool getNthStringFromPath(const std::string& path, int index,
56 std::string& result)
57{
Manojkiran Eda17a897d2020-09-12 15:31:58 +053058 if (index < 0)
James Feist5b4aa862018-08-16 14:07:01 -070059 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +053060 return false;
61 }
62
63 std::filesystem::path p1(path);
64 int count = -1;
65 for (auto const& element : p1)
66 {
67 if (element.has_filename())
James Feist5b4aa862018-08-16 14:07:01 -070068 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +053069 ++count;
James Feist5b4aa862018-08-16 14:07:01 -070070 if (count == index)
71 {
Manojkiran Eda17a897d2020-09-12 15:31:58 +053072 result = element.stem().string();
James Feist5b4aa862018-08-16 14:07:01 -070073 break;
74 }
75 }
76 }
77 if (count < index)
78 {
79 return false;
80 }
Manojkiran Eda17a897d2020-09-12 15:31:58 +053081
James Feist5b4aa862018-08-16 14:07:01 -070082 return true;
83}
84
Ratan Gupta22c33712019-05-03 21:50:28 +053085template <typename Callback>
86inline void checkDbusPathExists(const std::string& path, Callback&& callback)
87{
88 using GetObjectType =
89 std::vector<std::pair<std::string, std::vector<std::string>>>;
90
91 crow::connections::systemBus->async_method_call(
92 [callback{std::move(callback)}](const boost::system::error_code ec,
Ed Tanous81ce6092020-12-17 16:54:55 +000093 const GetObjectType& objectNames) {
94 callback(!ec && objectNames.size() != 0);
Ratan Gupta22c33712019-05-03 21:50:28 +053095 },
96 "xyz.openbmc_project.ObjectMapper",
97 "/xyz/openbmc_project/object_mapper",
98 "xyz.openbmc_project.ObjectMapper", "GetObject", path,
99 std::array<std::string, 0>());
100}
101
James Feist5b4aa862018-08-16 14:07:01 -0700102} // namespace utility
103} // namespace dbus