Update the RFCOMM server connection information 43/229843/3
authorDoHyun Pyun <dh79.pyun@samsung.com>
Mon, 6 Apr 2020 02:44:42 +0000 (11:44 +0900)
committerDoHyun Pyun <dh79.pyun@samsung.com>
Mon, 6 Apr 2020 05:52:26 +0000 (14:52 +0900)
When we use the RFCOMM direct method, the server
socket's connection is requested to bt-api direclty.
So bt-service does not know the information.

Change-Id: I3f541ad7a08d5d580783aa84b4c48760e0a0a198
Signed-off-by: DoHyun Pyun <dh79.pyun@samsung.com>
bt-api/bt-rfcomm-server.c
bt-service-adaptation/services/bt-request-handler.c
bt-service-adaptation/services/include/bt-service-battery-monitor.h
bt-service-adaptation/services/include/bt-service-rfcomm.h
bt-service-adaptation/services/rfcomm/bt-service-rfcomm.c
include/bt-internal-types.h

index 1365077..1e6e1c6 100644 (file)
 #define BLUETOOTH_SOCK_CONNECT_INFO_LEN 16
 #define BATTERY_MONITOR_RFCOMM_INTERVAL 5
 
+static int __rfcomm_send_conn_info(gboolean connected, int client_fd)
+{
+       int result;
+
+       if (bluetooth_get_battery_monitor_state() == 0) {
+               BT_DBG("Battery monitor state is off");
+               return BLUETOOTH_ERROR_NONE;
+       }
+
+       BT_INIT_PARAMS();
+       BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+       g_array_append_vals(in_param1, &connected, sizeof(gboolean));
+       g_array_append_vals(in_param2, &client_fd, sizeof(int));
+
+       result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SERVER_UPDATE_CONNECTION_INFO,
+               in_param1, in_param2, in_param3, in_param4, &out_param);
+
+       BT_DBG("result: %x", result);
+
+       BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+
+       return result;
+}
+
 #ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
 
 static GSList *rfcomm_nodes;
@@ -197,6 +222,9 @@ static void __connected_cb(rfcomm_info_t *info, rfcomm_conn_t *conn,
 
        BT_INFO_C("### Connected [RFCOMM Server]");
 
+       if (__rfcomm_send_conn_info(TRUE, conn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to send the connection info");
+
        _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
                        BLUETOOTH_ERROR_NONE, &conn_info,
                        event_info->cb, event_info->user_data);
@@ -234,6 +262,9 @@ static void __rfcomm_server_disconnect_conn(rfcomm_conn_t *conn,
        BT_INFO("Disconnected FD [%d]", conn->fd);
        disconn_info.socket_fd = conn->fd;
 
+       if (__rfcomm_send_conn_info(FALSE, disconn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
+               BT_ERR("Fail to send the connection info");
+
        _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
                        BLUETOOTH_ERROR_NONE, &disconn_info,
                        event_info->cb, event_info->user_data);
index e870bf2..d91f448 100644 (file)
@@ -1557,7 +1557,23 @@ int __bt_bluez_request(int function_name,
                BT_DBG("Sending details to bluetooth battery monitor: %ld, %ld, %d", (long int)uid, (long int)pid, size);
                _bt_bm_add_transaction_details(uid, pid, size, TX_DATA);
                break;
-        }
+       }
+       case BT_RFCOMM_SERVER_UPDATE_CONNECTION_INFO: {
+               gboolean connected = FALSE;
+               int client_fd = -1;
+
+               sender = (char *)g_dbus_method_invocation_get_sender(context);
+
+               __bt_service_get_parameters(in_param1, &connected, sizeof(gboolean));
+               __bt_service_get_parameters(in_param2, &client_fd, sizeof(int));
+
+               if (connected == TRUE)
+                       result = _bt_rfcomm_server_conn_added(sender, client_fd);
+               else
+                       result = _bt_rfcomm_server_conn_removed(sender, client_fd);
+
+               break;
+       }
        case BT_AUDIO_SELECT_ROLE: {
                bluetooth_audio_role_t role;
 
@@ -3992,6 +4008,7 @@ gboolean __bt_service_check_privilege(int function_name,
        case BT_RFCOMM_REMOVE_SOCKET:
        case BT_RFCOMM_SEND_RX_DETAILS:
        case BT_RFCOMM_SEND_TX_DETAILS:
+       case BT_RFCOMM_SERVER_UPDATE_CONNECTION_INFO:
 
        case BT_OPP_PUSH_FILES:
        case BT_OPP_CANCEL_PUSH:
@@ -4397,6 +4414,9 @@ static void __name_owner_changed(GDBusConnection *connection,
 
        /* Check if le_scanning app is terminated */
        _bt_check_le_scanner_app_termination(name);
+
+       /* Check if RFCOMM server is terminated */
+       _bt_rfcomm_server_check_termination(name);
 }
 
 static void __bt_service_bus_acquired_handler(GDBusConnection *connection,
index 7e227f8..093353f 100644 (file)
@@ -77,6 +77,10 @@ void _bt_bm_add_scan_app(bt_bm_scan_type_e type, uid_t uid, pid_t pid);
 
 void _bt_bm_remove_scan_app(bt_bm_scan_type_e type, uid_t uid, pid_t pid);
 
+void _bt_start_connect_time(void);
+
+void _bt_stop_connect_time(void);
+
 #ifdef __cplusplus
 }
 #endif /* __cplusplus */
index 98ac5ed..0f4f9db 100644 (file)
@@ -33,6 +33,9 @@ extern "C" {
 int _bt_rfcomm_connect_using_uuid(bluetooth_device_address_t *device_address, char *remote_uuid);
 int _bt_rfcomm_connect_using_channel(bluetooth_device_address_t *device_address, char *chan_str);
 int _bt_rfcomm_socket_listen(char *svc_name, char *uuid, bool accept);
+int _bt_rfcomm_server_conn_added(char *svc_name, int client_fd);
+int _bt_rfcomm_server_conn_removed(char *svc_name, int client_fd);
+void _bt_rfcomm_server_check_termination(const char *name);
 
 #ifdef __cplusplus
 }
index 9bfa821..a8ff590 100644 (file)
 #include "bt-service-common.h"
 #include "bt-service-rfcomm.h"
 #include "bt-service-socket.h"
+#include "bt-service-battery-monitor.h"
+
+typedef struct {
+       char *sender;
+       int client_fd;
+} bt_rfcomm_conn_info_t;
+
+static GSList *server_conn_list = NULL;
 
 static void __bt_rfcomm_reply_pending_request(int result,
                int service_function, void *user_data, unsigned int size)
@@ -245,3 +253,103 @@ int _bt_rfcomm_socket_listen(char *svc_name, char *uuid, bool accept)
        BT_DBG("-");
        return sock_fd;
 }
+
+static bt_rfcomm_conn_info_t* __bt_find_server_from_list(const char *sender, int client_fd)
+{
+       GSList *l;
+       bt_rfcomm_conn_info_t *conn_info;
+
+       retv_if(NULL == sender, NULL);
+
+       for (l = server_conn_list; l != NULL; l = g_slist_next(l)) {
+               conn_info = l->data;
+               if (g_strcmp0(conn_info->sender, sender) == 0) {
+                       if (client_fd == conn_info->client_fd)
+                               return conn_info;
+               }
+       }
+
+       return NULL;
+}
+
+int _bt_rfcomm_server_conn_added(char *svc_name, int client_fd)
+{
+       bt_rfcomm_conn_info_t *conn_info = NULL;
+
+       retv_if(NULL == svc_name, BLUETOOTH_ERROR_INVALID_PARAM);
+
+       if (__bt_find_server_from_list(svc_name, client_fd) != NULL) {
+               BT_ERR("Client fd already exist");
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+       }
+
+       BT_INFO("RFCOMM server is connected: [%s], [%d]", svc_name, client_fd);
+
+       conn_info = g_malloc0(sizeof(bt_rfcomm_conn_info_t));
+
+       if (conn_info == NULL)
+               return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
+
+       _bt_start_connect_time();
+
+       conn_info->sender = g_strdup(svc_name);
+       conn_info->client_fd = client_fd;
+
+       server_conn_list = g_slist_append(server_conn_list, conn_info);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+int _bt_rfcomm_server_conn_removed(char *svc_name, int client_fd)
+{
+       bt_rfcomm_conn_info_t *conn_info = NULL;
+
+       retv_if(NULL == svc_name, BLUETOOTH_ERROR_INVALID_PARAM);
+
+       BT_INFO("RFCOMM server is disconnected: [%s], [%d]", svc_name, client_fd);
+
+       /* Find the first connection */
+       conn_info = __bt_find_server_from_list(svc_name, client_fd);
+
+       if (conn_info == NULL) {
+               BT_ERR("No client fd is in list");
+               return BLUETOOTH_ERROR_INVALID_PARAM;
+       }
+
+       _bt_stop_connect_time();
+
+       server_conn_list = g_slist_remove(server_conn_list, conn_info);
+       g_free(conn_info->sender);
+       g_free(conn_info);
+
+       return BLUETOOTH_ERROR_NONE;
+}
+
+void _bt_rfcomm_server_check_termination(const char *name)
+{
+       GSList *l;
+       bt_rfcomm_conn_info_t *conn_info = NULL;
+
+       ret_if(NULL == name);
+
+       for (l = server_conn_list; l != NULL; ) {
+               conn_info = l->data;
+               l = g_slist_next(l);
+               if (g_strcmp0(conn_info->sender, name) == 0) {
+                       _bt_rfcomm_server_conn_removed((char *)name,
+                                               conn_info->client_fd);
+               }
+       }
+}
+
+static void __bt_free_server_conn(bt_rfcomm_conn_info_t *conn_info)
+{
+       g_free(conn_info->sender);
+       g_free(conn_info);
+}
+
+void _bt_rfcomm_server_all_conn_removed(void)
+{
+       g_slist_free_full(server_conn_list, (GDestroyNotify)__bt_free_server_conn);
+       server_conn_list = NULL;
+}
index f43b0d0..de99945 100644 (file)
@@ -335,7 +335,8 @@ typedef enum {
        BT_RFCOMM_CREATE_SOCKET_EX,
        BT_RFCOMM_REMOVE_SOCKET_EX,
        BT_RFCOMM_SEND_RX_DETAILS,
-        BT_RFCOMM_SEND_TX_DETAILS,
+       BT_RFCOMM_SEND_TX_DETAILS,
+       BT_RFCOMM_SERVER_UPDATE_CONNECTION_INFO,
        BT_PBAP_CONNECT = BT_FUNC_PBAP_BASE,
        BT_PBAP_DISCONNECT,
        BT_PBAP_GET_PHONEBOOK_SIZE,