xref: /openbmc/openpower-proc-control/ext_interface.cpp (revision 0c5fafdafcd4608f6bba8920dc69de31ed29bde0)
1 #include <ext_interface.hpp>
2 #include <phosphor-logging/log.hpp>
3 #include <sdbusplus/server.hpp>
4 
5 #include <string>
6 
7 // Mapper
8 constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
9 constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
10 constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
11 
12 // Reboot count
13 constexpr auto REBOOTCOUNTER_PATH("/xyz/openbmc_project/state/host0");
14 constexpr auto REBOOTCOUNTER_INTERFACE(
15     "xyz.openbmc_project.Control.Boot.RebootAttempts");
16 
17 using namespace phosphor::logging;
18 
19 /**
20  * @brief Get DBUS service for input interface via mapper call
21  *
22  * This is an internal function to be used only by functions within this
23  * file.
24  *
25  * @param[in] bus -  DBUS Bus Object
26  * @param[in] intf - DBUS Interface
27  * @param[in] path - DBUS Object Path
28  *
29  * @return distinct dbus name for input interface/path
30  **/
getService(sdbusplus::bus_t & bus,const std::string & intf,const std::string & path)31 std::string getService(sdbusplus::bus_t& bus, const std::string& intf,
32                        const std::string& path)
33 {
34     auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
35                                       MAPPER_INTERFACE, "GetObject");
36 
37     mapper.append(path);
38     mapper.append(std::vector<std::string>({intf}));
39 
40     auto mapperResponseMsg = bus.call(mapper);
41 
42     auto mapperResponse =
43         mapperResponseMsg
44             .unpack<std::map<std::string, std::vector<std::string>>>();
45 
46     if (mapperResponse.empty())
47     {
48         // TODO openbmc/openbmc#1712 - Handle empty mapper resp. consistently
49         throw std::runtime_error("ERROR in reading the mapper response");
50     }
51 
52     return mapperResponse.begin()->first;
53 }
54 
getBootCount()55 uint32_t getBootCount()
56 {
57     auto bus = sdbusplus::bus::new_default();
58 
59     auto rebootSvc =
60         getService(bus, REBOOTCOUNTER_INTERFACE, REBOOTCOUNTER_PATH);
61 
62     auto method = bus.new_method_call(rebootSvc.c_str(), REBOOTCOUNTER_PATH,
63                                       "org.freedesktop.DBus.Properties", "Get");
64 
65     method.append(REBOOTCOUNTER_INTERFACE, "AttemptsLeft");
66     auto reply = bus.call(method);
67 
68     auto rebootCount = reply.unpack<std::variant<uint32_t>>();
69 
70     return std::get<uint32_t>(rebootCount);
71 }
72