annotate server/ts.py @ 26:d3e5934fe55c

- Move crc16 to utils and fix it - Fix main.c crc calculation
author Matt Johnston <matt@ucc.asn.au>
date Thu, 07 Jun 2012 20:26:50 +0800
parents 2943f62c8e62
children dbbd503119ba
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 #!/usr/bin/env python2.7
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
3 BTADDR = "00:12:03:27:70:88"
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
4 SLEEP_TIME = 180
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
5 # time that the bluetooth takes to get going?
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
6 EXTRA_WAKEUP = 0
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
7
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
8 FETCH_TRIES = 3
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
9
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
10 # avoid turning off the bluetooth etc.
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
11 TESTING = True
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
12
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 import sys
25
2943f62c8e62 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
14 #import httplib
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
15 import time
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
16 import traceback
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
17
26
d3e5934fe55c - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 25
diff changeset
18 from utils import monotonic_time, retry, readline, crc16
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 lightblue = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 try:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 import lightblue
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 except ImportError:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 import bluetooth
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 def get_socket(addr):
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 if lightblue:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 s = lightblue.socket()
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
29 s.connect((addr, 1))
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
30 s.settimeout(3)
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 else:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 s.connect((addr, 1))
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
35 s.setblocking(False)
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
36
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 return s
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
40 @retry()
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 def fetch(sock):
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
42 print "fetch"
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 sock.send("fetch\n")
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 crc = 0
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 lines = []
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if l != 'START\n':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 print>>sys.stderr, "Bad expected START line '%s'\n" % l.rstrip('\n')
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 crc = crc16(l, crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 lines.append(l)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
55 while True:
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 crc = crc16(l, crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 if l == 'END\n':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 break
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 lines.append(l)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
65 print lines
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
66
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 recv_crc = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 try:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 k, v = l.rstrip('\n').split('=')
25
2943f62c8e62 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
71 print k,v
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 if k == 'CRC':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 recv_crc = int(v)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 if recv_crc < 0 or recv_crc > 0xffff:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 recv_crc = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 except ValueError:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 pass
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 if recv_crc is None:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 print>>sys.stderr, "Bad expected CRC line '%s'\n" % l.rstrip('\n')
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 if recv_crc != crc:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 print>>sys.stderr, "Bad CRC: calculated 0x%x vs received 0x%x\n" % (crc, recv_crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 return ''.join(lines)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
88
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
89 @retry()
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
90 def turn_off(sock):
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
91 if TESTING:
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
92 return None
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
93 sock.send("btoff\n");
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
94 # read newline
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
95 l = readline(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
96 if not l:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
97 print>>sys.stderr, "Bad response to btoff\n"
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
98 return None
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
99
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
100 off, next_wake = l.rstrip().split(':')
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
101 if off != 'Off':
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
102 print>>sys.stderr, "Bad response to btoff '%s'\n" % l
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
103
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
104 return int(next_wake)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
105
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
106 def do_comms(sock):
25
2943f62c8e62 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 24
diff changeset
107 print "do_comms"
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
108 d = None
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
109 # serial could be unreliable, try a few times
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
110 for i in range(FETCH_TRIES):
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
111 d = fetch(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
112 if d:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
113 break
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
114 time.sleep(1)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
115 if not d:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
116 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
117
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
118 res = send_results()
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
119 if not res:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
120 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
121
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
122 clear_meas(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
123
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
124 next_wake = turn_off(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
125 sock.close()
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
126 return next_wake
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
127
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
128 testcount = 0
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
129
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
130 def sleep_for(secs):
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
131 until = monotonic_time() + secs
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
132 while True:
24
44c5ab5ea879 - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 23
diff changeset
133 length = until - monotonic_time()
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
134 if length <= 0:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
135 return
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
136 time.sleep(length)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
137
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
138 def main():
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
139
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
140 while True:
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
141 sock = None
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
142 try:
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
143 sock = get_socket(BTADDR)
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
144 except Exception, e:
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
145 print>>sys.stderr, "Error connecting:"
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
146 traceback.print_exc(file=sys.stderr)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
147 sleep_time = SLEEP_TIME
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
148 if sock:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
149 next_wake = None
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
150 try:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
151 next_wake = do_comms(sock)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
152 except Exception, e:
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
153 print>>sys.stderr, "Error in do_comms:"
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
154 traceback.print_exc(file=sys.stderr)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
155 if next_wake:
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
156 sleep_time = min(next_wake+EXTRA_WAKEUP, sleep_time)
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
157
23
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
158 if TESTING:
b5925cb4f264 Fix bugs in server code (try actually running it)
Matt Johnston <matt@ucc.asn.au>
parents: 22
diff changeset
159 print "Sleeping for %d" % sleep_time
22
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
160 sleep_for(sleep_time)
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
161
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
162 if __name__ == '__main__':
885532437100 A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents: 21
diff changeset
163 main()