summaryrefslogtreecommitdiff
path: root/.gitlab-ci/bare-metal/serial_buffer.py
blob: 1dc7596df6697e0eda35da999d87c9615b0e8806 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python3
#
# Copyright © 2020 Google LLC
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice (including the next
# paragraph) shall be included in all copies or substantial portions of the
# Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

import argparse
from datetime import datetime, timezone
import queue
import serial
import threading
import time


class SerialBuffer:
    def __init__(self, dev, filename, prefix, timeout=None):
        self.filename = filename
        self.dev = dev

        if dev:
            self.f = open(filename, "wb+")
            self.serial = serial.Serial(dev, 115200, timeout=timeout)
        else:
            self.f = open(filename, "rb")
            self.serial = None

        self.byte_queue = queue.Queue()
        self.line_queue = queue.Queue()
        self.prefix = prefix
        self.timeout = timeout
        self.sentinel = object()
        self.closing = False

        if self.dev:
            self.read_thread = threading.Thread(
                target=self.serial_read_thread_loop, daemon=True)
        else:
            self.read_thread = threading.Thread(
                target=self.serial_file_read_thread_loop, daemon=True)
        self.read_thread.start()

        self.lines_thread = threading.Thread(
            target=self.serial_lines_thread_loop, daemon=True)
        self.lines_thread.start()

    def close(self):
        self.closing = True
        if self.serial:
            self.serial.cancel_read()
        self.read_thread.join()
        self.lines_thread.join()
        if self.serial:
            self.serial.close()

    # Thread that just reads the bytes from the serial device to try to keep from
    # buffer overflowing it. If nothing is received in 1 minute, it finalizes.
    def serial_read_thread_loop(self):
        greet = "Serial thread reading from %s\n" % self.dev
        self.byte_queue.put(greet.encode())

        while not self.closing:
            try:
                b = self.serial.read()
                if len(b) == 0:
                    break
                self.byte_queue.put(b)
            except Exception as err:
                print(self.prefix + str(err))
                break
        self.byte_queue.put(self.sentinel)

    # Thread that just reads the bytes from the file of serial output that some
    # other process is appending to.
    def serial_file_read_thread_loop(self):
        greet = "Serial thread reading from %s\n" % self.filename
        self.byte_queue.put(greet.encode())

        while not self.closing:
            line = self.f.readline()
            if line:
                self.byte_queue.put(line)
            else:
                time.sleep(0.1)
        self.byte_queue.put(self.sentinel)

    # Thread that processes the stream of bytes to 1) log to stdout, 2) log to
    # file, 3) add to the queue of lines to be read by program logic

    def serial_lines_thread_loop(self):
        line = bytearray()
        while True:
            bytes = self.byte_queue.get(block=True)

            if bytes == self.sentinel:
                self.read_thread.join()
                self.line_queue.put(self.sentinel)
                break

            # Write our data to the output file if we're the ones reading from
            # the serial device
            if self.dev:
                self.f.write(bytes)
                self.f.flush()

            for b in bytes:
                line.append(b)
                if b == b'\n'[0]:
                    line = line.decode(errors="replace")

                    time = datetime.now().strftime('%y-%m-%d %H:%M:%S')
                    print("{endc}{time} {prefix}{line}".format(
                        time=time, prefix=self.prefix, line=line, endc='\033[0m'), flush=True, end='')

                    self.line_queue.put(line)
                    line = bytearray()

    def lines(self, timeout=None, phase=None):
        start_time = time.monotonic()
        while True:
            read_timeout = None
            if timeout:
                read_timeout = timeout - (time.monotonic() - start_time)
                if read_timeout <= 0:
                    print("read timeout waiting for serial during {}".format(phase))
                    self.close()
                    break

            try:
                line = self.line_queue.get(timeout=read_timeout)
            except queue.Empty:
                print("read timeout waiting for serial during {}".format(phase))
                self.close()
                break

            if line == self.sentinel:
                print("End of serial output")
                self.lines_thread.join()
                break

            yield line


def main():
    parser = argparse.ArgumentParser()

    parser.add_argument('--dev', type=str, help='Serial device')
    parser.add_argument('--file', type=str,
                        help='Filename for serial output', required=True)
    parser.add_argument('--prefix', type=str,
                        help='Prefix for logging serial to stdout', nargs='?')

    args = parser.parse_args()

    ser = SerialBuffer(args.dev, args.file, args.prefix or "")
    for line in ser.lines():
        # We're just using this as a logger, so eat the produced lines and drop
        # them
        pass


if __name__ == '__main__':
    main()