4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Hocheol Seo <hocheol.seo@samsung.com>
7 * Girishashok Joshi <girish.joshi@samsung.com>
8 * Chanyeol Park <chanyeol.park@samsung.com>
10 * Licensed under the Apache License, Version 2.0 (the "License");
11 * you may not use this file except in compliance with the License.
12 * You may obtain a copy of the License at
14 * http://www.apache.org/licenses/LICENSE-2.0
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
27 #include <gio/gunixfdlist.h>
30 #include "bluetooth-api.h"
31 #include "bt-internal-types.h"
33 #include "bt-common.h"
34 #include "bt-request-sender.h"
35 #include "bt-event-handler.h"
39 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
40 "the remote application did not send a reply, " \
41 "the message bus security policy blocked the reply, " \
42 "the reply timeout expired, or the network connection " \
45 static GSList *rfcomm_clients;
47 /* Variable for privilege, only for write API,
48 before we should reduce time to bt-service dbus calling
49 -1 : Don't have a permission to access API
50 0 : Initial value, not yet check
51 1 : Have a permission to access API
53 static int privilege_token;
56 char bt_addr[BT_ADDRESS_STRING_SIZE];
59 gboolean disconnected;
63 char uuid[BLUETOOTH_UUID_STRING_MAX];
72 static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
73 char *dev_address, int result);
75 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
80 __rfcomm_delete_id(cb_data->id);
82 if (cb_data->object_id > 0)
83 _bt_unregister_gdbus(cb_data->object_id);
85 if (cb_data->obj_path) {
86 BT_INFO("Unregister profile");
87 _bt_unregister_profile(cb_data->obj_path);
90 if (cb_data->idle_id != 0) {
91 BT_INFO("Removing idle source");
92 g_source_remove(cb_data->idle_id);
95 g_free(cb_data->obj_path);
97 g_free(cb_data->device_path);
102 static void rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
105 BT_INFO("No more device connected remove info");
106 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
107 __bt_free_cb_data(info);
111 gint compare(gpointer *a, gpointer *b)
113 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
114 char *address = (char *)b;
115 return g_strcmp0(node->bt_addr, address);
118 gint compare_fd(gpointer *a, gpointer *b)
120 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
126 static void __bt_free_conn(rfcomm_conn_info_t *conn)
135 if (conn->watch_id > 0) {
136 g_source_remove(conn->watch_id);
144 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
147 rfcomm_conn_info_t *conn_info = NULL;
148 l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
152 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
153 __bt_free_conn(conn_info);
157 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
161 rfcomm_conn_info_t *device_node = NULL;
162 for(l = info->rfcomm_conns; l != NULL; l = l->next) {
163 device_node = l->data;
164 if ( device_node && device_node->fd == fd)
170 static rfcomm_conn_info_t * __get_conn_info_for_disconnection(
171 rfcomm_cb_data_t *info)
174 rfcomm_conn_info_t *device_node = NULL;
175 for(l = info->rfcomm_conns; l != NULL; l = l->next) {
176 device_node = l->data;
177 if (device_node && device_node->disconnected == TRUE)
183 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
187 rfcomm_conn_info_t *conn_info = NULL;
188 l = g_slist_find_custom(info->rfcomm_conns, dev_address,
189 (GCompareFunc)compare);
195 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
196 char *dev_address, int result)
198 __client_connected_cb(info, dev_address, result);
199 __rfcomm_remove_conn_info_t(info, dev_address);
201 if (info->rfcomm_conns == NULL)
202 rfcomm_cb_data_remove(info);
205 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
209 for (l = rfcomm_clients; l != NULL; l = l->next) {
210 rfcomm_cb_data_t *info = l->data;
211 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
212 (GCompareFunc)compare_fd);
220 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
224 for (l = rfcomm_clients; l != NULL; l = l->next) {
225 rfcomm_cb_data_t *info = l->data;
228 if (g_strcmp0(info->obj_path, path) == 0)
235 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
239 for (l = rfcomm_clients; l != NULL; l = l->next) {
240 rfcomm_cb_data_t *info = l->data;
242 if (g_strcmp0(info->uuid, uuid) == 0)
249 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
250 rfcomm_cb_data_t *info)
252 bluetooth_rfcomm_disconnection_t disconn_info;
253 bt_event_info_t *event_info = NULL;
254 if (conn_info->disconnected == FALSE)
256 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
257 if (event_info == NULL) {
258 if (info->rfcomm_conns == NULL)
259 rfcomm_cb_data_remove(info);
262 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
263 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
264 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
265 BT_DBG("Disconnected FD [%d]", conn_info->fd);
266 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
269 disconn_info.socket_fd = conn_info->fd;
271 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
272 BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
273 info->uuid, conn_info->fd);
274 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
275 BLUETOOTH_ERROR_NONE, &disconn_info,
276 event_info->cb, event_info->user_data);
279 BT_DBG("List is present deleting it");
280 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
282 if (info->rfcomm_conns == NULL)
283 rfcomm_cb_data_remove(info);
288 static gboolean __rfcomm_client_disconnect(gpointer user_data)
290 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *) user_data;
291 BT_INFO_C("Disconnected [RFCOMM Client]");
292 retv_if(info == NULL, FALSE);
294 if (g_slist_find(rfcomm_clients, info) == NULL) {
295 BT_INFO("rfcomm resource is already freed");
299 g_slist_foreach(info->rfcomm_conns,
300 (GFunc) _bt_rfcomm_disconnect_conn_info, info);
305 static gboolean __is_error_by_disconnect(GError *err)
307 return !g_strcmp0(err->message, "Connection reset by peer") ||
308 !g_strcmp0(err->message, "Connection timed out") ||
309 !g_strcmp0(err->message, "Software caused connection abort");
312 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
317 int result = BLUETOOTH_ERROR_NONE;
318 rfcomm_cb_data_t *info = data;
319 rfcomm_conn_info_t *conn_info = NULL;
320 bt_event_info_t *event_info;
321 bluetooth_rfcomm_received_data_t data_r;
322 GIOStatus status = G_IO_STATUS_NORMAL;
327 retv_if(info == NULL, FALSE);
328 fd = g_io_channel_unix_get_fd(chan);
329 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
330 BT_ERR_C("RFComm Client disconnected: %d", fd);
331 conn_info = __get_conn_info_from_fd(info, fd);
332 if (conn_info == NULL) {
333 BT_ERR("No Connection info found with FD [%d]", fd);
336 conn_info->disconnected = TRUE;
337 __rfcomm_client_disconnect(info);
341 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
343 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
345 if (status != G_IO_STATUS_NORMAL) {
346 BT_ERR("IO Channel read is failed with %d", status);
350 BT_ERR("IO Channel read error [%s]", err->message);
351 if (status == G_IO_STATUS_ERROR &&
352 __is_error_by_disconnect(err)) {
353 BT_ERR("cond : %d", cond);
355 conn_info = __get_conn_info_from_fd(info, fd);
356 if (conn_info == NULL) {
357 BT_ERR("No Connection info found with FD [%d]", fd);
360 conn_info->disconnected = TRUE;
361 __rfcomm_client_disconnect(info);
369 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
370 if (event_info == NULL) {
375 data_r.socket_fd = fd;
376 data_r.buffer_size = len;
377 data_r.buffer = buffer;
379 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
381 event_info->cb, event_info->user_data);
387 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
390 bluetooth_rfcomm_connection_t conn_info;
391 bt_event_info_t *event_info;
392 rfcomm_conn_info_t *conn_list_info = NULL;
393 BT_INFO_C("Connected [RFCOMM Client]");
395 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
396 if (event_info == NULL)
399 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
400 conn_info.device_role = RFCOMM_ROLE_CLIENT;
401 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
402 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
404 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
405 if (conn_list_info == NULL) {
406 BT_ERR("Device addres %s not found in connection list", dev_address);
409 conn_info.socket_fd = conn_list_info->fd;
410 conn_info.server_id = -1;
412 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
413 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
414 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
416 event_info->cb, event_info->user_data);
422 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
424 rfcomm_cb_data_t *info;
426 rfcomm_conn_info_t *conn_info = NULL;
427 BT_DBG("%s %d", path, fd);
428 char address[BT_ADDRESS_STRING_SIZE];
429 _bt_convert_addr_type_to_string(address,
430 (unsigned char *)addr);
431 info = __find_rfcomm_info_from_path(path);
434 conn_info = __get_conn_info_from_address(info, address);
436 if (conn_info == NULL) {
437 BT_ERR("Device Address %s not found in connection list", address);
441 BT_DBG("connection info fd %d", conn_info->fd);
442 data_io = g_io_channel_unix_new(fd);
443 g_io_channel_set_encoding(data_io, NULL, NULL);
444 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
445 conn_info->watch_id = g_io_add_watch(data_io,
446 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
447 __client_data_received_cb, info);
449 g_io_channel_unref(data_io);
451 __client_connected_cb(info, address,BLUETOOTH_ERROR_NONE);
456 static void __bt_connect_response_cb(DBusGProxy *proxy, DBusGProxyCall *call,
460 GError *error = NULL;
461 rfcomm_cb_data_t *cb_data;
462 char dev_address[BT_ADDRESS_STRING_SIZE];
466 ret_if(user_data == NULL);
470 if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_INVALID)) {
473 BT_ERR("Error : %s \n", error->message);
475 if (g_strcmp0(error->message, "In Progress") == 0)
476 result = BLUETOOTH_ERROR_DEVICE_BUSY;
478 result = BLUETOOTH_ERROR_INTERNAL;
479 path = dbus_g_proxy_get_path(proxy);
480 _bt_convert_device_path_to_address(path, dev_address);
481 __rfcomm_client_connected_cb(cb_data, dev_address, result);
484 g_object_unref(proxy);
489 static void __bt_discover_service_response_cb(DBusGProxy *proxy,
490 DBusGProxyCall *call, gpointer user_data)
492 rfcomm_cb_data_t *cb_data;
495 GHashTable *hash = NULL;
496 bt_register_profile_info_t info = {0};
497 int result = BLUETOOTH_ERROR_NONE;
498 char dev_address[BT_ADDRESS_STRING_SIZE];
503 ret_if(user_data == NULL);
507 path = dbus_g_proxy_get_path(proxy);
508 _bt_convert_device_path_to_address(path, dev_address);
509 BT_DBG("Device Adress [%s]", dev_address);
510 dbus_g_proxy_end_call(proxy, call, &err,
511 dbus_g_type_get_map("GHashTable",
512 G_TYPE_UINT, G_TYPE_STRING), &hash,
514 g_object_unref(proxy);
516 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
517 if (!strcmp("Operation canceled", err->message)) {
518 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
519 } else if (!strcmp("In Progress", err->message)) {
520 result = BLUETOOTH_ERROR_IN_PROGRESS;
521 } else if (!strcmp("Host is down", err->message)) {
522 result = BLUETOOTH_ERROR_HOST_DOWN;
523 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
524 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
525 ret = _bt_cancel_discovers(dev_address);
526 if (ret != BLUETOOTH_ERROR_NONE)
527 BT_ERR("Error: While CancelDiscovery");
529 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
531 __rfcomm_client_connected_cb(cb_data, dev_address, result);
534 BT_INFO("Services are Updated checking required uuid is there");
535 /* Check here for uuid present */
536 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
537 if (ret == BLUETOOTH_ERROR_NONE) {
538 info.uuid = (char *)cb_data->uuid;
539 info.obj_path = cb_data->obj_path;
540 info.role = "client";
542 ret = _bt_register_profile(&info, FALSE);
544 BT_DBG("Error: register profile");
545 ret = _bt_connect_profile(dev_address, cb_data->uuid,
546 __bt_connect_response_cb, cb_data);
548 if (ret != BLUETOOTH_ERROR_NONE) {
549 BT_ERR("ConnectProfile failed");
550 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
551 __rfcomm_client_connected_cb(cb_data, dev_address, result);
555 BT_ERR("remote uuid not found");
556 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
557 __rfcomm_client_connected_cb(cb_data, dev_address, result);
564 g_hash_table_destroy(hash);
567 BT_EXPORT_API int bluetooth_rfcomm_connect(
568 const bluetooth_device_address_t *remote_bt_address,
569 const char *remote_uuid)
573 rfcomm_cb_data_t *cb_data = NULL;
574 rfcomm_conn_info_t *conn = NULL;
578 bt_user_info_t *user_info;
579 char uuid[BLUETOOTH_UUID_STRING_MAX];
581 BT_CHECK_PARAMETER(remote_bt_address, return);
582 BT_CHECK_PARAMETER(remote_uuid, return);
583 BT_CHECK_ENABLED(return);
586 BT_INFO_C("<<<<<<<<< RFCOMM Connect request from app >>>>>>>>>>>");
591 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
592 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
593 BT_ERR("Don't have a privilege to use this API");
594 return BLUETOOTH_ERROR_PERMISSION_DEINED;
597 id = __rfcomm_assign_id();
599 return BLUETOOTH_ERROR_INTERNAL;
601 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
603 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
605 object_id = _bt_register_new_conn(path, new_connection);
607 __rfcomm_delete_id(id);
608 return BLUETOOTH_ERROR_INTERNAL;
611 cb_data = g_new0(rfcomm_cb_data_t, 1);
612 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
613 cb_data->obj_path = path;
614 cb_data->object_id = object_id;
617 conn = g_new0(rfcomm_conn_info_t, 1);
619 _bt_convert_addr_type_to_string(conn->bt_addr,
620 (unsigned char *)remote_bt_address->addr);
622 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
623 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
624 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
625 __bt_discover_service_response_cb, cb_data);
626 if (ret != BLUETOOTH_ERROR_NONE) {
627 BT_ERR("Error returned while service discovery");
628 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
629 if (cb_data->rfcomm_conns == NULL)
630 rfcomm_cb_data_remove(cb_data);
631 return BLUETOOTH_ERROR_INTERNAL;
633 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
634 BT_INFO("Adding callback information to rfcomm_clients");
635 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
637 BT_INFO("Callback information is already added");
639 return BLUETOOTH_ERROR_NONE;
641 user_info = _bt_get_user_data(BT_COMMON);
642 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
644 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
645 /* In now, we only support to connecty using UUID */
646 connect_type = BT_RFCOMM_UUID;
648 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
649 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
650 BT_ERR("Don't have a privilege to use this API");
651 return BLUETOOTH_ERROR_PERMISSION_DEINED;
655 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
657 g_array_append_vals(in_param1, remote_bt_address,
658 sizeof(bluetooth_device_address_t));
660 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
661 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
663 g_array_append_vals(in_param3, &connect_type, sizeof(int));
665 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
666 BT_RFCOMM_CLIENT_CONNECT,
667 in_param1, in_param2,
668 in_param3, in_param4,
669 user_info->cb, user_info->user_data);
671 BT_DBG("result: %x", result);
673 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
679 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(bluetooth_device_address_t *device_address, gboolean *connected)
682 GSList *conn_list = NULL;
683 rfcomm_cb_data_t *client_info;
684 rfcomm_conn_info_t *conn_info;
685 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
687 BT_CHECK_PARAMETER(device_address, return);
688 BT_CHECK_PARAMETER(connected, return);
690 _bt_convert_addr_type_to_string(address, device_address->addr);
693 for (l = rfcomm_clients; l != NULL; l = l->next) {
694 client_info = l->data;
695 if (client_info == NULL)
697 for(conn_list = client_info->rfcomm_conns;
698 conn_list != NULL; conn_list = conn_list->next) {
699 conn_info = conn_list->data;
700 if(conn_info == NULL)
703 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
705 return BLUETOOTH_ERROR_NONE;
710 return BLUETOOTH_ERROR_NONE;
713 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
716 int connected = FALSE;
718 BT_CHECK_ENABLED(return);
721 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
723 result = _bt_send_request(BT_BLUEZ_SERVICE,
724 BT_RFCOMM_CLIENT_IS_CONNECTED,
725 in_param1, in_param2, in_param3,
726 in_param4, &out_param);
728 BT_DBG("result: %x", result);
730 if (result == BLUETOOTH_ERROR_NONE) {
731 connected = g_array_index(out_param,
734 BT_ERR("Fail to send request");
737 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
742 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
745 rfcomm_cb_data_t *info;
746 rfcomm_conn_info_t *conn_info;
747 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
748 BT_CHECK_ENABLED(return);
750 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
751 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
752 BT_ERR("Don't have a privilege to use this API");
753 return BLUETOOTH_ERROR_PERMISSION_DEINED;
757 BT_ERR("Invalid FD");
758 return BLUETOOTH_ERROR_INVALID_PARAM;
760 BT_DBG("FDD %d", socket_fd);
762 info = __find_rfcomm_info_with_fd(socket_fd);
764 BT_DBG("Could not find in client, so check in server");
765 return bluetooth_rfcomm_server_disconnect(socket_fd);
767 conn_info = __get_conn_info_from_fd(info, socket_fd);
768 if (conn_info == NULL) {
769 BT_ERR("FATAL Error");
770 return BLUETOOTH_ERROR_INTERNAL;
772 if (conn_info->watch_id <= 0) {
773 BT_ERR("Invalid state");
774 return BLUETOOTH_ERROR_NOT_CONNECTED;
776 conn_info->disconnected = TRUE;
778 BT_INFO("conn_info %s", conn_info->bt_addr);
779 _bt_disconnect_profile(conn_info->bt_addr, info->uuid, NULL,NULL);
780 if (info->idle_id == 0)
781 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
783 return BLUETOOTH_ERROR_NONE;
786 int service_function;
788 BT_CHECK_ENABLED(return);
791 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
793 /* Support the OSP */
794 if (socket_fd == -1) {
796 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
798 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
799 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
802 result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
803 in_param1, in_param2, in_param3, in_param4, &out_param);
805 BT_DBG("result: %x", result);
807 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
813 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
822 BT_CHECK_PARAMETER(buf, return);
823 #ifndef RFCOMM_DIRECT
824 BT_CHECK_ENABLED(return);
826 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
829 switch (privilege_token) {
831 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
833 if (result == BLUETOOTH_ERROR_NONE) {
834 privilege_token = 1; /* Have a permission */
835 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
836 BT_ERR("Don't have a privilege to use this API");
837 privilege_token = -1; /* Don't have a permission */
838 return BLUETOOTH_ERROR_PERMISSION_DEINED;
840 /* Just break - It is not related with permission error */
844 /* Already have a privilege */
847 return BLUETOOTH_ERROR_PERMISSION_DEINED;
849 /* Invalid privilge token value */
850 return BLUETOOTH_ERROR_INTERNAL;
853 written = write(fd, buf, length);
854 /*BT_DBG("Length %d, written = %d, balance(%d)",
855 length, written, length - written); */
859 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
861 buffer = g_malloc0(length + 1);
863 memcpy(buffer, buf, length);
865 g_array_append_vals(in_param1, &fd, sizeof(int));
866 g_array_append_vals(in_param2, &length, sizeof(int));
867 g_array_append_vals(in_param3, buffer, length);
869 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
870 in_param1, in_param2, in_param3, in_param4, &out_param);
872 BT_DBG("result: %x", result);
874 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);