Refactoring of Code to improve SAM score Patch-2 57/280557/4
authorAnuj Jain <anuj01.jain@samsung.com>
Wed, 31 Aug 2022 16:19:16 +0000 (21:49 +0530)
committerAnuj Jain <anuj01.jain@samsung.com>
Fri, 2 Sep 2022 12:16:20 +0000 (17:46 +0530)
Remove duplicated code:

- Add sub function for duplicated code.

Change-Id: I4907f1d86e0530500ddff2a6777e8ef7eab5ea9a
Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
bt-oal/bluez_hal/src/bt-hal-device-dbus-handler.c

index 4b6037b..de78989 100644 (file)
@@ -643,14 +643,12 @@ static void __bt_device_parse_services(GVariant *result)
        g_variant_iter_free(property_iter);
 }
 
-int __bt_hal_dbus_enquire_remote_device_services(char *address)
+int __bt_hal_dbus_get_all_remote_device_properties(char *address, GVariant **result)
 {
        char *device_path = NULL;
        GError *error = NULL;
        GDBusProxy *device_proxy;
        GDBusConnection *conn;
-       GVariant *result;
-
        device_path = _bt_hal_get_device_object_path(address);
        if (!device_path) {
                ERR("Device not paired");
@@ -677,7 +675,7 @@ int __bt_hal_dbus_enquire_remote_device_services(char *address)
                return BT_STATUS_FAIL;
        }
 
-       result = g_dbus_proxy_call_sync(device_proxy,
+       *result = g_dbus_proxy_call_sync(device_proxy,
                        "GetAll",
                        g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
                        G_DBUS_CALL_FLAGS_NONE,
@@ -685,7 +683,7 @@ int __bt_hal_dbus_enquire_remote_device_services(char *address)
                        NULL,
                        &error);
 
-       if (!result) {
+       if (!*result) {
                ERR("Error occured in Proxy call");
                if (error != NULL) {
                        ERR("Error occured in Proxy call (Error: %s)", error->message);
@@ -699,11 +697,21 @@ int __bt_hal_dbus_enquire_remote_device_services(char *address)
        g_object_unref(device_proxy);
        g_free(device_path);
 
-       /* Fetch Device Services and send to HAL User */
-       __bt_device_parse_services(result);
+       return BT_STATUS_SUCCESS;
+}
+
+int __bt_hal_dbus_enquire_remote_device_services(char *address)
+{
+       GVariant *result;
+       int ret;
+
+       ret =  __bt_hal_dbus_get_all_remote_device_properties(address, &result);
+       if (ret == BT_STATUS_SUCCESS)
+               /* Fetch Device Services and send to HAL User */
+               __bt_device_parse_services(result);
 
        DBG("-");
-       return BT_STATUS_SUCCESS;
+       return ret;
 }
 
 static void __bt_hal_bond_device_cb(GDBusProxy *proxy, GAsyncResult *res,
@@ -996,12 +1004,9 @@ done:
 
 int _bt_hal_dbus_get_remote_device_properties(bt_bdaddr_t *remote_addr)
 {
-       char *device_path = NULL;
        char address[BT_HAL_ADDRESS_STRING_SIZE] = { 0 };
-       GError *error = NULL;
-       GDBusProxy *device_proxy;
-       GDBusConnection *conn;
        GVariant *result;
+       int ret;
 
        if (!remote_addr) {
                ERR("Invalid device address ptr received");
@@ -1009,60 +1014,16 @@ int _bt_hal_dbus_get_remote_device_properties(bt_bdaddr_t *remote_addr)
        }
 
        _bt_hal_convert_addr_type_to_string(address, remote_addr->address);
-       device_path = _bt_hal_get_device_object_path(address);
-       if (!device_path) {
-               ERR("Device not paired");
-               return BT_STATUS_FAIL;
-       }
-
-       conn = _bt_hal_get_system_gconn();
-       if (!conn) {
-               g_free(device_path);
-               ERR("_bt_hal_get_system_gconn failed");
-               return BT_STATUS_FAIL;
-       }
-
-       device_proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
-                       NULL,
-                       BT_HAL_BLUEZ_NAME,
-                       device_path,
-                       BT_HAL_PROPERTIES_INTERFACE,
-                       NULL, NULL);
-
-       if (!device_proxy) {
-               ERR("Error creating device_proxy");
-               g_free(device_path);
-               return BT_STATUS_FAIL;
-       }
-
-       result = g_dbus_proxy_call_sync(device_proxy,
-                       "GetAll",
-                       g_variant_new("(s)", BT_HAL_DEVICE_INTERFACE),
-                       G_DBUS_CALL_FLAGS_NONE,
-                       -1,
-                       NULL,
-                       &error);
-
-       if (!result) {
-               ERR("Error occured in Proxy call");
-               if (error != NULL) {
-                       ERR("Error occured in Proxy call (Error: %s)", error->message);
-                       g_clear_error(&error);
-               }
-               g_object_unref(device_proxy);
-               g_free(device_path);
-               return BT_STATUS_FAIL;
-       }
+       ret =  __bt_hal_dbus_get_all_remote_device_properties(address, &result);
 
-       g_object_unref(device_proxy);
-       g_free(device_path);
-       /*
-        * As we need to provide async callback to user from HAL, simply schedule a
-        * callback method which will carry actual result
-        */
-       g_idle_add(__bt_device_bonded_device_info_cb, (gpointer)result);
+       if (ret == BT_STATUS_SUCCESS)
+               /*
+                * As we need to provide async callback to user from HAL, simply schedule a
+                * callback method which will carry actual result
+                */
+               g_idle_add(__bt_device_bonded_device_info_cb, (gpointer)result);
 
-       return BT_STATUS_SUCCESS;
+       return ret;
 }
 
 static int __bt_hal_dbus_set_remote_device_alias(bt_bdaddr_t *remote_addr, char *alias)