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"
38 static void __bt_rfcomm_reply_pending_request(int result,
39 int service_function, void *user_data, unsigned int size)
43 invocation_info_t *req_info;
45 /* Get method invocation context */
46 for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
48 if (req_info == NULL || req_info->service_function != service_function)
51 /* Create out param */
52 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
54 switch (service_function) {
55 case BT_RFCOMM_CLIENT_CONNECT: {
56 GUnixFDList *fd_list = NULL;
58 char address[BT_ADDRESS_STRING_SIZE];
59 bluetooth_rfcomm_connection_t *ptr = user_data;
61 /* Check if connect address matched with requested address */
62 _bt_convert_addr_type_to_string(address, ptr->device_addr.addr);
63 if (strncasecmp(address, (char *)req_info->user_data,
64 BT_ADDRESS_STRING_SIZE)) {
65 BT_INFO("RFCOMM connected addr: [%s], requested addr: [%s]",
66 address, (char *)req_info->user_data);
70 g_array_append_vals(out_param, user_data, size);
72 if (BLUETOOTH_ERROR_NONE == result) {
73 fd_list = g_unix_fd_list_new();
74 g_unix_fd_list_append(fd_list, ptr->socket_fd, &error);
75 g_assert_no_error(error);
76 close(ptr->socket_fd);
79 _bt_service_method_return_with_unix_fd_list(
80 req_info->context, out_param, result, fd_list);
82 g_object_unref(fd_list);
84 g_free(req_info->user_data);
85 _bt_free_info_from_invocation_list(req_info);
86 g_array_free(out_param, TRUE);
89 case BT_RFCOMM_LISTEN_AND_ACCEPT:
90 case BT_RFCOMM_LISTEN: {
91 GUnixFDList *fd_list = NULL;
93 int *socket_fd = user_data;
95 BT_INFO("Server socket fd: %d", *socket_fd);
97 g_array_append_vals(out_param, user_data, size);
99 /* Add socket fd to unix_fd_list */
100 fd_list = g_unix_fd_list_new();
101 g_unix_fd_list_append(fd_list, *socket_fd, &error);
102 g_assert_no_error(error);
104 _bt_service_method_return_with_unix_fd_list(
105 req_info->context, out_param, result, fd_list);
108 g_object_unref(fd_list);
110 _bt_free_info_from_invocation_list(req_info);
111 g_array_free(out_param, TRUE);
115 BT_ERR("Unknown Service function");
122 static void __bt_rfcomm_socket_conn_cb(int result, int sock_fd, char *address, char *uuid, int chan)
124 bluetooth_rfcomm_connection_t conn_info;
126 ret_if(NULL == address);
127 ret_if(NULL == uuid);
131 BT_INFO("result: %d, socket_fd: %d, address: %s, uuid: %s, chan: %d",
132 result, sock_fd, address, uuid, chan);
134 /* Fill RFCOMM connection structure and send reply to pending request */
135 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
136 conn_info.socket_fd = sock_fd;
137 conn_info.device_role = RFCOMM_ROLE_CLIENT;
138 g_strlcpy(conn_info.uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
139 _bt_convert_addr_string_to_type(conn_info.device_addr.addr, address);
141 __bt_rfcomm_reply_pending_request(
142 result, BT_RFCOMM_CLIENT_CONNECT,
143 (void *)&conn_info, sizeof(bluetooth_rfcomm_connection_t));
148 int _bt_rfcomm_connect_using_uuid(bluetooth_device_address_t *device_address, char *remote_uuid)
151 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
155 retv_if(NULL == device_address, BLUETOOTH_ERROR_INVALID_PARAM);
156 retv_if(NULL == remote_uuid, BLUETOOTH_ERROR_INVALID_PARAM);
158 _bt_convert_addr_type_to_string(address, device_address->addr);
159 BT_INFO("RFCOMM connect called for [%s], uuid: [%s]", address, remote_uuid);
161 result = _bt_socket_client_connect(SOCK_TYPE_RFCOMM,
162 address, remote_uuid, -1, __bt_rfcomm_socket_conn_cb);
163 if (BLUETOOTH_ERROR_NONE != result) {
164 BT_ERR("_bt_socket_client_connect failed");
169 return BLUETOOTH_ERROR_NONE;
172 /* Range of the Channel : 0 <= channel <= 30 */
173 int _bt_rfcomm_connect_using_channel(bluetooth_device_address_t *device_address, char *chan_str)
176 int result = BLUETOOTH_ERROR_NONE;
177 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
181 retv_if(NULL == device_address, BLUETOOTH_ERROR_INVALID_PARAM);
182 retv_if(NULL == chan_str, BLUETOOTH_ERROR_INVALID_PARAM);
184 _bt_convert_addr_type_to_string(address, device_address->addr);
185 channel = atoi(chan_str);
186 BT_INFO("RFCOMM connect called for [%s], channel: %d", address, channel);
188 result = _bt_socket_client_connect(SOCK_TYPE_RFCOMM,
189 address, NULL, channel, __bt_rfcomm_socket_conn_cb);
190 if (BLUETOOTH_ERROR_NONE != result) {
191 BT_ERR("_bt_socket_client_connect failed");
199 gboolean __bt_send_rfcomm_server_fd(gpointer user_data)
203 __bt_rfcomm_reply_pending_request(BLUETOOTH_ERROR_NONE,
204 BT_RFCOMM_LISTEN, user_data, sizeof(int));
211 gboolean __bt_send_rfcomm_server_fd_with_accept(gpointer user_data)
215 __bt_rfcomm_reply_pending_request(BLUETOOTH_ERROR_NONE,
216 BT_RFCOMM_LISTEN_AND_ACCEPT, user_data, sizeof(int));
223 int _bt_rfcomm_socket_listen(char *svc_name, char *uuid, bool accept)
230 retv_if(NULL == svc_name, BLUETOOTH_ERROR_INVALID_PARAM);
231 retv_if(NULL == uuid, BLUETOOTH_ERROR_INVALID_PARAM);
233 BT_INFO("RFCOMM create socket called with svc name: [%s], uuid: [%s]", svc_name, uuid);
234 sock_fd = _bt_socket_listen(SOCK_TYPE_RFCOMM, svc_name, uuid, channel);
236 BT_ERR("_bt_socket_listen failed");
237 return BLUETOOTH_ERROR_INTERNAL;
241 g_idle_add(__bt_send_rfcomm_server_fd, g_memdup(&sock_fd, sizeof(int)));
243 g_idle_add(__bt_send_rfcomm_server_fd_with_accept, g_memdup(&sock_fd, sizeof(int)));