annotate old/server/utils.py @ 298:b0c0e88cbfcc

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