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: {
90 GUnixFDList *fd_list = NULL;
92 int *socket_fd = user_data;
94 BT_INFO("Server socket fd: %d", *socket_fd);
96 g_array_append_vals(out_param, user_data, size);
98 /* Add socket fd to unix_fd_list */
99 fd_list = g_unix_fd_list_new();
100 g_unix_fd_list_append(fd_list, *socket_fd, &error);
101 g_assert_no_error(error);
103 _bt_service_method_return_with_unix_fd_list(
104 req_info->context, out_param, result, fd_list);
107 g_object_unref(fd_list);
109 _bt_free_info_from_invocation_list(req_info);
110 g_array_free(out_param, TRUE);
114 BT_ERR("Unknown Service function");
121 static void __bt_rfcomm_socket_conn_cb(int result, int sock_fd, char *address, char *uuid, int chan)
123 bluetooth_rfcomm_connection_t conn_info;
125 ret_if(NULL == address);
126 ret_if(NULL == uuid);
130 BT_INFO("result: %d, socket_fd: %d, address: %s, uuid: %s, chan: %d",
131 result, sock_fd, address, uuid, chan);
133 /* Fill RFCOMM connection structure and send reply to pending request */
134 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
135 conn_info.socket_fd = sock_fd;
136 conn_info.device_role = RFCOMM_ROLE_CLIENT;
137 g_strlcpy(conn_info.uuid, uuid, BLUETOOTH_UUID_STRING_MAX);
138 _bt_convert_addr_string_to_type(conn_info.device_addr.addr, address);
140 __bt_rfcomm_reply_pending_request(
141 result, BT_RFCOMM_CLIENT_CONNECT,
142 (void *)&conn_info, sizeof(bluetooth_rfcomm_connection_t));
147 int _bt_rfcomm_connect_using_uuid(bluetooth_device_address_t *device_address, char *remote_uuid)
150 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
154 retv_if(NULL == device_address, BLUETOOTH_ERROR_INVALID_PARAM);
155 retv_if(NULL == remote_uuid, BLUETOOTH_ERROR_INVALID_PARAM);
157 _bt_convert_addr_type_to_string(address, device_address->addr);
158 BT_INFO("RFCOMM connect called for [%s], uuid: [%s]", address, remote_uuid);
160 result = _bt_socket_client_connect(SOCK_TYPE_RFCOMM,
161 address, remote_uuid, -1, __bt_rfcomm_socket_conn_cb);
162 if (BLUETOOTH_ERROR_NONE != result) {
163 BT_ERR("_bt_socket_client_connect failed");
168 return BLUETOOTH_ERROR_NONE;
171 /* Range of the Channel : 0 <= channel <= 30 */
172 int _bt_rfcomm_connect_using_channel(bluetooth_device_address_t *device_address, char *chan_str)
175 int result = BLUETOOTH_ERROR_NONE;
176 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
180 retv_if(NULL == device_address, BLUETOOTH_ERROR_INVALID_PARAM);
181 retv_if(NULL == chan_str, BLUETOOTH_ERROR_INVALID_PARAM);
183 _bt_convert_addr_type_to_string(address, device_address->addr);
184 channel = atoi(chan_str);
185 BT_INFO("RFCOMM connect called for [%s], channel: %d", address, channel);
187 result = _bt_socket_client_connect(SOCK_TYPE_RFCOMM,
188 address, NULL, channel, __bt_rfcomm_socket_conn_cb);
189 if (BLUETOOTH_ERROR_NONE != result) {
190 BT_ERR("_bt_socket_client_connect failed");
198 gboolean __bt_send_rfcomm_server_fd(gpointer user_data)
202 __bt_rfcomm_reply_pending_request(BLUETOOTH_ERROR_NONE,
203 BT_RFCOMM_LISTEN, user_data, sizeof(int));
210 int _bt_rfcomm_socket_listen(char *svc_name, char *uuid)
217 retv_if(NULL == svc_name, BLUETOOTH_ERROR_INVALID_PARAM);
218 retv_if(NULL == uuid, BLUETOOTH_ERROR_INVALID_PARAM);
220 BT_INFO("RFCOMM create socket called with svc name: [%s], uuid: [%s]", svc_name, uuid);
221 sock_fd = _bt_socket_listen(SOCK_TYPE_RFCOMM, svc_name, uuid, channel);
223 BT_ERR("_bt_socket_listen failed");
224 return BLUETOOTH_ERROR_INTERNAL;
227 g_idle_add(__bt_send_rfcomm_server_fd, g_memdup(&sock_fd, sizeof(int)));