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.
21 #include <gio/gunixfdlist.h>
24 #include "bluetooth-api.h"
25 #include "bt-internal-types.h"
27 #include "bt-common.h"
28 #include "bt-request-sender.h"
29 #include "bt-event-handler.h"
33 #define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
34 "the remote application did not send a reply, " \
35 "the message bus security policy blocked the reply, " \
36 "the reply timeout expired, or the network connection " \
39 static GSList *rfcomm_clients;
41 /* Variable for privilege, only for write API,
42 before we should reduce time to bt-service dbus calling
43 -1 : Don't have a permission to access API
44 0 : Initial value, not yet check
45 1 : Have a permission to access API
47 static int privilege_token;
50 char bt_addr[BT_ADDRESS_STRING_SIZE];
53 gboolean disconnected;
57 char uuid[BLUETOOTH_UUID_STRING_MAX];
66 static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
67 char *dev_address, int result);
69 static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
74 __rfcomm_delete_id(cb_data->id);
76 if (cb_data->object_id > 0)
77 _bt_unregister_gdbus(cb_data->object_id);
79 if (cb_data->obj_path) {
80 BT_INFO("Unregister profile");
81 _bt_unregister_profile(cb_data->obj_path);
84 if (cb_data->idle_id != 0) {
85 BT_INFO("Removing idle source");
86 g_source_remove(cb_data->idle_id);
89 g_free(cb_data->obj_path);
91 g_free(cb_data->device_path);
96 static void rfcomm_cb_data_remove(rfcomm_cb_data_t *info)
99 BT_INFO("No more device connected remove info");
100 rfcomm_clients = g_slist_remove(rfcomm_clients, info);
101 __bt_free_cb_data(info);
105 gint compare(gpointer *a, gpointer *b)
107 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
108 char *address = (char *)b;
109 return g_strcmp0(node->bt_addr, address);
112 gint compare_fd(gpointer *a, gpointer *b)
114 rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
120 static void __bt_free_conn(rfcomm_conn_info_t *conn)
129 if (conn->watch_id > 0) {
130 g_source_remove(conn->watch_id);
138 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
141 rfcomm_conn_info_t *conn_info = NULL;
142 l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
146 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
147 __bt_free_conn(conn_info);
151 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
155 rfcomm_conn_info_t *device_node = NULL;
156 for(l = info->rfcomm_conns; l != NULL; l = l->next) {
157 device_node = l->data;
158 if ( device_node && device_node->fd == fd)
164 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
168 rfcomm_conn_info_t *conn_info = NULL;
169 l = g_slist_find_custom(info->rfcomm_conns, dev_address,
170 (GCompareFunc)compare);
176 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
177 char *dev_address, int result)
179 __client_connected_cb(info, dev_address, result);
180 __rfcomm_remove_conn_info_t(info, dev_address);
182 if (info->rfcomm_conns == NULL)
183 rfcomm_cb_data_remove(info);
186 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
190 for (l = rfcomm_clients; l != NULL; l = l->next) {
191 rfcomm_cb_data_t *info = l->data;
192 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
193 (GCompareFunc)compare_fd);
201 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
205 for (l = rfcomm_clients; l != NULL; l = l->next) {
206 rfcomm_cb_data_t *info = l->data;
209 if (g_strcmp0(info->obj_path, path) == 0)
216 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
220 for (l = rfcomm_clients; l != NULL; l = l->next) {
221 rfcomm_cb_data_t *info = l->data;
223 if (g_strcmp0(info->uuid, uuid) == 0)
230 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
231 rfcomm_cb_data_t *info)
233 if (conn_info == NULL)
236 bluetooth_rfcomm_disconnection_t disconn_info;
237 bt_event_info_t *event_info = NULL;
238 if (conn_info->disconnected == FALSE)
240 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
241 if (event_info == NULL) {
242 if (info->rfcomm_conns == NULL)
243 rfcomm_cb_data_remove(info);
246 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
247 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
248 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
249 BT_DBG("Disconnected FD [%d]", conn_info->fd);
250 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
253 disconn_info.socket_fd = conn_info->fd;
255 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
256 BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
257 info->uuid, conn_info->fd);
258 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
259 BLUETOOTH_ERROR_NONE, &disconn_info,
260 event_info->cb, event_info->user_data);
263 BT_DBG("List is present deleting it");
264 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
266 if (info->rfcomm_conns == NULL)
267 rfcomm_cb_data_remove(info);
272 static gboolean __rfcomm_client_disconnect(gpointer user_data)
274 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *) user_data;
275 BT_INFO_C("Disconnected [RFCOMM Client]");
276 retv_if(info == NULL, FALSE);
278 if (g_slist_find(rfcomm_clients, info) == NULL) {
279 BT_INFO("rfcomm resource is already freed");
283 g_slist_foreach(info->rfcomm_conns,
284 (GFunc) _bt_rfcomm_disconnect_conn_info, info);
289 static gboolean __is_error_by_disconnect(GError *err)
291 return !g_strcmp0(err->message, "Connection reset by peer") ||
292 !g_strcmp0(err->message, "Connection timed out") ||
293 !g_strcmp0(err->message, "Software caused connection abort");
296 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
301 int result = BLUETOOTH_ERROR_NONE;
302 rfcomm_cb_data_t *info = data;
303 rfcomm_conn_info_t *conn_info = NULL;
304 bt_event_info_t *event_info;
305 bluetooth_rfcomm_received_data_t data_r;
306 GIOStatus status = G_IO_STATUS_NORMAL;
311 retv_if(info == NULL, FALSE);
312 fd = g_io_channel_unix_get_fd(chan);
313 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
314 BT_ERR_C("RFComm Client disconnected: %d", fd);
315 conn_info = __get_conn_info_from_fd(info, fd);
316 if (conn_info == NULL) {
317 BT_ERR("No Connection info found with FD [%d]", fd);
320 conn_info->disconnected = TRUE;
321 __rfcomm_client_disconnect(info);
325 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
327 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
329 if (status != G_IO_STATUS_NORMAL) {
330 BT_ERR("IO Channel read is failed with %d", status);
334 BT_ERR("IO Channel read error [%s]", err->message);
335 if (status == G_IO_STATUS_ERROR &&
336 __is_error_by_disconnect(err)) {
337 BT_ERR("cond : %d", cond);
339 conn_info = __get_conn_info_from_fd(info, fd);
340 if (conn_info == NULL) {
341 BT_ERR("No Connection info found with FD [%d]", fd);
344 conn_info->disconnected = TRUE;
345 __rfcomm_client_disconnect(info);
353 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
354 if (event_info == NULL) {
359 data_r.socket_fd = fd;
360 data_r.buffer_size = len;
361 data_r.buffer = buffer;
363 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
365 event_info->cb, event_info->user_data);
371 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
374 bluetooth_rfcomm_connection_t conn_info;
375 bt_event_info_t *event_info;
376 rfcomm_conn_info_t *conn_list_info = NULL;
377 BT_INFO_C("Connected [RFCOMM Client]");
379 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
380 if (event_info == NULL)
383 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
384 conn_info.device_role = RFCOMM_ROLE_CLIENT;
385 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
386 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
388 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
389 if (conn_list_info == NULL) {
390 BT_ERR("Device addres %s not found in connection list", dev_address);
393 conn_info.socket_fd = conn_list_info->fd;
394 conn_info.server_id = -1;
396 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
397 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
398 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
400 event_info->cb, event_info->user_data);
406 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
408 rfcomm_cb_data_t *info;
410 rfcomm_conn_info_t *conn_info = NULL;
411 BT_DBG("%s %d", path, fd);
412 char address[BT_ADDRESS_STRING_SIZE];
413 _bt_convert_addr_type_to_string(address,
414 (unsigned char *)addr);
415 info = __find_rfcomm_info_from_path(path);
418 conn_info = __get_conn_info_from_address(info, address);
420 if (conn_info == NULL) {
421 BT_ERR("Device Address %s not found in connection list", address);
425 BT_DBG("connection info fd %d", conn_info->fd);
426 data_io = g_io_channel_unix_new(fd);
427 g_io_channel_set_encoding(data_io, NULL, NULL);
428 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
429 conn_info->watch_id = g_io_add_watch(data_io,
430 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
431 __client_data_received_cb, info);
433 g_io_channel_unref(data_io);
435 __client_connected_cb(info, address,BLUETOOTH_ERROR_NONE);
440 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
444 GError *error = NULL;
445 rfcomm_cb_data_t *cb_data;
446 char dev_address[BT_ADDRESS_STRING_SIZE];
450 ret_if(user_data == NULL);
454 if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
456 g_dbus_error_strip_remote_error(error);
457 BT_ERR("Error : %s \n", error->message);
459 if (g_strcmp0(error->message, "In Progress") == 0)
460 result = BLUETOOTH_ERROR_DEVICE_BUSY;
462 result = BLUETOOTH_ERROR_INTERNAL;
463 path = g_dbus_proxy_get_object_path(proxy);
464 _bt_convert_device_path_to_address(path, dev_address);
465 __rfcomm_client_connected_cb(cb_data, dev_address, result);
470 g_object_unref(proxy);
475 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
476 GAsyncResult *res, gpointer user_data)
478 rfcomm_cb_data_t *cb_data;
481 bt_register_profile_info_t info = {0};
482 int result = BLUETOOTH_ERROR_NONE;
483 char dev_address[BT_ADDRESS_STRING_SIZE];
488 ret_if(user_data == NULL);
492 path = g_dbus_proxy_get_object_path(proxy);
494 _bt_convert_device_path_to_address(path, dev_address);
495 BT_DBG("Device Adress [%s]", dev_address);
496 g_dbus_proxy_call_finish(proxy, res, &err);
498 g_object_unref(proxy);
501 g_dbus_error_strip_remote_error(err);
502 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
503 if (!strcmp("Operation canceled", err->message)) {
504 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
505 } else if (!strcmp("In Progress", err->message)) {
506 result = BLUETOOTH_ERROR_IN_PROGRESS;
507 } else if (!strcmp("Host is down", err->message)) {
508 result = BLUETOOTH_ERROR_HOST_DOWN;
509 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
510 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
511 ret = _bt_cancel_discovers(dev_address);
512 if (ret != BLUETOOTH_ERROR_NONE)
513 BT_ERR("Error: While CancelDiscovery");
515 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
517 __rfcomm_client_connected_cb(cb_data, dev_address, result);
520 BT_INFO("Services are Updated checking required uuid is there");
521 /* Check here for uuid present */
522 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
523 if (ret == BLUETOOTH_ERROR_NONE) {
524 info.uuid = (char *)cb_data->uuid;
525 info.obj_path = cb_data->obj_path;
526 info.role = "client";
528 ret = _bt_register_profile(&info, FALSE);
530 BT_DBG("Error: register profile");
531 ret = _bt_connect_profile(dev_address, cb_data->uuid,
532 __bt_connect_response_cb, cb_data);
534 if (ret != BLUETOOTH_ERROR_NONE) {
535 BT_ERR("ConnectProfile failed");
536 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
537 __rfcomm_client_connected_cb(cb_data, dev_address, result);
541 BT_ERR("remote uuid not found");
542 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
543 __rfcomm_client_connected_cb(cb_data, dev_address, result);
551 BT_EXPORT_API int bluetooth_rfcomm_connect(
552 const bluetooth_device_address_t *remote_bt_address,
553 const char *remote_uuid)
557 rfcomm_cb_data_t *cb_data = NULL;
558 rfcomm_conn_info_t *conn = NULL;
562 bt_user_info_t *user_info;
563 char uuid[BLUETOOTH_UUID_STRING_MAX];
565 BT_CHECK_PARAMETER(remote_bt_address, return);
566 BT_CHECK_PARAMETER(remote_uuid, return);
567 BT_CHECK_ENABLED(return);
570 BT_INFO_C("<<<<<<<<< RFCOMM Connect request from app >>>>>>>>>>>");
575 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
576 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
577 BT_ERR("Don't have a privilege to use this API");
578 return BLUETOOTH_ERROR_PERMISSION_DEINED;
581 id = __rfcomm_assign_id();
583 return BLUETOOTH_ERROR_INTERNAL;
585 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
587 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
589 object_id = _bt_register_new_conn(path, new_connection);
591 __rfcomm_delete_id(id);
592 return BLUETOOTH_ERROR_INTERNAL;
595 cb_data = g_new0(rfcomm_cb_data_t, 1);
596 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
597 cb_data->obj_path = path;
598 cb_data->object_id = object_id;
601 conn = g_new0(rfcomm_conn_info_t, 1);
603 _bt_convert_addr_type_to_string(conn->bt_addr,
604 (unsigned char *)remote_bt_address->addr);
606 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
607 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
608 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
609 __bt_discover_service_response_cb, cb_data);
610 if (ret != BLUETOOTH_ERROR_NONE) {
611 BT_ERR("Error returned while service discovery");
612 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
613 if (cb_data->rfcomm_conns == NULL)
614 rfcomm_cb_data_remove(cb_data);
615 return BLUETOOTH_ERROR_INTERNAL;
617 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
618 BT_INFO("Adding callback information to rfcomm_clients");
619 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
621 BT_INFO("Callback information is already added");
623 return BLUETOOTH_ERROR_NONE;
625 user_info = _bt_get_user_data(BT_COMMON);
626 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
628 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
629 /* In now, we only support to connecty using UUID */
630 connect_type = BT_RFCOMM_UUID;
632 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
633 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
634 BT_ERR("Don't have a privilege to use this API");
635 return BLUETOOTH_ERROR_PERMISSION_DEINED;
639 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
641 g_array_append_vals(in_param1, remote_bt_address,
642 sizeof(bluetooth_device_address_t));
644 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
645 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
647 g_array_append_vals(in_param3, &connect_type, sizeof(int));
649 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
650 BT_RFCOMM_CLIENT_CONNECT,
651 in_param1, in_param2,
652 in_param3, in_param4,
653 user_info->cb, user_info->user_data);
655 BT_DBG("result: %x", result);
657 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
663 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
666 GSList *conn_list = NULL;
667 rfcomm_cb_data_t *client_info;
668 rfcomm_conn_info_t *conn_info;
669 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
671 BT_CHECK_PARAMETER(device_address, return);
672 BT_CHECK_PARAMETER(connected, return);
674 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
677 for (l = rfcomm_clients; l != NULL; l = l->next) {
678 client_info = l->data;
679 if (client_info == NULL)
681 for(conn_list = client_info->rfcomm_conns;
682 conn_list != NULL; conn_list = conn_list->next) {
683 conn_info = conn_list->data;
684 if(conn_info == NULL)
687 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
689 return BLUETOOTH_ERROR_NONE;
694 return BLUETOOTH_ERROR_NONE;
697 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
700 int connected = FALSE;
702 BT_CHECK_ENABLED(return);
705 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
707 result = _bt_send_request(BT_BLUEZ_SERVICE,
708 BT_RFCOMM_CLIENT_IS_CONNECTED,
709 in_param1, in_param2, in_param3,
710 in_param4, &out_param);
712 BT_DBG("result: %x", result);
714 if (result == BLUETOOTH_ERROR_NONE) {
715 connected = g_array_index(out_param,
718 BT_ERR("Fail to send request");
721 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
726 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
729 rfcomm_cb_data_t *info;
730 rfcomm_conn_info_t *conn_info;
731 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
732 BT_CHECK_ENABLED(return);
734 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
735 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
736 BT_ERR("Don't have a privilege to use this API");
737 return BLUETOOTH_ERROR_PERMISSION_DEINED;
741 BT_ERR("Invalid FD");
742 return BLUETOOTH_ERROR_INVALID_PARAM;
744 BT_DBG("FDD %d", socket_fd);
746 info = __find_rfcomm_info_with_fd(socket_fd);
748 BT_DBG("Could not find in client, so check in server");
749 return bluetooth_rfcomm_server_disconnect(socket_fd);
751 conn_info = __get_conn_info_from_fd(info, socket_fd);
752 if (conn_info == NULL) {
753 BT_ERR("FATAL Error");
754 return BLUETOOTH_ERROR_INTERNAL;
756 if (conn_info->watch_id <= 0) {
757 BT_ERR("Invalid state");
758 return BLUETOOTH_ERROR_NOT_CONNECTED;
760 conn_info->disconnected = TRUE;
762 BT_INFO("conn_info %s", conn_info->bt_addr);
763 _bt_disconnect_profile(conn_info->bt_addr, info->uuid, NULL,NULL);
764 if (info->idle_id == 0)
765 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
767 return BLUETOOTH_ERROR_NONE;
770 int service_function;
772 BT_CHECK_ENABLED(return);
775 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
777 /* Support the OSP */
778 if (socket_fd == -1) {
780 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
782 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
783 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
786 result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
787 in_param1, in_param2, in_param3, in_param4, &out_param);
789 BT_DBG("result: %x", result);
791 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
797 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
806 BT_CHECK_PARAMETER(buf, return);
808 BT_ERR("Invalid FD");
809 return BLUETOOTH_ERROR_INVALID_PARAM;
812 BT_DBG("FD : %d", fd);
814 #ifndef RFCOMM_DIRECT
815 BT_CHECK_ENABLED(return);
817 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
820 switch (privilege_token) {
822 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
824 if (result == BLUETOOTH_ERROR_NONE) {
825 privilege_token = 1; /* Have a permission */
826 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
827 BT_ERR("Don't have a privilege to use this API");
828 privilege_token = -1; /* Don't have a permission */
829 return BLUETOOTH_ERROR_PERMISSION_DEINED;
831 /* Just break - It is not related with permission error */
835 /* Already have a privilege */
838 return BLUETOOTH_ERROR_PERMISSION_DEINED;
840 /* Invalid privilge token value */
841 return BLUETOOTH_ERROR_INTERNAL;
844 written = write(fd, buf, length);
845 /*BT_DBG("Length %d, written = %d, balance(%d)",
846 length, written, length - written); */
850 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
852 buffer = g_malloc0(length + 1);
854 memcpy(buffer, buf, length);
856 g_array_append_vals(in_param1, &fd, sizeof(int));
857 g_array_append_vals(in_param2, &length, sizeof(int));
858 g_array_append_vals(in_param3, buffer, length);
860 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
861 in_param1, in_param2, in_param3, in_param4, &out_param);
863 BT_DBG("result: %x", result);
865 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);