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
15     REBOOTCOUNTER_INTERFACE("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  **/
31 std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
32                        const std::string& path)
33 {
34 
35     auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
36                                       MAPPER_INTERFACE, "GetObject");
37 
38     mapper.append(path);
39     mapper.append(std::vector<std::string>({intf}));
40 
41     auto mapperResponseMsg = bus.call(mapper);
42 
43     if (mapperResponseMsg.is_method_error())
44     {
45         // TODO openbmc/openbmc#851 - Once available, throw returned error
46         throw std::runtime_error("ERROR in mapper call");
47     }
48 
49     std::map<std::string, std::vector<std::string>> mapperResponse;
50     mapperResponseMsg.read(mapperResponse);
51 
52     if (mapperResponse.empty())
53     {
54         // TODO openbmc/openbmc#1712 - Handle empty mapper resp. consistently
55         throw std::runtime_error("ERROR in reading the mapper response");
56     }
57 
58     return mapperResponse.begin()->first;
59 }
60 
61 uint32_t getBootCount()
62 {
63     auto bus = sdbusplus::bus::new_default();
64 
65     auto rebootSvc =
66         getService(bus, REBOOTCOUNTER_INTERFACE, REBOOTCOUNTER_PATH);
67 
68     auto method = bus.new_method_call(rebootSvc.c_str(), REBOOTCOUNTER_PATH,
69                                       "org.freedesktop.DBus.Properties", "Get");
70 
71     method.append(REBOOTCOUNTER_INTERFACE, "AttemptsLeft");
72     auto reply = bus.call(method);
73     if (reply.is_method_error())
74     {
75         log<level::ERR>("Error in BOOTCOUNT getValue");
76         // TODO openbmc/openbmc#851 - Once available, throw returned error
77         throw std::runtime_error("ERROR in reading BOOTCOUNT");
78     }
79     std::variant<uint32_t> rebootCount;
80     reply.read(rebootCount);
81 
82     return std::get<uint32_t>(rebootCount);
83 }
84