5396f700107b1e0d7674de07a9b5e28dbd78b863
[platform/core/connectivity/bluetooth-frwk.git] / bt-api / bt-rfcomm-server.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <string.h>
21
22 #include "bluetooth-api.h"
23 #include "bt-internal-types.h"
24
25 #include "bt-common.h"
26 #include "bt-request-sender.h"
27 #include "bt-event-handler.h"
28
29
30 BT_EXPORT_API int bluetooth_rfcomm_create_socket(const char *uuid)
31 {
32         int result;
33         int socket_fd = -1;
34         char uuid_str[BLUETOOTH_UUID_STRING_MAX];
35
36         BT_CHECK_ENABLED(return);
37         BT_CHECK_PARAMETER(uuid, return);
38
39         BT_INIT_PARAMS();
40         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
41
42         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
43         g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
44
45         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_CREATE_SOCKET,
46                 in_param1, in_param2, in_param3, in_param4, &out_param);
47
48         BT_DBG("result: %x", result);
49
50         if (result == BLUETOOTH_ERROR_NONE) {
51                 socket_fd = g_array_index(out_param, int, 0);
52         } else {
53                 BT_ERR("Fail to send request");
54         }
55
56         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
57
58         return socket_fd;
59 }
60
61 BT_EXPORT_API int bluetooth_rfcomm_remove_socket(int socket_fd)
62 {
63         int result;
64
65         BT_CHECK_ENABLED(return);
66
67         BT_INIT_PARAMS();
68         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
69
70         g_array_append_vals(in_param1, &socket_fd, sizeof(int));
71
72         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REMOVE_SOCKET,
73                 in_param1, in_param2, in_param3, in_param4, &out_param);
74
75         BT_DBG("result: %x", result);
76
77         if (result == BLUETOOTH_ERROR_NONE) {
78                 _bt_remove_server(socket_fd);
79         }
80
81         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
82
83         return result;
84 }
85
86 BT_EXPORT_API int bluetooth_rfcomm_server_disconnect(int socket_fd)
87 {
88         int result;
89
90         BT_CHECK_ENABLED(return);
91
92         BT_INIT_PARAMS();
93         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
94
95         g_array_append_vals(in_param1, &socket_fd, sizeof(int));
96
97         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT,
98                 in_param1, in_param2, in_param3, in_param4, &out_param);
99
100         BT_DBG("result: %x", result);
101
102         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
103
104         return result;
105 }
106
107 BT_EXPORT_API gboolean bluetooth_rfcomm_is_server_uuid_available(const char *uuid)
108 {
109         int result;
110         gboolean available = TRUE;
111         char uuid_str[BLUETOOTH_UUID_STRING_MAX];
112
113         retv_if(uuid == NULL, FALSE);
114         retv_if(bluetooth_check_adapter() ==
115                                 BLUETOOTH_ADAPTER_DISABLED, FALSE);
116
117         BT_INIT_PARAMS();
118         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
119
120         g_strlcpy(uuid_str, uuid, sizeof(uuid_str));
121         g_array_append_vals(in_param1, uuid_str, BLUETOOTH_UUID_STRING_MAX);
122
123         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_IS_UUID_AVAILABLE,
124                 in_param1, in_param2, in_param3, in_param4, &out_param);
125
126         BT_DBG("result: %x", result);
127
128         if (result == BLUETOOTH_ERROR_NONE) {
129                 available = g_array_index(out_param, gboolean, 0);
130         }
131
132         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
133
134         BT_DBG("available: %d", available);
135
136         return available;
137 }
138
139 BT_EXPORT_API int bluetooth_rfcomm_listen_and_accept(int socket_fd, int max_pending_connection)
140 {
141         int result;
142         gboolean native_service = TRUE;
143
144         BT_CHECK_ENABLED(return);
145
146         BT_INIT_PARAMS();
147         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
148
149         g_array_append_vals(in_param1, &socket_fd, sizeof(int));
150         g_array_append_vals(in_param2, &max_pending_connection, sizeof(int));
151         g_array_append_vals(in_param3, &native_service, sizeof(gboolean));
152
153         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
154                 in_param1, in_param2, in_param3, in_param4, &out_param);
155
156         BT_DBG("result: %x", result);
157
158         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
159
160         return result;
161 }
162
163 BT_EXPORT_API int bluetooth_rfcomm_listen(int socket_fd, int max_pending_connection)
164 {
165         int result;
166         gboolean native_service = FALSE;
167
168         BT_CHECK_ENABLED(return);
169
170         BT_INIT_PARAMS();
171         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
172
173         g_array_append_vals(in_param1, &socket_fd, sizeof(int));
174         g_array_append_vals(in_param2, &max_pending_connection, sizeof(int));
175         g_array_append_vals(in_param3, &native_service, sizeof(gboolean));
176
177         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_LISTEN,
178                 in_param1, in_param2, in_param3, in_param4, &out_param);
179
180         BT_DBG("result: %x", result);
181
182         if (result == BLUETOOTH_ERROR_NONE) {
183                 _bt_add_server(socket_fd);
184         }
185
186         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
187
188         return result;
189 }
190
191 BT_EXPORT_API int bluetooth_rfcomm_accept_connection(int server_fd, int *client_fd)
192 {
193         int result;
194
195         BT_CHECK_ENABLED(return);
196
197         BT_INIT_PARAMS();
198         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
199
200         g_array_append_vals(in_param1, &server_fd, sizeof(int));
201
202         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_ACCEPT_CONNECTION,
203                 in_param1, in_param2, in_param3, in_param4, &out_param);
204
205         BT_DBG("result: %x", result);
206
207         if (result == BLUETOOTH_ERROR_NONE) {
208                 *client_fd = g_array_index(out_param, int, 0);
209         }
210
211         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
212
213         BT_DBG("client_fd: %d", *client_fd);
214
215         return result;
216 }
217
218 BT_EXPORT_API int bluetooth_rfcomm_reject_connection(int server_fd)
219 {
220         int result;
221
222         BT_CHECK_ENABLED(return);
223
224         BT_INIT_PARAMS();
225         BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
226
227         g_array_append_vals(in_param1, &server_fd, sizeof(int));
228
229         result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_REJECT_CONNECTION,
230                 in_param1, in_param2, in_param3, in_param4, &out_param);
231
232         BT_DBG("result: %x", result);
233
234         BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
235
236         return result;
237 }
238