1*1b191cb5SApple OSS Distributionsfrom __future__ import absolute_import 2*1b191cb5SApple OSS Distributions 3*1b191cb5SApple OSS Distributionsfrom builtins import object 4*1b191cb5SApple OSS Distributions 5*1b191cb5SApple OSS Distributionsimport logging 6*1b191cb5SApple OSS Distributions 7*1b191cb5SApple OSS Distributions 8*1b191cb5SApple OSS Distributionsclass Message(object): 9*1b191cb5SApple OSS Distributions """represents a message of Remote serial protocol""" 10*1b191cb5SApple OSS Distributions def __init__(self, data): 11*1b191cb5SApple OSS Distributions super(Message, self).__init__() 12*1b191cb5SApple OSS Distributions self.data = data 13*1b191cb5SApple OSS Distributions 14*1b191cb5SApple OSS Distributions def __str__(self): 15*1b191cb5SApple OSS Distributions return "Message: %s" % (self.data) 16*1b191cb5SApple OSS Distributions 17*1b191cb5SApple OSS Distributions def getData(self): 18*1b191cb5SApple OSS Distributions #TODO need to parse data and unescape 19*1b191cb5SApple OSS Distributions return self.data 20*1b191cb5SApple OSS Distributions 21*1b191cb5SApple OSS Distributions def getRSPByteData(self): 22*1b191cb5SApple OSS Distributions retval = ''.join(['$',self.data,'#']) 23*1b191cb5SApple OSS Distributions checksum = 0 24*1b191cb5SApple OSS Distributions for i in self.data: 25*1b191cb5SApple OSS Distributions checksum += ord(i) 26*1b191cb5SApple OSS Distributions checksum = checksum % 0x100 27*1b191cb5SApple OSS Distributions checksum_str = "{:02x}".format(checksum) 28*1b191cb5SApple OSS Distributions retval += checksum_str 29*1b191cb5SApple OSS Distributions return retval 30*1b191cb5SApple OSS Distributions 31*1b191cb5SApple OSS Distributions @classmethod 32*1b191cb5SApple OSS Distributions def fromRSPByteData(cls, bytedata): 33*1b191cb5SApple OSS Distributions data_begin = 0 34*1b191cb5SApple OSS Distributions data_end = 0 35*1b191cb5SApple OSS Distributions try: 36*1b191cb5SApple OSS Distributions data_begin = bytedata.index('$') 37*1b191cb5SApple OSS Distributions data_end = bytedata.index('#') 38*1b191cb5SApple OSS Distributions except ValueError as e: 39*1b191cb5SApple OSS Distributions logging.error('Invalid bytedata considered as message %s' % bytedata) 40*1b191cb5SApple OSS Distributions return None 41*1b191cb5SApple OSS Distributions 42*1b191cb5SApple OSS Distributions #validate the data 43*1b191cb5SApple OSS Distributions if data_begin + 1 >= data_end: 44*1b191cb5SApple OSS Distributions logging.debug("empty message %s"%bytedata) 45*1b191cb5SApple OSS Distributions data_begin -= 1 46*1b191cb5SApple OSS Distributions 47*1b191cb5SApple OSS Distributions data_begin += 1 48*1b191cb5SApple OSS Distributions logging.debug("Creating message from data %s" % bytedata[data_begin:data_end]) 49*1b191cb5SApple OSS Distributions ret_obj = cls(bytedata[data_begin:data_end]) 50*1b191cb5SApple OSS Distributions return ret_obj 51*1b191cb5SApple OSS Distributions 52*1b191cb5SApple OSS Distributionsclass ProtocolAcknowledgement(Message): 53*1b191cb5SApple OSS Distributions """Ack Messages""" 54*1b191cb5SApple OSS Distributions def __init__(self, ack_str): 55*1b191cb5SApple OSS Distributions super(ProtocolAcknowledgement, self).__init__(ack_str) 56*1b191cb5SApple OSS Distributions self.data = ack_str 57*1b191cb5SApple OSS Distributions 58*1b191cb5SApple OSS Distributions def getRSPByteData(self): 59*1b191cb5SApple OSS Distributions return self.data 60*1b191cb5SApple OSS Distributions 61*1b191cb5SApple OSS Distributions 62*1b191cb5SApple OSS DistributionsOKMessage = Message('OK') 63*1b191cb5SApple OSS Distributions 64*1b191cb5SApple OSS DistributionsAckMessage = ProtocolAcknowledgement('+') 65*1b191cb5SApple OSS DistributionsNAckMessage = ProtocolAcknowledgement('-') 66*1b191cb5SApple OSS DistributionsUnSupportedMessage = Message('') 67