update python tools to new proto names

This commit is contained in:
2024-02-25 19:51:41 +01:00
parent b09924b9c8
commit 85e08d31be
4 changed files with 49 additions and 49 deletions

View File

@@ -7,8 +7,8 @@ from google.protobuf.message import DecodeError
from serial.tools import list_ports
from crccheck.crc import Crc
from firmware_pb2 import FirmwareStart, FirmwareFileCheck , FirmwarePackage, FirmwarePackageAck, FirmwareDone
from usb_pb2 import UsbPackageType
from firmware_pb2 import Start, FileCheck , Package, PackageAck, Done
from usb_pb2 import PackageType
Crc32 = Crc(32,0x04C11DB7,0xffffffff)
@@ -26,7 +26,7 @@ def load_firmware(filename):
return crc, size, packages
def make_header(typeid: UsbPackageType, length: int) -> bytearray:
def make_header(typeid: PackageType, length: int) -> bytearray:
struct_format = '<HHB' # '<' for little-endian, 'H' for uint16_t, 'B' for uint8_t
# Calculate the check byte as the sum of length and type
typeidint = int(typeid)
@@ -35,7 +35,7 @@ def make_header(typeid: UsbPackageType, length: int) -> bytearray:
return packed_data
def send_package(typeid : UsbPackageType, data: bytearray, serial: serial.Serial):
def send_package(typeid : PackageType, data: bytearray, serial: serial.Serial):
head = make_header(typeid, len(data))
package = head + data
serial.write(package)
@@ -56,7 +56,7 @@ def read_header(serial):
def receive_ack(serial):
length, typeid = read_header(serial)
if not length or typeid != UsbPackageType.FIRMWAREPACKAGEACK:
if not length or typeid != PackageType.FIRMWAREPACKAGEACK:
return None
# Read the message
@@ -66,18 +66,18 @@ def receive_ack(serial):
return None
# Parse the message
ack = FirmwarePackageAck()
ack = PackageAck()
try:
ack.ParseFromString(message_data)
except DecodeError:
print('Failed to parse FirmwarePackageAck')
print('Failed to parse PackageAck')
return None
return ack
def receive_file_check(serial):
length, typeid = read_header(serial)
if not length or typeid != UsbPackageType.FIRMWAREFILECHECK:
if not length or typeid != PackageType.FIRMWAREFILECHECK:
return None
# Read the message
@@ -87,11 +87,11 @@ def receive_file_check(serial):
return None
# Parse the message
file_check = FirmwareFileCheck()
file_check = FileCheck()
try:
file_check.ParseFromString(message_data)
except DecodeError:
print('Failed to parse FirmwareFileCheck')
print('Failed to parse FileCheck')
return None
return file_check
@@ -121,60 +121,60 @@ if __name__ == "__main__":
crc, size, packages = load_firmware(FILENAME)
# Create a FirmwareStart message
start = FirmwareStart()
# Create a Start message
start = Start()
start.name = os.path.basename(FILENAME)
start.size = size
start.packages = len(packages)
start.device_id = ID
start.crc_fw = crc
# Send the FirmwareStart message
# Send the Start message
print(start)
send_package(UsbPackageType.FIRMWARESTART, start.SerializeToString(), ser)
send_package(PackageType.FIRMWARESTART, start.SerializeToString(), ser)
#time.sleep(1) # wait for the device to process the message
# Receive the FirmwareFileCheck message
# Receive the FileCheck message
file_check = receive_file_check(ser)
print(file_check)
if not file_check:
print('Failed to receive FirmwareFileCheck')
print('Failed to receive FileCheck')
exit(-1)
if file_check.crc_fw == start.crc_fw and file_check.size == start.size and not file_check.ready_for_data:
# Skip to FirmwareDone if the CRC and size match and ready_for_data is false
# Skip to Done if the CRC and size match and ready_for_data is false
print('No need for data transfer')
elif file_check.ready_for_data:
# Send the firmware packages
for (i,pack_data) in enumerate(packages):
package = FirmwarePackage()
package = Package()
package.counter = i
package.crc_pac = Crc32.calc(pack_data)
package.device_id = start.device_id
package.data = pack_data
# Send the FirmwarePackage message
# Send the Package message
print(package)
print(hex(package.crc_pac))
send_package(UsbPackageType.FIRMWAREPACKAGE, package.SerializeToString(), ser)
send_package(PackageType.FIRMWAREPACKAGE, package.SerializeToString(), ser)
# Wait for the FirmwarePackageAck message
# Wait for the PackageAck message
ack = receive_ack(ser)
print(ack)
if not ack.ack:
print(f'Package {i} not acknowledged')
exit(-1)
else:
print('Error in FirmwareFileCheck message')
print('Error in FileCheck message')
exit(-1)
# Send the FirmwareDone message
done = FirmwareDone()
# Send the Done message
done = Done()
done.size = start.size
done.crc_fw = start.crc_fw
done.device_id = start.device_id
print(done)
send_package(UsbPackageType.FIRMWAREDONE, done.SerializeToString(), ser)
send_package(PackageType.FIRMWAREDONE, done.SerializeToString(), ser)