blob: 04d6ceb3030640191aca8e499f9059ced8f83559 [file] [log] [blame]
Jennifer Leec5d03ff2019-03-08 15:42:58 -08001/*
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*/
Jennifer Leec5d03ff2019-03-08 15:42:58 -080016#pragma once
Ed Tanouse585b902021-11-02 15:50:54 -070017#ifndef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS
Jennifer Leec5d03ff2019-03-08 15:42:58 -080018
Ed Tanous168e20c2021-12-13 14:39:53 -080019#include <dbus_utility.hpp>
Nan Zhoucf7eba02022-07-21 23:53:20 +000020#include <sdbusplus/asio/property.hpp>
21
22#include <charconv>
23
Jennifer Leec5d03ff2019-03-08 15:42:58 -080024namespace redfish
25{
26
Abhishek Patelb4bec662021-06-21 17:32:02 -050027enum 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
41enum 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 */
50using 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
Jennifer Leec5d03ff2019-03-08 15:42:58 -080055template <typename CallbackFunc>
zhanghch058d1b46d2021-04-01 11:18:24 +080056void getMainChassisId(std::shared_ptr<bmcweb::AsyncResp> asyncResp,
Jennifer Leec5d03ff2019-03-08 15:42:58 -080057 CallbackFunc&& callback)
58{
59 // Find managed chassis
60 crow::connections::systemBus->async_method_call(
61 [callback,
62 asyncResp](const boost::system::error_code ec,
Ed Tanousb9d36b42022-02-26 21:42:46 -080063 const dbus::utility::MapperGetSubTreeResponse& subtree) {
Ed Tanous002d39b2022-05-31 08:59:27 -070064 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 }
Jennifer Leec5d03ff2019-03-08 15:42:58 -080074
Ed Tanous002d39b2022-05-31 08:59:27 -070075 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);
Jennifer Leec5d03ff2019-03-08 15:42:58 -080086 },
87 "xyz.openbmc_project.ObjectMapper",
88 "/xyz/openbmc_project/object_mapper",
89 "xyz.openbmc_project.ObjectMapper", "GetSubTree",
90 "/xyz/openbmc_project/inventory", 0,
AppaRao Pulif857e9a2020-03-12 14:28:03 +053091 std::array<const char*, 2>{
92 "xyz.openbmc_project.Inventory.Item.Board",
Jennifer Leec5d03ff2019-03-08 15:42:58 -080093 "xyz.openbmc_project.Inventory.Item.Chassis"});
94}
Abhishek Patelb4bec662021-06-21 17:32:02 -050095
96template <typename CallbackFunc>
97void getPortStatusAndPath(const std::string& serviceName,
98 CallbackFunc&& callback)
99{
100 crow::connections::systemBus->async_method_call(
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800101 [serviceName, callback{std::forward<CallbackFunc>(callback)}](
102 const boost::system::error_code ec,
103 const std::vector<UnitStruct>& r) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700104 if (ec)
105 {
106 BMCWEB_LOG_ERROR << ec;
Abhishek Patelb4bec662021-06-21 17:32:02 -0500107 // return error code
Ed Tanous002d39b2022-05-31 08:59:27 -0700108 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;
Abhishek Patelb4bec662021-06-21 17:32:02 -0500167 },
168 "org.freedesktop.systemd1", "/org/freedesktop/systemd1",
169 "org.freedesktop.systemd1.Manager", "ListUnits");
170}
171
172template <typename CallbackFunc>
173void getPortNumber(const std::string& socketPath, CallbackFunc&& callback)
174{
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700175 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",
Ed Tanousf94c4ec2022-01-06 12:44:41 -0800179 [callback{std::forward<CallbackFunc>(callback)}](
Abhishek Patelb4bec662021-06-21 17:32:02 -0500180 const boost::system::error_code ec,
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700181 const std::vector<std::tuple<std::string, std::string>>& resp) {
Ed Tanous002d39b2022-05-31 08:59:27 -0700182 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)
Abhishek Patelb4bec662021-06-21 17:32:02 -0500212 {
Ed Tanous002d39b2022-05-31 08:59:27 -0700213 ec3 = boost::system::errc::make_error_code(
214 boost::system::errc::result_out_of_range);
Abhishek Patelb4bec662021-06-21 17:32:02 -0500215 }
Ed Tanous002d39b2022-05-31 08:59:27 -0700216 // return error code
217 callback(ec3, 0);
218 BMCWEB_LOG_ERROR << ec3;
219 }
220 callback(ec, port);
Jonathan Doman1e1e5982021-06-11 09:36:17 -0700221 });
Abhishek Patelb4bec662021-06-21 17:32:02 -0500222}
223
Jennifer Leec5d03ff2019-03-08 15:42:58 -0800224} // namespace redfish
225#endif