def _update(self, resp): ScanEntry._update(self, resp) if self.addrType is "random": self.vendor = "None (Random MAC address)" else: self.vendor = lookup_vendor(self.addr) if self.scanData: self.data = self._get_data(self.getScanData())
def gatherAverageRSSI(self, manufacturer, n_samples, passive=False): self.clear() self.start(passive=passive) rssi_scans = [] while len(rssi_scans) < n_samples: # wait 3 seconds before a timeout resp = self._waitResp(['scan', 'stat'], 3.0) # ^ if resp is None: break respType = resp['rsp'][0] if respType == 'stat': # if scan ended, restart it if resp['state'][0] == 'disc': self._mgmtCmd("scan") elif respType == 'scan': # device found addr = binascii.b2a_hex(resp['addr'][0]).decode('utf-8') addr = ':'.join([addr[i:i + 2] for i in range(0, 12, 2)]) if addr in self.scanned: dev = self.scanned[addr] else: dev = ScanEntry(addr, self.iface) self.scanned[addr] = dev isNewData = dev._update(resp) for (adtype, desc, value) in dev.getScanData(): if desc == "Manufacturer" and value == manufacturer: rssi_scans.append(dev.rssi) else: raise BTLEException(BTLEException.INTERNAL_ERROR, "Unexpected response: " + respType) mean_rssi = mean(rssi_scans) std_dev = stdev(rssi_scans) cutoff_rssi = [] lower_cutoff = mean_rssi - std_dev upper_cutoff = mean_rssi + std_dev for x in rssi_scans: if not (x < lower_cutoff or x > upper_cutoff): cutoff_rssi.append(x) self.stop() return mean(cutoff_rssi)
def process(self, timeout=10.0): if self._helper is None: raise BTLEException(BTLEException.INTERNAL_ERROR, "Helper not started (did you call start()?)") start = time.time() while True: if timeout: remain = start + timeout - time.time() if remain <= 0.0: break else: remain = None resp = self._waitResp(['scan', 'stat'], remain) if resp is None: break respType = resp['rsp'][0] if respType == 'stat': # if scan ended, restart it if resp['state'][0] == 'disc': self._mgmtCmd("scan") elif respType == 'scan': # device found addr = binascii.b2a_hex(resp['addr'][0]).decode('utf-8') addr = ':'.join([addr[i:i + 2] for i in range(0, 12, 2)]) if addr in self.scanned: dev = self.scanned[addr] else: dev = ScanEntry(addr, self.iface) self.scanned[addr] = dev isNewData = dev._update(resp) if self.delegate is not None: self.delegate.handleDiscovery(dev, (dev.updateCount <= 1), isNewData) else: raise BTLEException(BTLEException.INTERNAL_ERROR, "Unexpected response: " + respType)
def process(self, timeout=300.0): """ Method receives advertisements from nodes. Variable timeout has default value 300 seconds, it define time after which method will end. """ if self._helper is None: raise BTLEException(BTLEException.INTERNAL_ERROR, "Helper not started (did you call start()?)") start = time.time() while True: if timeout: remain = start + timeout - time.time() if remain <= 0.0: break else: remain = None resp = self._waitResp(['scan', 'stat'], remain) if resp is None: break respType = resp['rsp'][0] if respType == 'stat': logging.info("STAT message,") # if scan ended, restart it if resp['state'][0] == 'disc': logging.warning("Executing SCAN cmd!") self._mgmtCmd("scan") elif respType == 'scan': # device found addr = binascii.b2a_hex(resp['addr'][0]).decode('utf-8') addr = ':'.join([addr[i:i + 2] for i in range(0, 12, 2)]) addr = addr.upper() if not Device.select(lambda d: d.id.upper() == addr).first( ) and not settings.ALLOW_ANY: logging.warning( "Unknown device {} send message, skipping...".format( addr)) continue dev = ScanEntry(addr, self.iface) logging.info("SCAN message from {}".format(addr)) dev._update(resp) if not settings.ALLOW_ANY: name = '' for data in dev.getScanData(): for x in data: if type("Name") == type(x) and "Name" in x: name = data[-1] if not settings.NAME_VALIDATION: logging.warning( "Accepting device without name validation...") elif Device[addr].name != name and Device[ addr].name != None: logging.warning( "{} invalid name valid: {}, received: {}, skipping..." .format(addr, name, Device[addr].name)) continue if self.delegate is not None: self.delegate.handleDiscovery(dev, (dev.updateCount <= 1), True) else: raise BTLEException(BTLEException.INTERNAL_ERROR, "Unexpected response: " + respType)