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 24import argparse 25from datetime import datetime, timezone 26import queue 27import serial 28import threading 29import time 30 31 32class 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 time = datetime.now().strftime('%y-%m-%d %H:%M:%S') 134 print("{endc}{time} {prefix}{line}".format( 135 time=time, prefix=self.prefix, line=line, endc='\033[0m'), flush=True, end='') 136 137 self.line_queue.put(line) 138 line = bytearray() 139 140 def lines(self, timeout=None, phase=None): 141 start_time = time.monotonic() 142 while True: 143 read_timeout = None 144 if timeout: 145 read_timeout = timeout - (time.monotonic() - start_time) 146 if read_timeout <= 0: 147 print("read timeout waiting for serial during {}".format(phase)) 148 self.close() 149 break 150 151 try: 152 line = self.line_queue.get(timeout=read_timeout) 153 except queue.Empty: 154 print("read timeout waiting for serial during {}".format(phase)) 155 self.close() 156 break 157 158 if line == self.sentinel: 159 print("End of serial output") 160 self.lines_thread.join() 161 break 162 163 yield line 164 165 166def main(): 167 parser = argparse.ArgumentParser() 168 169 parser.add_argument('--dev', type=str, help='Serial device') 170 parser.add_argument('--file', type=str, 171 help='Filename for serial output', required=True) 172 parser.add_argument('--prefix', type=str, 173 help='Prefix for logging serial to stdout', nargs='?') 174 175 args = parser.parse_args() 176 177 ser = SerialBuffer(args.dev, args.file, args.prefix or "") 178 for line in ser.lines(): 179 # We're just using this as a logger, so eat the produced lines and drop 180 # them 181 pass 182 183 184if __name__ == '__main__': 185 main() 186