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