2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 #include <bluetooth-api.h>
21 #include "bluetooth.h"
22 #include "bluetooth_private.h"
27 #define LOG_TAG "TIZEN_N_BLUETOOTH"
30 int bt_socket_create_rfcomm(const char *uuid, int *socket_fd)
34 BT_CHECK_INIT_STATUS();
35 BT_CHECK_INPUT_PARAMETER(uuid);
36 BT_CHECK_INPUT_PARAMETER(socket_fd);
38 ret = bluetooth_rfcomm_create_socket(uuid);
40 ret = _bt_get_error_code(ret);
41 LOGE("[%s] %s(0x%08x)", __FUNCTION__, _bt_convert_error_to_string(ret), ret);
49 int bt_socket_destroy_rfcomm(int socket_fd)
51 int error_code = BT_ERROR_NONE;
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);
62 int bt_socket_is_service_used(const char* service_uuid, bool *used)
64 BT_CHECK_INIT_STATUS();
65 BT_CHECK_INPUT_PARAMETER(service_uuid);
66 BT_CHECK_INPUT_PARAMETER(used);
68 *used = bluetooth_rfcomm_is_server_uuid_available(service_uuid);
73 int bt_socket_listen_and_accept_rfcomm(int socket_fd, int max_pending_connections)
75 int error_code = BT_ERROR_NONE;
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);
86 int bt_socket_listen(int socket_fd, int max_pending_connections)
88 int error_code = BT_ERROR_NONE;
90 BT_CHECK_INIT_STATUS();
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);
100 int bt_socket_accept(int socket_fd, int *connected_socket_fd)
102 int error_code = BT_ERROR_NONE;
104 BT_CHECK_INIT_STATUS();
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);
111 _bt_unset_cb(BT_EVENT_RFCOMM_CONNECTION_REQUESTED);
116 int bt_socket_reject(int socket_fd)
118 int error_code = BT_ERROR_NONE;
120 BT_CHECK_INIT_STATUS();
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);
130 int bt_socket_connect_rfcomm(const char *remote_address, const char *remote_port_uuid)
132 bluetooth_device_address_t addr_hex = { {0,} };
133 int error_code = BT_ERROR_NONE;
135 BT_CHECK_INIT_STATUS();
136 BT_CHECK_INPUT_PARAMETER(remote_address);
137 BT_CHECK_INPUT_PARAMETER(remote_port_uuid);
139 _bt_convert_address_to_hex(&addr_hex, remote_address);
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);
149 int bt_socket_disconnect_rfcomm(int socket_fd)
151 int ret = BT_ERROR_NONE;
153 BT_CHECK_INIT_STATUS();
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);
163 int bt_socket_send_data(int socket_fd, const char *data, int length)
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;
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);
182 int bt_socket_set_data_received_cb(bt_socket_data_received_cb callback, void *user_data)
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;
190 int bt_socket_unset_data_received_cb(void)
192 BT_CHECK_INIT_STATUS();
193 _bt_unset_cb(BT_EVENT_DATA_RECEIVED);
194 return BT_ERROR_NONE;
197 int bt_socket_set_connection_requested_cb(bt_socket_connection_requested_cb callback, void *user_data)
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;
205 int bt_socket_unset_connection_requested_cb(void)
207 BT_CHECK_INIT_STATUS();
208 _bt_unset_cb(BT_EVENT_RFCOMM_CONNECTION_REQUESTED);
209 return BT_ERROR_NONE;
212 int bt_socket_set_connection_state_changed_cb(bt_socket_connection_state_changed_cb callback, void *user_data)
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;
220 int bt_socket_unset_connection_state_changed_cb(void)
222 BT_CHECK_INIT_STATUS();
223 _bt_unset_cb(BT_EVENT_CONNECTION_STATE_CHANGED);
224 return BT_ERROR_NONE;