19c1b07c04be902343062844802277de1f84ab93
[platform/core/connectivity/bluetooth-frwk.git] / bt-service-adaptation / services / rfcomm / bt-service-rfcomm.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Author:  Atul Kumar Rai <a.rai@samsung.com>
7  *
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
11  *
12  *              http://www.apache.org/licenses/LICENSE-2.0
13  *
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.
19  *
20  */
21
22 #include <stdio.h>
23 #include <glib.h>
24 #include <gio/gio.h>
25 #include <gio/gunixfdlist.h>
26 #include <dlog.h>
27 #include <string.h>
28
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
38 static void __bt_rfcomm_reply_pending_request(int result,
39                 int service_function, void *user_data, unsigned int size)
40 {
41         GSList *l;
42         GArray *out_param;
43         invocation_info_t *req_info;
44
45         /* Get method invocation context */
46         for (l = _bt_get_invocation_list(); l != NULL; l = g_slist_next(l)) {
47                 req_info = l->data;
48                 if (req_info == NULL || req_info->service_function != service_function)
49                         continue;
50
51                 /* Create out param */
52                 out_param = g_array_new(FALSE, FALSE, sizeof(gchar));
53
54                 switch (service_function) {
55                 case BT_RFCOMM_CLIENT_CONNECT: {
56                         GUnixFDList *fd_list = NULL;
57                         GError *error = NULL;
58                         char address[BT_ADDRESS_STRING_SIZE];
59                         bluetooth_rfcomm_connection_t *ptr = user_data;
60
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);
67                                 break;
68                         }
69
70                         g_array_append_vals(out_param, user_data, size);
71
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);
77                         }
78
79                         _bt_service_method_return_with_unix_fd_list(
80                                         req_info->context, out_param, result, fd_list);
81                         if (fd_list)
82                                 g_object_unref(fd_list);
83
84                         g_free(req_info->user_data);
85                         _bt_free_info_from_invocation_list(req_info);
86                         g_array_free(out_param, TRUE);
87                         break;
88                 }
89                 case BT_RFCOMM_LISTEN: {
90                         GUnixFDList *fd_list = NULL;
91                         GError *error = NULL;
92                         int *socket_fd = user_data;
93
94                         BT_INFO("Server socket fd: %d", *socket_fd);
95
96                         g_array_append_vals(out_param, user_data, size);
97
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);
102
103                         _bt_service_method_return_with_unix_fd_list(
104                                         req_info->context, out_param, result, fd_list);
105
106                         close(*socket_fd);
107                         g_object_unref(fd_list);
108
109                         _bt_free_info_from_invocation_list(req_info);
110                         g_array_free(out_param, TRUE);
111                         break;
112                 }
113                 default:
114                         BT_ERR("Unknown Service function");
115                 }
116         }
117
118         return;
119 }
120
121 static void __bt_rfcomm_socket_conn_cb(int result, int sock_fd, char *address, char *uuid, int chan)
122 {
123         bluetooth_rfcomm_connection_t conn_info;
124
125         ret_if(NULL == address);
126         ret_if(NULL == uuid);
127
128         BT_DBG("+");
129
130         BT_INFO("result: %d, socket_fd: %d, address: %s, uuid: %s, chan: %d",
131                         result, sock_fd, address, uuid, chan);
132
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);
139
140         __bt_rfcomm_reply_pending_request(
141                         result, BT_RFCOMM_CLIENT_CONNECT,
142                         (void *)&conn_info, sizeof(bluetooth_rfcomm_connection_t));
143
144         BT_DBG("-");
145 }
146
147 int _bt_rfcomm_connect_using_uuid(bluetooth_device_address_t *device_address, char *remote_uuid)
148 {
149         int result;
150         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
151
152         BT_DBG("+");
153
154         retv_if(NULL == device_address, BLUETOOTH_ERROR_INVALID_PARAM);
155         retv_if(NULL == remote_uuid, BLUETOOTH_ERROR_INVALID_PARAM);
156
157         _bt_convert_addr_type_to_string(address, device_address->addr);
158         BT_INFO("RFCOMM connect called for [%s], uuid: [%s]", address, remote_uuid);
159
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");
164                 return result;
165         }
166
167         BT_DBG("-");
168         return BLUETOOTH_ERROR_NONE;
169 }
170
171 /* Range of the Channel : 0 <= channel <= 30 */
172 int _bt_rfcomm_connect_using_channel(bluetooth_device_address_t *device_address, char *chan_str)
173 {
174         int channel;
175         int result = BLUETOOTH_ERROR_NONE;
176         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
177
178         BT_DBG("+");
179
180         retv_if(NULL == device_address, BLUETOOTH_ERROR_INVALID_PARAM);
181         retv_if(NULL == chan_str, BLUETOOTH_ERROR_INVALID_PARAM);
182
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);
186
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");
191                 return result;
192         }
193
194         BT_DBG("-");
195         return result;
196 }
197
198 gboolean __bt_send_rfcomm_server_fd(gpointer user_data)
199 {
200         BT_DBG("+");
201
202         __bt_rfcomm_reply_pending_request(BLUETOOTH_ERROR_NONE,
203                         BT_RFCOMM_LISTEN, user_data, sizeof(int));
204
205         g_free(user_data);
206         BT_DBG("-");
207         return FALSE;
208 }
209
210 int _bt_rfcomm_socket_listen(char *svc_name, char *uuid)
211 {
212         int channel = 0;
213         int sock_fd;
214
215         BT_DBG("+");
216
217         retv_if(NULL == svc_name, BLUETOOTH_ERROR_INVALID_PARAM);
218         retv_if(NULL == uuid, BLUETOOTH_ERROR_INVALID_PARAM);
219
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);
222         if (sock_fd < 0) {
223                 BT_ERR("_bt_socket_listen failed");
224                 return BLUETOOTH_ERROR_INTERNAL;
225         }
226
227         g_idle_add(__bt_send_rfcomm_server_fd, g_memdup(&sock_fd, sizeof(int)));
228         BT_DBG("-");
229         return sock_fd;
230 }