Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 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 | */ |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 16 | #pragma once |
Ed Tanous | e585b90 | 2021-11-02 15:50:54 -0700 | [diff] [blame] | 17 | #ifndef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 18 | |
Ed Tanous | 168e20c | 2021-12-13 14:39:53 -0800 | [diff] [blame] | 19 | #include <dbus_utility.hpp> |
Nan Zhou | cf7eba0 | 2022-07-21 23:53:20 +0000 | [diff] [blame] | 20 | #include <sdbusplus/asio/property.hpp> |
| 21 | |
| 22 | #include <charconv> |
| 23 | |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 24 | namespace redfish |
| 25 | { |
| 26 | |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 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 | |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 55 | template <typename CallbackFunc> |
zhanghch05 | 8d1b46d | 2021-04-01 11:18:24 +0800 | [diff] [blame] | 56 | void getMainChassisId(std::shared_ptr<bmcweb::AsyncResp> asyncResp, |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 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, |
Ed Tanous | b9d36b4 | 2022-02-26 21:42:46 -0800 | [diff] [blame] | 63 | const dbus::utility::MapperGetSubTreeResponse& subtree) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 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 | } |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 74 | |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 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); |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 86 | }, |
| 87 | "xyz.openbmc_project.ObjectMapper", |
| 88 | "/xyz/openbmc_project/object_mapper", |
| 89 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", |
| 90 | "/xyz/openbmc_project/inventory", 0, |
AppaRao Puli | f857e9a | 2020-03-12 14:28:03 +0530 | [diff] [blame] | 91 | std::array<const char*, 2>{ |
| 92 | "xyz.openbmc_project.Inventory.Item.Board", |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 93 | "xyz.openbmc_project.Inventory.Item.Chassis"}); |
| 94 | } |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 95 | |
| 96 | template <typename CallbackFunc> |
| 97 | void getPortStatusAndPath(const std::string& serviceName, |
| 98 | CallbackFunc&& callback) |
| 99 | { |
| 100 | crow::connections::systemBus->async_method_call( |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 101 | [serviceName, callback{std::forward<CallbackFunc>(callback)}]( |
| 102 | const boost::system::error_code ec, |
| 103 | const std::vector<UnitStruct>& r) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 104 | if (ec) |
| 105 | { |
| 106 | BMCWEB_LOG_ERROR << ec; |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 107 | // return error code |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 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; |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 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 | { |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 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", |
Ed Tanous | f94c4ec | 2022-01-06 12:44:41 -0800 | [diff] [blame] | 179 | [callback{std::forward<CallbackFunc>(callback)}]( |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 180 | const boost::system::error_code ec, |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 181 | const std::vector<std::tuple<std::string, std::string>>& resp) { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 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) |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 212 | { |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 213 | ec3 = boost::system::errc::make_error_code( |
| 214 | boost::system::errc::result_out_of_range); |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 215 | } |
Ed Tanous | 002d39b | 2022-05-31 08:59:27 -0700 | [diff] [blame] | 216 | // return error code |
| 217 | callback(ec3, 0); |
| 218 | BMCWEB_LOG_ERROR << ec3; |
| 219 | } |
| 220 | callback(ec, port); |
Jonathan Doman | 1e1e598 | 2021-06-11 09:36:17 -0700 | [diff] [blame] | 221 | }); |
Abhishek Patel | b4bec66 | 2021-06-21 17:32:02 -0500 | [diff] [blame] | 222 | } |
| 223 | |
Jennifer Lee | c5d03ff | 2019-03-08 15:42:58 -0800 | [diff] [blame] | 224 | } // namespace redfish |
| 225 | #endif |