blob: 253a2dcd7823b82cd996f9e7c00ac8a15f9293e5 [file] [log] [blame]
Tom Josephf8da32a2016-12-06 16:56:04 +05301#include "guid.hpp"
2
William A. Kennington III4f09eae2019-02-12 17:10:35 -08003#include <ipmid/api.h>
Vernon Mauery9e801a22018-10-12 13:20:49 -07004#include <mapper.h>
5
George Liu7b7f25f2022-07-04 17:07:32 +08006#include <phosphor-logging/lg2.hpp>
George Liubc8958f2022-07-04 09:29:49 +08007
Tom Josephf8da32a2016-12-06 16:56:04 +05308#include <sstream>
9#include <string>
10
Vernon Maueryfc37e592018-12-19 14:55:15 -080011using namespace phosphor::logging;
12
Tom Joseph83029cb2017-09-01 16:37:31 +053013namespace cache
14{
15
16command::Guid guid;
17
18} // namespace cache
19
Tom Josephf8da32a2016-12-06 16:56:04 +053020namespace command
21{
22
Tom Joseph83029cb2017-09-01 16:37:31 +053023std::unique_ptr<sdbusplus::bus::match_t> matchPtr(nullptr);
24
25static constexpr auto guidObjPath = "/org/openbmc/control/chassis0";
26static constexpr auto propInterface = "org.freedesktop.DBus.Properties";
27
28Guid getSystemGUID()
Tom Josephf8da32a2016-12-06 16:56:04 +053029{
30 // Canned System GUID for QEMU where the Chassis DBUS object is not
31 // populated
Vernon Mauery9e801a22018-10-12 13:20:49 -070032 Guid guid = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
33 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10};
Tom Josephf8da32a2016-12-06 16:56:04 +053034
Tom Josephf8da32a2016-12-06 16:56:04 +053035 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();
Tom Josephf8da32a2016-12-06 16:56:04 +053040 char* uuid = nullptr;
41 char* busname = nullptr;
42
43 do
44 {
Patrick Venturea65e30d2018-10-26 17:55:08 -070045 int rc = mapper_get_service(bus, guidObjPath, &busname);
Tom Josephf8da32a2016-12-06 16:56:04 +053046 if (rc < 0)
47 {
George Liu7b7f25f2022-07-04 17:07:32 +080048 lg2::error("Failed to get bus name, path: {PATH}, error: {ERROR}",
49 "PATH", guidObjPath, "ERROR", strerror(-rc));
Tom Josephf8da32a2016-12-06 16:56:04 +053050 break;
51 }
52
Tom Joseph83029cb2017-09-01 16:37:31 +053053 rc = sd_bus_call_method(bus, busname, guidObjPath, propInterface, "Get",
54 &error, &reply, "ss", chassisIntf, "uuid");
Tom Josephf8da32a2016-12-06 16:56:04 +053055 if (rc < 0)
56 {
George Liu7b7f25f2022-07-04 17:07:32 +080057 lg2::error("Failed to call Get Method: {ERROR}", "ERROR",
58 strerror(-rc));
Tom Josephf8da32a2016-12-06 16:56:04 +053059 break;
60 }
61
62 rc = sd_bus_message_read(reply, "v", "s", &uuid);
63 if (rc < 0 || uuid == NULL)
64 {
George Liu7b7f25f2022-07-04 17:07:32 +080065 lg2::error("Failed to get a response: {ERROR}", "ERROR",
66 strerror(-rc));
Tom Josephf8da32a2016-12-06 16:56:04 +053067 break;
68 }
69
70 std::string readUUID(uuid);
71 auto len = readUUID.length();
72
Vernon Mauery9e801a22018-10-12 13:20:49 -070073 for (size_t iter = 0, inc = 0; iter < len && inc < BMC_GUID_LEN;
74 iter += 2, inc++)
Tom Josephf8da32a2016-12-06 16:56:04 +053075 {
Vernon Mauery9e801a22018-10-12 13:20:49 -070076 uint8_t hexVal =
77 std::strtoul(readUUID.substr(iter, 2).c_str(), NULL, 16);
Tom Josephf8da32a2016-12-06 16:56:04 +053078 guid[inc] = hexVal;
79 }
Vernon Mauery9e801a22018-10-12 13:20:49 -070080 } while (0);
Tom Josephf8da32a2016-12-06 16:56:04 +053081
82 sd_bus_error_free(&error);
83 reply = sd_bus_message_unref(reply);
84 free(busname);
85
86 return guid;
87}
88
Tom Joseph83029cb2017-09-01 16:37:31 +053089void registerGUIDChangeCallback()
90{
Vernon Mauery9e801a22018-10-12 13:20:49 -070091 if (matchPtr == nullptr)
Tom Joseph83029cb2017-09-01 16:37:31 +053092 {
93 using namespace sdbusplus::bus::match::rules;
Patrick Williams0a590622022-07-22 19:26:53 -050094 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Tom Joseph83029cb2017-09-01 16:37:31 +053095
96 matchPtr = std::make_unique<sdbusplus::bus::match_t>(
97 bus,
Vernon Mauery9e801a22018-10-12 13:20:49 -070098 path_namespace(guidObjPath) + type::signal() +
99 member("PropertiesChanged") + interface(propInterface),
Patrick Williams0a590622022-07-22 19:26:53 -0500100 [](sdbusplus::message_t&) { cache::guid = getSystemGUID(); });
Tom Joseph83029cb2017-09-01 16:37:31 +0530101 }
102}
103
Tom Josephf8da32a2016-12-06 16:56:04 +0530104} // namespace command