-static gboolean __bt_rfcomm_client_data_received_cb(GIOChannel *chan,
- GIOCondition cond,
- gpointer data)
-{
- char *buffer = NULL;
- gsize len;
- int result = BLUETOOTH_ERROR_NONE;
- bt_rfcomm_info_t *client_info = data;
-
- BT_DBG("condition: %d", cond);
-
- retv_if(client_info == NULL, FALSE);
-
- if (cond & (G_IO_NVAL | G_IO_HUP | G_IO_ERR)) {
- BT_ERR("Unix client disconnected (fd=%d)\n", client_info->fd);
- __bt_rfcomm_terminate_client(client_info->fd);
- return FALSE;
- }
-
- buffer = g_malloc0(BT_RFCOMM_BUFFER_MAX + 1);
-
- if (g_io_channel_read_chars(chan, buffer, BT_RFCOMM_BUFFER_MAX,
- &len, NULL) == G_IO_STATUS_ERROR) {
- BT_ERR("IO Channel read error client");
- g_free(buffer);
- __bt_rfcomm_terminate_client(client_info->fd);
- return FALSE;
- }
-
- if (len == 0) {
- BT_ERR("Read failed len=%d, fd=%d\n", len, client_info->fd);
- g_free(buffer);
- __bt_rfcomm_terminate_client(client_info->fd);
- return FALSE;
- }
-
- BT_DBG("%s - clientfd = %d", buffer, client_info->fd);
-
- _bt_send_event(BT_RFCOMM_CLIENT_EVENT,
- BLUETOOTH_EVENT_RFCOMM_DATA_RECEIVED,
- DBUS_TYPE_INT32, &result,
- DBUS_TYPE_INT16, &client_info->fd,
- DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
- &buffer, len,
- DBUS_TYPE_INVALID);
-
- g_free(buffer);
-
- return TRUE;
-}
-
-static void __bt_rfcomm_connected_cb(DBusGProxy *proxy, DBusGProxyCall *call,
- gpointer user_data)
-{
- BT_DBG("+\n");
- GError *err = NULL;
- gchar *rfcomm_device_node;
- int socket_fd = -1;
- int result = BLUETOOTH_ERROR_NONE;
- bt_rfcomm_info_t *client_info = NULL;
- request_info_t *req_info;
- bluetooth_rfcomm_connection_t conn_info;
- GArray *out_param1;
- GArray *out_param2;
-
- dbus_g_proxy_end_call(proxy, call, &err,
- G_TYPE_STRING, &rfcomm_device_node, G_TYPE_INVALID);
-
- g_object_unref(proxy);
-
- if (rfcomm_info == NULL) {
- BT_ERR("rfcomm_info == NULL");
- goto done;
- }
-
- if (err != NULL) {
- BT_ERR("Error occured in connecting port [%s]", err->message);
-
- if (!strcmp("Host is down", err->message))
- result = BLUETOOTH_ERROR_HOST_DOWN;
- else
- result = BLUETOOTH_ERROR_CONNECTION_ERROR;
-
- goto dbus_return;
- }
-
- BT_DBG("Succss Connect REMOTE Device RFCOMM Node[%s]", rfcomm_device_node);
-
- socket_fd = __bt_rfcomm_open_socket(rfcomm_device_node);
-
- if (socket_fd < 0) {
- BT_ERR("Fail to open socket: %d", socket_fd);
- goto dbus_return;
- }
-
- client_info = g_malloc0(sizeof(bt_rfcomm_info_t));
-
- client_info->fd = socket_fd;
- client_info->dev_node = g_strdup(rfcomm_device_node);
- client_info->address = g_strdup(rfcomm_info->address);
- client_info->uuid = g_strdup(rfcomm_info->uuid);
- client_info->io_channel = g_io_channel_unix_new(socket_fd);
- client_info->io_event = g_io_add_watch(client_info->io_channel,
- G_IO_IN | G_IO_HUP | G_IO_ERR | G_IO_NVAL,
- __bt_rfcomm_client_data_received_cb,
- client_info);
-
- g_io_channel_set_close_on_unref(client_info->io_channel, TRUE);
- g_io_channel_set_flags(client_info->io_channel,
- G_IO_FLAG_NONBLOCK, NULL);
-
- client_list = g_slist_append(client_list, client_info);
-
- _bt_send_event(BT_RFCOMM_CLIENT_EVENT,
- BLUETOOTH_EVENT_RFCOMM_CONNECTED,
- DBUS_TYPE_INT32, &result,
- DBUS_TYPE_STRING, &rfcomm_info->address,
- DBUS_TYPE_STRING, &rfcomm_info->uuid,
- DBUS_TYPE_INT16, &socket_fd,
- DBUS_TYPE_INVALID);
-
-dbus_return:
- req_info = _bt_get_request_info(rfcomm_info->req_id);
-
- if (req_info == NULL || req_info->context == NULL)
- goto done;
-
- memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
- conn_info.device_role = RFCOMM_ROLE_CLIENT;
- g_strlcpy(conn_info.uuid, rfcomm_info->uuid,
- BLUETOOTH_UUID_STRING_MAX);
-
- if (client_info)
- conn_info.socket_fd = client_info->fd;
- else
- conn_info.socket_fd = -1;
-
- _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
- rfcomm_info->address);
-
- out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
- out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
-
- g_array_append_vals(out_param1, &conn_info,
- sizeof(bluetooth_rfcomm_connection_t));
- g_array_append_vals(out_param2, &result, sizeof(int));
- dbus_g_method_return(req_info->context, out_param1, out_param2);
-
- g_array_free(out_param1, TRUE);
- g_array_free(out_param2, TRUE);
- _bt_delete_request_list(req_info->req_id);
-done:
- if (err)
- g_error_free(err);
-
- ret_if(rfcomm_info == NULL);
-
- g_free(rfcomm_info->address);
- g_free(rfcomm_info->uuid);
- g_free(rfcomm_info->channel);
- g_free(rfcomm_info);
- rfcomm_info = NULL;
-}
-
-static void __bt_rfcomm_discover_services_cb(DBusGProxy *proxy, DBusGProxyCall *call,
- gpointer user_data)
-{
- GError *err = NULL;
- GHashTable *hash = NULL;
- const char *dev_path = NULL;
- DBusGConnection *conn;
- DBusGProxy *rfcomm_proxy;
- int result = BLUETOOTH_ERROR_NONE;
- GArray *out_param1;
- GArray *out_param2;
- request_info_t *req_info;
- bluetooth_rfcomm_connection_t conn_info;
-
- dbus_g_proxy_end_call(proxy, call, &err,
- dbus_g_type_get_map("GHashTable",
- G_TYPE_UINT, G_TYPE_STRING),
- &hash, G_TYPE_INVALID);
-
- if (err != NULL) {
- BT_ERR("Error occured in Proxy call [%s]\n", err->message);
- result = BLUETOOTH_ERROR_CONNECTION_ERROR;
- g_error_free(err);
- goto fail;
- }
-
- g_hash_table_destroy(hash);
-
- if (rfcomm_info == NULL) {
- result = BLUETOOTH_ERROR_INTERNAL;
- goto fail;
- }
-
- conn = _bt_get_system_gconn();
- if (conn == NULL) {
- BT_ERR("ERROR: Can't get on system bus");
- result = BLUETOOTH_ERROR_INTERNAL;
- goto fail;
- }
-
- dev_path = dbus_g_proxy_get_path(proxy);
-
- rfcomm_proxy = dbus_g_proxy_new_for_name(conn, BT_BLUEZ_NAME,
- dev_path,
- BT_SERIAL_INTERFACE);
-
- g_object_unref(proxy);
- proxy = NULL;
-
- if (rfcomm_proxy == NULL) {
- BT_ERR("Failed to get the rfcomm proxy\n");
- result = BLUETOOTH_ERROR_SERVICE_NOT_FOUND;
- goto fail;
- }
-
- rfcomm_info->rfcomm_proxy = rfcomm_proxy;
-
- if (!dbus_g_proxy_begin_call(rfcomm_proxy, "Connect",
- (DBusGProxyCallNotify)__bt_rfcomm_connected_cb,
- NULL, /*user_data*/
- NULL, /*destroy*/
- G_TYPE_STRING, rfcomm_info->uuid,
- G_TYPE_INVALID)) {
- BT_ERR("RFCOMM connect Dbus Call Error");
- g_object_unref(rfcomm_proxy);
- result = BLUETOOTH_ERROR_INTERNAL;
- goto fail;
- }
-
- BT_DBG("-\n");
-
- return;
-fail:
- if (proxy)
- g_object_unref(proxy);
-
- ret_if(rfcomm_info == NULL);
-
- req_info = _bt_get_request_info(rfcomm_info->req_id);
-
- if (req_info && req_info->context) {
- memset(&conn_info, 0x00, sizeof(bluetooth_rfcomm_connection_t));
-
- conn_info.device_role = RFCOMM_ROLE_CLIENT;
- g_strlcpy(conn_info.uuid, rfcomm_info->uuid,
- BLUETOOTH_UUID_STRING_MAX);
-
- conn_info.socket_fd = -1;
- _bt_convert_addr_string_to_type(conn_info.device_addr.addr,
- rfcomm_info->address);
-
- out_param1 = g_array_new(FALSE, FALSE, sizeof(gchar));
- out_param2 = g_array_new(FALSE, FALSE, sizeof(gchar));
-
- g_array_append_vals(out_param1, &conn_info,
- sizeof(bluetooth_rfcomm_connection_t));
- g_array_append_vals(out_param2, &result, sizeof(int));
-
- dbus_g_method_return(req_info->context, out_param1, out_param2);
-
- g_array_free(out_param1, TRUE);
- g_array_free(out_param2, TRUE);
- _bt_delete_request_list(req_info->req_id);
- }
-
- g_free(rfcomm_info->address);
- g_free(rfcomm_info->uuid);
- g_free(rfcomm_info);
- rfcomm_info = NULL;
-}