blob: 8e6db179b6fdc7c9da9ec49d444d8f25c74dc6fc [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>
Brad Bishop5e5d4452020-10-27 19:46:13 -04004
Patrick Venturef78d9042018-11-01 15:39:53 -07005#include <string>
Andrew Geissler2548c7a2017-05-18 15:35:40 -05006
7// Mapper
8constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
9constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
10constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
11
12// Reboot count
Andrew Geisslerbc7cdf42017-08-07 14:55:42 -050013constexpr auto REBOOTCOUNTER_PATH("/xyz/openbmc_project/state/host0");
Patrick Venturef78d9042018-11-01 15:39:53 -070014constexpr auto
15 REBOOTCOUNTER_INTERFACE("xyz.openbmc_project.Control.Boot.RebootAttempts");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050016
17using 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 **/
Patrick Venturef78d9042018-11-01 15:39:53 -070031std::string getService(sdbusplus::bus::bus& bus, const std::string& intf,
Andrew Geissler2548c7a2017-05-18 15:35:40 -050032 const std::string& path)
33{
34
Patrick Venturef78d9042018-11-01 15:39:53 -070035 auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH,
36 MAPPER_INTERFACE, "GetObject");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050037
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
Andrew Geisslerd8dd9b22017-08-19 13:44:44 -050061uint32_t getBootCount()
Andrew Geissler2548c7a2017-05-18 15:35:40 -050062{
63 auto bus = sdbusplus::bus::new_default();
64
Patrick Venturef78d9042018-11-01 15:39:53 -070065 auto rebootSvc =
66 getService(bus, REBOOTCOUNTER_INTERFACE, REBOOTCOUNTER_PATH);
Andrew Geissler2548c7a2017-05-18 15:35:40 -050067
Patrick Venturef78d9042018-11-01 15:39:53 -070068 auto method = bus.new_method_call(rebootSvc.c_str(), REBOOTCOUNTER_PATH,
69 "org.freedesktop.DBus.Properties", "Get");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050070
Andrew Geisslerbc7cdf42017-08-07 14:55:42 -050071 method.append(REBOOTCOUNTER_INTERFACE, "AttemptsLeft");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050072 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 }
Patrick Williams883a59d2020-05-13 17:51:12 -050079 std::variant<uint32_t> rebootCount;
Andrew Geissler2548c7a2017-05-18 15:35:40 -050080 reply.read(rebootCount);
81
Patrick Williams72aeb922020-05-13 11:32:41 -050082 return std::get<uint32_t>(rebootCount);
Andrew Geissler2548c7a2017-05-18 15:35:40 -050083}