annotate zbtest.py @ 14:ac60a9244bdf

Use proper telnet protocol classes. Means we use raw/character at a time stuff and don't get extra echos. Keeps track of each line to log it to the file as well as printing the last seen line on client connect.
author darius@Inchoate
date Sat, 17 Jan 2009 14:42:51 +1030
parents 75f785a09e2e
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
1 #
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
2 # Test code for ZigBee code
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
3 #
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
4 # Copyright (c) 2009
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
5 # Daniel O'Connor <darius@dons.net.au>. All rights reserved.
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
6 #
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
7 # Redistribution and use in source and binary forms, with or without
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
8 # modification, are permitted provided that the following conditions
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
9 # are met:
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
10 # 1. Redistributions of source code must retain the above copyright
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
11 # notice, this list of conditions and the following disclaimer.
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
12 # 2. Redistributions in binary form must reproduce the above copyright
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
13 # notice, this list of conditions and the following disclaimer in the
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
14 # documentation and/or other materials provided with the distribution.
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
15 #
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
16 # THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS ``AS IS'' AND
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
19 # ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
26 # SUCH DAMAGE.
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
27 #
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
28
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
29 import zb, serial, time
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
30
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
31 def polldev():
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
32 while (1):
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
33 foo = up.getdata()
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
34 for p in up.pktq:
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
35 print p
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
36
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
37 if (foo == 0):
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
38 time.sleep(0.1)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
39
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
40 #for c in dir():
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
41 # if (issubclass(c, PktBase)):
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
42 # print ..
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
43
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
44 try:
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
45 s = serial.Serial(port='/dev/cuaU0', baudrate=38400, bytesize=8, parity='N', \
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
46 stopbits=1, rtscts=0)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
47 # Non-blocking
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
48 s.timeout = 0
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
49 #s.write('+++')
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
50 #s.readline(eol='\r')
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
51 except serial.serialutil.SerialException, e:
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
52 print "Can't open serial port - " + str(e)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
53 s = None
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
54
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
55 # 0x0001 (-36dBm) -> 1 samples, mask 0x000f, DIO - 0x00f
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
56 goodtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 15, 56]
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
57
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
58 # Checksum error
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
59 badtest = [126, 0, 10, 131, 0, 1, 36, 0, 1, 0, 15, 0, 14, 56]
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
60
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
61 #0x0005 (-36dBm) -> 1 samples, mask 0x020e, DIO - 0x00e, ADC0 - 0x3ff
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
62 adctest = [126, 0, 12, 131, 0, 5, 36, 0, 1, 2, 14, 0, 14, 3, 255, 50]
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
63
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
64 # Exception
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
65 badpkttypetest = [126, 0, 3, 10, 86, 76, 83]
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
66
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
67 # Frame ID = 0, Cmd = 'VL', Status = OK, Value = 'VL Result'
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
68 atreply = [126, 0, 14, 136, 0, 86, 76, 0, 86, 76, 32, 82, 101, 115, 117, 108, 116, 148]
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
69
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
70 # Do some basic tests
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
71 up = zb.Packets(s)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
72 up.process(goodtest)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
73 up.process(badtest)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
74 up.process(adctest)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
75 p = up.pktq.pop(0)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
76 assert(p.sender == 0x1 and p.rssi == -36 and p.nsamples == 1 and p.mask == 0xf)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
77
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
78 p = up.pktq.pop(0)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
79 assert(p.sender == 0x5 and p.rssi == -36 and p.nsamples == 1 and p.mask == 0x20e)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
80
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
81 assert len(up.pktq) == 0
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
82
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
83 assert(up.rx_cnt == 2)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
84 assert(up.ck_err == 1)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
85 assert(up.fr_err == 0)
75f785a09e2e Add license.
darius@Inchoate
parents:
diff changeset
86