Debian Codegraph @

.gitlab-ci/bare-metal/serial_buffer.py

raw
1 #!/usr/bin/env python3
2 #
3 # Copyright © 2020 Google LLC
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a
6 # copy of this software and associated documentation files (the "Software"),
7 # to deal in the Software without restriction, including without limitation
8 # the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 # and/or sell copies of the Software, and to permit persons to whom the
10 # Software is furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice (including the next
13 # paragraph) shall be included in all copies or substantial portions of the
14 # Software.
15 #
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 # THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 # FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22 # IN THE SOFTWARE.
23
24 import argparse
25 from datetime import datetime, UTC
26 import queue
27 import serial
28 import threading
29 import time
30
31
32 class SerialBuffer:
33 def __init__(self, dev, filename, prefix, timeout=None, line_queue=None):
34 self.filename = filename
35 self.dev = dev
36
37 if dev:
38 self.f = open(filename, "wb+")
39 self.serial = serial.Serial(dev, 115200, timeout=timeout)
40 else:
41 self.f = open(filename, "rb")
42 self.serial = None
43
44 self.byte_queue = queue.Queue()
45 # allow multiple SerialBuffers to share a line queue so you can merge
46 # servo's CPU and EC streams into one thing to watch the boot/test
47 # progress on.
48 if line_queue:
49 self.line_queue = line_queue
50 else:
51 self.line_queue = queue.Queue()
52 self.prefix = prefix
53 self.timeout = timeout
54 self.sentinel = object()
55 self.closing = False
56
57 if self.dev:
58 self.read_thread = threading.Thread(
59 target=self.serial_read_thread_loop, daemon=True)
60 else:
61 self.read_thread = threading.Thread(
62 target=self.serial_file_read_thread_loop, daemon=True)
63 self.read_thread.start()
64
65 self.lines_thread = threading.Thread(
66 target=self.serial_lines_thread_loop, daemon=True)
67 self.lines_thread.start()
68
69 def close(self):
70 self.closing = True
71 if self.serial:
72 self.serial.cancel_read()
73 self.read_thread.join()
74 self.lines_thread.join()
75 if self.serial:
76 self.serial.close()
77
78 # Thread that just reads the bytes from the serial device to try to keep from
79 # buffer overflowing it. If nothing is received in 1 minute, it finalizes.
80 def serial_read_thread_loop(self):
81 greet = "Serial thread reading from %s\n" % self.dev
82 self.byte_queue.put(greet.encode())
83
84 while not self.closing:
85 try:
86 b = self.serial.read()
87 if len(b) == 0:
88 break
89 self.byte_queue.put(b)
90 except Exception as err:
91 print(self.prefix + str(err))
92 break
93 self.byte_queue.put(self.sentinel)
94
95 # Thread that just reads the bytes from the file of serial output that some
96 # other process is appending to.
97 def serial_file_read_thread_loop(self):
98 greet = "Serial thread reading from %s\n" % self.filename
99 self.byte_queue.put(greet.encode())
100
101 while not self.closing:
102 line = self.f.readline()
103 if line:
104 self.byte_queue.put(line)
105 else:
106 time.sleep(0.1)
107 self.byte_queue.put(self.sentinel)
108
109 # Thread that processes the stream of bytes to 1) log to stdout, 2) log to
110 # file, 3) add to the queue of lines to be read by program logic
111
112 def serial_lines_thread_loop(self):
113 line = bytearray()
114 while True:
115 bytes = self.byte_queue.get(block=True)
116
117 if bytes == self.sentinel:
118 self.read_thread.join()
119 self.line_queue.put(self.sentinel)
120 break
121
122 # Write our data to the output file if we're the ones reading from
123 # the serial device
124 if self.dev:
125 self.f.write(bytes)
126 self.f.flush()
127
128 for b in bytes:
129 line.append(b)
130 if b == b'\n'[0]:
131 line = line.decode(errors="replace")
132
133 ts = datetime.now(tz=UTC)
134 ts_str = f"{ts.hour:02}:{ts.minute:02}:{ts.second:02}.{int(ts.microsecond / 1000):03}"
135 print("{endc}{time}{prefix}{line}".format(
136 time=ts_str, prefix=self.prefix, line=line, endc='\033[0m'), flush=True, end='')
137
138 self.line_queue.put(line)
139 line = bytearray()
140
141 def lines(self, timeout=None, phase=None):
142 start_time = time.monotonic()
143 while True:
144 read_timeout = None
145 if timeout:
146 read_timeout = timeout - (time.monotonic() - start_time)
147 if read_timeout <= 0:
148 print("read timeout waiting for serial during {}".format(phase))
149 self.close()
150 break
151
152 try:
153 line = self.line_queue.get(timeout=read_timeout)
154 except queue.Empty:
155 print("read timeout waiting for serial during {}".format(phase))
156 self.close()
157 break
158
159 if line == self.sentinel:
160 print("End of serial output")
161 self.lines_thread.join()
162 break
163
164 yield line
165
166
167 def main():
168 parser = argparse.ArgumentParser()
169
170 parser.add_argument('--dev', type=str, help='Serial device')
171 parser.add_argument('--file', type=str,
172 help='Filename for serial output', required=True)
173 parser.add_argument('--prefix', type=str,
174 help='Prefix for logging serial to stdout', nargs='?')
175
176 args = parser.parse_args()
177
178 ser = SerialBuffer(args.dev, args.file, args.prefix or "")
179 for line in ser.lines():
180 # We're just using this as a logger, so eat the produced lines and drop
181 # them
182 pass
183
184
185 if __name__ == '__main__':
186 main()