예제 #1
0
    def InvokeTerminal(self, instance_name, bits=32):
        """Initiates a handler for a particular shell terminal.

        This will initiate a driver service for a shell on the target side,
        create a mirror object for the shell, and register it in the tracker.

        Args:
            instance_name: string, the shell terminal instance name.
            bits: integer, processor architecture indicator: 32 or 64.
        """
        if not instance_name:
            raise error.ComponentLoadingError("instance_name is None")
        if bits not in [32, 64]:
            raise error.ComponentLoadingError(
                "Invalid value for bits: %s" % bits)

        if instance_name in self._registered_mirrors:
            logging.warning("shell driver %s already exists", instance_name)
            return

        client = vts_tcp_client.VtsTcpClient()
        client.Connect(command_port=self._host_command_port)

        logging.debug("Init the driver service for shell, %s", instance_name)
        launched = client.LaunchDriverService(
            driver_type=ASysCtrlMsg.VTS_DRIVER_TYPE_SHELL,
            service_name="shell_" + instance_name,
            bits=bits)

        if not launched:
            raise errors.ComponentLoadingError(
                "Failed to launch shell driver service %s" % instance_name)

        mirror = shell_mirror.ShellMirror(client, self._adb)
        self._registered_mirrors[instance_name] = mirror
예제 #2
0
    def InitHidlHal(self,
                    target_type,
                    target_version=None,
                    target_package=None,
                    target_component_name=None,
                    target_basepaths=_DEFAULT_TARGET_BASE_PATHS,
                    handler_name=None,
                    hw_binder_service_name=_DEFAULT_HWBINDER_SERVICE,
                    bits=64,
                    target_version_major=None,
                    target_version_minor=None,
                    is_test_hal=False):
        """Initiates a handler for a particular HIDL HAL.

        This will initiate a driver service for a HAL on the target side, create
        a mirror object for a HAL, and register it in the tracker.

        Args:
            target_type: string, the target type name (e.g., light, camera).
            target_version (deprecated, now use major and minor versions):
              float, the target component version (e.g., 1.0).
            target_package: string, the package name of a target HIDL HAL.
            target_basepaths: list of strings, the paths to look for target
                              files in. Default is _DEFAULT_TARGET_BASE_PATHS.
            handler_name: string, the name of the handler. target_type is used
                          by default.
            hw_binder_service_name: string, the name of a HW binder service.
            bits: integer, processor architecture indicator: 32 or 64.
            target_version_major:
              int, the target component major version (e.g., 1.0 -> 1).
            target_version_minor:
              int, the target component minor version (e.g., 1.0 -> 0).
              If host doesn't provide major and minor versions separately,
              parse it from the float version of target_version.
            is_test_hal: bool, whether the HAL service is a test HAL
                         (e.g. msgq).

        Raises:
            USERError if user doesn't provide a version of the HAL service.
        """
        target_version_major, target_version_minor = self.GetTargetVersion(
            target_version, target_version_major, target_version_minor)
        if not handler_name:
            handler_name = target_type
        client = vts_tcp_client.VtsTcpClient()
        client.Connect(
            command_port=self._host_command_port,
            callback_port=self._host_callback_port)
        mirror = hal_mirror.HalMirror(client, self._callback_server)
        mirror.InitHalDriver(target_type, target_version_major,
                             target_version_minor, target_package,
                             target_component_name, hw_binder_service_name,
                             handler_name, bits, is_test_hal)
        self._registered_mirrors[target_type] = mirror
예제 #3
0
    def InitHidlHandleForSingleFile(self,
                                    filepath,
                                    mode,
                                    ints=[],
                                    client=None,
                                    handle_name=None):
        """Initialize a hidl_handle object.

        This method will initialize a hidl_handle object on the target side,
        create a mirror object, and register it in the tracker.
        TODO: Currently only support creating a handle for a single file.
        In the future, need to support arbitrary file descriptor types
        (e.g. socket, pipe), and more than one file.

        Args:
            filepath: string, path to the file.
            mode: string, specifying the mode to open the file.
            ints: int list, useful integers to be stored in handle object.
            client: VtsTcpClient, if an existing session should be used.
                If not specified, create a new one.
            handle_name: string, name of the handle object.
                If not specified, dynamically assign the handle object a name.

        Returns:
            ResourceHidlHandleMirror object,
            it allows users to directly call methods on the mirror object.
        """
        # Check if handle_name already exists in tracker.
        if handle_name is not None and handle_name in self._registered_mirrors:
            logging.error("Handle name already exists in tracker.")
            return None

        # Need to initialize a client if caller doesn't provide one.
        if not client:
            client = vts_tcp_client.VtsTcpClient()
            client.Connect(
                command_port=self._host_command_port,
                callback_port=self._host_callback_port)

        # Create a resource_mirror object.
        mirror = resource_mirror.ResourceHidlHandleMirror(client)
        mirror._createHandleForSingleFile(filepath, mode, ints)
        if mirror.handleId == -1:
            # Failed to create handle object, error logged in resource_mirror.
            return None

        # Need to dynamically assign a handle name
        # if caller doesn't provide one.
        if handle_name is None:
            handle_name = "handle_id_" + str(mirror._handle_id)
        self._registered_mirrors[handle_name] = mirror
        return mirror
예제 #4
0
    def InitSharedLib(self,
                      target_type,
                      target_version=None,
                      target_basepaths=_DEFAULT_TARGET_BASE_PATHS,
                      target_package="",
                      target_filename=None,
                      handler_name=None,
                      bits=64,
                      target_version_major=None,
                      target_version_minor=None):
        """Initiates a handler for a particular lib.

        This will initiate a driver service for a lib on the target side, create
        a mirror object for a lib, and register it in the tracker.

        Args:
            target_type: string, the target type name (e.g., light, camera).
            target_version (deprecated, now use major and minor versions):
              float, the target component version (e.g., 1.0).
            target_basepaths: list of strings, the paths to look for target
                             files in. Default is _DEFAULT_TARGET_BASE_PATHS.
            target_package: . separated string (e.g., a.b.c) to denote the
                            package name of target component.
            target_filename: string, the target file name (e.g., libm.so).
            handler_name: string, the name of the handler. target_type is used
                          by default.
            bits: integer, processor architecture indicator: 32 or 64.
            target_version_major:
              int, the target component major version (e.g., 1.0 -> 1).
            target_version_minor:
              int, the target component minor version (e.g., 1.0 -> 0).
            If host doesn't provide major and minor versions separately,
            parse it from the float version of target_version.

        Raises:
            USERError if user doesn't provide a version of the HAL service.
        """
        target_version_major, target_version_minor = self.GetTargetVersion(
            target_version, target_version_major, target_version_minor)
        if not handler_name:
            handler_name = target_type
        client = vts_tcp_client.VtsTcpClient()
        client.Connect(command_port=self._host_command_port)
        mirror = lib_mirror.LibMirror(client)
        mirror.InitLibDriver(target_type, target_version_major,
                             target_version_minor, target_package,
                             target_filename, target_basepaths, handler_name,
                             bits)
        self._registered_mirrors[handler_name] = mirror
예제 #5
0
    def InitHidlMemory(self, mem_size=0, client=None, mem_name=None):
        """Initialize a hidl_memory object.

        This method will initialize a hidl_memory object on the target side,
        create a mirror object, and register it in the tracker.

        Args:
            mem_size: int, size of the memory region.
            client: VtsTcpClient, if an existing session should be used.
                If not specified, creates a new one.
            mem_name: string, name of the memory region.
                If not specified, dynamically assign the memory region a name.

        Returns:
            ResourceHidlMemoryMirror object,
            it allows users to directly call methods on the mirror object.
        """
        # Check if mem_name already exists in tracker.
        if mem_name is not None and mem_name in self._registered_mirrors:
            logging.error("Memory name already exists in tracker.")
            return None

        # Need to initialize a client if caller doesn't provide one.
        if client is None:
            client = vts_tcp_client.VtsTcpClient()
            client.Connect(
                command_port=self._host_command_port,
                callback_port=self._host_callback_port)

        # Create a resource_mirror object.
        mirror = resource_mirror.ResourceHidlMemoryMirror(client)
        mirror._allocate(mem_size)
        if mirror.memId == -1:
            # Failed to create memory object, error logged in resource_mirror.
            return None

        # Need to dynamically assign a memory name
        # if caller doesn't provide one.
        if mem_name is None:
            mem_name = "mem_id_" + str(mirror._mem_id)
        self._registered_mirrors[mem_name] = mirror
        return mirror
예제 #6
0
    def InitFmq(self,
                existing_queue=None,
                new_queue_name=None,
                data_type="uint16_t",
                sync=True,
                queue_size=0,
                blocking=False,
                reset_pointers=True,
                client=None):
        """Initializes a fast message queue object.

        This method will initialize a fast message queue object on the target side,
        create a mirror object for the FMQ, and register it in the tracker.

        Args:
            existing_queue: string or MirrorObject.
                This argument identifies an existing queue mirror object.
                If specified, it will tell the target driver to create a
                new message queue object based on an existing message queue.
                If it is None, that means creating a brand new message queue.
            new_queue_name: string, name of the new queue, used as key in the tracker.
                If not specified, this function dynamically generates a name.
            data_type: string, type of data in the queue.
            sync: bool, whether the queue is synchronized (only has one reader).
            queue_size: int, size of the queue.
            blocking: bool, whether blocking is enabled.
            reset_pointers: bool, whether to reset read/write pointers when
                creating a new message queue object based on an existing message queue.
            client: VtsTcpClient, if an existing session should be used.
                If not specified, creates a new one.

        Returns:
            ResourcFmqMirror object,
            it allows users to directly call methods on the mirror object.
        """
        # Check if queue name already exists in tracker.
        if new_queue_name is not None and new_queue_name in self._registered_mirrors:
            logging.error("Queue name already exists in tracker.")
            return None

        # Need to initialize a client if caller doesn't provide one.
        if client is None:
            client = vts_tcp_client.VtsTcpClient()
            client.Connect(
                command_port=self._host_command_port,
                callback_port=self._host_callback_port)

        # Create a new queue by default.
        existing_queue_id = -1
        # Check if caller wants to create a queue object based on
        # an existing queue object.
        if existing_queue is not None:
            # Check if caller provides a string.
            if type(existing_queue) == str:
                if existing_queue in self._registered_mirrors:
                    data_type = self._registered_mirrors[
                        existing_queue].dataType
                    sync = self._registered_mirrors[
                        existing_queue].sync
                    existing_queue_id = self._registered_mirrors[
                        existing_queue].queueId
                else:
                    logging.error("Nonexisting queue name in mirror_tracker.")
                    return None
            # Check if caller provides a resource mirror object.
            elif isinstance(existing_queue, resource_mirror.ResourceFmqMirror):
                data_type = existing_queue.dataType
                sync = existing_queue.sync
                existing_queue_id = existing_queue.queueId
            else:
                logging.error(
                    "Unsupported way of finding an existing queue object.")
                return None

        # Create a resource mirror object.
        mirror = resource_mirror.ResourceFmqMirror(data_type, sync, client)
        mirror._create(existing_queue_id, queue_size, blocking, reset_pointers)
        if mirror.queueId == -1:
            # Failed to create queue object, error logged in resource_mirror.
            return None

        # Needs to dynamically generate queue name if caller doesn't provide one
        if new_queue_name is None:
            new_queue_name = "queue_id_" + str(mirror._queue_id)
        self._registered_mirrors[new_queue_name] = mirror
        return mirror
    def _CreateMirrorObject(self,
                            target_class,
                            target_type,
                            target_version,
                            target_package=None,
                            target_component_name=None,
                            target_basepaths=_DEFAULT_TARGET_BASE_PATHS,
                            handler_name=None,
                            hw_binder_service_name=None,
                            bits=64):
        """Initiates the driver for a HAL on the target device and creates a top
        level MirroObject for it.

        Also starts the callback server to listen for callback invocations.

        Args:
            target_class: string, the target class name (e.g., hal).
            target_type: string, the target type name (e.g., light, camera).
            target_version: float, the target component version (e.g., 1.0).
            target_package: string, the package name of a HIDL HAL.
            target_component_name: string, the name of a target component.
            target_basepaths: list of strings, the paths to look for target
                             files in. Default is _DEFAULT_TARGET_BASE_PATHS.
            handler_name: string, the name of the handler. target_type is used
                          by default.
            hw_binder_service_name: string, the name of a HW binder service.
            bits: integer, processor architecture indicator: 32 or 64.

        Raises:
            errors.ComponentLoadingError is raised when error occurs trying to
            create a MirrorObject.
        """
        if bits not in [32, 64]:
            raise error.ComponentLoadingError("Invalid value for bits: %s" %
                                              bits)
        self._StartCallbackServer()
        self._client = vts_tcp_client.VtsTcpClient()
        self._client.Connect(command_port=self._host_command_port,
                             callback_port=self._host_callback_port)
        if not handler_name:
            handler_name = target_type
        service_name = "vts_driver_%s" % handler_name

        target_filename = None
        if target_class == "hal_conventional" or target_class == "hal_legacy":
            # Get all the HALs available on the target.
            hal_list = self._client.ListHals(target_basepaths)
            if not hal_list:
                raise errors.ComponentLoadingError(
                    "Could not find any HAL under path %s" % target_basepaths)
            logging.debug(hal_list)

            # Find the corresponding filename for HAL target type.
            for name in hal_list:
                if target_type in name:
                    # TODO: check more exactly (e.g., multiple hits).
                    target_filename = name
            if not target_filename:
                raise errors.ComponentLoadingError(
                    "No file found for HAL target type %s." % target_type)

            # Check whether the requested binder service is already running.
            # if client.CheckDriverService(service_name=service_name):
            #     raise errors.ComponentLoadingError("A driver for %s already exists" %
            #                                        service_name)
        elif target_class == "hal_hidl":
            # TODO: either user the default hw-binder service or start a new
            # service (e.g., if an instrumented binary is used).
            pass

        # Launch the corresponding driver of the requested HAL on the target.
        logging.info("Init the driver service for %s", target_type)
        target_class_id = COMPONENT_CLASS_DICT[target_class.lower()]
        target_type_id = COMPONENT_TYPE_DICT[target_type.lower()]
        driver_type = {
            "hal_conventional": ASysCtrlMsg.VTS_DRIVER_TYPE_HAL_CONVENTIONAL,
            "hal_legacy": ASysCtrlMsg.VTS_DRIVER_TYPE_HAL_LEGACY,
            "hal_hidl": ASysCtrlMsg.VTS_DRIVER_TYPE_HAL_HIDL
        }.get(target_class)

        launched = self._client.LaunchDriverService(
            driver_type=driver_type,
            service_name=service_name,
            bits=bits,
            file_path=target_filename,
            target_class=target_class_id,
            target_type=target_type_id,
            target_version=target_version,
            target_package=target_package,
            target_component_name=target_component_name,
            hw_binder_service_name=hw_binder_service_name)

        if not launched:
            raise errors.ComponentLoadingError(
                "Failed to launch driver service %s from file path %s" %
                (target_type, target_filename))

        # Create API spec message.
        found_api_spec = self._client.ListApis()
        if not found_api_spec:
            raise errors.ComponentLoadingError("No API found for %s" %
                                               service_name)
        logging.debug("Found %d APIs for %s:\n%s", len(found_api_spec),
                      service_name, found_api_spec)
        if_spec_msg = CompSpecMsg.ComponentSpecificationMessage()
        text_format.Merge(found_api_spec, if_spec_msg)

        # Instantiate a MirrorObject and return it.
        hal_mirror = mirror_object.MirrorObject(self._client, if_spec_msg,
                                                self._callback_server)
        self._hal_level_mirrors[handler_name] = hal_mirror
예제 #8
0
    def _CreateMirrorObject(self,
                            target_class,
                            target_type,
                            target_version,
                            target_basepaths=_DEFAULT_TARGET_BASE_PATHS,
                            target_package="",
                            target_filename=None,
                            handler_name=None,
                            bits=64):
        """Initiates the driver for a lib on the target device and creates a top
        level MirroObject for it.

        Args:
            target_class: string, the target class name (e.g., lib).
            target_type: string, the target type name (e.g., light, camera).
            target_version: float, the target component version (e.g., 1.0).
            target_basepaths: list of strings, the paths to look for target
                             files in. Default is _DEFAULT_TARGET_BASE_PATHS.
            target_package: . separated string (e.g., a.b.c) to denote the
                            package name of target component.
            target_filename: string, the target file name (e.g., libm.so).
            handler_name: string, the name of the handler. target_type is used
                          by default.
            bits: integer, processor architecture indicator: 32 or 64.

        Raises:
            errors.ComponentLoadingError is raised when error occurs trying to
            create a MirrorObject.
        """
        if bits not in [32, 64]:
            raise error.ComponentLoadingError("Invalid value for bits: %s" % bits)
        client = vts_tcp_client.VtsTcpClient()
        client.Connect(command_port=self._host_command_port)
        if not handler_name:
            handler_name = target_type
        service_name = "vts_driver_%s" % handler_name

        # Get all the libs available on the target.
        lib_list = client.ListHals(target_basepaths)
        if not lib_list:
            raise errors.ComponentLoadingError(
                "Could not find any lib under path %s" % target_basepaths)
        logging.debug(lib_list)

        # Find the corresponding filename for Lib target type.
        if target_filename is not None:
            for name in lib_list:
                if name.endswith(target_filename):
                    target_filename = name
                    break
        else:
          for name in lib_list:
              if target_type in name:
                  # TODO: check more exactly (e.g., multiple hits).
                  target_filename = name

        if not target_filename:
            raise errors.ComponentLoadingError(
                "No file found for target type %s." % target_type)

        # Check whether the requested binder service is already running.
        # if client.CheckDriverService(service_name=service_name):
        #     raise errors.ComponentLoadingError("A driver for %s already exists" %
        #                                        service_name)

        # Launch the corresponding driver of the requested Lib on the target.
        logging.info("Init the driver service for %s", target_type)
        target_class_id = hal_mirror.COMPONENT_CLASS_DICT[target_class.lower()]
        target_type_id = hal_mirror.COMPONENT_TYPE_DICT[target_type.lower()]
        launched = client.LaunchDriverService(
            driver_type=ASysCtrlMsg.VTS_DRIVER_TYPE_HAL_CONVENTIONAL,
            service_name=service_name,
            bits=bits,
            file_path=target_filename,
            target_class=target_class_id,
            target_type=target_type_id,
            target_version=target_version,
            target_package=target_package)

        if not launched:
            raise errors.ComponentLoadingError(
                "Failed to launch driver service %s from file path %s" %
                (target_type, target_filename))

        # Create API spec message.
        found_api_spec = client.ListApis()
        if not found_api_spec:
            raise errors.ComponentLoadingError("No API found for %s" %
                                               service_name)
        logging.debug("Found %d APIs for %s:\n%s", len(found_api_spec),
                      service_name, found_api_spec)
        if_spec_msg = CompSpecMsg.ComponentSpecificationMessage()
        text_format.Merge(found_api_spec, if_spec_msg)

        # Instantiate a MirrorObject and return it.
        lib_mirror = mirror_object.MirrorObject(client, if_spec_msg, None)
        self._lib_level_mirrors[handler_name] = lib_mirror