comparison 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
comparison
equal deleted inserted replaced
25:2943f62c8e62 26:d3e5934fe55c
13 import sys 13 import sys
14 #import httplib 14 #import httplib
15 import time 15 import time
16 import traceback 16 import traceback
17 17
18 from utils import monotonic_time, retry, readline 18 from utils import monotonic_time, retry, readline, crc16
19 19
20 lightblue = None 20 lightblue = None
21 try: 21 try:
22 import lightblue 22 import lightblue
23 except ImportError: 23 except ImportError:
33 s.connect((addr, 1)) 33 s.connect((addr, 1))
34 34
35 s.setblocking(False) 35 s.setblocking(False)
36 36
37 return s 37 return s
38
39 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
40 def crc16(buff, crc = 0, poly = 0x8408):
41 l = len(buff)
42 i = 0
43 while i < l:
44 ch = ord(buff[i])
45 uc = 0
46 while uc < 8:
47 if (crc & 1) ^ (ch & 1):
48 crc = (crc >> 1) ^ poly
49 else:
50 crc >>= 1
51 ch >>= 1
52 uc += 1
53 i += 1
54 return crc
55 38
56 39
57 @retry() 40 @retry()
58 def fetch(sock): 41 def fetch(sock):
59 print "fetch" 42 print "fetch"