|
| 1 | +# |
| 2 | +# Copyright (c) 2024 Project CHIP Authors |
| 3 | +# All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import argparse |
| 19 | +import logging |
| 20 | +import os |
| 21 | +import subprocess |
| 22 | +import sys |
| 23 | +import time |
| 24 | +from pathlib import Path |
| 25 | +from typing import Any |
| 26 | + |
| 27 | +import serial # type: ignore |
| 28 | +from pw_hdlc import rpc |
| 29 | +from pw_unit_test.rpc import run_tests |
| 30 | + |
| 31 | +PW_LOG = logging.getLogger(__name__) |
| 32 | + |
| 33 | +PROTO = Path(os.environ["PW_ROOT"], |
| 34 | + "pw_unit_test/pw_unit_test_proto/unit_test.proto") |
| 35 | + |
| 36 | + |
| 37 | +class colors: |
| 38 | + HEADER = "\033[95m" |
| 39 | + OKBLUE = "\033[94m" |
| 40 | + OKCYAN = "\033[96m" |
| 41 | + OKGREEN = "\033[92m" |
| 42 | + WARNING = "\033[93m" |
| 43 | + FAIL = "\033[91m" |
| 44 | + ENDC = "\033[0m" |
| 45 | + BOLD = "\033[1m" |
| 46 | + |
| 47 | + |
| 48 | +PASS_STRING = colors.OKGREEN + "\N{check mark}" + colors.ENDC |
| 49 | +FAIL_STRING = colors.FAIL + "FAILED" + colors.ENDC |
| 50 | + |
| 51 | + |
| 52 | +def _parse_args(): |
| 53 | + """Parses and returns the command line arguments.""" |
| 54 | + parser = argparse.ArgumentParser( |
| 55 | + description="CHIP on device unit test runner.") |
| 56 | + parser.add_argument("-d", "--device", help="the serial port to use") |
| 57 | + parser.add_argument( |
| 58 | + "-b", "--baudrate", type=int, default=115200, help="the baud rate to use" |
| 59 | + ) |
| 60 | + parser.add_argument( |
| 61 | + "-f", |
| 62 | + "--flash_image", |
| 63 | + help="a firmware image which will be flashed berfore runnning the test", |
| 64 | + ) |
| 65 | + parser.add_argument( |
| 66 | + "-o", |
| 67 | + "--output", |
| 68 | + type=argparse.FileType("wb"), |
| 69 | + default=sys.stdout.buffer, |
| 70 | + help=( |
| 71 | + "The file to which to write device output (HDLC channel 1); " |
| 72 | + "provide - or omit for stdout." |
| 73 | + ), |
| 74 | + ) |
| 75 | + return parser.parse_args() |
| 76 | + |
| 77 | + |
| 78 | +def flash_device(device: str, flash_image: str, **kwargs): |
| 79 | + """flashes the EFR32 device using commander""" |
| 80 | + err = subprocess.call( |
| 81 | + ["commander", "flash", "--device", "EFR32", flash_image]) |
| 82 | + if err: |
| 83 | + raise Exception("flash failed") |
| 84 | + |
| 85 | + |
| 86 | +def get_hdlc_rpc_client(device: str, baudrate: int, output: Any, **kwargs): |
| 87 | + """Get the HdlcRpcClient based on arguments.""" |
| 88 | + serial_device = serial.Serial(device, baudrate, timeout=1) |
| 89 | + reader = rpc.SerialReader(serial_device, 8192) |
| 90 | + write = serial_device.write |
| 91 | + return rpc.HdlcRpcClient( |
| 92 | + reader, |
| 93 | + PROTO, |
| 94 | + rpc.default_channels(write), |
| 95 | + lambda data: rpc.write_to_file(data, output), |
| 96 | + ) |
| 97 | + |
| 98 | + |
| 99 | +def runner(client: rpc.HdlcRpcClient) -> int: |
| 100 | + """Run the tests""" |
| 101 | + |
| 102 | + test_records = run_tests(client.rpcs()) |
| 103 | + |
| 104 | + return len(test_records.failing_tests) |
| 105 | + |
| 106 | + |
| 107 | +def main() -> int: |
| 108 | + args = _parse_args() |
| 109 | + if args.flash_image: |
| 110 | + flash_device(**vars(args)) |
| 111 | + time.sleep(1) # Give time for device to boot |
| 112 | + |
| 113 | + with get_hdlc_rpc_client(**vars(args)) as client: |
| 114 | + return runner(client) |
| 115 | + |
| 116 | + |
| 117 | +if __name__ == "__main__": |
| 118 | + sys.exit(main()) |
0 commit comments