1 /*
2 // Copyright (c) 2019 Intel Corporation
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 //      http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15 */
16 #ifndef BMCWEB_ENABLE_REDFISH_ONE_CHASSIS
17 #pragma once
18 
19 #include <node.hpp>
20 
21 namespace redfish
22 {
23 
24 template <typename CallbackFunc>
25 void getMainChassisId(std::shared_ptr<AsyncResp> asyncResp,
26                       CallbackFunc&& callback)
27 {
28     // Find managed chassis
29     crow::connections::systemBus->async_method_call(
30         [callback,
31          asyncResp](const boost::system::error_code ec,
32                     const crow::openbmc_mapper::GetSubTreeType& subtree) {
33             if (ec)
34             {
35                 BMCWEB_LOG_ERROR << ec;
36                 return;
37             }
38             if (subtree.size() == 0)
39             {
40                 BMCWEB_LOG_DEBUG << "Can't find chassis!";
41                 return;
42             }
43 
44             std::size_t idPos = subtree[0].first.rfind('/');
45             if (idPos == std::string::npos ||
46                 (idPos + 1) >= subtree[0].first.size())
47             {
48                 messages::internalError(asyncResp->res);
49                 BMCWEB_LOG_DEBUG << "Can't parse chassis ID!";
50                 return;
51             }
52             std::string chassisId = subtree[0].first.substr(idPos + 1);
53             BMCWEB_LOG_DEBUG << "chassisId = " << chassisId;
54             callback(chassisId, asyncResp);
55         },
56         "xyz.openbmc_project.ObjectMapper",
57         "/xyz/openbmc_project/object_mapper",
58         "xyz.openbmc_project.ObjectMapper", "GetSubTree",
59         "/xyz/openbmc_project/inventory", 0,
60         std::array<const char*, 2>{
61             "xyz.openbmc_project.Inventory.Item.Board",
62             "xyz.openbmc_project.Inventory.Item.Chassis"});
63 }
64 } // namespace redfish
65 #endif
66