1# Copyright 2021-2022 Google LLC 2# 3# Licensed under the Apache License, Version 2.0 (the "License"); 4# you may not use this file except in compliance with the License. 5# You may obtain a copy of the License at 6# 7# https://www.apache.org/licenses/LICENSE-2.0 8# 9# Unless required by applicable law or agreed to in writing, software 10# distributed under the License is distributed on an "AS IS" BASIS, 11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12# See the License for the specific language governing permissions and 13# limitations under the License. 14 15# ----------------------------------------------------------------------------- 16# Imports 17# ----------------------------------------------------------------------------- 18import logging 19 20from .common import Transport, AsyncPipeSink 21from ..link import RemoteLink 22from ..controller import Controller 23 24# ----------------------------------------------------------------------------- 25# Logging 26# ----------------------------------------------------------------------------- 27logger = logging.getLogger(__name__) 28 29 30# ----------------------------------------------------------------------------- 31async def open_transport(name): 32 ''' 33 Open a transport by name. 34 The name must be <type>:<parameters> 35 Where <parameters> depend on the type (and may be empty for some types). 36 The supported types are: serial,udp,tcp,pty,usb 37 ''' 38 scheme, *spec = name.split(':', 1) 39 if scheme == 'serial' and spec: 40 from .serial import open_serial_transport 41 return await open_serial_transport(spec[0]) 42 elif scheme == 'udp' and spec: 43 from .udp import open_udp_transport 44 return await open_udp_transport(spec[0]) 45 elif scheme == 'tcp-client' and spec: 46 from .tcp_client import open_tcp_client_transport 47 return await open_tcp_client_transport(spec[0]) 48 elif scheme == 'tcp-server' and spec: 49 from .tcp_server import open_tcp_server_transport 50 return await open_tcp_server_transport(spec[0]) 51 elif scheme == 'ws-client' and spec: 52 from .ws_client import open_ws_client_transport 53 return await open_ws_client_transport(spec[0]) 54 elif scheme == 'ws-server' and spec: 55 from .ws_server import open_ws_server_transport 56 return await open_ws_server_transport(spec[0]) 57 elif scheme == 'pty': 58 from .pty import open_pty_transport 59 return await open_pty_transport(spec[0] if spec else None) 60 elif scheme == 'file': 61 from .file import open_file_transport 62 return await open_file_transport(spec[0] if spec else None) 63 elif scheme == 'vhci': 64 from .vhci import open_vhci_transport 65 return await open_vhci_transport(spec[0] if spec else None) 66 elif scheme == 'hci-socket': 67 from .hci_socket import open_hci_socket_transport 68 return await open_hci_socket_transport(spec[0] if spec else None) 69 elif scheme == 'usb': 70 from .usb import open_usb_transport 71 return await open_usb_transport(spec[0] if spec else None) 72 elif scheme == 'pyusb': 73 from .pyusb import open_pyusb_transport 74 return await open_pyusb_transport(spec[0] if spec else None) 75 elif scheme == 'android-emulator': 76 from .android_emulator import open_android_emulator_transport 77 return await open_android_emulator_transport(spec[0] if spec else None) 78 else: 79 raise ValueError('unknown transport scheme') 80 81 82# ----------------------------------------------------------------------------- 83async def open_transport_or_link(name): 84 if name.startswith('link-relay:'): 85 link = RemoteLink(name[11:]) 86 await link.wait_until_connected() 87 controller = Controller('remote', link = link) 88 89 class LinkTransport(Transport): 90 async def close(self): 91 link.close() 92 93 return LinkTransport(controller, AsyncPipeSink(controller)) 94 else: 95 return await open_transport(name) 96