def find_locks(radio, panid=None): result = [] trackers = dict() last_sequence_number = dict() if panid is not None: print_notify("Looking at PAN ID 0x%04x for lights" % panid) else: print_notify("Looking for lights on the current channel") print_info("Monitoring the network for an extended period") timer = Timer(17) traffic_counter = 0 while not timer.has_expired(): frame = radio.receive() if frame is not None and not is_beacon_request(frame): traffic_counter += 1 if is_data_request(frame) and (panid is None or get_pan_id(frame) == panid): pan = get_pan_id(frame) source = get_source(frame) if not pan in trackers.keys(): trackers[pan] = dict() last_sequence_number[pan] = dict() if not source in trackers[pan].keys(): trackers[pan][source] = TrackWatch() last_sequence_number[pan][source] = -1 if last_sequence_number[pan][source] != frame[Dot15d4FCS].seqnum: trackers[pan][source].click() last_sequence_number[pan][source] = frame[Dot15d4FCS].seqnum if timer.time_passed() > 5 and traffic_counter == 0: print_info("No traffic observed for 5 seconds, giving up") break for pan in trackers: for addr in trackers[pan]: watch = trackers[pan][addr] if watch.variance() is not None and watch.variance( ) < THRESHOLD_VARIANCE and watch.mean() > MIN_FREQUENCY: result.append((pan, addr)) print_notify("Device 0x%04x on PAN 0x%04x resembles a lock" % (addr, pan)) print_debug( "Device 0x%04x on PAN 0x%04x had variance of %f and mean of %f" % (addr, pan, watch.variance(), watch.mean())) return result
def findLights(self, channel): self.radio.set_channel(channel) print_notify("Sending beacon on on channel %d" % self.radio.get_channel()) result = [] trackers = dict() last_sequence_number = dict() print_notify("Looking for lights on the current channel") print_info("Monitoring the network for an extended period") timer = Timer(17) traffic_counter = 0 while not timer.has_expired(): frame = self.radio.receive() if frame is not None and not is_beacon_request(frame): traffic_counter += 1 if is_data_frame(frame): pan = get_pan_id(frame) source = get_source(frame) if not pan in trackers.keys(): trackers[pan] = dict() last_sequence_number[pan] = dict() if not source in trackers[pan].keys(): trackers[pan][source] = TrackWatch() last_sequence_number[pan][source] = -1 if last_sequence_number[pan][source] != frame[ Dot15d4FCS].seqnum: trackers[pan][source].click() last_sequence_number[pan][source] = frame[ Dot15d4FCS].seqnum if timer.time_passed() > 15 and traffic_counter == 0: print_info("No traffic observed for 15 seconds, giving up") break # print(trackers.keys()) # print(last_sequence_number) gateways = dict() for pan in trackers: min_var = 0 gateway = None for addr in trackers[pan]: watch = trackers[pan][addr] if watch.variance() is not None and abs(watch.variance( ) - watch.mean()) > min_var or gateway is None: min_var = watch.variance() gateway = addr result.append((pan, addr)) print_debug( "Device 0x%04x on PAN 0x%04x had variance of %f and mean of %f" % (addr, pan, watch.variance(), watch.mean())) gateways[pan] = gateway print_notify( "Device 0x%04x on PAN 0x%04x possibly resembles a gateway" % (gateway, pan)) result = dict() result[pan] = dict() result[pan]['devices'] = list(trackers[pan].keys()) result[pan]['last_sequence_number'] = last_sequence_number[pan] result[pan]['coordinator'] = gateways[pan] # print(result) return result