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