annotate zb.py @ 9:d147529ad2db

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