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