62296a230b89749c598feb180bdd722efc7adcd7
[framework/api/bluetooth.git] / src / bluetooth-socket.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the License);
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an AS IS BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #include <dlog.h>
18 #include <stdio.h>
19 #include <bluetooth-api.h>
20
21 #include "bluetooth.h"
22 #include "bluetooth_private.h"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27 #define LOG_TAG "TIZEN_N_BLUETOOTH"
28
29
30 int bt_socket_create_rfcomm(const char *uuid, int *socket_fd)
31 {
32         int ret = 0;
33
34         BT_CHECK_INIT_STATUS();
35         BT_CHECK_INPUT_PARAMETER(uuid);
36         BT_CHECK_INPUT_PARAMETER(socket_fd);
37
38         ret = bluetooth_rfcomm_create_socket(uuid);
39         if (ret < 0) {
40                 ret = _bt_get_error_code(ret);
41                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(ret), ret);
42                 return ret;
43         } else {
44                 *socket_fd = ret;
45                 return BT_ERROR_NONE;
46         }
47 }
48
49 int bt_socket_destroy_rfcomm(int socket_fd)
50 {
51         int error_code = BT_ERROR_NONE;
52
53         BT_CHECK_INIT_STATUS();
54         error_code = _bt_get_error_code(bluetooth_rfcomm_remove_socket(socket_fd));
55         if (error_code != BT_ERROR_NONE) {
56                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(error_code), error_code);
57         }
58
59         return error_code;
60 }
61
62 int bt_socket_is_service_used(const char* service_uuid, bool *used)
63 {
64         BT_CHECK_INIT_STATUS();
65         BT_CHECK_INPUT_PARAMETER(service_uuid);
66         BT_CHECK_INPUT_PARAMETER(used);
67
68         *used = bluetooth_rfcomm_is_server_uuid_available(service_uuid);
69
70         return BT_ERROR_NONE;
71 }
72
73 int bt_socket_listen_and_accept_rfcomm(int socket_fd, int max_pending_connections)
74 {
75         int error_code = BT_ERROR_NONE;
76
77         BT_CHECK_INIT_STATUS();
78         error_code = _bt_get_error_code(bluetooth_rfcomm_listen_and_accept(socket_fd, max_pending_connections));
79         if (error_code != BT_ERROR_NONE) {
80                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(error_code), error_code);
81         }
82
83         return error_code;
84 }
85
86 int bt_socket_listen(int socket_fd, int max_pending_connections)
87 {
88         int error_code = BT_ERROR_NONE;
89
90         BT_CHECK_INIT_STATUS();
91
92         error_code = _bt_get_error_code(bluetooth_rfcomm_listen(socket_fd, max_pending_connections));
93         if (error_code != BT_ERROR_NONE) {
94                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(error_code), error_code);
95         }
96
97         return error_code;
98 }
99
100 int bt_socket_accept(int socket_fd, int *connected_socket_fd)
101 {
102         int error_code = BT_ERROR_NONE;
103
104         BT_CHECK_INIT_STATUS();
105
106         error_code = _bt_get_error_code(bluetooth_rfcomm_accept_connection(socket_fd, connected_socket_fd));
107         if (error_code != BT_ERROR_NONE) {
108                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(error_code), error_code);
109         }
110
111         _bt_unset_cb(BT_EVENT_RFCOMM_CONNECTION_REQUESTED);
112
113         return error_code;
114 }
115
116 int bt_socket_reject(int socket_fd)
117 {
118         int error_code = BT_ERROR_NONE;
119
120         BT_CHECK_INIT_STATUS();
121
122         error_code = _bt_get_error_code(bluetooth_rfcomm_reject_connection(socket_fd));
123         if (error_code != BT_ERROR_NONE) {
124                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(error_code), error_code);
125         }
126
127         return error_code;
128 }
129
130 int bt_socket_connect_rfcomm(const char *remote_address, const char *remote_port_uuid)
131 {
132         bluetooth_device_address_t addr_hex = { {0,} };
133         int error_code = BT_ERROR_NONE;
134
135         BT_CHECK_INIT_STATUS();
136         BT_CHECK_INPUT_PARAMETER(remote_address);
137         BT_CHECK_INPUT_PARAMETER(remote_port_uuid);
138
139         _bt_convert_address_to_hex(&addr_hex, remote_address);
140
141         error_code = _bt_get_error_code(bluetooth_rfcomm_connect(&addr_hex, remote_port_uuid));
142         if (error_code != BT_ERROR_NONE) {
143                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(error_code), error_code);
144         }
145
146         return error_code;
147 }
148
149 int bt_socket_disconnect_rfcomm(int socket_fd)
150 {
151         int ret = BT_ERROR_NONE;
152
153         BT_CHECK_INIT_STATUS();
154
155         ret = _bt_get_error_code(bluetooth_rfcomm_disconnect(socket_fd));
156         if (ret != BT_ERROR_NONE) {
157                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(ret), ret);
158         }
159
160         return ret;
161 }
162
163 int bt_socket_send_data(int socket_fd, const char *data, int length)
164 {
165         int ret = 0;
166
167         BT_CHECK_INIT_STATUS();
168         ret = bluetooth_rfcomm_write(socket_fd, data, length);
169         if (ret == BLUETOOTH_ERROR_NOT_IN_OPERATION) {
170                 LOGE("[%s] OPERATION_FAILED(0x%08x)", __FUNCTION__, BT_ERROR_OPERATION_FAILED);
171                 return BT_ERROR_OPERATION_FAILED;
172         }
173
174         ret = _bt_get_error_code(ret);
175         if (ret != BT_ERROR_NONE) {
176                 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(ret), ret);
177         }
178
179         return ret;
180 }
181
182 int bt_socket_set_data_received_cb(bt_socket_data_received_cb callback, void *user_data)
183 {
184         BT_CHECK_INIT_STATUS();
185         BT_CHECK_INPUT_PARAMETER(callback);
186         _bt_set_cb(BT_EVENT_DATA_RECEIVED, callback, user_data);
187         return BT_ERROR_NONE;
188 }
189
190 int bt_socket_unset_data_received_cb(void)
191 {
192         BT_CHECK_INIT_STATUS();
193         _bt_unset_cb(BT_EVENT_DATA_RECEIVED);
194         return BT_ERROR_NONE;
195 }
196
197 int bt_socket_set_connection_requested_cb(bt_socket_connection_requested_cb callback, void *user_data)
198 {
199         BT_CHECK_INIT_STATUS();
200         BT_CHECK_INPUT_PARAMETER(callback);
201         _bt_set_cb(BT_EVENT_RFCOMM_CONNECTION_REQUESTED, callback, user_data);
202         return BT_ERROR_NONE;
203 }
204
205 int bt_socket_unset_connection_requested_cb(void)
206 {
207         BT_CHECK_INIT_STATUS();
208         _bt_unset_cb(BT_EVENT_RFCOMM_CONNECTION_REQUESTED);
209         return BT_ERROR_NONE;
210 }
211
212 int bt_socket_set_connection_state_changed_cb(bt_socket_connection_state_changed_cb callback, void *user_data)
213 {
214         BT_CHECK_INIT_STATUS();
215         BT_CHECK_INPUT_PARAMETER(callback);
216         _bt_set_cb(BT_EVENT_CONNECTION_STATE_CHANGED, callback, user_data);
217         return BT_ERROR_NONE;
218 }
219
220 int bt_socket_unset_connection_state_changed_cb(void)
221 {
222         BT_CHECK_INIT_STATUS();
223         _bt_unset_cb(BT_EVENT_CONNECTION_STATE_CHANGED);
224         return BT_ERROR_NONE;
225 }
226