rewrite Get System GUID to use new provider API

Modify to use the new provider API. One by one, change calls to
remove any legacy API constructs.

Tested-by: ipmitool raw 6 0x37
            08 50 f3 d6 73 d5 98 b4 45 49 9a a6 24 76 f5 82
           This is the same as the BMC web value as shown by:
	   curl https://<bmcip>/redfish/v1/Systems/system
	       "UUID": "82f57624-a69a-4945-b498-d573d6f35008"

Change-Id: Ic3ea8c53266d5be07bc957fe7076b208b621f00b
Signed-off-by: Vernon Mauery <vernon.mauery@linux.intel.com>
diff --git a/apphandler.cpp b/apphandler.cpp
index 929e922..09b1c2f 100644
--- a/apphandler.cpp
+++ b/apphandler.cpp
@@ -42,10 +42,6 @@
 
 constexpr auto bmc_state_interface = "xyz.openbmc_project.State.BMC";
 constexpr auto bmc_state_property = "CurrentBMCState";
-constexpr auto bmc_interface = "xyz.openbmc_project.Inventory.Item.Bmc";
-constexpr auto bmc_guid_interface = "xyz.openbmc_project.Common.UUID";
-constexpr auto bmc_guid_property = "UUID";
-constexpr auto bmc_guid_len = 16;
 
 static constexpr auto redundancyIntf =
     "xyz.openbmc_project.Software.RedundancyPriority";
@@ -799,71 +795,49 @@
                                  outputBufferSize, transactionTime, nrRetries);
 }
 
-ipmi_ret_t ipmi_app_get_sys_guid(ipmi_netfn_t netfn, ipmi_cmd_t cmd,
-                                 ipmi_request_t request,
-                                 ipmi_response_t response,
-                                 ipmi_data_len_t data_len,
-                                 ipmi_context_t context)
-
+auto ipmiAppGetSystemGuid() -> ipmi::RspType<std::array<uint8_t, 16>>
 {
-    ipmi_ret_t rc = IPMI_CC_OK;
-    sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
+    static constexpr auto bmcInterface =
+        "xyz.openbmc_project.Inventory.Item.Bmc";
+    static constexpr auto uuidInterface = "xyz.openbmc_project.Common.UUID";
+    static constexpr auto uuidProperty = "UUID";
 
+    ipmi::Value propValue;
     try
     {
         // Get the Inventory object implementing BMC interface
-        ipmi::DbusObjectInfo bmcObject =
-            ipmi::getDbusObject(bus, bmc_interface);
+        auto busPtr = getSdBus();
+        auto objectInfo = ipmi::getDbusObject(*busPtr, bmcInterface);
 
         // Read UUID property value from bmcObject
         // UUID is in RFC4122 format Ex: 61a39523-78f2-11e5-9862-e6402cfc3223
-        auto variant =
-            ipmi::getDbusProperty(bus, bmcObject.second, bmcObject.first,
-                                  bmc_guid_interface, bmc_guid_property);
-        std::string guidProp = std::get<std::string>(variant);
-
-        // Erase "-" characters from the property value
-        guidProp.erase(std::remove(guidProp.begin(), guidProp.end(), '-'),
-                       guidProp.end());
-
-        auto guidPropLen = guidProp.length();
-        // Validate UUID data
-        // Divide by 2 as 1 byte is built from 2 chars
-        if ((guidPropLen <= 0) || ((guidPropLen / 2) != bmc_guid_len))
-
-        {
-            log<level::ERR>("Invalid UUID property value",
-                            entry("UUID_LENGTH=%d", guidPropLen));
-            return IPMI_CC_RESPONSE_ERROR;
-        }
-
-        // Convert data in RFC4122(MSB) format to LSB format
-        // Get 2 characters at a time as 1 byte is built from 2 chars and
-        // convert to hex byte
-        // TODO: Data printed for GUID command is not as per the
-        // GUID format defined in IPMI specification 2.0 section 20.8
-        // Ticket raised: https://sourceforge.net/p/ipmitool/bugs/501/
-        uint8_t respGuid[bmc_guid_len];
-        for (size_t i = 0, respLoc = (bmc_guid_len - 1);
-             i < guidPropLen && respLoc >= 0; i += 2, respLoc--)
-        {
-            auto value = static_cast<uint8_t>(
-                std::stoi(guidProp.substr(i, 2).c_str(), NULL, 16));
-            respGuid[respLoc] = value;
-        }
-
-        *data_len = bmc_guid_len;
-        std::memcpy(response, &respGuid, bmc_guid_len);
+        propValue =
+            ipmi::getDbusProperty(*busPtr, objectInfo.second, objectInfo.first,
+                                  uuidInterface, uuidProperty);
     }
     catch (const InternalFailure& e)
     {
         log<level::ERR>("Failed in reading BMC UUID property",
-                        entry("INTERFACE=%s", bmc_interface),
-                        entry("PROPERTY_INTERFACE=%s", bmc_guid_interface),
-                        entry("PROPERTY=%s", bmc_guid_property));
-        return IPMI_CC_UNSPECIFIED_ERROR;
+                        entry("INTERFACE=%s", uuidInterface),
+                        entry("PROPERTY=%s", uuidProperty));
+        return ipmi::responseUnspecifiedError();
     }
-    return rc;
+    std::array<uint8_t, 16> uuid;
+    std::string rfc4122Uuid = std::get<std::string>(propValue);
+    try
+    {
+        // convert to IPMI format
+        uuid = rfc4122ToIpmi(rfc4122Uuid);
+    }
+    catch (const InvalidArgument& e)
+    {
+        log<level::ERR>("Failed in parsing BMC UUID property",
+                        entry("INTERFACE=%s", uuidInterface),
+                        entry("PROPERTY=%s", uuidProperty),
+                        entry("VALUE=%s", rfc4122Uuid.c_str()));
+        return ipmi::responseUnspecifiedError();
+    }
+    return ipmi::responseSuccess(uuid);
 }
 
 static std::unique_ptr<SysInfoParamStore> sysInfoParamStore;
@@ -1333,8 +1307,9 @@
     }
 
     // <Get System GUID Command>
-    ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_SYS_GUID, NULL,
-                           ipmi_app_get_sys_guid, PRIVILEGE_USER);
+    ipmi::registerHandler(ipmi::prioOpenBmcBase, ipmi::netFnApp,
+                          ipmi::app::cmdGetSystemGuid, ipmi::Privilege::User,
+                          ipmiAppGetSystemGuid);
 
     // <Get Channel Cipher Suites Command>
     ipmi_register_callback(NETFUN_APP, IPMI_CMD_GET_CHAN_CIPHER_SUITES, NULL,