artiq/artiq/frontend/artiq_route.py

84 lines
2.3 KiB
Python
Raw Normal View History

2018-09-04 19:04:27 +08:00
#!/usr/bin/env python3
import argparse
def get_argparser():
parser = argparse.ArgumentParser(description="ARTIQ DRTIO routing table "
"manipulation tool")
parser.add_argument("file", metavar="FILE", type=str,
help="target file")
action = parser.add_subparsers(dest="action")
action.required = True
action.add_parser("init", help="create a new empty routing table")
action.add_parser("show", help="show contents of routing table")
a_set = action.add_parser("set", help="set routing table entry")
a_set.add_argument("destination", metavar="DESTINATION", type=int,
help="destination to operate on")
a_set.add_argument("hop", metavar="HOP", type=int, nargs="*",
help="hop(s) to the destination")
return parser
2018-09-09 21:49:28 +08:00
DEST_COUNT = 256
2018-09-04 19:04:27 +08:00
MAX_HOPS = 32
def init(filename):
with open(filename, "wb") as f:
2018-09-09 21:49:28 +08:00
f.write(b"\xff"*(DEST_COUNT*MAX_HOPS))
2018-09-04 19:04:27 +08:00
def show_routes(filename):
routes = []
with open(filename, "rb") as f:
2018-09-09 21:49:28 +08:00
for i in range(DEST_COUNT):
2018-09-04 19:04:27 +08:00
hops = [int.from_bytes(f.read(1), "big") for j in range(MAX_HOPS)]
routes.append(hops)
for destination, route in enumerate(routes):
if route[0] != 0xff:
fmt = "{:3d}:".format(destination)
for hop in route:
if hop == 0xff:
break
fmt += " {:3d}".format(hop)
print(fmt)
def set_route(filename, destination, hops):
with open(filename, "r+b") as f:
2018-09-09 21:49:28 +08:00
if destination >= DEST_COUNT:
raise ValueError("destination must be less than {}".format(DEST_COUNT))
2018-09-04 19:04:27 +08:00
f.seek(destination*MAX_HOPS)
if len(hops) + 1 >= MAX_HOPS:
raise ValueError("too many hops")
for hop in hops:
if hop >= 0xff:
raise ValueError("all hops must be less than 255")
hops = hops + [0xff]*(MAX_HOPS-len(hops))
f.write(bytes(hops))
def main():
args = get_argparser().parse_args()
if args.action == "init":
init(args.file)
elif args.action == "show":
show_routes(args.file)
elif args.action == "set":
set_route(args.file, args.destination, args.hop)
else:
raise ValueError
if __name__ == "__main__":
main()