Hi Ulrich, On Thu, Jan 20, 2011 at 12:44 PM, Ulrich BÃrgi <buergi@xxxxxxxxxxxx> wrote: > Hello everyone, > > I'm encountering a strange behavior regarding inquiry results. I'm working > on a C program, which has a method called 'scanner_start'. This method > performs an inquiry and reports the corresponding addresses and RSSI values > of all results. > When I just call this method and do nothing else, it works as intended. > However, if I do anything prior to that, I only get 'normal' inquiry results > without RSSI. > > As example, this works: > /* do nothing here */ > scanner_start(); > /* do something here */ > > whereas this fails: > /* do nothing here */ > int i; > scanner_start(); > /* do something here */ > > Below is the complete source code needed to reproduce this behavior. > > I cannot really imagine, how an unused declaration can have an impact on the > inquiry results. Does anybody have a clue, how this could be? > > Thank you, > Ulrich > > -- > > #include <stdio.h> > #include <stdlib.h> > #include <unistd.h> > #include <sys/socket.h> > #include <sys/poll.h> > #include <sys/ioctl.h> > #include <bluetooth/bluetooth.h> > #include <bluetooth/hci.h> > #include <bluetooth/hci_lib.h> > > static void print_result(bdaddr_t *bdaddr, char has_rssi, int rssi) > { >    Âchar addr[18]; > >    Âba2str(bdaddr, addr); > >    Âprintf("%17s", addr); >    Âif(has_rssi) >        Âprintf(" RSSI:%d", rssi); >    Âelse >        Âprintf(" RSSI:n/a"); >    Âprintf("\n"); >    Âfflush(NULL); > } > > > static void scanner_start() > { >    Âint dev_id, sock = 0; >    Âstruct hci_filter flt; >    Âinquiry_cp cp; >    Âunsigned char buf[HCI_MAX_EVENT_SIZE], *ptr; >    Âhci_event_hdr *hdr; >    Âchar canceled = 0; >    Âinquiry_info_with_rssi *info_rssi; >    Âinquiry_info *info; >    Âint results, i, len; >    Âstruct pollfd p; > >    Âdev_id = hci_get_route(NULL); >    Âsock = hci_open_dev( dev_id ); >    Âif (dev_id < 0 || sock < 0) { >        Âperror("Can't open socket"); >        Âreturn; >    Â} > >    Â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_WITH_RSSI, &flt); >    Âhci_filter_set_event(EVT_INQUIRY_COMPLETE, &flt); >    Âif (setsockopt(sock, SOL_HCI, HCI_FILTER, &flt, sizeof(flt)) < 0) { >        Âperror("Can't set HCI filter"); >        Âreturn; >    Â} > >    Âif (hci_send_cmd(sock, OGF_HOST_CTL, OCF_WRITE_INQUIRY_MODE, > WRITE_INQUIRY_MODE_RP_SIZE, &cp) < 0) { Taking a look at your code, a few things look out of place: you are using cp uninitialized, it is expecting a write_inquiry_mode_cp, not a inquiry_cp and the plen parameter seems wrong. Declaring (or not) the variable i is messing with the stack state and affecting the value that goes into cp. >        Âperror("Can't set inquiry mode"); >        Âreturn; >    Â} > >    Âmemset (&cp, 0, sizeof(cp)); >    Âcp.lap[2] = 0x9e; >    Âcp.lap[1] = 0x8b; >    Âcp.lap[0] = 0x33; >    Âcp.num_rsp = 0; >    Âcp.length = 0x30; > >    Âprintf("Starting inquiry with RSSI...\n"); > >    Âif (hci_send_cmd (sock, OGF_LINK_CTL, OCF_INQUIRY, INQUIRY_CP_SIZE, > &cp) < 0) { >        Âperror("Can't start inquiry"); >        Âreturn; >    Â} > >    Âp.fd = sock; >    Âp.events = POLLIN | POLLERR | POLLHUP; > >    Âwhile(!canceled) { >        Âp.revents = 0; > >        Â/* poll the BT device for an event */ >        Âif (poll(&p, 1, -1) > 0) { >            Âlen = read(sock, buf, sizeof(buf)); > >            Âif (len < 0) >                Âcontinue; >            Âelse if (len == 0) >                Âbreak; > >            Âhdr = (void *) (buf + 1); >            Âptr = buf + (1 + HCI_EVENT_HDR_SIZE); > >            Âresults = ptr[0]; > >            Âswitch (hdr->evt) { >                Âcase EVT_INQUIRY_RESULT: >                    Âfor (i = 0; i < results; i++) { >                        Âinfo = (void *)ptr + > (sizeof(*info) * i) + 1; >                        Âprint_result(&info->bdaddr, > 0, 0); >                    Â} >                    Âbreak; > >                Âcase EVT_INQUIRY_RESULT_WITH_RSSI: >                    Âfor (i = 0; i < results; i++) { >                        Âinfo_rssi = (void *)ptr + > (sizeof(*info_rssi) * i) + 1; > > Âprint_result(&info_rssi->bdaddr, 1, info_rssi->rssi); >                    Â} >                    Âbreak; > >                Âcase EVT_INQUIRY_COMPLETE: >                    Âcanceled = 1; >                    Âbreak; >            Â} >        Â} >    Â} >    Âclose(sock); > } > > int main(int argc, char **argv) > { >    Â//int i; /* causes inq. result to have no rssi value */ >    Âscanner_start(); >    Âreturn 0; > } > -- > To unsubscribe from this list: send the line "unsubscribe linux-bluetooth" > in > the body of a message to majordomo@xxxxxxxxxxxxxxx > More majordomo info at Âhttp://vger.kernel.org/majordomo-info.html > Cheers, -- Vinicius ÿô.nÇ·®+%˱é¥wÿº{.nÇ·¥{±ý¶â^nr¡öë¨è&£ûz¹Þúzf£¢·h§~Ûÿÿïÿê_èæ+v¨þ)ßø