Discussion:
ioctl(RFCOMMCREATEDEV) failed: File descriptor in bad state
Zhang, Zhenhua
2009-09-14 10:02:03 UTC
Permalink
Dear all,

I am calling ioctl(RFCOMMCREATEDEV) to create tty device from a rfcomm fd
using hands-free profile. If the HF send connection request to AG and make
rfcomm connect, the tty could be made successfully. However, if the
AG(mobile) side make request to create rfcomm connection, the rfcomm could
be created but fail to create the tty device. The kernel returns error
because socket is not connected, see below code. Any idea? Thanks much!

if (req.flags & (1 << RFCOMM_REUSE_DLC)) {
/* Socket must be connected */
if (sk->sk_state != BT_CONNECTED)
return -EBADFD;


The pseudo code logic looks like this:

void rfcomm_connect_cb(GIOChannel *rfcomm, GError *err,
gpointer user_data)
{
...
ba2str(&dev->src, src);
ba2str(&dev->dst, dst);

/* make /dev/rfcomm serial port from rfcomm */
memset(&req, 0, sizeof(req));
req.dev_id = -1;
req.flags = (1 << RFCOMM_REUSE_DLC); <---set RFCOMM_REUSE_DLC flag
bacpy(&req.src, &dev->src);
bacpy(&req.dst, &dev->dst);
req.channel = gw->channel;
sk = g_io_channel_unix_get_fd(rfcomm);
id = ioctl(sk, RFCOMMCREATEDEV, &req);
if (id < 0) {
int err = errno; <---return errno 77
error("ioctl(RFCOMMCREATEDEV) failed: %s (%d)", strerror(err), err);
g_io_channel_shutdown(rfcomm, TRUE, NULL);
g_io_channel_unref(rfcomm);
gw->rfcomm = NULL;
goto fail;
}
gw->rfcomm = g_io_channel_ref(rfcomm);

gw->rfcomm_id = id;
gw->tty = g_strdup_printf("/dev/rfcomm%d", id);

failed:
return;
}

static void hf_io_cb(...)
{
bt_io_get(chan, BT_IO_RFCOMM, &err,
BT_IO_OPT_SOURCE_BDADDR, &src,
BT_IO_OPT_DEST_BDADDR, &dst,
BT_IO_OPT_CHANNEL, &ch,
BT_IO_OPT_INVALID);

//request authorization
if (!bt_io_accept(gw->rfcomm, rfcomm_connect_cb, dev, NULL,
&err)) {
error("bt_io_accept: %s", err->message);
g_error_free(err);
}

}

Regar
Marcel Holtmann
2009-09-14 11:01:48 UTC
Permalink
Hi,
Post by Zhang, Zhenhua
I am calling ioctl(RFCOMMCREATEDEV) to create tty device from a rfcomm fd
using hands-free profile. If the HF send connection request to AG and make
rfcomm connect, the tty could be made successfully. However, if the
AG(mobile) side make request to create rfcomm connection, the rfcomm could
be created but fail to create the tty device. The kernel returns error
because socket is not connected, see below code. Any idea? Thanks much!
why do you wanna create the TTY in the first place? Going via the TTY
layer is pretty bad idea. Don't even think about doing that. Use the
RFCOMM socket.

Regards

Marcel
Zhang, Zhenhua
2009-09-14 11:38:57 UTC
Permalink
Hi Marcel,
Post by Marcel Holtmann
Hi,
Post by Zhang, Zhenhua
I am calling ioctl(RFCOMMCREATEDEV) to create tty device from a
rfcomm fd using hands-free profile. If the HF send connection
request to AG and make rfcomm connect, the tty could be made
successfully. However, if the AG(mobile) side make request to create
rfcomm connection, the rfcomm could be created but fail to create
the tty device. The kernel returns error because socket is not
connected, see below code. Any idea? Thanks much!
why do you wanna create the TTY in the first place? Going via the TTY
layer is pretty bad idea. Don't even think about doing that. Use the
RFCOMM socket.
Regards
Marcel
I want to create TTY and expose it to Ofono as /dev/rfcomm0. Ofono side
could treat bluetooth connection as a TTY device, and create
GAtChat *chat by using g_at_chat_new_from_tty(tty, syntax).

Regards,
Zhenhua
Marcel Holtmann
2009-09-14 13:28:20 UTC
Permalink
Hi,
Post by Zhang, Zhenhua
Post by Marcel Holtmann
Post by Zhang, Zhenhua
I am calling ioctl(RFCOMMCREATEDEV) to create tty device from a
rfcomm fd using hands-free profile. If the HF send connection
request to AG and make rfcomm connect, the tty could be made
successfully. However, if the AG(mobile) side make request to create
rfcomm connection, the rfcomm could be created but fail to create
the tty device. The kernel returns error because socket is not
connected, see below code. Any idea? Thanks much!
why do you wanna create the TTY in the first place? Going via the TTY
layer is pretty bad idea. Don't even think about doing that. Use the
RFCOMM socket.
I want to create TTY and expose it to Ofono as /dev/rfcomm0. Ofono side
could treat bluetooth connection as a TTY device, and create
GAtChat *chat by using g_at_chat_new_from_tty(tty, syntax).
and that is just wrong to begin with. Write an oFono plugin that uses
Bluetooth RFCOMM sockets directly. I really don't understand why you
wanna make your life so complicated. Within oFono you have to do a
non-blocking RFCOMM connect() anyway using GIOChannel. And once you have
the GIOChannel you can just hand it to g_at_chat_new(). Plain and
simple. Just extend modemconf.c to support a "bluetooth" driver and then
you can even configure address and channel.

Regards

Marcel

Loading...