Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 1 | #include "guid.hpp" |
| 2 | |
William A. Kennington III | 4f09eae | 2019-02-12 17:10:35 -0800 | [diff] [blame] | 3 | #include <ipmid/api.h> |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 4 | #include <mapper.h> |
| 5 | |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 6 | #include <phosphor-logging/log.hpp> |
George Liu | bc8958f | 2022-07-04 09:29:49 +0800 | [diff] [blame^] | 7 | |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 8 | #include <sstream> |
| 9 | #include <string> |
| 10 | |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 11 | using namespace phosphor::logging; |
| 12 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 13 | namespace cache |
| 14 | { |
| 15 | |
| 16 | command::Guid guid; |
| 17 | |
| 18 | } // namespace cache |
| 19 | |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 20 | namespace command |
| 21 | { |
| 22 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 23 | std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr); |
| 24 | |
| 25 | static constexpr auto guidObjPath = "/org/openbmc/control/chassis0"; |
| 26 | static constexpr auto propInterface = "org.freedesktop.DBus.Properties"; |
| 27 | |
| 28 | Guid getSystemGUID() |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 29 | { |
| 30 | // Canned System GUID for QEMU where the Chassis DBUS object is not |
| 31 | // populated |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 32 | Guid guid = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 33 | 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}; |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 34 | |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 35 | constexpr auto chassisIntf = "org.openbmc.control.Chassis"; |
| 36 | |
| 37 | sd_bus_message* reply = nullptr; |
| 38 | sd_bus_error error = SD_BUS_ERROR_NULL; |
| 39 | sd_bus* bus = ipmid_get_sd_bus_connection(); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 40 | char* uuid = nullptr; |
| 41 | char* busname = nullptr; |
| 42 | |
| 43 | do |
| 44 | { |
Patrick Venture | a65e30d | 2018-10-26 17:55:08 -0700 | [diff] [blame] | 45 | int rc = mapper_get_service(bus, guidObjPath, &busname); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 46 | if (rc < 0) |
| 47 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 48 | log<level::ERR>("Failed to get bus name", |
| 49 | entry("PATH=%s", guidObjPath), |
| 50 | entry("ERROR=%s", strerror(-rc))); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 51 | break; |
| 52 | } |
| 53 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 54 | rc = sd_bus_call_method(bus, busname, guidObjPath, propInterface, "Get", |
| 55 | &error, &reply, "ss", chassisIntf, "uuid"); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 56 | if (rc < 0) |
| 57 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 58 | log<level::ERR>("Failed to call Get Method", |
| 59 | entry("ERROR=%s", strerror(-rc))); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 60 | break; |
| 61 | } |
| 62 | |
| 63 | rc = sd_bus_message_read(reply, "v", "s", &uuid); |
| 64 | if (rc < 0 || uuid == NULL) |
| 65 | { |
Vernon Mauery | fc37e59 | 2018-12-19 14:55:15 -0800 | [diff] [blame] | 66 | log<level::ERR>("Failed to get a response", |
| 67 | entry("ERROR=%s", strerror(-rc))); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 68 | break; |
| 69 | } |
| 70 | |
| 71 | std::string readUUID(uuid); |
| 72 | auto len = readUUID.length(); |
| 73 | |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 74 | for (size_t iter = 0, inc = 0; iter < len && inc < BMC_GUID_LEN; |
| 75 | iter += 2, inc++) |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 76 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 77 | uint8_t hexVal = |
| 78 | std::strtoul(readUUID.substr(iter, 2).c_str(), NULL, 16); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 79 | guid[inc] = hexVal; |
| 80 | } |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 81 | } while (0); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 82 | |
| 83 | sd_bus_error_free(&error); |
| 84 | reply = sd_bus_message_unref(reply); |
| 85 | free(busname); |
| 86 | |
| 87 | return guid; |
| 88 | } |
| 89 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 90 | void registerGUIDChangeCallback() |
| 91 | { |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 92 | if (matchPtr == nullptr) |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 93 | { |
| 94 | using namespace sdbusplus::bus::match::rules; |
| 95 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 96 | |
| 97 | matchPtr = std::make_unique<sdbusplus::bus::match_t>( |
| 98 | bus, |
Vernon Mauery | 9e801a2 | 2018-10-12 13:20:49 -0700 | [diff] [blame] | 99 | path_namespace(guidObjPath) + type::signal() + |
| 100 | member("PropertiesChanged") + interface(propInterface), |
| 101 | [](sdbusplus::message::message&) { |
| 102 | cache::guid = getSystemGUID(); |
| 103 | }); |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 107 | } // namespace command |