James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 1 | // Copyright (c) 2018 Intel Corporation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Ed Tanous | b9b2e0b | 2018-09-13 13:47:50 -0700 | [diff] [blame] | 14 | |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 15 | #pragma once |
Ed Tanous | c94ad49 | 2019-10-10 15:39:33 -0700 | [diff] [blame] | 16 | #include <app.h> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 17 | #include <tinyxml2.h> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 18 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 19 | #include <async_resp.hpp> |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 20 | #include <boost/algorithm/string.hpp> |
| 21 | #include <boost/container/flat_set.hpp> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 22 | #include <dbus_singleton.hpp> |
James Feist | 5b4aa86 | 2018-08-16 14:07:01 -0700 | [diff] [blame] | 23 | #include <dbus_utility.hpp> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 24 | #include <sdbusplus/message/types.hpp> |
| 25 | |
James Feist | 4418c7f | 2019-04-15 11:09:15 -0700 | [diff] [blame] | 26 | #include <filesystem> |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 27 | #include <fstream> |
Ramesh Iyyar | d920704 | 2019-07-05 08:04:42 -0500 | [diff] [blame] | 28 | #include <regex> |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 29 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 30 | namespace crow |
| 31 | { |
| 32 | namespace openbmc_mapper |
| 33 | { |
Ed Tanous | ba9f9a6 | 2017-10-11 16:40:35 -0700 | [diff] [blame] | 34 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 35 | using GetSubTreeType = std::vector< |
| 36 | std::pair<std::string, |
| 37 | std::vector<std::pair<std::string, std::vector<std::string>>>>>; |
| 38 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 39 | const char* notFoundMsg = "404 Not Found"; |
| 40 | const char* badReqMsg = "400 Bad Request"; |
| 41 | const char* methodNotAllowedMsg = "405 Method Not Allowed"; |
| 42 | const char* forbiddenMsg = "403 Forbidden"; |
| 43 | const char* methodFailedMsg = "500 Method Call Failed"; |
| 44 | const char* methodOutputFailedMsg = "500 Method Output Error"; |
| 45 | const char* notFoundDesc = |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 46 | "org.freedesktop.DBus.Error.FileNotFound: path or object not found"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 47 | const char* propNotFoundDesc = "The specified property cannot be found"; |
| 48 | const char* noJsonDesc = "No JSON object could be decoded"; |
| 49 | const char* methodNotFoundDesc = "The specified method cannot be found"; |
| 50 | const char* methodNotAllowedDesc = "Method not allowed"; |
| 51 | const char* forbiddenPropDesc = "The specified property cannot be created"; |
| 52 | const char* forbiddenResDesc = "The specified resource cannot be created"; |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 53 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 54 | void setErrorResponse(crow::Response& res, boost::beast::http::status result, |
| 55 | const std::string& desc, const std::string_view msg) |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 56 | { |
| 57 | res.result(result); |
| 58 | res.jsonValue = {{"data", {{"description", desc}}}, |
| 59 | {"message", msg}, |
| 60 | {"status", "error"}}; |
| 61 | } |
| 62 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 63 | void introspectObjects(const std::string& processName, |
| 64 | const std::string& objectPath, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 65 | std::shared_ptr<bmcweb::AsyncResp> transaction) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 66 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 67 | if (transaction->res.jsonValue.is_null()) |
| 68 | { |
| 69 | transaction->res.jsonValue = {{"status", "ok"}, |
| 70 | {"bus_name", processName}, |
| 71 | {"objects", nlohmann::json::array()}}; |
| 72 | } |
| 73 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 74 | crow::connections::systemBus->async_method_call( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 75 | [transaction, processName{std::string(processName)}, |
| 76 | objectPath{std::string(objectPath)}]( |
| 77 | const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 78 | const std::string& introspect_xml) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 79 | if (ec) |
| 80 | { |
| 81 | BMCWEB_LOG_ERROR |
| 82 | << "Introspect call failed with error: " << ec.message() |
| 83 | << " on process: " << processName << " path: " << objectPath |
| 84 | << "\n"; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 85 | return; |
| 86 | } |
| 87 | transaction->res.jsonValue["objects"].push_back( |
| 88 | {{"path", objectPath}}); |
| 89 | |
| 90 | tinyxml2::XMLDocument doc; |
| 91 | |
| 92 | doc.Parse(introspect_xml.c_str()); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 93 | tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 94 | if (pRoot == nullptr) |
| 95 | { |
| 96 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 97 | << processName << " " << objectPath << "\n"; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 98 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 99 | else |
| 100 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 101 | tinyxml2::XMLElement* node = pRoot->FirstChildElement("node"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 102 | while (node != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 103 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 104 | const char* childPath = node->Attribute("name"); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 105 | if (childPath != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 106 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 107 | std::string newpath; |
| 108 | if (objectPath != "/") |
| 109 | { |
| 110 | newpath += objectPath; |
| 111 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 112 | newpath += std::string("/") + childPath; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 113 | // introspect the subobjects as well |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 114 | introspectObjects(processName, newpath, transaction); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 115 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 116 | |
| 117 | node = node->NextSiblingElement("node"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 118 | } |
| 119 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 120 | }, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 121 | processName, objectPath, "org.freedesktop.DBus.Introspectable", |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 122 | "Introspect"); |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 123 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 124 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 125 | void getPropertiesForEnumerate(const std::string& objectPath, |
| 126 | const std::string& service, |
| 127 | const std::string& interface, |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 128 | std::shared_ptr<bmcweb::AsyncResp> asyncResp) |
| 129 | { |
| 130 | BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " " |
| 131 | << service << " " << interface; |
| 132 | |
| 133 | crow::connections::systemBus->async_method_call( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 134 | [asyncResp, objectPath, service, interface]( |
| 135 | const boost::system::error_code ec, |
| 136 | const std::vector<std::pair< |
| 137 | std::string, dbus::utility::DbusVariantType>>& propertiesList) { |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 138 | if (ec) |
| 139 | { |
| 140 | BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface " |
| 141 | << interface << " service " << service |
| 142 | << " failed with code " << ec; |
| 143 | return; |
| 144 | } |
| 145 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 146 | nlohmann::json& dataJson = asyncResp->res.jsonValue["data"]; |
| 147 | nlohmann::json& objectJson = dataJson[objectPath]; |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 148 | if (objectJson.is_null()) |
| 149 | { |
| 150 | objectJson = nlohmann::json::object(); |
| 151 | } |
| 152 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 153 | for (const auto& [name, value] : propertiesList) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 154 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 155 | nlohmann::json& propertyJson = objectJson[name]; |
| 156 | std::visit([&propertyJson](auto&& val) { propertyJson = val; }, |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 157 | value); |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 158 | } |
| 159 | }, |
| 160 | service, objectPath, "org.freedesktop.DBus.Properties", "GetAll", |
| 161 | interface); |
| 162 | } |
| 163 | |
| 164 | // Find any results that weren't picked up by ObjectManagers, to be |
| 165 | // called after all ObjectManagers are searched for and called. |
| 166 | void findRemainingObjectsForEnumerate( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 167 | const std::string& objectPath, std::shared_ptr<GetSubTreeType> subtree, |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 168 | std::shared_ptr<bmcweb::AsyncResp> asyncResp) |
| 169 | { |
| 170 | BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate"; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 171 | const nlohmann::json& dataJson = asyncResp->res.jsonValue["data"]; |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 172 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 173 | for (const auto& [path, interface_map] : *subtree) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 174 | { |
| 175 | if (path == objectPath) |
| 176 | { |
| 177 | // An enumerate does not return the target path's properties |
| 178 | continue; |
| 179 | } |
| 180 | if (dataJson.find(path) == dataJson.end()) |
| 181 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 182 | for (const auto& [service, interfaces] : interface_map) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 183 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 184 | for (const auto& interface : interfaces) |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 185 | { |
| 186 | if (!boost::starts_with(interface, "org.freedesktop.DBus")) |
| 187 | { |
| 188 | getPropertiesForEnumerate(path, service, interface, |
| 189 | asyncResp); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | } |
| 196 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 197 | struct InProgressEnumerateData |
| 198 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 199 | InProgressEnumerateData(const std::string& objectPath, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 200 | std::shared_ptr<bmcweb::AsyncResp> asyncResp) : |
| 201 | objectPath(objectPath), |
| 202 | asyncResp(asyncResp) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 203 | {} |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 204 | |
| 205 | ~InProgressEnumerateData() |
| 206 | { |
Matt Spinler | 2df1e7d | 2018-12-05 15:53:16 -0600 | [diff] [blame] | 207 | findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp); |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | const std::string objectPath; |
| 211 | std::shared_ptr<GetSubTreeType> subtree; |
| 212 | std::shared_ptr<bmcweb::AsyncResp> asyncResp; |
| 213 | }; |
| 214 | |
| 215 | void getManagedObjectsForEnumerate( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 216 | const std::string& object_name, const std::string& object_manager_path, |
| 217 | const std::string& connection_name, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 218 | std::shared_ptr<InProgressEnumerateData> transaction) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 219 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 220 | BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << object_name |
| 221 | << " object_manager_path " << object_manager_path |
| 222 | << " connection_name " << connection_name; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 223 | crow::connections::systemBus->async_method_call( |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 224 | [transaction, object_name, |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 225 | connection_name](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 226 | const dbus::utility::ManagedObjectType& objects) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 227 | if (ec) |
| 228 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 229 | BMCWEB_LOG_ERROR << "GetManagedObjects on path " << object_name |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 230 | << " on connection " << connection_name |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 231 | << " failed with code " << ec; |
| 232 | return; |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 233 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 234 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 235 | nlohmann::json& dataJson = |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 236 | transaction->asyncResp->res.jsonValue["data"]; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 237 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 238 | for (const auto& objectPath : objects) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 239 | { |
| 240 | if (boost::starts_with(objectPath.first.str, object_name)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 241 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 242 | BMCWEB_LOG_DEBUG << "Reading object " |
| 243 | << objectPath.first.str; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 244 | nlohmann::json& objectJson = dataJson[objectPath.first.str]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 245 | if (objectJson.is_null()) |
| 246 | { |
| 247 | objectJson = nlohmann::json::object(); |
| 248 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 249 | for (const auto& interface : objectPath.second) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 250 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 251 | for (const auto& property : interface.second) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 252 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 253 | nlohmann::json& propertyJson = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 254 | objectJson[property.first]; |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 255 | std::visit([&propertyJson]( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 256 | auto&& val) { propertyJson = val; }, |
Ed Tanous | abf2add | 2019-01-22 16:40:12 -0800 | [diff] [blame] | 257 | property.second); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 261 | for (const auto& interface : objectPath.second) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 262 | { |
| 263 | if (interface.first == "org.freedesktop.DBus.ObjectManager") |
| 264 | { |
| 265 | getManagedObjectsForEnumerate( |
| 266 | objectPath.first.str, objectPath.first.str, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 267 | connection_name, transaction); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 268 | } |
| 269 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 270 | } |
| 271 | }, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 272 | connection_name, object_manager_path, |
| 273 | "org.freedesktop.DBus.ObjectManager", "GetManagedObjects"); |
| 274 | } |
| 275 | |
| 276 | void findObjectManagerPathForEnumerate( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 277 | const std::string& object_name, const std::string& connection_name, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 278 | std::shared_ptr<InProgressEnumerateData> transaction) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 279 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 280 | BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << object_name |
| 281 | << " on connection:" << connection_name; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 282 | crow::connections::systemBus->async_method_call( |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 283 | [transaction, object_name, connection_name]( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 284 | const boost::system::error_code ec, |
| 285 | const boost::container::flat_map< |
| 286 | std::string, boost::container::flat_map< |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 287 | std::string, std::vector<std::string>>>& |
| 288 | objects) { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 289 | if (ec) |
| 290 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 291 | BMCWEB_LOG_ERROR << "GetAncestors on path " << object_name |
| 292 | << " failed with code " << ec; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 293 | return; |
| 294 | } |
| 295 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 296 | for (const auto& pathGroup : objects) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 297 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 298 | for (const auto& connectionGroup : pathGroup.second) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 299 | { |
| 300 | if (connectionGroup.first == connection_name) |
| 301 | { |
| 302 | // Found the object manager path for this resource. |
| 303 | getManagedObjectsForEnumerate( |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 304 | object_name, pathGroup.first, connection_name, |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 305 | transaction); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 306 | return; |
| 307 | } |
| 308 | } |
| 309 | } |
| 310 | }, |
| 311 | "xyz.openbmc_project.ObjectMapper", |
| 312 | "/xyz/openbmc_project/object_mapper", |
| 313 | "xyz.openbmc_project.ObjectMapper", "GetAncestors", object_name, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 314 | std::array<const char*, 1>{"org.freedesktop.DBus.ObjectManager"}); |
Ed Tanous | aa2e59c | 2018-04-12 12:17:20 -0700 | [diff] [blame] | 315 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 316 | |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 317 | // Uses GetObject to add the object info about the target /enumerate path to |
| 318 | // the results of GetSubTree, as GetSubTree will not return info for the |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 319 | // target path, and then continues on enumerating the rest of the tree. |
| 320 | void getObjectAndEnumerate(std::shared_ptr<InProgressEnumerateData> transaction) |
| 321 | { |
| 322 | using GetObjectType = |
| 323 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 324 | |
| 325 | crow::connections::systemBus->async_method_call( |
| 326 | [transaction](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 327 | const GetObjectType& objects) { |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 328 | if (ec) |
| 329 | { |
| 330 | BMCWEB_LOG_ERROR << "GetObject for path " |
| 331 | << transaction->objectPath |
| 332 | << " failed with code " << ec; |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath |
| 337 | << " has " << objects.size() << " entries"; |
| 338 | if (!objects.empty()) |
| 339 | { |
| 340 | transaction->subtree->emplace_back(transaction->objectPath, |
| 341 | objects); |
| 342 | } |
| 343 | |
| 344 | // Map indicating connection name, and the path where the object |
| 345 | // manager exists |
| 346 | boost::container::flat_map<std::string, std::string> connections; |
| 347 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 348 | for (const auto& object : *(transaction->subtree)) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 349 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 350 | for (const auto& connection : object.second) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 351 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 352 | std::string& objectManagerPath = |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 353 | connections[connection.first]; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 354 | for (const auto& interface : connection.second) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 355 | { |
| 356 | BMCWEB_LOG_DEBUG << connection.first |
| 357 | << " has interface " << interface; |
| 358 | if (interface == "org.freedesktop.DBus.ObjectManager") |
| 359 | { |
| 360 | BMCWEB_LOG_DEBUG << "found object manager path " |
| 361 | << object.first; |
| 362 | objectManagerPath = object.first; |
| 363 | } |
| 364 | } |
| 365 | } |
| 366 | } |
| 367 | BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections"; |
| 368 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 369 | for (const auto& connection : connections) |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 370 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 371 | // If we already know where the object manager is, we don't |
| 372 | // need to search for it, we can call directly in to |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 373 | // getManagedObjects |
| 374 | if (!connection.second.empty()) |
| 375 | { |
| 376 | getManagedObjectsForEnumerate( |
| 377 | transaction->objectPath, connection.second, |
| 378 | connection.first, transaction); |
| 379 | } |
| 380 | else |
| 381 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 382 | // otherwise we need to find the object manager path |
| 383 | // before we can continue |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 384 | findObjectManagerPathForEnumerate( |
| 385 | transaction->objectPath, connection.first, transaction); |
| 386 | } |
| 387 | } |
| 388 | }, |
| 389 | "xyz.openbmc_project.ObjectMapper", |
| 390 | "/xyz/openbmc_project/object_mapper", |
| 391 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 392 | transaction->objectPath, std::array<const char*, 0>()); |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 393 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 394 | |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 395 | // Structure for storing data on an in progress action |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 396 | struct InProgressActionData |
| 397 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 398 | InProgressActionData(crow::Response& res) : res(res){}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 399 | ~InProgressActionData() |
| 400 | { |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 401 | // Methods could have been called across different owners |
| 402 | // and interfaces, where some calls failed and some passed. |
| 403 | // |
| 404 | // The rules for this are: |
| 405 | // * if no method was called - error |
| 406 | // * if a method failed and none passed - error |
| 407 | // (converse: if at least one method passed - OK) |
| 408 | // * for the method output: |
| 409 | // * if output processing didn't fail, return the data |
| 410 | |
| 411 | // Only deal with method returns if nothing failed earlier |
| 412 | if (res.result() == boost::beast::http::status::ok) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 413 | { |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 414 | if (!methodPassed) |
| 415 | { |
Matt Spinler | 06b1b63 | 2019-06-18 16:09:25 -0500 | [diff] [blame] | 416 | if (!methodFailed) |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 417 | { |
| 418 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 419 | methodNotFoundDesc, notFoundMsg); |
| 420 | } |
| 421 | } |
| 422 | else |
| 423 | { |
| 424 | if (outputFailed) |
| 425 | { |
| 426 | setErrorResponse( |
| 427 | res, boost::beast::http::status::internal_server_error, |
| 428 | "Method output failure", methodOutputFailedMsg); |
| 429 | } |
| 430 | else |
| 431 | { |
| 432 | res.jsonValue = {{"status", "ok"}, |
| 433 | {"message", "200 OK"}, |
| 434 | {"data", methodResponse}}; |
| 435 | } |
| 436 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 437 | } |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 438 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 439 | res.end(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 440 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 441 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 442 | void setErrorStatus(const std::string& desc) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 443 | { |
Matt Spinler | c0eb9bd | 2019-01-09 15:22:30 -0600 | [diff] [blame] | 444 | setErrorResponse(res, boost::beast::http::status::bad_request, desc, |
| 445 | badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 446 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 447 | crow::Response& res; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 448 | std::string path; |
| 449 | std::string methodName; |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 450 | std::string interfaceName; |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 451 | bool methodPassed = false; |
| 452 | bool methodFailed = false; |
| 453 | bool outputFailed = false; |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 454 | bool convertedToArray = false; |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 455 | nlohmann::json methodResponse; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 456 | nlohmann::json arguments; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 457 | }; |
| 458 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 459 | std::vector<std::string> dbusArgSplit(const std::string& string) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 460 | { |
| 461 | std::vector<std::string> ret; |
| 462 | if (string.empty()) |
| 463 | { |
| 464 | return ret; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 465 | } |
Ed Tanous | 0f0353b | 2019-10-24 11:37:51 -0700 | [diff] [blame] | 466 | ret.emplace_back(""); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 467 | int containerDepth = 0; |
| 468 | |
| 469 | for (std::string::const_iterator character = string.begin(); |
| 470 | character != string.end(); character++) |
| 471 | { |
| 472 | ret.back() += *character; |
| 473 | switch (*character) |
| 474 | { |
| 475 | case ('a'): |
| 476 | break; |
| 477 | case ('('): |
| 478 | case ('{'): |
| 479 | containerDepth++; |
| 480 | break; |
| 481 | case ('}'): |
| 482 | case (')'): |
| 483 | containerDepth--; |
| 484 | if (containerDepth == 0) |
| 485 | { |
| 486 | if (character + 1 != string.end()) |
| 487 | { |
Ed Tanous | 0f0353b | 2019-10-24 11:37:51 -0700 | [diff] [blame] | 488 | ret.emplace_back(""); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 489 | } |
| 490 | } |
| 491 | break; |
| 492 | default: |
| 493 | if (containerDepth == 0) |
| 494 | { |
| 495 | if (character + 1 != string.end()) |
| 496 | { |
Ed Tanous | 0f0353b | 2019-10-24 11:37:51 -0700 | [diff] [blame] | 497 | ret.emplace_back(""); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 498 | } |
| 499 | } |
| 500 | break; |
| 501 | } |
| 502 | } |
Matt Spinler | 4ae611d | 2019-01-11 15:37:06 -0600 | [diff] [blame] | 503 | |
| 504 | return ret; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 505 | } |
| 506 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 507 | int convertJsonToDbus(sd_bus_message* m, const std::string& arg_type, |
| 508 | const nlohmann::json& input_json) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 509 | { |
| 510 | int r = 0; |
| 511 | BMCWEB_LOG_DEBUG << "Converting " << input_json.dump() |
| 512 | << " to type: " << arg_type; |
| 513 | const std::vector<std::string> argTypes = dbusArgSplit(arg_type); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 514 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 515 | // Assume a single object for now. |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 516 | const nlohmann::json* j = &input_json; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 517 | nlohmann::json::const_iterator jIt = input_json.begin(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 518 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 519 | for (const std::string& argCode : argTypes) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 520 | { |
| 521 | // If we are decoding multiple objects, grab the pointer to the |
| 522 | // iterator, and increment it for the next loop |
| 523 | if (argTypes.size() > 1) |
| 524 | { |
| 525 | if (jIt == input_json.end()) |
| 526 | { |
| 527 | return -2; |
| 528 | } |
| 529 | j = &*jIt; |
| 530 | jIt++; |
| 531 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 532 | const int64_t* intValue = j->get_ptr<const int64_t*>(); |
| 533 | const std::string* stringValue = j->get_ptr<const std::string*>(); |
| 534 | const double* doubleValue = j->get_ptr<const double*>(); |
| 535 | const bool* b = j->get_ptr<const bool*>(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 536 | int64_t v = 0; |
| 537 | double d = 0.0; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 538 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 539 | // Do some basic type conversions that make sense. uint can be |
| 540 | // converted to int. int and uint can be converted to double |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 541 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 542 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 543 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 544 | if (uintValue != nullptr) |
| 545 | { |
| 546 | v = static_cast<int64_t>(*uintValue); |
| 547 | intValue = &v; |
| 548 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 549 | } |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 550 | if (doubleValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 551 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 552 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 553 | if (uintValue != nullptr) |
| 554 | { |
| 555 | d = static_cast<double>(*uintValue); |
| 556 | doubleValue = &d; |
| 557 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 558 | } |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 559 | if (doubleValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 560 | { |
Ed Tanous | 66664f2 | 2019-10-11 13:05:49 -0700 | [diff] [blame] | 561 | if (intValue != nullptr) |
| 562 | { |
| 563 | d = static_cast<double>(*intValue); |
| 564 | doubleValue = &d; |
| 565 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 566 | } |
| 567 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 568 | if (argCode == "s") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 569 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 570 | if (stringValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 571 | { |
| 572 | return -1; |
| 573 | } |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 574 | r = sd_bus_message_append_basic( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 575 | m, argCode[0], static_cast<const void*>(stringValue->data())); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 576 | if (r < 0) |
| 577 | { |
| 578 | return r; |
| 579 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 580 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 581 | else if (argCode == "i") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 582 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 583 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 584 | { |
| 585 | return -1; |
| 586 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 587 | if ((*intValue < std::numeric_limits<int32_t>::lowest()) || |
| 588 | (*intValue > std::numeric_limits<int32_t>::max())) |
| 589 | { |
| 590 | return -ERANGE; |
| 591 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 592 | int32_t i = static_cast<int32_t>(*intValue); |
| 593 | r = sd_bus_message_append_basic(m, argCode[0], &i); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 594 | if (r < 0) |
| 595 | { |
| 596 | return r; |
| 597 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 598 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 599 | else if (argCode == "b") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 600 | { |
| 601 | // lots of ways bool could be represented here. Try them all |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 602 | int boolInt = false; |
| 603 | if (intValue != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 604 | { |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 605 | if (*intValue == 1) |
| 606 | { |
| 607 | boolInt = true; |
| 608 | } |
| 609 | else if (*intValue == 0) |
| 610 | { |
| 611 | boolInt = false; |
| 612 | } |
| 613 | else |
| 614 | { |
| 615 | return -ERANGE; |
| 616 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 617 | } |
| 618 | else if (b != nullptr) |
| 619 | { |
Matt Spinler | a2f0263 | 2019-01-18 10:15:35 -0600 | [diff] [blame] | 620 | boolInt = *b ? 1 : 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 621 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 622 | else if (stringValue != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 623 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 624 | boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 625 | } |
| 626 | else |
| 627 | { |
| 628 | return -1; |
| 629 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 630 | r = sd_bus_message_append_basic(m, argCode[0], &boolInt); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 631 | if (r < 0) |
| 632 | { |
| 633 | return r; |
| 634 | } |
| 635 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 636 | else if (argCode == "n") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 637 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 638 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 639 | { |
| 640 | return -1; |
| 641 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 642 | if ((*intValue < std::numeric_limits<int16_t>::lowest()) || |
| 643 | (*intValue > std::numeric_limits<int16_t>::max())) |
| 644 | { |
| 645 | return -ERANGE; |
| 646 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 647 | int16_t n = static_cast<int16_t>(*intValue); |
| 648 | r = sd_bus_message_append_basic(m, argCode[0], &n); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 649 | if (r < 0) |
| 650 | { |
| 651 | return r; |
| 652 | } |
| 653 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 654 | else if (argCode == "x") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 655 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 656 | if (intValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 657 | { |
| 658 | return -1; |
| 659 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 660 | if ((*intValue < std::numeric_limits<int64_t>::lowest()) || |
| 661 | (*intValue > std::numeric_limits<int64_t>::max())) |
| 662 | { |
| 663 | return -ERANGE; |
| 664 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 665 | r = sd_bus_message_append_basic(m, argCode[0], intValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 666 | if (r < 0) |
| 667 | { |
| 668 | return r; |
| 669 | } |
| 670 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 671 | else if (argCode == "y") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 672 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 673 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 674 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 675 | { |
| 676 | return -1; |
| 677 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 678 | if ((*uintValue < std::numeric_limits<uint8_t>::lowest()) || |
| 679 | (*uintValue > std::numeric_limits<uint8_t>::max())) |
| 680 | { |
| 681 | return -ERANGE; |
| 682 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 683 | uint8_t y = static_cast<uint8_t>(*uintValue); |
| 684 | r = sd_bus_message_append_basic(m, argCode[0], &y); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 685 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 686 | else if (argCode == "q") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 687 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 688 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 689 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 690 | { |
| 691 | return -1; |
| 692 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 693 | if ((*uintValue < std::numeric_limits<uint16_t>::lowest()) || |
| 694 | (*uintValue > std::numeric_limits<uint16_t>::max())) |
| 695 | { |
| 696 | return -ERANGE; |
| 697 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 698 | uint16_t q = static_cast<uint16_t>(*uintValue); |
| 699 | r = sd_bus_message_append_basic(m, argCode[0], &q); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 700 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 701 | else if (argCode == "u") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 702 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 703 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 704 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 705 | { |
| 706 | return -1; |
| 707 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 708 | if ((*uintValue < std::numeric_limits<uint32_t>::lowest()) || |
| 709 | (*uintValue > std::numeric_limits<uint32_t>::max())) |
| 710 | { |
| 711 | return -ERANGE; |
| 712 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 713 | uint32_t u = static_cast<uint32_t>(*uintValue); |
| 714 | r = sd_bus_message_append_basic(m, argCode[0], &u); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 715 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 716 | else if (argCode == "t") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 717 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 718 | const uint64_t* uintValue = j->get_ptr<const uint64_t*>(); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 719 | if (uintValue == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 720 | { |
| 721 | return -1; |
| 722 | } |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 723 | if ((*uintValue < std::numeric_limits<uint64_t>::lowest()) || |
| 724 | (*uintValue > std::numeric_limits<uint64_t>::max())) |
| 725 | { |
| 726 | return -ERANGE; |
| 727 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 728 | r = sd_bus_message_append_basic(m, argCode[0], uintValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 729 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 730 | else if (argCode == "d") |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 731 | { |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 732 | if (doubleValue == nullptr) |
| 733 | { |
| 734 | return -1; |
| 735 | } |
| 736 | if ((*doubleValue < std::numeric_limits<double>::lowest()) || |
| 737 | (*doubleValue > std::numeric_limits<double>::max())) |
| 738 | { |
| 739 | return -ERANGE; |
| 740 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 741 | sd_bus_message_append_basic(m, argCode[0], doubleValue); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 742 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 743 | else if (boost::starts_with(argCode, "a")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 744 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 745 | std::string containedType = argCode.substr(1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 746 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 747 | containedType.c_str()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 748 | if (r < 0) |
| 749 | { |
| 750 | return r; |
| 751 | } |
| 752 | |
| 753 | for (nlohmann::json::const_iterator it = j->begin(); it != j->end(); |
| 754 | ++it) |
| 755 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 756 | r = convertJsonToDbus(m, containedType, *it); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 757 | if (r < 0) |
| 758 | { |
| 759 | return r; |
| 760 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 761 | } |
| 762 | sd_bus_message_close_container(m); |
| 763 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 764 | else if (boost::starts_with(argCode, "v")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 765 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 766 | std::string containedType = argCode.substr(1); |
| 767 | BMCWEB_LOG_DEBUG << "variant type: " << argCode |
| 768 | << " appending variant of type: " << containedType; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 769 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 770 | containedType.c_str()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 771 | if (r < 0) |
| 772 | { |
| 773 | return r; |
| 774 | } |
| 775 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 776 | r = convertJsonToDbus(m, containedType, input_json); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 777 | if (r < 0) |
| 778 | { |
| 779 | return r; |
| 780 | } |
| 781 | |
| 782 | r = sd_bus_message_close_container(m); |
| 783 | if (r < 0) |
| 784 | { |
| 785 | return r; |
| 786 | } |
| 787 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 788 | else if (boost::starts_with(argCode, "(") && |
| 789 | boost::ends_with(argCode, ")")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 790 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 791 | std::string containedType = argCode.substr(1, argCode.size() - 1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 792 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 793 | containedType.c_str()); |
Ed Tanous | f1eebf0 | 2019-03-04 15:57:09 -0800 | [diff] [blame] | 794 | if (r < 0) |
| 795 | { |
| 796 | return r; |
| 797 | } |
| 798 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 799 | nlohmann::json::const_iterator it = j->begin(); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 800 | for (const std::string& argCode : dbusArgSplit(arg_type)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 801 | { |
| 802 | if (it == j->end()) |
| 803 | { |
| 804 | return -1; |
| 805 | } |
Ed Tanous | b01bf29 | 2019-03-25 19:25:26 +0000 | [diff] [blame] | 806 | r = convertJsonToDbus(m, argCode, *it); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 807 | if (r < 0) |
| 808 | { |
| 809 | return r; |
| 810 | } |
| 811 | it++; |
| 812 | } |
| 813 | r = sd_bus_message_close_container(m); |
| 814 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 815 | else if (boost::starts_with(argCode, "{") && |
| 816 | boost::ends_with(argCode, "}")) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 817 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 818 | std::string containedType = argCode.substr(1, argCode.size() - 1); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 819 | r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY, |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 820 | containedType.c_str()); |
Ed Tanous | f1eebf0 | 2019-03-04 15:57:09 -0800 | [diff] [blame] | 821 | if (r < 0) |
| 822 | { |
| 823 | return r; |
| 824 | } |
| 825 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 826 | std::vector<std::string> codes = dbusArgSplit(containedType); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 827 | if (codes.size() != 2) |
| 828 | { |
| 829 | return -1; |
| 830 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 831 | const std::string& key_type = codes[0]; |
| 832 | const std::string& value_type = codes[1]; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 833 | for (auto it : j->items()) |
| 834 | { |
| 835 | r = convertJsonToDbus(m, key_type, it.key()); |
| 836 | if (r < 0) |
| 837 | { |
| 838 | return r; |
| 839 | } |
| 840 | |
| 841 | r = convertJsonToDbus(m, value_type, it.value()); |
| 842 | if (r < 0) |
| 843 | { |
| 844 | return r; |
| 845 | } |
| 846 | } |
| 847 | r = sd_bus_message_close_container(m); |
| 848 | } |
| 849 | else |
| 850 | { |
| 851 | return -2; |
| 852 | } |
| 853 | if (r < 0) |
| 854 | { |
| 855 | return r; |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 856 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 857 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 858 | if (argTypes.size() > 1) |
| 859 | { |
| 860 | jIt++; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 861 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 862 | } |
Matt Spinler | 127ea54 | 2019-01-14 11:04:28 -0600 | [diff] [blame] | 863 | |
| 864 | return r; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 865 | } |
| 866 | |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 867 | template <typename T> |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 868 | int readMessageItem(const std::string& typeCode, sdbusplus::message::message& m, |
| 869 | nlohmann::json& data) |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 870 | { |
| 871 | T value; |
| 872 | |
| 873 | int r = sd_bus_message_read_basic(m.get(), typeCode.front(), &value); |
| 874 | if (r < 0) |
| 875 | { |
| 876 | BMCWEB_LOG_ERROR << "sd_bus_message_read_basic on type " << typeCode |
| 877 | << " failed!"; |
| 878 | return r; |
| 879 | } |
| 880 | |
| 881 | data = value; |
| 882 | return 0; |
| 883 | } |
| 884 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 885 | int convertDBusToJSON(const std::string& returnType, |
| 886 | sdbusplus::message::message& m, nlohmann::json& response); |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 887 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 888 | int readDictEntryFromMessage(const std::string& typeCode, |
| 889 | sdbusplus::message::message& m, |
| 890 | nlohmann::json& object) |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 891 | { |
| 892 | std::vector<std::string> types = dbusArgSplit(typeCode); |
| 893 | if (types.size() != 2) |
| 894 | { |
| 895 | BMCWEB_LOG_ERROR << "wrong number contained types in dictionary: " |
| 896 | << types.size(); |
| 897 | return -1; |
| 898 | } |
| 899 | |
| 900 | int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_DICT_ENTRY, |
| 901 | typeCode.c_str()); |
| 902 | if (r < 0) |
| 903 | { |
| 904 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container with rc " << r; |
| 905 | return r; |
| 906 | } |
| 907 | |
| 908 | nlohmann::json key; |
| 909 | r = convertDBusToJSON(types[0], m, key); |
| 910 | if (r < 0) |
| 911 | { |
| 912 | return r; |
| 913 | } |
| 914 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 915 | const std::string* keyPtr = key.get_ptr<const std::string*>(); |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 916 | if (keyPtr == nullptr) |
| 917 | { |
| 918 | // json doesn't support non-string keys. If we hit this condition, |
| 919 | // convert the result to a string so we can proceed |
| 920 | key = key.dump(); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 921 | keyPtr = key.get_ptr<const std::string*>(); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 922 | // in theory this can't fail now, but lets be paranoid about it |
| 923 | // anyway |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 924 | if (keyPtr == nullptr) |
| 925 | { |
| 926 | return -1; |
| 927 | } |
| 928 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 929 | nlohmann::json& value = object[*keyPtr]; |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 930 | |
| 931 | r = convertDBusToJSON(types[1], m, value); |
| 932 | if (r < 0) |
| 933 | { |
| 934 | return r; |
| 935 | } |
| 936 | |
| 937 | r = sd_bus_message_exit_container(m.get()); |
| 938 | if (r < 0) |
| 939 | { |
| 940 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 941 | return r; |
| 942 | } |
| 943 | |
| 944 | return 0; |
| 945 | } |
| 946 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 947 | int readArrayFromMessage(const std::string& typeCode, |
| 948 | sdbusplus::message::message& m, nlohmann::json& data) |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 949 | { |
| 950 | if (typeCode.size() < 2) |
| 951 | { |
| 952 | BMCWEB_LOG_ERROR << "Type code " << typeCode |
| 953 | << " too small for an array"; |
| 954 | return -1; |
| 955 | } |
| 956 | |
| 957 | std::string containedType = typeCode.substr(1); |
| 958 | |
| 959 | int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_ARRAY, |
| 960 | containedType.c_str()); |
| 961 | if (r < 0) |
| 962 | { |
| 963 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 964 | << r; |
| 965 | return r; |
| 966 | } |
| 967 | |
| 968 | bool dict = boost::starts_with(containedType, "{") && |
| 969 | boost::ends_with(containedType, "}"); |
| 970 | |
| 971 | if (dict) |
| 972 | { |
| 973 | // Remove the { } |
| 974 | containedType = containedType.substr(1, containedType.size() - 2); |
| 975 | data = nlohmann::json::object(); |
| 976 | } |
| 977 | else |
| 978 | { |
| 979 | data = nlohmann::json::array(); |
| 980 | } |
| 981 | |
| 982 | while (true) |
| 983 | { |
| 984 | r = sd_bus_message_at_end(m.get(), false); |
| 985 | if (r < 0) |
| 986 | { |
| 987 | BMCWEB_LOG_ERROR << "sd_bus_message_at_end failed"; |
| 988 | return r; |
| 989 | } |
| 990 | |
| 991 | if (r > 0) |
| 992 | { |
| 993 | break; |
| 994 | } |
| 995 | |
| 996 | // Dictionaries are only ever seen in an array |
| 997 | if (dict) |
| 998 | { |
| 999 | r = readDictEntryFromMessage(containedType, m, data); |
| 1000 | if (r < 0) |
| 1001 | { |
| 1002 | return r; |
| 1003 | } |
| 1004 | } |
| 1005 | else |
| 1006 | { |
| 1007 | data.push_back(nlohmann::json()); |
| 1008 | |
| 1009 | r = convertDBusToJSON(containedType, m, data.back()); |
| 1010 | if (r < 0) |
| 1011 | { |
| 1012 | return r; |
| 1013 | } |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | r = sd_bus_message_exit_container(m.get()); |
| 1018 | if (r < 0) |
| 1019 | { |
| 1020 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 1021 | return r; |
| 1022 | } |
| 1023 | |
| 1024 | return 0; |
| 1025 | } |
| 1026 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1027 | int readStructFromMessage(const std::string& typeCode, |
| 1028 | sdbusplus::message::message& m, nlohmann::json& data) |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1029 | { |
| 1030 | if (typeCode.size() < 3) |
| 1031 | { |
| 1032 | BMCWEB_LOG_ERROR << "Type code " << typeCode |
| 1033 | << " too small for a struct"; |
| 1034 | return -1; |
| 1035 | } |
| 1036 | |
| 1037 | std::string containedTypes = typeCode.substr(1, typeCode.size() - 2); |
| 1038 | std::vector<std::string> types = dbusArgSplit(containedTypes); |
| 1039 | |
| 1040 | int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_STRUCT, |
| 1041 | containedTypes.c_str()); |
| 1042 | if (r < 0) |
| 1043 | { |
| 1044 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 1045 | << r; |
| 1046 | return r; |
| 1047 | } |
| 1048 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1049 | for (const std::string& type : types) |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1050 | { |
| 1051 | data.push_back(nlohmann::json()); |
| 1052 | r = convertDBusToJSON(type, m, data.back()); |
| 1053 | if (r < 0) |
| 1054 | { |
| 1055 | return r; |
| 1056 | } |
| 1057 | } |
| 1058 | |
| 1059 | r = sd_bus_message_exit_container(m.get()); |
| 1060 | if (r < 0) |
| 1061 | { |
| 1062 | BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed"; |
| 1063 | return r; |
| 1064 | } |
| 1065 | return 0; |
| 1066 | } |
| 1067 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1068 | int readVariantFromMessage(sdbusplus::message::message& m, nlohmann::json& data) |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1069 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1070 | const char* containerType; |
Ed Tanous | 99131cd | 2019-10-24 11:12:47 -0700 | [diff] [blame] | 1071 | int r = sd_bus_message_peek_type(m.get(), nullptr, &containerType); |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1072 | if (r < 0) |
| 1073 | { |
| 1074 | BMCWEB_LOG_ERROR << "sd_bus_message_peek_type failed"; |
| 1075 | return r; |
| 1076 | } |
| 1077 | |
| 1078 | r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_VARIANT, |
| 1079 | containerType); |
| 1080 | if (r < 0) |
| 1081 | { |
| 1082 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc " |
| 1083 | << r; |
| 1084 | return r; |
| 1085 | } |
| 1086 | |
| 1087 | r = convertDBusToJSON(containerType, m, data); |
| 1088 | if (r < 0) |
| 1089 | { |
| 1090 | return r; |
| 1091 | } |
| 1092 | |
| 1093 | r = sd_bus_message_exit_container(m.get()); |
| 1094 | if (r < 0) |
| 1095 | { |
| 1096 | BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed"; |
| 1097 | return r; |
| 1098 | } |
| 1099 | |
| 1100 | return 0; |
| 1101 | } |
| 1102 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1103 | int convertDBusToJSON(const std::string& returnType, |
| 1104 | sdbusplus::message::message& m, nlohmann::json& response) |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1105 | { |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1106 | int r = 0; |
| 1107 | const std::vector<std::string> returnTypes = dbusArgSplit(returnType); |
| 1108 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1109 | for (const std::string& typeCode : returnTypes) |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1110 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1111 | nlohmann::json* thisElement = &response; |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1112 | if (returnTypes.size() > 1) |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1113 | { |
| 1114 | response.push_back(nlohmann::json{}); |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1115 | thisElement = &response.back(); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1116 | } |
| 1117 | |
| 1118 | if (typeCode == "s") |
| 1119 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1120 | r = readMessageItem<char*>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1121 | if (r < 0) |
| 1122 | { |
| 1123 | return r; |
| 1124 | } |
| 1125 | } |
| 1126 | else if (typeCode == "g") |
| 1127 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1128 | r = readMessageItem<char*>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1129 | if (r < 0) |
| 1130 | { |
| 1131 | return r; |
| 1132 | } |
| 1133 | } |
| 1134 | else if (typeCode == "o") |
| 1135 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1136 | r = readMessageItem<char*>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1137 | if (r < 0) |
| 1138 | { |
| 1139 | return r; |
| 1140 | } |
| 1141 | } |
| 1142 | else if (typeCode == "b") |
| 1143 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1144 | r = readMessageItem<int>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1145 | if (r < 0) |
| 1146 | { |
| 1147 | return r; |
| 1148 | } |
| 1149 | |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1150 | *thisElement = static_cast<bool>(thisElement->get<int>()); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1151 | } |
| 1152 | else if (typeCode == "u") |
| 1153 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1154 | r = readMessageItem<uint32_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1155 | if (r < 0) |
| 1156 | { |
| 1157 | return r; |
| 1158 | } |
| 1159 | } |
| 1160 | else if (typeCode == "i") |
| 1161 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1162 | r = readMessageItem<int32_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1163 | if (r < 0) |
| 1164 | { |
| 1165 | return r; |
| 1166 | } |
| 1167 | } |
| 1168 | else if (typeCode == "x") |
| 1169 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1170 | r = readMessageItem<int64_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1171 | if (r < 0) |
| 1172 | { |
| 1173 | return r; |
| 1174 | } |
| 1175 | } |
| 1176 | else if (typeCode == "t") |
| 1177 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1178 | r = readMessageItem<uint64_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1179 | if (r < 0) |
| 1180 | { |
| 1181 | return r; |
| 1182 | } |
| 1183 | } |
| 1184 | else if (typeCode == "n") |
| 1185 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1186 | r = readMessageItem<int16_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1187 | if (r < 0) |
| 1188 | { |
| 1189 | return r; |
| 1190 | } |
| 1191 | } |
| 1192 | else if (typeCode == "q") |
| 1193 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1194 | r = readMessageItem<uint16_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1195 | if (r < 0) |
| 1196 | { |
| 1197 | return r; |
| 1198 | } |
| 1199 | } |
| 1200 | else if (typeCode == "y") |
| 1201 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1202 | r = readMessageItem<uint8_t>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1203 | if (r < 0) |
| 1204 | { |
| 1205 | return r; |
| 1206 | } |
| 1207 | } |
| 1208 | else if (typeCode == "d") |
| 1209 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1210 | r = readMessageItem<double>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1211 | if (r < 0) |
| 1212 | { |
| 1213 | return r; |
| 1214 | } |
| 1215 | } |
| 1216 | else if (typeCode == "h") |
| 1217 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1218 | r = readMessageItem<int>(typeCode, m, *thisElement); |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1219 | if (r < 0) |
| 1220 | { |
| 1221 | return r; |
| 1222 | } |
| 1223 | } |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 1224 | else if (boost::starts_with(typeCode, "a")) |
| 1225 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1226 | r = readArrayFromMessage(typeCode, m, *thisElement); |
Matt Spinler | 6df8f99 | 2019-01-14 12:47:47 -0600 | [diff] [blame] | 1227 | if (r < 0) |
| 1228 | { |
| 1229 | return r; |
| 1230 | } |
| 1231 | } |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1232 | else if (boost::starts_with(typeCode, "(") && |
| 1233 | boost::ends_with(typeCode, ")")) |
| 1234 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1235 | r = readStructFromMessage(typeCode, m, *thisElement); |
Matt Spinler | 75c6c67 | 2019-01-14 13:01:46 -0600 | [diff] [blame] | 1236 | if (r < 0) |
| 1237 | { |
| 1238 | return r; |
| 1239 | } |
| 1240 | } |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1241 | else if (boost::starts_with(typeCode, "v")) |
| 1242 | { |
Matt Spinler | f39420c | 2019-01-30 12:57:18 -0600 | [diff] [blame] | 1243 | r = readVariantFromMessage(m, *thisElement); |
Matt Spinler | 89c1970 | 2019-01-14 13:13:00 -0600 | [diff] [blame] | 1244 | if (r < 0) |
| 1245 | { |
| 1246 | return r; |
| 1247 | } |
| 1248 | } |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1249 | else |
| 1250 | { |
Matt Spinler | d22a713 | 2019-01-14 12:14:30 -0600 | [diff] [blame] | 1251 | BMCWEB_LOG_ERROR << "Invalid D-Bus signature type " << typeCode; |
| 1252 | return -2; |
| 1253 | } |
| 1254 | } |
| 1255 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1256 | return 0; |
| 1257 | } |
| 1258 | |
| 1259 | void handleMethodResponse(std::shared_ptr<InProgressActionData> transaction, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1260 | sdbusplus::message::message& m, |
| 1261 | const std::string& returnType) |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1262 | { |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 1263 | nlohmann::json data; |
| 1264 | |
| 1265 | int r = convertDBusToJSON(returnType, m, data); |
| 1266 | if (r < 0) |
| 1267 | { |
| 1268 | transaction->outputFailed = true; |
| 1269 | return; |
| 1270 | } |
| 1271 | |
| 1272 | if (data.is_null()) |
| 1273 | { |
| 1274 | return; |
| 1275 | } |
| 1276 | |
| 1277 | if (transaction->methodResponse.is_null()) |
| 1278 | { |
| 1279 | transaction->methodResponse = std::move(data); |
| 1280 | return; |
| 1281 | } |
| 1282 | |
| 1283 | // If they're both dictionaries or arrays, merge into one. |
| 1284 | // Otherwise, make the results an array with every result |
| 1285 | // an entry. Could also just fail in that case, but it |
| 1286 | // seems better to get the data back somehow. |
| 1287 | |
| 1288 | if (transaction->methodResponse.is_object() && data.is_object()) |
| 1289 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1290 | for (const auto& obj : data.items()) |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 1291 | { |
| 1292 | // Note: Will overwrite the data for a duplicate key |
| 1293 | transaction->methodResponse.emplace(obj.key(), |
| 1294 | std::move(obj.value())); |
| 1295 | } |
| 1296 | return; |
| 1297 | } |
| 1298 | |
| 1299 | if (transaction->methodResponse.is_array() && data.is_array()) |
| 1300 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1301 | for (auto& obj : data) |
Matt Spinler | 39a4e39 | 2019-01-15 11:53:13 -0600 | [diff] [blame] | 1302 | { |
| 1303 | transaction->methodResponse.push_back(std::move(obj)); |
| 1304 | } |
| 1305 | return; |
| 1306 | } |
| 1307 | |
| 1308 | if (!transaction->convertedToArray) |
| 1309 | { |
| 1310 | // They are different types. May as well turn them into an array |
| 1311 | nlohmann::json j = std::move(transaction->methodResponse); |
| 1312 | transaction->methodResponse = nlohmann::json::array(); |
| 1313 | transaction->methodResponse.push_back(std::move(j)); |
| 1314 | transaction->methodResponse.push_back(std::move(data)); |
| 1315 | transaction->convertedToArray = true; |
| 1316 | } |
| 1317 | else |
| 1318 | { |
| 1319 | transaction->methodResponse.push_back(std::move(data)); |
| 1320 | } |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1321 | } |
| 1322 | |
Ed Tanous | d76323e | 2018-08-07 14:35:40 -0700 | [diff] [blame] | 1323 | void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1324 | const std::string& connectionName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1325 | { |
| 1326 | BMCWEB_LOG_DEBUG << "findActionOnInterface for connection " |
| 1327 | << connectionName; |
| 1328 | crow::connections::systemBus->async_method_call( |
| 1329 | [transaction, connectionName{std::string(connectionName)}]( |
| 1330 | const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1331 | const std::string& introspect_xml) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1332 | BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml; |
| 1333 | if (ec) |
| 1334 | { |
| 1335 | BMCWEB_LOG_ERROR |
| 1336 | << "Introspect call failed with error: " << ec.message() |
| 1337 | << " on process: " << connectionName << "\n"; |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1338 | return; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1339 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1340 | tinyxml2::XMLDocument doc; |
| 1341 | |
| 1342 | doc.Parse(introspect_xml.data(), introspect_xml.size()); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1343 | tinyxml2::XMLNode* pRoot = doc.FirstChildElement("node"); |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1344 | if (pRoot == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1345 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1346 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 1347 | << connectionName << "\n"; |
| 1348 | return; |
| 1349 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1350 | tinyxml2::XMLElement* interfaceNode = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1351 | pRoot->FirstChildElement("interface"); |
| 1352 | while (interfaceNode != nullptr) |
| 1353 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1354 | const char* thisInterfaceName = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1355 | interfaceNode->Attribute("name"); |
| 1356 | if (thisInterfaceName != nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1357 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1358 | if (!transaction->interfaceName.empty() && |
| 1359 | (transaction->interfaceName != thisInterfaceName)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1360 | { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1361 | interfaceNode = |
| 1362 | interfaceNode->NextSiblingElement("interface"); |
| 1363 | continue; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1364 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1365 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1366 | tinyxml2::XMLElement* methodNode = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1367 | interfaceNode->FirstChildElement("method"); |
| 1368 | while (methodNode != nullptr) |
| 1369 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1370 | const char* thisMethodName = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1371 | methodNode->Attribute("name"); |
| 1372 | BMCWEB_LOG_DEBUG << "Found method: " << thisMethodName; |
| 1373 | if (thisMethodName != nullptr && |
| 1374 | thisMethodName == transaction->methodName) |
| 1375 | { |
| 1376 | BMCWEB_LOG_DEBUG |
| 1377 | << "Found method named " << thisMethodName |
| 1378 | << " on interface " << thisInterfaceName; |
| 1379 | sdbusplus::message::message m = |
| 1380 | crow::connections::systemBus->new_method_call( |
| 1381 | connectionName.c_str(), |
| 1382 | transaction->path.c_str(), |
| 1383 | thisInterfaceName, |
| 1384 | transaction->methodName.c_str()); |
| 1385 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1386 | tinyxml2::XMLElement* argumentNode = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1387 | methodNode->FirstChildElement("arg"); |
| 1388 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1389 | std::string returnType; |
| 1390 | |
| 1391 | // Find the output type |
| 1392 | while (argumentNode != nullptr) |
| 1393 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1394 | const char* argDirection = |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1395 | argumentNode->Attribute("direction"); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1396 | const char* argType = |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1397 | argumentNode->Attribute("type"); |
| 1398 | if (argDirection != nullptr && |
| 1399 | argType != nullptr && |
| 1400 | std::string(argDirection) == "out") |
| 1401 | { |
| 1402 | returnType = argType; |
| 1403 | break; |
| 1404 | } |
| 1405 | argumentNode = |
| 1406 | argumentNode->NextSiblingElement("arg"); |
| 1407 | } |
| 1408 | |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1409 | nlohmann::json::const_iterator argIt = |
| 1410 | transaction->arguments.begin(); |
| 1411 | |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1412 | argumentNode = methodNode->FirstChildElement("arg"); |
| 1413 | |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1414 | while (argumentNode != nullptr) |
| 1415 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1416 | const char* argDirection = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1417 | argumentNode->Attribute("direction"); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1418 | const char* argType = |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1419 | argumentNode->Attribute("type"); |
| 1420 | if (argDirection != nullptr && |
| 1421 | argType != nullptr && |
| 1422 | std::string(argDirection) == "in") |
| 1423 | { |
| 1424 | if (argIt == transaction->arguments.end()) |
| 1425 | { |
| 1426 | transaction->setErrorStatus( |
| 1427 | "Invalid method args"); |
| 1428 | return; |
| 1429 | } |
| 1430 | if (convertJsonToDbus(m.get(), |
| 1431 | std::string(argType), |
| 1432 | *argIt) < 0) |
| 1433 | { |
| 1434 | transaction->setErrorStatus( |
| 1435 | "Invalid method arg type"); |
| 1436 | return; |
| 1437 | } |
| 1438 | |
| 1439 | argIt++; |
| 1440 | } |
| 1441 | argumentNode = |
| 1442 | argumentNode->NextSiblingElement("arg"); |
| 1443 | } |
| 1444 | |
| 1445 | crow::connections::systemBus->async_send( |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1446 | m, [transaction, returnType]( |
| 1447 | boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1448 | sdbusplus::message::message& m) { |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1449 | if (ec) |
| 1450 | { |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1451 | transaction->methodFailed = true; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1452 | const sd_bus_error* e = m.get_error(); |
Matt Spinler | 06b1b63 | 2019-06-18 16:09:25 -0500 | [diff] [blame] | 1453 | |
| 1454 | if (e) |
| 1455 | { |
| 1456 | setErrorResponse( |
| 1457 | transaction->res, |
| 1458 | boost::beast::http::status:: |
| 1459 | bad_request, |
| 1460 | e->name, e->message); |
| 1461 | } |
| 1462 | else |
| 1463 | { |
| 1464 | setErrorResponse( |
| 1465 | transaction->res, |
| 1466 | boost::beast::http::status:: |
| 1467 | bad_request, |
| 1468 | "Method call failed", |
| 1469 | methodFailedMsg); |
| 1470 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1471 | return; |
| 1472 | } |
Matt Spinler | 16caaee | 2019-01-15 11:40:34 -0600 | [diff] [blame] | 1473 | else |
| 1474 | { |
| 1475 | transaction->methodPassed = true; |
| 1476 | } |
| 1477 | |
| 1478 | handleMethodResponse(transaction, m, |
| 1479 | returnType); |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1480 | }); |
| 1481 | break; |
| 1482 | } |
| 1483 | methodNode = methodNode->NextSiblingElement("method"); |
| 1484 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1485 | } |
Matt Spinler | 318bd89 | 2019-01-15 09:59:20 -0600 | [diff] [blame] | 1486 | interfaceNode = interfaceNode->NextSiblingElement("interface"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1487 | } |
| 1488 | }, |
| 1489 | connectionName, transaction->path, |
| 1490 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1491 | } |
| 1492 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1493 | void handleAction(const crow::Request& req, crow::Response& res, |
| 1494 | const std::string& objectPath, const std::string& methodName) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1495 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1496 | BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method " |
| 1497 | << methodName; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1498 | nlohmann::json requestDbusData = |
| 1499 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1500 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1501 | if (requestDbusData.is_discarded()) |
| 1502 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1503 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1504 | noJsonDesc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1505 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1506 | return; |
| 1507 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1508 | nlohmann::json::iterator data = requestDbusData.find("data"); |
| 1509 | if (data == requestDbusData.end()) |
| 1510 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1511 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1512 | noJsonDesc, badReqMsg); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1513 | res.end(); |
| 1514 | return; |
| 1515 | } |
| 1516 | |
| 1517 | if (!data->is_array()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1518 | { |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1519 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1520 | noJsonDesc, badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1521 | res.end(); |
| 1522 | return; |
| 1523 | } |
| 1524 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 1525 | |
| 1526 | transaction->path = objectPath; |
| 1527 | transaction->methodName = methodName; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1528 | transaction->arguments = std::move(*data); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1529 | crow::connections::systemBus->async_method_call( |
| 1530 | [transaction]( |
| 1531 | const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1532 | const std::vector<std::pair<std::string, std::vector<std::string>>>& |
| 1533 | interfaceNames) { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1534 | if (ec || interfaceNames.size() <= 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1535 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1536 | BMCWEB_LOG_ERROR << "Can't find object"; |
Matt Spinler | 6db0624 | 2018-12-11 11:21:22 -0600 | [diff] [blame] | 1537 | setErrorResponse(transaction->res, |
| 1538 | boost::beast::http::status::not_found, |
| 1539 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1540 | return; |
| 1541 | } |
| 1542 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1543 | BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size() |
| 1544 | << " object(s)"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1545 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1546 | for (const std::pair<std::string, std::vector<std::string>>& |
| 1547 | object : interfaceNames) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1548 | { |
| 1549 | findActionOnInterface(transaction, object.first); |
| 1550 | } |
| 1551 | }, |
| 1552 | "xyz.openbmc_project.ObjectMapper", |
| 1553 | "/xyz/openbmc_project/object_mapper", |
| 1554 | "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath, |
| 1555 | std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1556 | } |
| 1557 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1558 | void handleDelete(const crow::Request& req, crow::Response& res, |
| 1559 | const std::string& objectPath) |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1560 | { |
| 1561 | BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath; |
| 1562 | |
| 1563 | crow::connections::systemBus->async_method_call( |
| 1564 | [&res, objectPath]( |
| 1565 | const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1566 | const std::vector<std::pair<std::string, std::vector<std::string>>>& |
| 1567 | interfaceNames) { |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1568 | if (ec || interfaceNames.size() <= 0) |
| 1569 | { |
| 1570 | BMCWEB_LOG_ERROR << "Can't find object"; |
Matt Spinler | 62d2e8b | 2019-01-22 13:45:51 -0600 | [diff] [blame] | 1571 | setErrorResponse(res, |
| 1572 | boost::beast::http::status::method_not_allowed, |
| 1573 | methodNotAllowedDesc, methodNotAllowedMsg); |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1574 | res.end(); |
| 1575 | return; |
| 1576 | } |
| 1577 | |
| 1578 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 1579 | transaction->path = objectPath; |
| 1580 | transaction->methodName = "Delete"; |
| 1581 | transaction->interfaceName = "xyz.openbmc_project.Object.Delete"; |
| 1582 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1583 | for (const std::pair<std::string, std::vector<std::string>>& |
| 1584 | object : interfaceNames) |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1585 | { |
| 1586 | findActionOnInterface(transaction, object.first); |
| 1587 | } |
| 1588 | }, |
| 1589 | "xyz.openbmc_project.ObjectMapper", |
| 1590 | "/xyz/openbmc_project/object_mapper", |
| 1591 | "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1592 | std::array<const char*, 0>()); |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 1593 | } |
| 1594 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1595 | void handleList(crow::Response& res, const std::string& objectPath, |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 1596 | int32_t depth = 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1597 | { |
| 1598 | crow::connections::systemBus->async_method_call( |
| 1599 | [&res](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1600 | std::vector<std::string>& objectPaths) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1601 | if (ec) |
| 1602 | { |
Matt Spinler | d6091dd | 2018-12-06 14:08:27 -0600 | [diff] [blame] | 1603 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1604 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1605 | } |
| 1606 | else |
| 1607 | { |
| 1608 | res.jsonValue = {{"status", "ok"}, |
| 1609 | {"message", "200 OK"}, |
| 1610 | {"data", std::move(objectPaths)}}; |
| 1611 | } |
| 1612 | res.end(); |
| 1613 | }, |
| 1614 | "xyz.openbmc_project.ObjectMapper", |
| 1615 | "/xyz/openbmc_project/object_mapper", |
| 1616 | "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath, |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 1617 | depth, std::array<std::string, 0>()); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1618 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1619 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1620 | void handleEnumerate(crow::Response& res, const std::string& objectPath) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1621 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 1622 | BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath; |
| 1623 | auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res); |
| 1624 | |
| 1625 | asyncResp->res.jsonValue = {{"message", "200 OK"}, |
| 1626 | {"status", "ok"}, |
| 1627 | {"data", nlohmann::json::object()}}; |
| 1628 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1629 | crow::connections::systemBus->async_method_call( |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1630 | [objectPath, asyncResp](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1631 | GetSubTreeType& object_names) { |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1632 | auto transaction = std::make_shared<InProgressEnumerateData>( |
| 1633 | objectPath, asyncResp); |
| 1634 | |
| 1635 | transaction->subtree = |
| 1636 | std::make_shared<GetSubTreeType>(std::move(object_names)); |
| 1637 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1638 | if (ec) |
| 1639 | { |
Matt Spinler | 2ae6009 | 2018-12-06 10:35:36 -0600 | [diff] [blame] | 1640 | BMCWEB_LOG_ERROR << "GetSubTree failed on " |
| 1641 | << transaction->objectPath; |
| 1642 | setErrorResponse(transaction->asyncResp->res, |
| 1643 | boost::beast::http::status::not_found, |
| 1644 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1645 | return; |
| 1646 | } |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1647 | |
Matt Spinler | 3ae4ba7 | 2018-12-05 14:01:22 -0600 | [diff] [blame] | 1648 | // Add the data for the path passed in to the results |
| 1649 | // as if GetSubTree returned it, and continue on enumerating |
| 1650 | getObjectAndEnumerate(transaction); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1651 | }, |
| 1652 | "xyz.openbmc_project.ObjectMapper", |
| 1653 | "/xyz/openbmc_project/object_mapper", |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 1654 | "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath, 0, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1655 | std::array<const char*, 0>()); |
Ed Tanous | 6453001 | 2018-02-06 17:08:16 -0800 | [diff] [blame] | 1656 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1657 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1658 | void handleGet(crow::Response& res, std::string& objectPath, |
| 1659 | std::string& destProperty) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1660 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1661 | BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty; |
| 1662 | std::shared_ptr<std::string> propertyName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1663 | std::make_shared<std::string>(std::move(destProperty)); |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 1664 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1665 | std::shared_ptr<std::string> path = |
| 1666 | std::make_shared<std::string>(std::move(objectPath)); |
Ed Tanous | 75db20e | 2018-07-27 13:44:44 -0700 | [diff] [blame] | 1667 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1668 | using GetObjectType = |
| 1669 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
| 1670 | crow::connections::systemBus->async_method_call( |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 1671 | [&res, path, propertyName](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1672 | const GetObjectType& object_names) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1673 | if (ec || object_names.size() <= 0) |
| 1674 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1675 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1676 | notFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1677 | res.end(); |
| 1678 | return; |
| 1679 | } |
| 1680 | std::shared_ptr<nlohmann::json> response = |
| 1681 | std::make_shared<nlohmann::json>(nlohmann::json::object()); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 1682 | // The mapper should never give us an empty interface names |
| 1683 | // list, but check anyway |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1684 | for (const std::pair<std::string, std::vector<std::string>> |
| 1685 | connection : object_names) |
| 1686 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1687 | const std::vector<std::string>& interfaceNames = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1688 | connection.second; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1689 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1690 | if (interfaceNames.size() <= 0) |
| 1691 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1692 | setErrorResponse(res, boost::beast::http::status::not_found, |
| 1693 | notFoundDesc, notFoundMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1694 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1695 | return; |
| 1696 | } |
| 1697 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1698 | for (const std::string& interface : interfaceNames) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1699 | { |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1700 | sdbusplus::message::message m = |
| 1701 | crow::connections::systemBus->new_method_call( |
| 1702 | connection.first.c_str(), path->c_str(), |
| 1703 | "org.freedesktop.DBus.Properties", "GetAll"); |
| 1704 | m.append(interface); |
| 1705 | crow::connections::systemBus->async_send( |
| 1706 | m, [&res, response, |
| 1707 | propertyName](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1708 | sdbusplus::message::message& msg) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1709 | if (ec) |
| 1710 | { |
| 1711 | BMCWEB_LOG_ERROR << "Bad dbus request error: " |
| 1712 | << ec; |
| 1713 | } |
| 1714 | else |
| 1715 | { |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1716 | nlohmann::json properties; |
| 1717 | int r = |
| 1718 | convertDBusToJSON("a{sv}", msg, properties); |
| 1719 | if (r < 0) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1720 | { |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1721 | BMCWEB_LOG_ERROR |
| 1722 | << "convertDBusToJSON failed"; |
| 1723 | } |
| 1724 | else |
| 1725 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1726 | for (auto& prop : properties.items()) |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1727 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 1728 | // if property name is empty, or |
| 1729 | // matches our search query, add it |
| 1730 | // to the response json |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1731 | |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1732 | if (propertyName->empty()) |
| 1733 | { |
| 1734 | (*response)[prop.key()] = |
| 1735 | std::move(prop.value()); |
| 1736 | } |
| 1737 | else if (prop.key() == *propertyName) |
| 1738 | { |
| 1739 | *response = std::move(prop.value()); |
| 1740 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1741 | } |
| 1742 | } |
| 1743 | } |
| 1744 | if (response.use_count() == 1) |
| 1745 | { |
Matt Spinler | dc2f9f1 | 2018-12-06 13:53:53 -0600 | [diff] [blame] | 1746 | if (!propertyName->empty() && response->empty()) |
| 1747 | { |
| 1748 | setErrorResponse( |
| 1749 | res, |
| 1750 | boost::beast::http::status::not_found, |
| 1751 | propNotFoundDesc, notFoundMsg); |
| 1752 | } |
| 1753 | else |
| 1754 | { |
| 1755 | res.jsonValue = {{"status", "ok"}, |
| 1756 | {"message", "200 OK"}, |
| 1757 | {"data", *response}}; |
| 1758 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1759 | res.end(); |
| 1760 | } |
Matt Spinler | fe7e97d | 2019-01-30 15:33:21 -0600 | [diff] [blame] | 1761 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1762 | } |
| 1763 | } |
| 1764 | }, |
| 1765 | "xyz.openbmc_project.ObjectMapper", |
| 1766 | "/xyz/openbmc_project/object_mapper", |
| 1767 | "xyz.openbmc_project.ObjectMapper", "GetObject", *path, |
| 1768 | std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1771 | struct AsyncPutRequest |
| 1772 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1773 | AsyncPutRequest(crow::Response& res) : res(res) |
| 1774 | {} |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1775 | ~AsyncPutRequest() |
| 1776 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1777 | if (res.jsonValue.empty()) |
| 1778 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1779 | setErrorResponse(res, boost::beast::http::status::forbidden, |
| 1780 | forbiddenMsg, forbiddenPropDesc); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1781 | } |
| 1782 | |
| 1783 | res.end(); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1784 | } |
| 1785 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1786 | void setErrorStatus(const std::string& desc) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1787 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1788 | setErrorResponse(res, boost::beast::http::status::internal_server_error, |
| 1789 | desc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1790 | } |
| 1791 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1792 | crow::Response& res; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1793 | std::string objectPath; |
| 1794 | std::string propertyName; |
| 1795 | nlohmann::json propertyValue; |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1796 | }; |
| 1797 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1798 | void handlePut(const crow::Request& req, crow::Response& res, |
| 1799 | const std::string& objectPath, const std::string& destProperty) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1800 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1801 | if (destProperty.empty()) |
| 1802 | { |
| 1803 | setErrorResponse(res, boost::beast::http::status::forbidden, |
| 1804 | forbiddenResDesc, forbiddenMsg); |
| 1805 | res.end(); |
| 1806 | return; |
| 1807 | } |
| 1808 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1809 | nlohmann::json requestDbusData = |
| 1810 | nlohmann::json::parse(req.body, nullptr, false); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1811 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1812 | if (requestDbusData.is_discarded()) |
| 1813 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1814 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1815 | noJsonDesc, badReqMsg); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1816 | res.end(); |
| 1817 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1818 | } |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1819 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1820 | nlohmann::json::const_iterator propertyIt = requestDbusData.find("data"); |
| 1821 | if (propertyIt == requestDbusData.end()) |
| 1822 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1823 | setErrorResponse(res, boost::beast::http::status::bad_request, |
| 1824 | noJsonDesc, badReqMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1825 | res.end(); |
| 1826 | return; |
| 1827 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1828 | const nlohmann::json& propertySetValue = *propertyIt; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1829 | auto transaction = std::make_shared<AsyncPutRequest>(res); |
| 1830 | transaction->objectPath = objectPath; |
| 1831 | transaction->propertyName = destProperty; |
| 1832 | transaction->propertyValue = propertySetValue; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1833 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1834 | using GetObjectType = |
| 1835 | std::vector<std::pair<std::string, std::vector<std::string>>>; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1836 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1837 | crow::connections::systemBus->async_method_call( |
| 1838 | [transaction](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1839 | const GetObjectType& object_names) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1840 | if (!ec && object_names.size() <= 0) |
| 1841 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1842 | setErrorResponse(transaction->res, |
| 1843 | boost::beast::http::status::not_found, |
| 1844 | propNotFoundDesc, notFoundMsg); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1845 | return; |
| 1846 | } |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1847 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1848 | for (const std::pair<std::string, std::vector<std::string>> |
| 1849 | connection : object_names) |
| 1850 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1851 | const std::string& connectionName = connection.first; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1852 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1853 | crow::connections::systemBus->async_method_call( |
| 1854 | [connectionName{std::string(connectionName)}, |
| 1855 | transaction](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1856 | const std::string& introspectXml) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1857 | if (ec) |
| 1858 | { |
| 1859 | BMCWEB_LOG_ERROR |
| 1860 | << "Introspect call failed with error: " |
| 1861 | << ec.message() |
| 1862 | << " on process: " << connectionName; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1863 | transaction->setErrorStatus("Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1864 | return; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1865 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1866 | tinyxml2::XMLDocument doc; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1867 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1868 | doc.Parse(introspectXml.c_str()); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1869 | tinyxml2::XMLNode* pRoot = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1870 | doc.FirstChildElement("node"); |
| 1871 | if (pRoot == nullptr) |
| 1872 | { |
| 1873 | BMCWEB_LOG_ERROR << "XML document failed to parse: " |
| 1874 | << introspectXml; |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1875 | transaction->setErrorStatus("Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1876 | return; |
Ed Tanous | 911ac31 | 2017-08-15 09:37:42 -0700 | [diff] [blame] | 1877 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1878 | tinyxml2::XMLElement* ifaceNode = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1879 | pRoot->FirstChildElement("interface"); |
| 1880 | while (ifaceNode != nullptr) |
| 1881 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1882 | const char* interfaceName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1883 | ifaceNode->Attribute("name"); |
| 1884 | BMCWEB_LOG_DEBUG << "found interface " |
| 1885 | << interfaceName; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1886 | tinyxml2::XMLElement* propNode = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1887 | ifaceNode->FirstChildElement("property"); |
| 1888 | while (propNode != nullptr) |
| 1889 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1890 | const char* propertyName = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1891 | propNode->Attribute("name"); |
| 1892 | BMCWEB_LOG_DEBUG << "Found property " |
| 1893 | << propertyName; |
| 1894 | if (propertyName == transaction->propertyName) |
| 1895 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1896 | const char* argType = |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1897 | propNode->Attribute("type"); |
| 1898 | if (argType != nullptr) |
| 1899 | { |
| 1900 | sdbusplus::message::message m = |
| 1901 | crow::connections::systemBus |
| 1902 | ->new_method_call( |
| 1903 | connectionName.c_str(), |
| 1904 | transaction->objectPath |
| 1905 | .c_str(), |
| 1906 | "org.freedesktop.DBus." |
| 1907 | "Properties", |
| 1908 | "Set"); |
| 1909 | m.append(interfaceName, |
| 1910 | transaction->propertyName); |
| 1911 | int r = sd_bus_message_open_container( |
| 1912 | m.get(), SD_BUS_TYPE_VARIANT, |
| 1913 | argType); |
| 1914 | if (r < 0) |
| 1915 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1916 | transaction->setErrorStatus( |
| 1917 | "Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1918 | return; |
| 1919 | } |
| 1920 | r = convertJsonToDbus( |
| 1921 | m.get(), argType, |
| 1922 | transaction->propertyValue); |
| 1923 | if (r < 0) |
| 1924 | { |
Adriana Kobylak | c66c859 | 2019-08-06 15:08:05 -0500 | [diff] [blame] | 1925 | if (r == -ERANGE) |
| 1926 | { |
| 1927 | transaction->setErrorStatus( |
| 1928 | "Provided property value " |
| 1929 | "is out of range for the " |
| 1930 | "property type"); |
| 1931 | } |
| 1932 | else |
| 1933 | { |
| 1934 | transaction->setErrorStatus( |
| 1935 | "Invalid arg type"); |
| 1936 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1937 | return; |
| 1938 | } |
| 1939 | r = sd_bus_message_close_container( |
| 1940 | m.get()); |
| 1941 | if (r < 0) |
| 1942 | { |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1943 | transaction->setErrorStatus( |
| 1944 | "Unexpected Error"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1945 | return; |
| 1946 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1947 | crow::connections::systemBus |
| 1948 | ->async_send( |
| 1949 | m, |
| 1950 | [transaction]( |
| 1951 | boost::system::error_code |
| 1952 | ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1953 | sdbusplus::message::message& |
| 1954 | m) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1955 | BMCWEB_LOG_DEBUG << "sent"; |
| 1956 | if (ec) |
| 1957 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 1958 | const sd_bus_error* e = |
Lei YU | 97d2a47 | 2019-06-11 17:44:27 +0800 | [diff] [blame] | 1959 | m.get_error(); |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1960 | setErrorResponse( |
| 1961 | transaction->res, |
| 1962 | boost::beast::http:: |
| 1963 | status:: |
| 1964 | forbidden, |
Matt Spinler | 06b1b63 | 2019-06-18 16:09:25 -0500 | [diff] [blame] | 1965 | (e) ? e->name |
| 1966 | : ec.category() |
| 1967 | .name(), |
| 1968 | (e) ? e->message |
| 1969 | : ec.message()); |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1970 | } |
| 1971 | else |
| 1972 | { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1973 | transaction->res |
Matt Spinler | fbc19ea | 2018-12-11 14:03:42 -0600 | [diff] [blame] | 1974 | .jsonValue = { |
| 1975 | {"status", "ok"}, |
| 1976 | {"message", |
| 1977 | "200 OK"}, |
| 1978 | {"data", nullptr}}; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1979 | } |
| 1980 | }); |
| 1981 | } |
| 1982 | } |
| 1983 | propNode = |
| 1984 | propNode->NextSiblingElement("property"); |
| 1985 | } |
| 1986 | ifaceNode = |
| 1987 | ifaceNode->NextSiblingElement("interface"); |
| 1988 | } |
| 1989 | }, |
| 1990 | connectionName, transaction->objectPath, |
| 1991 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 1992 | } |
| 1993 | }, |
| 1994 | "xyz.openbmc_project.ObjectMapper", |
| 1995 | "/xyz/openbmc_project/object_mapper", |
| 1996 | "xyz.openbmc_project.ObjectMapper", "GetObject", |
| 1997 | transaction->objectPath, std::array<std::string, 0>()); |
Ed Tanous | d4bb9bb | 2018-05-16 13:36:42 -0700 | [diff] [blame] | 1998 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 1999 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2000 | inline void handleDBusUrl(const crow::Request& req, crow::Response& res, |
| 2001 | std::string& objectPath) |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2002 | { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2003 | |
| 2004 | // If accessing a single attribute, fill in and update objectPath, |
| 2005 | // otherwise leave destProperty blank |
| 2006 | std::string destProperty = ""; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2007 | const char* attrSeperator = "/attr/"; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2008 | size_t attrPosition = objectPath.find(attrSeperator); |
| 2009 | if (attrPosition != objectPath.npos) |
| 2010 | { |
| 2011 | destProperty = objectPath.substr(attrPosition + strlen(attrSeperator), |
| 2012 | objectPath.length()); |
| 2013 | objectPath = objectPath.substr(0, attrPosition); |
| 2014 | } |
| 2015 | |
| 2016 | if (req.method() == "POST"_method) |
| 2017 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2018 | constexpr const char* actionSeperator = "/action/"; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2019 | size_t actionPosition = objectPath.find(actionSeperator); |
| 2020 | if (actionPosition != objectPath.npos) |
| 2021 | { |
| 2022 | std::string postProperty = |
| 2023 | objectPath.substr((actionPosition + strlen(actionSeperator)), |
| 2024 | objectPath.length()); |
| 2025 | objectPath = objectPath.substr(0, actionPosition); |
| 2026 | handleAction(req, res, objectPath, postProperty); |
| 2027 | return; |
| 2028 | } |
| 2029 | } |
| 2030 | else if (req.method() == "GET"_method) |
| 2031 | { |
| 2032 | if (boost::ends_with(objectPath, "/enumerate")) |
| 2033 | { |
| 2034 | objectPath.erase(objectPath.end() - sizeof("enumerate"), |
| 2035 | objectPath.end()); |
| 2036 | handleEnumerate(res, objectPath); |
| 2037 | } |
| 2038 | else if (boost::ends_with(objectPath, "/list")) |
| 2039 | { |
| 2040 | objectPath.erase(objectPath.end() - sizeof("list"), |
| 2041 | objectPath.end()); |
| 2042 | handleList(res, objectPath); |
| 2043 | } |
| 2044 | else |
| 2045 | { |
Ed Tanous | f839dfe | 2018-11-12 11:11:15 -0800 | [diff] [blame] | 2046 | // Trim any trailing "/" at the end |
| 2047 | if (boost::ends_with(objectPath, "/")) |
| 2048 | { |
| 2049 | objectPath.pop_back(); |
| 2050 | handleList(res, objectPath, 1); |
| 2051 | } |
| 2052 | else |
| 2053 | { |
| 2054 | handleGet(res, objectPath, destProperty); |
| 2055 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2056 | } |
| 2057 | return; |
| 2058 | } |
| 2059 | else if (req.method() == "PUT"_method) |
| 2060 | { |
| 2061 | handlePut(req, res, objectPath, destProperty); |
| 2062 | return; |
| 2063 | } |
Matt Spinler | de81881 | 2018-12-11 16:39:20 -0600 | [diff] [blame] | 2064 | else if (req.method() == "DELETE"_method) |
| 2065 | { |
| 2066 | handleDelete(req, res, objectPath); |
| 2067 | return; |
| 2068 | } |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2069 | |
Matt Spinler | c4e8d21d6 | 2018-12-11 11:47:17 -0600 | [diff] [blame] | 2070 | setErrorResponse(res, boost::beast::http::status::method_not_allowed, |
| 2071 | methodNotAllowedDesc, methodNotAllowedMsg); |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2072 | res.end(); |
| 2073 | } |
| 2074 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2075 | template <typename... Middlewares> |
| 2076 | void requestRoutes(Crow<Middlewares...>& app) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2077 | { |
| 2078 | BMCWEB_ROUTE(app, "/bus/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2079 | .requires({"Login"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2080 | .methods("GET"_method)( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2081 | [](const crow::Request& req, crow::Response& res) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2082 | res.jsonValue = {{"busses", {{{"name", "system"}}}}, |
| 2083 | {"status", "ok"}}; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2084 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2085 | }); |
| 2086 | |
| 2087 | BMCWEB_ROUTE(app, "/bus/system/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2088 | .requires({"Login"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2089 | .methods("GET"_method)( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2090 | [](const crow::Request& req, crow::Response& res) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2091 | auto myCallback = [&res](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2092 | std::vector<std::string>& names) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2093 | if (ec) |
| 2094 | { |
| 2095 | BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec; |
| 2096 | res.result( |
| 2097 | boost::beast::http::status::internal_server_error); |
| 2098 | } |
| 2099 | else |
| 2100 | { |
| 2101 | std::sort(names.begin(), names.end()); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2102 | res.jsonValue = {{"status", "ok"}}; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2103 | auto& objectsSub = res.jsonValue["objects"]; |
| 2104 | for (auto& name : names) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2105 | { |
| 2106 | objectsSub.push_back({{"name", name}}); |
| 2107 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2108 | } |
| 2109 | res.end(); |
| 2110 | }; |
| 2111 | crow::connections::systemBus->async_method_call( |
| 2112 | std::move(myCallback), "org.freedesktop.DBus", "/", |
| 2113 | "org.freedesktop.DBus", "ListNames"); |
| 2114 | }); |
| 2115 | |
| 2116 | BMCWEB_ROUTE(app, "/list/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2117 | .requires({"Login"}) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2118 | .methods("GET"_method)( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2119 | [](const crow::Request& req, crow::Response& res) { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2120 | handleList(res, "/"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2121 | }); |
| 2122 | |
| 2123 | BMCWEB_ROUTE(app, "/xyz/<path>") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2124 | .requires({"Login"}) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2125 | .methods("GET"_method)([](const crow::Request& req, crow::Response& res, |
| 2126 | const std::string& path) { |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2127 | std::string objectPath = "/xyz/" + path; |
| 2128 | handleDBusUrl(req, res, objectPath); |
| 2129 | }); |
| 2130 | |
| 2131 | BMCWEB_ROUTE(app, "/xyz/<path>") |
| 2132 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 2133 | .methods("PUT"_method, "POST"_method, "DELETE"_method)( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2134 | [](const crow::Request& req, crow::Response& res, |
| 2135 | const std::string& path) { |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2136 | std::string objectPath = "/xyz/" + path; |
| 2137 | handleDBusUrl(req, res, objectPath); |
| 2138 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2139 | |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2140 | BMCWEB_ROUTE(app, "/org/<path>") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2141 | .requires({"Login"}) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2142 | .methods("GET"_method)([](const crow::Request& req, crow::Response& res, |
| 2143 | const std::string& path) { |
Ed Tanous | e128140 | 2019-04-03 07:07:10 -0700 | [diff] [blame] | 2144 | std::string objectPath = "/org/" + path; |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2145 | handleDBusUrl(req, res, objectPath); |
| 2146 | }); |
| 2147 | |
| 2148 | BMCWEB_ROUTE(app, "/org/<path>") |
| 2149 | .requires({"ConfigureComponents", "ConfigureManager"}) |
| 2150 | .methods("PUT"_method, "POST"_method, "DELETE"_method)( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2151 | [](const crow::Request& req, crow::Response& res, |
| 2152 | const std::string& path) { |
Ed Tanous | e128140 | 2019-04-03 07:07:10 -0700 | [diff] [blame] | 2153 | std::string objectPath = "/org/" + path; |
Ed Tanous | 049a051 | 2018-11-01 13:58:42 -0700 | [diff] [blame] | 2154 | handleDBusUrl(req, res, objectPath); |
| 2155 | }); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2156 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2157 | BMCWEB_ROUTE(app, "/download/dump/<str>/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2158 | .requires({"ConfigureManager"}) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2159 | .methods("GET"_method)([](const crow::Request& req, crow::Response& res, |
| 2160 | const std::string& dumpId) { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2161 | std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]*)$"); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2162 | if (!std::regex_match(dumpId, validFilename)) |
| 2163 | { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2164 | res.result(boost::beast::http::status::bad_request); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2165 | res.end(); |
| 2166 | return; |
| 2167 | } |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 2168 | std::filesystem::path loc( |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2169 | "/var/lib/phosphor-debug-collector/dumps"); |
| 2170 | |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2171 | loc /= dumpId; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2172 | |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 2173 | if (!std::filesystem::exists(loc) || |
| 2174 | !std::filesystem::is_directory(loc)) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2175 | { |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2176 | BMCWEB_LOG_ERROR << loc << "Not found"; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2177 | res.result(boost::beast::http::status::not_found); |
| 2178 | res.end(); |
| 2179 | return; |
| 2180 | } |
James Feist | f615040 | 2019-01-08 10:36:20 -0800 | [diff] [blame] | 2181 | std::filesystem::directory_iterator files(loc); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2182 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2183 | for (auto& file : files) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2184 | { |
| 2185 | std::ifstream readFile(file.path()); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2186 | if (!readFile.good()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2187 | { |
| 2188 | continue; |
| 2189 | } |
Ramesh Iyyar | d920704 | 2019-07-05 08:04:42 -0500 | [diff] [blame] | 2190 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2191 | res.addHeader("Content-Type", "application/octet-stream"); |
Ramesh Iyyar | d920704 | 2019-07-05 08:04:42 -0500 | [diff] [blame] | 2192 | |
| 2193 | // Assuming only one dump file will be present in the dump id |
| 2194 | // directory |
| 2195 | std::string dumpFileName = file.path().filename().string(); |
| 2196 | |
| 2197 | // Filename should be in alphanumeric, dot and underscore |
| 2198 | // Its based on phosphor-debug-collector application dumpfile |
| 2199 | // format |
| 2200 | std::regex dumpFileRegex("[a-zA-Z0-9\\._]+"); |
| 2201 | if (!std::regex_match(dumpFileName, dumpFileRegex)) |
| 2202 | { |
| 2203 | BMCWEB_LOG_ERROR << "Invalid dump filename " |
| 2204 | << dumpFileName; |
| 2205 | res.result(boost::beast::http::status::not_found); |
| 2206 | res.end(); |
| 2207 | return; |
| 2208 | } |
| 2209 | std::string contentDispositionParam = |
| 2210 | "attachment; filename=\"" + dumpFileName + "\""; |
| 2211 | |
| 2212 | res.addHeader("Content-Disposition", contentDispositionParam); |
| 2213 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2214 | res.body() = {std::istreambuf_iterator<char>(readFile), |
| 2215 | std::istreambuf_iterator<char>()}; |
| 2216 | res.end(); |
Ed Tanous | ad18f07 | 2018-11-14 14:07:48 -0800 | [diff] [blame] | 2217 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2218 | } |
| 2219 | res.result(boost::beast::http::status::not_found); |
| 2220 | res.end(); |
| 2221 | return; |
| 2222 | }); |
| 2223 | |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2224 | BMCWEB_ROUTE(app, "/bus/system/<str>/") |
Tanous | f00032d | 2018-11-05 01:18:10 -0300 | [diff] [blame] | 2225 | .requires({"Login"}) |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2226 | .methods("GET"_method)([](const crow::Request& req, crow::Response& res, |
| 2227 | const std::string& Connection) { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2228 | introspectObjects(Connection, "/", |
| 2229 | std::make_shared<bmcweb::AsyncResp>(res)); |
| 2230 | }); |
| 2231 | |
| 2232 | BMCWEB_ROUTE(app, "/bus/system/<str>/<path>") |
Ed Tanous | 8251ffe | 2019-10-10 14:33:54 -0700 | [diff] [blame] | 2233 | .requires({"ConfigureComponents", "ConfigureManager"}) |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2234 | .methods("GET"_method, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2235 | "POST"_method)([](const crow::Request& req, |
| 2236 | crow::Response& res, |
| 2237 | const std::string& processName, |
| 2238 | const std::string& requestedPath) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2239 | std::vector<std::string> strs; |
| 2240 | boost::split(strs, requestedPath, boost::is_any_of("/")); |
| 2241 | std::string objectPath; |
| 2242 | std::string interfaceName; |
| 2243 | std::string methodName; |
| 2244 | auto it = strs.begin(); |
| 2245 | if (it == strs.end()) |
| 2246 | { |
| 2247 | objectPath = "/"; |
| 2248 | } |
| 2249 | while (it != strs.end()) |
| 2250 | { |
| 2251 | // Check if segment contains ".". If it does, it must be an |
| 2252 | // interface |
| 2253 | if (it->find(".") != std::string::npos) |
| 2254 | { |
| 2255 | break; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2256 | // This check is neccesary as the trailing slash gets |
| 2257 | // parsed as part of our <path> specifier above, which |
| 2258 | // causes the normal trailing backslash redirector to |
| 2259 | // fail. |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2260 | } |
| 2261 | else if (!it->empty()) |
| 2262 | { |
| 2263 | objectPath += "/" + *it; |
| 2264 | } |
| 2265 | it++; |
| 2266 | } |
| 2267 | if (it != strs.end()) |
| 2268 | { |
| 2269 | interfaceName = *it; |
| 2270 | it++; |
| 2271 | |
| 2272 | // after interface, we might have a method name |
| 2273 | if (it != strs.end()) |
| 2274 | { |
| 2275 | methodName = *it; |
| 2276 | it++; |
| 2277 | } |
| 2278 | } |
| 2279 | if (it != strs.end()) |
| 2280 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2281 | // if there is more levels past the method name, something |
| 2282 | // went wrong, return not found |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2283 | res.result(boost::beast::http::status::not_found); |
AppaRao Puli | 3c27ed3 | 2020-03-31 01:21:57 +0530 | [diff] [blame] | 2284 | res.end(); |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2285 | return; |
| 2286 | } |
| 2287 | if (interfaceName.empty()) |
| 2288 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2289 | std::shared_ptr<bmcweb::AsyncResp> asyncResp = |
| 2290 | std::make_shared<bmcweb::AsyncResp>(res); |
| 2291 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2292 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2293 | [asyncResp, processName, |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2294 | objectPath](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2295 | const std::string& introspect_xml) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2296 | if (ec) |
| 2297 | { |
| 2298 | BMCWEB_LOG_ERROR |
| 2299 | << "Introspect call failed with error: " |
| 2300 | << ec.message() |
| 2301 | << " on process: " << processName |
| 2302 | << " path: " << objectPath << "\n"; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2303 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2304 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2305 | tinyxml2::XMLDocument doc; |
| 2306 | |
| 2307 | doc.Parse(introspect_xml.c_str()); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2308 | tinyxml2::XMLNode* pRoot = |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2309 | doc.FirstChildElement("node"); |
| 2310 | if (pRoot == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2311 | { |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2312 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 2313 | << processName << " " << objectPath |
| 2314 | << "\n"; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2315 | asyncResp->res.jsonValue = { |
| 2316 | {"status", "XML parse error"}}; |
| 2317 | asyncResp->res.result(boost::beast::http::status:: |
| 2318 | internal_server_error); |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2319 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2320 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2321 | |
| 2322 | BMCWEB_LOG_DEBUG << introspect_xml; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2323 | asyncResp->res.jsonValue = { |
| 2324 | {"status", "ok"}, |
| 2325 | {"bus_name", processName}, |
| 2326 | {"object_path", objectPath}}; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2327 | nlohmann::json& interfacesArray = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2328 | asyncResp->res.jsonValue["interfaces"]; |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2329 | interfacesArray = nlohmann::json::array(); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2330 | tinyxml2::XMLElement* interface = |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2331 | pRoot->FirstChildElement("interface"); |
| 2332 | |
| 2333 | while (interface != nullptr) |
| 2334 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2335 | const char* ifaceName = |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2336 | interface->Attribute("name"); |
| 2337 | if (ifaceName != nullptr) |
| 2338 | { |
| 2339 | interfacesArray.push_back( |
| 2340 | {{"name", ifaceName}}); |
| 2341 | } |
| 2342 | |
| 2343 | interface = |
| 2344 | interface->NextSiblingElement("interface"); |
| 2345 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2346 | }, |
| 2347 | processName, objectPath, |
| 2348 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 2349 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2350 | else if (methodName.empty()) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2351 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2352 | std::shared_ptr<bmcweb::AsyncResp> asyncResp = |
| 2353 | std::make_shared<bmcweb::AsyncResp>(res); |
| 2354 | |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2355 | crow::connections::systemBus->async_method_call( |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2356 | [asyncResp, processName, objectPath, |
| 2357 | interfaceName](const boost::system::error_code ec, |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2358 | const std::string& introspect_xml) { |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2359 | if (ec) |
| 2360 | { |
| 2361 | BMCWEB_LOG_ERROR |
| 2362 | << "Introspect call failed with error: " |
| 2363 | << ec.message() |
| 2364 | << " on process: " << processName |
| 2365 | << " path: " << objectPath << "\n"; |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2366 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2367 | } |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2368 | tinyxml2::XMLDocument doc; |
| 2369 | |
| 2370 | doc.Parse(introspect_xml.data(), introspect_xml.size()); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2371 | tinyxml2::XMLNode* pRoot = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2372 | doc.FirstChildElement("node"); |
| 2373 | if (pRoot == nullptr) |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2374 | { |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2375 | BMCWEB_LOG_ERROR << "XML document failed to parse " |
| 2376 | << processName << " " << objectPath |
| 2377 | << "\n"; |
| 2378 | asyncResp->res.result(boost::beast::http::status:: |
| 2379 | internal_server_error); |
| 2380 | return; |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2381 | } |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2382 | asyncResp->res.jsonValue = { |
| 2383 | {"status", "ok"}, |
| 2384 | {"bus_name", processName}, |
| 2385 | {"interface", interfaceName}, |
| 2386 | {"object_path", objectPath}}; |
| 2387 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2388 | nlohmann::json& methodsArray = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2389 | asyncResp->res.jsonValue["methods"]; |
| 2390 | methodsArray = nlohmann::json::array(); |
| 2391 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2392 | nlohmann::json& signalsArray = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2393 | asyncResp->res.jsonValue["signals"]; |
| 2394 | signalsArray = nlohmann::json::array(); |
| 2395 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2396 | nlohmann::json& propertiesObj = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2397 | asyncResp->res.jsonValue["properties"]; |
| 2398 | propertiesObj = nlohmann::json::object(); |
| 2399 | |
| 2400 | // if we know we're the only call, build the |
| 2401 | // json directly |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2402 | tinyxml2::XMLElement* interface = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2403 | pRoot->FirstChildElement("interface"); |
| 2404 | while (interface != nullptr) |
| 2405 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2406 | const char* ifaceName = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2407 | interface->Attribute("name"); |
| 2408 | |
| 2409 | if (ifaceName != nullptr && |
| 2410 | ifaceName == interfaceName) |
| 2411 | { |
| 2412 | break; |
| 2413 | } |
| 2414 | |
| 2415 | interface = |
| 2416 | interface->NextSiblingElement("interface"); |
| 2417 | } |
| 2418 | if (interface == nullptr) |
| 2419 | { |
| 2420 | // if we got to the end of the list and |
| 2421 | // never found a match, throw 404 |
| 2422 | asyncResp->res.result( |
| 2423 | boost::beast::http::status::not_found); |
| 2424 | return; |
| 2425 | } |
| 2426 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2427 | tinyxml2::XMLElement* methods = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2428 | interface->FirstChildElement("method"); |
| 2429 | while (methods != nullptr) |
| 2430 | { |
| 2431 | nlohmann::json argsArray = nlohmann::json::array(); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2432 | tinyxml2::XMLElement* arg = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2433 | methods->FirstChildElement("arg"); |
| 2434 | while (arg != nullptr) |
| 2435 | { |
| 2436 | nlohmann::json thisArg; |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2437 | for (const char* fieldName : |
| 2438 | std::array<const char*, 3>{ |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2439 | "name", "direction", "type"}) |
| 2440 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2441 | const char* fieldValue = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2442 | arg->Attribute(fieldName); |
| 2443 | if (fieldValue != nullptr) |
| 2444 | { |
| 2445 | thisArg[fieldName] = fieldValue; |
| 2446 | } |
| 2447 | } |
| 2448 | argsArray.push_back(std::move(thisArg)); |
| 2449 | arg = arg->NextSiblingElement("arg"); |
| 2450 | } |
| 2451 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2452 | const char* name = methods->Attribute("name"); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2453 | if (name != nullptr) |
| 2454 | { |
| 2455 | methodsArray.push_back( |
| 2456 | {{"name", name}, |
| 2457 | {"uri", "/bus/system/" + processName + |
| 2458 | objectPath + "/" + |
| 2459 | interfaceName + "/" + name}, |
| 2460 | {"args", argsArray}}); |
| 2461 | } |
| 2462 | methods = methods->NextSiblingElement("method"); |
| 2463 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2464 | tinyxml2::XMLElement* signals = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2465 | interface->FirstChildElement("signal"); |
| 2466 | while (signals != nullptr) |
| 2467 | { |
| 2468 | nlohmann::json argsArray = nlohmann::json::array(); |
| 2469 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2470 | tinyxml2::XMLElement* arg = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2471 | signals->FirstChildElement("arg"); |
| 2472 | while (arg != nullptr) |
| 2473 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2474 | const char* name = arg->Attribute("name"); |
| 2475 | const char* type = arg->Attribute("type"); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2476 | if (name != nullptr && type != nullptr) |
| 2477 | { |
| 2478 | argsArray.push_back({ |
| 2479 | {"name", name}, |
| 2480 | {"type", type}, |
| 2481 | }); |
| 2482 | } |
| 2483 | arg = arg->NextSiblingElement("arg"); |
| 2484 | } |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2485 | const char* name = signals->Attribute("name"); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2486 | if (name != nullptr) |
| 2487 | { |
| 2488 | signalsArray.push_back( |
| 2489 | {{"name", name}, {"args", argsArray}}); |
| 2490 | } |
| 2491 | |
| 2492 | signals = signals->NextSiblingElement("signal"); |
| 2493 | } |
| 2494 | |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2495 | tinyxml2::XMLElement* property = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2496 | interface->FirstChildElement("property"); |
| 2497 | while (property != nullptr) |
| 2498 | { |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2499 | const char* name = property->Attribute("name"); |
| 2500 | const char* type = property->Attribute("type"); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2501 | if (type != nullptr && name != nullptr) |
| 2502 | { |
| 2503 | sdbusplus::message::message m = |
| 2504 | crow::connections::systemBus |
| 2505 | ->new_method_call(processName.c_str(), |
| 2506 | objectPath.c_str(), |
| 2507 | "org.freedesktop." |
| 2508 | "DBus." |
| 2509 | "Properties", |
| 2510 | "Get"); |
| 2511 | m.append(interfaceName, name); |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2512 | nlohmann::json& propertyItem = |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2513 | propertiesObj[name]; |
| 2514 | crow::connections::systemBus->async_send( |
| 2515 | m, [&propertyItem, asyncResp]( |
Gunnar Mills | 1214b7e | 2020-06-04 10:11:30 -0500 | [diff] [blame] | 2516 | boost::system::error_code& e, |
| 2517 | sdbusplus::message::message& msg) { |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2518 | if (e) |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2519 | { |
| 2520 | return; |
| 2521 | } |
| 2522 | |
Ed Tanous | 271584a | 2019-07-09 16:24:22 -0700 | [diff] [blame] | 2523 | convertDBusToJSON("v", msg, |
| 2524 | propertyItem); |
Ed Tanous | 7c09162 | 2019-05-23 11:42:36 -0700 | [diff] [blame] | 2525 | }); |
| 2526 | } |
| 2527 | property = property->NextSiblingElement("property"); |
| 2528 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2529 | }, |
| 2530 | processName, objectPath, |
| 2531 | "org.freedesktop.DBus.Introspectable", "Introspect"); |
| 2532 | } |
Ed Tanous | e3cb5a3 | 2018-08-08 14:16:49 -0700 | [diff] [blame] | 2533 | else |
| 2534 | { |
| 2535 | if (req.method() != "POST"_method) |
| 2536 | { |
| 2537 | res.result(boost::beast::http::status::not_found); |
| 2538 | res.end(); |
| 2539 | return; |
| 2540 | } |
| 2541 | |
| 2542 | nlohmann::json requestDbusData = |
| 2543 | nlohmann::json::parse(req.body, nullptr, false); |
| 2544 | |
| 2545 | if (requestDbusData.is_discarded()) |
| 2546 | { |
| 2547 | res.result(boost::beast::http::status::bad_request); |
| 2548 | res.end(); |
| 2549 | return; |
| 2550 | } |
| 2551 | if (!requestDbusData.is_array()) |
| 2552 | { |
| 2553 | res.result(boost::beast::http::status::bad_request); |
| 2554 | res.end(); |
| 2555 | return; |
| 2556 | } |
| 2557 | auto transaction = std::make_shared<InProgressActionData>(res); |
| 2558 | |
| 2559 | transaction->path = objectPath; |
| 2560 | transaction->methodName = methodName; |
| 2561 | transaction->arguments = std::move(requestDbusData); |
| 2562 | |
| 2563 | findActionOnInterface(transaction, processName); |
| 2564 | } |
Ed Tanous | 1abe55e | 2018-09-05 08:30:59 -0700 | [diff] [blame] | 2565 | }); |
| 2566 | } |
| 2567 | } // namespace openbmc_mapper |
| 2568 | } // namespace crow |