annotate server/ts.py @ 21:2029633912c2

untested simple server proxy code
author Matt Johnston <matt@ucc.asn.au>
date Tue, 22 May 2012 23:45:16 +0800
parents
children 885532437100
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
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
3 import sys
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import httplib
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 lightblue = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
7 try:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
8 import lightblue
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
9 except ImportError:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
10 import bluetooth
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
12 BTADDR = "00:12:03:27:70:88"
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 def get_socket(addr):
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 if lightblue:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 s = lightblue.socket()
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 s.connect(addr, 1)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 else:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19 s = bluetooth.BluetoothSocket( bluetooth.RFCOMM )
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 s.connect((addr, 1))
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 return s
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 def crc16(buff, crc = 0, poly = 0x8408):
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26     l = len(buff)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27     i = 0
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28     while i < l:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29         ch = ord(buff[i])
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30         uc = 0
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31         while uc < 8:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32             if (crc & 1) ^ (ch & 1):
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33                 crc = (crc >> 1) ^ poly
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34             else:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35                 crc >>= 1
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36             ch >>= 1
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37             uc += 1
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38         i += 1
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39     return crc
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 def fetch(sock):
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 def readline(self):
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 buf = ''
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 while true:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48 c = self.recv(1)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 if c == '\r':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 continue
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 buf.append(c)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if c == '\n':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 return buf
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 crc = 0
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 lines = []
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 if l != 'START\n':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 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
62 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 crc = crc16(l, crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 lines.append(l)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 while true:
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
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 crc = crc16(l, crc)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 if l == 'END\n':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 break
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 lines.append(l)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76 l = readline(sock)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 recv_crc = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 try:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 k, v = l.rstrip('\n').split('=')
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 if k == 'CRC':
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 recv_crc = int(v)
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 if recv_crc < 0 or recv_crc > 0xffff:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 recv_crc = None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84 except ValueError:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 pass
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 if recv_crc is None:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 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
89 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 if recv_crc != crc:
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92 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
93 return None
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
2029633912c2 untested simple server proxy code
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 return ''.join(lines)