annotate zb.py @ 6:9c499d923544

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