comparison zb.py @ 4:ed7abe6f59c2

Many fixes (again) - Worked out how to auto-discover packet type classes. - Refactor receive class to make it usable for 16 & 64 bit modes. - Use property() to map to data values. - Add another test case, document what the others look like.
author darius@inchoate.localdomain
date Wed, 31 Oct 2007 20:01:54 +1030
parents 7bf4d4265339
children 5d5963d542bc
comparison
equal deleted inserted replaced
3:7bf4d4265339 4:ed7abe6f59c2
1 import serial 1 import serial, inspect
2 2
3 class PktBase(object): 3 class PktBase(object):
4 def __init__(self, data): 4 def __init__(self, data = []):
5 self.data = data 5 self.data = data
6 print "Constructing " + self.__class__.__name__ 6 print "Constructing " + self.__class__.__name__
7 7
8 def __repr__(self): 8 def __repr__(self):
9 return str(self.__class__) + "(" + str([self.PKT_TYPE] + self.data) + ")" 9 return str(self.__class__) + "(" + str([self.PKT_TYPE] + self.data) + ")"
10 10
11 #def __str__(self): 11 def Encapsulate(self):
12 # return "%s: %s" % (self.PKT_DESC, str(self.data)) 12 return Packets.Encapsulate([self.PKT_TYPE] + self.data)
13 13
14 def resize(self, dlen):
15 """Ensure the data list can hold at least len elements (0 fill)"""
16 if (len(self.data) < dlen):
17 self.data = (self.data + [0] * dlen)[0:dlen]
18
14 class AT_Cmd(PktBase): 19 class AT_Cmd(PktBase):
15 PKT_TYPE = 0x08 20 PKT_TYPE = 0x08
16 PKT_DESC = "AT Command" 21 PKT_DESC = "AT Command"
17 22
18 class AT_Cmd_Queue(PktBase): 23 def __init__(self, *args):
24 if (len(args) == 1):
25 super(AT_Cmd, self).__init__(args[0])
26 elif (len(args) == 2):
27 super(AT_Cmd, self).__init__([])
28 self.cmd = args[0]
29 if (args[1] != None):
30 self.cmdarg = args[1]
31 else:
32 raise TypeError("__init__ takes 1 list of ordinals or 2 strings")
33
34 def setcmd(self, value):
35 self.resize(2)
36 self.data[0] = ord(value[0])
37 self.data[1] = ord(value[1])
38 cmd = property(lambda s: chr(s.data[0]) + chr(s.data[1]), setcmd)
39
40 def setcmdarg(self, value):
41 self.resize(2 + len(value))
42 self.data = self.data[0:2] + map(ord, value)
43 def delcmdarg(self):
44 self.data = self.data[0:2]
45 cmdarg = property(lambda s: map(chr, s.data[2:]), setcmdarg, delcmdarg)
46
47 class AT_Cmd_Queue(AT_Cmd):
19 PKT_TYPE = 0x09 48 PKT_TYPE = 0x09
20 PKT_DESC = "AT Command (queued)" 49 PKT_DESC = "AT Command (queued)"
21 50
22 class AT_Response(PktBase): 51 class AT_Response(PktBase):
23 PKT_TYPE = 0x88 52 PKT_TYPE = 0x88
25 54
26 class Modem_Status(PktBase): 55 class Modem_Status(PktBase):
27 PKT_TYPE = 0x8a 56 PKT_TYPE = 0x8a
28 PKT_DESC = "Modem Status" 57 PKT_DESC = "Modem Status"
29 58
30 class RX_64_Bit(PktBase):
31 PKT_TYPE = 0x80
32 PKT_DESC = "RX Packet: 64 bit address"
33
34 class RX_16_Bit(PktBase): 59 class RX_16_Bit(PktBase):
35 PKT_TYPE = 0x81 60 PKT_TYPE = 0x81
36 PKT_DESC = "RX Packet: 16 bit address" 61 PKT_DESC = "RX Packet: 16 bit address"
37 62 ADDR_SIZE = 2
38 class RXIO_64_Bit(PktBase): 63
39 PKT_TYPE = 0x82 64 def setsender(self, value):
40 PKT_DESC = "RXIO Packet: 64 bit address" 65 self.resize(self.ADDR_SIZE)
41 66 for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
42 class RXIO_16_Bit(PktBase): 67 self.data[i] = (value & (0xff << j)) >> j
68
69 def getsender(self):
70 value = 0
71 for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
72 value |= self.data[i] << j
73 return value
74 sender = property(getsender, setsender)
75
76 def setrssi(self, value):
77 self.resize(self.ADDR_SIZE + 1)
78 self.data[self.ADDR_SIZE] = -1 * value
79 rssi = property(lambda s: -1 * s.data[s.ADDR_SIZE], setrssi)
80
81 def setflags(self, value):
82 self.resize(self.ADDR_SIZE + 2)
83 self.data[self.ADDR_SIZE + 1] = value
84 flags = property(lambda s: s.data[s.ADDR_SIZE + 1], setflags)
85
86 class RX_64_Bit(RX_16_Bit):
87 PKT_TYPE = 0x80
88 PKT_DESC = "RX Packet: 64 bit address"
89 ADDR_SIZE = 8
90
91 class RXIO_16_Bit(RX_16_Bit):
43 PKT_TYPE = 0x83 92 PKT_TYPE = 0x83
44 PKT_DESC = "RXIO Packet: 16 bit address" 93 PKT_DESC = "RXIO Packet: 16 bit address"
45 94
95 def setnsamples(self, value):
96 self.resize(self.ADDR_SIZE + 3)
97 self.data[self.ADDR_SIZE + 2] = value
98 nsamples = property(lambda s: s.data[s.ADDR_SIZE + 2], setnsamples)
99
100 def setmask(self, value):
101 self.resize(self.ADDR_SIZE + 5)
102 self.data[self.ADDR_SIZE + 3] = (value & 0xff00) >> 8
103 self.data[self.ADDR_SIZE + 4] = value & 0xff
104 mask = property(lambda s: s.data[s.ADDR_SIZE + 3] << 8 | s.data[s.ADDR_SIZE + 4], setmask)
105
46 def __str__(self): 106 def __str__(self):
47 sender = self.data[0] << 8 | self.data[1] 107 rtn = "0x%0*x (%ddBm) -> %d samples, mask 0x%04x" % (self.ADDR_SIZE * 2, self.sender,
48 rssi = -1 * self.data[2] 108 self.rssi, self.nsamples, self.mask)
49 flags = self.data[3]
50 nsamples = self.data[4]
51 mask = self.data[5] << 8 | self.data[6]
52 rtn = "0x%04x (%ddBm) -> %d samples, mask 0x%04x" % (sender, rssi, nsamples, mask)
53 # Any DIO lines enabled? 109 # Any DIO lines enabled?
54 if (mask | 0x01ff): 110 if (self.mask | 0x01ff):
55 rtn = rtn + ", DIO - 0x%03x" % (self.data[7] << 8 | self.data[8]) 111 rtn = rtn + ", DIO - 0x%03x" % (self.data[self.ADDR_SIZE + 5] << 8 |
56 offs = 9 112 self.data[self.ADDR_SIZE + 6])
113 offs = self.ADDR_SIZE + 7
57 else: 114 else:
58 offs = 7 115 offs = self.ADDR_SIZE + 5
59 116
60 # Any ADC lines enabled? 117 # Any ADC lines enabled?
61 if (mask | 0x7e00): 118 if (self.mask | 0x7e00):
62 for i in range(6): 119 for i in range(6):
63 if (mask & 1 << (i + 9)): 120 if (self.mask & 1 << (i + 9)):
64 rtn = rtn + ", ADC%d - 0x%02x" % (i, self.data[offs] << 8 | 121 rtn = rtn + ", ADC%d - 0x%02x" % (i, self.data[offs] << 8 |
65 self.data[offs + 1]) 122 self.data[offs + 1])
66 offs = offs + 2 123 offs = offs + 2
67 124
68 return rtn 125 return rtn
69 126
127 class RXIO_64_Bit(RX_16_Bit):
128 PKT_TYPE = 0x82
129 PKT_DESC = "RXIO Packet: 64 bit address"
130 ADDR_SIZE = 8
131
132 class TX_16_Bit(PktBase):
133 PKT_TYPE = 0x00
134 PKT_DESC = "TX Packet: 16 bit address"
135
70 class TX_64_Bit(PktBase): 136 class TX_64_Bit(PktBase):
71 PKT_TYPE = 0x00 137 PKT_TYPE = 0x00
72 PKT_DESC = "TX Packet: 64 bit address" 138 PKT_DESC = "TX Packet: 64 bit address"
73 139
74 class TX_16_Bit(PktBase):
75 PKT_TYPE = 0x00
76 PKT_DESC = "TX Packet: 16 bit address"
77
78 class TX_Status(PktBase): 140 class TX_Status(PktBase):
79 PKT_TYPE = 0x89 141 PKT_TYPE = 0x89
80 PKT_DESC = "TX Status" 142 PKT_DESC = "TX Status"
81 143
82 class Packets(object): 144 class Packets(object):
83 PKT_CLASSES = [AT_Cmd, AT_Cmd_Queue, AT_Response, Modem_Status, RX_64_Bit, 145 PKT_CLASSES = None
84 RX_16_Bit, RXIO_64_Bit, RXIO_16_Bit, TX_64_Bit, TX_16_Bit, 146
85 TX_Status] 147 def Build(self, data):
86 148 if (self.PKT_CLASSES == None):
87 def Build(data): 149 m = inspect.getmodule(self)
88 for p in Packets.PKT_CLASSES: 150 # Generate list of objects from their names
151 mobjs = map(lambda n: m.__dict__[n], m.__dict__)
152 # Find all the classes
153 pktclasses = filter(inspect.isclass, mobjs)
154 # Find all subclasses of PktBase (but not PktBase itself)
155 pktclasses = filter(lambda s: issubclass(s, m.PktBase) and s != m.PktBase, pktclasses)
156 self.PKT_CLASSES = pktclasses
157
158 for p in self.PKT_CLASSES:
89 if (p.PKT_TYPE == data[0]): 159 if (p.PKT_TYPE == data[0]):
90 return(p(data[1:])) 160 return(p(data[1:]))
91 161
92 Build = staticmethod(Build) 162 raise ValueError("Unknown packet type 0x%02x" % (data[0]))
163 Build = classmethod(Build)
93 164
94 def Encapsulate(data): 165 def Encapsulate(data):
95 pktsum = reduce(lambda x, y: x + y, data) & 0xff 166 pktsum = reduce(lambda x, y: x + y, data) & 0xff
96 pkt = [0x7e] + [len(data) >> 8] + [len(data) & 0xff] + data + [0xff - pktsum] 167 pkt = [0x7e] + [len(data) >> 8] + [len(data) & 0xff] + data + [0xff - pktsum]
97 return(map(chr, pkt)) 168 return(map(chr, pkt))
98 169
99 Encapsulate = staticmethod(Encapsulate) 170 Encapsulate = staticmethod(Encapsulate)
100 171
101 def __init__(self): 172 def __init__(self):
173 print str(inspect.getmodule(self))
102 self.buffer = [] 174 self.buffer = []
103 self.state = 'init' 175 self.state = 'init'
104 self.packets = [] 176 self.packets = []
105 177
106 self.bufmsb = 0 178 self.bufmsb = 0
123 return self.process(l) 195 return self.process(l)
124 196
125 def process(self, data): 197 def process(self, data):
126 pktcount = 0 198 pktcount = 0
127 for d in data: 199 for d in data:
128 dord = ord(d)
129 if (self.state == 'init'): 200 if (self.state == 'init'):
130 if (d != '\x7e'): 201 if (d != '\x7e'):
131 print "Framing error, got 0x%02x, expected 0x7f" % (dord) 202 print "Framing error, got 0x%02x, expected 0x7f" % (ord(d))
132 self.fr_err = self.fr_err + 1 203 self.fr_err += 1
133 continue 204 continue
134 205
135 self.state = 'sizemsb' 206 self.state = 'sizemsb'
136 elif (self.state == 'sizemsb'): 207 elif (self.state == 'sizemsb'):
137 self.bufmsb = dord 208 self.bufmsb = ord(d)
138 self.state = 'sizelsb' 209 self.state = 'sizelsb'
139 elif (self.state == 'sizelsb'): 210 elif (self.state == 'sizelsb'):
140 self.dataleft = self.bufmsb << 8 | \ 211 self.dataleft = self.bufmsb << 8 | \
141 dord 212 ord(d)
142 self.state = 'data' 213 self.state = 'data'
143 elif (self.state == 'data'): 214 elif (self.state == 'data'):
144 self.buffer.append(dord) 215 self.buffer.append(ord(d))
145 self.dataleft = self.dataleft - 1 216 self.dataleft = self.dataleft - 1
146 if (self.dataleft == 0): 217 if (self.dataleft == 0):
147 self.state = 'cksum' 218 self.state = 'cksum'
148 elif (self.state == 'cksum'): 219 elif (self.state == 'cksum'):
149 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff 220 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff
150 rxcksum = dord 221 rxcksum = ord(d)
151 self.state = 'init' 222 self.state = 'init'
152 if (pktsum + rxcksum != 0xff): 223 if (pktsum + rxcksum != 0xff):
153 self.buffer = [] 224 self.buffer = []
154 self.ck_err = self.ck_err + 1 225 self.ck_err += 1
155 print "Checksum error, got 0x%02x, expected 0x%02x" % \ 226 print "Checksum error, got 0x%02x, expected 0x%02x" % \
156 (rxcksum, 0xff - pktsum) 227 (rxcksum, 0xff - pktsum)
157 else: 228 else:
158 print "Got a packet - " + str(self.buffer) 229 print "Got a packet - " + str(self.buffer)
159 p = Packets.Build(self.buffer) 230 p = Packets.Build(self.buffer)
160 self.pktq.append(p) 231 self.pktq.append(p)
161 self.buffer = [] 232 self.buffer = []
162 pktcount = pktcount + 1 233 pktcount += 1
163 self.rx_cnt = self.rx_cnt + 1 234 self.rx_cnt += 1
164 else: 235 else:
165 print "Invalid state %s! Resetting" % (self.state) 236 print "Invalid state %s! Resetting" % (self.state)
166 self.state = 'init' 237 self.state = 'init'
167 238
168 return pktcount 239 return pktcount
175 stopbits=1, rtscts=0) 246 stopbits=1, rtscts=0)
176 s.setTimeout(0.1) 247 s.setTimeout(0.1)
177 #s.write('+++') 248 #s.write('+++')
178 #s.readline(eol='\r') 249 #s.readline(eol='\r')
179 250
251
252 # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f
180 goodtest = ['~', '\x00', '\n', '\x83', '\x00', '\x01', '$', '\x00', '\x01', '\x00', '\x0f', '\x00', '\x0f', '8'] 253 goodtest = ['~', '\x00', '\n', '\x83', '\x00', '\x01', '$', '\x00', '\x01', '\x00', '\x0f', '\x00', '\x0f', '8']
254
255 # Checksum error
181 badtest = ['~', '\x00', '\n', '\x83', '\x00', '\x01', '$', '\x00', '\x01', '\x00', '\x0f', '\x00', '\x0e', '8'] 256 badtest = ['~', '\x00', '\n', '\x83', '\x00', '\x01', '$', '\x00', '\x01', '\x00', '\x0f', '\x00', '\x0e', '8']
257
258 #0x0005 (-36dBm) -> 1 samples, mask 0x020e, DIO - 0x00e, ADC0 - 0x3ff
182 adctest = ['~', '\x00', '\x0c', '\x83', '\x00', '\x05', '$', '\x00', '\x01', '\x02', '\x0e', '\x00', '\x0e', '\x03', '\xff', '2' ] 259 adctest = ['~', '\x00', '\x0c', '\x83', '\x00', '\x05', '$', '\x00', '\x01', '\x02', '\x0e', '\x00', '\x0e', '\x03', '\xff', '2' ]
260
261 # Exception
262 badpkttypetest = ['~', '\x00', '\x03', '\x0a', 'V', 'L', 'S']
263
183 up = Packets() 264 up = Packets()
184 up.process(goodtest) 265 up.process(goodtest)
185 up.process(badtest) 266 up.process(badtest)
186 up.process(adctest) 267 up.process(adctest)
187 print up.pktq.pop(0) 268 print up.pktq.pop(0)