|
| 1 | +# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD |
| 2 | +# SPDX-License-Identifier: Unlicense OR CC0-1.0 |
| 3 | +from __future__ import print_function, unicode_literals |
| 4 | + |
| 5 | +import subprocess |
| 6 | +import time |
| 7 | +from threading import Event, Thread |
| 8 | + |
| 9 | +import netifaces |
| 10 | + |
| 11 | + |
| 12 | +def is_esp32(port): |
| 13 | + """ |
| 14 | + Check if the given port is connected to an ESP32 using esptool. |
| 15 | + """ |
| 16 | + try: |
| 17 | + result = subprocess.run( |
| 18 | + ['esptool.py', '--port', port, 'chip_id'], |
| 19 | + stdout=subprocess.PIPE, stderr=subprocess.PIPE, check=True, text=True |
| 20 | + ) |
| 21 | + return 'ESP32' in result.stdout |
| 22 | + except subprocess.CalledProcessError: |
| 23 | + return False |
| 24 | + |
| 25 | + |
| 26 | +def run_server(server_stop, port): |
| 27 | + print('Launching SLIP netif on port: {}'.format(port)) |
| 28 | + try: |
| 29 | + arg_list = ['sudo', 'slattach', '-v', '-L', '-s', '115200','-p', 'slip', port] |
| 30 | + p = subprocess.Popen(arg_list, stdout=subprocess.PIPE, bufsize=1) |
| 31 | + while not server_stop.is_set(): |
| 32 | + if p.poll() is not None: |
| 33 | + if p.poll() == 16: |
| 34 | + print('[SLIP:] Terminated: hang-up received') |
| 35 | + break |
| 36 | + else: |
| 37 | + raise ValueError( |
| 38 | + 'ENV_TEST_FAILURE: SLIP terminated unexpectedly with {}' |
| 39 | + .format(p.poll())) |
| 40 | + time.sleep(0.1) |
| 41 | + except Exception as e: |
| 42 | + print(e) |
| 43 | + raise ValueError('ENV_TEST_FAILURE: Error running SLIP netif') |
| 44 | + finally: |
| 45 | + p.terminate() |
| 46 | + print('SLIP netif stopped') |
| 47 | + |
| 48 | + |
| 49 | +def test_examples_protocol_slip(dut): |
| 50 | + |
| 51 | + # the PPP test env is reused for SLIP test and it uses three ttyUSB's: two for ESP32 board and another one for the ppp server |
| 52 | + server_port = None |
| 53 | + for i in ['/dev/ttyUSB0', '/dev/ttyUSB1', '/dev/ttyUSB2']: |
| 54 | + if i == dut.serial.port: |
| 55 | + print(f'DUT port: {i}') |
| 56 | + elif is_esp32(i): |
| 57 | + print(f'Some other ESP32: {i}') |
| 58 | + else: |
| 59 | + print(f'Port for SLIP: {i}') |
| 60 | + server_port = i |
| 61 | + if server_port is None: |
| 62 | + print('ENV_TEST_FAILURE: Cannot locate SLIP port') |
| 63 | + raise |
| 64 | + # Attach to the SLI netif |
| 65 | + server_stop = Event() |
| 66 | + t = Thread(target=run_server, args=(server_stop, server_port)) |
| 67 | + t.start() |
| 68 | + try: |
| 69 | + # Setups the SLIP interface |
| 70 | + ppp_server_timeout = time.time() + 30 |
| 71 | + while 'sl0' not in netifaces.interfaces(): |
| 72 | + print( |
| 73 | + "SLIP netif hasn't appear yet, list of active netifs:{}" |
| 74 | + .format(netifaces.interfaces())) |
| 75 | + time.sleep(0.5) |
| 76 | + if time.time() > ppp_server_timeout: |
| 77 | + raise ValueError( |
| 78 | + 'ENV_TEST_FAILURE: SLIP netif failed to setup sl0 interface within timeout' |
| 79 | + ) |
| 80 | + # Configure the SLIP interface with IP addresses of both ends |
| 81 | + cmd = ['sudo', 'ifconfig', 'sl0', '10.0.0.1', 'dstaddr', '10.0.0.2'] |
| 82 | + try: |
| 83 | + subprocess.run(cmd, check=True) |
| 84 | + print('SLIP interface configured successfully.') |
| 85 | + except subprocess.CalledProcessError as e: |
| 86 | + print(f'Failed to configure SLIP interface: {e}') |
| 87 | + # Ping the SLIP interface with 5 packets and 10 seconds timeout |
| 88 | + cmd = ['ping', '10.0.0.2', '-c', '5', '-W', '10'] |
| 89 | + try: |
| 90 | + subprocess.run(cmd, check=True) |
| 91 | + print('Pinging SLIP interface successfully.') |
| 92 | + except subprocess.CalledProcessError as e: |
| 93 | + print(f'Failed to ping SLIP interface: {e}') |
| 94 | + raise |
| 95 | + |
| 96 | + finally: |
| 97 | + server_stop.set() |
| 98 | + t.join() |
0 commit comments