blob: 1e25b2daea1eb3e9703ac2abeb8fc417666a0164 [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 Williams1bcde492025-02-01 08:22:16 -050014constexpr auto REBOOTCOUNTER_INTERFACE(
15 "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 Williamsaaea6862022-07-22 19:26:54 -050031std::string getService(sdbusplus::bus_t& bus, const std::string& intf,
Andrew Geissler2548c7a2017-05-18 15:35:40 -050032 const std::string& path)
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
Andrew Geissler2548c7a2017-05-18 15:35:40 -050042 std::map<std::string, std::vector<std::string>> mapperResponse;
43 mapperResponseMsg.read(mapperResponse);
44
45 if (mapperResponse.empty())
46 {
47 // TODO openbmc/openbmc#1712 - Handle empty mapper resp. consistently
48 throw std::runtime_error("ERROR in reading the mapper response");
49 }
50
51 return mapperResponse.begin()->first;
52}
53
Andrew Geisslerd8dd9b22017-08-19 13:44:44 -050054uint32_t getBootCount()
Andrew Geissler2548c7a2017-05-18 15:35:40 -050055{
56 auto bus = sdbusplus::bus::new_default();
57
Patrick Williams1e43be02024-08-16 15:20:32 -040058 auto rebootSvc =
59 getService(bus, REBOOTCOUNTER_INTERFACE, REBOOTCOUNTER_PATH);
Andrew Geissler2548c7a2017-05-18 15:35:40 -050060
Patrick Venturef78d9042018-11-01 15:39:53 -070061 auto method = bus.new_method_call(rebootSvc.c_str(), REBOOTCOUNTER_PATH,
62 "org.freedesktop.DBus.Properties", "Get");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050063
Andrew Geisslerbc7cdf42017-08-07 14:55:42 -050064 method.append(REBOOTCOUNTER_INTERFACE, "AttemptsLeft");
Andrew Geissler2548c7a2017-05-18 15:35:40 -050065 auto reply = bus.call(method);
Patrick Williams2bf01cc2025-11-04 22:23:25 -050066
Patrick Williams883a59d2020-05-13 17:51:12 -050067 std::variant<uint32_t> rebootCount;
Andrew Geissler2548c7a2017-05-18 15:35:40 -050068 reply.read(rebootCount);
69
Patrick Williams72aeb922020-05-13 11:32:41 -050070 return std::get<uint32_t>(rebootCount);
Andrew Geissler2548c7a2017-05-18 15:35:40 -050071}