blob: 67b71e274b9a7638de87a78b1e13f946a73b6c73 [file] [log] [blame]
Tom Josephf8da32a2016-12-06 16:56:04 +05301#include "guid.hpp"
2
Vernon Mauery9e801a22018-10-12 13:20:49 -07003#include <host-ipmid/ipmid-api.h>
4#include <mapper.h>
5
Tom Josephf8da32a2016-12-06 16:56:04 +05306#include <iostream>
7#include <sstream>
8#include <string>
9
Tom Joseph83029cb2017-09-01 16:37:31 +053010namespace cache
11{
12
13command::Guid guid;
14
15} // namespace cache
16
Tom Josephf8da32a2016-12-06 16:56:04 +053017namespace command
18{
19
Tom Joseph83029cb2017-09-01 16:37:31 +053020std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr);
21
22static constexpr auto guidObjPath = "/org/openbmc/control/chassis0";
23static constexpr auto propInterface = "org.freedesktop.DBus.Properties";
24
25Guid getSystemGUID()
Tom Josephf8da32a2016-12-06 16:56:04 +053026{
27 // Canned System GUID for QEMU where the Chassis DBUS object is not
28 // populated
Vernon Mauery9e801a22018-10-12 13:20:49 -070029 Guid guid = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
30 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
Tom Josephf8da32a2016-12-06 16:56:04 +053031
Tom Josephf8da32a2016-12-06 16:56:04 +053032 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 Joseph83029cb2017-09-01 16:37:31 +053043 rc = mapper_get_service(bus, guidObjPath, &busname);
Tom Josephf8da32a2016-12-06 16:56:04 +053044 if (rc < 0)
45 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070046 std::cerr << "Failed to get " << guidObjPath
47 << " bus name: " << strerror(-rc) << "\n";
Tom Josephf8da32a2016-12-06 16:56:04 +053048 break;
49 }
50
Tom Joseph83029cb2017-09-01 16:37:31 +053051 rc = sd_bus_call_method(bus, busname, guidObjPath, propInterface, "Get",
52 &error, &reply, "ss", chassisIntf, "uuid");
Tom Josephf8da32a2016-12-06 16:56:04 +053053 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
Vernon Mauery9e801a22018-10-12 13:20:49 -070069 for (size_t iter = 0, inc = 0; iter < len && inc < BMC_GUID_LEN;
70 iter += 2, inc++)
Tom Josephf8da32a2016-12-06 16:56:04 +053071 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070072 uint8_t hexVal =
73 std::strtoul(readUUID.substr(iter, 2).c_str(), NULL, 16);
Tom Josephf8da32a2016-12-06 16:56:04 +053074 guid[inc] = hexVal;
75 }
Vernon Mauery9e801a22018-10-12 13:20:49 -070076 } while (0);
Tom Josephf8da32a2016-12-06 16:56:04 +053077
78 sd_bus_error_free(&error);
79 reply = sd_bus_message_unref(reply);
80 free(busname);
81
82 return guid;
83}
84
Tom Joseph83029cb2017-09-01 16:37:31 +053085void registerGUIDChangeCallback()
86{
Vernon Mauery9e801a22018-10-12 13:20:49 -070087 if (matchPtr == nullptr)
Tom Joseph83029cb2017-09-01 16:37:31 +053088 {
89 using namespace sdbusplus::bus::match::rules;
90 sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
91
92 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
93 bus,
Vernon Mauery9e801a22018-10-12 13:20:49 -070094 path_namespace(guidObjPath) + type::signal() +
95 member("PropertiesChanged") + interface(propInterface),
96 [](sdbusplus::message::message&) {
97 cache::guid = getSystemGUID();
98 });
Tom Joseph83029cb2017-09-01 16:37:31 +053099 }
100}
101
Tom Josephf8da32a2016-12-06 16:56:04 +0530102} // namespace command