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