xref: /openbmc/fb-ipmi-oem/src/commandutils.cpp (revision 010dee0487db34138e6311a5bbb51e0f7683de30)
1b2ae88b4Scchoux /*
2b2ae88b4Scchoux  * Copyright (c)  2023-present Facebook.
3b2ae88b4Scchoux  *
4b2ae88b4Scchoux  * Licensed under the Apache License, Version 2.0 (the "License");
5b2ae88b4Scchoux  * you may not use this file except in compliance with the License.
6b2ae88b4Scchoux  * You may obtain a copy of the License at
7b2ae88b4Scchoux  *
8b2ae88b4Scchoux  *      http://www.apache.org/licenses/LICENSE-2.0
9b2ae88b4Scchoux  *
10b2ae88b4Scchoux  * Unless required by applicable law or agreed to in writing, software
11b2ae88b4Scchoux  * distributed under the License is distributed on an "AS IS" BASIS,
12b2ae88b4Scchoux  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13b2ae88b4Scchoux  * See the License for the specific language governing permissions and
14b2ae88b4Scchoux  * limitations under the License.
15b2ae88b4Scchoux  */
16b2ae88b4Scchoux 
17b2ae88b4Scchoux #include <commandutils.hpp>
18b2ae88b4Scchoux #include <ipmid/utils.hpp>
19b2ae88b4Scchoux #include <phosphor-logging/log.hpp>
20b2ae88b4Scchoux 
getMbFruDevice(void)21b2ae88b4Scchoux std::optional<std::pair<uint8_t, uint8_t>> getMbFruDevice(void)
22b2ae88b4Scchoux {
23b2ae88b4Scchoux     static std::optional<std::pair<uint8_t, uint8_t>> device = std::nullopt;
24b2ae88b4Scchoux 
25b2ae88b4Scchoux     if (device)
26b2ae88b4Scchoux     {
27b2ae88b4Scchoux         return device;
28b2ae88b4Scchoux     }
29b2ae88b4Scchoux 
30b2ae88b4Scchoux     sdbusplus::bus_t dbus(ipmid_get_sd_bus_connection());
31*010dee04SPatrick Williams     auto mapperCall = dbus.new_method_call(
32b2ae88b4Scchoux         "xyz.openbmc_project.ObjectMapper",
33*010dee04SPatrick Williams         "/xyz/openbmc_project/object_mapper",
34*010dee04SPatrick Williams         "xyz.openbmc_project.ObjectMapper", "GetSubTreePaths");
35b2ae88b4Scchoux     static constexpr int32_t depth = 0;
36b2ae88b4Scchoux     static constexpr auto iface = "xyz.openbmc_project.Configuration.EEPROM";
37b2ae88b4Scchoux     static constexpr auto entityManager = "xyz.openbmc_project.EntityManager";
38b2ae88b4Scchoux     static constexpr std::array<const char*, 1> interface = {iface};
39b2ae88b4Scchoux     mapperCall.append("/xyz/openbmc_project/inventory/", depth, interface);
40b2ae88b4Scchoux 
41b2ae88b4Scchoux     std::vector<std::string> paths;
42b2ae88b4Scchoux     try
43b2ae88b4Scchoux     {
44b2ae88b4Scchoux         auto resp = dbus.call(mapperCall);
45b2ae88b4Scchoux         resp.read(paths);
46b2ae88b4Scchoux     }
47b2ae88b4Scchoux     catch (const sdbusplus::exception_t& e)
48b2ae88b4Scchoux     {
49b2ae88b4Scchoux         phosphor::logging::log<phosphor::logging::level::ERR>(e.what());
50b2ae88b4Scchoux         return std::nullopt;
51b2ae88b4Scchoux     }
52b2ae88b4Scchoux 
53b2ae88b4Scchoux     const std::string suffix = "/MB_FRU";
54b2ae88b4Scchoux     for (const auto& path : paths)
55b2ae88b4Scchoux     {
56b2ae88b4Scchoux         if (path.ends_with(suffix))
57b2ae88b4Scchoux         {
58b2ae88b4Scchoux             uint8_t fruBus = std::get<uint64_t>(
59b2ae88b4Scchoux                 ipmi::getDbusProperty(dbus, entityManager, path, iface, "Bus"));
60b2ae88b4Scchoux             uint8_t fruAddr = std::get<uint64_t>(ipmi::getDbusProperty(
61b2ae88b4Scchoux                 dbus, entityManager, path, iface, "Address"));
62b2ae88b4Scchoux             device = std::make_pair(fruBus, fruAddr);
63b2ae88b4Scchoux             break;
64b2ae88b4Scchoux         }
65b2ae88b4Scchoux     }
66b2ae88b4Scchoux 
67b2ae88b4Scchoux     return device;
68b2ae88b4Scchoux }
69