~aleteoryx/muditaos

aea5472a13a090a3a1cb2f1c879faea076ab5eba — Marek Niepieklo 4 years ago 0596951
[CP-254] Use Send File for Update package

Switched from raw data transfer to FS Send File
1 files changed, 18 insertions(+), 35 deletions(-)

M test/send_update_package.py
M test/send_update_package.py => test/send_update_package.py +18 -35
@@ 9,6 9,7 @@ import os.path
import json
import atexit
import binascii
from tqdm import tqdm

sys.path.append(
    os.path.abspath(os.path.join(os.path.dirname(__file__), os.path.pardir)))


@@ 18,54 19,36 @@ from harness.interface.defs import status
from harness.interface.error import TestError, Error
from functools import partial

# uploaded file chunk size - according to
# https://appnroll.atlassian.net/wiki/spaces/MFP/pages/656637953/Protocol+description
CHUNK_SIZE = 1024 * 16

def send_update_package(harness, package_filepath: str):
    file_size = os.path.getsize(package_filepath)
    file_name = package_filepath.split('/')[-1]

    with open(package_filepath, 'rb') as file:
        file_data = open(package_filepath,'rb').read()
        file_crc32 = format((binascii.crc32(file_data) & 0xFFFFFFFF), '08x')

    print(f"Sending {file_name}, size {file_size}, CRC32 {file_crc32}")

    body = {"command": "download", "fileName": file_name, "fileSize": file_size, "fileCrc32" : file_crc32}
    ret = harness.endpoint_request("filesystem", "post", body)
    body = {"fileName" : "/sys/user/updates/" + file_name, "fileSize" : file_size, "fileCrc32" : file_crc32}
    ret = harness.endpoint_request("filesystem", "put", body)

    if ret["status"] != status["Accepted"]:
        print("Failed to initiate package transfer")
        print(json.dumps(ret))
        return False
    assert ret["status"] == status["OK"]
    assert ret["body"]["txID"] != 0

    time.sleep(.1)
    txID      = ret["body"]["txID"]
    chunkSize = ret["body"]["chunkSize"]
    chunkNo = 1

    print("Sending update file to the target")
    serial = harness.get_connection().get_serial()
    with open(package_filepath, 'rb') as file:
        for chunk in iter(partial(file.read, CHUNK_SIZE), b''):
            print(".", end='', flush=True)
            serial.write(chunk)

    print("")

    time.sleep(.1)

    if serial.in_waiting > 0:
        result = harness.get_connection().read(10)
        ret = json.loads(result)
        body = ret['body']

        if "status" in body:
            stat = body["status"]
            print(f"Transfer status: {stat}")

        if "fileCrc32" in body:
            fileCrc32 = body["fileCrc32"]
            if fileCrc32 != file_crc32:
                print(f"Returned CRC32 mismatch: {fileCrc32}")
                return False
        with tqdm(total=file_size, unit='B', unit_scale=True) as pbar:
            for chunk in iter(partial(file.read, chunkSize), b''):
                data = binascii.b2a_base64(chunk).decode()
                pbar.update(chunkSize)
                body = {"txID" : txID, "chunkNo": chunkNo, "data" : data}
                ret = harness.endpoint_request("filesystem", "put", body)
                assert ret["status"] == status["OK"]
                time.sleep(.1)
                chunkNo += 1

    print("Sending complete")