comparison server/ts.py @ 24:44c5ab5ea879

- some fixes for server code - don't turn off bluetooth in avr code
author Matt Johnston <matt@ucc.asn.au>
date Wed, 06 Jun 2012 22:32:49 +0800
parents b5925cb4f264
children 2943f62c8e62
comparison
equal deleted inserted replaced
23:b5925cb4f264 24:44c5ab5ea879
2 2
3 BTADDR = "00:12:03:27:70:88" 3 BTADDR = "00:12:03:27:70:88"
4 SLEEP_TIME = 180 4 SLEEP_TIME = 180
5 # time that the bluetooth takes to get going? 5 # time that the bluetooth takes to get going?
6 EXTRA_WAKEUP = 0 6 EXTRA_WAKEUP = 0
7
8 FETCH_TRIES = 3
7 9
8 # avoid turning off the bluetooth etc. 10 # avoid turning off the bluetooth etc.
9 TESTING = True 11 TESTING = True
10 12
11 import sys 13 import sys
23 25
24 def get_socket(addr): 26 def get_socket(addr):
25 if lightblue: 27 if lightblue:
26 s = lightblue.socket() 28 s = lightblue.socket()
27 s.connect((addr, 1)) 29 s.connect((addr, 1))
30 s.settimeout(3)
28 else: 31 else:
29 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM ) 32 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
30 s.connect((addr, 1)) 33 s.connect((addr, 1))
31 34
32 s.setnonblocking(True) 35 s.setblocking(False)
36
37 time.sleep(30)
33 38
34 return s 39 return s
35 40
36 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html 41 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
37 def crc16(buff, crc = 0, poly = 0x8408): 42 def crc16(buff, crc = 0, poly = 0x8408):
51 return crc 56 return crc
52 57
53 58
54 @retry() 59 @retry()
55 def fetch(sock): 60 def fetch(sock):
61 print "fetch"
56 sock.send("fetch\n") 62 sock.send("fetch\n")
57 63
58 crc = 0 64 crc = 0
59 65
60 lines = [] 66 lines = []
63 print>>sys.stderr, "Bad expected START line '%s'\n" % l.rstrip('\n') 69 print>>sys.stderr, "Bad expected START line '%s'\n" % l.rstrip('\n')
64 return None 70 return None
65 crc = crc16(l, crc) 71 crc = crc16(l, crc)
66 lines.append(l) 72 lines.append(l)
67 73
68 while true: 74 while True:
69 l = readline(sock) 75 l = readline(sock)
70 76
71 crc = crc16(l, crc) 77 crc = crc16(l, crc)
72 78
73 if l == 'END\n': 79 if l == 'END\n':
74 break 80 break
75 81
76 lines.append(l) 82 lines.append(l)
83
84 print lines
77 85
78 l = readline(sock) 86 l = readline(sock)
79 recv_crc = None 87 recv_crc = None
80 try: 88 try:
81 k, v = l.rstrip('\n').split('=') 89 k, v = l.rstrip('\n').split('=')
137 testcount = 0 145 testcount = 0
138 146
139 def sleep_for(secs): 147 def sleep_for(secs):
140 until = monotonic_time() + secs 148 until = monotonic_time() + secs
141 while True: 149 while True:
142 length = until < monotonic_time() 150 length = until - monotonic_time()
143 if length <= 0: 151 if length <= 0:
144 return 152 return
145 time.sleep(length) 153 time.sleep(length)
146 154
147 def main(): 155 def main():