humpback-dds/flash.py

49 lines
1.5 KiB
Python
Raw Normal View History

2020-10-06 15:01:04 +08:00
import argparse
import struct
def get_argparser():
parser = argparse.ArgumentParser(description="MQTT-controlled 4-channel DDS signal generator")
parser.add_argument("cidr", nargs=1,
metavar=("Client_CIDR"),
help="IPv4 Address of Humpback-DDS in CIDR notation")
parser.add_argument("mac_addr", nargs=1,
metavar=("Client_MAC"),
help="MAC Address of Humpback-DDS")
parser.add_argument("broker_ip", nargs=1,
metavar=("Broker_IP"),
help="IP address of MQTT broker")
parser.add_argument("name", nargs=1,
metavar=("Name"),
help="Device name of Humpback-DDS")
return parser
def write_record(f, key, value):
key_size = len(key) + 1
value_size = len(value)
record_size = key_size + value_size + 4
2020-12-18 17:44:38 +08:00
f.write(struct.pack(">l", record_size))
2020-10-06 15:01:04 +08:00
f.write(key.encode())
f.write(b"\x00")
f.write(value)
def write_end_marker(f):
f.write(b"\xff\xff\xff\xff")
def main():
args = get_argparser().parse_args()
with open("flash_config.bin", "wb") as fo:
write_record(fo, "CIDR", args.cidr[0].encode())
write_record(fo, "MAC", args.mac_addr[0].encode())
write_record(fo, "BrokerIP", args.broker_ip[0].encode())
write_record(fo, "Name", args.name[0].encode())
write_end_marker(fo)
if __name__ == "__main__":
main()