1# 2# Copyright (C) 2016 The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15# 16 17import logging 18 19from vts.runners.host import errors 20from vts.proto import AndroidSystemControlMessage_pb2 as ASysCtrlMsg 21from vts.runners.host.tcp_client import vts_tcp_client 22from vts.utils.python.mirror import shell_mirror_object 23 24 25class ShellMirror(object): 26 """The class that acts as the mirror to an Android device's shell terminal. 27 28 This class holds and manages the life cycle of multiple mirror objects that 29 map to different shell instances. 30 31 Attributes: 32 _host_command_port: int, the host-side port for command-response 33 sessions. 34 _shell_mirrors: dict, key is instance name, value is mirror object. 35 """ 36 37 def __init__(self, host_command_port): 38 self._shell_mirrors = {} 39 self._host_command_port = host_command_port 40 41 def __del__(self): 42 for instance_name in self._shell_mirrors: 43 self.RemoveShell(instance_name) 44 self._shell_mirrors = {} 45 46 def RemoveShell(self, instance_name): 47 """Removes a shell mirror object. 48 49 Args: 50 instance_name: string, the shell terminal instance name. 51 """ 52 shell_mirror = self._shell_mirrors[instance_name] 53 shell_mirror.CleanUp() 54 55 def InvokeTerminal(self, instance_name, bits=32): 56 """Initiates a handler for a particular conventional HAL. 57 58 This will initiate a driver service for a shell on the target side, 59 create the top level mirror object for the same, and register it in 60 the manager. 61 62 Args: 63 instance_name: string, the shell terminal instance name. 64 bits: integer, processor architecture indicator: 32 or 64. 65 """ 66 if not instance_name: 67 raise error.ComponentLoadingError("instance_name is None") 68 if bits not in [32, 64]: 69 raise error.ComponentLoadingError("Invalid value for bits: %s" % bits) 70 71 client = vts_tcp_client.VtsTcpClient() 72 client.Connect(command_port=self._host_command_port) 73 74 logging.info("Init the driver service for shell, %s", instance_name) 75 launched = client.LaunchDriverService( 76 driver_type=ASysCtrlMsg.VTS_DRIVER_TYPE_SHELL, 77 service_name="shell_" + instance_name, 78 bits=bits) 79 80 if not launched: 81 raise errors.ComponentLoadingError( 82 "Failed to launch shell driver service %s" % instance_name) 83 84 mirror_object = shell_mirror_object.ShellMirrorObject(client) 85 self._shell_mirrors[instance_name] = mirror_object 86 87 def __getattr__(self, name): 88 return self._shell_mirrors[name] 89