########################################################################### # # Copyright (c) 2019-2020 Diality Inc. - All Rights Reserved. # # THIS CODE MAY NOT BE COPIED OR REPRODUCED IN ANY FORM, IN PART OR IN # WHOLE, WITHOUT THE EXPLICIT PERMISSION OF THE COPYRIGHT OWNER. # # @file messageBuilder.py # # @author (last) Peter Lucia # @date (last) 21-Oct-2020 # @author (original) Peter Lucia # @date (original) 09-Jul-2020 # ############################################################################ from dialin.ui import utils, crc syncByte = 'A5' def toCandumpFormat(vMsg): """ the converter method which converts the message vMsg to the string that candump tool understands. :param vMsg: the message :return: converted string to candump tool format """ if type(vMsg) == list: for index, value in enumerate(vMsg): vMsg[index] = ".".join(utils.partition(value, 2, False)) else: vMsg = ".".join(utils.partition(vMsg, 2, False)) return vMsg def toFrames(vMsg): """ converts the message vMsg to frames :param vMsg: adds the crc8 checksum at the end of the message vMsg :return: the frames """ mLen = 16 padded = utils.padding(vMsg, mLen) frames = utils.partition(padded, mLen, False) return frames def addCRC8(vString, vDelimiter = ""): """ adds the crc8 checksum at the end of the string vString :param vString: the string to be used :param vDelimiter: the string delimiter :return: the string with crc8 """ return vString + vDelimiter + crc.calcCRC8(vString, vDelimiter) def textToByte(vText, vLen): """ converts the string vText to bytes by the length of vLen :param vText: :param vLen: :return: converted text """ text = "" l = len(vText) for i in range(vLen): if i < l: text += utils.toI08(ord(vText[i])) else: text += utils.toI08(0) text += utils.toI08(0) #null /0 return text def buildMessage(vMsgID, vLen, vAck, *vArgs): """ builds message from the parameter givven :param vMsgID: the message ID :param vLen: length of the message payload in bytes :param vAck: if true the message requires acknowledge back :param vArgs: payload arguments. :return: """ msg = "" if vAck: seq = -1 else: seq = 1 msg += utils.toI16(seq) # always used seq# (-)1 (for now) msg += utils.toI16(vMsgID) msg += utils.toI08(vLen) for arg in vArgs: msg += arg msg += crc.calcCRC8(msg) return syncByte + msg