Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 1 | #include <ext_interface.hpp> |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 2 | #include <phosphor-logging/log.hpp> |
| 3 | #include <sdbusplus/server.hpp> |
Brad Bishop | 5e5d445 | 2020-10-27 19:46:13 -0400 | [diff] [blame] | 4 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 5 | #include <string> |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 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 |
Andrew Geissler | bc7cdf4 | 2017-08-07 14:55:42 -0500 | [diff] [blame] | 13 | constexpr auto REBOOTCOUNTER_PATH("/xyz/openbmc_project/state/host0"); |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 14 | constexpr auto |
| 15 | REBOOTCOUNTER_INTERFACE("xyz.openbmc_project.Control.Boot.RebootAttempts"); |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 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 | **/ |
Patrick Williams | aaea686 | 2022-07-22 19:26:54 -0500 | [diff] [blame] | 31 | std::string getService(sdbusplus::bus_t& bus, const std::string& intf, |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 32 | const std::string& path) |
| 33 | { |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 34 | auto mapper = bus.new_method_call(MAPPER_BUSNAME, MAPPER_PATH, |
| 35 | MAPPER_INTERFACE, "GetObject"); |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 36 | |
| 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 Geissler | d8dd9b2 | 2017-08-19 13:44:44 -0500 | [diff] [blame] | 60 | uint32_t getBootCount() |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 61 | { |
| 62 | auto bus = sdbusplus::bus::new_default(); |
| 63 | |
Patrick Williams | 1e43be0 | 2024-08-16 15:20:32 -0400 | [diff] [blame] | 64 | auto rebootSvc = |
| 65 | getService(bus, REBOOTCOUNTER_INTERFACE, REBOOTCOUNTER_PATH); |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 66 | |
Patrick Venture | f78d904 | 2018-11-01 15:39:53 -0700 | [diff] [blame] | 67 | auto method = bus.new_method_call(rebootSvc.c_str(), REBOOTCOUNTER_PATH, |
| 68 | "org.freedesktop.DBus.Properties", "Get"); |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 69 | |
Andrew Geissler | bc7cdf4 | 2017-08-07 14:55:42 -0500 | [diff] [blame] | 70 | method.append(REBOOTCOUNTER_INTERFACE, "AttemptsLeft"); |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 71 | 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 Williams | 883a59d | 2020-05-13 17:51:12 -0500 | [diff] [blame] | 78 | std::variant<uint32_t> rebootCount; |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 79 | reply.read(rebootCount); |
| 80 | |
Patrick Williams | 72aeb92 | 2020-05-13 11:32:41 -0500 | [diff] [blame] | 81 | return std::get<uint32_t>(rebootCount); |
Andrew Geissler | 2548c7a | 2017-05-18 15:35:40 -0500 | [diff] [blame] | 82 | } |