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