/*
- * Bluetooth-frwk
- *
- * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
- *
- * Contact: Hocheol Seo <hocheol.seo@samsung.com>
- * Girishashok Joshi <girish.joshi@samsung.com>
- * Chanyeol Park <chanyeol.park@samsung.com>
+ * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
*/
#include <string.h>
-#ifdef RFCOMM_DIRECT
-#include <errno.h>
#include <gio/gunixfdlist.h>
+
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
+#include <errno.h>
#endif
#include "bluetooth-api.h"
#include "bt-request-sender.h"
#include "bt-event-handler.h"
-#ifdef RFCOMM_DIRECT
-
-#define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
- "the remote application did not send a reply, " \
- "the message bus security policy blocked the reply, " \
- "the reply timeout expired, or the network connection " \
- "was broken."
-
-static GSList *rfcomm_clients;
+#ifdef TIZEN_FEATURE_BT_DPM
+#include "bt-dpm.h"
+#endif
/* Variable for privilege, only for write API,
before we should reduce time to bt-service dbus calling
*/
static int privilege_token;
+
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
+#define BT_TIMEOUT_MESSAGE "Did not receive a reply. Possible causes include: " \
+ "the remote application did not send a reply, " \
+ "the message bus security policy blocked the reply, " \
+ "the reply timeout expired, or the network connection " \
+ "was broken."
+
+#define BATTERY_MONITOR_RFCOMM_INTERVAL 5
+
+static GSList *rfcomm_clients;
+
typedef struct {
char bt_addr[BT_ADDRESS_STRING_SIZE];
int fd;
- int watch_id;
+ guint watch_id;
gboolean disconnected;
} rfcomm_conn_info_t;
unsigned int idle_id;
} rfcomm_cb_data_t;
+static unsigned int rx_data = 0, tx_data = 0;
+static guint tx_tag = 0, rx_tag = 0;
+
static void __client_connected_cb(rfcomm_cb_data_t *cb_data,
char *dev_address, int result);
+static bool __rfcomm_record_tx_data(void)
+{
+ if (tx_data > 0) {
+ int ret = _bt_common_send_rfcomm_tx_details(tx_data);
+ if (ret == BLUETOOTH_ERROR_NONE) {
+ tx_data = 0;
+ return TRUE;
+ } else {
+ BT_ERR("RFCOMM tx data could not be registered");
+ }
+ }
+
+ tx_data = 0;
+ tx_tag = 0;
+
+ return FALSE;
+}
+
+static bool __rfcomm_record_rx_data(void)
+{
+ if (rx_data) {
+ int ret = _bt_common_send_rfcomm_rx_details(rx_data);
+ if (ret == BLUETOOTH_ERROR_NONE) {
+ rx_data = 0;
+ return TRUE;
+ } else {
+ BT_ERR("RFCOMM rx data could not be registered");
+ }
+ }
+
+ rx_data = 0;
+ rx_tag = 0;
+
+ return FALSE;
+}
+
static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
{
BT_DBG("+");
gint compare_fd(gpointer *a, gpointer *b)
{
rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
- int fd = (int )*b;
- if (node->fd == fd)
+ int *fd = (int *)b;
+ if (node->fd == *fd)
return 0;
+
return 1;
}
static void __bt_free_conn(rfcomm_conn_info_t *conn)
if (conn == NULL)
return;
- if (conn->fd > 0)
- close(conn->fd);
if (conn->watch_id > 0) {
g_source_remove(conn->watch_id);
conn->watch_id = 0;
}
+
g_free(conn);
BT_DBG("-");
{
GSList *l;
rfcomm_conn_info_t *device_node = NULL;
- for(l = info->rfcomm_conns; l != NULL; l = l->next) {
- device_node = l->data;
- if ( device_node && device_node->fd == fd)
- return device_node;
- }
- return NULL;
-}
-
-static rfcomm_conn_info_t * __get_conn_info_for_disconnection(
- rfcomm_cb_data_t *info)
-{
- GSList *l;
- rfcomm_conn_info_t *device_node = NULL;
- for(l = info->rfcomm_conns; l != NULL; l = l->next) {
+ for (l = info->rfcomm_conns; l != NULL; l = l->next) {
device_node = l->data;
- if (device_node && device_node->disconnected == TRUE)
+ if (device_node && device_node->fd == fd)
return device_node;
}
return NULL;
static void __rfcomm_client_connected_cb(rfcomm_cb_data_t *info,
char *dev_address, int result)
{
+ if (g_slist_find(rfcomm_clients, info) == NULL) {
+ BT_INFO("rfcomm resource is already freed");
+ return;
+ }
+
__client_connected_cb(info, dev_address, result);
__rfcomm_remove_conn_info_t(info, dev_address);
static void _bt_rfcomm_disconnect_conn_info(rfcomm_conn_info_t *conn_info,
rfcomm_cb_data_t *info)
{
+ if (conn_info == NULL)
+ return;
+
bluetooth_rfcomm_disconnection_t disconn_info;
bt_event_info_t *event_info = NULL;
+
if (conn_info->disconnected == FALSE)
return;
+
event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
if (event_info == NULL) {
+ __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
+
if (info->rfcomm_conns == NULL)
rfcomm_cb_data_remove(info);
return;
}
+
memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
disconn_info.device_role = RFCOMM_ROLE_CLIENT;
g_strlcpy(disconn_info.uuid, info->uuid, BLUETOOTH_UUID_STRING_MAX);
- BT_DBG("Disconnected FD [%d]", conn_info->fd);
_bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
conn_info->bt_addr);
+ BT_DBG("Disconnected FD [%d]", conn_info->fd);
disconn_info.socket_fd = conn_info->fd;
BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
BLUETOOTH_ERROR_NONE, &disconn_info,
event_info->cb, event_info->user_data);
- if(conn_info) {
- BT_DBG("List is present deleting it");
- __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
- }
+ __rfcomm_remove_conn_info_t(info, conn_info->bt_addr);
+
if (info->rfcomm_conns == NULL)
rfcomm_cb_data_remove(info);
+ if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_CLIENT,
+ FALSE, disconn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
+ BT_ERR("Fail to send the connection info");
+
BT_DBG("-");
}
static gboolean __rfcomm_client_disconnect(gpointer user_data)
{
- rfcomm_cb_data_t *info = (rfcomm_cb_data_t *) user_data;
- BT_INFO_C("Disconnected [RFCOMM Client]");
+ rfcomm_cb_data_t *info = (rfcomm_cb_data_t *)user_data;
+
+ BT_INFO_C("### Disconnected [RFCOMM Client]");
+
retv_if(info == NULL, FALSE);
if (g_slist_find(rfcomm_clients, info) == NULL) {
return FALSE;
}
info->idle_id = 0;
+
g_slist_foreach(info->rfcomm_conns,
- (GFunc) _bt_rfcomm_disconnect_conn_info, info);
+ (GFunc)_bt_rfcomm_disconnect_conn_info, info);
+
BT_DBG("-");
return FALSE;
}
GIOStatus status = G_IO_STATUS_NORMAL;
GError *err = NULL;
int fd;
- BT_DBG("");
+ static int resource_unavailable_cnt = 0;
retv_if(info == NULL, FALSE);
fd = g_io_channel_unix_get_fd(chan);
if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
BT_ERR_C("RFComm Client disconnected: %d", fd);
+
conn_info = __get_conn_info_from_fd(info, fd);
if (conn_info == NULL) {
BT_ERR("No Connection info found with FD [%d]", fd);
return FALSE;
}
- conn_info->disconnected = TRUE;
+
+ if (conn_info->disconnected == FALSE) {
+ close(conn_info->fd);
+ conn_info->disconnected = TRUE;
+ }
__rfcomm_client_disconnect(info);
return FALSE;
}
BT_ERR("IO Channel read is failed with %d", status);
g_free(buffer);
+
if (err) {
BT_ERR("IO Channel read error [%s]", err->message);
if (status == G_IO_STATUS_ERROR &&
__is_error_by_disconnect(err)) {
+
BT_ERR("cond : %d", cond);
g_error_free(err);
- conn_info = __get_conn_info_from_fd(info, fd);
- if (conn_info == NULL) {
- BT_ERR("No Connection info found with FD [%d]", fd);
- return FALSE;
- }
- conn_info->disconnected = TRUE;
- __rfcomm_client_disconnect(info);
- return FALSE;
+ goto fail;
}
g_error_free(err);
}
+
+ if (status == G_IO_STATUS_ERROR ||
+ status == G_IO_STATUS_EOF) {
+ goto fail;
+ } else if (status == G_IO_STATUS_AGAIN) {
+ resource_unavailable_cnt++;
+ if (resource_unavailable_cnt > 10)
+ goto fail;
+ }
+
return TRUE;
}
+ resource_unavailable_cnt = 0;
event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
if (event_info == NULL) {
result, &data_r,
event_info->cb, event_info->user_data);
+ if (bluetooth_get_battery_monitor_state()) {
+ if (rx_tag == 0) {
+ BT_DBG("Adding rfcomm rx timeout function for battery monitor");
+ rx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_rx_data, NULL);
+ }
+ rx_data += len;
+ }
+
g_free(buffer);
return TRUE;
+
+fail:
+ conn_info = __get_conn_info_from_fd(info, fd);
+ if (conn_info == NULL) {
+ BT_ERR("No Connection info found with FD [%d]", fd);
+ return FALSE;
+ }
+
+ if (conn_info->disconnected == FALSE) {
+ close(conn_info->fd);
+ conn_info->disconnected = TRUE;
+ }
+ __rfcomm_client_disconnect(info);
+
+ return FALSE;
}
static void __client_connected_cb(rfcomm_cb_data_t *cb_data, char *dev_address,
bluetooth_rfcomm_connection_t conn_info;
bt_event_info_t *event_info;
rfcomm_conn_info_t *conn_list_info = NULL;
- BT_INFO_C("Connected [RFCOMM Client]");
+
+ if (result == BLUETOOTH_ERROR_NONE)
+ BT_INFO_C("### Connected [RFCOMM Client]");
event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
if (event_info == NULL)
conn_info.socket_fd = conn_list_info->fd;
conn_info.server_id = -1;
+ if (_bt_common_send_rfcomm_conn_info(RFCOMM_ROLE_CLIENT,
+ TRUE, conn_info.socket_fd) != BLUETOOTH_ERROR_NONE)
+ BT_ERR("Fail to send the connection info");
+
BT_DBG("Connection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
result, conn_list_info->bt_addr, cb_data->uuid, conn_list_info->fd);
_bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_CONNECTED,
BT_DBG("-");
}
-#endif
+void _bt_rfcomm_client_disconnect_all(void)
+{
+ GSList *client;
+ GSList *conn;
+
+ BT_INFO_C("### Disconnect all RFCOMM client connections");
+
+ for (client = rfcomm_clients; client; ) {
+ rfcomm_cb_data_t *info = client->data;
+
+ for (conn = info->rfcomm_conns; conn; conn = conn->next) {
+ rfcomm_conn_info_t *conn_info = conn->data;
+
+ if (conn_info == NULL)
+ continue;
+
+ if (conn_info->watch_id == 0 || conn_info->disconnected)
+ continue;
+
+ close(conn_info->fd);
+ conn_info->disconnected = TRUE;
+
+ _bt_disconnect_ext_profile(conn_info->bt_addr,
+ info->obj_path);
+ }
+
+ client = client->next;
+ __rfcomm_client_disconnect(info);
+ }
+
+ return;
+}
+
+void _bt_rfcomm_client_reset_timer(void)
+{
+ if (rx_tag > 0) {
+ rx_tag = 0;
+ g_source_remove(rx_tag);
+ }
+
+ if (tx_tag > 0) {
+ g_source_remove(tx_tag);
+ tx_tag = 0;
+ }
+
+ rx_data = 0;
+ tx_data = 0;
+}
int new_connection(const char *path, int fd, bluetooth_device_address_t *addr)
{
rfcomm_cb_data_t *info;
GIOChannel *data_io;
rfcomm_conn_info_t *conn_info = NULL;
- BT_DBG("%s %d", path, fd);
char address[BT_ADDRESS_STRING_SIZE];
+
+ BT_INFO("%s %d", path, fd);
+
_bt_convert_addr_type_to_string(address,
(unsigned char *)addr);
+
info = __find_rfcomm_info_from_path(path);
- if (info == NULL)
+ if (info == NULL) {
+ BT_ERR("rfcomm info is NULL");
return -1;
- conn_info = __get_conn_info_from_address(info, address);
+ }
+ conn_info = __get_conn_info_from_address(info, address);
if (conn_info == NULL) {
- BT_ERR("Device Address %s not found in connection list", address);
+ BT_ERR("connection info is NULL");
return -1;
}
+
conn_info->fd = fd;
- BT_DBG("connection info fd %d", conn_info->fd);
+
data_io = g_io_channel_unix_new(fd);
+
g_io_channel_set_encoding(data_io, NULL, NULL);
g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
+
conn_info->watch_id = g_io_add_watch(data_io,
G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
__client_data_received_cb, info);
g_io_channel_unref(data_io);
- __client_connected_cb(info, address,BLUETOOTH_ERROR_NONE);
+ __client_connected_cb(info, address, BLUETOOTH_ERROR_NONE);
return 0;
}
-static void __bt_connect_response_cb(DBusGProxy *proxy, DBusGProxyCall *call,
+static void __bt_connect_response_cb(GDBusProxy *proxy, GAsyncResult *res,
gpointer user_data)
{
GError *error = NULL;
+ GVariant *value;
rfcomm_cb_data_t *cb_data;
char dev_address[BT_ADDRESS_STRING_SIZE];
const char *path;
cb_data = user_data;
- if (!dbus_g_proxy_end_call(proxy, call, &error, G_TYPE_INVALID)) {
+ value = g_dbus_proxy_call_finish(proxy, res, &error);
+ if (value == NULL) {
int result;
-
+ g_dbus_error_strip_remote_error(error);
BT_ERR("Error : %s \n", error->message);
if (g_strcmp0(error->message, "In Progress") == 0)
result = BLUETOOTH_ERROR_DEVICE_BUSY;
else
result = BLUETOOTH_ERROR_INTERNAL;
- path = dbus_g_proxy_get_path(proxy);
+ path = g_dbus_proxy_get_object_path(proxy);
_bt_convert_device_path_to_address(path, dev_address);
__rfcomm_client_connected_cb(cb_data, dev_address, result);
g_error_free(error);
- g_object_unref(proxy);
+ } else {
+ g_variant_unref(value);
}
+
+ if (proxy)
+ g_object_unref(proxy);
+
BT_DBG("-");
}
-static void __bt_discover_service_response_cb(DBusGProxy *proxy,
- DBusGProxyCall *call, gpointer user_data)
+static void __bt_discover_service_response_cb(GDBusProxy *proxy,
+ GAsyncResult *res, gpointer user_data)
{
rfcomm_cb_data_t *cb_data;
int ret = 0;
GError *err = NULL;
- GHashTable *hash = NULL;
+ GVariant *value;
bt_register_profile_info_t info = {0};
int result = BLUETOOTH_ERROR_NONE;
char dev_address[BT_ADDRESS_STRING_SIZE];
cb_data = user_data;
- path = dbus_g_proxy_get_path(proxy);
+ path = g_dbus_proxy_get_object_path(proxy);
+
_bt_convert_device_path_to_address(path, dev_address);
BT_DBG("Device Adress [%s]", dev_address);
- dbus_g_proxy_end_call(proxy, call, &err,
- dbus_g_type_get_map("GHashTable",
- G_TYPE_UINT, G_TYPE_STRING), &hash,
- G_TYPE_INVALID);
- g_object_unref(proxy);
+ value = g_dbus_proxy_call_finish(proxy, res, &err);
+ if (proxy)
+ g_object_unref(proxy);
+ if (value)
+ g_variant_unref(value);
+
if (err != NULL) {
+ g_dbus_error_strip_remote_error(err);
BT_ERR("Error occured in Proxy call [%s]\n", err->message);
if (!strcmp("Operation canceled", err->message)) {
result = BLUETOOTH_ERROR_CANCEL_BY_USER;
}
done:
if (err)
- g_error_free(err);
- if (hash)
- g_hash_table_destroy(hash);
+ g_clear_error(&err);
}
+#else
+GSList *rfcomm_clients;
+
+typedef struct {
+ char *uuid;
+ char *remote_addr;
+ int sock_fd;
+ int watch_id;
+} rfcomm_client_conn_info_t;
+
+static gboolean __is_error_by_disconnect(GError *err)
+{
+ return !g_strcmp0(err->message, "Connection reset by peer") ||
+ !g_strcmp0(err->message, "Connection timed out") ||
+ !g_strcmp0(err->message, "Software caused connection abort");
+}
+
+static rfcomm_client_conn_info_t *__find_rfcomm_conn_info_with_fd(int fd)
+{
+ GSList *l;
+
+ BT_DBG("+");
+
+ for (l = rfcomm_clients; l != NULL; l = l->next) {
+ rfcomm_client_conn_info_t *info = l->data;
+
+ if (info && info->sock_fd == fd) {
+ BT_INFO("Match found");
+ return info;
+ }
+ }
+
+ BT_DBG("-");
+ return NULL;
+}
+
+static void __rfcomm_remove_client_conn_info_t(rfcomm_client_conn_info_t *info)
+{
+ ret_if(info == NULL);
+
+ rfcomm_clients = g_slist_remove(rfcomm_clients, info);
+ g_free(info->uuid);
+ g_free(info->remote_addr);
+}
+
+static void __bt_rfcomm_client_disconnected(rfcomm_client_conn_info_t *conn_info)
+{
+
+ bluetooth_rfcomm_disconnection_t disconn_info;
+ bt_event_info_t *event_info = NULL;
+
+ ret_if(conn_info == NULL);
+
+ event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
+ ret_if(event_info == NULL);
+
+ memset(&disconn_info, 0x00, sizeof(bluetooth_rfcomm_disconnection_t));
+ disconn_info.device_role = RFCOMM_ROLE_CLIENT;
+ disconn_info.socket_fd = conn_info->sock_fd;
+ g_strlcpy(disconn_info.uuid, conn_info->uuid, BLUETOOTH_UUID_STRING_MAX);
+ _bt_convert_addr_string_to_type(disconn_info.device_addr.addr,
+ conn_info->remote_addr);
+
+ BT_DBG("Disconnection Result[%d] BT_ADDRESS[%s] UUID[%s] FD[%d]",
+ BLUETOOTH_ERROR_NONE, conn_info->remote_addr,
+ conn_info->uuid, conn_info->sock_fd);
+ _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DISCONNECTED,
+ BLUETOOTH_ERROR_NONE, &disconn_info,
+ event_info->cb, event_info->user_data);
+
+ BT_DBG("-");
+}
+
+static gboolean __client_data_received_cb(GIOChannel *chan, GIOCondition cond, gpointer data)
+{
+ bt_event_info_t *event_info;
+ bluetooth_rfcomm_received_data_t data_r;
+ rfcomm_client_conn_info_t *conn_info;
+
+ int fd;
+ gsize len = 0;
+ char *buffer;
+ GError *err = NULL;
+ GIOStatus status = G_IO_STATUS_NORMAL;
+
+ BT_DBG("+");
+
+ fd = g_io_channel_unix_get_fd(chan);
+ if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
+ BT_ERR_C("RFComm Client disconnected: %d", fd);
+ goto fail;
+ }
+
+ buffer = g_malloc0(BT_RFCOMM_BUFFER_LEN + 1);
+ status = g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_LEN,
+ &len, &err);
+ if (status != G_IO_STATUS_NORMAL) {
+ BT_ERR("IO Channel read is failed with %d", status);
+ g_free(buffer);
+ if (err) {
+ BT_ERR("IO Channel read error [%s]", err->message);
+ if (status == G_IO_STATUS_ERROR &&
+ __is_error_by_disconnect(err)) {
+ BT_ERR("cond : %d", cond);
+ g_error_free(err);
+ goto fail;
+ }
+ g_error_free(err);
+ }
+
+ return TRUE;
+ }
+
+ if (len == 0) {
+ BT_ERR("Length is zero, remote end hang up");
+ goto fail;
+ }
+
+ BT_DBG("fd: %d, len: %d, buffer: %s", fd, len, buffer);
+
+ event_info = _bt_event_get_cb_data(BT_RFCOMM_CLIENT_EVENT);
+ if (event_info == NULL) {
+ BT_INFO("event_info == NULL");
+ g_free(buffer);
+ return TRUE;
+ }
+
+ data_r.socket_fd = fd;
+ data_r.buffer_size = len;
+ data_r.buffer = buffer;
+
+ _bt_common_event_cb(BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
+ BLUETOOTH_ERROR_NONE, &data_r,
+ event_info->cb, event_info->user_data);
+
+ g_free(buffer);
+ return TRUE;
+
+fail:
+ conn_info = __find_rfcomm_conn_info_with_fd(fd);
+ if (conn_info) {
+ __bt_rfcomm_client_disconnected(conn_info);
+ __rfcomm_remove_client_conn_info_t(conn_info);
+ } else {
+ BT_ERR("RFCOMM client conn_info not found");
+ }
+ return FALSE;
+}
+
+static void __rfcomm_client_connection_create_watch(rfcomm_client_conn_info_t *conn_info)
+{
+ GIOChannel *data_io;
+
+ ret_if(NULL == conn_info);
+
+ BT_DBG("+");
+
+ data_io = g_io_channel_unix_new(conn_info->sock_fd);
+ g_io_channel_set_encoding(data_io, NULL, NULL);
+ g_io_channel_set_flags(data_io, G_IO_FLAG_NONBLOCK, NULL);
+ conn_info->watch_id = g_io_add_watch(data_io,
+ G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
+ __client_data_received_cb, NULL);
+ g_io_channel_unref(data_io);
+
+ BT_DBG("-");
+}
+
+static void __bt_rfcomm_handle_new_client_connection(bluetooth_rfcomm_connection_t *info)
+{
+ rfcomm_client_conn_info_t *conn_info;
+
+ ret_if(NULL == info);
+
+ BT_DBG("+");
+
+ conn_info = g_malloc0(sizeof(rfcomm_client_conn_info_t));
+ conn_info->remote_addr = g_malloc0(BT_ADDRESS_STRING_SIZE);
+ _bt_convert_addr_type_to_string(
+ conn_info->remote_addr, info->device_addr.addr);
+ conn_info->uuid = g_strdup(info->uuid);
+ conn_info->sock_fd = info->socket_fd;
+
+ BT_DBG("Address:%s, UUID:%s Socket: %d",
+ conn_info->remote_addr, conn_info->uuid, conn_info->sock_fd);
+
+ rfcomm_clients = g_slist_append(rfcomm_clients, conn_info);
+ __rfcomm_client_connection_create_watch(conn_info);
+
+ BT_DBG("-");
+}
+
+static void __bt_fill_garray_from_variant(GVariant *var, GArray *param)
+{
+ char *data;
+ int size;
+
+ size = g_variant_get_size(var);
+ if (size > 0) {
+ data = (char *)g_variant_get_data(var);
+ if (data)
+ param = g_array_append_vals(param, data, size);
+
+ }
+}
+
+
+/* TODO_40 : 4.0 merge */
+/* Don't use this function directly. Instead of it, get the out parameter only */
+static void __bt_get_event_info(int service_function, GArray *output,
+ int *event, int *event_type, void **param_data)
+{
+ ret_if(event == NULL);
+
+ BT_DBG("service_function : %s (0x%x)",
+ _bt_convert_service_function_to_string(service_function),
+ service_function);
+ switch (service_function) {
+ case BT_RFCOMM_CLIENT_CONNECT:
+ *event_type = BT_RFCOMM_CLIENT_EVENT;
+ *event = BLUETOOTH_EVENT_RFCOMM_CONNECTED;
+ ret_if(output == NULL);
+ *param_data = &g_array_index(output,
+ bluetooth_rfcomm_connection_t, 0);
+ break;
+ default:
+ BT_ERR("Unknown function");
+ return;
+ }
+}
+
+
+static void __async_req_cb_with_unix_fd_list(GDBusProxy *proxy, GAsyncResult *res, gpointer user_data)
+{
+ int result = BLUETOOTH_ERROR_NONE;
+ int event_type = BT_ADAPTER_EVENT;
+ bt_req_info_t *cb_data = user_data;
+ bluetooth_event_param_t bt_event;
+
+ GError *error = NULL;
+ GVariant *value;
+ GVariant *param1;
+ GArray *out_param1 = NULL;
+ GUnixFDList *out_fd_list = NULL;
+
+ BT_DBG("+");
+
+ memset(&bt_event, 0x00, sizeof(bluetooth_event_param_t));
+
+ value = g_dbus_proxy_call_with_unix_fd_list_finish(proxy, &out_fd_list, res, &error);
+ if (value == NULL) {
+ if (error) {
+ /* dBUS gives error cause */
+ BT_ERR("D-Bus API failure: message[%s]",
+ error->message);
+ g_clear_error(&error);
+ }
+ result = BLUETOOTH_ERROR_TIMEOUT;
+
+ ret_if(cb_data == NULL);
+
+ __bt_get_event_info(cb_data->service_function, NULL,
+ &bt_event.event, &event_type,
+ &bt_event.param_data);
+ goto failed;
+ }
+
+ g_variant_get(value, "(iv)", &result, ¶m1);
+ g_variant_unref(value);
+
+ if (param1) {
+ out_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
+ __bt_fill_garray_from_variant(param1, out_param1);
+ g_variant_unref(param1);
+ }
+
+ if (!cb_data)
+ goto done;
+
+ __bt_get_event_info(cb_data->service_function, out_param1,
+ &bt_event.event, &event_type,
+ &bt_event.param_data);
+
+ if (result == BLUETOOTH_ERROR_NONE && out_param1) {
+ if (BT_RFCOMM_CLIENT_CONNECT == cb_data->service_function) {
+ int *fd_list_array;
+ int len = 0;
+ bluetooth_rfcomm_connection_t *conn_info;
+
+ conn_info = (bluetooth_rfcomm_connection_t *)bt_event.param_data;
+ if (!out_fd_list) {
+ BT_ERR("out_fd_list is NULL");
+ goto failed;
+ }
+
+ fd_list_array = g_unix_fd_list_steal_fds(out_fd_list, &len);
+ BT_INFO("Num fds in fd_list is : %d, fd_list[0]: %d", len, fd_list_array[0]);
+ conn_info->socket_fd = fd_list_array[0];
+
+ BT_DBG("conn_info->socket_fd: %d", conn_info->socket_fd);
+ __bt_rfcomm_handle_new_client_connection(conn_info);
+
+ if (cb_data->cb != NULL) {
+ /* Send client connected event */
+ bt_event.result = result;
+ BT_INFO("event_type[%d], result=[%d]", event_type, result);
+ ((bluetooth_cb_func_ptr)cb_data->cb)(
+ bt_event.event, &bt_event, cb_data->user_data);
+ }
+
+ g_free(fd_list_array);
+ g_object_unref(out_fd_list);
+ }
+ goto done;
+ }
+
+failed:
+ if (cb_data->cb == NULL)
+ goto done;
+
+ /* Only if fail case, call the callback function*/
+ bt_event.result = result;
+
+ BT_INFO("event_type[%d], result=[%d]", event_type, result);
+ if (event_type == BT_RFCOMM_CLIENT_EVENT) {
+ ((bluetooth_cb_func_ptr)cb_data->cb)(bt_event.event,
+ &bt_event, cb_data->user_data);
+ } else {
+ BT_INFO("Not handled event type : %d", event_type);
+ }
+done:
+ if (out_param1)
+ g_array_free(out_param1, TRUE);
+
+ g_free(cb_data);
+ BT_DBG("-");
+}
+#endif
BT_EXPORT_API int bluetooth_rfcomm_connect(
const bluetooth_device_address_t *remote_bt_address,
const char *remote_uuid)
{
-#ifdef RFCOMM_DIRECT
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
rfcomm_cb_data_t *cb_data = NULL;
rfcomm_conn_info_t *conn = NULL;
#else
BT_CHECK_PARAMETER(remote_uuid, return);
BT_CHECK_ENABLED(return);
-#ifdef RFCOMM_DIRECT
- BT_INFO_C("<<<<<<<<< RFCOMM Connect request from app >>>>>>>>>>>");
+#ifdef TIZEN_FEATURE_BT_DPM
+ if (_bt_check_dpm(BT_DPM_ADDRESS, (void *)remote_bt_address) == BT_DPM_RESTRICTED) {
+ BT_ERR("Blacklist device");
+ return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
+ }
+
+ if (_bt_check_dpm(BT_DPM_SPP, NULL) == BT_DPM_RESTRICTED ||
+ _bt_check_dpm(BT_DPM_HF_ONLY, NULL) == BT_DPM_RESTRICTED) {
+ BT_ERR("Not allow to connect the RFCOMM service");
+ return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
+ }
+
+ if (_bt_check_dpm(BT_DPM_DESKTOP, NULL) == BT_DPM_RESTRICTED) {
+ char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+ bluetooth_device_class_t dev_class;
+
+ _bt_convert_addr_type_to_string(address, (unsigned char *)remote_bt_address->addr);
+ _bt_get_cod_by_address(address, &dev_class);
+
+ if (dev_class.major_class == BLUETOOTH_DEVICE_MAJOR_CLASS_COMPUTER) {
+ BT_ERR("Reject a authorization due to MDM Policy");
+ return BLUETOOTH_ERROR_DEVICE_POLICY_RESTRICTION;
+ }
+ }
+#endif
+
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
+ BT_INFO_C("### Connect RFCOMM");
int ret;
int id, object_id;
char *path;
- if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
+ if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
== BLUETOOTH_ERROR_PERMISSION_DEINED) {
BT_ERR("Don't have a privilege to use this API");
return BLUETOOTH_ERROR_PERMISSION_DEINED;
object_id = _bt_register_new_conn(path, new_connection);
if (object_id < 0) {
__rfcomm_delete_id(id);
+ g_free(path);
return BLUETOOTH_ERROR_INTERNAL;
}
cb_data->object_id = object_id;
cb_data->id = id;
}
+
conn = g_new0(rfcomm_conn_info_t, 1);
conn->fd = -1;
_bt_convert_addr_type_to_string(conn->bt_addr,
BT_DBG("Connecting to %s uuid %s", conn->bt_addr, remote_uuid);
cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
+
ret = _bt_discover_services(conn->bt_addr, (char *)remote_uuid,
__bt_discover_service_response_cb, cb_data);
if (ret != BLUETOOTH_ERROR_NONE) {
rfcomm_cb_data_remove(cb_data);
return BLUETOOTH_ERROR_INTERNAL;
}
+
if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
BT_INFO("Adding callback information to rfcomm_clients");
rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
/* In now, we only support to connecty using UUID */
connect_type = BT_RFCOMM_UUID;
- if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_CLIENT_CONNECT)
+ if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_CLIENT_CONNECT)
== BLUETOOTH_ERROR_PERMISSION_DEINED) {
BT_ERR("Don't have a privilege to use this API");
return BLUETOOTH_ERROR_PERMISSION_DEINED;
g_array_append_vals(in_param3, &connect_type, sizeof(int));
- result = _bt_send_request_async(BT_BLUEZ_SERVICE,
+ result = _bt_send_request_async_with_unix_fd_list(BT_BLUEZ_SERVICE,
BT_RFCOMM_CLIENT_CONNECT,
in_param1, in_param2,
in_param3, in_param4,
- user_info->cb, user_info->user_data);
+ user_info->cb, user_info->user_data,
+ NULL, (GAsyncReadyCallback)__async_req_cb_with_unix_fd_list);
BT_DBG("result: %x", result);
#endif
}
-BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(bluetooth_device_address_t *device_address, gboolean *connected)
+BT_EXPORT_API int bluetooth_rfcomm_client_is_connected(const bluetooth_device_address_t *device_address, gboolean *connected)
{
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
GSList *l;
GSList *conn_list = NULL;
rfcomm_cb_data_t *client_info;
BT_CHECK_PARAMETER(device_address, return);
BT_CHECK_PARAMETER(connected, return);
- _bt_convert_addr_type_to_string(address, device_address->addr);
+ _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
*connected = FALSE;
for (l = rfcomm_clients; l != NULL; l = l->next) {
client_info = l->data;
if (client_info == NULL)
continue;
- for(conn_list = client_info->rfcomm_conns;
+ for (conn_list = client_info->rfcomm_conns;
conn_list != NULL; conn_list = conn_list->next) {
conn_info = conn_list->data;
- if(conn_info == NULL)
+ if (conn_info == NULL)
continue;
if (g_strcmp0(address, conn_info->bt_addr) == 0) {
}
return BLUETOOTH_ERROR_NONE;
+#else
+ GSList *l;
+ char address[BT_ADDRESS_STRING_SIZE] = { 0 };
+
+ BT_CHECK_PARAMETER(device_address, return);
+ BT_CHECK_PARAMETER(connected, return);
+
+ BT_DBG("+");
+
+ *connected = FALSE;
+ _bt_convert_addr_type_to_string(address, (unsigned char *)device_address->addr);
+ BT_INFO("Client address: [%s]", address);
+
+ for (l = rfcomm_clients; l != NULL; l = l->next) {
+ rfcomm_client_conn_info_t *info = l->data;
+
+ if (info && !strncasecmp(info->remote_addr, address, BT_ADDRESS_STRING_SIZE)) {
+ BT_INFO("Match found");
+ *connected = TRUE;
+ return BLUETOOTH_ERROR_NONE;
+ }
+ }
+
+ BT_DBG("-");
+ return BLUETOOTH_ERROR_NONE;
+#endif
}
BT_EXPORT_API gboolean bluetooth_rfcomm_is_client_connected(void)
BT_EXPORT_API int bluetooth_rfcomm_disconnect(int socket_fd)
{
-#ifdef RFCOMM_DIRECT
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
rfcomm_cb_data_t *info;
rfcomm_conn_info_t *conn_info;
- BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
+
+ BT_INFO_C("### Disconnect RFCOMM");
+
BT_CHECK_ENABLED(return);
- if (_bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_DISCONNECT)
+ if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
== BLUETOOTH_ERROR_PERMISSION_DEINED) {
BT_ERR("Don't have a privilege to use this API");
return BLUETOOTH_ERROR_PERMISSION_DEINED;
}
+ BT_DBG("Requested FD %d", socket_fd);
if (socket_fd < 0) {
BT_ERR("Invalid FD");
return BLUETOOTH_ERROR_INVALID_PARAM;
BT_DBG("Could not find in client, so check in server");
return bluetooth_rfcomm_server_disconnect(socket_fd);
}
+
conn_info = __get_conn_info_from_fd(info, socket_fd);
if (conn_info == NULL) {
- BT_ERR("FATAL Error");
+ BT_ERR("Could not find connection info");
return BLUETOOTH_ERROR_INTERNAL;
}
- if (conn_info->watch_id <= 0) {
+
+ if (conn_info->watch_id == 0 || conn_info->disconnected) {
BT_ERR("Invalid state");
return BLUETOOTH_ERROR_NOT_CONNECTED;
}
+
+ close(conn_info->fd);
conn_info->disconnected = TRUE;
- close(socket_fd);
+
BT_INFO("conn_info %s", conn_info->bt_addr);
- _bt_disconnect_profile(conn_info->bt_addr, info->uuid, NULL,NULL);
+ _bt_disconnect_ext_profile(conn_info->bt_addr, info->obj_path);
+
if (info->idle_id == 0)
info->idle_id = g_idle_add(__rfcomm_client_disconnect, info);
return BLUETOOTH_ERROR_NONE;
#else
- int result;
- int service_function;
+ rfcomm_client_conn_info_t *conn_info;
- BT_CHECK_ENABLED(return);
+ BT_INFO_C("<<<<<<<<< RFCOMM Disconnect request from app >>>>>>>>");
- BT_INIT_PARAMS();
- BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ BT_CHECK_ENABLED(return);
+ retv_if(socket_fd < 0, BLUETOOTH_ERROR_INVALID_PARAM);
- /* Support the OSP */
- if (socket_fd == -1) {
- /* Cancel connect */
- service_function = BT_RFCOMM_CLIENT_CANCEL_CONNECT;
- } else {
- g_array_append_vals(in_param1, &socket_fd, sizeof(int));
- service_function = BT_RFCOMM_SOCKET_DISCONNECT;
+ if (_bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_DISCONNECT)
+ == BLUETOOTH_ERROR_PERMISSION_DEINED) {
+ BT_ERR("Don't have a privilege to use this API");
+ return BLUETOOTH_ERROR_PERMISSION_DEINED;
}
- result = _bt_send_request(BT_BLUEZ_SERVICE, service_function,
- in_param1, in_param2, in_param3, in_param4, &out_param);
+ BT_DBG("FD %d", socket_fd);
- BT_DBG("result: %x", result);
+ conn_info = __find_rfcomm_conn_info_with_fd(socket_fd);
+ if (conn_info == NULL) {
+ BT_DBG("Could not find in client, so check in server");
+ /* Check for fd in server list and perform the disconnection if present */
+ return bluetooth_rfcomm_server_disconnect(socket_fd);
+ }
- BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
+ if (conn_info->watch_id <= 0) {
+ BT_ERR("Invalid state");
+ return BLUETOOTH_ERROR_NOT_CONNECTED;
+ }
- return result;
+ /*
+ * Just close socket here and return. Socket close will be detected via I/O watch
+ * and disconnection event as well as info cleanup will be performed there.
+ */
+ close(conn_info->sock_fd);
+
+ return BLUETOOTH_ERROR_NONE;
#endif
}
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
+#else
+static int __write_all(int fd, const char *buf, int len)
+{
+ int sent = 0, try = 0;
+
+ BT_DBG("+");
+ while (len > 0) {
+ int written;
+
+ written = write(fd, buf, len);
+ BT_DBG("written: %d", written);
+ if (written < 0) {
+ if (errno == EINTR || errno == EAGAIN) {
+ try++;
+ if (try <= 49)
+ continue;
+ }
+ return -1;
+ }
+
+ if (!written)
+ return 0;
+
+ len -= written;
+ buf += written;
+ sent += written;
+ try = 0;
+ }
+
+ BT_DBG("-");
+ return sent;
+}
+#endif
+
BT_EXPORT_API int bluetooth_rfcomm_write(int fd, const char *buf, int length)
{
-#ifdef RFCOMM_DIRECT
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
int written;
-#else
- char *buffer;
#endif
int result;
- BT_CHECK_PARAMETER(buf, return);
-#ifndef RFCOMM_DIRECT
+#ifndef TIZEN_FEATURE_BT_RFCOMM_DIRECT
BT_CHECK_ENABLED(return);
#endif
+ BT_CHECK_PARAMETER(buf, return);
+ if (fd < 0) {
+ BT_ERR("Invalid FD");
+ return BLUETOOTH_ERROR_INVALID_PARAM;
+ }
+
retv_if(length <= 0, BLUETOOTH_ERROR_INVALID_PARAM);
-#ifdef RFCOMM_DIRECT
switch (privilege_token) {
case 0:
- result = _bt_check_privilege(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE);
+ result = _bt_check_privilege(BT_CHECK_PRIVILEGE, BT_RFCOMM_SOCKET_WRITE);
if (result == BLUETOOTH_ERROR_NONE) {
privilege_token = 1; /* Have a permission */
return BLUETOOTH_ERROR_INTERNAL;
}
+#ifdef TIZEN_FEATURE_BT_RFCOMM_DIRECT
written = write(fd, buf, length);
/*BT_DBG("Length %d, written = %d, balance(%d)",
- length, written, length - written); */
+ length, written, length - written); */
+ if (written > 0 && bluetooth_get_battery_monitor_state()) {
+ if (tx_tag == 0) {
+ BT_DBG("Adding rfcomm tx timeout function for battery monitor");
+ tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);
+ }
+ tx_data += written;
+ }
return written;
#else
- BT_INIT_PARAMS();
- BT_ALLOC_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
-
- buffer = g_malloc0(length + 1);
-
- memcpy(buffer, buf, length);
-
- g_array_append_vals(in_param1, &fd, sizeof(int));
- g_array_append_vals(in_param2, &length, sizeof(int));
- g_array_append_vals(in_param3, buffer, length);
-
- result = _bt_send_request(BT_BLUEZ_SERVICE, BT_RFCOMM_SOCKET_WRITE,
- in_param1, in_param2, in_param3, in_param4, &out_param);
-
- BT_DBG("result: %x", result);
-
- BT_FREE_PARAMS(in_param1, in_param2, in_param3, in_param4, out_param);
-
- g_free(buffer);
-
+ result = __write_all(fd, buf, length);
+ if (result > 0 && bluetooth_get_battery_monitor_state()) {
+ if (tx_tag == 0) {
+ BT_DBG("Adding rfcomm tx timeout function for battery monitor");
+ tx_tag = g_timeout_add_seconds(BATTERY_MONITOR_RFCOMM_INTERVAL, (GSourceFunc)__rfcomm_record_tx_data, NULL);
+ }
+ tx_data += written;
+ }
return result;
#endif
}
-