Skip to contentSkip to conversation

how to do asynchronous device inquiry?

Bluez-devel Albert Huang 3 participants 8 messages Jun 29, 2004 – Jun 30, 2004 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

hello,

Can somone point me to an example of how to do an asynchronous device inquiry using bluez? I'd like to start an inquiry with an ioctl or something like that, and then be notified either with signals or by polling as devices are discovered. If this is currently not possible, can you point me to which source files I could modify that would allow me to do this? Thanks!

-albert

-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Bluez-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel

#1 lore raw

G'day Albert,

You could certainly fork() a process to exec() hcitool, and when it finishes it could send the information to your program, through a pipe that your program is select()ing on, or something similar.

If you knew your application environment well, you could configure the inquiry to take far less time. e.g. "hcitool inq --length=1" is supposed to work for 1.28 seconds only, assuming no cache. If you can change the inquiry parameters of the other device, you can help this to go even faster.

I've tried running "hcitool inq --length=1" repeatedly until another device is found. It doesn't seem as reliable as doing a full inquiry.

You could use threads. Erk.

But how to implement it asynchronously all in one process using an event model ... doesn't seem to be a way to do it. Maybe I just can't find it. Here's how I looked ...

Looking at tools/hcitool.c, an inquiry uses hci_inquiry() function, part of libbluetooth1 on my system. I'm looking at 2.7.

In libbluetooth1, hci_inquiry() calls ioctl() to do the work. It passes an hci_inquiry_req struct. I'm not sure what possible flag bits are valid. IREQ_CACHE_FLUSH is the only one I can see defined.

The ioctl() is handled by kernel function hci_sock_ioctl() in net/bluetooth/hci_sock.c which dispatches it to hci_inquiry() in net/bluetooth/hci_core.c and there doesn't seem to be any clear support for doing the inquiry asynchronously. I'm looking at 2.6.6.

Warning though; in hci_request() in the hci_core.c file, all requests to the device are serialised. Multiple attempts to do hci_inquiry() will be queued.

Signature · 12 lines
James Cameron


-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training.
Attend Black Hat Briefings & Training, Las Vegas July 24-29 - 
digital self defense, top technical experts, no vendor pitches, 
unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Bluez-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel
maintainer
#2 lore raw

Hi James,

Quoted 3 lines

You could certainly fork() a process to exec() hcitool, and when it finishes it could send the information to your program, through a pipe that your program is select()ing on, or something similar.

very bad idea :(

Quoted 1 line

You could use threads. Erk.

Possible, but not needed.

Quoted 3 lines

But how to implement it asynchronously all in one process using an event model ... doesn't seem to be a way to do it. Maybe I just can't find it. Here's how I looked ...

You don't looked into the details. The inquiry ioctl() is one way. The other one is the HCI raw socket. Open and bind a device and then set an event filter on it. Send an inquiry command according to the spec. and listen for the incoming inquiry result events.

Quoted 3 lines

Warning though; in hci_request() in the hci_core.c file, all requests to the device are serialised. Multiple attempts to do hci_inquiry() will be queued.

It is impossible to run two inquiries at the same time. The Bluetooth chip won't allow and even the HCI core don't accepts it. There is the INQUIRY flag that will set if an inquiry is in progress.

Regards

Marcel

-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Bluez-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel

#3 lore raw
Quoted 6 lines · Marcel Holtmann
Quoted 3 lines · James

You could certainly fork() a process to exec() hcitool, and when it finishes it could send the information to your program, through a pipe that your program is select()ing on, or something similar.

very bad idea :(

Aww, it's just another way to do it, that doesn't require as much dependency or learning. ;-)

Quoted 4 lines

You don't looked into the details. The inquiry ioctl() is one way. The other one is the HCI raw socket. Open and bind a device and then set an event filter on it. Send an inquiry command according to the spec. and listen for the incoming inquiry result events.

I stand corrected.

Albert, there is a good example of this in src/btctl-discovery-source.c in the libbtctl-0.4.1 package.
http://usefulinc.com/software/gnome-bluetooth/

btctl_discovery_source_new()
The hci device is opened, then set O_NONBLOCK, and the event filters are set. There's no sign of binding.

btctl_discovery_source_send_inquiry()
is used to send the inquiry command, but returns immediately.

The surrounding code handles the data as it comes back, using the event model; typical for a GNOME or glib based application.

Fascinating.

Signature · 1 line
James Cameron
maintainer
#4 lore raw

Hi James,

Quoted 8 lines
Quoted 5 lines
Quoted 3 lines

You could certainly fork() a process to exec() hcitool, and when it finishes it could send the information to your program, through a pipe that your program is select()ing on, or something similar.

very bad idea :(

Aww, it's just another way to do it, that doesn't require as much dependency or learning. ;-)

It is not fork() that is bad. It the combination of exec() and hcitool you propose to use. If you fork or use a thread then use hci_inquiry().

Quoted 7 lines
Quoted 4 lines

You don't looked into the details. The inquiry ioctl() is one way. The other one is the HCI raw socket. Open and bind a device and then set an event filter on it. Send an inquiry command according to the spec. and listen for the incoming inquiry result events.

I stand corrected.

Quoted 7 lines

Albert, there is a good example of this in src/btctl-discovery-source.c in the libbtctl-0.4.1 package.
http://usefulinc.com/software/gnome-bluetooth/

btctl_discovery_source_new()
The hci device is opened, then set O_NONBLOCK, and the event filters are set. There's no sign of binding.

If you use hci_open_dev() the binding is done by that call.

Regards

Marcel

-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Bluez-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel

maintainer
#5 lore raw
In reply to Albert Huang

Hi Albert,

Quoted 6 lines

Can somone point me to an example of how to do an asynchronous device inquiry using bluez? I'd like to start an inquiry with an ioctl or something like that, and then be notified either with signals or by polling as devices are discovered. If this is currently not possible, can you point me to which source files I could modify that would allow me to do this? Thanks!

you must use the HCI raw socket interface for it. Some time ago I posted example code for integration into the GNOME Bluetooth subsystem. Check the mailing list archives.

Regards

Marcel

-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Bluez-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel

#6 lore raw

sorry, I'm having a tough time finding this piece of code you mention.
The interface to sourceforge mailing list archives is terrible. Do
you remember around when you posted it?

thanks.

-albert

Quoted 20 lines · Marcel Holtmann

Hi Albert,

Quoted 6 lines

Can somone point me to an example of how to do an asynchronous device inquiry using bluez? I'd like to start an inquiry with an ioctl or something like that, and then be notified either with signals or by polling as devices are discovered. If this is currently not possible, can you point me to which source files I could modify that would allow me to do this? Thanks!

you must use the HCI raw socket interface for it. Some time ago I posted example code for integration into the GNOME Bluetooth subsystem. Check the mailing list archives.

Regards

Marcel

maintainer
#7 lore raw

Hi Albert,

Quoted 3 lines

sorry, I'm having a tough time finding this piece of code you mention.
The interface to sourceforge mailing list archives is terrible. Do
you remember around when you posted it?

no I don't know when I posted it. Use the Gmane archive.

Regards

Marcel

-------------------------------------------------------
This SF.Net email sponsored by Black Hat Briefings & Training. Attend Black Hat Briefings & Training, Las Vegas July 24-29 - digital self defense, top technical experts, no vendor pitches, unmatched networking opportunities. Visit www.blackhat.com
_______________________________________________
Bluez-devel mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-devel