blob: a5c1c3cc4406195a8c98096432d3e4c06823fc9a [file] [log] [blame]
Andrew Geissler2548c7a2017-05-18 15:35:40 -05001#include <ext_interface.hpp>
Patrick Venturef78d9042018-11-01 15:39:53 -07002#include <phosphor-logging/log.hpp>
3#include <sdbusplus/server.hpp>
4#include <string>
Andrew Geissler2548c7a2017-05-18 15:35:40 -05005
6// Mapper
7constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
8constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
9constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
10
11// Reboot count
Andrew Geisslerbc7cdf42017-08-07 14:55:42 -050012constexpr auto REBOOTCOUNTER_PATH("/xyz/openbmc_project/state/host0");
Patrick Venturef78d9042018-11-01 15:39:53 -070013constexpr auto
14 REBOOTCOUNTER_INTERFACE("xyz.openbmc_project.Control.Boot.RebootAttempts");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050015
16using namespace phosphor::logging;
17
18/**
19 * @brief Get DBUS service for input interface via mapper call
20 *
21 * This is an internal function to be used only by functions within this
22 * file.
23 *
24 * @param[in] bus - DBUS Bus Object
25 * @param[in] intf - DBUS Interface
26 * @param[in] path - DBUS Object Path
27 *
28 * @return distinct dbus name for input interface/path
29 **/
Patrick Venturef78d9042018-11-01 15:39:53 -070030std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
Andrew Geissler2548c7a2017-05-18 15:35:40 -050031 const std::string& path)
32{
33
Patrick Venturef78d9042018-11-01 15:39:53 -070034 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
35 MAPPER_INTERFACE, "GetObject");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050036
37 mapper.append(path);
38 mapper.append(std::vector<std::string>({intf}));
39
40 auto mapperResponseMsg = bus.call(mapper);
41
42 if (mapperResponseMsg.is_method_error())
43 {
44 // TODO openbmc/openbmc#851 - Once available, throw returned error
45 throw std::runtime_error("ERROR in mapper call");
46 }
47
48 std::map<std::string, std::vector<std::string>> mapperResponse;
49 mapperResponseMsg.read(mapperResponse);
50
51 if (mapperResponse.empty())
52 {
53 // TODO openbmc/openbmc#1712 - Handle empty mapper resp. consistently
54 throw std::runtime_error("ERROR in reading the mapper response");
55 }
56
57 return mapperResponse.begin()->first;
58}
59
Andrew Geisslerd8dd9b22017-08-19 13:44:44 -050060uint32_t getBootCount()
Andrew Geissler2548c7a2017-05-18 15:35:40 -050061{
62 auto bus = sdbusplus::bus::new_default();
63
Patrick Venturef78d9042018-11-01 15:39:53 -070064 auto rebootSvc =
65 getService(bus, REBOOTCOUNTER_INTERFACE, REBOOTCOUNTER_PATH);
Andrew Geissler2548c7a2017-05-18 15:35:40 -050066
Patrick Venturef78d9042018-11-01 15:39:53 -070067 auto method = bus.new_method_call(rebootSvc.c_str(), REBOOTCOUNTER_PATH,
68 "org.freedesktop.DBus.Properties", "Get");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050069
Andrew Geisslerbc7cdf42017-08-07 14:55:42 -050070 method.append(REBOOTCOUNTER_INTERFACE, "AttemptsLeft");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050071 auto reply = bus.call(method);
72 if (reply.is_method_error())
73 {
74 log<level::ERR>("Error in BOOTCOUNT getValue");
75 // TODO openbmc/openbmc#851 - Once available, throw returned error
76 throw std::runtime_error("ERROR in reading BOOTCOUNT");
77 }
Patrick Williams883a59d2020-05-13 17:51:12 -050078 std::variant<uint32_t> rebootCount;
Andrew Geissler2548c7a2017-05-18 15:35:40 -050079 reply.read(rebootCount);
80
Patrick Williams72aeb922020-05-13 11:32:41 -050081 return std::get<uint32_t>(rebootCount);
Andrew Geissler2548c7a2017-05-18 15:35:40 -050082}