1 #include "guid.hpp" 2 3 #include <ipmid/api.h> 4 #include <mapper.h> 5 6 #include <phosphor-logging/lg2.hpp> 7 8 #include <sstream> 9 #include <string> 10 11 using namespace phosphor::logging; 12 13 namespace cache 14 { 15 16 command::Guid guid; 17 18 } // namespace cache 19 20 namespace command 21 { 22 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() 29 { 30 // Canned System GUID for QEMU where the Chassis DBUS object is not 31 // populated 32 Guid guid = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 33 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10}; 34 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(); 40 char* uuid = nullptr; 41 char* busname = nullptr; 42 43 do 44 { 45 int rc = mapper_get_service(bus, guidObjPath, &busname); 46 if (rc < 0) 47 { 48 lg2::error("Failed to get bus name, path: {PATH}, error: {ERROR}", 49 "PATH", guidObjPath, "ERROR", strerror(-rc)); 50 break; 51 } 52 53 rc = sd_bus_call_method(bus, busname, guidObjPath, propInterface, "Get", 54 &error, &reply, "ss", chassisIntf, "uuid"); 55 if (rc < 0) 56 { 57 lg2::error("Failed to call Get Method: {ERROR}", "ERROR", 58 strerror(-rc)); 59 break; 60 } 61 62 rc = sd_bus_message_read(reply, "v", "s", &uuid); 63 if (rc < 0 || uuid == NULL) 64 { 65 lg2::error("Failed to get a response: {ERROR}", "ERROR", 66 strerror(-rc)); 67 break; 68 } 69 70 std::string readUUID(uuid); 71 auto len = readUUID.length(); 72 73 for (size_t iter = 0, inc = 0; iter < len && inc < BMC_GUID_LEN; 74 iter += 2, inc++) 75 { 76 uint8_t hexVal = 77 std::strtoul(readUUID.substr(iter, 2).c_str(), NULL, 16); 78 guid[inc] = hexVal; 79 } 80 } while (0); 81 82 sd_bus_error_free(&error); 83 reply = sd_bus_message_unref(reply); 84 free(busname); 85 86 return guid; 87 } 88 89 void registerGUIDChangeCallback() 90 { 91 if (matchPtr == nullptr) 92 { 93 using namespace sdbusplus::bus::match::rules; 94 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; 95 96 matchPtr = std::make_unique<sdbusplus::bus::match_t>( 97 bus, 98 path_namespace(guidObjPath) + type::signal() + 99 member("PropertiesChanged") + interface(propInterface), 100 [](sdbusplus::message_t&) { cache::guid = getSystemGUID(); }); 101 } 102 } 103 104 } // namespace command 105