Remove use of legacy bmc control interface

Tested:
Tested using below given command for cold/warm resets
>ipmitool mc reset [ warm | cold ] -I dbus

Resolves openbmc/openbmc#2919

Change-Id: I15fc5ab53b7d8b2b17bc9fa8f3f2030e93bd0483
Signed-off-by: Nagaraju Goruganti <ngorugan@in.ibm.com>
diff --git a/globalhandler.cpp b/globalhandler.cpp
index 304867c..32f2bc6 100644
--- a/globalhandler.cpp
+++ b/globalhandler.cpp
@@ -1,89 +1,50 @@
 #include "globalhandler.h"
 #include "host-ipmid/ipmid-api.h"
 #include <stdio.h>
-#include <string.h>
-#include <stdint.h>
-#include <mapper.h>
+#include <string>
+#include <utils.hpp>
+#include <phosphor-logging/log.hpp>
+#include <phosphor-logging/elog-errors.hpp>
+#include "xyz/openbmc_project/Common/error.hpp"
+#include "xyz/openbmc_project/State/BMC/server.hpp"
 
-const char  *control_object_name  =  "/org/openbmc/control/bmc0";
-const char  *control_intf_name    =  "org.openbmc.control.Bmc";
+static constexpr auto bmcStateRoot = "/xyz/openbmc_project/state";
+static constexpr auto bmcStateIntf = "xyz.openbmc_project.State.BMC";
+static constexpr auto reqTransition = "RequestedBMCTransition";
+static constexpr auto match = "bmc0";
+
+using namespace phosphor::logging;
+using BMC = sdbusplus::xyz::openbmc_project::State::server::BMC;
 
 void register_netfn_global_functions() __attribute__((constructor));
 
-int dbus_reset(const char *method)
+void resetBMC()
 {
-    sd_bus_error error = SD_BUS_ERROR_NULL;
-    sd_bus_message *m = NULL;
-    sd_bus *bus = NULL;
-    char* connection = NULL;
-    int r;
+    sdbusplus::bus::bus bus{ipmid_get_sd_bus_connection()};
 
-    bus = ipmid_get_sd_bus_connection();
-    r = mapper_get_service(bus, control_object_name, &connection);
-    if (r < 0) {
-        fprintf(stderr, "Failed to get connection for %s: %s\n",
-                control_object_name, strerror(-r));
-        goto finish;
-    }
+    auto bmcStateObj = ipmi::getDbusObject(bus, bmcStateIntf, bmcStateRoot,
+                                           match);
 
-    printf("connection: %s\n", connection);
+    auto service = ipmi::getService(bus, bmcStateIntf, bmcStateObj.first);
 
-    // Open the system bus where most system services are provided.
-    bus = ipmid_get_sd_bus_connection();
-    
-    /*
-     * Bus, service, object path, interface and method are provided to call
-     * the method.
-     * Signatures and input arguments are provided by the arguments at the
-     * end.
-     */
-    r = sd_bus_call_method(bus,
-            connection,                                /* service to contact */
-            control_object_name,                       /* object path */
-            control_intf_name,                         /* interface name */
-            method,                               /* method name */
-            &error,                                    /* object to return error in */
-            &m,                                        /* return message on success */
-            NULL,
-            NULL
-            );
-
-    if (r < 0) {
-        fprintf(stderr, "Failed to issue method call: %s\n", error.message);
-        goto finish;
-    }
-
-finish:
-    sd_bus_error_free(&error);
-    sd_bus_message_unref(m);
-    free(connection);
-
-    return r;
+    ipmi::setDbusProperty(bus, service, bmcStateObj.first, bmcStateIntf,
+                    reqTransition, convertForMessage(BMC::Transition::Reboot));
 }
 
-ipmi_ret_t ipmi_global_warm_reset(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)
+
+ipmi_ret_t ipmi_global_reset(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)
 {
-    printf("Handling GLOBAL warmReset Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
-
-    // TODO: call the correct dbus method for warmReset.
-    dbus_reset("warmReset");
-
-    // Status code.
-    ipmi_ret_t rc = IPMI_CC_OK;
-    *data_len = 0;
-    return rc;
-}
-
-ipmi_ret_t ipmi_global_cold_reset(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)
-{
-    printf("Handling GLOBAL coldReset Netfn:[0x%X], Cmd:[0x%X]\n",netfn, cmd);
-
-    // TODO: call the correct dbus method for coldReset.
-    dbus_reset("coldReset");
+    try
+    {
+        resetBMC();
+    }
+    catch (std::exception& e)
+    {
+        log<level::ERR>(e.what());
+        return IPMI_CC_UNSPECIFIED_ERROR;
+    }
 
     // Status code.
     ipmi_ret_t rc = IPMI_CC_OK;
@@ -94,14 +55,12 @@
 void register_netfn_global_functions()
 {
     // Cold Reset
-    printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_COLD_RESET);
-    ipmi_register_callback(NETFUN_APP, IPMI_CMD_COLD_RESET, NULL, ipmi_global_cold_reset,
-                           PRIVILEGE_ADMIN);
+    ipmi_register_callback(NETFUN_APP, IPMI_CMD_COLD_RESET, NULL,
+                           ipmi_global_reset, PRIVILEGE_ADMIN);
 
     // <Warm Reset>
-    printf("Registering NetFn:[0x%X], Cmd:[0x%X]\n",NETFUN_APP, IPMI_CMD_WARM_RESET);
-    ipmi_register_callback(NETFUN_APP, IPMI_CMD_WARM_RESET, NULL, ipmi_global_warm_reset,
-                           PRIVILEGE_ADMIN);
+    ipmi_register_callback(NETFUN_APP, IPMI_CMD_WARM_RESET, NULL,
+                           ipmi_global_reset, PRIVILEGE_ADMIN);
 
     return;
 }