xref: /openbmc/fb-ipmi-oem/src/commandutils.cpp (revision b647d63f)
1 /*
2  * Copyright (c)  2023-present Facebook.
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 
17 #include <commandutils.hpp>
18 #include <ipmid/utils.hpp>
19 #include <phosphor-logging/log.hpp>
20 
21 std::optional<std::pair<uint8_t, uint8_t>> getMbFruDevice(void)
22 {
23     static std::optional<std::pair<uint8_t, uint8_t>> device = std::nullopt;
24 
25     if (device)
26     {
27         return device;
28     }
29 
30     sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection());
31     auto mapperCall = dbus.new_method_call("xyz.openbmc_project.ObjectMapper",
32                                            "/xyz/openbmc_project/object_mapper",
33                                            "xyz.openbmc_project.ObjectMapper",
34                                            "GetSubTreePaths");
35     static constexpr int32_t depth = 0;
36     static constexpr auto iface = "xyz.openbmc_project.Configuration.EEPROM";
37     static constexpr auto entityManager = "xyz.openbmc_project.EntityManager";
38     static constexpr std::array<const char*, 1> interface = {iface};
39     mapperCall.append("/xyz/openbmc_project/inventory/", depth, interface);
40 
41     std::vector<std::string> paths;
42     try
43     {
44         auto resp = dbus.call(mapperCall);
45         resp.read(paths);
46     }
47     catch (const sdbusplus::exception_t& e)
48     {
49         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
50         return std::nullopt;
51     }
52 
53     const std::string suffix = "/MB_FRU";
54     for (const auto& path : paths)
55     {
56         if (path.ends_with(suffix))
57         {
58             uint8_t fruBus = std::get<uint64_t>(
59                 ipmi::getDbusProperty(dbus, entityManager, path, iface, "Bus"));
60             uint8_t fruAddr = std::get<uint64_t>(ipmi::getDbusProperty(
61                 dbus, entityManager, path, iface, "Address"));
62             device = std::make_pair(fruBus, fruAddr);
63             break;
64         }
65     }
66 
67     return device;
68 }
69