Refactoring of Code to improve SAM score Patch-5 60/280560/2
authorAnuj Jain <anuj01.jain@samsung.com>
Wed, 31 Aug 2022 16:34:35 +0000 (22:04 +0530)
committerAnuj Jain <anuj01.jain@samsung.com>
Fri, 2 Sep 2022 12:51:48 +0000 (18:21 +0530)
Remove Duplicate code:

- Move duplicate functions to common accessible file.
- Implement new function in common file for duplicate code segment.

Change-Id: Id2e205e587a21e195aba7474b9165f8ac36b06b7
Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
bt-api/bt-common.c
bt-api/bt-l2cap-le-client.c
bt-api/bt-rfcomm-client.c
bt-api/include/bt-common.h

index 5132176..b4ace2a 100644 (file)
@@ -2626,3 +2626,97 @@ BT_EXPORT_API int bluetooth_unregister_callback(void)
 
        return BLUETOOTH_ERROR_NONE;
 }
+
+static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
+{
+       char *data;
+       int size;
+
+       size = g_variant_get_size(var);
+       if (size > 0) {
+               data = (char *)g_variant_get_data(var);
+               if (data)
+                        param = g_array_append_vals(param, data, size);
+       }
+}
+
+/* TODO_40 : 4.0 merge  */
+/* Don't use this function directly. Instead of it, get the out parameter only */
+static void __bt_get_event_info(int service_function, GArray *output,
+               int *event, int *event_type, void **param_data)
+{
+       ret_if(event == NULL);
+
+       BT_INFO("service_function : %s (0x%x)",
+               _bt_convert_service_function_to_string(service_function),
+               service_function);
+       switch (service_function) {
+       case BT_L2CAP_LE_CLIENT_CONNECT:
+               *event_type = BT_L2CAP_LE_CLIENT_EVENT;
+               *event = BLUETOOTH_EVENT_L2CAP_LE_CONNECTED;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                       bluetooth_l2cap_le_connection_t, 0);
+               break;
+       case BT_RFCOMM_CLIENT_CONNECT:
+               *event_type = BT_RFCOMM_CLIENT_EVENT;
+               *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
+               ret_if(output == NULL);
+               *param_data = &g_array_index(output,
+                       bluetooth_rfcomm_connection_t, 0);
+               break;
+       default:
+               BT_ERR("Unknown function");
+               return;
+       }
+}
+
+void _bt_get_fd_list_info(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data, bluetooth_event_param_t *bt_event, GArray **out_param1,
+               int *event_type, GUnixFDList **out_fd_list, int *result, gboolean *fail)
+{
+       bt_req_info_t *cb_data = user_data;
+
+       GError *error = NULL;
+       GVariant *value;
+       GVariant *param1;
+
+       BT_DBG("+");
+
+       memset(bt_event, 0x00, sizeof(bluetooth_event_param_t));
+
+       value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, out_fd_list, res, &error);
+       if (value == NULL) {
+               if (error) {
+                       /* dBUS gives error cause */
+                       BT_ERR("D-Bus API failure: message[%s]",
+                                       error->message);
+                       g_clear_error(&error);
+               }
+               *result = BLUETOOTH_ERROR_TIMEOUT;
+
+               ret_if(cb_data == NULL);
+
+               __bt_get_event_info(cb_data->service_function, NULL,
+                               &bt_event->event, event_type,
+                               &bt_event->param_data);
+               *fail = true;
+               return;
+       }
+
+       g_variant_get(value, "(iv)", result, &param1);
+       g_variant_unref(value);
+
+       if (param1) {
+               *out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
+               __bt_fill_garray_from_variant(param1, *out_param1);
+               g_variant_unref(param1);
+       }
+
+       if (!cb_data)
+               return;
+
+       __bt_get_event_info(cb_data->service_function, *out_param1,
+                       &bt_event->event, event_type,
+                       &bt_event->param_data);
+       return;
+}
index 1632ab0..5f7b700 100644 (file)
@@ -277,96 +277,29 @@ static void __bt_l2cap_le_handle_new_client_connection(
        BT_DBG("-");
 }
 
-static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
-{
-       char *data;
-       int size;
-
-       size = g_variant_get_size(var);
-       if (size > 0) {
-               data = (char *)g_variant_get_data(var);
-               if (data)
-                       param = g_array_append_vals(param, data, size);
-
-       }
-}
-
-/* TODO_40 : 4.0 merge  */
-/* Don't use this function directly. Instead of it, get the out parameter only */
-static void __bt_get_event_info(int service_function, GArray *output,
-                       int *event, int *event_type, void **param_data)
-{
-       ret_if(event == NULL);
-
-       BT_INFO("service_function : %s (0x%x)",
-               _bt_convert_service_function_to_string(service_function),
-               service_function);
-       switch (service_function) {
-       case BT_L2CAP_LE_CLIENT_CONNECT:
-               *event_type = BT_L2CAP_LE_CLIENT_EVENT;
-               *event = BLUETOOTH_EVENT_L2CAP_LE_CONNECTED;
-               ret_if(output == NULL);
-               *param_data = &g_array_index(output,
-                               bluetooth_l2cap_le_connection_t, 0);
-               break;
-       default:
-               BT_ERR("Unknown function");
-               return;
-       }
-}
-
 static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy,
                                                GAsyncResult *res, gpointer user_data)
 {
        int result = BLUETOOTH_ERROR_NONE;
        int event_type = BT_LE_ADAPTER_EVENT;
-       bt_req_info_t *cb_data = user_data;
-       bluetooth_event_param_t bt_event;
+       gboolean fail = false;
 
-       GError *error = NULL;
-       GVariant *value;
-       GVariant *param1;
+       bt_req_info_t *cb_data = user_data;
+       bluetooth_event_param_t bt_event;       
        GArray *out_param1 = NULL;
        GUnixFDList *out_fd_list = NULL;
 
        BT_DBG("+");
 
-       memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
-
-       value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
-       if (value == NULL) {
-               if (error) {
-                       /* dBUS gives error cause */
-                       BT_ERR("D-Bus API failure: message[%s]",
-                                       error->message);
-                       g_clear_error(&error);
-               }
-               result = BLUETOOTH_ERROR_TIMEOUT;
-
-               ret_if(cb_data == NULL);
+       _bt_get_fd_list_info(proxy, res, user_data, &bt_event, &out_param1,
+               &event_type, &out_fd_list, &result, &fail);
 
-               __bt_get_event_info(cb_data->service_function, NULL,
-                               &bt_event.event, &event_type,
-                               &bt_event.param_data);
+       if (fail)
                goto failed;
-       }
-
-       g_variant_get(value, "(iv)", &result, &param1);
-       g_variant_unref(value);
-
-       if (param1) {
-               out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
-               __bt_fill_garray_from_variant(param1, out_param1);
-               g_variant_unref(param1);
-       }
 
        if (!cb_data)
                goto done;
 
-       __bt_get_event_info(cb_data->service_function, out_param1,
-                       &bt_event.event, &event_type,
-                       &bt_event.param_data);
-
        if (result == BLUETOOTH_ERROR_NONE && out_param1) {
                if (BT_L2CAP_LE_CLIENT_CONNECT == cb_data->service_function) {
                        int *fd_list_array;
index c362fd0..7a5d813 100644 (file)
@@ -901,97 +901,27 @@ static void __bt_rfcomm_handle_new_client_connection(bluetooth_rfcomm_connection
        BT_DBG("-");
 }
 
-static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
-{
-       char *data;
-       int size;
-
-       size = g_variant_get_size(var);
-       if (size > 0) {
-               data = (char *)g_variant_get_data(var);
-               if (data)
-                       param = g_array_append_vals(param, data, size);
-
-       }
-}
-
-
-/* TODO_40 : 4.0 merge  */
-/* Don't use this function directly. Instead of it, get the out parameter only */
-static void __bt_get_event_info(int service_function, GArray *output,
-                       int *event, int *event_type, void **param_data)
-{
-       ret_if(event == NULL);
-
-       BT_DBG("service_function : %s (0x%x)",
-               _bt_convert_service_function_to_string(service_function),
-               service_function);
-       switch (service_function) {
-       case BT_RFCOMM_CLIENT_CONNECT:
-               *event_type = BT_RFCOMM_CLIENT_EVENT;
-               *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
-               ret_if(output == NULL);
-               *param_data = &g_array_index(output,
-                               bluetooth_rfcomm_connection_t, 0);
-               break;
-       default:
-               BT_ERR("Unknown function");
-               return;
-       }
-}
-
-
 static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
 {
        int result = BLUETOOTH_ERROR_NONE;
        int event_type = BT_ADAPTER_EVENT;
+       gboolean fail = false;
+
        bt_req_info_t *cb_data = user_data;
        bluetooth_event_param_t bt_event;
 
-       GError *error = NULL;
-       GVariant *value;
-       GVariant *param1;
        GArray *out_param1 = NULL;
        GUnixFDList *out_fd_list = NULL;
 
-       BT_DBG("+");
-
-       memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
-
-       value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
-       if (value == NULL) {
-               if (error) {
-                       /* dBUS gives error cause */
-                       BT_ERR("D-Bus API failure: message[%s]",
-                                       error->message);
-                       g_clear_error(&error);
-               }
-               result = BLUETOOTH_ERROR_TIMEOUT;
-
-               ret_if(cb_data == NULL);
+       _bt_get_fd_list_info(proxy, res, user_data, &bt_event, &out_param1,
+                       &event_type, &out_fd_list, &result, &fail);
 
-               __bt_get_event_info(cb_data->service_function, NULL,
-                               &bt_event.event, &event_type,
-                               &bt_event.param_data);
+       if (fail)
                goto failed;
-       }
-
-       g_variant_get(value, "(iv)", &result, &param1);
-       g_variant_unref(value);
-
-       if (param1) {
-               out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
-               __bt_fill_garray_from_variant(param1, out_param1);
-               g_variant_unref(param1);
-       }
 
        if (!cb_data)
                goto done;
 
-       __bt_get_event_info(cb_data->service_function, out_param1,
-                       &bt_event.event, &event_type,
-                       &bt_event.param_data);
-
        if (result == BLUETOOTH_ERROR_NONE && out_param1) {
                if (BT_RFCOMM_CLIENT_CONNECT == cb_data->service_function) {
                        int *fd_list_array;
index 7d517f9..89fd05d 100644 (file)
@@ -419,6 +419,10 @@ void _bt_rfcomm_client_reset_timer(void);
 int _get_l2cap_le_server_id(int psm, gboolean *auto_accept);
 void _bt_l2cap_le_server_set_pending_conn(int server_id, char *address);
 
+void _bt_get_fd_list_info(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data,
+               bluetooth_event_param_t *bt_event, GArray **out_param1,
+               int *event_type, GUnixFDList **out_fd_list, int *result, gboolean *fail);
+
 void _bt_reset_battery_monitor_info(void);
 
 int _bt_hid_device_get_fd(const char *address, int *ctrl, int *intr);