예제 #1
0
def main():
    parser = argparse.ArgumentParser(description='Decode packets from a serial port')
    parser.add_argument("port", help="Serial port to open")
    parser.add_argument("baud", type=int, help="Baudrate to use")
    parser.add_argument("--check_crc", type=bool, default=False)
    args = parser.parse_args()

    port_config = PortConfig.from_dict(0, {
        "port.enabled": True,
        "port.type": "serial",
        "serial.device": args.port,
        "serial.speed": args.baud
    })

    loop = asyncio.get_event_loop()
    in_queue: asyncio.Queue = asyncio.Queue()
    out_queue: asyncio.Queue = asyncio.Queue()
    protocol_factory = partial(KISSProtocol, loop, in_queue, out_queue, port_config.port_id(), check_crc=args.check_crc)
    protocol = serial_asyncio.create_serial_connection(loop, protocol_factory, args.port, baudrate=args.baud)
    asyncio.ensure_future(protocol)
    print('Scheduled Serial connection')

    asyncio.ensure_future(printer(in_queue))
    loop.run_forever()
    print('Done')
예제 #2
0
async def async_main():
    parser = argparse.ArgumentParser(
        description='Send random data to port1 and verify it on port2')
    parser.add_argument("port1", help="Serial device 1")
    parser.add_argument("port2", help="Serial device 2")
    parser.add_argument("baud", type=int, help="Baudrate to use")
    parser.add_argument("count",
                        type=int,
                        default=1000,
                        help="Number of packets to send")
    parser.add_argument("size",
                        type=int,
                        default=32,
                        help="Number of random bytes per packet")

    parser.add_argument("--check_crc", type=bool, default=False)
    args = parser.parse_args()

    print("Start")

    port_1 = PortConfig.from_dict(
        1, {
            "port.type": "serial",
            "serial.device": args.port1,
            "serial.speed": args.baud,
            "port.framing": "kiss",
            "kiss.checksum": args.check_crc
        })

    port_2 = PortConfig.from_dict(
        2, {
            "port.type": "serial",
            "serial.device": args.port2,
            "serial.speed": args.baud,
            "port.framing": "kiss",
            "kiss.checksum": args.check_crc
        })
    in_queue_1: asyncio.Queue = asyncio.Queue()
    out_queue_1: asyncio.Queue = asyncio.Queue()
    await kiss_port_factory(in_queue_1, out_queue_1, port_1)

    in_queue_2: asyncio.Queue = asyncio.Queue()
    out_queue_2: asyncio.Queue = asyncio.Queue()
    await kiss_port_factory(in_queue_2, out_queue_2, port_2)

    produce_coro = produce(args.count, args.size, out_queue_1)
    consume_coro = consume(args.count, in_queue_2)

    t0 = time.time()
    sent = await produce_coro
    try:
        received = await asyncio.wait_for(consume_coro, 10)
    except asyncio.TimeoutError:
        print('Timed out!')
        exit(1)

    t1 = time.time()

    print(f"Time taken: {t1-t0:0.4f}s")
    print(f"Packets per second: {(len(sent) / (t1-t0)):0.0f}")
    print(f"Packets sent: {len(sent)}")
    print(f"Packets heard: {len(received)}")

    corrupt = 0
    for a, b in zip(sent, received):
        if a.data != b.buffer:
            corrupt += 1

    print(f"Corrupt packets: {corrupt}")
예제 #3
0
def main():
    parser = argparse.ArgumentParser(
        description='Decode packets from a serial port')
    parser.add_argument("port", help="Serial port to open")
    parser.add_argument("baud", type=int, help="Baudrate to use")
    parser.add_argument("local_call", help="Your callsign (e.g., K4DBZ-10)")
    parser.add_argument("local_alias", help="Your alias (e.g., ZDBZ10)")
    parser.add_argument("remote_call", help="Remote callsign")
    parser.add_argument("-datalink", help="Force L2 mode", action="store_true")
    parser.add_argument("--check-crc", type=bool, default=False)
    parser.add_argument("--monitor-port", type=int)
    parser.add_argument("--debug", action="store_true")

    args = parser.parse_args()

    port_config = PortConfig.from_dict(
        0, {
            "port.enabled": True,
            "port.type": "serial",
            "serial.device": args.port,
            "serial.speed": args.baud
        })

    loop = asyncio.get_event_loop()

    in_queue: asyncio.Queue = asyncio.Queue()
    out_queue: asyncio.Queue = asyncio.Queue()
    loop.create_task(kiss_port_factory(in_queue, out_queue, port_config))

    # Wire the port with an AX25 layer
    dlm = DataLinkManager(AX25Call.parse(args.local_call),
                          port_config.port_id(), in_queue, out_queue,
                          loop.create_future)
    dlm.add_l3_handler(IdHandler())

    # Wire up the network

    network_config = NetworkConfig.from_dict({
        "netrom.node.call": args.local_call,
        "netrom.node.alias": args.local_alias,
        "netrom.ttl": 7,
        "netrom.nodes.interval": 60,
        "netrom.obs.init": 6,
        "netrom.obs.min": 4,
        "netrom.nodes.quality.min": 74
    })
    nl = NetworkManager(network_config, loop)
    nl.attach_data_link(dlm)
    tty = TTY(args.local_call, args.local_alias, args.remote_call, nl)

    loop.create_task(tty.start())
    loop.add_reader(sys.stdin, tty.handle_stdin)

    #server = loop.run_until_complete(loop.create_server(lambda: Monitor(), '127.0.0.1', 8889))
    #loop.create_task(server.serve_forever())

    # Configure logging
    main_logger = logging.getLogger("root")
    main_logger.setLevel(logging.ERROR)
    #packet_logger.addHandler(logging.StreamHandler(sys.stdout))

    if args.debug:
        main_logger.setLevel(logging.DEBUG)

        state_logger = logging.getLogger("ax25.state")
        state_logger.setLevel(logging.DEBUG)
        state_logger.addHandler(logging.StreamHandler(sys.stdout))

        state_logger = logging.getLogger("netrom.state")
        state_logger.setLevel(logging.DEBUG)
        state_logger.addHandler(logging.StreamHandler(sys.stdout))

    loop.add_signal_handler(signal.SIGTERM, handle_signal, dlm, tty, loop)
    loop.add_signal_handler(signal.SIGINT, handle_signal, dlm, tty, loop)
    loop.create_task(dlm.start())
    loop.create_task(nl.start())

    try:
        loop.run_forever()
    finally:
        loop.close()
예제 #4
0
def main():
    parser = argparse.ArgumentParser(description='Decode packets from a serial port')
    parser.add_argument("port", help="Serial port to open")
    parser.add_argument("baud", type=int, help="Baudrate to use")
    parser.add_argument("local_call", help="Your callsign (e.g., K4DBZ-10)")
    parser.add_argument("local_alias", help="Your alias (e.g., ZDBZ10)")
    parser.add_argument("remote_call", help="Remote callsign")
    parser.add_argument("-datalink", help="Force L2 mode", action="store_true")
    parser.add_argument("--check-crc", type=bool, default=False)
    parser.add_argument("--monitor-port", type=int)
    parser.add_argument("--debug", action="store_true")
    args = parser.parse_args()

    # Configure logging
    main_logger = logging.getLogger("root")
    main_logger.setLevel(logging.ERROR)
    if args.debug:
        main_logger.setLevel(logging.DEBUG)

        state_logger = logging.getLogger("ax25.state")
        state_logger.setLevel(logging.DEBUG)
        state_logger.addHandler(logging.StreamHandler(sys.stdout))

        state_logger = logging.getLogger("netrom.state")
        state_logger.setLevel(logging.DEBUG)
        state_logger.addHandler(logging.StreamHandler(sys.stdout))

    scheduler = Scheduler()

    # Configure and initialize I/O device and L2
    port_config = PortConfig.from_dict(0, {
        "port.enabled": True,
        "port.type": "serial",
        "serial.device": args.port,
        "serial.speed": args.baud
    })

    # Initialize I/O device and L2
    l3_protocols = L3Protocols()
    l2_multi = DefaultLinkMultiplexer(L3PriorityQueue, scheduler)
    l2_queueing = L2FIFOQueue(20, AX25Protocol.maximum_frame_size())
    l2 = AX25Protocol(port_config, port_config.port_id(), AX25Call.parse(args.local_call), scheduler,
                      l2_queueing, l2_multi, l3_protocols)
    kiss = KISSProtocol(port_config.port_id(), l2_queueing, port_config.get_boolean("kiss.checksum", False))
    SerialDevice(kiss,
                 port_config.get("serial.device"),
                 port_config.get_int("serial.speed"),
                 port_config.get_float("serial.timeout"),
                 scheduler)
    scheduler.submit(L2IOLoop(l2_queueing, l2))

    # Initialize L3 and L4
    network_config = NetworkConfig.from_dict({
        "netrom.node.call": args.local_call,
        "netrom.node.alias": args.local_alias,
        "netrom.ttl": 7,
        "netrom.nodes.interval": 60,
        "netrom.obs.init": 6,
        "netrom.obs.min": 4,
        "netrom.nodes.quality.min": 74
    })

    # Register L3 protocols
    netrom_l3 = NetRomL3(AX25Call.parse(network_config.node_call()), network_config.node_alias(),
                         scheduler, l2_multi, NetRomRoutingTable(network_config.node_alias()))
    l3_protocols.register(netrom_l3)

    # Create the L4 protocol
    netrom_l4 = NetRomTransportProtocol(network_config, netrom_l3, scheduler)

    async def wait_for_network():
        tty = TTY()
        loop.add_reader(sys.stdin, tty.handle_stdin)
        loop.add_signal_handler(signal.SIGTERM, tty.handle_signal, loop, scheduler)
        loop.add_signal_handler(signal.SIGINT, tty.handle_signal, loop, scheduler)

        remote_call = AX25Call.parse(args.remote_call)
        while True:
            (found, mtu) = netrom_l3.route_packet(NetRomAddress.from_call(remote_call))
            if found:
                break
            await asyncio.sleep(0.200)
        main_logger.info(f"Learned route to {remote_call}")
        await asyncio.sleep(1.200)
        netrom_l4.open(protocol_factory=lambda: tty,
                       local_call=AX25Call.parse(args.local_call),
                       remote_call=AX25Call.parse(args.remote_call),
                       origin_user=AX25Call(args.local_alias),
                       origin_node=AX25Call(args.local_call))

    loop = asyncio.get_event_loop()
    loop.create_task(wait_for_network())

    try:
        loop.run_forever()
    finally:
        loop.close()
예제 #5
0
def main():
    parser = argparse.ArgumentParser(
        description='Broadcast to mesh network over serial device')
    parser.add_argument("device", help="Serial port to open")
    parser.add_argument("baud", type=int, help="Baudrate to use")
    parser.add_argument("callsign", help="Your callsign (e.g., K4DBZ-10)")
    parser.add_argument("address", help="Local address, e.g., 00.1a")
    parser.add_argument("port", type=int, help="Port", default=10)
    parser.add_argument("--debug", action="store_true")
    args = parser.parse_args()

    # Configure logging
    main_logger = logging.getLogger("root")
    main_logger.setLevel(logging.ERROR)
    main_logger.addHandler(logging.StreamHandler(sys.stdout))

    if args.debug:
        main_logger.setLevel(logging.DEBUG)
        state_logger = logging.getLogger("ax25.state")
        state_logger.setLevel(logging.DEBUG)
        state_logger.addHandler(logging.StreamHandler(sys.stdout))

    scheduler = Scheduler()

    # Configure and initialize I/O device and L2
    port_config = PortConfig.from_dict(
        0, {
            "port.enabled": True,
            "port.type": "serial",
            "serial.device": args.device,
            "serial.speed": args.baud
        })

    # Initialize I/O device and L2
    l3_protocols = L3Protocols()
    l2_multi = DefaultLinkMultiplexer(L3PriorityQueue, scheduler)
    l2_queueing = L2FIFOQueue(20, AX25Protocol.maximum_frame_size())
    l2 = AX25Protocol(port_config, port_config.port_id(),
                      AX25Call.parse(args.callsign), scheduler, l2_queueing,
                      l2_multi, l3_protocols)
    kiss = KISSProtocol(port_config.port_id(), l2_queueing,
                        port_config.get_boolean("kiss.checksum", False))
    SerialDevice(kiss, port_config.get("serial.device"),
                 port_config.get_int("serial.speed"),
                 port_config.get_float("serial.timeout"), scheduler)
    scheduler.submit(L2IOLoop(l2_queueing, l2))

    addr = MeshAddress.parse(args.address)
    mesh_l3 = MeshProtocol(our_address=addr,
                           link_multiplexer=l2_multi,
                           scheduler=scheduler)
    l3_protocols.register(mesh_l3)

    mesh_l4 = MeshTransportManager(mesh_l3)

    tty = TTY()
    loop = asyncio.get_event_loop()
    loop.add_reader(sys.stdin, tty.handle_stdin)
    loop.add_signal_handler(signal.SIGTERM, tty.handle_signal, loop, scheduler)
    loop.add_signal_handler(signal.SIGINT, tty.handle_signal, loop, scheduler)

    mesh_l4.connect(protocol_factory=lambda: tty,
                    port=args.port,
                    local_address=addr,
                    remote_address=MeshProtocol.BroadcastAddress)

    try:
        loop.run_forever()
    finally:
        loop.close()