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;
121 static void __bt_free_conn(rfcomm_conn_info_t *conn)
130 if (conn->watch_id > 0) {
131 g_source_remove(conn->watch_id);
139 static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
142 rfcomm_conn_info_t *conn_info = NULL;
143 l = g_slist_find_custom(info->rfcomm_conns, address, (GCompareFunc)compare);
147 info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
148 __bt_free_conn(conn_info);
152 static rfcomm_conn_info_t *__get_conn_info_from_fd(rfcomm_cb_data_t *info,
156 rfcomm_conn_info_t *device_node = NULL;
157 for(l = info->rfcomm_conns; l != NULL; l = l->next) {
158 device_node = l->data;
159 if ( device_node && device_node->fd == fd)
165 static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
169 rfcomm_conn_info_t *conn_info = NULL;
170 l = g_slist_find_custom(info->rfcomm_conns, dev_address,
171 (GCompareFunc)compare);
177 static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
178 char *dev_address, int result)
180 __client_connected_cb(info, dev_address, result);
181 __rfcomm_remove_conn_info_t(info, dev_address);
183 if (info->rfcomm_conns == NULL)
184 rfcomm_cb_data_remove(info);
187 static rfcomm_cb_data_t *__find_rfcomm_info_with_fd(int fd)
191 for (l = rfcomm_clients; l != NULL; l = l->next) {
192 rfcomm_cb_data_t *info = l->data;
193 device_fd = g_slist_find_custom(info->rfcomm_conns, &fd,
194 (GCompareFunc)compare_fd);
202 static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
206 for (l = rfcomm_clients; l != NULL; l = l->next) {
207 rfcomm_cb_data_t *info = l->data;
210 if (g_strcmp0(info->obj_path, path) == 0)
217 static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
221 for (l = rfcomm_clients; l != NULL; l = l->next) {
222 rfcomm_cb_data_t *info = l->data;
224 if (g_strcmp0(info->uuid, uuid) == 0)
231 static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
232 rfcomm_cb_data_t *info)
234 if (conn_info == NULL)
237 bluetooth_rfcomm_disconnection_t disconn_info;
238 bt_event_info_t *event_info = NULL;
239 if (conn_info->disconnected == FALSE)
241 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
242 if (event_info == NULL) {
243 if (info->rfcomm_conns == NULL)
244 rfcomm_cb_data_remove(info);
247 memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
248 disconn_info.device_role = RFCOMM_ROLE_CLIENT;
249 g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
250 BT_DBG("Disconnected FD [%d]", conn_info->fd);
251 _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
254 disconn_info.socket_fd = conn_info->fd;
256 BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
257 BLUETOOTH_ERROR_NONE, conn_info->bt_addr,
258 info->uuid, conn_info->fd);
259 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
260 BLUETOOTH_ERROR_NONE, &disconn_info,
261 event_info->cb, event_info->user_data);
264 BT_DBG("List is present deleting it");
265 __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
267 if (info->rfcomm_conns == NULL)
268 rfcomm_cb_data_remove(info);
273 static gboolean __rfcomm_client_disconnect(gpointer user_data)
275 rfcomm_cb_data_t *info = (rfcomm_cb_data_t *) user_data;
276 BT_INFO_C("Disconnected [RFCOMM Client]");
277 retv_if(info == NULL, FALSE);
279 if (g_slist_find(rfcomm_clients, info) == NULL) {
280 BT_INFO("rfcomm resource is already freed");
284 g_slist_foreach(info->rfcomm_conns,
285 (GFunc) _bt_rfcomm_disconnect_conn_info, info);
290 static gboolean __is_error_by_disconnect(GError *err)
292 return !g_strcmp0(err->message, "Connection reset by peer") ||
293 !g_strcmp0(err->message, "Connection timed out") ||
294 !g_strcmp0(err->message, "Software caused connection abort");
297 static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond,
302 int result = BLUETOOTH_ERROR_NONE;
303 rfcomm_cb_data_t *info = data;
304 rfcomm_conn_info_t *conn_info = NULL;
305 bt_event_info_t *event_info;
306 bluetooth_rfcomm_received_data_t data_r;
307 GIOStatus status = G_IO_STATUS_NORMAL;
312 retv_if(info == NULL, FALSE);
313 fd = g_io_channel_unix_get_fd(chan);
314 if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
315 BT_ERR_C("RFComm Client disconnected: %d", fd);
316 conn_info = __get_conn_info_from_fd(info, fd);
317 if (conn_info == NULL) {
318 BT_ERR("No Connection info found with FD [%d]", fd);
321 conn_info->disconnected = TRUE;
322 __rfcomm_client_disconnect(info);
326 buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
328 status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
330 if (status != G_IO_STATUS_NORMAL) {
331 BT_ERR("IO Channel read is failed with %d", status);
335 BT_ERR("IO Channel read error [%s]", err->message);
336 if (status == G_IO_STATUS_ERROR &&
337 __is_error_by_disconnect(err)) {
338 BT_ERR("cond : %d", cond);
340 conn_info = __get_conn_info_from_fd(info, fd);
341 if (conn_info == NULL) {
342 BT_ERR("No Connection info found with FD [%d]", fd);
345 conn_info->disconnected = TRUE;
346 __rfcomm_client_disconnect(info);
354 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
355 if (event_info == NULL) {
360 data_r.socket_fd = fd;
361 data_r.buffer_size = len;
362 data_r.buffer = buffer;
364 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
366 event_info->cb, event_info->user_data);
372 static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
375 bluetooth_rfcomm_connection_t conn_info;
376 bt_event_info_t *event_info;
377 rfcomm_conn_info_t *conn_list_info = NULL;
378 BT_INFO_C("Connected [RFCOMM Client]");
380 event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
381 if (event_info == NULL)
384 memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
385 conn_info.device_role = RFCOMM_ROLE_CLIENT;
386 g_strlcpy(conn_info.uuid, cb_data->uuid, BLUETOOTH_UUID_STRING_MAX);
387 _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
389 conn_list_info = __get_conn_info_from_address(cb_data, dev_address);
390 if (conn_list_info == NULL) {
391 BT_ERR("Device addres %s not found in connection list", dev_address);
394 conn_info.socket_fd = conn_list_info->fd;
395 conn_info.server_id = -1;
397 BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
398 result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
399 _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
401 event_info->cb, event_info->user_data);
407 int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
409 rfcomm_cb_data_t *info;
411 rfcomm_conn_info_t *conn_info = NULL;
412 BT_DBG("%s %d", path, fd);
413 char address[BT_ADDRESS_STRING_SIZE];
414 _bt_convert_addr_type_to_string(address,
415 (unsigned char *)addr);
416 info = __find_rfcomm_info_from_path(path);
419 conn_info = __get_conn_info_from_address(info, address);
421 if (conn_info == NULL) {
422 BT_ERR("Device Address %s not found in connection list", address);
426 BT_DBG("connection info fd %d", conn_info->fd);
427 data_io = g_io_channel_unix_new(fd);
428 g_io_channel_set_encoding(data_io, NULL, NULL);
429 g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
430 conn_info->watch_id = g_io_add_watch(data_io,
431 G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
432 __client_data_received_cb, info);
434 g_io_channel_unref(data_io);
436 __client_connected_cb(info, address,BLUETOOTH_ERROR_NONE);
441 static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
445 GError *error = NULL;
446 rfcomm_cb_data_t *cb_data;
447 char dev_address[BT_ADDRESS_STRING_SIZE];
451 ret_if(user_data == NULL);
455 if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
457 g_dbus_error_strip_remote_error(error);
458 BT_ERR("Error : %s \n", error->message);
460 if (g_strcmp0(error->message, "In Progress") == 0)
461 result = BLUETOOTH_ERROR_DEVICE_BUSY;
463 result = BLUETOOTH_ERROR_INTERNAL;
464 path = g_dbus_proxy_get_object_path(proxy);
465 _bt_convert_device_path_to_address(path, dev_address);
466 __rfcomm_client_connected_cb(cb_data, dev_address, result);
471 g_object_unref(proxy);
476 static void __bt_discover_service_response_cb(GDBusProxy *proxy,
477 GAsyncResult *res, gpointer user_data)
479 rfcomm_cb_data_t *cb_data;
482 bt_register_profile_info_t info = {0};
483 int result = BLUETOOTH_ERROR_NONE;
484 char dev_address[BT_ADDRESS_STRING_SIZE];
489 ret_if(user_data == NULL);
493 path = g_dbus_proxy_get_object_path(proxy);
495 _bt_convert_device_path_to_address(path, dev_address);
496 BT_DBG("Device Adress [%s]", dev_address);
497 g_dbus_proxy_call_finish(proxy, res, &err);
499 g_object_unref(proxy);
502 g_dbus_error_strip_remote_error(err);
503 BT_ERR("Error occured in Proxy call [%s]\n", err->message);
504 if (!strcmp("Operation canceled", err->message)) {
505 result = BLUETOOTH_ERROR_CANCEL_BY_USER;
506 } else if (!strcmp("In Progress", err->message)) {
507 result = BLUETOOTH_ERROR_IN_PROGRESS;
508 } else if (!strcmp("Host is down", err->message)) {
509 result = BLUETOOTH_ERROR_HOST_DOWN;
510 } else if (!strcmp(BT_TIMEOUT_MESSAGE, err->message)) {
511 result = BLUETOOTH_ERROR_SERVICE_SEARCH_ERROR;
512 ret = _bt_cancel_discovers(dev_address);
513 if (ret != BLUETOOTH_ERROR_NONE)
514 BT_ERR("Error: While CancelDiscovery");
516 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
518 __rfcomm_client_connected_cb(cb_data, dev_address, result);
521 BT_INFO("Services are Updated checking required uuid is there");
522 /* Check here for uuid present */
523 ret = _bt_discover_service_uuids(dev_address, (char *)cb_data->uuid);
524 if (ret == BLUETOOTH_ERROR_NONE) {
525 info.uuid = (char *)cb_data->uuid;
526 info.obj_path = cb_data->obj_path;
527 info.role = "client";
529 ret = _bt_register_profile(&info, FALSE);
531 BT_DBG("Error: register profile");
532 ret = _bt_connect_profile(dev_address, cb_data->uuid,
533 __bt_connect_response_cb, cb_data);
535 if (ret != BLUETOOTH_ERROR_NONE) {
536 BT_ERR("ConnectProfile failed");
537 result = BLUETOOTH_ERROR_CONNECTION_ERROR;
538 __rfcomm_client_connected_cb(cb_data, dev_address, result);
542 BT_ERR("remote uuid not found");
543 result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
544 __rfcomm_client_connected_cb(cb_data, dev_address, result);
552 BT_EXPORT_API int bluetooth_rfcomm_connect(
553 const bluetooth_device_address_t *remote_bt_address,
554 const char *remote_uuid)
558 rfcomm_cb_data_t *cb_data = NULL;
559 rfcomm_conn_info_t *conn = NULL;
563 bt_user_info_t *user_info;
564 char uuid[BLUETOOTH_UUID_STRING_MAX];
566 BT_CHECK_PARAMETER(remote_bt_address, return);
567 BT_CHECK_PARAMETER(remote_uuid, return);
568 BT_CHECK_ENABLED(return);
571 BT_INFO_C("<<<<<<<<< RFCOMM Connect request from app >>>>>>>>>>>");
576 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
577 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
578 BT_ERR("Don't have a privilege to use this API");
579 return BLUETOOTH_ERROR_PERMISSION_DEINED;
582 id = __rfcomm_assign_id();
584 return BLUETOOTH_ERROR_INTERNAL;
586 cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
588 path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
590 object_id = _bt_register_new_conn(path, new_connection);
592 __rfcomm_delete_id(id);
593 return BLUETOOTH_ERROR_INTERNAL;
596 cb_data = g_new0(rfcomm_cb_data_t, 1);
597 g_strlcpy(cb_data->uuid, remote_uuid, BLUETOOTH_UUID_STRING_MAX);
598 cb_data->obj_path = path;
599 cb_data->object_id = object_id;
602 conn = g_new0(rfcomm_conn_info_t, 1);
604 _bt_convert_addr_type_to_string(conn->bt_addr,
605 (unsigned char *)remote_bt_address->addr);
607 BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
608 cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
609 ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
610 __bt_discover_service_response_cb, cb_data);
611 if (ret != BLUETOOTH_ERROR_NONE) {
612 BT_ERR("Error returned while service discovery");
613 __rfcomm_remove_conn_info_t(cb_data, conn->bt_addr);
614 if (cb_data->rfcomm_conns == NULL)
615 rfcomm_cb_data_remove(cb_data);
616 return BLUETOOTH_ERROR_INTERNAL;
618 if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
619 BT_INFO("Adding callback information to rfcomm_clients");
620 rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
622 BT_INFO("Callback information is already added");
624 return BLUETOOTH_ERROR_NONE;
626 user_info = _bt_get_user_data(BT_COMMON);
627 retv_if(user_info->cb == NULL, BLUETOOTH_ERROR_INTERNAL);
629 /* connect_type: BT_RFCOMM_UUID / BT_RFCOMM_CHANNEL*/
630 /* In now, we only support to connecty using UUID */
631 connect_type = BT_RFCOMM_UUID;
633 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
634 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
635 BT_ERR("Don't have a privilege to use this API");
636 return BLUETOOTH_ERROR_PERMISSION_DEINED;
640 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
642 g_array_append_vals(in_param1, remote_bt_address,
643 sizeof(bluetooth_device_address_t));
645 g_strlcpy(uuid, remote_uuid, sizeof(uuid));
646 g_array_append_vals(in_param2, uuid, BLUETOOTH_UUID_STRING_MAX);
648 g_array_append_vals(in_param3, &connect_type, sizeof(int));
650 result = _bt_send_request_async(BT_BLUEZ_SERVICE,
651 BT_RFCOMM_CLIENT_CONNECT,
652 in_param1, in_param2,
653 in_param3, in_param4,
654 user_info->cb, user_info->user_data);
656 BT_DBG("result: %x", result);
658 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
664 BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
667 GSList *conn_list = NULL;
668 rfcomm_cb_data_t *client_info;
669 rfcomm_conn_info_t *conn_info;
670 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
672 BT_CHECK_PARAMETER(device_address, return);
673 BT_CHECK_PARAMETER(connected, return);
675 _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
678 for (l = rfcomm_clients; l != NULL; l = l->next) {
679 client_info = l->data;
680 if (client_info == NULL)
682 for(conn_list = client_info->rfcomm_conns;
683 conn_list != NULL; conn_list = conn_list->next) {
684 conn_info = conn_list->data;
685 if(conn_info == NULL)
688 if (g_strcmp0(address, conn_info->bt_addr) == 0) {
690 return BLUETOOTH_ERROR_NONE;
695 return BLUETOOTH_ERROR_NONE;
698 BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
701 int connected = FALSE;
703 BT_CHECK_ENABLED(return);
706 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
708 result = _bt_send_request(BT_BLUEZ_SERVICE,
709 BT_RFCOMM_CLIENT_IS_CONNECTED,
710 in_param1, in_param2, in_param3,
711 in_param4, &out_param);
713 BT_DBG("result: %x", result);
715 if (result == BLUETOOTH_ERROR_NONE) {
716 connected = g_array_index(out_param,
719 BT_ERR("Fail to send request");
722 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
727 BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
730 rfcomm_cb_data_t *info;
731 rfcomm_conn_info_t *conn_info;
732 BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
733 BT_CHECK_ENABLED(return);
735 if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
736 == BLUETOOTH_ERROR_PERMISSION_DEINED) {
737 BT_ERR("Don't have a privilege to use this API");
738 return BLUETOOTH_ERROR_PERMISSION_DEINED;
742 BT_ERR("Invalid FD");
743 return BLUETOOTH_ERROR_INVALID_PARAM;
745 BT_DBG("FDD %d", socket_fd);
747 info = __find_rfcomm_info_with_fd(socket_fd);
749 BT_DBG("Could not find in client, so check in server");
750 return bluetooth_rfcomm_server_disconnect(socket_fd);
752 conn_info = __get_conn_info_from_fd(info, socket_fd);
753 if (conn_info == NULL) {
754 BT_ERR("FATAL Error");
755 return BLUETOOTH_ERROR_INTERNAL;
757 if (conn_info->watch_id <= 0) {
758 BT_ERR("Invalid state");
759 return BLUETOOTH_ERROR_NOT_CONNECTED;
761 conn_info->disconnected = TRUE;
763 BT_INFO("conn_info %s", conn_info->bt_addr);
764 _bt_disconnect_profile(conn_info->bt_addr, info->uuid, NULL,NULL);
765 if (info->idle_id == 0)
766 info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
768 return BLUETOOTH_ERROR_NONE;
771 int service_function;
773 BT_CHECK_ENABLED(return);
776 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
778 /* Support the OSP */
779 if (socket_fd == -1) {
781 service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
783 g_array_append_vals(in_param1, &socket_fd, sizeof(int));
784 service_function = BT_RFCOMM_SOCKET_DISCONNECT;
787 result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
788 in_param1, in_param2, in_param3, in_param4, &out_param);
790 BT_DBG("result: %x", result);
792 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
798 BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
807 BT_CHECK_PARAMETER(buf, return);
809 BT_ERR("Invalid FD");
810 return BLUETOOTH_ERROR_INVALID_PARAM;
813 BT_DBG("FD : %d", fd);
815 #ifndef RFCOMM_DIRECT
816 BT_CHECK_ENABLED(return);
818 retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
821 switch (privilege_token) {
823 result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
825 if (result == BLUETOOTH_ERROR_NONE) {
826 privilege_token = 1; /* Have a permission */
827 } else if (result == BLUETOOTH_ERROR_PERMISSION_DEINED) {
828 BT_ERR("Don't have a privilege to use this API");
829 privilege_token = -1; /* Don't have a permission */
830 return BLUETOOTH_ERROR_PERMISSION_DEINED;
832 /* Just break - It is not related with permission error */
836 /* Already have a privilege */
839 return BLUETOOTH_ERROR_PERMISSION_DEINED;
841 /* Invalid privilge token value */
842 return BLUETOOTH_ERROR_INTERNAL;
845 written = write(fd, buf, length);
846 /*BT_DBG("Length %d, written = %d, balance(%d)",
847 length, written, length - written); */
851 BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
853 buffer = g_malloc0(length + 1);
855 memcpy(buffer, buf, length);
857 g_array_append_vals(in_param1, &fd, sizeof(int));
858 g_array_append_vals(in_param2, &length, sizeof(int));
859 g_array_append_vals(in_param3, buffer, length);
861 result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
862 in_param1, in_param2, in_param3, in_param4, &out_param);
864 BT_DBG("result: %x", result);
866 BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);