예제 #1
0
def device_inquiry_with_with_rssi(sock):
    # save current filter
    old_filter = sock.getsockopt(bluez.SOL_HCI, bluez.HCI_FILTER, 14)

    # perform a device inquiry on bluetooth device #0
    # The inquiry should last 8 * 1.28 = 10.24 seconds
    # before the inquiry is performed, bluez should flush its cache of
    # previously discovered devices
    flt = bluez.hci_filter_new()
    bluez.hci_filter_all_events(flt)
    bluez.hci_filter_set_ptype(flt, bluez.HCI_EVENT_PKT)
    sock.setsockopt(bluez.SOL_HCI, bluez.HCI_FILTER, flt)

    duration = 4
    max_responses = 255
    cmd_pkt = struct.pack("BBBBB", 0x33, 0x8b, 0x9e, duration, max_responses)
    bluez.hci_send_cmd(sock, bluez.OGF_LINK_CTL, bluez.OCF_INQUIRY, cmd_pkt)

    results = []

    done = False
    while not done:
        pkt = sock.recv(255)
        ptype, event, plen = struct.unpack("BBB", pkt[:3])
        if event == bluez.EVT_INQUIRY_RESULT_WITH_RSSI:
            pkt = pkt[3:]
            nrsp = struct.unpack("B", pkt[0])[0]
            for i in range(nrsp):
                addr = bluez.ba2str(pkt[1 + 6 * i:1 + 6 * i + 6])
                rssi = struct.unpack("b", pkt[1 + 13 * nrsp + i])[0]
                results.append((addr, rssi))
                print "[%s] RSSI: [%d]" % (addr, rssi)
        elif event == bluez.EVT_INQUIRY_COMPLETE:
            done = True
        elif event == bluez.EVT_CMD_STATUS:
            status, ncmd, opcode = struct.unpack("BBH", pkt[3:7])
            if status != 0:
                print "uh oh..."
                printpacket(pkt[3:7])
                done = True
        elif event == bluez.EVT_INQUIRY_RESULT:
            pkt = pkt[3:]
            nrsp = struct.unpack("B", pkt[0])[0]
            for i in range(nrsp):
                addr = bluez.ba2str(pkt[1 + 6 * i:1 + 6 * i + 6])
                results.append((addr, -1))
                print "[%s] (no RRSI)" % addr
        else:
            print "unrecognized packet type 0x%02x" % ptype
            print "event ", event

    # restore old filter
    sock.setsockopt(bluez.SOL_HCI, bluez.HCI_FILTER, old_filter)

    return results
예제 #2
0
def device_inquiry_with_with_rssi(sock):
    # save current filter
    old_filter = sock.getsockopt(bluez.SOL_HCI, bluez.HCI_FILTER, 14)

    # perform a device inquiry on bluetooth device #0
    # The inquiry should last 8 * 1.28 = 10.24 seconds
    # before the inquiry is performed, bluez should flush its cache of
    # previously discovered devices
    flt = bluez.hci_filter_new()
    bluez.hci_filter_all_events(flt)
    bluez.hci_filter_set_ptype(flt, bluez.HCI_EVENT_PKT)
    sock.setsockopt(bluez.SOL_HCI, bluez.HCI_FILTER, flt)

    duration = 4
    max_responses = 255
    cmd_pkt = struct.pack("BBBBB", 0x33, 0x8B, 0x9E, duration, max_responses)
    bluez.hci_send_cmd(sock, bluez.OGF_LINK_CTL, bluez.OCF_INQUIRY, cmd_pkt)

    results = []

    done = False
    while not done:
        pkt = sock.recv(255)
        ptype, event, plen = struct.unpack("BBB", pkt[:3])
        if event == bluez.EVT_INQUIRY_RESULT_WITH_RSSI:
            pkt = pkt[3:]
            nrsp = struct.unpack("B", pkt[0])[0]
            for i in range(nrsp):
                addr = bluez.ba2str(pkt[1 + 6 * i : 1 + 6 * i + 6])
                rssi = struct.unpack("b", pkt[1 + 13 * nrsp + i])[0]
                results.append((addr, rssi))
                print "[%s] RSSI: [%d]" % (addr, rssi)
        elif event == bluez.EVT_INQUIRY_COMPLETE:
            done = True
        elif event == bluez.EVT_CMD_STATUS:
            status, ncmd, opcode = struct.unpack("BBH", pkt[3:7])
            if status != 0:
                print "uh oh..."
                printpacket(pkt[3:7])
                done = True
        elif event == bluez.EVT_INQUIRY_RESULT:
            pkt = pkt[3:]
            nrsp = struct.unpack("B", pkt[0])[0]
            for i in range(nrsp):
                addr = bluez.ba2str(pkt[1 + 6 * i : 1 + 6 * i + 6])
                results.append((addr, -1))
                print "[%s] (no RRSI)" % addr
        else:
            print "unrecognized packet type 0x%02x" % ptype
            print "event ", event

    # restore old filter
    sock.setsockopt(bluez.SOL_HCI, bluez.HCI_FILTER, old_filter)

    return results
예제 #3
0
    def _process_hci_event(self):
        import socket

        if self.sock is None: return
        # voodoo magic!!!
        pkt = self.sock.recv(255)
        ptype, event, plen = struct.unpack("BBB", pkt[:3])
        pkt = pkt[3:]
        if event == _bt.EVT_INQUIRY_RESULT:
            nrsp = struct.unpack("B", pkt[0])[0]
            for i in range(nrsp):
                addr = _bt.ba2str(pkt[1 + 6 * i:1 + 6 * i + 6])
                psrm = pkt[1 + 6 * nrsp + i]
                pspm = pkt[1 + 7 * nrsp + i]
                devclass_raw = struct.unpack(
                    "BBB", pkt[1 + 9 * nrsp + 3 * i:1 + 9 * nrsp + 3 * i + 3])
                devclass = (devclass_raw[2] << 16) | \
                        (devclass_raw[1] << 8) | \
                        devclass_raw[0]
                clockoff = pkt[1 + 12 * nrsp + 2 * i:1 + 12 * nrsp + 2 * i + 2]

                self._device_discovered(addr, devclass, psrm, pspm, clockoff)
        elif event == _bt.EVT_INQUIRY_RESULT_WITH_RSSI:
            nrsp = struct.unpack("B", pkt[0])[0]
            for i in range(nrsp):
                addr = _bt.ba2str(pkt[1 + 6 * i:1 + 6 * i + 6])
                psrm = pkt[1 + 6 * nrsp + i]
                pspm = pkt[1 + 7 * nrsp + i]
                #                devclass_raw = pkt[1+8*nrsp+3*i:1+8*nrsp+3*i+3]
                #                devclass = struct.unpack ("I", "%s\0" % devclass_raw)[0]
                devclass_raw = struct.unpack(
                    "BBB", pkt[1 + 8 * nrsp + 3 * i:1 + 8 * nrsp + 3 * i + 3])
                devclass = (devclass_raw[2] << 16) | \
                        (devclass_raw[1] << 8) | \
                        devclass_raw[0]
                clockoff = pkt[1 + 11 * nrsp + 2 * i:1 + 11 * nrsp + 2 * i + 2]
                rssi = struct.unpack("b", pkt[1 + 13 * nrsp + i])[0]

                self._device_discovered(addr, devclass, psrm, pspm, clockoff)
        elif event == _bt.EVT_INQUIRY_COMPLETE:
            self.is_inquiring = False
            if len(self.names_to_find) == 0:
                #                print "inquiry complete (evt_inquiry_complete)"
                self.sock.close()
                self.inquiry_complete()
            else:
                self._send_next_name_req()

        elif event == _bt.EVT_CMD_STATUS:
            # XXX shold this be "<BBH"
            status, ncmd, opcode = struct.unpack("BBH", pkt[:4])
            if status != 0:
                self.is_inquiring = False
                self.sock.close()

                #                print "inquiry complete (bad status 0x%X 0x%X 0x%X)" % \
                #                        (status, ncmd, opcode)
                self.names_to_find = {}
                self.inquiry_complete()
        elif event == _bt.EVT_REMOTE_NAME_REQ_COMPLETE:
            status = struct.unpack("B", pkt[0])[0]
            addr = _bt.ba2str(pkt[1:7])
            if status == 0:
                try:
                    name = pkt[7:].split('\0')[0]
                except IndexError:
                    name = ''
                if addr in self.names_to_find:
                    device_class = self.names_to_find[addr][0]
                    self.device_discovered(addr, device_class, name)
                    del self.names_to_find[addr]
                    self.names_found[addr] = (device_class, name)
                else:
                    pass
            else:
                if addr in self.names_to_find: del self.names_to_find[addr]
                # XXX should we do something when a name lookup fails?
#                print "name req unsuccessful %s - %s" % (addr, status)

            if len(self.names_to_find) == 0:
                self.is_inquiring = False
                self.sock.close()
                self.inquiry_complete()
#                print "inquiry complete (name req complete)"
            else:
                self._send_next_name_req()
        else:
            pass
예제 #4
0
    def _process_hci_event(self):
        if self.sock is None: return
        # voodoo magic!!!
        pkt = self.sock.recv(258)
        ptype, event, plen = struct.unpack("BBB", pkt[:3])
        pkt = pkt[3:]
        if event == _bt.EVT_INQUIRY_RESULT:
            nrsp = get_byte(pkt[0])
            for i in range(nrsp):
                addr = _bt.ba2str(pkt[1 + 6 * i:1 + 6 * i + 6])
                psrm = pkt[1 + 6 * nrsp + i]
                pspm = pkt[1 + 7 * nrsp + i]
                devclass_raw = struct.unpack(
                    "BBB", pkt[1 + 9 * nrsp + 3 * i:1 + 9 * nrsp + 3 * i + 3])
                devclass = (devclass_raw[2] << 16) | \
                        (devclass_raw[1] << 8) | \
                        devclass_raw[0]
                clockoff = pkt[1 + 12 * nrsp + 2 * i:1 + 12 * nrsp + 2 * i + 2]

                self._device_discovered(addr, devclass, psrm, pspm, clockoff,
                                        None, None)
        elif event == _bt.EVT_INQUIRY_RESULT_WITH_RSSI:
            nrsp = get_byte(pkt[0])
            for i in range(nrsp):
                addr = _bt.ba2str(pkt[1 + 6 * i:1 + 6 * i + 6])
                psrm = pkt[1 + 6 * nrsp + i]
                pspm = pkt[1 + 7 * nrsp + i]
                #                devclass_raw = pkt[1+8*nrsp+3*i:1+8*nrsp+3*i+3]
                #                devclass = struct.unpack ("I", "%s\0" % devclass_raw)[0]
                devclass_raw = struct.unpack(
                    "BBB", pkt[1 + 8 * nrsp + 3 * i:1 + 8 * nrsp + 3 * i + 3])
                devclass = (devclass_raw[2] << 16) | \
                        (devclass_raw[1] << 8) | \
                        devclass_raw[0]
                clockoff = pkt[1 + 11 * nrsp + 2 * i:1 + 11 * nrsp + 2 * i + 2]
                rssi = byte_to_signed_int(get_byte(pkt[1 + 13 * nrsp + i]))

                self._device_discovered(addr, devclass, psrm, pspm, clockoff,
                                        rssi, None)
        elif _bt.HAVE_EVT_EXTENDED_INQUIRY_RESULT and event == _bt.EVT_EXTENDED_INQUIRY_RESULT:
            nrsp = get_byte(pkt[0])
            for i in range(nrsp):
                addr = _bt.ba2str(pkt[1 + 6 * i:1 + 6 * i + 6])
                psrm = pkt[1 + 6 * nrsp + i]
                pspm = pkt[1 + 7 * nrsp + i]
                devclass_raw = struct.unpack(
                    "BBB", pkt[1 + 8 * nrsp + 3 * i:1 + 8 * nrsp + 3 * i + 3])
                devclass = (devclass_raw[2] << 16) | \
                        (devclass_raw[1] << 8) | \
                        devclass_raw[0]
                clockoff = pkt[1 + 11 * nrsp + 2 * i:1 + 11 * nrsp + 2 * i + 2]
                rssi = byte_to_signed_int(get_byte(pkt[1 + 13 * nrsp + i]))

                data_len = _bt.EXTENDED_INQUIRY_INFO_SIZE - _bt.INQUIRY_INFO_WITH_RSSI_SIZE
                data = pkt[1 + 14 * nrsp + i:1 + 14 * nrsp + i + data_len]
                name = None
                pos = 0
                while (pos <= len(data)):
                    struct_len = get_byte(data[pos])
                    if struct_len == 0:
                        break
                    eir_type = get_byte(data[pos + 1])
                    if eir_type == 0x09:  # Complete local name
                        name = data[pos + 2:pos + struct_len + 1]
                    pos += struct_len + 2

                self._device_discovered(addr, devclass, psrm, pspm, clockoff,
                                        rssi, name)
        elif event == _bt.EVT_INQUIRY_COMPLETE or event == _bt.EVT_CMD_COMPLETE:
            self.is_inquiring = False
            if len(self.names_to_find) == 0:
                #                print "inquiry complete (evt_inquiry_complete)"
                self.sock.close()
                self._inquiry_complete()
            else:
                self._send_next_name_req()

        elif event == _bt.EVT_CMD_STATUS:
            # XXX shold this be "<BBH"
            status, ncmd, opcode = struct.unpack("BBH", pkt[:4])
            if status != 0:
                self.is_inquiring = False
                self.sock.close()

                #                print "inquiry complete (bad status 0x%X 0x%X 0x%X)" % \
                #                        (status, ncmd, opcode)
                self.names_to_find = {}
                self._inquiry_complete()
        elif event == _bt.EVT_REMOTE_NAME_REQ_COMPLETE:
            status = get_byte(pkt[0])
            addr = _bt.ba2str(pkt[1:7])
            if status == 0:
                try:
                    name = pkt[7:].split('\0')[0]
                except IndexError:
                    name = ''
                if addr in self.names_to_find:
                    device_class, rssi = self.names_to_find[addr][:2]
                    self.device_discovered(addr, device_class, rssi, name)
                    del self.names_to_find[addr]
                    self.names_found[addr] = (device_class, rssi, name)
                else:
                    pass
            else:
                if addr in self.names_to_find: del self.names_to_find[addr]
                # XXX should we do something when a name lookup fails?
#                print "name req unsuccessful %s - %s" % (addr, status)

            if len(self.names_to_find) == 0:
                self.is_inquiring = False
                self.sock.close()
                self.inquiry_complete()
#                print "inquiry complete (name req complete)"
            else:
                self._send_next_name_req()
        else:
            pass
예제 #5
0
파일: bluez.py 프로젝트: AldwinP/pybluez
    def _process_hci_event (self):

        if self.sock is None: return
        # voodoo magic!!!
        pkt = self.sock.recv (258)
        ptype, event, plen = struct.unpack ("BBB", pkt[:3])
        pkt = pkt[3:]
        if event == _bt.EVT_INQUIRY_RESULT:
            nrsp = struct.unpack ("B", pkt[0])[0]
            for i in range (nrsp):
                addr = _bt.ba2str (pkt[1+6*i:1+6*i+6])
                psrm = pkt[ 1+6*nrsp+i ]
                pspm = pkt[ 1+7*nrsp+i ]
                devclass_raw = struct.unpack ("BBB", 
                        pkt[1+9*nrsp+3*i:1+9*nrsp+3*i+3])
                devclass = (devclass_raw[2] << 16) | \
                        (devclass_raw[1] << 8) | \
                        devclass_raw[0]
                clockoff = pkt[1+12*nrsp+2*i:1+12*nrsp+2*i+2]

                self._device_discovered (addr, devclass, 
                        psrm, pspm, clockoff, None, None)
        elif event == _bt.EVT_INQUIRY_RESULT_WITH_RSSI:
            nrsp = struct.unpack ("B", pkt[0])[0]
            for i in range (nrsp):
                addr = _bt.ba2str (pkt[1+6*i:1+6*i+6])
                psrm = pkt[ 1+6*nrsp+i ]
                pspm = pkt[ 1+7*nrsp+i ]
#                devclass_raw = pkt[1+8*nrsp+3*i:1+8*nrsp+3*i+3]
#                devclass = struct.unpack ("I", "%s\0" % devclass_raw)[0]
                devclass_raw = struct.unpack ("BBB", 
                        pkt[1+8*nrsp+3*i:1+8*nrsp+3*i+3])
                devclass = (devclass_raw[2] << 16) | \
                        (devclass_raw[1] << 8) | \
                        devclass_raw[0]
                clockoff = pkt[1+11*nrsp+2*i:1+11*nrsp+2*i+2]
                rssi = struct.unpack ("b", pkt[1+13*nrsp+i])[0]

                self._device_discovered (addr, devclass, 
                        psrm, pspm, clockoff, rssi, None)
        elif _bt.HAVE_EVT_EXTENDED_INQUIRY_RESULT and event == _bt.EVT_EXTENDED_INQUIRY_RESULT:
            nrsp = struct.unpack ("B", pkt[0])[0]
            for i in range (nrsp):
                addr = _bt.ba2str (pkt[1+6*i:1+6*i+6])
                psrm = pkt[ 1+6*nrsp+i ]
                pspm = pkt[ 1+7*nrsp+i ]
                devclass_raw = struct.unpack ("BBB",
                        pkt[1+8*nrsp+3*i:1+8*nrsp+3*i+3])
                devclass = (devclass_raw[2] << 16) | \
                        (devclass_raw[1] << 8) | \
                        devclass_raw[0]
                clockoff = pkt[1+11*nrsp+2*i:1+11*nrsp+2*i+2]
                rssi = struct.unpack ("b", pkt[1+13*nrsp+i])[0]

                data_len = _bt.EXTENDED_INQUIRY_INFO_SIZE - _bt.INQUIRY_INFO_WITH_RSSI_SIZE
                data = pkt[1+14*nrsp+i:1+14*nrsp+i+data_len]
                name = None
                pos = 0
                while(pos <= len(data)):
                    struct_len = ord(data[pos])
                    if struct_len == 0:
                        break
                    eir_type = ord(data[pos+1])
                    if eir_type == 0x09: # Complete local name
                        name = data[pos+2:pos+struct_len+1]
                    pos += struct_len + 2

                self._device_discovered (addr, devclass,
                        psrm, pspm, clockoff, rssi, name)
        elif event == _bt.EVT_INQUIRY_COMPLETE:
            self.is_inquiring = False
            if len (self.names_to_find) == 0:
#                print "inquiry complete (evt_inquiry_complete)"
                self.sock.close ()
                self.inquiry_complete ()
            else:
                self._send_next_name_req ()

        elif event == _bt.EVT_CMD_STATUS:
            # XXX shold this be "<BBH"
            status, ncmd, opcode = struct.unpack ("BBH", pkt[:4])
            if status != 0:
                self.is_inquiring = False
                self.sock.close ()
                
#                print "inquiry complete (bad status 0x%X 0x%X 0x%X)" % \
#                        (status, ncmd, opcode)
                self.names_to_find = {}
                self.inquiry_complete ()
        elif event == _bt.EVT_REMOTE_NAME_REQ_COMPLETE:
            status = struct.unpack ("B", pkt[0])[0]
            addr = _bt.ba2str (pkt[1:7])
            if status == 0:
                try:
                    name = pkt[7:].split ('\0')[0]
                except IndexError:
                    name = '' 
                if addr in self.names_to_find:
                    device_class, rssi = self.names_to_find[addr][:2]
                    self.device_discovered (addr, device_class, rssi, name)
                    del self.names_to_find[addr]
                    self.names_found[addr] = ( device_class, rssi, name)
                else:
                    pass
            else:
                if addr in self.names_to_find: del self.names_to_find[addr]
                # XXX should we do something when a name lookup fails?
#                print "name req unsuccessful %s - %s" % (addr, status)

            if len (self.names_to_find) == 0:
                self.is_inquiring = False
                self.sock.close ()
                self.inquiry_complete ()
#                print "inquiry complete (name req complete)"
            else:
                self._send_next_name_req ()
        else:
            pass
예제 #6
0
파일: bluez.py 프로젝트: Elleo/rockwatch
    def _process_hci_event(self):
        import socket

        if self.sock is None:
            return
        # voodoo magic!!!
        pkt = self.sock.recv(255)
        ptype, event, plen = struct.unpack("BBB", pkt[:3])
        pkt = pkt[3:]
        if event == _bt.EVT_INQUIRY_RESULT:
            nrsp = struct.unpack("B", pkt[0])[0]
            for i in range(nrsp):
                addr = _bt.ba2str(pkt[1 + 6 * i : 1 + 6 * i + 6])
                psrm = pkt[1 + 6 * nrsp + i]
                pspm = pkt[1 + 7 * nrsp + i]
                devclass_raw = struct.unpack("BBB", pkt[1 + 9 * nrsp + 3 * i : 1 + 9 * nrsp + 3 * i + 3])
                devclass = (devclass_raw[2] << 16) | (devclass_raw[1] << 8) | devclass_raw[0]
                clockoff = pkt[1 + 12 * nrsp + 2 * i : 1 + 12 * nrsp + 2 * i + 2]

                self._device_discovered(addr, devclass, psrm, pspm, clockoff)
        elif event == _bt.EVT_INQUIRY_RESULT_WITH_RSSI:
            nrsp = struct.unpack("B", pkt[0])[0]
            for i in range(nrsp):
                addr = _bt.ba2str(pkt[1 + 6 * i : 1 + 6 * i + 6])
                psrm = pkt[1 + 6 * nrsp + i]
                pspm = pkt[1 + 7 * nrsp + i]
                #                devclass_raw = pkt[1+8*nrsp+3*i:1+8*nrsp+3*i+3]
                #                devclass = struct.unpack ("I", "%s\0" % devclass_raw)[0]
                devclass_raw = struct.unpack("BBB", pkt[1 + 8 * nrsp + 3 * i : 1 + 8 * nrsp + 3 * i + 3])
                devclass = (devclass_raw[2] << 16) | (devclass_raw[1] << 8) | devclass_raw[0]
                clockoff = pkt[1 + 11 * nrsp + 2 * i : 1 + 11 * nrsp + 2 * i + 2]
                rssi = struct.unpack("b", pkt[1 + 13 * nrsp + i])[0]

                self._device_discovered(addr, devclass, psrm, pspm, clockoff)
        elif event == _bt.EVT_INQUIRY_COMPLETE:
            self.is_inquiring = False
            if len(self.names_to_find) == 0:
                #                print "inquiry complete (evt_inquiry_complete)"
                self.sock.close()
                self.inquiry_complete()
            else:
                self._send_next_name_req()

        elif event == _bt.EVT_CMD_STATUS:
            # XXX shold this be "<BBH"
            status, ncmd, opcode = struct.unpack("BBH", pkt[:4])
            if status != 0:
                self.is_inquiring = False
                self.sock.close()

                #                print "inquiry complete (bad status 0x%X 0x%X 0x%X)" % \
                #                        (status, ncmd, opcode)
                self.names_to_find = {}
                self.inquiry_complete()
        elif event == _bt.EVT_REMOTE_NAME_REQ_COMPLETE:
            status = struct.unpack("B", pkt[0])[0]
            addr = _bt.ba2str(pkt[1:7])
            if status == 0:
                try:
                    name = pkt[7:].split("\0")[0]
                except IndexError:
                    name = ""
                if addr in self.names_to_find:
                    device_class = self.names_to_find[addr][0]
                    self.device_discovered(addr, device_class, name)
                    del self.names_to_find[addr]
                    self.names_found[addr] = (device_class, name)
                else:
                    pass
            else:
                if addr in self.names_to_find:
                    del self.names_to_find[addr]
                # XXX should we do something when a name lookup fails?
            #                print "name req unsuccessful %s - %s" % (addr, status)

            if len(self.names_to_find) == 0:
                self.is_inquiring = False
                self.sock.close()
                self.inquiry_complete()
            #                print "inquiry complete (name req complete)"
            else:
                self._send_next_name_req()
        else:
            pass