annotate zb.py @ 10:4c91fdfc862e

- Further changes to make things cleaner (ie RX packets are "read only" etc). - Add helper routine to poll the serial port and print any packets seen. - Make opening the serial port optional.
author darius@inchoate.localdomain
date Wed, 07 Nov 2007 17:10:08 +1030
parents d147529ad2db
children 75f785a09e2e
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
1 import serial, inspect, time
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
2
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
3 def easyord(i):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
4 if (type(i) != type(str())):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
5 return i
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
6 else:
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
7 return ord(i)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
8
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
9 class PktBase(object):
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
10 PKT_MAXLEN = 2 ** 16
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
11
10
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
12 def __init__(self):
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
13 print "Constructing " + self.__class__.__name__
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
14
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
15 def Encapsulate(self):
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
16 return Packets.Encapsulate([self.PKT_TYPE] + self.data)
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
17
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
18 def resize(self, dlen):
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
19 """Ensure the data list can hold at least len elements (0 fill)"""
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
20 if (len(self._data) < dlen):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
21 self._data = (self._data + [0] * dlen)[0:dlen]
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
22
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
23 def _checklist(list, min = 0, max = 255, maxlen = None):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
24 if (maxlen != None and len(list) > maxlen):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
25 raise ValueError("must have %d elements" % (maxlen))
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
26
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
27 for i in xrange(len(list)):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
28 if (easyord(list[i]) < min or easyord(list[i]) > max):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
29 raise ValueError("element %d (= %d) out of range must be between %d and %d inclusive" %
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
30 (i, ord(list[i]), min, max))
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
31 _checklist = staticmethod(_checklist)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
32
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
33 class TXPkts(PktBase):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
34 """Base class for all packets that go to the module"""
10
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
35
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
36 def setframeid(self, value):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
37 if (value < 0 or value > 255):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
38 raise ValueError("FrameID must be 0-255")
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
39 self._frameid = value
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
40 frameid = property(lambda s: s._frameid, setframeid)
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
41
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
42
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
43 class AT_Cmd(TXPkts):
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
44 PKT_TYPE = 0x08
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
45 PKT_DESC = "AT Command"
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
46
10
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
47 def __init__(self, cmd = None, cmdarg = None):
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
48 super(AT_Cmd, self).__init__()
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
49 if (cmd != None):
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
50 self.cmd = cmd
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
51 if (cmdarg != None):
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
52 self.cmdarg = cmdarg
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
53
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
54 self.frameid = 0
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
55 self.cmdarg = []
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
56
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
57 def setcmd(self, value):
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
58 if (len(value) != 2):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
59 raise ValueError("must have 2 elements")
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
60 self._checklist(value, ord('0'), ord('z'))
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
61 self._cmd = value
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
62 cmd = property(lambda s: s._cmd, setcmd)
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
63
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
64 def setcmdarg(self, value):
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
65 self._checklist(value, maxlen = self.PKT_MAXLEN - 3)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
66 self._cmdarg = value
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
67 cmdarg = property(lambda s: s._cmdarg, setcmdarg)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
68
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
69 def getdata(self):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
70 return([self.frameid] + map(ord, self.cmd) + map(easyord, self.cmdarg))
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
71 data = property(getdata)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
72
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
73 class AT_Cmd_Queue(AT_Cmd):
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
74 PKT_TYPE = 0x09
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
75 PKT_DESC = "AT Command (queued)"
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
76
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
77 class AT_Response(PktBase):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
78 PKT_TYPE = 0x88
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
79 PKT_DESC = "AT Command response"
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
80 frameid = property(lambda s: s._data[0], None)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
81 cmd = property(lambda s: chr(s._data[1]) + chr(s._data[2]), None)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
82 statusOK = property(lambda s: s._data[3] == 0, None)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
83 payload = property(lambda s: s._data[4:], None)
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
84
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
85 class Modem_Status(PktBase):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
86 PKT_TYPE = 0x8a
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
87 PKT_DESC = "Modem Status"
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
88
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
89 class RX_16_Bit(PktBase):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
90 PKT_TYPE = 0x81
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
91 PKT_DESC = "RX Packet: 16 bit address"
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
92 ADDR_SIZE = 2
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
93
10
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
94 def __init__(self, data = []):
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
95 super(RX_16_Bit, self).__init__()
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
96 self._data = data
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
97
8
9f0808b13454 Use non-blocking serial access. Add __str__ method for RX packets.
darius@inchoate.localdomain
parents: 7
diff changeset
98 def __str__(self):
9f0808b13454 Use non-blocking serial access. Add __str__ method for RX packets.
darius@inchoate.localdomain
parents: 7
diff changeset
99 return "0x%0*x (%ddBm) -> %s" % (self.ADDR_SIZE * 2, self.sender,
9f0808b13454 Use non-blocking serial access. Add __str__ method for RX packets.
darius@inchoate.localdomain
parents: 7
diff changeset
100 self.rssi, str(self.payload))
9f0808b13454 Use non-blocking serial access. Add __str__ method for RX packets.
darius@inchoate.localdomain
parents: 7
diff changeset
101
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
102 def getsender(self):
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
103 value = 0
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
104 for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
105 value |= self._data[i] << j
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
106 return value
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
107 sender = property(getsender, None)
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
108
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
109 rssi = property(lambda s: -1 * s._data[s.ADDR_SIZE], None)
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
110
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
111 flags = property(lambda s: s._data[s.ADDR_SIZE + 1], None)
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
112
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
113 payload = property(lambda s: s._data[s.ADDR_SIZE + 2:], None)
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
114
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
115 class RX_64_Bit(RX_16_Bit):
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
116 PKT_TYPE = 0x80
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
117 PKT_DESC = "RX Packet: 64 bit address"
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
118 ADDR_SIZE = 8
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
119
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
120 class RXIO_16_Bit(RX_16_Bit):
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
121 PKT_TYPE = 0x83
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
122 PKT_DESC = "RXIO Packet: 16 bit address"
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
123
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
124 nsamples = property(lambda s: s._data[s.ADDR_SIZE + 2])
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
125
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
126 mask = property(lambda s: s._data[s.ADDR_SIZE + 3] << 8 | s._data[s.ADDR_SIZE + 4])
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
127
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
128 def __str__(self):
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
129 rtn = "0x%0*x (%ddBm) -> %d samples, mask 0x%04x" % (self.ADDR_SIZE * 2, self.sender,
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
130 self.rssi, self.nsamples, self.mask)
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
131 # Any DIO lines enabled?
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
132 if (self.mask | 0x01ff):
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
133 rtn = rtn + ", DIO - 0x%03x" % (self._data[self.ADDR_SIZE + 5] << 8 |
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
134 self._data[self.ADDR_SIZE + 6])
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
135 offs = self.ADDR_SIZE + 7
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
136 else:
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
137 offs = self.ADDR_SIZE + 5
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
138
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
139 # Any ADC lines enabled?
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
140 if (self.mask | 0x7e00):
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
141 for i in range(6):
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
142 if (self.mask & 1 << (i + 9)):
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
143 rtn = rtn + ", ADC%d - 0x%02x" % (i, self._data[offs] << 8 |
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
144 self._data[offs + 1])
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
145 offs = offs + 2
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
146 return rtn
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
147
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
148 class RXIO_64_Bit(RXIO_16_Bit):
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
149 PKT_TYPE = 0x82
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
150 PKT_DESC = "RXIO Packet: 64 bit address"
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
151 ADDR_SIZE = 8
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
152
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
153 class TX_16_Bit(TXPkts):
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
154 PKT_TYPE = 0x01
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
155 PKT_DESC = "TX Packet: 16 bit address"
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
156 ADDR_SIZE = 2
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
157 FLG_DISABLE_ACK = 0x01
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
158 FLG_BCAST_PANID = 0x04
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
159 PKT_MAX_PAYLOAD = 100
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
160
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
161 def __init__(self, *args):
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
162 if (len(args) == 1):
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
163 super(TX_16_Bit, self).__init__()
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
164 self.recipient = args[0]
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
165 elif (len(args) == 2):
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
166 super(TX_16_Bit, self).__init__([])
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
167 self.recipient = args[0]
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
168 self.payload = args[1]
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
169 else:
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
170 raise TypeError("__init__ takes 1 list of ordinals or 2 strings")
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
171
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
172 self.frameid = 0
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
173 self.flags = 0
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
174 self.payload = []
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
175
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
176 def setrecipient(self, value):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
177 if (value < 0 or value > 2 ** (self.ADDR_SIZE * 8)):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
178 raise ValueError("value out of range must be between 0 and %d" % (2 ** self.ADDR_SIZE))
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
179
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
180 self._recipient = value
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
181 recipient = property(lambda s: s._recipient, setrecipient)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
182
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
183 def setflags(self, value):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
184 if (value < 0 or value > 255):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
185 raise ValueError("Value must be between 0 and 255 inclusive")
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
186
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
187 self._flags = value
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
188 flags = property(lambda s: s._flags, setflags)
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
189
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
190 def setpayload(self, value):
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
191 self._checklist(value, maxlen = self.PKT_MAX_PAYLOAD)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
192 self._payload = value
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
193 payload = property(lambda s: s._payload, setpayload)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
194
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
195 def getdata(self):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
196 data = [self.frameid]
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
197 for i, j in zip(reversed(range(self.ADDR_SIZE)), range(0, self.ADDR_SIZE * 8, 8)):
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
198 data.append((self.recipient & (0xff << j)) >> j)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
199 data.append(self.flags)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
200 data.extend(map(easyord, self.payload))
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
201 return(data)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
202 data = property(getdata)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
203
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
204 class TX_64_Bit(TX_16_Bit):
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
205 PKT_TYPE = 0x00
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
206 PKT_DESC = "TX Packet: 64 bit address"
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
207 ADDR_SIZE = 8
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
208
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
209 class TX_Status(PktBase):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
210 PKT_TYPE = 0x89
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
211 PKT_DESC = "TX Status"
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
212 statusTxt = ['OK', 'No Ack', 'CCA failure', 'Purged']
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
213 frameid = property(lambda s: s._data[0], None)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
214 status = property(lambda s: s._data[1], None)
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
215 statusMsg = property(lambda s: s.statusTxt[s._data[1]], None)
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
216
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
217 class Packets(object):
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
218 PKT_CLASSES = None
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
219
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
220 def Build(self, data):
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
221 if (self.PKT_CLASSES == None):
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
222 m = inspect.getmodule(self)
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
223 # Generate list of objects from their names
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
224 mobjs = map(lambda n: m.__dict__[n], m.__dict__)
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
225 # Find all the classes
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
226 pktclasses = filter(inspect.isclass, mobjs)
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
227 # Find all subclasses of PktBase (but not PktBase itself)
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
228 pktclasses = filter(lambda s: issubclass(s, m.PktBase) and s != m.PktBase, pktclasses)
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
229 self.PKT_CLASSES = pktclasses
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
230
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
231 for p in self.PKT_CLASSES:
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
232 if (p.PKT_TYPE == data[0]):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
233 return(p(data[1:]))
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
234
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
235 raise ValueError("Unknown packet type 0x%02x" % (data[0]))
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
236 Build = classmethod(Build)
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
237
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
238 def Encapsulate(data):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
239 pktsum = reduce(lambda x, y: x + y, data) & 0xff
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
240 pkt = [0x7e] + [len(data) >> 8] + [len(data) & 0xff] + data + [0xff - pktsum]
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
241 return(map(chr, pkt))
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
242
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
243 Encapsulate = staticmethod(Encapsulate)
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
244
10
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
245 def __init__(self, s = None):
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
246 print str(inspect.getmodule(self))
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
247 self.buffer = []
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
248 self.state = 'init'
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
249 self.packets = []
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
250
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
251 self.bufmsb = 0
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
252 self._dataleft = 0
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
253
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
254 self.fr_err = 0 # Framing error
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
255 self.ck_err = 0 # Checksum error
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
256 self.rx_cnt = 0 # Packet count
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
257
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
258 self.pktq = []
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
259 self.s = s
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
260
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
261 def writedata(self, data):
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
262 self.s.write("".join(map(str, data)))
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
263
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
264 def getdata(self):
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
265 l = []
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
266 while (1):
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
267 a = self.s.read()
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
268 if (a == ''):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
269 break
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
270 l.append(ord(a))
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
271
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
272 return self.process(l)
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
273
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
274 def process(self, data):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
275 pktcount = 0
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
276 for d in data:
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
277 if (self.state == 'init'):
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
278 if (d != 0x7e):
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
279 print "Framing error, got 0x%02x, expected 0x7e" % (d)
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
280 self.fr_err += 1
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
281 continue
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
282
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
283 self.state = 'sizemsb'
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
284 elif (self.state == 'sizemsb'):
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
285 self.bufmsb = d
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
286 self.state = 'sizelsb'
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
287 elif (self.state == 'sizelsb'):
7
579dedf5a1f1 Rejoin line break, left over from removal of struct.
darius@inchoate.localdomain
parents: 6
diff changeset
288 self.dataleft = self.bufmsb << 8 | d
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
289 self.state = 'data'
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
290 elif (self.state == 'data'):
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
291 self.buffer.append(d)
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
292 self.dataleft = self.dataleft - 1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
293 if (self.dataleft == 0):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
294 self.state = 'cksum'
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
295 elif (self.state == 'cksum'):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
296 pktsum = reduce(lambda x, y: x + y, self.buffer) & 0xff
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
297 rxcksum = d
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
298 self.state = 'init'
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
299 if (pktsum + rxcksum != 0xff):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
300 self.buffer = []
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
301 self.ck_err += 1
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
302 print "Checksum error, got 0x%02x, expected 0x%02x" % \
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
303 (rxcksum, 0xff - pktsum)
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
304 else:
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
305 print "Got a packet - " + str(self.buffer)
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
306 p = Packets.Build(self.buffer)
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
307 self.pktq.append(p)
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
308 self.buffer = []
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
309 pktcount += 1
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
310 self.rx_cnt += 1
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
311 else:
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
312 print "Invalid state %s! Resetting" % (self.state)
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
313 self.state = 'init'
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
314
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
315 return pktcount
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
316
10
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
317 def polldev():
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
318 while (1):
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
319 foo = up.getdata()
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
320 for p in up.pktq:
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
321 print p
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
322
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
323 if (foo == 0):
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
324 time.sleep(0.1)
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
325
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
326 #for c in dir():
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
327 # if (issubclass(c, PktBase)):
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
328 # print ..
0
bdcdd8380d94 Some test routines & framework for talking to MaxStream ZigBee modules.
darius@inchoate.localdomain
parents:
diff changeset
329
10
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
330 try:
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
331 s = serial.Serial(port='/dev/cuaU0', baudrate=9600, bytesize=8, parity='N', \
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
332 stopbits=1, rtscts=0)
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
333 # Non-blocking
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
334 s.timeout = 0
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
335 #s.write('+++')
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
336 #s.readline(eol='\r')
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
337 except serial.serialutil.SerialException, e:
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
338 print "Can't open serial port - " + str(e)
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
339 s = None
4c91fdfc862e - Further changes to make things cleaner (ie RX packets are "read only" etc).
darius@inchoate.localdomain
parents: 9
diff changeset
340
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
341 # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
342 goodtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 15, 56]
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
343
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
344 # Checksum error
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
345 badtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 14, 56]
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
346
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
347 #0x0005 (-36dBm) -> 1 samples, mask 0x020e, DIO - 0x00e, ADC0 - 0x3ff
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
348 adctest = [126, 0, 12, 131, 0, 5, 36, 0, 1, 2, 14, 0, 14, 3, 255, 50]
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
349
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
350 # Exception
5
5d5963d542bc More tidyups & fixes.
darius@inchoate.localdomain
parents: 4
diff changeset
351 badpkttypetest = [126, 0, 3, 10, 86, 76, 83]
4
ed7abe6f59c2 Many fixes (again)
darius@inchoate.localdomain
parents: 3
diff changeset
352
9
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
353 # Frame ID = 0, Cmd = 'VL', Status = OK, Value = 'VL Result'
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
354 atreply = [126, 0, 14, 136, 0, 86, 76, 0, 86, 76, 32, 82, 101, 115, 117, 108, 116, 148]
d147529ad2db Switch to only reading RX pkts and writing TX pkts to make things simpler.
darius@inchoate.localdomain
parents: 8
diff changeset
355
6
9c499d923544 Many fixes.
darius@inchoate.localdomain
parents: 5
diff changeset
356 up = Packets(s)
1
b5d8ec0ffd21 Lots of changes..
darius@inchoate.localdomain
parents: 0
diff changeset
357 up.process(goodtest)
3
7bf4d4265339 Pop off the packets we test with.
darius@inchoate.localdomain
parents: 1
diff changeset
358 up.process(badtest)
7bf4d4265339 Pop off the packets we test with.
darius@inchoate.localdomain
parents: 1
diff changeset
359 up.process(adctest)
7bf4d4265339 Pop off the packets we test with.
darius@inchoate.localdomain
parents: 1
diff changeset
360 print up.pktq.pop(0)
7bf4d4265339 Pop off the packets we test with.
darius@inchoate.localdomain
parents: 1
diff changeset
361 print up.pktq.pop(0)
0
bdcdd8380d94 Some test routines & framework for talking to MaxStream ZigBee modules.
darius@inchoate.localdomain
parents:
diff changeset
362