1 /*
2 // Copyright (c) 2019 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 #ifndef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS
18 
19 #include <dbus_utility.hpp>
20 #include <sdbusplus/asio/property.hpp>
21 
22 #include <charconv>
23 
24 namespace redfish
25 {
26 
27 enum NetworkProtocolUnitStructFields
28 {
29     NET_PROTO_UNIT_NAME,
30     NET_PROTO_UNIT_DESC,
31     NET_PROTO_UNIT_LOAD_STATE,
32     NET_PROTO_UNIT_ACTIVE_STATE,
33     NET_PROTO_UNIT_SUB_STATE,
34     NET_PROTO_UNIT_DEVICE,
35     NET_PROTO_UNIT_OBJ_PATH,
36     NET_PROTO_UNIT_ALWAYS_0,
37     NET_PROTO_UNIT_ALWAYS_EMPTY,
38     NET_PROTO_UNIT_ALWAYS_ROOT_PATH
39 };
40 
41 enum NetworkProtocolListenResponseElements
42 {
43     NET_PROTO_LISTEN_TYPE,
44     NET_PROTO_LISTEN_STREAM
45 };
46 
47 /**
48  * @brief D-Bus Unit structure returned in array from ListUnits Method
49  */
50 using UnitStruct =
51     std::tuple<std::string, std::string, std::string, std::string, std::string,
52                std::string, sdbusplus::message::object_path, uint32_t,
53                std::string, sdbusplus::message::object_path>;
54 
55 template <typename CallbackFunc>
56 void getMainChassisId(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
57                       CallbackFunc&& callback)
58 {
59     // Find managed chassis
60     crow::connections::systemBus->async_method_call(
61         [callback,
62          asyncResp](const boost::system::error_code ec,
63                     const dbus::utility::MapperGetSubTreeResponse& subtree) {
64         if (ec)
65         {
66             BMCWEB_LOG_ERROR << ec;
67             return;
68         }
69         if (subtree.empty())
70         {
71             BMCWEB_LOG_DEBUG << "Can't find chassis!";
72             return;
73         }
74 
75         std::size_t idPos = subtree[0].first.rfind('/');
76         if (idPos == std::string::npos ||
77             (idPos + 1) >= subtree[0].first.size())
78         {
79             messages::internalError(asyncResp->res);
80             BMCWEB_LOG_DEBUG << "Can't parse chassis ID!";
81             return;
82         }
83         std::string chassisId = subtree[0].first.substr(idPos + 1);
84         BMCWEB_LOG_DEBUG << "chassisId = " << chassisId;
85         callback(chassisId, asyncResp);
86         },
87         "xyz.openbmc_project.ObjectMapper",
88         "/xyz/openbmc_project/object_mapper",
89         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
90         "/xyz/openbmc_project/inventory", 0,
91         std::array<const char*, 2>{
92             "xyz.openbmc_project.Inventory.Item.Board",
93             "xyz.openbmc_project.Inventory.Item.Chassis"});
94 }
95 
96 template <typename CallbackFunc>
97 void getPortStatusAndPath(const std::string& serviceName,
98                           CallbackFunc&& callback)
99 {
100     crow::connections::systemBus->async_method_call(
101         [serviceName, callback{std::forward<CallbackFunc>(callback)}](
102             const boost::system::error_code ec,
103             const std::vector<UnitStruct>& r) {
104         if (ec)
105         {
106             BMCWEB_LOG_ERROR << ec;
107             // return error code
108             callback(ec, "", false);
109             return;
110         }
111 
112         for (const UnitStruct& unit : r)
113         {
114             // Only traverse through <xyz>.socket units
115             const std::string& unitName = std::get<NET_PROTO_UNIT_NAME>(unit);
116 
117             // find "." into unitsName
118             size_t lastCharPos = unitName.rfind('.');
119             if (lastCharPos == std::string::npos)
120             {
121                 continue;
122             }
123 
124             // is unitsName end with ".socket"
125             std::string unitNameEnd = unitName.substr(lastCharPos);
126             if (unitNameEnd != ".socket")
127             {
128                 continue;
129             }
130 
131             // find "@" into unitsName
132             if (size_t atCharPos = unitName.rfind('@');
133                 atCharPos != std::string::npos)
134             {
135                 lastCharPos = atCharPos;
136             }
137 
138             // unitsName without "@eth(x).socket", only <xyz>
139             // unitsName without ".socket", only <xyz>
140             std::string unitNameStr = unitName.substr(0, lastCharPos);
141 
142             // We are interested in services, which starts with
143             // mapped service name
144             if (unitNameStr != serviceName)
145             {
146                 continue;
147             }
148 
149             const std::string& socketPath =
150                 std::get<NET_PROTO_UNIT_OBJ_PATH>(unit);
151             const std::string& unitState =
152                 std::get<NET_PROTO_UNIT_SUB_STATE>(unit);
153 
154             bool isProtocolEnabled =
155                 ((unitState == "running") || (unitState == "listening"));
156             // We found service, return from inner loop.
157             callback(ec, socketPath, isProtocolEnabled);
158             return;
159         }
160 
161         //  no service foudn, throw error
162         boost::system::error_code ec1 = boost::system::errc::make_error_code(
163             boost::system::errc::no_such_process);
164         // return error code
165         callback(ec1, "", false);
166         BMCWEB_LOG_ERROR << ec1;
167         },
168         "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
169         "org.freedesktop.systemd1.Manager", "ListUnits");
170 }
171 
172 template <typename CallbackFunc>
173 void getPortNumber(const std::string& socketPath, CallbackFunc&& callback)
174 {
175     sdbusplus::asio::getProperty<
176         std::vector<std::tuple<std::string, std::string>>>(
177         *crow::connections::systemBus, "org.freedesktop.systemd1", socketPath,
178         "org.freedesktop.systemd1.Socket", "Listen",
179         [callback{std::forward<CallbackFunc>(callback)}](
180             const boost::system::error_code ec,
181             const std::vector<std::tuple<std::string, std::string>>& resp) {
182         if (ec)
183         {
184             BMCWEB_LOG_ERROR << ec;
185             callback(ec, 0);
186             return;
187         }
188         if (resp.empty())
189         {
190             // Network Protocol Listen Response Elements is empty
191             boost::system::error_code ec1 =
192                 boost::system::errc::make_error_code(
193                     boost::system::errc::bad_message);
194             // return error code
195             callback(ec1, 0);
196             BMCWEB_LOG_ERROR << ec1;
197             return;
198         }
199         const std::string& listenStream =
200             std::get<NET_PROTO_LISTEN_STREAM>(resp[0]);
201         const char* pa = &listenStream[listenStream.rfind(':') + 1];
202         int port{0};
203         if (auto [p, ec2] = std::from_chars(pa, nullptr, port);
204             ec2 != std::errc())
205         {
206             // there is only two possibility invalid_argument and
207             // result_out_of_range
208             boost::system::error_code ec3 =
209                 boost::system::errc::make_error_code(
210                     boost::system::errc::invalid_argument);
211             if (ec2 == std::errc::result_out_of_range)
212             {
213                 ec3 = boost::system::errc::make_error_code(
214                     boost::system::errc::result_out_of_range);
215             }
216             // return error code
217             callback(ec3, 0);
218             BMCWEB_LOG_ERROR << ec3;
219         }
220         callback(ec, port);
221         });
222 }
223 
224 } // namespace redfish
225 #endif
226