import shutil
import sys
import os
import json

import prottcp

BOOT_PATH ="file/JQ_JCXB_V54.bin"
APP_PATH ="file/jqcoder_slave_20230912--1.bin"
SCHEME_PATH = "file/JQ硬件EX工厂注码090701--2.json"
OUT_PATH = "file/coder_jqslave_boot.bin"

# 创建离线下载器的镜像


# 填充指定个数的byte
def arr_byte_copy(byte:int,num:int):
    t=bytearray()
    for i in range(num):
        t.append(byte)
    return t
# int转数组
def arr_from_int(num:int):
    return  bytearray([num&0xff,(num>>8)&0xff,(num>>16)&0xff,(num>>24)&0xff])


def creat():
    boot=BOOT_PATH
    app=APP_PATH
    scheme=SCHEME_PATH
    d=bytearray()
    with open(boot,"rb") as f:
        d+=f.read()
    d+=arr_byte_copy(0xff,0x2400-len(d))
    with open(app,"rb") as f:
        d+=f.read()
    d+=arr_byte_copy(0xff,0xf400-len(d))
    with open(scheme,"rb") as f:
        json_obj=json.loads(f.read())
        d+=prottcp.scheme_to_byte(json_obj)
    d+=arr_byte_copy(0xff,0xfc00-len(d))
    d+=arr_from_int(0x669955aa)
    with open(OUT_PATH,"wb+") as f:
        f.write(d)
        print(OUT_PATH+" create boot file success.")
    
    

if __name__ == "__main__":
    creat()


