annotate server/utils.py @ 385:a827f1c5ddbe

don't stay awake for any command - chews battery if server goes down
author Matt Johnston <matt@ucc.asn.au>
date Mon, 09 Jul 2012 00:39:07 +0800
parents dae8eb26eaa3
children 51d889ad39a3
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import sys
331
5de3fc71ce48 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 330
diff changeset
3 #import ctypes
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import time
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import select
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
7 lightblue = None
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
8 try:
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
9 import lightblue
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
10 except ImportError:
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
11 pass
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
12
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 DEFAULT_TRIES = 3
353
20f7161399a8 change timeout for readline
Matt Johnston <matt@ucc.asn.au>
parents: 332
diff changeset
14 READLINE_SELECT_TIMEOUT = 4
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16 __all__ = ('monotonic_time', 'retry')
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 clock_gettime = None
331
5de3fc71ce48 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 330
diff changeset
19 no_clock_gettime = True
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
20 def monotonic_time():
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
21 global clock_gettime
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
22 global no_clock_gettime
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
23 if no_clock_gettime:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 return time.time()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 class timespec(ctypes.Structure):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 _fields_ = [
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 ('tv_sec', ctypes.c_long),
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 ('tv_nsec', ctypes.c_long)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 ]
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 if not clock_gettime:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 try:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 librt = ctypes.CDLL('librt.so.0', use_errno=True)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 clock_gettime = librt.clock_gettime
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35 clock_gettime.argtypes = [ctypes.c_int, ctypes.POINTER(timespec)]
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
36 except:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 print>>sys.stderr, "No clock_gettime(), using fake fallback."
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 no_clock_gettime = True
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39 return time.time()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 t = timespec()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 CLOCK_MONOTONIC = 1 # see <linux/time.h>
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 if clock_gettime(CLOCK_MONOTONIC, ctypes.pointer(t)) != 0:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 errno_ = ctypes.get_errno()
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 raise OSError(errno_, os.strerror(errno_))
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 return t.tv_sec + t.tv_nsec * 1e-9
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 # decorator, tries a number of times, returns None on failure, sleeps between
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 # Must be used as "@retry()" if arguments are defaulted
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 def retry(retries=DEFAULT_TRIES, try_time = 1):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 def inner(func):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 def new_f(*args, **kwargs):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 for i in range(retries):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55 d = func(*args, **kwargs)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 if d:
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 return d
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 time.sleep(try_time)
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59 new_f.func_name = func.func_name
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 return new_f
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 return inner
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 def readline(sock):
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 timeout = READLINE_SELECT_TIMEOUT
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 buf = ''
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
66 while True:
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
67 if not lightblue:
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
68 (rlist, wlist, xlist) = select.select([sock], [], [], timeout)
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
69 if sock not in rlist:
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
70 # hit timeout
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
71 return None
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 c = sock.recv(1)
330
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
74 if c == '':
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
75 # lightblue timeout
7ac6b8846eea - some fixes for server code
Matt Johnston <matt@ucc.asn.au>
parents: 329
diff changeset
76 return None
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 if c == '\r':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 continue
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79
331
5de3fc71ce48 - Make the python work on openwrt
Matt Johnston <matt@ucc.asn.au>
parents: 330
diff changeset
80 buf += c
328
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 if c == '\n':
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 return buf
46070aaf29ea A bit of work on the server python
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83
332
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
84 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
85 def crc16(buff, crc = 0, poly = 0x8408):
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
86 l = len(buff)
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
87 i = 0
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
88 while i < l:
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
89 ch = ord(buff[i])
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
90 uc = 0
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
91 while uc < 8:
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
92 if (crc & 1) ^ (ch & 1):
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
93 crc = (crc >> 1) ^ poly
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
94 else:
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
95 crc >>= 1
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
96 ch >>= 1
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
97 uc += 1
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
98 i += 1
05c1249da994 - Move crc16 to utils and fix it
Matt Johnston <matt@ucc.asn.au>
parents: 331
diff changeset
99 return crc
371
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
100
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
101 def cheap_daemon():
372
dae8eb26eaa3 dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 371
diff changeset
102 print "Daemonising."
dae8eb26eaa3 dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 371
diff changeset
103 sys.stdout.flush()
dae8eb26eaa3 dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 371
diff changeset
104 sys.stderr.flush()
dae8eb26eaa3 dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 371
diff changeset
105 out = file('/dev/null', 'a+')
dae8eb26eaa3 dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 371
diff changeset
106 os.dup2(out.fileno(), sys.stdout.fileno())
dae8eb26eaa3 dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 371
diff changeset
107 os.dup2(out.fileno(), sys.stderr.fileno())
dae8eb26eaa3 dup2 to devnull
Matt Johnston <matt@ucc.asn.au>
parents: 371
diff changeset
108
371
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
109 try:
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
110 pid = os.fork()
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
111 if pid > 0:
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
112 sys.exit(0)
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
113 except OSError, e:
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
114 print>>sys.stderr, "Bad fork()"
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
115 sys.exit(1)
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
116
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
117 os.setsid()
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
118
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
119 try:
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
120 pid = os.fork()
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
121 if pid > 0:
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
122 sys.exit(0)
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
123 except OSError, e:
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
124 print>>sys.stderr, "Bad fork()"
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
125 sys.exit(1)
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
126
7dec59b7eeef cheap_daemon()
Matt Johnston <matt@ucc.asn.au>
parents: 353
diff changeset
127