annotate py/utils.py @ 141:4755e6f9a5b8

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