def data_received(self, data): """Collect data until a whole frame has been read (FEND to FEND) and then emit a Frame """ for b in data: if b == KISSMagic.FEND: if self.in_frame: frame = decode_kiss_frame(self._buffer, self.check_crc) if frame is None: continue logger.debug(f"Received {frame}") if frame.command == KISSCommand.Data: asyncio.create_task( self.inbound.put( FrameData(self.port_id, frame.data))) elif frame.command == KISSCommand.SetHardware: self.on_hardware(frame) else: logger.warning(f"Ignoring KISS frame {frame}") self.msgs_recvd += 1 self._buffer.clear() self.in_frame = False else: # keep consuming sequential FENDs continue else: if not self.in_frame: self.in_frame = True self._buffer.append(b)
def datagram_received(self, data: bytes, addr: Tuple[str, int]) -> None: """ Upon receiving a UDP payload from g8bpq: * Check CRC * Find port needed for forwarding * Send payload to port's outbound queue """ udp_logger.debug(f"Received UDP payload {data} from {addr}") try: payload = data[0:-2] crc = struct.unpack(">H", data[-2:])[0] if crc == crc16x25(payload): packet = decode_ax25_packet(data[0:-2]) port_id = self.port_mapping.get(packet.dest) if port_id is not None: packet_logger.info( f"[Port={port_id} UDP] TX {len(packet.buffer)}: {packet}" ) self.port_queues.get(port_id).offer_outbound( FrameData(port_id, packet.buffer)) else: packet_logger.warning( f"[Port=??? UDP] DROP {len(packet.buffer)}: {packet}") else: udp_logger.error( f"Bad CRC, got {crc}, calculated {crc16x25(payload)}") except Exception: udp_logger.exception("Had an error handling UDP data")
def write_packet(self, packet: AX25Packet): frame = FrameData(self.link_port, packet.buffer) if packet.dest == AX25Call("NODES"): packet_logger.debug(f"TX: {packet}") else: packet_logger.info(f"TX: {packet}") if not self.queue.offer_outbound(frame): self.warning("Could not send frame, buffer full")
def write_packet(self, packet: AX25Packet): if packet.dest == AX25Call("NODES"): self.debug(f"TX: {packet}") else: self.debug(f"TX: {packet}") packet_logger.info(f"TX: {packet}") frame = FrameData(self.link_port, packet.buffer) asyncio.create_task(self.outbound.put(frame))
async def produce(count, size, queue): out = [] for i in range(count): data = os.urandom(32) dl_frame = FrameData(1, data, 0) out.append(dl_frame) await asyncio.create_task(queue.put(dl_frame)) print("Done sending") return out
def handle_bytes(self, data: bytes) -> None: """ Collect data until a whole frame has been read (FEND to FEND) and then emit a Frame """ for b in data: if b == KISSMagic.FEND: if self.in_frame: frame = decode_kiss_frame(self._buffer, self.check_crc) if frame is not None and self.saw_fend: self.debug(f"Received frame {frame}") if frame.command == KISSCommand.Data: self.meter("kiss", "decoded").mark() self._l2_queue.offer_inbound( FrameData(self.port_id, frame.data)) elif frame.command == KISSCommand.SetHardware: self.counter("kiss", "hardware").inc() self._on_hardware(frame) else: self.warning( f"Dropping KISS frame with unsupported command {frame.command}: {frame}" ) self.counter("kiss", "unknown").inc() self.msgs_recvd += 1 elif not self.saw_fend: self.warning(f"Dropping partial KISS frame {frame}") self.counter("kiss", "partial").inc() else: self.warning( f"Could not decode frame from bytes {self._buffer}, dropping" ) self.counter("kiss", "error").inc() self._buffer.clear() self.saw_fend = False self.in_frame = False else: # keep consuming sequential FENDs self.saw_fend = True continue else: if not self.in_frame: self.in_frame = True self._buffer.append(b)