Skip to contentSkip to conversation

select(2) or read(2) on bluetooth sockets not working correctly?

Bluez-devel Pawel Kot 2 participants 4 messages Nov 15, 2007 – Nov 26, 2007 Bluetooth drivers

lore
Share & tools
lore

Pawel Kot

author
#0 lore raw

Hi,

In gnokii project, we use Bluetooth sockets as one of the phone communication medium. We use it in quite usual way (simplified code):

fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); [...]
bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); [...]
connect(fd, (struct sockaddr *)&raddr, sizeof(raddr));

Then we have (simplified code again):

while (1) {
[...]
write(fd, write_buf, size);
[...]
FD_ZERO(&readfds);
FD_SET(fd, &readfds);

   if (select(fd + 1, &readfds, NULL, NULL, &timeout) > 0) {
      read(fd, read_buf, size);
      [...]

}
[...]
}

The problem is (or rather was) that then bluetooth device gets out of range (or bluetooth gets disabled in the device) the program hangs on read(2). So select(2) went OK and told us that there's something to be read, but actually nothing can be read. According to select(2) manual page, after the successful select(2), read(2) should not hang (it may fail though).

It can be workarounded by setting O_NONBLOCK flag on the socket, but still I consider it as a bug that read(2) hangs after successful select(2).

take care,
pkot
PS. Please Cc me on replies.

Signature · 11 lines
Pawel Kot

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Bluez-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel
maintainer
#1 lore raw

Hi Pawel,

Quoted 31 lines

In gnokii project, we use Bluetooth sockets as one of the phone communication medium. We use it in quite usual way (simplified code):

fd = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM); [...]
bind(fd, (struct sockaddr *)&laddr, sizeof(laddr)); [...]
connect(fd, (struct sockaddr *)&raddr, sizeof(raddr));

Then we have (simplified code again):

while (1) {
   [...]
   write(fd, write_buf, size);
   [...]
   FD_ZERO(&readfds);
   FD_SET(fd, &readfds);
   if (select(fd + 1, &readfds, NULL, NULL, &timeout) > 0) {
      read(fd, read_buf, size);
      [...]
   }
   [...]
}

The problem is (or rather was) that then bluetooth device gets out of range (or bluetooth gets disabled in the device) the program hangs on read(2). So select(2) went OK and told us that there's something to be read, but actually nothing can be read. According to select(2) manual page, after the successful select(2), read(2) should not hang (it may fail though).

Quoted 3 lines

It can be workarounded by setting O_NONBLOCK flag on the socket, but still I consider it as a bug that read(2) hangs after successful select(2).

which kernel version? Can you construct a simple demo program that shows this effect easily with a 2.6.24-rc2 kernel?

Regards

Marcel

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/
_______________________________________________
Bluez-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel

author
#2 lore raw
In reply to Pawel Kot

Hi,

I have put simple program demonstrating the problem at
http://tfuj.pl/gnokii/bt.c with compilation hints and usage. The testing I did was on Ubuntu kernels, so that's for sure not the recent one, but I think it is really easy to reproduce if the problem still persists.

You may catch me on #bluez if needed.

take care,
pkot
PS. I'm really not subscribed, so please Cc on reply.

Signature · 10 lines
Pawel Kot

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Bluez-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel
maintainer
#3 lore raw

Hi Pawel,

Quoted 5 lines

I have put simple program demonstrating the problem at
http://tfuj.pl/gnokii/bt.c with compilation hints and usage. The testing I did was on Ubuntu kernels, so that's for sure not the recent one, but I think it is really easy to reproduce if the problem still persists.

this from the select() manual page:

Those listed in readfds will be watched to see if characters become available for reading (more precisely, to see if a read will not block; in particular, a file descriptor is also ready on end-of-file)

If a device disconnects or gets out of range, it is considered to be an end of file situation. It might help to also watch the exceptfds for the hangup signal. The read() should return an error if the connection has been terminated. So that part is strange.

It means that if you connect, then poweroff and then call read() you would see the same effect. The read() command will block. Can you confirm this?

Regards

Marcel

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Bluez-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel