Index: dialin/dg/accelerometer.py =================================================================== diff -u -ra2cdbc7e007c6bd73e12344b4c89356a20b53f4f -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/dg/accelerometer.py (.../accelerometer.py) (revision a2cdbc7e007c6bd73e12344b4c89356a20b53f4f) +++ dialin/dg/accelerometer.py (.../accelerometer.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -239,40 +239,3 @@ else: self.logger.debug("Timeout!!!!") return False - - def cmd_set_accel_calibration(self, x_offset, y_offset, z_offset): - """ - Constructs and sends the DG accelerometer axis calibration_record offsets message to the DG. - - @param x_offset: float - calibration_record offset for X axis - @param y_offset: float - calibration_record offset for Y axis - @param z_offset: float - calibration_record offset for Z axis - - @return: 1 if successful, zero otherwise - """ - x = float_to_bytearray(x_offset) - y = float_to_bytearray(y_offset) - z = float_to_bytearray(z_offset) - payload = x + y + z - - message = DenaliMessage.build_message(channel_id=DenaliChannels.dialin_to_dg_ch_id, - message_id=MsgIds.MSG_ID_DG_ACCEL_SET_CALIBRATION.value, - payload=payload) - - self.logger.debug("Setting DG accelerometer axis calibration_record offsets") - - # Send message - received_message = self.can_interface.send(message) - - # If there is content... - if received_message is not None: - self.logger.debug(received_message) - # str_res = str(flow) - self.logger.debug("DG accelerometer calibration_record axis offsets set to : " + str(x_offset) + ", " + str(y_offset) + ", " + - str(z_offset) + ". " + str(received_message['message'][DenaliMessage.PAYLOAD_START_INDEX])) - # response payload is OK or not OK - return received_message['message'][DenaliMessage.PAYLOAD_START_INDEX] - else: - self.logger.debug("Timeout!!!!") - return False - Index: dialin/dg/calibration_record.py =================================================================== diff -u -r3f54ef1730d579fecd7a33e12ac7fdc85e8d54cb -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/dg/calibration_record.py (.../calibration_record.py) (revision 3f54ef1730d579fecd7a33e12ac7fdc85e8d54cb) +++ dialin/dg/calibration_record.py (.../calibration_record.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -3,15 +3,14 @@ import datetime import time from collections import OrderedDict -from ..ui.crc import crc_16 from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import (DenaliMessage, DenaliChannels) from ..utils.base import _AbstractSubSystem, _publish from ..utils.nv_ops_utils import NVOpsUtils from logging import Logger -class DGCalibration(_AbstractSubSystem): +class DGCalibrationNVRecord(_AbstractSubSystem): """ Dialysate Generator (DG) Dialin API sub-class for calibration commands. @@ -29,10 +28,13 @@ _DEFAULT_TIME_VALUE = 0 _DEFAULT_CRC_VALUE = 0 + # Maximum allowed bytes that are allowed to be written to EEPROM in firmware + # The padding size then is calculated to be divisions of 16 _EEPROM_MAX_BYTES_TO_WRITE = 16 - _PAYLOAD_TRANSFER_DELAY = 0.2 - _DIALIN_RECORD_UPDATE_DELAY = 0.2 + # Delay in between each payload transfer + _PAYLOAD_TRANSFER_DELAY_S = 0.2 + _DIALIN_RECORD_UPDATE_DELAY_S = 0.2 def __init__(self, can_interface, logger: Logger): """ @@ -46,7 +48,7 @@ self.logger = logger self.current_message = 0 self.total_messages = 0 - self.length = 0 + self.received_msg_length = 0 self.cal_data = 0 self._raw_cal_record = [] self._write_fw_data_to_excel = False @@ -106,7 +108,7 @@ self.logger.debug("Timeout!!!!") return False - @_publish(["current_message", "total_messages", "length", "service_data"]) + @_publish(["current_message", "total_messages", "received_msg_length", "service_data"]) def _handler_dg_calibration_sync(self, message): """ Handles published DG calibration_record record messages. DG calibration records are captured for @@ -125,10 +127,10 @@ self.current_message = curr self.total_messages = total - self.length = length + self.received_msg_length = length # The end of calibration_record record payload is from the start index + 12 bytes for the current message +total - # messages + the length of calibration_record. The rest is the CAN messaging CRC that is not needed to be kept - end_of_data_index = self._RECORD_START_INDEX + self._RECORD_SPECS_BYTES + self.length + # messages + the received_msg_length of calibration_record. The rest is the CAN messaging CRC that is not needed to be kept + end_of_data_index = self._RECORD_START_INDEX + self._RECORD_SPECS_BYTES + self.received_msg_length # Get the calibration_record data only self.cal_data = message['message'][self._RECORD_START_INDEX:end_of_data_index] @@ -159,7 +161,7 @@ self.get_dg_calibration_record() # Wait until reading calibration record from firmware is updated while self._utilities.get_reading_record_status() is not True: - time.sleep(self._DIALIN_RECORD_UPDATE_DELAY) + time.sleep(self._DIALIN_RECORD_UPDATE_DELAY_S) # Write the excel record self._utilities.write_excel_record_to_dialin_record(self.dg_calibration_record) @@ -172,7 +174,7 @@ # should receive for packet in record_packets: # Sleep to let the firmware receive and process the data - time.sleep(self._PAYLOAD_TRANSFER_DELAY) + time.sleep(self._PAYLOAD_TRANSFER_DELAY_S) # Convert the list packet to a bytearray payload = b''.join(packet) Index: dialin/dg/dialysate_generator.py =================================================================== diff -u -rf9549f885d852f0b9607e9d198af5384cea7d6c7 -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/dg/dialysate_generator.py (.../dialysate_generator.py) (revision f9549f885d852f0b9607e9d198af5384cea7d6c7) +++ dialin/dg/dialysate_generator.py (.../dialysate_generator.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -31,10 +31,10 @@ from .fans import Fans from .uv_reactors import UVReactors from .concentrate_pumps import ConcentratePumps -from .calibration_record import DGCalibration -from .system_record import DGSystemRecord -from .service_record import DGServiceRecord -from .scheduled_runs_record import DGScheduledRunsRecord +from .calibration_record import DGCalibrationNVRecord +from .system_record import DGSystemNVRecord +from .service_record import DGServiceNVRecord +from .scheduled_runs_record import DGScheduledRunsNVRecord from ..utils.conversions import integer_to_bytearray from ..protocols.CAN import (DenaliCanMessenger, DenaliMessage, DenaliChannels) from ..utils.base import _AbstractSubSystem, _publish, _LogManager @@ -152,10 +152,10 @@ self.thermistors = Thermistors(self.can_interface, self.logger) self.fans = Fans(self.can_interface, self.logger) self.uv_reactors = UVReactors(self.can_interface, self.logger) - self.calibration_record = DGCalibration(self.can_interface, self.logger) - self.system_record = DGSystemRecord(self.can_interface, self.logger) - self.service_record = DGServiceRecord(self.can_interface, self.logger) - self.scheduled_runs_record = DGScheduledRunsRecord(self.can_interface, self.logger) + self.calibration_record = DGCalibrationNVRecord(self.can_interface, self.logger) + self.system_record = DGSystemNVRecord(self.can_interface, self.logger) + self.service_record = DGServiceNVRecord(self.can_interface, self.logger) + self.scheduled_runs_record = DGScheduledRunsNVRecord(self.can_interface, self.logger) def get_version(self): """ Index: dialin/dg/scheduled_runs_record.py =================================================================== diff -u -ra2cdbc7e007c6bd73e12344b4c89356a20b53f4f -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/dg/scheduled_runs_record.py (.../scheduled_runs_record.py) (revision a2cdbc7e007c6bd73e12344b4c89356a20b53f4f) +++ dialin/dg/scheduled_runs_record.py (.../scheduled_runs_record.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -4,14 +4,13 @@ import time import math from collections import OrderedDict -from ..ui.crc import crc_16 from ..common.msg_defs import MsgIds, MsgFieldPositions from ..protocols.CAN import (DenaliMessage, DenaliChannels) from ..utils.base import _AbstractSubSystem, DialinEnum from logging import Logger -class DGScheduledRunsRecord(_AbstractSubSystem): +class DGScheduledRunsNVRecord(_AbstractSubSystem): """ Dialysate Generator (DG) Dialin API sub-class for scheduled runs commands. @@ -125,7 +124,8 @@ self.total_messages = total self.length = length # The end of scheduled runs record payload is from the start index + 12 bytes for the current message + total - # messages + the length of scheduled runs record. The rest is the CAN messaging CRC and is not needed. + # messages + the received_msg_length of scheduled runs record. The rest is the CAN messaging CRC and is not + # needed. end_of_data_index = self.SCHEDULED_RUNS_RECORD_START_INDEX + self.SCHEDULED_RECORD_SPECS_BYTES + self.length # Get the scheduled runs data only @@ -171,9 +171,9 @@ data_type = self.DG_SCHEDULED_RUNS_RECORD[group][self.SCHEDULED_RUNS_DATA_TYPE_INDEX] # Check the data type of the padding values (they are unsigned chars) value_bytes = self._get_data_type_bytes(data_type) - # Get the length of the padding list + # Get the received_msg_length of the padding list padding_length = len(self.DG_SCHEDULED_RUNS_RECORD[group][self.SCHEDULED_RUNS_VALUE_INDEX]) - # Add the length of the padding bytes to the payload start address + # Add the received_msg_length of the padding bytes to the payload start address raw_payload_temp_start_index += padding_length * value_bytes # The rest of the groups are hardware which are layered dictionaries else: @@ -315,7 +315,7 @@ [self.SCHEDULED_RUNS_VALUE_INDEX])) scheduled_runs_record_packets.append(temp_buffer) - # If the group is padding, and the length of padding is not 0 + # If the group is padding, and the received_msg_length of padding is not 0 elif group == 'padding': if len(self.DG_SCHEDULED_RUNS_RECORD[group][self.SCHEDULED_RUNS_VALUE_INDEX]) != 0: # Get the data type and calculate the number of bytes @@ -366,10 +366,10 @@ # Append the temp buffer list into the main list to be process later scheduled_runs_record_packets.append(temp_buffer) # Increment the current message count as a new temp buffer is created for the next data - # packet. Update the current payload length with the latest addition to the new temp buffer. + # packet. Update the current payload received_msg_length with the latest addition to the new temp buffer. current_message_count += 1 current_payload_length = data_type_bytes - # Create a new temp buffer and add the current message count, total length of payload so far + # Create a new temp buffer and add the current message count, total received_msg_length of payload so far # and the data that there was not enough space in the previous data packet. temp_buffer = [struct.pack(' 0: - # Make the sure variables are 16-bit integers - left = (crc << 8) & 0x0000FFFF - right = (crc >> 8) & 0x0000FFFF - crc = left ^ crc_16_table[data[i] ^ (right & 0x00FF)] - l -= 1 - i += 1 - return crc Index: dialin/ui/messageBuilder.py =================================================================== diff -u -re5b68dd61c18dd97545d5e527a7f0a8f84061cb6 -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/ui/messageBuilder.py (.../messageBuilder.py) (revision e5b68dd61c18dd97545d5e527a7f0a8f84061cb6) +++ dialin/ui/messageBuilder.py (.../messageBuilder.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -54,7 +54,7 @@ def textToByte(vText, vLen): """ - converts the string vText to bytes by the length of vLen + converts the string vText to bytes by the received_msg_length of vLen :param vText: :param vLen: :return: converted text @@ -74,7 +74,7 @@ """ builds message from the parameter givven :param vMsgID: the message ID - :param vLen: length of the message payload in bytes + :param vLen: received_msg_length of the message payload in bytes :param vAck: if true the message requires acknowledge back :param vArgs: payload arguments. :return: Index: dialin/ui/utils.py =================================================================== diff -u -rdf4353089f674167fa92bebb1447d2278b9b30d9 -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/ui/utils.py (.../utils.py) (revision df4353089f674167fa92bebb1447d2278b9b30d9) +++ dialin/ui/utils.py (.../utils.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -116,7 +116,7 @@ and puts the sections from right to left if vRightDirection is True after the split is done :param vString: (str) the given string - :param vPart: (int) length of the section + :param vPart: (int) received_msg_length of the section :param vRightDirection: (bool) order of sections in the output list :return: (list) the section of the string vString in list """ @@ -125,9 +125,9 @@ def padding(vString, vLen): """ - added zero at the right side of the string to be of length of vLen + added zero at the right side of the string to be of received_msg_length of vLen :param vString: (str) the string to add trailing zero to - :param vLen: (int) the entire length of the string + :param vLen: (int) the entire received_msg_length of the string :return: (str) padded string """ lStr = len(vString) Index: dialin/utils/base.py =================================================================== diff -u -r8a4783c11ec826a2b84480ffb6b033edcac12954 -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/utils/base.py (.../base.py) (revision 8a4783c11ec826a2b84480ffb6b033edcac12954) +++ dialin/utils/base.py (.../base.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -152,7 +152,7 @@ @param is_virtual: (bool) If the interface is virtual or not @param bitrate: (int) The desired bitrate (applies to non-virtual interfaces only) @param restart_ms: (int) The desired restart_ms (applies to non-virtual interfaces only) - @param txqueue: (int) The desired txqueue length + @param txqueue: (int) The desired txqueue received_msg_length @return: """ cmd_iface_up = "sudo ip link set {0} up type can bitrate {1} restart-ms {2}"\ @@ -176,7 +176,7 @@ success, info = run_cmd(cmd_txqueue) if not success: - print("Warning: Could not set txtqueue length: {0}".format(cmd_txqueue)) + print("Warning: Could not set txtqueue received_msg_length: {0}".format(cmd_txqueue)) def create_logger(log_path: str = "/tmp/DialinScript.log", Index: dialin/utils/excel_ops.py =================================================================== diff -u -r7cd740bfb30cafa48847843cd09c3ee346b882a4 -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/utils/excel_ops.py (.../excel_ops.py) (revision 7cd740bfb30cafa48847843cd09c3ee346b882a4) +++ dialin/utils/excel_ops.py (.../excel_ops.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -87,7 +87,7 @@ @param merge: Merge cells. Cells must be provided with A1:A4 format (default none) @param horizontal: Horizontal alignment (default left) @param freeze: Freeze top row (default false) - @param max_col_len: maximum length of a column (default none, means it is not restricted) + @param max_col_len: maximum received_msg_length of a column (default none, means it is not restricted) @return: None """ @@ -101,27 +101,27 @@ # Set the provided data into the specified row and column and set the bold, color and horizontal alignment active_sheet.cell(row=row, column=column).value = data active_sheet.cell(row=row, column=column).font = Font(size=font, bold=bold, name=name) - # Wrapping text is not needed unless the length of the data is more than maximum column length + # Wrapping text is not needed unless the received_msg_length of the data is more than maximum column received_msg_length active_sheet[cell_name].alignment = Alignment(vertical='center', horizontal=horizontal, wrap_text=False) # Get the width of the current column column_width = active_sheet.column_dimensions[column_letter].width # When the column width is on the default, openpyxl reports None. If the width is reported as None, # it will be set to 0 for math comparison column_width = 0 if column_width is None else column_width - # If the length is not provided, use the default maximum length + # If the received_msg_length is not provided, use the default maximum received_msg_length max_len = self.COLUMN_MAX_WIDTH if max_col_len is None else max_col_len # Remove all the end of the line artifacts length_of_data = len(str(data).rstrip()) - # If the length of data was greater than the maximum length, calculate the number of rows is needed with the + # If the received_msg_length of data was greater than the maximum received_msg_length, calculate the number of rows is needed with the # default height if length_of_data > max_len: - # Since the length is greater than maximum, enable wrap text + # Since the received_msg_length is greater than maximum, enable wrap text active_sheet[cell_name].alignment = Alignment(vertical='center', horizontal=horizontal, wrap_text=True) # Calculate what the row height should be when the cell is extended row_height = math.ceil(length_of_data / max_len) * self.ROW_DEFAULT_HEIGHT active_sheet.column_dimensions[column_letter].width = max_len - # If the length of the data provided is already less than the length of cell, do nothing + # If the received_msg_length of the data provided is already less than the received_msg_length of cell, do nothing elif column_width < length_of_data: active_sheet.column_dimensions[column_letter].width = length_of_data + self.COLUMN_WIDTH_TOLERANCE row_height = self.ROW_DEFAULT_HEIGHT Index: dialin/utils/nv_ops_utils.py =================================================================== diff -u -rf9549f885d852f0b9607e9d198af5384cea7d6c7 -r551e0a9620126efd158e93c45e9ed84ee6ec85fb --- dialin/utils/nv_ops_utils.py (.../nv_ops_utils.py) (revision f9549f885d852f0b9607e9d198af5384cea7d6c7) +++ dialin/utils/nv_ops_utils.py (.../nv_ops_utils.py) (revision 551e0a9620126efd158e93c45e9ed84ee6ec85fb) @@ -5,7 +5,6 @@ import math import os import shutil -from ..ui.crc import crc_16 from .excel_ops import ExcelOps class NVOpsUtils: @@ -15,6 +14,41 @@ the calibration_record records, service records, system records, and the scheduled runs records. The records are prepared to be sent to firmware to be received from firmware. """ + CRC_16_TABLE = ( + 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50a5, 0x60c6, 0x70e7, + 0x8108, 0x9129, 0xa14a, 0xb16b, 0xc18c, 0xd1ad, 0xe1ce, 0xf1ef, + 0x1231, 0x0210, 0x3273, 0x2252, 0x52b5, 0x4294, 0x72f7, 0x62d6, + 0x9339, 0x8318, 0xb37b, 0xa35a, 0xd3bd, 0xc39c, 0xf3ff, 0xe3de, + 0x2462, 0x3443, 0x0420, 0x1401, 0x64e6, 0x74c7, 0x44a4, 0x5485, + 0xa56a, 0xb54b, 0x8528, 0x9509, 0xe5ee, 0xf5cf, 0xc5ac, 0xd58d, + 0x3653, 0x2672, 0x1611, 0x0630, 0x76d7, 0x66f6, 0x5695, 0x46b4, + 0xb75b, 0xa77a, 0x9719, 0x8738, 0xf7df, 0xe7fe, 0xd79d, 0xc7bc, + 0x48c4, 0x58e5, 0x6886, 0x78a7, 0x0840, 0x1861, 0x2802, 0x3823, + 0xc9cc, 0xd9ed, 0xe98e, 0xf9af, 0x8948, 0x9969, 0xa90a, 0xb92b, + 0x5af5, 0x4ad4, 0x7ab7, 0x6a96, 0x1a71, 0x0a50, 0x3a33, 0x2a12, + 0xdbfd, 0xcbdc, 0xfbbf, 0xeb9e, 0x9b79, 0x8b58, 0xbb3b, 0xab1a, + 0x6ca6, 0x7c87, 0x4ce4, 0x5cc5, 0x2c22, 0x3c03, 0x0c60, 0x1c41, + 0xedae, 0xfd8f, 0xcdec, 0xddcd, 0xad2a, 0xbd0b, 0x8d68, 0x9d49, + 0x7e97, 0x6eb6, 0x5ed5, 0x4ef4, 0x3e13, 0x2e32, 0x1e51, 0x0e70, + 0xff9f, 0xefbe, 0xdfdd, 0xcffc, 0xbf1b, 0xaf3a, 0x9f59, 0x8f78, + 0x9188, 0x81a9, 0xb1ca, 0xa1eb, 0xd10c, 0xc12d, 0xf14e, 0xe16f, + 0x1080, 0x00a1, 0x30c2, 0x20e3, 0x5004, 0x4025, 0x7046, 0x6067, + 0x83b9, 0x9398, 0xa3fb, 0xb3da, 0xc33d, 0xd31c, 0xe37f, 0xf35e, + 0x02b1, 0x1290, 0x22f3, 0x32d2, 0x4235, 0x5214, 0x6277, 0x7256, + 0xb5ea, 0xa5cb, 0x95a8, 0x8589, 0xf56e, 0xe54f, 0xd52c, 0xc50d, + 0x34e2, 0x24c3, 0x14a0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, + 0xa7db, 0xb7fa, 0x8799, 0x97b8, 0xe75f, 0xf77e, 0xc71d, 0xd73c, + 0x26d3, 0x36f2, 0x0691, 0x16b0, 0x6657, 0x7676, 0x4615, 0x5634, + 0xd94c, 0xc96d, 0xf90e, 0xe92f, 0x99c8, 0x89e9, 0xb98a, 0xa9ab, + 0x5844, 0x4865, 0x7806, 0x6827, 0x18c0, 0x08e1, 0x3882, 0x28a3, + 0xcb7d, 0xdb5c, 0xeb3f, 0xfb1e, 0x8bf9, 0x9bd8, 0xabbb, 0xbb9a, + 0x4a75, 0x5a54, 0x6a37, 0x7a16, 0x0af1, 0x1ad0, 0x2ab3, 0x3a92, + 0xfd2e, 0xed0f, 0xdd6c, 0xcd4d, 0xbdaa, 0xad8b, 0x9de8, 0x8dc9, + 0x7c26, 0x6c07, 0x5c64, 0x4c45, 0x3ca2, 0x2c83, 0x1ce0, 0x0cc1, + 0xef1f, 0xff3e, 0xcf5d, 0xdf7c, 0xaf9b, 0xbfba, 0x8fd9, 0x9ff8, + 0x6e17, 0x7e36, 0x4e55, 0x5e74, 0x2e93, 0x3eb2, 0x0ed1, 0x1ef0 + ) + _RECORD_START_INDEX = 6 _RECORD_SPECS_BYTES = 12 _RECORD_SPECS_BYTE_ARRAY = 3 @@ -185,7 +219,7 @@ if is_value_different and 'crc' in spec: # Convert the data in the temp buffer to bytes and calculate its crc. data = b''.join(temp_buffer) - crc_value = crc_16(data) + crc_value = self.crc_16(data) # Clear the temp buffer for the next round of data is_value_different = False dialin_record[group][hardware][spec][1] = crc_value @@ -299,9 +333,9 @@ data_type = dialin_record[group][self._DATA_TYPE_INDEX] # Check the data type of the padding values (they are unsigned chars) value_bytes = self.get_data_type_bytes(data_type) - # Get the length of the padding list + # Get the received_msg_length of the padding list padding_length = len(dialin_record[group][self._DATA_VALUE_INDEX]) - # Add the length of the padding bytes to the payload start address + # Add the received_msg_length of the padding bytes to the payload start address raw_payload_temp_start_index += padding_length * value_bytes # The rest of the groups are hardware which are layered dictionaries else: @@ -399,7 +433,7 @@ temp_buffer.append(struct.pack(data_type, dialin_record[group][self._DATA_VALUE_INDEX])) self._record_packets_to_send_to_fw.append(temp_buffer) - # If the group is padding, and the length of padding is not 0 + # If the group is padding, and the received_msg_length of padding is not 0 elif group == 'padding': if len(dialin_record[group][self._DATA_VALUE_INDEX]) != 0: # Get the data type and calculate the number of bytes @@ -453,15 +487,15 @@ # packet since it is the final CRC of the entire dictionary. The final CRC itself # is not included in the CRC calculations. # The actual calibration_record data is started from the 3rd element of the data packet. - # The first 3 are current message, total messages, and the payload length of the current packet. + # The first 3 are current message, total messages, and the payload received_msg_length of the current packet. if self._record_packets_to_send_to_fw.index(svc) == len(self._record_packets_to_send_to_fw) - 1: data += b''.join(svc[self._RECORD_SPECS_BYTE_ARRAY:-1]) else: # Get all the elements including the last one data += b''.join(svc[self._RECORD_SPECS_BYTE_ARRAY:]) # Calculate the 16-bit crc. - crc = crc_16(data) + crc = self.crc_16(data) # Convert the crc into a 2-byte value record_crc = struct.pack(' self._DICT_VALUE_LIST_LEN: byte_size += current_byte_size * group[key][self._CHAR_LENGTH_INDEX] else: byte_size += current_byte_size return byte_size + def crc_16(self, data): + """ + generates crc16 for the provided data + @param data: byte of data + @return: (int) the crc code + """ + crc = 0xFFFF + l = len(data) + i = 0 + + while l > 0: + # Make the sure variables are 16-bit integers + left = (crc << 8) & 0x0000FFFF + right = (crc >> 8) & 0x0000FFFF + crc = left ^ self.CRC_16_TABLE[data[i] ^ (right & 0x00FF)] + l -= 1 + i += 1 + + return crc + @staticmethod def get_data_type_bytes(data): """ @@ -627,7 +681,7 @@ @staticmethod def calculate_padding_byte_size(total_byte_size, max_buffer_size): """ - Handles calculating the padding length based on the provided buffer sizes. This is a static method. + Handles calculating the padding received_msg_length based on the provided buffer sizes. This is a static method. @param total_byte_size: total byte size of a record dictionary @param max_buffer_size: max buffer size that is allowed to be used in the dictionary