Skip to contentSkip to conversation

Detecting RSSI without connecting

Bluez-users Scott W Gifford 4 participants 13 messages Nov 9, 2004 – Dec 2, 2004 Bluetooth drivers

lore
Share & tools
lore
#0 lore raw

Hello again,

For a project I'm working on, I'd like to determine which of several (possibly many) Bluetooth devices is closest. "Best RSSI" is a good enough approximation of "closest" for my purposes, and since there may be many devices and I don't have any other interest in them, I'd like to get the RSSI without establishing a connection with each device.

It appears that "hcitool rssi <bdaddr>" requires that a connection is established. Do any cards provide a way of getting RSSI that doesn't require establishing a connection?

Thanks!

----ScottG.

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
_______________________________________________
Bluez-users mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users/listinfo/bluez-users

maintainer
#1 lore raw

Hi Scott,

Quoted 9 lines

For a project I'm working on, I'd like to determine which of several (possibly many) Bluetooth devices is closest. "Best RSSI" is a good enough approximation of "closest" for my purposes, and since there may be many devices and I don't have any other interest in them, I'd like to get the RSSI without establishing a connection with each device.

It appears that "hcitool rssi <bdaddr>" requires that a connection is established. Do any cards provide a way of getting RSSI that doesn't require establishing a connection?

starting with the Bluetooth 1.2 specification it is possible to get the RSSI value from the inquiry results. Check the mailing list archives for more details.

Regards

Marcel

-------------------------------------------------------
This SF.Net email is sponsored by:
Sybase ASE Linux Express Edition - download now for FREE LinuxWorld Reader's Choice Award Winner for best database on Linux.
http://ads.osdn.com/?ad_id=5588&alloc_id=12065&op=click
_______________________________________________
Bluez-users mailing list [email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users/listinfo/bluez-users

#2 lore raw
Quoted 8 lines · Marcel Holtmann

Hi Scott,

Quoted 6 lines

For a project I'm working on, I'd like to determine which of several (possibly many) Bluetooth devices is closest. "Best RSSI" is a good enough approximation of "closest" for my purposes, and since there may be many devices and I don't have any other interest in them, I'd like to get the RSSI without establishing a connection with each device.

Quoted 7 lines
Quoted 3 lines

It appears that "hcitool rssi <bdaddr>" requires that a connection is established. Do any cards provide a way of getting RSSI that doesn't require establishing a connection?

starting with the Bluetooth 1.2 specification it is possible to get the RSSI value from the inquiry results. Check the mailing list archives for more details.

Thanks Marcel,

I bought a Bluetooth 1.2 adapter (an IOGear GBU211), but it still doesn't seem to work. From what I found in the archives and the source code, it looks like I should set the inquiry mode to 1, then just use "hcitool scan". But it looks like the inquiry mode doesn't get set properly:

[root@gifford tools]# /usr/local/sbin/hcidump -X &

    [1] 10382
    HCIDump - HCI packet analyzer ver 1.12
    device: hci0 snap_len: 1028 filter: 0xffffffff
  [root@gifford tools]# /usr/local/sbin/hciconfig hci0 inqmode
    < HCI Command: Read Inquiry Mode (0x03|0x0044) plen 0
    > HCI Event: Command Complete (0x0e) plen 4
      0000: 01 44 0c 11                                       .D..
    Read inquiry mode on hci0 returned status 17
  [root@gifford tools]# /usr/local/sbin/hciconfig hci0 inqmode 1
    < HCI Command: Write Inquiry Mode (0x03|0x0045) plen 1
      0000: 01                                                .
    > HCI Event: Command Complete (0x0e) plen 4
      0000: 01 45 0c 11                                       .E..
  [root@gifford tools]# /usr/local/sbin/hciconfig hci0 inqmode
    < HCI Command: Read Inquiry Mode (0x03|0x0044) plen 0
    > HCI Event: Command Complete (0x0e) plen 4
      0000: 01 44 0c 11                                       .D..
    Read inquiry mode on hci0 returned status 17
  [root@gifford tools]# hcitool inq
    Inquiring ...
    < HCI Command: Inquiry (0x01|0x0001) plen 5
      0000: 33 8b 9e 08 64                                    3...d
    > HCI Event: Command Status (0x0f) plen 4
      0000: 00 01 01 04                                       ....
    > HCI Event: Inquiry Result (0x02) plen 15
      0000: 01 a5 2b 19 17 00 08 01  00 00 10 01 32 6a 78     ..+.........2jx
    > HCI Event: Inquiry Result (0x02) plen 15
      0000: 01 a5 2b 19 17 00 08 01  00 00 10 01 32 6a 78     ..+.........2jx
    > HCI Event: Inquiry Complete (0x01) plen 1
      0000: 00                                                .
        08:00:17:19:2B:A5       clock offset: 0x786a    class: 0x320110

It looks like read and write are both returning 17 (hex 0x11), which seems to mean unsupported. hciconfig -a says:

hci0: Type: USB
BD Address: 00:02:72:C0:08:CB ACL MTU: 377:10 SCO MTU: 16:0 UP RUNNING PSCAN ISCAN
RX bytes:3340 acl:0 sco:0 events:135 errors:0
TX bytes:728 acl:0 sco:0 commands:87 errors:0
Features: 0xff 0xff 0x0d 0x38 0x00 0x00 0x00 0x00 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 Link policy: RSWITCH HOLD SNIFF PARK
Link mode: SLAVE ACCEPT
Name: 'BlueZ (0)'
Class: 0x000100
Service Classes: Unspecified
Device Class: Computer, Uncategorized
HCI Ver: 1.2 (0x2) HCI Rev: 0x69 LMP Ver: 1.2 (0x2) LMP
Subver: 0x694a
Manufacturer: Broadcom Corporation (15)

Am I doing this wrong, or is it possible that a Bluetooth 1.2 device doesn't support RSSI on inquiry? If this device doesn't support it, can anybody recommend a device available in the US that does support RSSI on inquiry? Does the feature need to be supported on both the inquiring device and the inquired device to get RSSI information, or is support on the inquiring device enough?

Thanks!

----ScottG.

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#3 lore raw
Quoted 1 line · Scott W Gifford
        Features: 0xff 0xff 0x0d 0x38 0x00 0x00 0x00 0x00

[...]

Quoted 2 lines

Am I doing this wrong, or is it possible that a Bluetooth 1.2 device doesn't support RSSI on inquiry?

As far as I'm aware, it's not mandatory. There's a features bit that tells you whether it's supported. If I've counted right, it's bit 6 of byte 3 (numbering from 0 in both cases). That is, it's:

00 00 00 40 00 00 00 00

Quoted 1 line

From the features mask you've given, it looks like this device doesn't

support it.

Quoted 3 lines

If this device doesn't support it,
can anybody recommend a device available in the US that does support RSSI on inquiry?

Modesty forbids.

Marcel's page at http://www.holtmann.org/linux/bluetooth/features.html
really needs updating to quote more features bits now that 1.2 devices are starting to appear.

Quoted 3 lines

Does the feature need to be supported on both the
inquiring device and the inquired device to get RSSI information, or is support on the inquiring device enough?

It needs to be supported just on the inquiring device.

It's worth noting that although the inquiring device can accurately (one hopes) report the received signal strength, there's no requirement for the inquired device to trasnsmit at any particular power. This might be an issue if the inquired devices are a mix of different power classes and from different manufacturers. You may also have to watch the effects of fading (but if you get more than one report for a different device then you can just take the highest RSSI to eliminate fading effects).

However, devices that are closer will tend to have a higher RSSI than devices that are further away, so it's often a good place to start.

- Steven

**********************************************************************
This email and any files transmitted with it are confidential and intended solely for the use of the individual or entity to whom they are addressed. If you have received this email in error please notify the system manager.

This footnote also confirms that this email message has been swept by MIMEsweeper for the presence of computer viruses.

www.mimesweeper.com
**********************************************************************

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

maintainer
#4 lore raw

Hi Steven,

Quoted 5 lines
Quoted 3 lines

If this device doesn't support it,
can anybody recommend a device available in the US that does support RSSI on inquiry?

Modesty forbids.

all CSR chips with HCI 18.x firmware support inquiry with RSSI as defined in the specification and without any bugs. So I said it ;)

Quoted 3 lines

Marcel's page at http://www.holtmann.org/linux/bluetooth/features.html
really needs updating to quote more features bits now that 1.2 devices are starting to appear.

I know, I know. Time is always against me :(

Regards

Marcel

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

maintainer
#5 lore raw
In reply to Scott W Gifford

Hi Scott,

Quoted 24 lines
Quoted 6 lines
Quoted 6 lines

For a project I'm working on, I'd like to determine which of several (possibly many) Bluetooth devices is closest. "Best RSSI" is a good enough approximation of "closest" for my purposes, and since there may be many devices and I don't have any other interest in them, I'd like to get the RSSI without establishing a connection with each device.

Quoted 7 lines
Quoted 3 lines

It appears that "hcitool rssi <bdaddr>" requires that a connection is established. Do any cards provide a way of getting RSSI that doesn't require establishing a connection?

starting with the Bluetooth 1.2 specification it is possible to get the RSSI value from the inquiry results. Check the mailing list archives for more details.

I bought a Bluetooth 1.2 adapter (an IOGear GBU211), but it still doesn't seem to work. From what I found in the archives and the source code, it looks like I should set the inquiry mode to 1, then just use "hcitool scan". But it looks like the inquiry mode doesn't get set properly:

[root@gifford tools]# /usr/local/sbin/hcidump -X &

    [1] 10382
    HCIDump - HCI packet analyzer ver 1.12
    device: hci0 snap_len: 1028 filter: 0xffffffff
Quoted 10 lines

[root@gifford tools]# /usr/local/sbin/hciconfig hci0 inqmode
< HCI Command: Read Inquiry Mode (0x03|0x0044) plen 0 > HCI Event: Command Complete (0x0e) plen 4
0000: 01 44 0c 11 .D..
Read inquiry mode on hci0 returned status 17
[root@gifford tools]# /usr/local/sbin/hciconfig hci0 inqmode 1
< HCI Command: Write Inquiry Mode (0x03|0x0045) plen 1
0000: 01 .
> HCI Event: Command Complete (0x0e) plen 4
0000: 01 45 0c 11 .E..

this command returns with error code 0x11 which means unsupported feature or parameter value. Actually I think this should return unknown HCI command with error code 0x01.

Quoted 19 lines

It looks like read and write are both returning 17 (hex 0x11), which seems to mean unsupported. hciconfig -a says:

hci0: Type: USB
BD Address: 00:02:72:C0:08:CB ACL MTU: 377:10 SCO MTU: 16:0 UP RUNNING PSCAN ISCAN
RX bytes:3340 acl:0 sco:0 events:135 errors:0
TX bytes:728 acl:0 sco:0 commands:87 errors:0
Features: 0xff 0xff 0x0d 0x38 0x00 0x00 0x00 0x00 Packet type: DM1 DM3 DM5 DH1 DH3 DH5 HV1 HV2 HV3 Link policy: RSWITCH HOLD SNIFF PARK
Link mode: SLAVE ACCEPT
Name: 'BlueZ (0)'
Class: 0x000100
Service Classes: Unspecified
Device Class: Computer, Uncategorized
HCI Ver: 1.2 (0x2) HCI Rev: 0x69 LMP Ver: 1.2 (0x2) LMP
Subver: 0x694a
Manufacturer: Broadcom Corporation (15)

Check with "hciconfig hci0 features" if the inquiry with RSSI feature bit is set.

Quoted 6 lines

Am I doing this wrong, or is it possible that a Bluetooth 1.2 device doesn't support RSSI on inquiry? If this device doesn't support it, can anybody recommend a device available in the US that does support RSSI on inquiry? Does the feature need to be supported on both the inquiring device and the inquired device to get RSSI information, or is support on the inquiring device enough?

It can be that Broadcom didn't implemented this, which is actually somekind of stupid. Maybe they only added AFH.

If you buy a CSR based Bluetooth 1.2 device it will support inquiry with RSSI. Also the 1.2 Silicon Wave based and the AVM BlueFRITZ! USB v2.0 have this support. However both have a bug, but you can work around it. Maybe the easiest way is a D-Link DBT-120 Rev. B3 and then put the Apple firmware update on it.

Regards

Marcel

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#6 lore raw

[Bluez-users] Firmware update & HID profile (Was: Detecting RSSI without connecting)

Quoted 3 lines · Marcel Holtmann

[...]
Maybe the easiest way is a D-Link DBT-120 Rev. B3 and then put the Apple firmware update on it.

BTW, any improvements in the new Apple firmware update?

I used the previous one on two BT-120 (H/W: B2 and H/W: B3) and both of them now start with HID profile.

I know that to have them back starting with hci I should change a PS key setting, but the only linux program I found is for serial modules only.

Is the new update behaving the same way?

(I know that I can use hid2hci each time I use them, but, unfortunately, I don't use Bluetooth _only_ with Linux and with my up-to-date Linux box).

Regards,

Signature · 13 lines
Diego Liziero <[email protected]>



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now. 
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users
maintainer
#7 lore raw

Re: [Bluez-users] Firmware update & HID profile (Was: Detecting RSSI without connecting)

Hi Diego,

Quoted 5 lines
Quoted 2 lines

Maybe the easiest way is a D-Link DBT-120 Rev. B3 and then put the Apple firmware update on it.

BTW, any improvements in the new Apple firmware update?

Quoted 7 lines

I used the previous one on two BT-120 (H/W: B2 and H/W: B3) and both of them now start with HID profile.

I know that to have them back starting with hci I should change a PS key setting, but the only linux program I found is for serial modules only.

Is the new update behaving the same way?

I think so, but changing the PS key is quite simply. However no public tool for this exists and I am not going to write one.

Regards

Marcel

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#8 lore raw
In reply to Marcel Holtmann

Marcel Holtmann <[email protected]> writes:

[...]

Quoted 5 lines

If you buy a CSR based Bluetooth 1.2 device it will support inquiry with RSSI. Also the 1.2 Silicon Wave based and the AVM BlueFRITZ! USB v2.0 have this support. However both have a bug, but you can work around it. Maybe the easiest way is a D-Link DBT-120 Rev. B3 and then put the Apple firmware update on it.

Thanks for the advice, I got a DBT-120 and put the Apple firmware on it (yet another reason I'm glad we bought a Mac for my wife), and I can now get the RSSI from an inquiry!

That is to say, I can see it in hcidump. I don't see the results in "hcitool inq", and from a quick look at the source code it looks like that information isn't returned from the kernel. Are there plans to provide this information via the kernel and libraries, or should I plan on hacking something up with hcidump?

Thanks again!

----ScottG.

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

maintainer
#9 lore raw

Hi Scott,

Quoted 10 lines
Quoted 5 lines

If you buy a CSR based Bluetooth 1.2 device it will support inquiry with RSSI. Also the 1.2 Silicon Wave based and the AVM BlueFRITZ! USB v2.0 have this support. However both have a bug, but you can work around it. Maybe the easiest way is a D-Link DBT-120 Rev. B3 and then put the Apple firmware update on it.

Thanks for the advice, I got a DBT-120 and put the Apple firmware on it (yet another reason I'm glad we bought a Mac for my wife), and I can now get the RSSI from an inquiry!

Quoted 5 lines

That is to say, I can see it in hcidump. I don't see the results in "hcitool inq", and from a quick look at the source code it looks like that information isn't returned from the kernel. Are there plans to provide this information via the kernel and libraries, or should I plan on hacking something up with hcidump?

I maybe gonna change the complete interface to the inquiry stuff in the future and so I haven't added support for inquiry with RSSI so far. You don't need to hack something on hcidump, simply use the HCI raw socket interface. Send the inquiry command and wait for the events. You will find example code for this and listening on the 1.1 inquiry event in the mailing list archive. It should be easy to adapt.

Regards

Marcel

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#10 lore raw

Marcel Holtmann <[email protected]> writes:

[...]

Quoted 2 lines

I maybe gonna change the complete interface to the inquiry stuff in the future and so I haven't added support for inquiry with RSSI so far.

OK. I can find time to help test this and perhaps help with the coding when you're ready to add this support.

Quoted 5 lines

You don't need to hack something on hcidump, simply use the HCI raw socket interface. Send the inquiry command and wait for the events. You will find example code for this and listening on the 1.1 inquiry event in the mailing list archive. It should be easy to adapt.

Ah, this program:

    http://article.gmane.org/gmane.linux.bluez.user/1191

looks like a great place to start. I'll hack at it for a bit over the next few days and post an update.

Thanks!

----ScottG.

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

maintainer
#11 lore raw

Hi Scott,

Quoted 5 lines
Quoted 2 lines

I maybe gonna change the complete interface to the inquiry stuff in the future and so I haven't added support for inquiry with RSSI so far.

OK. I can find time to help test this and perhaps help with the coding when you're ready to add this support.

right now I like to wait until I get access to the Extended Inquiry Response (EIR) proposal. The Bluetooth 2.0 specification deprecated another field of the inquiry result events and the next interface must be a little bit more generic.

Quoted 6 lines
Quoted 5 lines

You don't need to hack something on hcidump, simply use the HCI raw socket interface. Send the inquiry command and wait for the events. You will find example code for this and listening on the 1.1 inquiry event in the mailing list archive. It should be easy to adapt.

Quoted 6 lines

Ah, this program:

    http://article.gmane.org/gmane.linux.bluez.user/1191

looks like a great place to start. I'll hack at it for a bit over the next few days and post an update.

Exactly one of these (there are for examples).

Regards

Marcel

-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users. Discover which products truly live up to the hype. Start reading now.
http://productguide.itmanagersjournal.com/
_______________________________________________
Bluez-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/bluez-users

#12 lore raw
In reply to Scott W Gifford

Scott W Gifford <[email protected]> writes:

[...]

Quoted 6 lines

Ah, this program:

    http://article.gmane.org/gmane.linux.bluez.user/1191

looks like a great place to start. I'll hack at it for a bit over the next few days and post an update.

I got something working to my satisfaction. I've attached it as a plain text file, for comment and as another example.

----ScottG.

/*
*
* Repeatedly scan with RSSI
*

 *  Copyright (C) 2004  Scott Gifford <[email protected]>
 *  Copyright (C) 2003  Marcel Holtmann <[email protected]>
 *
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*
*/

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <time.h>
#include <getopt.h>
#include <limits.h>

#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>

int debug = 0;

#define POLL_TIMEOUT 11000
#define MAX_DELAY 2000

void usage(char *name)
{
fprintf(stderr,"Usage: %s [-i hci-if]\n",name);
exit(2);
}

static volatile sig_atomic_t __io_canceled = 0;

static void sig_hup(int sig)
{
return;
}

static void sig_term(int sig)
{
__io_canceled = 1;
}

static void show_inquiry_result(bdaddr_t *bdaddr, int rssi) {
char addr[18];

  ba2str(bdaddr, addr);
  printf("%18s: ", addr);

  if (rssi == INT_MIN)
    printf("%4c\n",'?');
  else
    printf("%4d\n",rssi);

fflush(stdout);
}

static void inquiry_result(int dd, unsigned char *buf, int len) {
inquiry_info *info;
uint8_t num;
int i;

  num = buf[0];
  if (debug) fprintf(stderr,"inquiry_result:\tnum %d\n", num);
  for (i = 0; i < num; i++) {
    info = (void *) buf + (sizeof(*info) * i) + 1;
    show_inquiry_result(&info->bdaddr, INT_MIN);

}
}

static void inquiry_result_with_rssi(int dd, unsigned char *buf, int len) {
inquiry_info_with_rssi *info;
uint8_t num;
int i;

  num = buf[0];
  if (debug) fprintf(stderr,"inquiry_result_with_rssi:\tnum %d\n", num);
  for (i = 0; i < num; i++) {
    info = (void *) buf + (sizeof(*info) * i) + 1;
    show_inquiry_result(&info->bdaddr, info->rssi);

}
}

/* Try to active RSSI on inquiry, but if it doesn't work oh well. */ static void activate_rssi(int dd)
{
write_inquiry_mode_cp cp;
int err;

cp.mode = 1;
err = hci_send_cmd(dd, OGF_HOST_CTL, OCF_WRITE_INQUIRY_MODE, WRITE_INQUIRY_MODE_RP_SIZE, &cp); if (debug) fprintf(stderr,"activate_rssi: err=%d\n",err);
/* No other error checking, since this may fail and we don't care. */
}

static void begin_inquiry(int dd)
{
inquiry_cp cp;
int err;

  if (debug) fprintf(stderr,"begin_inquiry: starting\n");
  memset (&cp, 0, sizeof(cp));
  cp.lap[2] = 0x9e;
  cp.lap[1] = 0x8b;
  cp.lap[0] = 0x33;
  cp.num_rsp = 0;
  cp.length = 0x30;

err = hci_send_cmd (dd, OGF_LINK_CTL, OCF_INQUIRY,

		      INQUIRY_CP_SIZE, &cp);
  if (err < 0)
  {
    fprintf(stderr,"Error #%d beginning inquiry\n",err);
    exit(1);

}
}

static void cancel_inquiry(int dd)
{
int err;

err = hci_send_cmd (dd, OGF_LINK_CTL, OCF_INQUIRY_CANCEL,

		      0, NULL);

if (debug) fprintf(stderr,"cancel_inquiry: err=%d\n",err); /* No other error checking, because what would we do? */
}

int main(int argc, char *argv[])
{
unsigned char buf[HCI_MAX_EVENT_SIZE], *ptr;
hci_event_hdr *hdr;
struct hci_filter flt;
struct sigaction sa;
struct pollfd p;
int dd = -1, dev = 0, len;
int errflg = 0;
int c;
extern char *optarg;
extern int optind;

  /* Process command-line options */
  while ((c = getopt(argc, argv, "i:")) != EOF)
  {
    switch(c)
    {
      case 'i':
        if ((dev = hci_devid(optarg)) < 0)
        {
          fprintf(stderr,"Invalid device '%s': %s\n",optarg,strerror(errno));
          exit(1);
        }
        break;
      case '?':
        errflg++;
        break;
    }
  }

  if (errflg)
    usage(argv[0]);
	
  /* Open the Bluetooth device */
  dd = hci_open_dev(dev);
  if (dd < 0) {
    perror("Can't open HCI device");
    exit(1);
  }

  /* Set up an event filter; we only care about inquiry-related
   *  events.
   */
  hci_filter_clear(&flt);
  hci_filter_set_ptype(HCI_EVENT_PKT, &flt);
  hci_filter_set_event(EVT_INQUIRY_RESULT, &flt);
  hci_filter_set_event(EVT_INQUIRY_RESULT, &flt);
  hci_filter_set_event(EVT_INQUIRY_RESULT_WITH_RSSI, &flt);
  hci_filter_set_event(EVT_INQUIRY_COMPLETE, &flt);
  if (setsockopt(dd, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) {
    perror("Can't set HCI filter");
    exit(1);
  }

  /* Set up signal handlers */
  memset(&sa, 0, sizeof(sa));
  sa.sa_flags   = SA_NOCLDSTOP;
  sa.sa_handler = SIG_IGN;
  sigaction(SIGCHLD, &sa, NULL);
  sigaction(SIGPIPE, &sa, NULL);

  sa.sa_handler = sig_term;
  sigaction(SIGTERM, &sa, NULL);
  sigaction(SIGINT,  &sa, NULL);

  sa.sa_handler = sig_hup;
  sigaction(SIGHUP, &sa, NULL);

/* Set up the random number generator, for random delays for
* scan. This tries to avoid multiple machines running this
* software from starting at the same time and flooding the
* network at the same time.
*/

  srand(time(NULL) ^ getpid());

  /* Request RSSI if available, then start the first inquiry. */
  activate_rssi(dd);
  usleep(rand() % MAX_DELAY);
  begin_inquiry(dd);

  /* Now poll for events until a signal tells is to cancel. */
  p.fd = dd;
  p.events = POLLIN | POLLERR | POLLHUP;
  while (!__io_canceled) {
    p.revents = 0;
    if (debug) fprintf(stderr,"Polling...\n");
    if (poll(&p, 1, POLL_TIMEOUT) > 0) {
      len = read(dd, buf, sizeof(buf));
      if (len < 0)
        continue;
      else if (len == 0)
        break; /* EOF */
      hdr = (void *) (buf + 1);
      ptr = buf + (1 + HCI_EVENT_HDR_SIZE);
      len -= (1 + HCI_EVENT_HDR_SIZE);
      if (debug) fprintf(stderr,"Got event!  Type is %d\n",hdr->evt);

      switch (hdr->evt) {

        case EVT_INQUIRY_RESULT:
          inquiry_result(dd, ptr, len);
          break;

        case EVT_INQUIRY_RESULT_WITH_RSSI:
          inquiry_result_with_rssi(dd, ptr, len);
          break;

        case EVT_INQUIRY_COMPLETE:
          /* Inquiry is finished, wait a random time
           * then start another. 
           */

          if (debug) fprintf(stderr,"Inquiry complete\n");
          usleep(rand() % MAX_DELAY);
          begin_inquiry(dd);
          break;
      }
    }

}

  if (debug) fprintf(stderr,"Program finished.\n");

  cancel_inquiry(dd);
	
  if (hci_close_dev(dd) < 0) {
    perror("Can't close HCI device");
    exit(1);
  }

return 0;
}