def loop(verbose=False): while True: pkt = Radio.rx() if pkt is None: continue try: ieee, typ = Parser.parse(pkt, filter_dupes=True) if typ != "zcl": continue except: print(pkt) continue try: cluster = ieee.payload.payload.cluster # NWK APS ZCL ZCL payload = ieee.payload.payload.payload.payload command = payload.command if verbose: print(command) if cluster not in handlers: continue if command not in handlers[cluster]: continue handlers[cluster][command](payload) except: print("Handler failed") raise
def process_packet(data): try: ieee, packet_type = Parser.parse(data) if ieee.frame_type == IEEE802154.FRAME_TYPE_ACK: print("ack(%d)" % ieee.seq) return print(packet_type + ":", ieee) if packet_type != "zcl": return nwk = ieee.payload aps = nwk.payload zcl = aps.payload print("%02x %04x%c %04x: cluster %04x:%02x/%d" % ( nwk.seq, nwk.src, ' ' if nwk.src == ieee.src else '+', # repeater nwk.dst, aps.cluster, zcl.command, zcl.direction, )) except: print(hexlify(data)) raise
def parse(verbose=False): mem_info() gc.collect() mem_info() count = 0 while True: pkt = Radio.rx() if pkt is None: continue try: ieee, typ = Parser.parse(pkt, filter_dupes=True) if typ is None: continue except: print(pkt) raise if typ != "zcl": if typ == "zcl?" and ieee.payload.payload.cluster != 0: print(ieee) continue if verbose: print(ieee) #cluster = ieee.payload.payload.cluster # IEEE/NWK/APS/ZCL/ZCL zb = ieee.payload.payload.payload zcl = zb.payload #print(ieee) if verbose or zb.command != 0x0B: print(zcl)
def rx(self, data): if data is None or len( data) < 2: # or len(data) < 2 or len(data) & 1 != 0: self.tick() return try: ieee = IEEE802154.IEEE802154(data=data) # check for duplicates, using the short address # long address duplicates will be processed if type(ieee.src) is int: if ieee.src in self.seqs \ and self.seqs[ieee.src] == ieee.seq: return self.seqs[ieee.src] = ieee.seq if self.verbose: print("RX: " + str(Parser.parse(data)[0])) except Exception as e: print("IEEE error: " + str(hexlify(data))) raise return if ieee.frame_type == IEEE802154.FRAME_TYPE_CMD: return self.handle_command(ieee) elif ieee.frame_type == IEEE802154.FRAME_TYPE_ACK: return self.handle_ack(ieee) elif ieee.frame_type == IEEE802154.FRAME_TYPE_BEACON: return self.handle_beacon(ieee) elif ieee.frame_type == IEEE802154.FRAME_TYPE_DATA: # for the next layer up, pass it to the handler return self.handler(ieee.payload) else: # should signal a wtf? print("RX: " + str(Parser.parse(data)[0])) pass
def tx_ieee(self, pkt): pkt.seq = self.seq self.seq = (self.seq + 1) & 0x3F #print("SENDING %02x" % (pkt.seq)) self.last_pkt = pkt.serialize() if pkt.command != IEEE802154.COMMAND_DATA_REQUEST: print("TX: " + str(Parser.parse(self.last_pkt)[0])) self.radio.tx(self.last_pkt) if pkt.ack_req: self.last_tx = ticks_us() self.pending_seq = pkt.seq self.retries = 3 else: self.pending_seq = None