pycard10.py 8.59 KB
Newer Older
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# This file is part of the card10 project card10.badge.events.ccc.de
#
# The MIT License (MIT)
#
# Copyright (c) 2019 Alexander Böhm
#
# 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 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.

"""
card10 interface

This module provides is an extenstion of the Pyboard class, used to
communicate with the card10 badge over the serial connection via USB.

For description of the card10 board look at the project website <https://card10.badge.events.ccc.de>.

Python2 isn't supported.

Example usage:

    import pycard10
    card10 = pycard10.PyCard10('/dev/ttyACM0')

Then:

    card10.enter_raw_repl()
    card10.exec('import leds')
    card10.exec('leds.set_rocket(0, 31)')
    card10.exit_raw_repl()

    import pycard10
    pycard10.execfile('test.py', device='/dev/ttyACM0')

This script can also be run directly.  To execute a local script, use:

    tools/pycard10.py test.py

Or:
    python3 tools/pycard10.py test.py

"""

__author__ = "Alexander Böhm"
__copyright__ = "Copyright 2019, Alexander Böhm"
__license__ = "MIT"
__email__ = "alexander.boehm@malbolge.net"

import sys
import time
import os
from pyboard import (
    stdout_write_bytes,
    PyboardError,
    Pyboard,
    ProcessPtyToTerminal,
    ProcessToSerial,
    TelnetToSerial,
)


class PyCard10(Pyboard):
    """
    Python card10 connector.
    """

    def __init__(self, device, wait=0):
        """
        Open a connection to the card10 over the serial device *device*.
        """
        Pyboard.__init__(
            self, device=device, baudrate=115200, user=None, password=None, wait=wait
        )

    def exec_raw_no_follow(self, command):
        """
        Execute a the command 'command' on the card10.

        Parameters:
        command (bytes): Command or multiple commands

        Returns:
        None
        """

        if isinstance(command, bytes):
            command_bytes = command
        else:
            command_bytes = bytes(command, encoding="utf8")

        data = self.read_until(1, b">")
        if not data.endswith(b">"):
            raise PyboardError("card10 not in raw repl mode: (response: %r)" % (data))

        # write command
        for i in range(0, len(command_bytes), 256):
            self.serial.write(command_bytes[i : min(i + 256, len(command_bytes))])
            time.sleep(0.01)

        self.serial.write(b"\x04")

        # check if we could exec command
        data = self.serial.read(2)
        if data != b"OK":
            raise PyboardError("could not exec command (response: %r)" % data)

    def enter_raw_repl(self):
        """
        Enter the RAW repl mode. After the prompt character ('>') left in the buffer of the serial line.

        Returns:
        None
        """
        self.serial.write(b"\x03\x03")  # ctrl-C twice: interrupt any running program

        # flush input (without relying on serial.flushInput())
        n = self.serial.inWaiting()
        while n > 0:
            self.serial.read(n)
            n = self.serial.inWaiting()

        self.serial.write(b"\x01")  # ctrl-A: enter raw REPL
        data = self.read_until(1, b"raw REPL; CTRL-B to exit\r\n>")
        if not data.endswith(b"raw REPL; CTRL-B to exit\r\n>"):
            raise PyboardError("could not enter raw repl")

        self.serial.write(b"\r\x04")  # execute nothing
        data = self.serial.read(2)
        if data != b"OK":
            raise PyboardError("could not enter raw repl")

    def exec(self, command):
        """
        Execute a command on the card10 and return the output of the card10.

        Parameters:
        command (bytes): Command or multiple commands

        Returns:
        data(bytes), data_err(bytes): data is standard out, data_err is standard error
        """
        return self.exec_(command)

    def soft_reset(self):
        """
        Doing a soft reset on the board and going to menu.

        Returns:
        None
        """

        self.serial.write(b"\x03\x03")  # ctrl-C twice: interrupt any running program

        self.serial.write(b"\x01")  # ctrl-B: ensue it's the normal mode

        self.serial.write(b"\x04")  # ctrl-D: do the reset

        n = self.serial.inWaiting()
        while n > 0:
            self.serial.read(n)
            n = self.serial.inWaiting()


def execfile(filename, device="/dev/ttyACM0"):
    """
    Execute python source from *filename* via the RAW repl mode on the card10 connected via serial line *device*.

    Parameters:
    filename(str): Path text file with commands
    device(str): Path to the card10 device.

    Returns:
    None
    """
    c = PyCard10(device)
    c.enter_raw_repl()
    output = c.execfile(filename)
    stdout_write_bytes(output)
    c.exit_raw_repl()
    c.close()


def main():
    """
    The main method.

    Returns:
    None
    """

    import argparse

    cmd_parser = argparse.ArgumentParser(description="Run scripts on the card10.")
    cmd_parser.add_argument(
        "--device", default="/dev/ttyACM0", help="the serial device of the card10"
    )
    cmd_parser.add_argument("-c", "--command", help="program passed in as string")
    cmd_parser.add_argument(
        "-w",
        "--wait",
        default=0,
        type=int,
        help="seconds to wait for USB connected board to become available",
    )
    cmd_parser.add_argument(
        "--follow",
        action="store_true",
        help="follow the output after running the scripts [default if no scripts given]",
    )
    cmd_parser.add_argument(
        "--reset", action="store_true", help="Soft reseting the card10"
    )
    cmd_parser.add_argument("files", nargs="*", help="input files")
    args = cmd_parser.parse_args()

    # open the connection to the card10
    try:
        card10 = PyCard10(args.device, args.wait)
    except PyboardError as er:
        print(er)
        sys.exit(1)

    if args.reset:
        card10.soft_reset()

    elif args.command is not None or len(args.files):
        # we must enter raw-REPL mode to execute commands
        # this will do a soft-reset of the board
        try:
            card10.enter_raw_repl()
        except PyboardError as er:
            print(er)
            card10.close()
            sys.exit(1)

        def execbuffer(buf):
            try:
                ret, ret_err = card10.exec_raw(
                    buf, timeout=None, data_consumer=stdout_write_bytes
                )
            except PyboardError as er:
                print(er)
                card10.close()
                sys.exit(1)
            except KeyboardInterrupt:
                sys.exit(1)
            if ret_err:
                card10.exit_raw_repl()
                card10.close()
                stdout_write_bytes(ret_err)
                sys.exit(1)

        # run the command, if given
        if args.command is not None:
            execbuffer(args.command.encode("utf-8"))

        # run any files
        for filename in args.files:
            with open(filename, "rb") as f:
                pyfile = f.read()
                execbuffer(pyfile)

        # exiting raw-REPL just drops to friendly-REPL mode
        card10.exit_raw_repl()

    # if asked explicitly, or no files given, then follow the output
    elif args.follow or (args.command is None and len(args.files) == 0):
        try:
            ret, ret_err = card10.follow(timeout=None, data_consumer=stdout_write_bytes)
        except PyboardError as er:
            print(er)
            sys.exit(1)
        except KeyboardInterrupt:
            sys.exit(1)
        if ret_err:
            card10.close()
            stdout_write_bytes(ret_err)
            sys.exit(1)

    # close the connection to the card10
    card10.close()


if __name__ == "__main__":
    main()