Refactoring of Code to improve SAM score Patch-4 59/280559/2
authorAnuj Jain <anuj01.jain@samsung.com>
Wed, 31 Aug 2022 16:26:37 +0000 (21:56 +0530)
committerAnuj Jain <anuj01.jain@samsung.com>
Fri, 2 Sep 2022 12:34:34 +0000 (18:04 +0530)
Remove duplicated code:

- Create new common file and move duplicate function to it.

Change-Id: I715b5806174d05a3febb58752519ee8681c23e7b
Signed-off-by: Anuj Jain <anuj01.jain@samsung.com>
bt-oal/bluez_hal/CMakeLists.txt
bt-oal/bluez_hal/src/bt-hal-l2cap-le-dbus-handler.c
bt-oal/bluez_hal/src/bt-hal-rfcomm-dbus-handler.c
bt-oal/bluez_hal/src/bt-hal-socket-common.c [new file with mode: 0644]
bt-oal/bluez_hal/src/bt-hal-socket-common.h [new file with mode: 0644]

index 56d1833..dd35af6 100644 (file)
@@ -40,6 +40,7 @@ SET(SRCS
 ./src/bt-hal-tds.c
 ./src/bt-hal-tds-dbus-handler.c
 ./src/bt-hal-l2cap-le-dbus-handler.c
+./src/bt-hal-socket-common.c
 )
 
 SET(PREFIX ${CMAKE_INSTALL_PREFIX})
index 4c7667f..ca3ff15 100644 (file)
@@ -30,6 +30,7 @@
 
 #include "bt-hal-l2cap-le-dbus-handler.h"
 #include "bt-hal-dbus-common-utils.h"
+#include "bt-hal-socket-common.h"
 
 #define BT_HAL_L2CAP_LE_ID_MAX 245
 #define BT_HAL_L2CAP_LE_MAX_BUFFER_SIZE 1024
@@ -593,46 +594,6 @@ static l2cap_le_server_data_t *__find_l2cap_le_server_info_from_psm(int psm)
        return NULL;
 }
 
-static int __send_sock_fd(int sock_fd, const void *buf, int size, int send_fd)
-{
-       ssize_t ret;
-       struct msghdr msg;
-       struct cmsghdr *cmsg;
-       struct iovec iov;
-       char cmsg_buf[CMSG_SPACE(sizeof(int))];
-       char err_msg[256] = {0, };
-
-       if (sock_fd == -1 || send_fd == -1)
-               return -1;
-
-       memset(&msg, 0, sizeof(msg));
-       memset(cmsg_buf, 0, sizeof(cmsg_buf));
-
-       msg.msg_control = cmsg_buf;
-       msg.msg_controllen = sizeof(cmsg_buf);
-
-       cmsg = CMSG_FIRSTHDR(&msg);
-       cmsg->cmsg_level = SOL_SOCKET;
-       cmsg->cmsg_type = SCM_RIGHTS;
-       cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
-
-       memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
-
-       iov.iov_base = (unsigned char *) buf;
-       iov.iov_len = size;
-
-       msg.msg_iov = &iov;
-       msg.msg_iovlen = 1;
-
-       ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
-       if (ret < 0) {
-               strerror_r(errno, err_msg, sizeof(err_msg));
-               ERR("sendmsg(): sock_fd %d send_fd %d: %s",
-                               sock_fd, send_fd, err_msg);
-       }
-
-       return ret;
-}
 
 static int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
 {
@@ -658,7 +619,7 @@ static int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
        ev.size = sizeof(ev);
        memcpy(ev.bdaddr, addr->address, 6);
        ev.status = BT_STATUS_SUCCESS;
-       ret = __send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
+       ret = _bt_hal_send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
        if (ret < 0) {
                ERR("Error sending connect event");
                close(fd);
index c4cc17b..0a96b57 100644 (file)
@@ -33,6 +33,7 @@
 
 #include "bt-hal-rfcomm-dbus-handler.h"
 #include "bt-hal-dbus-common-utils.h"
+#include "bt-hal-socket-common.h"
 
 #define BT_HAL_RFCOMM_ID_MAX 245
 #define BT_HAL_RFCOMM_MAX_BUFFER_SIZE 1024
@@ -641,47 +642,6 @@ static rfcomm_server_data_t *__find_rfcomm_server_info_from_uuid(const char *uui
        return NULL;
 }
 
-static int __send_sock_fd(int sock_fd, const void *buf, int size, int send_fd)
-{
-       ssize_t ret;
-       struct msghdr msg;
-       struct cmsghdr *cmsg;
-       struct iovec iov;
-       char cmsg_buf[CMSG_SPACE(sizeof(int))];
-       char err_msg[256] = {0, };
-
-       if (sock_fd == -1 || send_fd == -1)
-               return -1;
-
-       memset(&msg, 0, sizeof(msg));
-       memset(cmsg_buf, 0, sizeof(cmsg_buf));
-
-       msg.msg_control = cmsg_buf;
-       msg.msg_controllen = sizeof(cmsg_buf);
-
-       cmsg = CMSG_FIRSTHDR(&msg);
-       cmsg->cmsg_level = SOL_SOCKET;
-       cmsg->cmsg_type = SCM_RIGHTS;
-       cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
-
-       memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
-
-       iov.iov_base = (unsigned char *) buf;
-       iov.iov_len = size;
-
-       msg.msg_iov = &iov;
-       msg.msg_iovlen = 1;
-
-       ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
-       if (ret < 0) {
-               strerror_r(errno, err_msg, sizeof(err_msg));
-               ERR("sendmsg(): sock_fd %d send_fd %d: %s",
-                               sock_fd, send_fd, err_msg);
-       }
-
-       return ret;
-}
-
 int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
 {
        int ret;
@@ -706,7 +666,7 @@ int __new_server_connection(const char *path, int fd, bt_bdaddr_t *addr)
        ev.size = sizeof(ev);
        memcpy(ev.bdaddr, addr->address, 6);
        ev.status = BT_STATUS_SUCCESS;
-       ret = __send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
+       ret = _bt_hal_send_sock_fd(info->server_fd, (void *)&ev, sizeof(ev), fd);
        if (ret < 0) {
                ERR("Error sending connect event");
                close(fd);
diff --git a/bt-oal/bluez_hal/src/bt-hal-socket-common.c b/bt-oal/bluez_hal/src/bt-hal-socket-common.c
new file mode 100644 (file)
index 0000000..b7c0297
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Bluetooth-frwk
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *             http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+#include <sys/socket.h>
+
+#include <dlog.h>
+
+#include "bt-hal-socket-common.h"
+
+int _bt_hal_send_sock_fd(int sock_fd, const void *buf, int size, int send_fd)
+{
+       ssize_t ret;
+       struct msghdr msg;
+       struct cmsghdr *cmsg;
+       struct iovec iov;
+       char cmsg_buf[CMSG_SPACE(sizeof(int))];
+       char err_msg[256] = {0, };
+
+       if (sock_fd == -1 || send_fd == -1)
+               return -1;
+
+       memset(&msg, 0, sizeof(msg));
+       memset(cmsg_buf, 0, sizeof(cmsg_buf));
+
+       msg.msg_control = cmsg_buf;
+       msg.msg_controllen = sizeof(cmsg_buf);
+
+       cmsg = CMSG_FIRSTHDR(&msg);
+       cmsg->cmsg_level = SOL_SOCKET;
+       cmsg->cmsg_type = SCM_RIGHTS;
+       cmsg->cmsg_len = CMSG_LEN(sizeof(send_fd));
+
+       memcpy(CMSG_DATA(cmsg), &send_fd, sizeof(send_fd));
+
+       iov.iov_base = (unsigned char *) buf;
+       iov.iov_len = size;
+
+       msg.msg_iov = &iov;
+       msg.msg_iovlen = 1;
+
+       ret = sendmsg(sock_fd, &msg, MSG_NOSIGNAL);
+       if (ret < 0) {
+               strerror_r(errno, err_msg, sizeof(err_msg));
+               ERR("sendmsg(): sock_fd %d send_fd %d: %s",
+                               sock_fd, send_fd, err_msg);
+       }
+
+       return ret;
+}
diff --git a/bt-oal/bluez_hal/src/bt-hal-socket-common.h b/bt-oal/bluez_hal/src/bt-hal-socket-common.h
new file mode 100644 (file)
index 0000000..cc8e8da
--- /dev/null
@@ -0,0 +1,39 @@
+/* Bluetooth-frwk
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *              http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+
+#ifndef _BT_HAL_SOCKET_COMMON_H_
+#define _BT_HAL_SOCKET_COMMON_H_
+
+#include <glib.h>
+#include <sys/types.h>
+
+#include "bt-hal.h"
+#include "bt-hal-log.h"
+#include "bt-hal-msg.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+int _bt_hal_send_sock_fd(int sock_fd, const void *buf, int size, int send_fd);
+
+#ifdef __cplusplus
+}
+#endif /* __cplusplus */
+#endif /* _BT_HAL_SOCKET_COMMON_H_ */