#define BT_HAL_RFCOMM_MAX_BUFFER_SIZE 1024
typedef struct {
- char uuid[BT_HAL_UUID_STRING_LEN];
- char *device_path;
- char *obj_path;
- int object_id;
- int id;
- GSList *rfcomm_conns;
-} rfcomm_cb_data_t;
-
-typedef struct {
char remote_addr[BT_HAL_ADDRESS_STRING_SIZE];
int hal_fd;
unsigned int hal_watch;
unsigned int bt_watch;
} rfcomm_conn_info_t;
+typedef struct {
+ char uuid[BT_HAL_UUID_STRING_LEN];
+ char *device_path;
+ char *obj_path;
+ int object_id;
+ int id;
+ rfcomm_conn_info_t *conn_info;
+} rfcomm_cb_data_t;
+
static GSList *rfcomm_clients;
static int latest_id = -1;
static gboolean id_used[BT_HAL_RFCOMM_ID_MAX];
DBG("id: %d, latest_id: %d", id, latest_id);
}
-static rfcomm_cb_data_t *__find_rfcomm_info_from_uuid(const char *uuid)
+static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
{
GSList *l;
for (l = rfcomm_clients; l != NULL; l = l->next) {
rfcomm_cb_data_t *info = l->data;
- if (g_strcmp0(info->uuid, uuid) == 0)
- return info;
+ if (info != NULL)
+ if (g_strcmp0(info->obj_path, path) == 0)
+ return info;
}
return NULL;
}
-static rfcomm_cb_data_t *__find_rfcomm_info_from_path(const char *path)
+static void __bt_free_conn(rfcomm_conn_info_t *conn)
{
- GSList *l;
+ DBG("+");
- for (l = rfcomm_clients; l != NULL; l = l->next) {
- rfcomm_cb_data_t *info = l->data;
+ if (conn == NULL)
+ return;
- if (info != NULL)
- if (g_strcmp0(info->obj_path, path) == 0)
- return info;
+ if (0 < conn->hal_fd)
+ close(conn->hal_fd);
+
+ if (conn->hal_watch > 0) {
+ g_source_remove(conn->hal_watch);
+ conn->hal_watch = 0;
}
- return NULL;
+ if (0 < conn->stack_fd)
+ close(conn->stack_fd);
+
+ if (conn->bt_watch > 0) {
+ g_source_remove(conn->bt_watch);
+ conn->bt_watch = 0;
+ }
+
+ g_free(conn);
+ DBG("-");
}
static void __bt_free_cb_data(rfcomm_cb_data_t *cb_data)
return;
}
- if (info->rfcomm_conns == NULL) {
- INFO("No more device connected remove info");
- rfcomm_clients = g_slist_remove(rfcomm_clients, info);
- __bt_free_cb_data(info);
- }
-}
-
-static rfcomm_conn_info_t *__find_conn_info_with_stack_fd(rfcomm_cb_data_t *info, int fd)
-{
- GSList *l;
-
- for (l = info->rfcomm_conns; l != NULL; l = l->next) {
- rfcomm_conn_info_t *conn_info = l->data;
-
- if (conn_info && (conn_info->stack_fd == fd))
- return conn_info;
- }
-
- return NULL;
-}
-
-static rfcomm_conn_info_t *__find_conn_info_with_hal_fd(rfcomm_cb_data_t *info, int fd)
-{
- GSList *l;
- for (l = info->rfcomm_conns; l != NULL; l = l->next) {
- rfcomm_conn_info_t *conn_info = l->data;
-
- if (conn_info && (conn_info->hal_fd == fd))
- return conn_info;
- }
-
- return NULL;
-}
-
-static gint compare(gpointer *a, gpointer *b)
-{
- rfcomm_conn_info_t *node = (rfcomm_conn_info_t *)a;
- char *address = (char *)b;
- return g_strcmp0(node->remote_addr, address);
-}
-
-static rfcomm_conn_info_t *__get_conn_info_from_address(rfcomm_cb_data_t *info,
- char *dev_address)
-{
- GSList *l = NULL;
- rfcomm_conn_info_t *conn_info = NULL;
- l = g_slist_find_custom(info->rfcomm_conns, dev_address,
- (GCompareFunc)compare);
- if (l)
- conn_info = l->data;
- return conn_info;
-}
-
-static void __bt_free_conn(rfcomm_conn_info_t *conn)
-{
- DBG("+");
-
- if (conn == NULL)
- return;
-
- if (0 < conn->hal_fd)
- close(conn->hal_fd);
-
- if (conn->hal_watch > 0) {
- g_source_remove(conn->hal_watch);
- conn->hal_watch = 0;
- }
-
- if (0 < conn->stack_fd)
- close(conn->stack_fd);
-
- if (conn->bt_watch > 0) {
- g_source_remove(conn->bt_watch);
- conn->bt_watch = 0;
- }
-
- g_free(conn);
- DBG("-");
-}
-
-static void __rfcomm_remove_conn_info_t(rfcomm_cb_data_t *info, char *address)
-{
- rfcomm_conn_info_t *conn_info;
-
- conn_info = __get_conn_info_from_address(info, address);
- if (conn_info) {
- info->rfcomm_conns = g_slist_remove(info->rfcomm_conns, conn_info);
- __bt_free_conn(conn_info);
- }
+ rfcomm_clients = g_slist_remove(rfcomm_clients, info);
+ __bt_free_conn(info->conn_info);
+ __bt_free_cb_data(info);
}
static int write_all(int fd, unsigned char *buf, int len)
DBG("+");
fd = g_io_channel_unix_get_fd(io);
- conn_info = __find_conn_info_with_hal_fd(info, fd);
+ conn_info = info->conn_info;
if (cond & G_IO_HUP) {
ERR("Socket %d hang up", fd);
DBG("-");
return TRUE;
fail:
- __rfcomm_remove_conn_info_t(info, conn_info->remote_addr);
__rfcomm_cb_data_remove(info);
return FALSE;
}
DBG("+");
fd = g_io_channel_unix_get_fd(io);
- conn_info = __find_conn_info_with_stack_fd(info, fd);
+ conn_info = info->conn_info;
if (cond & G_IO_HUP) {
ERR("Socket %d hang up", fd);
DBG("-");
return TRUE;
fail:
- __rfcomm_remove_conn_info_t(info, conn_info->remote_addr);
__rfcomm_cb_data_remove(info);
return FALSE;
}
return -1;
}
- _bt_convert_addr_type_to_string(address, addr->address);
info = __find_rfcomm_info_from_path(path);
if (info == NULL)
return -1;
- conn_info = __get_conn_info_from_address(info, address);
+ conn_info = info->conn_info;
+ _bt_convert_addr_type_to_string(address, addr->address);
if (conn_info == NULL) {
- ERR("Device Address %s not found in connection list", address);
+ ERR("conn_info is NULL for dev:[%s]", address);
return -1;
}
return 0;
fail:
- __rfcomm_remove_conn_info_t(info, address);
__rfcomm_cb_data_remove(info);
return -1;
}
{
GError *error = NULL;
rfcomm_cb_data_t *cb_data;
- char dev_address[BT_HAL_ADDRESS_STRING_SIZE];
- const char *path;
DBG("+");
if (!g_dbus_proxy_call_finish(proxy, res, &error)) {
ERR("Error : %s \n", error->message);
- path = g_dbus_proxy_get_object_path(proxy);
- _bt_convert_device_path_to_address(path, dev_address);
- __rfcomm_remove_conn_info_t(cb_data, dev_address);
__rfcomm_cb_data_remove(cb_data);
g_error_free(error);
}
g_object_unref(proxy);
if (err != NULL) {
ERR("Error occured in Proxy call [%s]\n", err->message);
- __rfcomm_remove_conn_info_t(cb_data, dev_address);
__rfcomm_cb_data_remove(cb_data);
goto done;
} else {
__bt_connect_response_cb, cb_data);
if (ret != BT_STATUS_SUCCESS) {
ERR("ConnectProfile failed");
- __rfcomm_remove_conn_info_t(cb_data, dev_address);
__rfcomm_cb_data_remove(cb_data);
goto done;
}
} else {
ERR("remote uuid not found");
- __rfcomm_remove_conn_info_t(cb_data, dev_address);
__rfcomm_cb_data_remove(cb_data);
}
}
DBG("+");
- cb_data = __find_rfcomm_info_from_uuid(remote_uuid);
- if (!cb_data) {
- id = __rfcomm_assign_id();
- if (id < 0) {
- ERR("__rfcomm_assign_id failed");
- return NULL;
- }
-
- path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
- object_id = _bt_register_new_gdbus_object(path, __new_connection);
- if (object_id < 0) {
- ERR("_bt_register_new_gdbus_object failed");
- __rfcomm_delete_id(id);
- return NULL;
- }
+ id = __rfcomm_assign_id();
+ if (id < 0) {
+ ERR("__rfcomm_assign_id failed");
+ return NULL;
+ }
- cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
- g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
- cb_data->obj_path = path;
- cb_data->object_id = object_id;
- cb_data->id = id;
+ path = g_strdup_printf("/org/socket/client/%d/%d", getpid(), id);
+ object_id = _bt_register_new_gdbus_object(path, __new_connection);
+ if (object_id < 0) {
+ ERR("_bt_register_new_gdbus_object failed");
+ __rfcomm_delete_id(id);
+ return NULL;
}
+ cb_data = g_malloc0(sizeof(rfcomm_cb_data_t));
+ g_strlcpy(cb_data->uuid, remote_uuid, BT_HAL_UUID_STRING_LEN);
+ cb_data->obj_path = path;
+ cb_data->object_id = object_id;
+ cb_data->id = id;
+
DBG("-");
return cb_data;
}
_bt_convert_addr_type_to_string(remote_addr, addr);
DBG("Connecting to %s, uuid %s", remote_addr, remote_uuid);
conn = __rfcomm_create_conn_info(remote_addr, sock);
- if (!conn)
+ if (!conn) {
+ __rfcomm_cb_data_remove(cb_data);
return BT_STATUS_FAIL;
+ }
- cb_data->rfcomm_conns = g_slist_append(cb_data->rfcomm_conns, conn);
+ cb_data->conn_info = conn;
ret = _bt_discover_services(remote_addr, (char *)remote_uuid,
__bt_discover_service_response_cb, cb_data);
if (ret != BT_STATUS_SUCCESS) {
ERR("Error returned while service discovery");
- __rfcomm_remove_conn_info_t(cb_data, conn->remote_addr);
- __rfcomm_cb_data_remove(cb_data);
+ __bt_free_conn(conn);
+ __bt_free_cb_data(cb_data);
return BT_STATUS_FAIL;
}
- if (g_slist_find(rfcomm_clients, cb_data) == NULL) {
- INFO("Adding callback information to rfcomm_clients");
- rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
- } else
- INFO("Callback information is already added");
+ INFO("Adding callback information to rfcomm_clients");
+ rfcomm_clients = g_slist_append(rfcomm_clients, cb_data);
return BT_STATUS_SUCCESS;
}