annotate zb.py @ 8:9f0808b13454

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