Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 1 | #include "guid.hpp" |
| 2 | |
| 3 | #include <iostream> |
| 4 | #include <sstream> |
| 5 | #include <string> |
| 6 | |
| 7 | #include <host-ipmid/ipmid-api.h> |
| 8 | #include <mapper.h> |
| 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 |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [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(); |
| 37 | int rc = 0; |
| 38 | char* uuid = nullptr; |
| 39 | char* busname = nullptr; |
| 40 | |
| 41 | do |
| 42 | { |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 43 | rc = mapper_get_service(bus, guidObjPath, &busname); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 44 | if (rc < 0) |
| 45 | { |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 46 | std::cerr << "Failed to get " << guidObjPath << " bus name: " |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 47 | << strerror(-rc) << "\n"; |
| 48 | break; |
| 49 | } |
| 50 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 51 | rc = sd_bus_call_method(bus, busname, guidObjPath, propInterface, "Get", |
| 52 | &error, &reply, "ss", chassisIntf, "uuid"); |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 53 | if (rc < 0) |
| 54 | { |
| 55 | std::cerr << "Failed to call Get Method:" << strerror(-rc) << "\n"; |
| 56 | break; |
| 57 | } |
| 58 | |
| 59 | rc = sd_bus_message_read(reply, "v", "s", &uuid); |
| 60 | if (rc < 0 || uuid == NULL) |
| 61 | { |
| 62 | std::cerr << "Failed to get a response:" << strerror(-rc) << "\n"; |
| 63 | break; |
| 64 | } |
| 65 | |
| 66 | std::string readUUID(uuid); |
| 67 | auto len = readUUID.length(); |
| 68 | |
| 69 | for (size_t iter = 0, inc = 0; |
| 70 | iter < len && inc < BMC_GUID_LEN; iter += 2, inc++) |
| 71 | { |
| 72 | uint8_t hexVal = std::strtoul(readUUID.substr(iter, 2).c_str(), |
| 73 | NULL, 16); |
| 74 | guid[inc] = hexVal; |
| 75 | } |
| 76 | } |
| 77 | while (0); |
| 78 | |
| 79 | sd_bus_error_free(&error); |
| 80 | reply = sd_bus_message_unref(reply); |
| 81 | free(busname); |
| 82 | |
| 83 | return guid; |
| 84 | } |
| 85 | |
Tom Joseph | 83029cb | 2017-09-01 16:37:31 +0530 | [diff] [blame] | 86 | void registerGUIDChangeCallback() |
| 87 | { |
| 88 | if(matchPtr == nullptr) |
| 89 | { |
| 90 | using namespace sdbusplus::bus::match::rules; |
| 91 | sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()}; |
| 92 | |
| 93 | matchPtr = std::make_unique<sdbusplus::bus::match_t>( |
| 94 | bus, |
| 95 | path_namespace(guidObjPath) + |
| 96 | type::signal() + |
| 97 | member("PropertiesChanged") + |
| 98 | interface(propInterface), |
| 99 | [](sdbusplus::message::message&){cache::guid = getSystemGUID();}); |
| 100 | } |
| 101 | } |
| 102 | |
Tom Joseph | f8da32a | 2016-12-06 16:56:04 +0530 | [diff] [blame] | 103 | } // namespace command |