4 * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
6 * Author: Atul Kumar Rai <a.rai@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 #include <gio/gunixfdlist.h>
29 #include "bluetooth-api.h"
30 #include "bt-internal-types.h"
31 #include "bt-request-handler.h"
32 #include "bt-service-util.h"
33 #include "bt-service-event.h"
34 #include "bt-service-common.h"
35 #include "bt-service-rfcomm.h"
36 #include "bt-service-socket.h"
37 #include "bt-service-battery-monitor.h"
42 } bt_rfcomm_conn_info_t;
44 static GSList *server_conn_list = NULL;
46 static void __bt_rfcomm_reply_pending_request(int result,
47 int service_function, void *user_data, unsigned int size)
51 invocation_info_t *req_info;
53 /* Get method invocation context */
54 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
56 if (req_info == NULL || req_info->service_function != service_function)
59 /* Create out param */
60 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
62 switch (service_function) {
63 case BT_RFCOMM_CLIENT_CONNECT: {
64 GUnixFDList *fd_list = NULL;
66 char address[BT_ADDRESS_STRING_SIZE];
67 bluetooth_rfcomm_connection_t *ptr = user_data;
69 /* Check if connect address matched with requested address */
70 _bt_convert_addr_type_to_string(address, ptr->device_addr.addr);
71 if (strncasecmp(address, (char *)req_info->user_data,
72 BT_ADDRESS_STRING_SIZE)) {
73 BT_INFO("RFCOMM connected addr: [%s], requested addr: [%s]",
74 address, (char *)req_info->user_data);
78 g_array_append_vals(out_param, user_data, size);
80 if (BLUETOOTH_ERROR_NONE == result) {
81 fd_list = g_unix_fd_list_new();
82 g_unix_fd_list_append(fd_list, ptr->socket_fd, &error);
83 g_assert_no_error(error);
84 close(ptr->socket_fd);
87 _bt_service_method_return_with_unix_fd_list(
88 req_info->context, out_param, result, fd_list);
90 g_object_unref(fd_list);
92 g_free(req_info->user_data);
93 _bt_free_info_from_invocation_list(req_info);
94 g_array_free(out_param, TRUE);
97 case BT_RFCOMM_LISTEN_AND_ACCEPT:
98 case BT_RFCOMM_LISTEN: {
99 GUnixFDList *fd_list = NULL;
100 GError *error = NULL;
101 int *socket_fd = user_data;
103 BT_INFO("Server socket fd: %d", *socket_fd);
105 g_array_append_vals(out_param, user_data, size);
107 /* Add socket fd to unix_fd_list */
108 fd_list = g_unix_fd_list_new();
109 g_unix_fd_list_append(fd_list, *socket_fd, &error);
110 g_assert_no_error(error);
112 _bt_service_method_return_with_unix_fd_list(
113 req_info->context, out_param, result, fd_list);
116 g_object_unref(fd_list);
118 _bt_free_info_from_invocation_list(req_info);
119 g_array_free(out_param, TRUE);
123 BT_ERR("Unknown Service function");
130 static void __bt_rfcomm_socket_conn_cb(int result, int sock_fd, char *address, char *uuid, int chan)
132 bluetooth_rfcomm_connection_t conn_info;
134 ret_if(NULL == address);
135 ret_if(NULL == uuid);
139 BT_INFO("result: %d, socket_fd: %d, address: %s, uuid: %s, chan: %d",
140 result, sock_fd, address, uuid, chan);
142 /* Fill RFCOMM connection structure and send reply to pending request */
143 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
144 conn_info.socket_fd = sock_fd;
145 conn_info.device_role = RFCOMM_ROLE_CLIENT;
146 g_strlcpy(conn_info.uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
147 _bt_convert_addr_string_to_type(conn_info.device_addr.addr, address);
149 __bt_rfcomm_reply_pending_request(
150 result, BT_RFCOMM_CLIENT_CONNECT,
151 (void *)&conn_info, sizeof(bluetooth_rfcomm_connection_t));
156 int _bt_rfcomm_connect_using_uuid(bluetooth_device_address_t *device_address, char *remote_uuid)
159 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
163 retv_if(NULL == device_address, BLUETOOTH_ERROR_INVALID_PARAM);
164 retv_if(NULL == remote_uuid, BLUETOOTH_ERROR_INVALID_PARAM);
166 _bt_convert_addr_type_to_string(address, device_address->addr);
167 BT_INFO("RFCOMM connect called for [%s], uuid: [%s]", address, remote_uuid);
169 result = _bt_socket_client_connect(SOCK_TYPE_RFCOMM,
170 address, remote_uuid, -1, __bt_rfcomm_socket_conn_cb);
171 if (BLUETOOTH_ERROR_NONE != result) {
172 BT_ERR("_bt_socket_client_connect failed");
177 return BLUETOOTH_ERROR_NONE;
180 /* Range of the Channel : 0 <= channel <= 30 */
181 int _bt_rfcomm_connect_using_channel(bluetooth_device_address_t *device_address, char *chan_str)
184 int result = BLUETOOTH_ERROR_NONE;
185 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
189 retv_if(NULL == device_address, BLUETOOTH_ERROR_INVALID_PARAM);
190 retv_if(NULL == chan_str, BLUETOOTH_ERROR_INVALID_PARAM);
192 _bt_convert_addr_type_to_string(address, device_address->addr);
193 channel = atoi(chan_str);
194 BT_INFO("RFCOMM connect called for [%s], channel: %d", address, channel);
196 result = _bt_socket_client_connect(SOCK_TYPE_RFCOMM,
197 address, NULL, channel, __bt_rfcomm_socket_conn_cb);
198 if (BLUETOOTH_ERROR_NONE != result) {
199 BT_ERR("_bt_socket_client_connect failed");
207 gboolean __bt_send_rfcomm_server_fd(gpointer user_data)
211 __bt_rfcomm_reply_pending_request(BLUETOOTH_ERROR_NONE,
212 BT_RFCOMM_LISTEN, user_data, sizeof(int));
219 gboolean __bt_send_rfcomm_server_fd_with_accept(gpointer user_data)
223 __bt_rfcomm_reply_pending_request(BLUETOOTH_ERROR_NONE,
224 BT_RFCOMM_LISTEN_AND_ACCEPT, user_data, sizeof(int));
231 int _bt_rfcomm_socket_listen(char *svc_name, char *uuid, bool accept)
238 retv_if(NULL == svc_name, BLUETOOTH_ERROR_INVALID_PARAM);
239 retv_if(NULL == uuid, BLUETOOTH_ERROR_INVALID_PARAM);
241 BT_INFO("RFCOMM create socket called with svc name: [%s], uuid: [%s]", svc_name, uuid);
242 sock_fd = _bt_socket_listen(SOCK_TYPE_RFCOMM, svc_name, uuid, channel);
244 BT_ERR("_bt_socket_listen failed");
245 return BLUETOOTH_ERROR_INTERNAL;
249 g_idle_add(__bt_send_rfcomm_server_fd, g_memdup(&sock_fd, sizeof(int)));
251 g_idle_add(__bt_send_rfcomm_server_fd_with_accept, g_memdup(&sock_fd, sizeof(int)));
257 static bt_rfcomm_conn_info_t* __bt_find_server_from_list(const char *sender, int client_fd)
260 bt_rfcomm_conn_info_t *conn_info;
262 retv_if(NULL == sender, NULL);
264 for (l = server_conn_list; l != NULL; l = g_slist_next(l)) {
266 if (g_strcmp0(conn_info->sender, sender) == 0) {
267 if (client_fd == conn_info->client_fd)
275 int _bt_rfcomm_server_conn_added(char *svc_name, int client_fd)
277 bt_rfcomm_conn_info_t *conn_info = NULL;
279 retv_if(NULL == svc_name, BLUETOOTH_ERROR_INVALID_PARAM);
281 if (__bt_find_server_from_list(svc_name, client_fd) != NULL) {
282 BT_ERR("Client fd already exist");
283 return BLUETOOTH_ERROR_INVALID_PARAM;
286 BT_INFO("RFCOMM server is connected: [%s], [%d]", svc_name, client_fd);
288 conn_info = g_malloc0(sizeof(bt_rfcomm_conn_info_t));
290 if (conn_info == NULL)
291 return BLUETOOTH_ERROR_MEMORY_ALLOCATION;
293 _bt_start_connect_time();
295 conn_info->sender = g_strdup(svc_name);
296 conn_info->client_fd = client_fd;
298 server_conn_list = g_slist_append(server_conn_list, conn_info);
300 return BLUETOOTH_ERROR_NONE;
303 int _bt_rfcomm_server_conn_removed(char *svc_name, int client_fd)
305 bt_rfcomm_conn_info_t *conn_info = NULL;
307 retv_if(NULL == svc_name, BLUETOOTH_ERROR_INVALID_PARAM);
309 BT_INFO("RFCOMM server is disconnected: [%s], [%d]", svc_name, client_fd);
311 /* Find the first connection */
312 conn_info = __bt_find_server_from_list(svc_name, client_fd);
314 if (conn_info == NULL) {
315 BT_ERR("No client fd is in list");
316 return BLUETOOTH_ERROR_INVALID_PARAM;
319 _bt_stop_connect_time();
321 server_conn_list = g_slist_remove(server_conn_list, conn_info);
322 g_free(conn_info->sender);
325 return BLUETOOTH_ERROR_NONE;
328 void _bt_rfcomm_server_check_termination(const char *name)
331 bt_rfcomm_conn_info_t *conn_info = NULL;
333 ret_if(NULL == name);
335 for (l = server_conn_list; l != NULL; ) {
338 if (g_strcmp0(conn_info->sender, name) == 0) {
339 _bt_rfcomm_server_conn_removed((char *)name,
340 conn_info->client_fd);
344 /* Cleanup osp server */
345 _bt_unregister_osp_server_in_agent_by_sender(name);
348 static void __bt_free_server_conn(bt_rfcomm_conn_info_t *conn_info)
350 g_free(conn_info->sender);
354 void _bt_rfcomm_server_all_conn_removed(void)
356 g_slist_free_full(server_conn_list, (GDestroyNotify)__bt_free_server_conn);
357 server_conn_list = NULL;