Index: scripts/update_package_script/update_package.py =================================================================== diff -u -r37ed1a7ad66212bf70875ea723fdae6576405ce8 -r52796f2db103cc8f253d00b7334b4f79c2d2cfd2 --- scripts/update_package_script/update_package.py (.../update_package.py) (revision 37ed1a7ad66212bf70875ea723fdae6576405ce8) +++ scripts/update_package_script/update_package.py (.../update_package.py) (revision 52796f2db103cc8f253d00b7334b4f79c2d2cfd2) @@ -35,6 +35,7 @@ _MCS_DATA_TYPE_START_INDEX = 7 _MCS_DATA_TYPE_END_INDEX = 9 _MCS_UPDATE_DATA_START_INDEX = 9 + _MCS_SUM_MODULO_256 = 256 def __init__(self): @@ -202,7 +203,8 @@ """ f = open(file_path, 'r') payload_bytes = bytes() - for line in f: + for line_number, line in enumerate(f, start=1): + summation = 0 line = line.strip() # Read the line: e.g. :10000000FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00 -> :[Length][Address][Record Type][Data][Checksum] # 1. ":" is the start of the line @@ -219,8 +221,24 @@ if self._MCS_NUM_OF_BYTES_DEC != num_of_bytes: continue data_type = line[self._MCS_DATA_TYPE_START_INDEX:self._MCS_DATA_TYPE_END_INDEX] if self._MCS_DATA_DATA_TYPE_DEC != data_type: continue - update_data = line[self._MCS_UPDATE_DATA_START_INDEX:len(line) - self._MCS_LINE_CRC_LEN] + # Get the last two characters of the line as CRC + line_crc = line[len(line) - self._MCS_LINE_CRC_LEN:] + line_crc = line_crc[0] + line_crc[1] + line_crc = int(line_crc, 16) + data_line_without_crc = line[self._MCS_START_CODE_END_INDEX:len(line) - self._MCS_LINE_CRC_LEN] + for c in range(0, len(data_line_without_crc)): + # Loop through the line and sum all bytes (length, address, type, and data) + if c % 2 == 0: continue + int_value = data_line_without_crc[c - 1] + data_line_without_crc[c] + int_value = int(int_value, 16) + summation += int_value + # Get the 8-bit modulo. The CRC is the 2's complement + remainder = summation % self._MCS_SUM_MODULO_256 + crc = self._MCS_SUM_MODULO_256 - remainder + if crc != line_crc: self._utilities.file_handle.write("Line Num: {}, Line: {}, Line CRC: {}, Calc CRC: {}\r".format(line_number, line, line_crc, crc)) # TODO if the CRC failed, we should fail the update + + update_data = line[self._MCS_UPDATE_DATA_START_INDEX:len(line) - self._MCS_LINE_CRC_LEN] for b in range(0, len(update_data)): # Loop through the list of the data extracted data every two characters are concatenated to become one byte of data # For example: 00000000300020010000001430014004 -> 00 00 00 00 30 00 20 01 00 00 00 14 30 01 40 04 @@ -241,6 +259,7 @@ self._utilities.send_software_update_msg(target, payload_bytes, len(payload_bytes)) self._update_progress_status() payload_bytes = bytes() + # Close the mcs file f.close() def _verify_stack_update(self): @@ -318,7 +337,7 @@ if len(list_of_update_files) == 0: exit(0) # No update files to process #self._utilities.send_update_available_to_firmware_stacks() # TODO uncomment - #self._handle_broadcast_message_thread(start_thread=True) + self._handle_broadcast_message_thread(start_thread=True) time.sleep(1.5) # Wait a while or have a broadcast check for update_file in list_of_update_files: # Make sure the file is opened a binary or hex @@ -329,4 +348,4 @@ self._verify_stack_update() # TODO skip if the timeout or another error # Done with update binary files, stop sending the broadcast message - #self._handle_broadcast_message_thread(start_thread=False) + self._handle_broadcast_message_thread(start_thread=False)