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_from_address(rfcomm_cb_data_t *info,
174 rfcomm_conn_info_t *conn_info = NULL;
175 l = g_slist_find_custom(info->rfcomm_conns, dev_address,
176 (GCompareFunc)compare);
182 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
183 char *dev_address, int result)
185 __client_connected_cb(info, dev_address, result);
186 __rfcomm_remove_conn_info_t(info, dev_address);
188 if (info->rfcomm_conns == NULL)
189 rfcomm_cb_data_remove(info);
192 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
196 for (l = rfcomm_clients; l != NULL; l = l->next) {
197 rfcomm_cb_data_t *info = l->data;
198 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
199 (GCompareFunc)compare_fd);
207 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
211 for (l = rfcomm_clients; l != NULL; l = l->next) {
212 rfcomm_cb_data_t *info = l->data;
215 if (g_strcmp0(info->obj_path, path) == 0)
222 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
226 for (l = rfcomm_clients; l != NULL; l = l->next) {
227 rfcomm_cb_data_t *info = l->data;
229 if (g_strcmp0(info->uuid, uuid) == 0)
236 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
237 rfcomm_cb_data_t *info)
239 if (conn_info == NULL)
242 bluetooth_rfcomm_disconnection_t disconn_info;
243 bt_event_info_t *event_info = NULL;
244 if (conn_info->disconnected == FALSE)
246 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
247 if (event_info == NULL) {
248 if (info->rfcomm_conns == NULL)
249 rfcomm_cb_data_remove(info);
252 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
253 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
254 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
255 BT_DBG("Disconnected FD [%d]", conn_info->fd);
256 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
259 disconn_info.socket_fd = conn_info->fd;
261 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
262 BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
263 info->uuid, conn_info->fd);
264 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
265 BLUETOOTH_ERROR_NONE, &disconn_info,
266 event_info->cb, event_info->user_data);
269 BT_DBG("List is present deleting it");
270 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
272 if (info->rfcomm_conns == NULL)
273 rfcomm_cb_data_remove(info);
278 static gboolean __rfcomm_client_disconnect(gpointer user_data)
280 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *) user_data;
281 BT_INFO_C("Disconnected [RFCOMM Client]");
282 retv_if(info == NULL, FALSE);
284 if (g_slist_find(rfcomm_clients, info) == NULL) {
285 BT_INFO("rfcomm resource is already freed");
289 g_slist_foreach(info->rfcomm_conns,
290 (GFunc) _bt_rfcomm_disconnect_conn_info, info);
295 static gboolean __is_error_by_disconnect(GError *err)
297 return !g_strcmp0(err->message, "Connection reset by peer") ||
298 !g_strcmp0(err->message, "Connection timed out") ||
299 !g_strcmp0(err->message, "Software caused connection abort");
302 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
307 int result = BLUETOOTH_ERROR_NONE;
308 rfcomm_cb_data_t *info = data;
309 rfcomm_conn_info_t *conn_info = NULL;
310 bt_event_info_t *event_info;
311 bluetooth_rfcomm_received_data_t data_r;
312 GIOStatus status = G_IO_STATUS_NORMAL;
317 retv_if(info == NULL, FALSE);
318 fd = g_io_channel_unix_get_fd(chan);
319 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
320 BT_ERR_C("RFComm Client disconnected: %d", fd);
321 conn_info = __get_conn_info_from_fd(info, fd);
322 if (conn_info == NULL) {
323 BT_ERR("No Connection info found with FD [%d]", fd);
326 conn_info->disconnected = TRUE;
327 __rfcomm_client_disconnect(info);
331 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
333 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
335 if (status != G_IO_STATUS_NORMAL) {
336 BT_ERR("IO Channel read is failed with %d", status);
340 BT_ERR("IO Channel read error [%s]", err->message);
341 if (status == G_IO_STATUS_ERROR &&
342 __is_error_by_disconnect(err)) {
343 BT_ERR("cond : %d", cond);
345 conn_info = __get_conn_info_from_fd(info, fd);
346 if (conn_info == NULL) {
347 BT_ERR("No Connection info found with FD [%d]", fd);
350 conn_info->disconnected = TRUE;
351 __rfcomm_client_disconnect(info);
359 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
360 if (event_info == NULL) {
365 data_r.socket_fd = fd;
366 data_r.buffer_size = len;
367 data_r.buffer = buffer;
369 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
371 event_info->cb, event_info->user_data);
377 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
380 bluetooth_rfcomm_connection_t conn_info;
381 bt_event_info_t *event_info;
382 rfcomm_conn_info_t *conn_list_info = NULL;
383 BT_INFO_C("Connected [RFCOMM Client]");
385 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
386 if (event_info == NULL)
389 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
390 conn_info.device_role = RFCOMM_ROLE_CLIENT;
391 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
392 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
394 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
395 if (conn_list_info == NULL) {
396 BT_ERR("Device addres %s not found in connection list", dev_address);
399 conn_info.socket_fd = conn_list_info->fd;
400 conn_info.server_id = -1;
402 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
403 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
404 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
406 event_info->cb, event_info->user_data);
412 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
414 rfcomm_cb_data_t *info;
416 rfcomm_conn_info_t *conn_info = NULL;
417 BT_DBG("%s %d", path, fd);
418 char address[BT_ADDRESS_STRING_SIZE];
419 _bt_convert_addr_type_to_string(address,
420 (unsigned char *)addr);
421 info = __find_rfcomm_info_from_path(path);
424 conn_info = __get_conn_info_from_address(info, address);
426 if (conn_info == NULL) {
427 BT_ERR("Device Address %s not found in connection list", address);
431 BT_DBG("connection info fd %d", conn_info->fd);
432 data_io = g_io_channel_unix_new(fd);
433 g_io_channel_set_encoding(data_io, NULL, NULL);
434 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
435 conn_info->watch_id = g_io_add_watch(data_io,
436 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
437 __client_data_received_cb, info);
439 g_io_channel_unref(data_io);
441 __client_connected_cb(info, address,BLUETOOTH_ERROR_NONE);
446 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
450 GError *error = NULL;
451 rfcomm_cb_data_t *cb_data;
452 char dev_address[BT_ADDRESS_STRING_SIZE];
456 ret_if(user_data == NULL);
460 if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
463 BT_ERR("Error : %s \n", error->message);
465 if (g_strcmp0(error->message, "In Progress") == 0)
466 result = BLUETOOTH_ERROR_DEVICE_BUSY;
468 result = BLUETOOTH_ERROR_INTERNAL;
469 path = g_dbus_proxy_get_object_path(proxy);
470 _bt_convert_device_path_to_address(path, dev_address);
471 __rfcomm_client_connected_cb(cb_data, dev_address, result);
476 g_object_unref(proxy);
481 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
482 GAsyncResult *res, gpointer user_data)
484 rfcomm_cb_data_t *cb_data;
487 bt_register_profile_info_t info = {0};
488 int result = BLUETOOTH_ERROR_NONE;
489 char dev_address[BT_ADDRESS_STRING_SIZE];
494 ret_if(user_data == NULL);
498 path = g_dbus_proxy_get_object_path(proxy);
500 _bt_convert_device_path_to_address(path, dev_address);
501 BT_DBG("Device Adress [%s]", dev_address);
502 g_dbus_proxy_call_finish(proxy, res, &err);
504 g_object_unref(proxy);
507 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
508 if (!strcmp("Operation canceled", err->message)) {
509 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
510 } else if (!strcmp("In Progress", err->message)) {
511 result = BLUETOOTH_ERROR_IN_PROGRESS;
512 } else if (!strcmp("Host is down", err->message)) {
513 result = BLUETOOTH_ERROR_HOST_DOWN;
514 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
515 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
516 ret = _bt_cancel_discovers(dev_address);
517 if (ret != BLUETOOTH_ERROR_NONE)
518 BT_ERR("Error: While CancelDiscovery");
520 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
522 __rfcomm_client_connected_cb(cb_data, dev_address, result);
525 BT_INFO("Services are Updated checking required uuid is there");
526 /* Check here for uuid present */
527 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
528 if (ret == BLUETOOTH_ERROR_NONE) {
529 info.uuid = (char *)cb_data->uuid;
530 info.obj_path = cb_data->obj_path;
531 info.role = "client";
533 ret = _bt_register_profile(&info, FALSE);
535 BT_DBG("Error: register profile");
536 ret = _bt_connect_profile(dev_address, cb_data->uuid,
537 __bt_connect_response_cb, cb_data);
539 if (ret != BLUETOOTH_ERROR_NONE) {
540 BT_ERR("ConnectProfile failed");
541 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
542 __rfcomm_client_connected_cb(cb_data, dev_address, result);
546 BT_ERR("remote uuid not found");
547 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
548 __rfcomm_client_connected_cb(cb_data, dev_address, result);
556 BT_EXPORT_API int bluetooth_rfcomm_connect(
557 const bluetooth_device_address_t *remote_bt_address,
558 const char *remote_uuid)
562 rfcomm_cb_data_t *cb_data = NULL;
563 rfcomm_conn_info_t *conn = NULL;
567 bt_user_info_t *user_info;
568 char uuid[BLUETOOTH_UUID_STRING_MAX];
570 BT_CHECK_PARAMETER(remote_bt_address, return);
571 BT_CHECK_PARAMETER(remote_uuid, return);
572 BT_CHECK_ENABLED(return);
575 BT_INFO_C("<<<<<<<<< RFCOMM Connect request from app >>>>>>>>>>>");
580 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
581 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
582 BT_ERR("Don't have a privilege to use this API");
583 return BLUETOOTH_ERROR_PERMISSION_DEINED;
586 id = __rfcomm_assign_id();
588 return BLUETOOTH_ERROR_INTERNAL;
590 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
592 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
594 object_id = _bt_register_new_conn(path, new_connection);
596 __rfcomm_delete_id(id);
597 return BLUETOOTH_ERROR_INTERNAL;
600 cb_data = g_new0(rfcomm_cb_data_t, 1);
601 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
602 cb_data->obj_path = path;
603 cb_data->object_id = object_id;
606 conn = g_new0(rfcomm_conn_info_t, 1);
608 _bt_convert_addr_type_to_string(conn->bt_addr,
609 (unsigned char *)remote_bt_address->addr);
611 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
612 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
613 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
614 __bt_discover_service_response_cb, cb_data);
615 if (ret != BLUETOOTH_ERROR_NONE) {
616 BT_ERR("Error returned while service discovery");
617 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
618 if (cb_data->rfcomm_conns == NULL)
619 rfcomm_cb_data_remove(cb_data);
620 return BLUETOOTH_ERROR_INTERNAL;
622 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
623 BT_INFO("Adding callback information to rfcomm_clients");
624 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
626 BT_INFO("Callback information is already added");
628 return BLUETOOTH_ERROR_NONE;
630 user_info = _bt_get_user_data(BT_COMMON);
631 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
633 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
634 /* In now, we only support to connecty using UUID */
635 connect_type = BT_RFCOMM_UUID;
637 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
638 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
639 BT_ERR("Don't have a privilege to use this API");
640 return BLUETOOTH_ERROR_PERMISSION_DEINED;
644 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
646 g_array_append_vals(in_param1, remote_bt_address,
647 sizeof(bluetooth_device_address_t));
649 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
650 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
652 g_array_append_vals(in_param3, &connect_type, sizeof(int));
654 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
655 BT_RFCOMM_CLIENT_CONNECT,
656 in_param1, in_param2,
657 in_param3, in_param4,
658 user_info->cb, user_info->user_data);
660 BT_DBG("result: %x", result);
662 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
668 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
671 GSList *conn_list = NULL;
672 rfcomm_cb_data_t *client_info;
673 rfcomm_conn_info_t *conn_info;
674 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
676 BT_CHECK_PARAMETER(device_address, return);
677 BT_CHECK_PARAMETER(connected, return);
679 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
682 for (l = rfcomm_clients; l != NULL; l = l->next) {
683 client_info = l->data;
684 if (client_info == NULL)
686 for(conn_list = client_info->rfcomm_conns;
687 conn_list != NULL; conn_list = conn_list->next) {
688 conn_info = conn_list->data;
689 if(conn_info == NULL)
692 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
694 return BLUETOOTH_ERROR_NONE;
699 return BLUETOOTH_ERROR_NONE;
702 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
705 int connected = FALSE;
707 BT_CHECK_ENABLED(return);
710 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
712 result = _bt_send_request(BT_BLUEZ_SERVICE,
713 BT_RFCOMM_CLIENT_IS_CONNECTED,
714 in_param1, in_param2, in_param3,
715 in_param4, &out_param);
717 BT_DBG("result: %x", result);
719 if (result == BLUETOOTH_ERROR_NONE) {
720 connected = g_array_index(out_param,
723 BT_ERR("Fail to send request");
726 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
731 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
734 rfcomm_cb_data_t *info;
735 rfcomm_conn_info_t *conn_info;
736 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
737 BT_CHECK_ENABLED(return);
739 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
740 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
741 BT_ERR("Don't have a privilege to use this API");
742 return BLUETOOTH_ERROR_PERMISSION_DEINED;
746 BT_ERR("Invalid FD");
747 return BLUETOOTH_ERROR_INVALID_PARAM;
749 BT_DBG("FDD %d", socket_fd);
751 info = __find_rfcomm_info_with_fd(socket_fd);
753 BT_DBG("Could not find in client, so check in server");
754 return bluetooth_rfcomm_server_disconnect(socket_fd);
756 conn_info = __get_conn_info_from_fd(info, socket_fd);
757 if (conn_info == NULL) {
758 BT_ERR("FATAL Error");
759 return BLUETOOTH_ERROR_INTERNAL;
761 if (conn_info->watch_id <= 0) {
762 BT_ERR("Invalid state");
763 return BLUETOOTH_ERROR_NOT_CONNECTED;
765 conn_info->disconnected = TRUE;
767 BT_INFO("conn_info %s", conn_info->bt_addr);
768 _bt_disconnect_profile(conn_info->bt_addr, info->uuid, NULL,NULL);
769 if (info->idle_id == 0)
770 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
772 return BLUETOOTH_ERROR_NONE;
775 int service_function;
777 BT_CHECK_ENABLED(return);
780 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
782 /* Support the OSP */
783 if (socket_fd == -1) {
785 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
787 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
788 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
791 result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
792 in_param1, in_param2, in_param3, in_param4, &out_param);
794 BT_DBG("result: %x", result);
796 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
802 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
811 BT_CHECK_PARAMETER(buf, return);
812 #ifndef RFCOMM_DIRECT
813 BT_CHECK_ENABLED(return);
815 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
818 switch (privilege_token) {
820 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
822 if (result == BLUETOOTH_ERROR_NONE) {
823 privilege_token = 1; /* Have a permission */
824 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
825 BT_ERR("Don't have a privilege to use this API");
826 privilege_token = -1; /* Don't have a permission */
827 return BLUETOOTH_ERROR_PERMISSION_DEINED;
829 /* Just break - It is not related with permission error */
833 /* Already have a privilege */
836 return BLUETOOTH_ERROR_PERMISSION_DEINED;
838 /* Invalid privilge token value */
839 return BLUETOOTH_ERROR_INTERNAL;
842 written = write(fd, buf, length);
843 /*BT_DBG("Length %d, written = %d, balance(%d)",
844 length, written, length - written); */
848 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
850 buffer = g_malloc0(length + 1);
852 memcpy(buffer, buf, length);
854 g_array_append_vals(in_param1, &fd, sizeof(int));
855 g_array_append_vals(in_param2, &length, sizeof(int));
856 g_array_append_vals(in_param3, buffer, length);
858 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
859 in_param1, in_param2, in_param3, in_param4, &out_param);
861 BT_DBG("result: %x", result);
863 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);