blob: a5024be4fdc8f4144d47a898831ef33cbc8309c4 [file] [log] [blame]
James Feist5b4aa862018-08-16 14:07:01 -07001// 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 Tanousb9b2e0b2018-09-13 13:47:50 -070014
James Feist5b4aa862018-08-16 14:07:01 -070015#pragma once
James Feistf6150402019-01-08 10:36:20 -080016#include "filesystem.hpp"
17
Ed Tanous911ac312017-08-15 09:37:42 -070018#include <crow/app.h>
Ed Tanous911ac312017-08-15 09:37:42 -070019#include <tinyxml2.h>
Ed Tanous1abe55e2018-09-05 08:30:59 -070020
Ed Tanouse3cb5a32018-08-08 14:16:49 -070021#include <async_resp.hpp>
Ed Tanous1abe55e2018-09-05 08:30:59 -070022#include <boost/algorithm/string.hpp>
23#include <boost/container/flat_set.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070024#include <dbus_singleton.hpp>
James Feist5b4aa862018-08-16 14:07:01 -070025#include <dbus_utility.hpp>
Ed Tanousd4bb9bb2018-05-16 13:36:42 -070026#include <fstream>
William A. Kennington III0a63b1c2018-10-18 13:37:19 -070027#include <sdbusplus/message/types.hpp>
Ed Tanous911ac312017-08-15 09:37:42 -070028
Ed Tanous1abe55e2018-09-05 08:30:59 -070029namespace crow
30{
31namespace openbmc_mapper
32{
Ed Tanousba9f9a62017-10-11 16:40:35 -070033
Matt Spinler3ae4ba72018-12-05 14:01:22 -060034using GetSubTreeType = std::vector<
35 std::pair<std::string,
36 std::vector<std::pair<std::string, std::vector<std::string>>>>>;
37
Matt Spinler2ae60092018-12-06 10:35:36 -060038const std::string notFoundMsg = "404 Not Found";
Matt Spinler6db06242018-12-11 11:21:22 -060039const std::string badReqMsg = "400 Bad Request";
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -060040const std::string methodNotAllowedMsg = "405 Method Not Allowed";
Matt Spinlerfbc19ea2018-12-11 14:03:42 -060041const std::string forbiddenMsg = "403 Forbidden";
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -060042const std::string methodFailedMsg = "500 Method Call Failed";
Matt Spinler16caaee2019-01-15 11:40:34 -060043const std::string methodOutputFailedMsg = "500 Method Output Error";
Matt Spinler6db06242018-12-11 11:21:22 -060044
Matt Spinler2ae60092018-12-06 10:35:36 -060045const std::string notFoundDesc =
46 "org.freedesktop.DBus.Error.FileNotFound: path or object not found";
Matt Spinlerdc2f9f12018-12-06 13:53:53 -060047const std::string propNotFoundDesc = "The specified property cannot be found";
Matt Spinler6db06242018-12-11 11:21:22 -060048const std::string noJsonDesc = "No JSON object could be decoded";
49const std::string methodNotFoundDesc = "The specified method cannot be found";
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -060050const std::string methodNotAllowedDesc = "Method not allowed";
Matt Spinlerfbc19ea2018-12-11 14:03:42 -060051const std::string forbiddenPropDesc =
52 "The specified property cannot be created";
53const std::string forbiddenResDesc = "The specified resource cannot be created";
Matt Spinler2ae60092018-12-06 10:35:36 -060054
55void setErrorResponse(crow::Response &res, boost::beast::http::status result,
56 const std::string &desc, const std::string &msg)
57{
58 res.result(result);
59 res.jsonValue = {{"data", {{"description", desc}}},
60 {"message", msg},
61 {"status", "error"}};
62}
63
Ed Tanouse3cb5a32018-08-08 14:16:49 -070064void introspectObjects(const std::string &processName,
65 const std::string &objectPath,
66 std::shared_ptr<bmcweb::AsyncResp> transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -070067{
Ed Tanouse3cb5a32018-08-08 14:16:49 -070068 if (transaction->res.jsonValue.is_null())
69 {
70 transaction->res.jsonValue = {{"status", "ok"},
71 {"bus_name", processName},
72 {"objects", nlohmann::json::array()}};
73 }
74
Ed Tanous1abe55e2018-09-05 08:30:59 -070075 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -070076 [transaction, processName{std::string(processName)},
77 objectPath{std::string(objectPath)}](
78 const boost::system::error_code ec,
79 const std::string &introspect_xml) {
Ed Tanous1abe55e2018-09-05 08:30:59 -070080 if (ec)
81 {
82 BMCWEB_LOG_ERROR
83 << "Introspect call failed with error: " << ec.message()
84 << " on process: " << processName << " path: " << objectPath
85 << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -070086 return;
87 }
88 transaction->res.jsonValue["objects"].push_back(
89 {{"path", objectPath}});
90
91 tinyxml2::XMLDocument doc;
92
93 doc.Parse(introspect_xml.c_str());
94 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
95 if (pRoot == nullptr)
96 {
97 BMCWEB_LOG_ERROR << "XML document failed to parse "
98 << processName << " " << objectPath << "\n";
Ed Tanous911ac312017-08-15 09:37:42 -070099 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700100 else
101 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700102 tinyxml2::XMLElement *node = pRoot->FirstChildElement("node");
103 while (node != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700104 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700105 const char *childPath = node->Attribute("name");
106 if (childPath != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700107 {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700108 std::string newpath;
109 if (objectPath != "/")
110 {
111 newpath += objectPath;
112 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700113 newpath += std::string("/") + childPath;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700114 // introspect the subobjects as well
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700115 introspectObjects(processName, newpath, transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700116 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700117
118 node = node->NextSiblingElement("node");
Ed Tanous1abe55e2018-09-05 08:30:59 -0700119 }
120 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700121 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700122 processName, objectPath, "org.freedesktop.DBus.Introspectable",
Ed Tanous1abe55e2018-09-05 08:30:59 -0700123 "Introspect");
Ed Tanous911ac312017-08-15 09:37:42 -0700124}
Ed Tanous64530012018-02-06 17:08:16 -0800125
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600126void getPropertiesForEnumerate(const std::string &objectPath,
127 const std::string &service,
128 const std::string &interface,
129 std::shared_ptr<bmcweb::AsyncResp> asyncResp)
130{
131 BMCWEB_LOG_DEBUG << "getPropertiesForEnumerate " << objectPath << " "
132 << service << " " << interface;
133
134 crow::connections::systemBus->async_method_call(
135 [asyncResp, objectPath, service,
136 interface](const boost::system::error_code ec,
137 const std::vector<
138 std::pair<std::string, dbus::utility::DbusVariantType>>
139 &propertiesList) {
140 if (ec)
141 {
142 BMCWEB_LOG_ERROR << "GetAll on path " << objectPath << " iface "
143 << interface << " service " << service
144 << " failed with code " << ec;
145 return;
146 }
147
148 nlohmann::json &dataJson = asyncResp->res.jsonValue["data"];
149 nlohmann::json &objectJson = dataJson[objectPath];
150 if (objectJson.is_null())
151 {
152 objectJson = nlohmann::json::object();
153 }
154
155 for (const auto &[name, value] : propertiesList)
156 {
157 nlohmann::json &propertyJson = objectJson[name];
158 sdbusplus::message::variant_ns::visit(
159 [&propertyJson](auto &&val) { propertyJson = val; }, value);
160 }
161 },
162 service, objectPath, "org.freedesktop.DBus.Properties", "GetAll",
163 interface);
164}
165
166// Find any results that weren't picked up by ObjectManagers, to be
167// called after all ObjectManagers are searched for and called.
168void findRemainingObjectsForEnumerate(
169 const std::string &objectPath, std::shared_ptr<GetSubTreeType> subtree,
170 std::shared_ptr<bmcweb::AsyncResp> asyncResp)
171{
172 BMCWEB_LOG_DEBUG << "findRemainingObjectsForEnumerate";
173 const nlohmann::json &dataJson = asyncResp->res.jsonValue["data"];
174
175 for (const auto &[path, interface_map] : *subtree)
176 {
177 if (path == objectPath)
178 {
179 // An enumerate does not return the target path's properties
180 continue;
181 }
182 if (dataJson.find(path) == dataJson.end())
183 {
184 for (const auto &[service, interfaces] : interface_map)
185 {
186 for (const auto &interface : interfaces)
187 {
188 if (!boost::starts_with(interface, "org.freedesktop.DBus"))
189 {
190 getPropertiesForEnumerate(path, service, interface,
191 asyncResp);
192 }
193 }
194 }
195 }
196 }
197}
198
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600199struct InProgressEnumerateData
200{
201 InProgressEnumerateData(const std::string &objectPath,
202 std::shared_ptr<bmcweb::AsyncResp> asyncResp) :
203 objectPath(objectPath),
204 asyncResp(asyncResp)
205 {
206 }
207
208 ~InProgressEnumerateData()
209 {
Matt Spinler2df1e7d2018-12-05 15:53:16 -0600210 findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp);
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600211 }
212
213 const std::string objectPath;
214 std::shared_ptr<GetSubTreeType> subtree;
215 std::shared_ptr<bmcweb::AsyncResp> asyncResp;
216};
217
218void getManagedObjectsForEnumerate(
219 const std::string &object_name, const std::string &object_manager_path,
220 const std::string &connection_name,
221 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700222{
Ed Tanous049a0512018-11-01 13:58:42 -0700223 BMCWEB_LOG_DEBUG << "getManagedObjectsForEnumerate " << object_name
224 << " object_manager_path " << object_manager_path
225 << " connection_name " << connection_name;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700226 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600227 [transaction, object_name,
Ed Tanous049a0512018-11-01 13:58:42 -0700228 connection_name](const boost::system::error_code ec,
229 const dbus::utility::ManagedObjectType &objects) {
Ed Tanous1abe55e2018-09-05 08:30:59 -0700230 if (ec)
231 {
Ed Tanous049a0512018-11-01 13:58:42 -0700232 BMCWEB_LOG_ERROR << "GetManagedObjects on path " << object_name
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600233 << " on connection " << connection_name
Ed Tanous049a0512018-11-01 13:58:42 -0700234 << " failed with code " << ec;
235 return;
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700236 }
Ed Tanous64530012018-02-06 17:08:16 -0800237
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600238 nlohmann::json &dataJson =
239 transaction->asyncResp->res.jsonValue["data"];
Ed Tanous049a0512018-11-01 13:58:42 -0700240
241 for (const auto &objectPath : objects)
242 {
243 if (boost::starts_with(objectPath.first.str, object_name))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700244 {
Ed Tanous049a0512018-11-01 13:58:42 -0700245 BMCWEB_LOG_DEBUG << "Reading object "
246 << objectPath.first.str;
247 nlohmann::json &objectJson = dataJson[objectPath.first.str];
Ed Tanous1abe55e2018-09-05 08:30:59 -0700248 if (objectJson.is_null())
249 {
250 objectJson = nlohmann::json::object();
251 }
252 for (const auto &interface : objectPath.second)
253 {
254 for (const auto &property : interface.second)
255 {
256 nlohmann::json &propertyJson =
257 objectJson[property.first];
William A. Kennington III0a63b1c2018-10-18 13:37:19 -0700258 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -0700259 [&propertyJson](auto &&val) {
260 propertyJson = val;
261 },
262 property.second);
263 }
264 }
265 }
Ed Tanous049a0512018-11-01 13:58:42 -0700266 for (const auto &interface : objectPath.second)
267 {
268 if (interface.first == "org.freedesktop.DBus.ObjectManager")
269 {
270 getManagedObjectsForEnumerate(
271 objectPath.first.str, objectPath.first.str,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600272 connection_name, transaction);
Ed Tanous049a0512018-11-01 13:58:42 -0700273 }
274 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700275 }
276 },
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700277 connection_name, object_manager_path,
278 "org.freedesktop.DBus.ObjectManager", "GetManagedObjects");
279}
280
281void findObjectManagerPathForEnumerate(
282 const std::string &object_name, const std::string &connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600283 std::shared_ptr<InProgressEnumerateData> transaction)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700284{
Ed Tanous049a0512018-11-01 13:58:42 -0700285 BMCWEB_LOG_DEBUG << "Finding objectmanager for path " << object_name
286 << " on connection:" << connection_name;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700287 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600288 [transaction, object_name, connection_name](
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700289 const boost::system::error_code ec,
290 const boost::container::flat_map<
291 std::string, boost::container::flat_map<
292 std::string, std::vector<std::string>>>
293 &objects) {
294 if (ec)
295 {
Ed Tanous049a0512018-11-01 13:58:42 -0700296 BMCWEB_LOG_ERROR << "GetAncestors on path " << object_name
297 << " failed with code " << ec;
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700298 return;
299 }
300
Ed Tanousf254ba72018-10-12 13:40:35 -0700301 for (const auto &pathGroup : objects)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700302 {
Ed Tanousf254ba72018-10-12 13:40:35 -0700303 for (const auto &connectionGroup : pathGroup.second)
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700304 {
305 if (connectionGroup.first == connection_name)
306 {
307 // Found the object manager path for this resource.
308 getManagedObjectsForEnumerate(
Ed Tanous049a0512018-11-01 13:58:42 -0700309 object_name, pathGroup.first, connection_name,
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600310 transaction);
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700311 return;
312 }
313 }
314 }
315 },
316 "xyz.openbmc_project.ObjectMapper",
317 "/xyz/openbmc_project/object_mapper",
318 "xyz.openbmc_project.ObjectMapper", "GetAncestors", object_name,
319 std::array<const char *, 1>{"org.freedesktop.DBus.ObjectManager"});
Ed Tanousaa2e59c2018-04-12 12:17:20 -0700320}
Ed Tanous64530012018-02-06 17:08:16 -0800321
Matt Spinler3ae4ba72018-12-05 14:01:22 -0600322// Uses GetObject to add the object info about the target /enumerate path to the
323// results of GetSubTree, as GetSubTree will not return info for the
324// target path, and then continues on enumerating the rest of the tree.
325void getObjectAndEnumerate(std::shared_ptr<InProgressEnumerateData> transaction)
326{
327 using GetObjectType =
328 std::vector<std::pair<std::string, std::vector<std::string>>>;
329
330 crow::connections::systemBus->async_method_call(
331 [transaction](const boost::system::error_code ec,
332 const GetObjectType &objects) {
333 if (ec)
334 {
335 BMCWEB_LOG_ERROR << "GetObject for path "
336 << transaction->objectPath
337 << " failed with code " << ec;
338 return;
339 }
340
341 BMCWEB_LOG_DEBUG << "GetObject for " << transaction->objectPath
342 << " has " << objects.size() << " entries";
343 if (!objects.empty())
344 {
345 transaction->subtree->emplace_back(transaction->objectPath,
346 objects);
347 }
348
349 // Map indicating connection name, and the path where the object
350 // manager exists
351 boost::container::flat_map<std::string, std::string> connections;
352
353 for (const auto &object : *(transaction->subtree))
354 {
355 for (const auto &connection : object.second)
356 {
357 std::string &objectManagerPath =
358 connections[connection.first];
359 for (const auto &interface : connection.second)
360 {
361 BMCWEB_LOG_DEBUG << connection.first
362 << " has interface " << interface;
363 if (interface == "org.freedesktop.DBus.ObjectManager")
364 {
365 BMCWEB_LOG_DEBUG << "found object manager path "
366 << object.first;
367 objectManagerPath = object.first;
368 }
369 }
370 }
371 }
372 BMCWEB_LOG_DEBUG << "Got " << connections.size() << " connections";
373
374 for (const auto &connection : connections)
375 {
376 // If we already know where the object manager is, we don't need
377 // to search for it, we can call directly in to
378 // getManagedObjects
379 if (!connection.second.empty())
380 {
381 getManagedObjectsForEnumerate(
382 transaction->objectPath, connection.second,
383 connection.first, transaction);
384 }
385 else
386 {
387 // otherwise we need to find the object manager path before
388 // we can continue
389 findObjectManagerPathForEnumerate(
390 transaction->objectPath, connection.first, transaction);
391 }
392 }
393 },
394 "xyz.openbmc_project.ObjectMapper",
395 "/xyz/openbmc_project/object_mapper",
396 "xyz.openbmc_project.ObjectMapper", "GetObject",
397 transaction->objectPath, std::array<const char *, 0>());
398}
Ed Tanous64530012018-02-06 17:08:16 -0800399
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700400// Structure for storing data on an in progress action
Ed Tanous1abe55e2018-09-05 08:30:59 -0700401struct InProgressActionData
402{
403 InProgressActionData(crow::Response &res) : res(res){};
404 ~InProgressActionData()
405 {
Matt Spinler16caaee2019-01-15 11:40:34 -0600406 // Methods could have been called across different owners
407 // and interfaces, where some calls failed and some passed.
408 //
409 // The rules for this are:
410 // * if no method was called - error
411 // * if a method failed and none passed - error
412 // (converse: if at least one method passed - OK)
413 // * for the method output:
414 // * if output processing didn't fail, return the data
415
416 // Only deal with method returns if nothing failed earlier
417 if (res.result() == boost::beast::http::status::ok)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700418 {
Matt Spinler16caaee2019-01-15 11:40:34 -0600419 if (!methodPassed)
420 {
421 if (methodFailed)
422 {
423 setErrorResponse(res,
424 boost::beast::http::status::bad_request,
425 "Method call failed", methodFailedMsg);
426 }
427 else
428 {
429 setErrorResponse(res, boost::beast::http::status::not_found,
430 methodNotFoundDesc, notFoundMsg);
431 }
432 }
433 else
434 {
435 if (outputFailed)
436 {
437 setErrorResponse(
438 res, boost::beast::http::status::internal_server_error,
439 "Method output failure", methodOutputFailedMsg);
440 }
441 else
442 {
443 res.jsonValue = {{"status", "ok"},
444 {"message", "200 OK"},
445 {"data", methodResponse}};
446 }
447 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700448 }
Matt Spinler16caaee2019-01-15 11:40:34 -0600449
Ed Tanous1abe55e2018-09-05 08:30:59 -0700450 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700451 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700452
Matt Spinler6db06242018-12-11 11:21:22 -0600453 void setErrorStatus(const std::string &desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700454 {
Matt Spinlerc0eb9bd2019-01-09 15:22:30 -0600455 setErrorResponse(res, boost::beast::http::status::bad_request, desc,
456 badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700457 }
458 crow::Response &res;
459 std::string path;
460 std::string methodName;
Matt Spinlerde818812018-12-11 16:39:20 -0600461 std::string interfaceName;
Matt Spinler16caaee2019-01-15 11:40:34 -0600462 bool methodPassed = false;
463 bool methodFailed = false;
464 bool outputFailed = false;
465 nlohmann::json methodResponse;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700466 nlohmann::json arguments;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700467};
468
Ed Tanous1abe55e2018-09-05 08:30:59 -0700469std::vector<std::string> dbusArgSplit(const std::string &string)
470{
471 std::vector<std::string> ret;
472 if (string.empty())
473 {
474 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700475 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700476 ret.push_back("");
477 int containerDepth = 0;
478
479 for (std::string::const_iterator character = string.begin();
480 character != string.end(); character++)
481 {
482 ret.back() += *character;
483 switch (*character)
484 {
485 case ('a'):
486 break;
487 case ('('):
488 case ('{'):
489 containerDepth++;
490 break;
491 case ('}'):
492 case (')'):
493 containerDepth--;
494 if (containerDepth == 0)
495 {
496 if (character + 1 != string.end())
497 {
498 ret.push_back("");
499 }
500 }
501 break;
502 default:
503 if (containerDepth == 0)
504 {
505 if (character + 1 != string.end())
506 {
507 ret.push_back("");
508 }
509 }
510 break;
511 }
512 }
Matt Spinler4ae611d2019-01-11 15:37:06 -0600513
514 return ret;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700515}
516
Ed Tanousd76323e2018-08-07 14:35:40 -0700517int convertJsonToDbus(sd_bus_message *m, const std::string &arg_type,
Ed Tanous1abe55e2018-09-05 08:30:59 -0700518 const nlohmann::json &input_json)
519{
520 int r = 0;
521 BMCWEB_LOG_DEBUG << "Converting " << input_json.dump()
522 << " to type: " << arg_type;
523 const std::vector<std::string> argTypes = dbusArgSplit(arg_type);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700524
Ed Tanous1abe55e2018-09-05 08:30:59 -0700525 // Assume a single object for now.
526 const nlohmann::json *j = &input_json;
527 nlohmann::json::const_iterator jIt = input_json.begin();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700528
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700529 for (const std::string &argCode : argTypes)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700530 {
531 // If we are decoding multiple objects, grab the pointer to the
532 // iterator, and increment it for the next loop
533 if (argTypes.size() > 1)
534 {
535 if (jIt == input_json.end())
536 {
537 return -2;
538 }
539 j = &*jIt;
540 jIt++;
541 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700542 const int64_t *intValue = j->get_ptr<const int64_t *>();
543 const uint64_t *uintValue = j->get_ptr<const uint64_t *>();
544 const std::string *stringValue = j->get_ptr<const std::string *>();
545 const double *doubleValue = j->get_ptr<const double *>();
Ed Tanous1abe55e2018-09-05 08:30:59 -0700546 const bool *b = j->get_ptr<const bool *>();
547 int64_t v = 0;
548 double d = 0.0;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700549
Ed Tanous1abe55e2018-09-05 08:30:59 -0700550 // Do some basic type conversions that make sense. uint can be
551 // converted to int. int and uint can be converted to double
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700552 if (uintValue != nullptr && intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700553 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700554 v = static_cast<int64_t>(*uintValue);
555 intValue = &v;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700556 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700557 if (uintValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700558 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700559 d = static_cast<double>(*uintValue);
560 doubleValue = &d;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700561 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700562 if (intValue != nullptr && doubleValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700563 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700564 d = static_cast<double>(*intValue);
565 doubleValue = &d;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700566 }
567
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700568 if (argCode == "s")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700569 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700570 if (stringValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700571 {
572 return -1;
573 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700574 r = sd_bus_message_append_basic(m, argCode[0],
575 (void *)stringValue->c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700576 if (r < 0)
577 {
578 return r;
579 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700580 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700581 else if (argCode == "i")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700582 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700583 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700584 {
585 return -1;
586 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700587 int32_t i = static_cast<int32_t>(*intValue);
588 r = sd_bus_message_append_basic(m, argCode[0], &i);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700589 if (r < 0)
590 {
591 return r;
592 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700593 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700594 else if (argCode == "b")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700595 {
596 // lots of ways bool could be represented here. Try them all
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700597 int boolInt = false;
598 if (intValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700599 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700600 boolInt = *intValue > 0 ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700601 }
602 else if (b != nullptr)
603 {
Matt Spinlera2f02632019-01-18 10:15:35 -0600604 boolInt = *b ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700605 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700606 else if (stringValue != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700607 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700608 boolInt = boost::istarts_with(*stringValue, "t") ? 1 : 0;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700609 }
610 else
611 {
612 return -1;
613 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700614 r = sd_bus_message_append_basic(m, argCode[0], &boolInt);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700615 if (r < 0)
616 {
617 return r;
618 }
619 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700620 else if (argCode == "n")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700621 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700622 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700623 {
624 return -1;
625 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700626 int16_t n = static_cast<int16_t>(*intValue);
627 r = sd_bus_message_append_basic(m, argCode[0], &n);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700628 if (r < 0)
629 {
630 return r;
631 }
632 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700633 else if (argCode == "x")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700634 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700635 if (intValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700636 {
637 return -1;
638 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700639 r = sd_bus_message_append_basic(m, argCode[0], intValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700640 if (r < 0)
641 {
642 return r;
643 }
644 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700645 else if (argCode == "y")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700646 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700647 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700648 {
649 return -1;
650 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700651 uint8_t y = static_cast<uint8_t>(*uintValue);
652 r = sd_bus_message_append_basic(m, argCode[0], &y);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700653 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700654 else if (argCode == "q")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700655 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700656 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700657 {
658 return -1;
659 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700660 uint16_t q = static_cast<uint16_t>(*uintValue);
661 r = sd_bus_message_append_basic(m, argCode[0], &q);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700662 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700663 else if (argCode == "u")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700664 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700665 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700666 {
667 return -1;
668 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700669 uint32_t u = static_cast<uint32_t>(*uintValue);
670 r = sd_bus_message_append_basic(m, argCode[0], &u);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700671 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700672 else if (argCode == "t")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700673 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700674 if (uintValue == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -0700675 {
676 return -1;
677 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700678 r = sd_bus_message_append_basic(m, argCode[0], uintValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700679 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700680 else if (argCode == "d")
Ed Tanous1abe55e2018-09-05 08:30:59 -0700681 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700682 sd_bus_message_append_basic(m, argCode[0], doubleValue);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700683 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700684 else if (boost::starts_with(argCode, "a"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700685 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700686 std::string containedType = argCode.substr(1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700687 r = sd_bus_message_open_container(m, SD_BUS_TYPE_ARRAY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700688 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700689 if (r < 0)
690 {
691 return r;
692 }
693
694 for (nlohmann::json::const_iterator it = j->begin(); it != j->end();
695 ++it)
696 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700697 r = convertJsonToDbus(m, containedType, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700698 if (r < 0)
699 {
700 return r;
701 }
Ed Tanous1abe55e2018-09-05 08:30:59 -0700702 }
703 sd_bus_message_close_container(m);
704 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700705 else if (boost::starts_with(argCode, "v"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700706 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700707 std::string containedType = argCode.substr(1);
708 BMCWEB_LOG_DEBUG << "variant type: " << argCode
709 << " appending variant of type: " << containedType;
Ed Tanous1abe55e2018-09-05 08:30:59 -0700710 r = sd_bus_message_open_container(m, SD_BUS_TYPE_VARIANT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700711 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700712 if (r < 0)
713 {
714 return r;
715 }
716
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700717 r = convertJsonToDbus(m, containedType, input_json);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700718 if (r < 0)
719 {
720 return r;
721 }
722
723 r = sd_bus_message_close_container(m);
724 if (r < 0)
725 {
726 return r;
727 }
728 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700729 else if (boost::starts_with(argCode, "(") &&
730 boost::ends_with(argCode, ")"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700731 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700732 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700733 r = sd_bus_message_open_container(m, SD_BUS_TYPE_STRUCT,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700734 containedType.c_str());
Ed Tanous1abe55e2018-09-05 08:30:59 -0700735 nlohmann::json::const_iterator it = j->begin();
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700736 for (const std::string &argCode : dbusArgSplit(arg_type))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700737 {
738 if (it == j->end())
739 {
740 return -1;
741 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700742 r = convertJsonToDbus(m, argCode, *it);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700743 if (r < 0)
744 {
745 return r;
746 }
747 it++;
748 }
749 r = sd_bus_message_close_container(m);
750 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700751 else if (boost::starts_with(argCode, "{") &&
752 boost::ends_with(argCode, "}"))
Ed Tanous1abe55e2018-09-05 08:30:59 -0700753 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700754 std::string containedType = argCode.substr(1, argCode.size() - 1);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700755 r = sd_bus_message_open_container(m, SD_BUS_TYPE_DICT_ENTRY,
Ed Tanouse3cb5a32018-08-08 14:16:49 -0700756 containedType.c_str());
757 std::vector<std::string> codes = dbusArgSplit(containedType);
Ed Tanous1abe55e2018-09-05 08:30:59 -0700758 if (codes.size() != 2)
759 {
760 return -1;
761 }
762 const std::string &key_type = codes[0];
763 const std::string &value_type = codes[1];
764 for (auto it : j->items())
765 {
766 r = convertJsonToDbus(m, key_type, it.key());
767 if (r < 0)
768 {
769 return r;
770 }
771
772 r = convertJsonToDbus(m, value_type, it.value());
773 if (r < 0)
774 {
775 return r;
776 }
777 }
778 r = sd_bus_message_close_container(m);
779 }
780 else
781 {
782 return -2;
783 }
784 if (r < 0)
785 {
786 return r;
Ed Tanous75db20e2018-07-27 13:44:44 -0700787 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700788
Ed Tanous1abe55e2018-09-05 08:30:59 -0700789 if (argTypes.size() > 1)
790 {
791 jIt++;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700792 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700793 }
Matt Spinler127ea542019-01-14 11:04:28 -0600794
795 return r;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -0700796}
797
Matt Spinlerd22a7132019-01-14 12:14:30 -0600798template <typename T>
799int readMessageItem(const std::string &typeCode, sdbusplus::message::message &m,
800 nlohmann::json &data)
801{
802 T value;
803
804 int r = sd_bus_message_read_basic(m.get(), typeCode.front(), &value);
805 if (r < 0)
806 {
807 BMCWEB_LOG_ERROR << "sd_bus_message_read_basic on type " << typeCode
808 << " failed!";
809 return r;
810 }
811
812 data = value;
813 return 0;
814}
815
Matt Spinler16caaee2019-01-15 11:40:34 -0600816int convertDBusToJSON(const std::string &returnType,
Matt Spinler6df8f992019-01-14 12:47:47 -0600817 sdbusplus::message::message &m, nlohmann::json &response);
818
819int readDictEntryFromMessage(const std::string &typeCode,
820 sdbusplus::message::message &m,
821 nlohmann::json &object)
822{
823 std::vector<std::string> types = dbusArgSplit(typeCode);
824 if (types.size() != 2)
825 {
826 BMCWEB_LOG_ERROR << "wrong number contained types in dictionary: "
827 << types.size();
828 return -1;
829 }
830
831 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_DICT_ENTRY,
832 typeCode.c_str());
833 if (r < 0)
834 {
835 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container with rc " << r;
836 return r;
837 }
838
839 nlohmann::json key;
840 r = convertDBusToJSON(types[0], m, key);
841 if (r < 0)
842 {
843 return r;
844 }
845
846 const std::string *keyPtr = key.get_ptr<const std::string *>();
847 if (keyPtr == nullptr)
848 {
849 // json doesn't support non-string keys. If we hit this condition,
850 // convert the result to a string so we can proceed
851 key = key.dump();
852 keyPtr = key.get_ptr<const std::string *>();
853 // in theory this can't fail now, but lets be paranoid about it anyway
854 if (keyPtr == nullptr)
855 {
856 return -1;
857 }
858 }
859 nlohmann::json &value = object[*keyPtr];
860
861 r = convertDBusToJSON(types[1], m, value);
862 if (r < 0)
863 {
864 return r;
865 }
866
867 r = sd_bus_message_exit_container(m.get());
868 if (r < 0)
869 {
870 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
871 return r;
872 }
873
874 return 0;
875}
876
877int readArrayFromMessage(const std::string &typeCode,
878 sdbusplus::message::message &m, nlohmann::json &data)
879{
880 if (typeCode.size() < 2)
881 {
882 BMCWEB_LOG_ERROR << "Type code " << typeCode
883 << " too small for an array";
884 return -1;
885 }
886
887 std::string containedType = typeCode.substr(1);
888
889 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_ARRAY,
890 containedType.c_str());
891 if (r < 0)
892 {
893 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
894 << r;
895 return r;
896 }
897
898 bool dict = boost::starts_with(containedType, "{") &&
899 boost::ends_with(containedType, "}");
900
901 if (dict)
902 {
903 // Remove the { }
904 containedType = containedType.substr(1, containedType.size() - 2);
905 data = nlohmann::json::object();
906 }
907 else
908 {
909 data = nlohmann::json::array();
910 }
911
912 while (true)
913 {
914 r = sd_bus_message_at_end(m.get(), false);
915 if (r < 0)
916 {
917 BMCWEB_LOG_ERROR << "sd_bus_message_at_end failed";
918 return r;
919 }
920
921 if (r > 0)
922 {
923 break;
924 }
925
926 // Dictionaries are only ever seen in an array
927 if (dict)
928 {
929 r = readDictEntryFromMessage(containedType, m, data);
930 if (r < 0)
931 {
932 return r;
933 }
934 }
935 else
936 {
937 data.push_back(nlohmann::json());
938
939 r = convertDBusToJSON(containedType, m, data.back());
940 if (r < 0)
941 {
942 return r;
943 }
944 }
945 }
946
947 r = sd_bus_message_exit_container(m.get());
948 if (r < 0)
949 {
950 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
951 return r;
952 }
953
954 return 0;
955}
956
Matt Spinler75c6c672019-01-14 13:01:46 -0600957int readStructFromMessage(const std::string &typeCode,
958 sdbusplus::message::message &m, nlohmann::json &data)
959{
960 if (typeCode.size() < 3)
961 {
962 BMCWEB_LOG_ERROR << "Type code " << typeCode
963 << " too small for a struct";
964 return -1;
965 }
966
967 std::string containedTypes = typeCode.substr(1, typeCode.size() - 2);
968 std::vector<std::string> types = dbusArgSplit(containedTypes);
969
970 int r = sd_bus_message_enter_container(m.get(), SD_BUS_TYPE_STRUCT,
971 containedTypes.c_str());
972 if (r < 0)
973 {
974 BMCWEB_LOG_ERROR << "sd_bus_message_enter_container failed with rc "
975 << r;
976 return r;
977 }
978
979 for (const std::string &type : types)
980 {
981 data.push_back(nlohmann::json());
982 r = convertDBusToJSON(type, m, data.back());
983 if (r < 0)
984 {
985 return r;
986 }
987 }
988
989 r = sd_bus_message_exit_container(m.get());
990 if (r < 0)
991 {
992 BMCWEB_LOG_ERROR << "sd_bus_message_exit_container failed";
993 return r;
994 }
995 return 0;
996}
997
Matt Spinler6df8f992019-01-14 12:47:47 -0600998int convertDBusToJSON(const std::string &returnType,
Matt Spinler16caaee2019-01-15 11:40:34 -0600999 sdbusplus::message::message &m, nlohmann::json &response)
1000{
Matt Spinlerd22a7132019-01-14 12:14:30 -06001001 int r = 0;
1002 const std::vector<std::string> returnTypes = dbusArgSplit(returnType);
1003
1004 nlohmann::json &thisElement = response;
1005 for (const std::string &typeCode : returnTypes)
1006 {
1007 if (returnType.size() > 1)
1008 {
1009 response.push_back(nlohmann::json{});
1010 thisElement = response.back();
1011 }
1012
1013 if (typeCode == "s")
1014 {
1015 r = readMessageItem<char *>(typeCode, m, thisElement);
1016 if (r < 0)
1017 {
1018 return r;
1019 }
1020 }
1021 else if (typeCode == "g")
1022 {
1023 r = readMessageItem<char *>(typeCode, m, thisElement);
1024 if (r < 0)
1025 {
1026 return r;
1027 }
1028 }
1029 else if (typeCode == "o")
1030 {
1031 r = readMessageItem<char *>(typeCode, m, thisElement);
1032 if (r < 0)
1033 {
1034 return r;
1035 }
1036 }
1037 else if (typeCode == "b")
1038 {
1039 r = readMessageItem<int>(typeCode, m, thisElement);
1040 if (r < 0)
1041 {
1042 return r;
1043 }
1044
1045 thisElement = static_cast<bool>(thisElement.get<int>());
1046 }
1047 else if (typeCode == "u")
1048 {
1049 r = readMessageItem<uint32_t>(typeCode, m, thisElement);
1050 if (r < 0)
1051 {
1052 return r;
1053 }
1054 }
1055 else if (typeCode == "i")
1056 {
1057 r = readMessageItem<int32_t>(typeCode, m, thisElement);
1058 if (r < 0)
1059 {
1060 return r;
1061 }
1062 }
1063 else if (typeCode == "x")
1064 {
1065 r = readMessageItem<int64_t>(typeCode, m, thisElement);
1066 if (r < 0)
1067 {
1068 return r;
1069 }
1070 }
1071 else if (typeCode == "t")
1072 {
1073 r = readMessageItem<uint64_t>(typeCode, m, thisElement);
1074 if (r < 0)
1075 {
1076 return r;
1077 }
1078 }
1079 else if (typeCode == "n")
1080 {
1081 r = readMessageItem<int16_t>(typeCode, m, thisElement);
1082 if (r < 0)
1083 {
1084 return r;
1085 }
1086 }
1087 else if (typeCode == "q")
1088 {
1089 r = readMessageItem<uint16_t>(typeCode, m, thisElement);
1090 if (r < 0)
1091 {
1092 return r;
1093 }
1094 }
1095 else if (typeCode == "y")
1096 {
1097 r = readMessageItem<uint8_t>(typeCode, m, thisElement);
1098 if (r < 0)
1099 {
1100 return r;
1101 }
1102 }
1103 else if (typeCode == "d")
1104 {
1105 r = readMessageItem<double>(typeCode, m, thisElement);
1106 if (r < 0)
1107 {
1108 return r;
1109 }
1110 }
1111 else if (typeCode == "h")
1112 {
1113 r = readMessageItem<int>(typeCode, m, thisElement);
1114 if (r < 0)
1115 {
1116 return r;
1117 }
1118 }
Matt Spinler6df8f992019-01-14 12:47:47 -06001119 else if (boost::starts_with(typeCode, "a"))
1120 {
1121 r = readArrayFromMessage(typeCode, m, thisElement);
1122 if (r < 0)
1123 {
1124 return r;
1125 }
1126 }
Matt Spinler75c6c672019-01-14 13:01:46 -06001127 else if (boost::starts_with(typeCode, "(") &&
1128 boost::ends_with(typeCode, ")"))
1129 {
1130 r = readStructFromMessage(typeCode, m, thisElement);
1131 if (r < 0)
1132 {
1133 return r;
1134 }
1135 }
Matt Spinlerd22a7132019-01-14 12:14:30 -06001136 else
1137 {
Matt Spinler75c6c672019-01-14 13:01:46 -06001138 // TODO: add variant support
Matt Spinlerd22a7132019-01-14 12:14:30 -06001139 BMCWEB_LOG_ERROR << "Invalid D-Bus signature type " << typeCode;
1140 return -2;
1141 }
1142 }
1143
Matt Spinler16caaee2019-01-15 11:40:34 -06001144 return 0;
1145}
1146
1147void handleMethodResponse(std::shared_ptr<InProgressActionData> transaction,
1148 sdbusplus::message::message &m,
1149 const std::string &returnType)
1150{
1151}
1152
Ed Tanousd76323e2018-08-07 14:35:40 -07001153void findActionOnInterface(std::shared_ptr<InProgressActionData> transaction,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001154 const std::string &connectionName)
1155{
1156 BMCWEB_LOG_DEBUG << "findActionOnInterface for connection "
1157 << connectionName;
1158 crow::connections::systemBus->async_method_call(
1159 [transaction, connectionName{std::string(connectionName)}](
1160 const boost::system::error_code ec,
1161 const std::string &introspect_xml) {
1162 BMCWEB_LOG_DEBUG << "got xml:\n " << introspect_xml;
1163 if (ec)
1164 {
1165 BMCWEB_LOG_ERROR
1166 << "Introspect call failed with error: " << ec.message()
1167 << " on process: " << connectionName << "\n";
Matt Spinler318bd892019-01-15 09:59:20 -06001168 return;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001169 }
Matt Spinler318bd892019-01-15 09:59:20 -06001170 tinyxml2::XMLDocument doc;
1171
1172 doc.Parse(introspect_xml.data(), introspect_xml.size());
1173 tinyxml2::XMLNode *pRoot = doc.FirstChildElement("node");
1174 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001175 {
Matt Spinler318bd892019-01-15 09:59:20 -06001176 BMCWEB_LOG_ERROR << "XML document failed to parse "
1177 << connectionName << "\n";
1178 return;
1179 }
1180 tinyxml2::XMLElement *interfaceNode =
1181 pRoot->FirstChildElement("interface");
1182 while (interfaceNode != nullptr)
1183 {
1184 const char *thisInterfaceName =
1185 interfaceNode->Attribute("name");
1186 if (thisInterfaceName != nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001187 {
Matt Spinler318bd892019-01-15 09:59:20 -06001188 if (!transaction->interfaceName.empty() &&
1189 (transaction->interfaceName != thisInterfaceName))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001190 {
Matt Spinler318bd892019-01-15 09:59:20 -06001191 interfaceNode =
1192 interfaceNode->NextSiblingElement("interface");
1193 continue;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001194 }
Matt Spinler318bd892019-01-15 09:59:20 -06001195
1196 tinyxml2::XMLElement *methodNode =
1197 interfaceNode->FirstChildElement("method");
1198 while (methodNode != nullptr)
1199 {
1200 const char *thisMethodName =
1201 methodNode->Attribute("name");
1202 BMCWEB_LOG_DEBUG << "Found method: " << thisMethodName;
1203 if (thisMethodName != nullptr &&
1204 thisMethodName == transaction->methodName)
1205 {
1206 BMCWEB_LOG_DEBUG
1207 << "Found method named " << thisMethodName
1208 << " on interface " << thisInterfaceName;
1209 sdbusplus::message::message m =
1210 crow::connections::systemBus->new_method_call(
1211 connectionName.c_str(),
1212 transaction->path.c_str(),
1213 thisInterfaceName,
1214 transaction->methodName.c_str());
1215
1216 tinyxml2::XMLElement *argumentNode =
1217 methodNode->FirstChildElement("arg");
1218
Matt Spinler16caaee2019-01-15 11:40:34 -06001219 std::string returnType;
1220
1221 // Find the output type
1222 while (argumentNode != nullptr)
1223 {
1224 const char *argDirection =
1225 argumentNode->Attribute("direction");
1226 const char *argType =
1227 argumentNode->Attribute("type");
1228 if (argDirection != nullptr &&
1229 argType != nullptr &&
1230 std::string(argDirection) == "out")
1231 {
1232 returnType = argType;
1233 break;
1234 }
1235 argumentNode =
1236 argumentNode->NextSiblingElement("arg");
1237 }
1238
Matt Spinler318bd892019-01-15 09:59:20 -06001239 nlohmann::json::const_iterator argIt =
1240 transaction->arguments.begin();
1241
Matt Spinler16caaee2019-01-15 11:40:34 -06001242 argumentNode = methodNode->FirstChildElement("arg");
1243
Matt Spinler318bd892019-01-15 09:59:20 -06001244 while (argumentNode != nullptr)
1245 {
1246 const char *argDirection =
1247 argumentNode->Attribute("direction");
1248 const char *argType =
1249 argumentNode->Attribute("type");
1250 if (argDirection != nullptr &&
1251 argType != nullptr &&
1252 std::string(argDirection) == "in")
1253 {
1254 if (argIt == transaction->arguments.end())
1255 {
1256 transaction->setErrorStatus(
1257 "Invalid method args");
1258 return;
1259 }
1260 if (convertJsonToDbus(m.get(),
1261 std::string(argType),
1262 *argIt) < 0)
1263 {
1264 transaction->setErrorStatus(
1265 "Invalid method arg type");
1266 return;
1267 }
1268
1269 argIt++;
1270 }
1271 argumentNode =
1272 argumentNode->NextSiblingElement("arg");
1273 }
1274
1275 crow::connections::systemBus->async_send(
Matt Spinler16caaee2019-01-15 11:40:34 -06001276 m, [transaction, returnType](
1277 boost::system::error_code ec,
1278 sdbusplus::message::message &m) {
Matt Spinler318bd892019-01-15 09:59:20 -06001279 if (ec)
1280 {
Matt Spinler16caaee2019-01-15 11:40:34 -06001281 transaction->methodFailed = true;
Matt Spinler318bd892019-01-15 09:59:20 -06001282 return;
1283 }
Matt Spinler16caaee2019-01-15 11:40:34 -06001284 else
1285 {
1286 transaction->methodPassed = true;
1287 }
1288
1289 handleMethodResponse(transaction, m,
1290 returnType);
Matt Spinler318bd892019-01-15 09:59:20 -06001291 });
1292 break;
1293 }
1294 methodNode = methodNode->NextSiblingElement("method");
1295 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001296 }
Matt Spinler318bd892019-01-15 09:59:20 -06001297 interfaceNode = interfaceNode->NextSiblingElement("interface");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001298 }
1299 },
1300 connectionName, transaction->path,
1301 "org.freedesktop.DBus.Introspectable", "Introspect");
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001302}
1303
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001304void handleAction(const crow::Request &req, crow::Response &res,
1305 const std::string &objectPath, const std::string &methodName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001306{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001307 BMCWEB_LOG_DEBUG << "handleAction on path: " << objectPath << " and method "
1308 << methodName;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001309 nlohmann::json requestDbusData =
1310 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001311
Ed Tanous1abe55e2018-09-05 08:30:59 -07001312 if (requestDbusData.is_discarded())
1313 {
Matt Spinler6db06242018-12-11 11:21:22 -06001314 setErrorResponse(res, boost::beast::http::status::bad_request,
1315 noJsonDesc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001316 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001317 return;
1318 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001319 nlohmann::json::iterator data = requestDbusData.find("data");
1320 if (data == requestDbusData.end())
1321 {
Matt Spinler6db06242018-12-11 11:21:22 -06001322 setErrorResponse(res, boost::beast::http::status::bad_request,
1323 noJsonDesc, badReqMsg);
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001324 res.end();
1325 return;
1326 }
1327
1328 if (!data->is_array())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001329 {
Matt Spinler6db06242018-12-11 11:21:22 -06001330 setErrorResponse(res, boost::beast::http::status::bad_request,
1331 noJsonDesc, badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001332 res.end();
1333 return;
1334 }
1335 auto transaction = std::make_shared<InProgressActionData>(res);
1336
1337 transaction->path = objectPath;
1338 transaction->methodName = methodName;
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001339 transaction->arguments = std::move(*data);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001340 crow::connections::systemBus->async_method_call(
1341 [transaction](
1342 const boost::system::error_code ec,
1343 const std::vector<std::pair<std::string, std::vector<std::string>>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001344 &interfaceNames) {
1345 if (ec || interfaceNames.size() <= 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001346 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001347 BMCWEB_LOG_ERROR << "Can't find object";
Matt Spinler6db06242018-12-11 11:21:22 -06001348 setErrorResponse(transaction->res,
1349 boost::beast::http::status::not_found,
1350 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001351 return;
1352 }
1353
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001354 BMCWEB_LOG_DEBUG << "GetObject returned " << interfaceNames.size()
1355 << " object(s)";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001356
1357 for (const std::pair<std::string, std::vector<std::string>>
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001358 &object : interfaceNames)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001359 {
1360 findActionOnInterface(transaction, object.first);
1361 }
1362 },
1363 "xyz.openbmc_project.ObjectMapper",
1364 "/xyz/openbmc_project/object_mapper",
1365 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
1366 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001367}
1368
Matt Spinlerde818812018-12-11 16:39:20 -06001369void handleDelete(const crow::Request &req, crow::Response &res,
1370 const std::string &objectPath)
1371{
1372 BMCWEB_LOG_DEBUG << "handleDelete on path: " << objectPath;
1373
1374 crow::connections::systemBus->async_method_call(
1375 [&res, objectPath](
1376 const boost::system::error_code ec,
1377 const std::vector<std::pair<std::string, std::vector<std::string>>>
1378 &interfaceNames) {
1379 if (ec || interfaceNames.size() <= 0)
1380 {
1381 BMCWEB_LOG_ERROR << "Can't find object";
1382 setErrorResponse(res, boost::beast::http::status::not_found,
1383 notFoundDesc, notFoundMsg);
1384 res.end();
1385 return;
1386 }
1387
1388 auto transaction = std::make_shared<InProgressActionData>(res);
1389 transaction->path = objectPath;
1390 transaction->methodName = "Delete";
1391 transaction->interfaceName = "xyz.openbmc_project.Object.Delete";
1392
1393 for (const std::pair<std::string, std::vector<std::string>>
1394 &object : interfaceNames)
1395 {
1396 findActionOnInterface(transaction, object.first);
1397 }
1398 },
1399 "xyz.openbmc_project.ObjectMapper",
1400 "/xyz/openbmc_project/object_mapper",
1401 "xyz.openbmc_project.ObjectMapper", "GetObject", objectPath,
1402 std::array<const char *, 0>());
1403}
1404
Ed Tanousf839dfe2018-11-12 11:11:15 -08001405void handleList(crow::Response &res, const std::string &objectPath,
1406 int32_t depth = 0)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001407{
1408 crow::connections::systemBus->async_method_call(
1409 [&res](const boost::system::error_code ec,
1410 std::vector<std::string> &objectPaths) {
1411 if (ec)
1412 {
Matt Spinlerd6091dd2018-12-06 14:08:27 -06001413 setErrorResponse(res, boost::beast::http::status::not_found,
1414 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001415 }
1416 else
1417 {
1418 res.jsonValue = {{"status", "ok"},
1419 {"message", "200 OK"},
1420 {"data", std::move(objectPaths)}};
1421 }
1422 res.end();
1423 },
1424 "xyz.openbmc_project.ObjectMapper",
1425 "/xyz/openbmc_project/object_mapper",
1426 "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths", objectPath,
Ed Tanousf839dfe2018-11-12 11:11:15 -08001427 depth, std::array<std::string, 0>());
Ed Tanous1abe55e2018-09-05 08:30:59 -07001428}
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001429
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001430void handleEnumerate(crow::Response &res, const std::string &objectPath)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001431{
Ed Tanous049a0512018-11-01 13:58:42 -07001432 BMCWEB_LOG_DEBUG << "Doing enumerate on " << objectPath;
1433 auto asyncResp = std::make_shared<bmcweb::AsyncResp>(res);
1434
1435 asyncResp->res.jsonValue = {{"message", "200 OK"},
1436 {"status", "ok"},
1437 {"data", nlohmann::json::object()}};
1438
Ed Tanous1abe55e2018-09-05 08:30:59 -07001439 crow::connections::systemBus->async_method_call(
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001440 [objectPath, asyncResp](const boost::system::error_code ec,
1441 GetSubTreeType &object_names) {
1442 auto transaction = std::make_shared<InProgressEnumerateData>(
1443 objectPath, asyncResp);
1444
1445 transaction->subtree =
1446 std::make_shared<GetSubTreeType>(std::move(object_names));
1447
Ed Tanous1abe55e2018-09-05 08:30:59 -07001448 if (ec)
1449 {
Matt Spinler2ae60092018-12-06 10:35:36 -06001450 BMCWEB_LOG_ERROR << "GetSubTree failed on "
1451 << transaction->objectPath;
1452 setErrorResponse(transaction->asyncResp->res,
1453 boost::beast::http::status::not_found,
1454 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001455 return;
1456 }
Ed Tanous64530012018-02-06 17:08:16 -08001457
Matt Spinler3ae4ba72018-12-05 14:01:22 -06001458 // Add the data for the path passed in to the results
1459 // as if GetSubTree returned it, and continue on enumerating
1460 getObjectAndEnumerate(transaction);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001461 },
1462 "xyz.openbmc_project.ObjectMapper",
1463 "/xyz/openbmc_project/object_mapper",
1464 "xyz.openbmc_project.ObjectMapper", "GetSubTree", objectPath,
Ed Tanous049a0512018-11-01 13:58:42 -07001465 static_cast<int32_t>(0), std::array<const char *, 0>());
Ed Tanous64530012018-02-06 17:08:16 -08001466}
Ed Tanous911ac312017-08-15 09:37:42 -07001467
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001468void handleGet(crow::Response &res, std::string &objectPath,
1469 std::string &destProperty)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001470{
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001471 BMCWEB_LOG_DEBUG << "handleGet: " << objectPath << " prop:" << destProperty;
1472 std::shared_ptr<std::string> propertyName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07001473 std::make_shared<std::string>(std::move(destProperty));
Ed Tanous75db20e2018-07-27 13:44:44 -07001474
Ed Tanous1abe55e2018-09-05 08:30:59 -07001475 std::shared_ptr<std::string> path =
1476 std::make_shared<std::string>(std::move(objectPath));
Ed Tanous75db20e2018-07-27 13:44:44 -07001477
Ed Tanous1abe55e2018-09-05 08:30:59 -07001478 using GetObjectType =
1479 std::vector<std::pair<std::string, std::vector<std::string>>>;
1480 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001481 [&res, path, propertyName](const boost::system::error_code ec,
1482 const GetObjectType &object_names) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001483 if (ec || object_names.size() <= 0)
1484 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001485 setErrorResponse(res, boost::beast::http::status::not_found,
1486 notFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001487 res.end();
1488 return;
1489 }
1490 std::shared_ptr<nlohmann::json> response =
1491 std::make_shared<nlohmann::json>(nlohmann::json::object());
1492 // The mapper should never give us an empty interface names list,
1493 // but check anyway
1494 for (const std::pair<std::string, std::vector<std::string>>
1495 connection : object_names)
1496 {
1497 const std::vector<std::string> &interfaceNames =
1498 connection.second;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001499
Ed Tanous1abe55e2018-09-05 08:30:59 -07001500 if (interfaceNames.size() <= 0)
1501 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001502 setErrorResponse(res, boost::beast::http::status::not_found,
1503 notFoundDesc, notFoundMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001504 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001505 return;
1506 }
1507
1508 for (const std::string &interface : interfaceNames)
1509 {
1510 crow::connections::systemBus->async_method_call(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001511 [&res, response, propertyName](
Ed Tanous1abe55e2018-09-05 08:30:59 -07001512 const boost::system::error_code ec,
James Feist5b4aa862018-08-16 14:07:01 -07001513 const std::vector<std::pair<
1514 std::string, dbus::utility::DbusVariantType>>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001515 &properties) {
1516 if (ec)
1517 {
1518 BMCWEB_LOG_ERROR << "Bad dbus request error: "
1519 << ec;
1520 }
1521 else
1522 {
James Feist5b4aa862018-08-16 14:07:01 -07001523 for (const std::pair<
1524 std::string,
1525 dbus::utility::DbusVariantType>
Ed Tanous1abe55e2018-09-05 08:30:59 -07001526 &property : properties)
1527 {
1528 // if property name is empty, or matches our
1529 // search query, add it to the response json
1530
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001531 if (propertyName->empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001532 {
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07001533 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001534 [&response, &property](auto &&val) {
1535 (*response)[property.first] =
1536 val;
1537 },
1538 property.second);
1539 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001540 else if (property.first == *propertyName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001541 {
William A. Kennington III0a63b1c2018-10-18 13:37:19 -07001542 sdbusplus::message::variant_ns::visit(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001543 [&response](auto &&val) {
1544 (*response) = val;
1545 },
1546 property.second);
1547 }
1548 }
1549 }
1550 if (response.use_count() == 1)
1551 {
Matt Spinlerdc2f9f12018-12-06 13:53:53 -06001552 if (!propertyName->empty() && response->empty())
1553 {
1554 setErrorResponse(
1555 res,
1556 boost::beast::http::status::not_found,
1557 propNotFoundDesc, notFoundMsg);
1558 }
1559 else
1560 {
1561 res.jsonValue = {{"status", "ok"},
1562 {"message", "200 OK"},
1563 {"data", *response}};
1564 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001565 res.end();
1566 }
1567 },
1568 connection.first, *path,
1569 "org.freedesktop.DBus.Properties", "GetAll", interface);
1570 }
1571 }
1572 },
1573 "xyz.openbmc_project.ObjectMapper",
1574 "/xyz/openbmc_project/object_mapper",
1575 "xyz.openbmc_project.ObjectMapper", "GetObject", *path,
1576 std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001577}
1578
Ed Tanous1abe55e2018-09-05 08:30:59 -07001579struct AsyncPutRequest
1580{
1581 AsyncPutRequest(crow::Response &res) : res(res)
1582 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001583 }
1584 ~AsyncPutRequest()
1585 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001586 if (res.jsonValue.empty())
1587 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001588 setErrorResponse(res, boost::beast::http::status::forbidden,
1589 forbiddenMsg, forbiddenPropDesc);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001590 }
1591
1592 res.end();
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001593 }
1594
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001595 void setErrorStatus(const std::string &desc)
Ed Tanous1abe55e2018-09-05 08:30:59 -07001596 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001597 setErrorResponse(res, boost::beast::http::status::internal_server_error,
1598 desc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001599 }
1600
Ed Tanous1abe55e2018-09-05 08:30:59 -07001601 crow::Response &res;
1602 std::string objectPath;
1603 std::string propertyName;
1604 nlohmann::json propertyValue;
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001605};
1606
Ed Tanousd76323e2018-08-07 14:35:40 -07001607void handlePut(const crow::Request &req, crow::Response &res,
Ed Tanous1abe55e2018-09-05 08:30:59 -07001608 const std::string &objectPath, const std::string &destProperty)
1609{
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001610 if (destProperty.empty())
1611 {
1612 setErrorResponse(res, boost::beast::http::status::forbidden,
1613 forbiddenResDesc, forbiddenMsg);
1614 res.end();
1615 return;
1616 }
1617
Ed Tanous1abe55e2018-09-05 08:30:59 -07001618 nlohmann::json requestDbusData =
1619 nlohmann::json::parse(req.body, nullptr, false);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001620
Ed Tanous1abe55e2018-09-05 08:30:59 -07001621 if (requestDbusData.is_discarded())
1622 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001623 setErrorResponse(res, boost::beast::http::status::bad_request,
1624 noJsonDesc, badReqMsg);
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001625 res.end();
1626 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001627 }
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001628
Ed Tanous1abe55e2018-09-05 08:30:59 -07001629 nlohmann::json::const_iterator propertyIt = requestDbusData.find("data");
1630 if (propertyIt == requestDbusData.end())
1631 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001632 setErrorResponse(res, boost::beast::http::status::bad_request,
1633 noJsonDesc, badReqMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001634 res.end();
1635 return;
1636 }
1637 const nlohmann::json &propertySetValue = *propertyIt;
1638 auto transaction = std::make_shared<AsyncPutRequest>(res);
1639 transaction->objectPath = objectPath;
1640 transaction->propertyName = destProperty;
1641 transaction->propertyValue = propertySetValue;
Ed Tanous911ac312017-08-15 09:37:42 -07001642
Ed Tanous1abe55e2018-09-05 08:30:59 -07001643 using GetObjectType =
1644 std::vector<std::pair<std::string, std::vector<std::string>>>;
Ed Tanous911ac312017-08-15 09:37:42 -07001645
Ed Tanous1abe55e2018-09-05 08:30:59 -07001646 crow::connections::systemBus->async_method_call(
1647 [transaction](const boost::system::error_code ec,
1648 const GetObjectType &object_names) {
1649 if (!ec && object_names.size() <= 0)
1650 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001651 setErrorResponse(transaction->res,
1652 boost::beast::http::status::not_found,
1653 propNotFoundDesc, notFoundMsg);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001654 return;
1655 }
Ed Tanous911ac312017-08-15 09:37:42 -07001656
Ed Tanous1abe55e2018-09-05 08:30:59 -07001657 for (const std::pair<std::string, std::vector<std::string>>
1658 connection : object_names)
1659 {
1660 const std::string &connectionName = connection.first;
Ed Tanous911ac312017-08-15 09:37:42 -07001661
Ed Tanous1abe55e2018-09-05 08:30:59 -07001662 crow::connections::systemBus->async_method_call(
1663 [connectionName{std::string(connectionName)},
1664 transaction](const boost::system::error_code ec,
1665 const std::string &introspectXml) {
1666 if (ec)
1667 {
1668 BMCWEB_LOG_ERROR
1669 << "Introspect call failed with error: "
1670 << ec.message()
1671 << " on process: " << connectionName;
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001672 transaction->setErrorStatus("Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001673 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001674 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001675 tinyxml2::XMLDocument doc;
Ed Tanous911ac312017-08-15 09:37:42 -07001676
Ed Tanous1abe55e2018-09-05 08:30:59 -07001677 doc.Parse(introspectXml.c_str());
1678 tinyxml2::XMLNode *pRoot =
1679 doc.FirstChildElement("node");
1680 if (pRoot == nullptr)
1681 {
1682 BMCWEB_LOG_ERROR << "XML document failed to parse: "
1683 << introspectXml;
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001684 transaction->setErrorStatus("Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001685 return;
Ed Tanous911ac312017-08-15 09:37:42 -07001686 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001687 tinyxml2::XMLElement *ifaceNode =
1688 pRoot->FirstChildElement("interface");
1689 while (ifaceNode != nullptr)
1690 {
1691 const char *interfaceName =
1692 ifaceNode->Attribute("name");
1693 BMCWEB_LOG_DEBUG << "found interface "
1694 << interfaceName;
1695 tinyxml2::XMLElement *propNode =
1696 ifaceNode->FirstChildElement("property");
1697 while (propNode != nullptr)
1698 {
1699 const char *propertyName =
1700 propNode->Attribute("name");
1701 BMCWEB_LOG_DEBUG << "Found property "
1702 << propertyName;
1703 if (propertyName == transaction->propertyName)
1704 {
1705 const char *argType =
1706 propNode->Attribute("type");
1707 if (argType != nullptr)
1708 {
1709 sdbusplus::message::message m =
1710 crow::connections::systemBus
1711 ->new_method_call(
1712 connectionName.c_str(),
1713 transaction->objectPath
1714 .c_str(),
1715 "org.freedesktop.DBus."
1716 "Properties",
1717 "Set");
1718 m.append(interfaceName,
1719 transaction->propertyName);
1720 int r = sd_bus_message_open_container(
1721 m.get(), SD_BUS_TYPE_VARIANT,
1722 argType);
1723 if (r < 0)
1724 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001725 transaction->setErrorStatus(
1726 "Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001727 return;
1728 }
1729 r = convertJsonToDbus(
1730 m.get(), argType,
1731 transaction->propertyValue);
1732 if (r < 0)
1733 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001734 transaction->setErrorStatus(
1735 "Invalid arg type");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001736 return;
1737 }
1738 r = sd_bus_message_close_container(
1739 m.get());
1740 if (r < 0)
1741 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001742 transaction->setErrorStatus(
1743 "Unexpected Error");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001744 return;
1745 }
Ed Tanous911ac312017-08-15 09:37:42 -07001746
Ed Tanous1abe55e2018-09-05 08:30:59 -07001747 crow::connections::systemBus
1748 ->async_send(
1749 m,
1750 [transaction](
1751 boost::system::error_code
1752 ec,
1753 sdbusplus::message::message
1754 &m) {
1755 BMCWEB_LOG_DEBUG << "sent";
1756 if (ec)
1757 {
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001758 setErrorResponse(
1759 transaction->res,
1760 boost::beast::http::
1761 status::
1762 forbidden,
1763 forbiddenPropDesc,
1764 ec.message());
1765 }
1766 else
1767 {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001768 transaction->res
Matt Spinlerfbc19ea2018-12-11 14:03:42 -06001769 .jsonValue = {
1770 {"status", "ok"},
1771 {"message",
1772 "200 OK"},
1773 {"data", nullptr}};
Ed Tanous1abe55e2018-09-05 08:30:59 -07001774 }
1775 });
1776 }
1777 }
1778 propNode =
1779 propNode->NextSiblingElement("property");
1780 }
1781 ifaceNode =
1782 ifaceNode->NextSiblingElement("interface");
1783 }
1784 },
1785 connectionName, transaction->objectPath,
1786 "org.freedesktop.DBus.Introspectable", "Introspect");
1787 }
1788 },
1789 "xyz.openbmc_project.ObjectMapper",
1790 "/xyz/openbmc_project/object_mapper",
1791 "xyz.openbmc_project.ObjectMapper", "GetObject",
1792 transaction->objectPath, std::array<std::string, 0>());
Ed Tanousd4bb9bb2018-05-16 13:36:42 -07001793}
Ed Tanous1abe55e2018-09-05 08:30:59 -07001794
Ed Tanous049a0512018-11-01 13:58:42 -07001795inline void handleDBusUrl(const crow::Request &req, crow::Response &res,
1796 std::string &objectPath)
1797{
Ed Tanous049a0512018-11-01 13:58:42 -07001798
1799 // If accessing a single attribute, fill in and update objectPath,
1800 // otherwise leave destProperty blank
1801 std::string destProperty = "";
1802 const char *attrSeperator = "/attr/";
1803 size_t attrPosition = objectPath.find(attrSeperator);
1804 if (attrPosition != objectPath.npos)
1805 {
1806 destProperty = objectPath.substr(attrPosition + strlen(attrSeperator),
1807 objectPath.length());
1808 objectPath = objectPath.substr(0, attrPosition);
1809 }
1810
1811 if (req.method() == "POST"_method)
1812 {
1813 constexpr const char *actionSeperator = "/action/";
1814 size_t actionPosition = objectPath.find(actionSeperator);
1815 if (actionPosition != objectPath.npos)
1816 {
1817 std::string postProperty =
1818 objectPath.substr((actionPosition + strlen(actionSeperator)),
1819 objectPath.length());
1820 objectPath = objectPath.substr(0, actionPosition);
1821 handleAction(req, res, objectPath, postProperty);
1822 return;
1823 }
1824 }
1825 else if (req.method() == "GET"_method)
1826 {
1827 if (boost::ends_with(objectPath, "/enumerate"))
1828 {
1829 objectPath.erase(objectPath.end() - sizeof("enumerate"),
1830 objectPath.end());
1831 handleEnumerate(res, objectPath);
1832 }
1833 else if (boost::ends_with(objectPath, "/list"))
1834 {
1835 objectPath.erase(objectPath.end() - sizeof("list"),
1836 objectPath.end());
1837 handleList(res, objectPath);
1838 }
1839 else
1840 {
Ed Tanousf839dfe2018-11-12 11:11:15 -08001841 // Trim any trailing "/" at the end
1842 if (boost::ends_with(objectPath, "/"))
1843 {
1844 objectPath.pop_back();
1845 handleList(res, objectPath, 1);
1846 }
1847 else
1848 {
1849 handleGet(res, objectPath, destProperty);
1850 }
Ed Tanous049a0512018-11-01 13:58:42 -07001851 }
1852 return;
1853 }
1854 else if (req.method() == "PUT"_method)
1855 {
1856 handlePut(req, res, objectPath, destProperty);
1857 return;
1858 }
Matt Spinlerde818812018-12-11 16:39:20 -06001859 else if (req.method() == "DELETE"_method)
1860 {
1861 handleDelete(req, res, objectPath);
1862 return;
1863 }
Ed Tanous049a0512018-11-01 13:58:42 -07001864
Matt Spinlerc4e8d21d62018-12-11 11:47:17 -06001865 setErrorResponse(res, boost::beast::http::status::method_not_allowed,
1866 methodNotAllowedDesc, methodNotAllowedMsg);
Ed Tanous049a0512018-11-01 13:58:42 -07001867 res.end();
1868}
1869
Ed Tanous1abe55e2018-09-05 08:30:59 -07001870template <typename... Middlewares> void requestRoutes(Crow<Middlewares...> &app)
1871{
1872 BMCWEB_ROUTE(app, "/bus/")
1873 .methods("GET"_method)(
1874 [](const crow::Request &req, crow::Response &res) {
1875 res.jsonValue = {{"busses", {{{"name", "system"}}}},
1876 {"status", "ok"}};
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001877 res.end();
Ed Tanous1abe55e2018-09-05 08:30:59 -07001878 });
1879
1880 BMCWEB_ROUTE(app, "/bus/system/")
1881 .methods("GET"_method)(
1882 [](const crow::Request &req, crow::Response &res) {
1883 auto myCallback = [&res](const boost::system::error_code ec,
1884 std::vector<std::string> &names) {
1885 if (ec)
1886 {
1887 BMCWEB_LOG_ERROR << "Dbus call failed with code " << ec;
1888 res.result(
1889 boost::beast::http::status::internal_server_error);
1890 }
1891 else
1892 {
1893 std::sort(names.begin(), names.end());
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001894 res.jsonValue = {{"status", "ok"}};
1895 auto &objectsSub = res.jsonValue["objects"];
Ed Tanous1abe55e2018-09-05 08:30:59 -07001896 for (auto &name : names)
1897 {
1898 objectsSub.push_back({{"name", name}});
1899 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07001900 }
1901 res.end();
1902 };
1903 crow::connections::systemBus->async_method_call(
1904 std::move(myCallback), "org.freedesktop.DBus", "/",
1905 "org.freedesktop.DBus", "ListNames");
1906 });
1907
1908 BMCWEB_ROUTE(app, "/list/")
1909 .methods("GET"_method)(
1910 [](const crow::Request &req, crow::Response &res) {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001911 handleList(res, "/");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001912 });
1913
1914 BMCWEB_ROUTE(app, "/xyz/<path>")
Matt Spinlerde818812018-12-11 16:39:20 -06001915 .methods("GET"_method, "PUT"_method, "POST"_method, "DELETE"_method)(
Ed Tanous049a0512018-11-01 13:58:42 -07001916 [](const crow::Request &req, crow::Response &res,
1917 const std::string &path) {
1918 std::string objectPath = "/xyz/" + path;
1919 handleDBusUrl(req, res, objectPath);
1920 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001921
Ed Tanous049a0512018-11-01 13:58:42 -07001922 BMCWEB_ROUTE(app, "/org/<path>")
Matt Spinlerde818812018-12-11 16:39:20 -06001923 .methods("GET"_method, "PUT"_method, "POST"_method, "DELETE"_method)(
Ed Tanous049a0512018-11-01 13:58:42 -07001924 [](const crow::Request &req, crow::Response &res,
1925 const std::string &path) {
1926 std::string objectPath = "/org/" + path;
1927 handleDBusUrl(req, res, objectPath);
1928 });
Ed Tanous1abe55e2018-09-05 08:30:59 -07001929
Ed Tanous1abe55e2018-09-05 08:30:59 -07001930 BMCWEB_ROUTE(app, "/download/dump/<str>/")
1931 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
1932 const std::string &dumpId) {
Ed Tanousad18f072018-11-14 14:07:48 -08001933 std::regex validFilename("^[\\w\\- ]+(\\.?[\\w\\- ]*)$");
Ed Tanous1abe55e2018-09-05 08:30:59 -07001934 if (!std::regex_match(dumpId, validFilename))
1935 {
Ed Tanousad18f072018-11-14 14:07:48 -08001936 res.result(boost::beast::http::status::bad_request);
Ed Tanous1abe55e2018-09-05 08:30:59 -07001937 res.end();
1938 return;
1939 }
James Feistf6150402019-01-08 10:36:20 -08001940 std::filesystem::path loc(
Ed Tanous1abe55e2018-09-05 08:30:59 -07001941 "/var/lib/phosphor-debug-collector/dumps");
1942
Ed Tanousad18f072018-11-14 14:07:48 -08001943 loc /= dumpId;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001944
James Feistf6150402019-01-08 10:36:20 -08001945 if (!std::filesystem::exists(loc) ||
1946 !std::filesystem::is_directory(loc))
Ed Tanous1abe55e2018-09-05 08:30:59 -07001947 {
Ed Tanousad18f072018-11-14 14:07:48 -08001948 BMCWEB_LOG_ERROR << loc << "Not found";
Ed Tanous1abe55e2018-09-05 08:30:59 -07001949 res.result(boost::beast::http::status::not_found);
1950 res.end();
1951 return;
1952 }
James Feistf6150402019-01-08 10:36:20 -08001953 std::filesystem::directory_iterator files(loc);
Ed Tanousad18f072018-11-14 14:07:48 -08001954
Ed Tanous1abe55e2018-09-05 08:30:59 -07001955 for (auto &file : files)
1956 {
1957 std::ifstream readFile(file.path());
Ed Tanousad18f072018-11-14 14:07:48 -08001958 if (!readFile.good())
Ed Tanous1abe55e2018-09-05 08:30:59 -07001959 {
1960 continue;
1961 }
1962 res.addHeader("Content-Type", "application/octet-stream");
1963 res.body() = {std::istreambuf_iterator<char>(readFile),
1964 std::istreambuf_iterator<char>()};
1965 res.end();
Ed Tanousad18f072018-11-14 14:07:48 -08001966 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07001967 }
1968 res.result(boost::beast::http::status::not_found);
1969 res.end();
1970 return;
1971 });
1972
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001973 BMCWEB_ROUTE(app, "/bus/system/<str>/")
Ed Tanous1abe55e2018-09-05 08:30:59 -07001974 .methods("GET"_method)([](const crow::Request &req, crow::Response &res,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07001975 const std::string &Connection) {
1976 introspectObjects(Connection, "/",
1977 std::make_shared<bmcweb::AsyncResp>(res));
1978 });
1979
1980 BMCWEB_ROUTE(app, "/bus/system/<str>/<path>")
1981 .methods("GET"_method,
1982 "POST"_method)([](const crow::Request &req,
1983 crow::Response &res,
1984 const std::string &processName,
1985 const std::string &requestedPath) {
Ed Tanous1abe55e2018-09-05 08:30:59 -07001986 std::vector<std::string> strs;
1987 boost::split(strs, requestedPath, boost::is_any_of("/"));
1988 std::string objectPath;
1989 std::string interfaceName;
1990 std::string methodName;
1991 auto it = strs.begin();
1992 if (it == strs.end())
1993 {
1994 objectPath = "/";
1995 }
1996 while (it != strs.end())
1997 {
1998 // Check if segment contains ".". If it does, it must be an
1999 // interface
2000 if (it->find(".") != std::string::npos)
2001 {
2002 break;
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002003 // This check is neccesary as the trailing slash gets parsed
Ed Tanous1abe55e2018-09-05 08:30:59 -07002004 // as part of our <path> specifier above, which causes the
2005 // normal trailing backslash redirector to fail.
2006 }
2007 else if (!it->empty())
2008 {
2009 objectPath += "/" + *it;
2010 }
2011 it++;
2012 }
2013 if (it != strs.end())
2014 {
2015 interfaceName = *it;
2016 it++;
2017
2018 // after interface, we might have a method name
2019 if (it != strs.end())
2020 {
2021 methodName = *it;
2022 it++;
2023 }
2024 }
2025 if (it != strs.end())
2026 {
2027 // if there is more levels past the method name, something went
2028 // wrong, return not found
2029 res.result(boost::beast::http::status::not_found);
2030 res.end();
2031 return;
2032 }
2033 if (interfaceName.empty())
2034 {
2035 crow::connections::systemBus->async_method_call(
2036 [&, processName,
2037 objectPath](const boost::system::error_code ec,
2038 const std::string &introspect_xml) {
2039 if (ec)
2040 {
2041 BMCWEB_LOG_ERROR
2042 << "Introspect call failed with error: "
2043 << ec.message()
2044 << " on process: " << processName
2045 << " path: " << objectPath << "\n";
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002046 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002047 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002048 tinyxml2::XMLDocument doc;
2049
2050 doc.Parse(introspect_xml.c_str());
2051 tinyxml2::XMLNode *pRoot =
2052 doc.FirstChildElement("node");
2053 if (pRoot == nullptr)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002054 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002055 BMCWEB_LOG_ERROR << "XML document failed to parse "
2056 << processName << " " << objectPath
2057 << "\n";
2058 res.jsonValue = {{"status", "XML parse error"}};
2059 res.result(boost::beast::http::status::
2060 internal_server_error);
2061 return;
Ed Tanous1abe55e2018-09-05 08:30:59 -07002062 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002063
2064 BMCWEB_LOG_DEBUG << introspect_xml;
2065 res.jsonValue = {{"status", "ok"},
2066 {"bus_name", processName},
2067 {"object_path", objectPath}};
2068 nlohmann::json &interfacesArray =
2069 res.jsonValue["interfaces"];
2070 interfacesArray = nlohmann::json::array();
2071 tinyxml2::XMLElement *interface =
2072 pRoot->FirstChildElement("interface");
2073
2074 while (interface != nullptr)
2075 {
2076 const char *ifaceName =
2077 interface->Attribute("name");
2078 if (ifaceName != nullptr)
2079 {
2080 interfacesArray.push_back(
2081 {{"name", ifaceName}});
2082 }
2083
2084 interface =
2085 interface->NextSiblingElement("interface");
2086 }
2087
Ed Tanous1abe55e2018-09-05 08:30:59 -07002088 res.end();
2089 },
2090 processName, objectPath,
2091 "org.freedesktop.DBus.Introspectable", "Introspect");
2092 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002093 else if (methodName.empty())
Ed Tanous1abe55e2018-09-05 08:30:59 -07002094 {
2095 crow::connections::systemBus->async_method_call(
2096 [&, processName, objectPath,
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002097 interfaceName{std::move(interfaceName)}](
Ed Tanous1abe55e2018-09-05 08:30:59 -07002098 const boost::system::error_code ec,
2099 const std::string &introspect_xml) {
2100 if (ec)
2101 {
2102 BMCWEB_LOG_ERROR
2103 << "Introspect call failed with error: "
2104 << ec.message()
2105 << " on process: " << processName
2106 << " path: " << objectPath << "\n";
2107 }
2108 else
2109 {
2110 tinyxml2::XMLDocument doc;
2111
2112 doc.Parse(introspect_xml.c_str());
2113 tinyxml2::XMLNode *pRoot =
2114 doc.FirstChildElement("node");
2115 if (pRoot == nullptr)
2116 {
2117 BMCWEB_LOG_ERROR
2118 << "XML document failed to parse "
2119 << processName << " " << objectPath << "\n";
2120 res.result(boost::beast::http::status::
2121 internal_server_error);
2122 }
2123 else
2124 {
2125 tinyxml2::XMLElement *node =
2126 pRoot->FirstChildElement("node");
2127
2128 // if we know we're the only call, build the
2129 // json directly
Ed Tanous1abe55e2018-09-05 08:30:59 -07002130 tinyxml2::XMLElement *interface =
2131 pRoot->FirstChildElement("interface");
2132
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002133 res.jsonValue = {
2134 {"status", "ok"},
2135 {"bus_name", processName},
2136 {"interface", interfaceName},
2137 {"object_path", objectPath},
2138 {"properties", nlohmann::json::object()}};
2139
2140 nlohmann::json &methodsArray =
2141 res.jsonValue["methods"];
2142 methodsArray = nlohmann::json::array();
2143
2144 nlohmann::json &signalsArray =
2145 res.jsonValue["signals"];
2146 signalsArray = nlohmann::json::array();
2147
Ed Tanous1abe55e2018-09-05 08:30:59 -07002148 while (interface != nullptr)
2149 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002150 const char *ifaceName =
Ed Tanous1abe55e2018-09-05 08:30:59 -07002151 interface->Attribute("name");
2152
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002153 if (ifaceName != nullptr &&
2154 ifaceName == interfaceName)
Ed Tanous1abe55e2018-09-05 08:30:59 -07002155 {
2156 tinyxml2::XMLElement *methods =
2157 interface->FirstChildElement(
2158 "method");
2159 while (methods != nullptr)
2160 {
2161 nlohmann::json argsArray =
2162 nlohmann::json::array();
2163 tinyxml2::XMLElement *arg =
2164 methods->FirstChildElement(
2165 "arg");
2166 while (arg != nullptr)
2167 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002168 nlohmann::json thisArg;
2169 for (const char *fieldName :
2170 std::array<const char *,
2171 3>{"name",
2172 "direction",
2173 "type"})
2174 {
2175 const char *fieldValue =
2176 arg->Attribute(
2177 fieldName);
2178 if (fieldValue != nullptr)
2179 {
2180 thisArg[fieldName] =
2181 fieldValue;
2182 }
2183 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002184 argsArray.push_back(
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002185 std::move(thisArg));
Ed Tanous1abe55e2018-09-05 08:30:59 -07002186 arg = arg->NextSiblingElement(
2187 "arg");
2188 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002189
2190 const char *name =
2191 methods->Attribute("name");
2192 if (name != nullptr)
2193 {
2194 methodsArray.push_back(
2195 {{"name", name},
2196 {"uri", "/bus/system/" +
2197 processName +
2198 objectPath +
2199 "/" +
2200 interfaceName +
2201 "/" + name},
2202 {"args", argsArray}});
2203 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002204 methods =
2205 methods->NextSiblingElement(
2206 "method");
2207 }
2208 tinyxml2::XMLElement *signals =
2209 interface->FirstChildElement(
2210 "signal");
2211 while (signals != nullptr)
2212 {
2213 nlohmann::json argsArray =
2214 nlohmann::json::array();
2215
2216 tinyxml2::XMLElement *arg =
2217 signals->FirstChildElement(
2218 "arg");
2219 while (arg != nullptr)
2220 {
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002221 const char *name =
Ed Tanous1abe55e2018-09-05 08:30:59 -07002222 arg->Attribute("name");
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002223 const char *type =
Ed Tanous1abe55e2018-09-05 08:30:59 -07002224 arg->Attribute("type");
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002225 if (name != nullptr &&
2226 type != nullptr)
2227 {
2228 argsArray.push_back({
2229 {"name", name},
2230 {"type", type},
2231 });
2232 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002233 arg = arg->NextSiblingElement(
2234 "arg");
2235 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002236 const char *name =
2237 signals->Attribute("name");
2238 if (name != nullptr)
2239 {
2240 signalsArray.push_back(
2241 {{"name", name},
2242 {"args", argsArray}});
2243 }
2244
Ed Tanous1abe55e2018-09-05 08:30:59 -07002245 signals =
2246 signals->NextSiblingElement(
2247 "signal");
2248 }
2249
Ed Tanous1abe55e2018-09-05 08:30:59 -07002250 break;
2251 }
2252
2253 interface = interface->NextSiblingElement(
2254 "interface");
2255 }
2256 if (interface == nullptr)
2257 {
2258 // if we got to the end of the list and
2259 // never found a match, throw 404
2260 res.result(
2261 boost::beast::http::status::not_found);
2262 }
2263 }
2264 }
2265 res.end();
2266 },
2267 processName, objectPath,
2268 "org.freedesktop.DBus.Introspectable", "Introspect");
2269 }
Ed Tanouse3cb5a32018-08-08 14:16:49 -07002270 else
2271 {
2272 if (req.method() != "POST"_method)
2273 {
2274 res.result(boost::beast::http::status::not_found);
2275 res.end();
2276 return;
2277 }
2278
2279 nlohmann::json requestDbusData =
2280 nlohmann::json::parse(req.body, nullptr, false);
2281
2282 if (requestDbusData.is_discarded())
2283 {
2284 res.result(boost::beast::http::status::bad_request);
2285 res.end();
2286 return;
2287 }
2288 if (!requestDbusData.is_array())
2289 {
2290 res.result(boost::beast::http::status::bad_request);
2291 res.end();
2292 return;
2293 }
2294 auto transaction = std::make_shared<InProgressActionData>(res);
2295
2296 transaction->path = objectPath;
2297 transaction->methodName = methodName;
2298 transaction->arguments = std::move(requestDbusData);
2299
2300 findActionOnInterface(transaction, processName);
2301 }
Ed Tanous1abe55e2018-09-05 08:30:59 -07002302 });
2303}
2304} // namespace openbmc_mapper
2305} // namespace crow