def stop_adb_server(self):
        try:
            cmd_line = "adb kill-server"

            if self._logger:
                self._logger.debug("[ADB-SERVER] Stopping ADB server ...")

            status, status_msg = internal_shell_exec(cmd_line, 5)

            if status == Global.SUCCESS:
                self._adb_server_is_running = False
            else:
                if self._logger:
                    self._logger.error(
                        "[ADB-SERVER] Stopping failure (status: %s)" %
                        str(status_msg))

            if not self._adb_server_is_running:
                if self._logger:
                    self._logger.debug("[ADB-SERVER] ADB server stopped")

        except Exception as error:  # pylint: disable=W0703
            if self._logger:
                self._logger.error(
                    "[ADB-SERVER] Stopping failure; adb not found (cmd: %s, error: %s)"
                    % (str(cmd_line), str(error)))
            raise
    def adb_ethernet_stop(self, ip_address, port, timeout=10):
        """
        Stop adb server and disconnect from adbd
        """
        is_disconnected = False

        if self._logger:
            self._logger.debug("[ADB-SERVER] stopping and disconnecting over ETHERNET (IP: %s, Port: %s)..." %
                               (ip_address, port))

        try:
            cmd_line = "adb disconnect %s:%s" % (ip_address, port)
            status, return_msg = internal_shell_exec(cmd_line, timeout)

            if status == Global.SUCCESS:
                if return_msg.startswith("") or return_msg.startswith("No such"):
                    msg = "[ADB-SERVER] stopped and disconnected from DUT (%s)" % return_msg
                    is_disconnected = True
                else:
                    msg = "[ADB-SERVER] stopped but connection to DUT status is unknown (%s)" % return_msg
            else:
                msg = "[ADB-SERVER] Stop failure (status: %s)" % str(return_msg)

            if self._logger:
                self._logger.debug(msg)

        except Exception as error:  # pylint: disable=W0703
            if self._logger:
                self._logger.debug("[ADB-SERVER] fail to stop over ETHERNET (%s)" % str(error))

        return is_disconnected
    def adb_ethernet_start(self, ip_address, port, retries=3, timeout=10):
        """
        Start adb server and connect to adbd
        :type  ip_address: str
        :param ip_address: ip address of the device

        :type  port: str
        :param port: port number to connect to device adb client

        :type max_retry_number: int
        :param max_retry_number: max number of tries for adb connection with the device

        :rtype: bool
        :return: adb connection state (True if adb is connected else False)
        """
        is_connected = False
        current_try = 1
        if self._logger:
            self._logger.debug("[ADB-SERVER] starting and connecting over ETHERNET (IP: %s, Port: %s)..." %
                               (ip_address, port))

        while current_try <= retries and not is_connected:
            try:
                msg = "adb connect KO"
                cmd = "adb connect %s:%s" % (ip_address, port)
                result, return_msg = internal_shell_exec(cmd, timeout)

                if result == Global.SUCCESS:
                    if return_msg.startswith("unable"):
                        msg = "[ADB-SERVER] Fail to connect to DUT (%s)" % return_msg
                    elif "already" in return_msg or "connected" in return_msg:
                        # In ethernet, we also need check adb connection with adb shell presence
                        msg = "[ADB-SERVER] Connected to DUT (%s)" % return_msg

                        run_cmd_code, run_cmd_msg = self.run_cmd("adb shell echo", 10, True)

                        if run_cmd_code == Global.FAILURE:
                            msg += "/ adb shell NOK (%s)" % run_cmd_msg
                        else:
                            msg += "/ adb shell OK (%s)" % run_cmd_msg
                            is_connected = True
                    else:
                        msg = "[ADB-SERVER] Connection to DUT status is unknown (%s)" % return_msg
                else:
                    msg = "Connect to adb server has failed"

                if self._logger:
                    self._logger.debug(msg)

            except Exception as error:  # pylint: disable=W0703
                if self._logger:
                    self._logger.debug("[ADB-SERVER] started but fail to connect over ETHERNET (%s)" % str(error))

            finally:
                # increment the connection retry number
                current_try += 1

        return is_connected
    def adb_start(self, timeout=10):
        """Start adb server
        """

        self.__lock.acquire()
        cmd_line = "adb start-server"
        self._adb_server_is_running = False

        try:
            # WA to integrate the patch until all server are killed
            # self._adb_host_port = self.get_adb_server_port(port=self._adb_host_port,
            #                                               retry=5)
            os.environ["ANDROID_ADB_SERVER_PORT"] = str(self._adb_host_port)
            self.stop_adb_server()

            if self._logger:
                self._logger.debug(
                    "[ADB-SERVER] Starting ADB server on {0}...".format(
                        self._adb_host_port))
            # below sleep time gives some time for adb to respond after
            # "stop_adb_server()"
            time.sleep(1)
            status, status_msg = internal_shell_exec(cmd_line, timeout)

            if status == Global.SUCCESS:
                self._adb_server_is_running = True
                if self._logger:
                    self._logger.debug("[ADB-SERVER] ADB server started")
            else:
                if self._logger:
                    self._logger.error(
                        "[ADB-SERVER] Starting failure (status: %s)" %
                        str(status_msg))

        except Exception as error:  # pylint: disable=W0703
            if self._logger:
                self._logger.error(
                    "[ADB-SERVER] Starting failure; adb not found (cmd: %s, error: %s)"
                    % (str(cmd_line), str(error)))
            raise
        finally:
            self.__lock.release()
Esempio n. 5
0
    def run(self, context):
        """
        Runs the test step

        :type context: TestStepContext
        :param context: test case context

        Context parameters list:
        - cmd: command to be run
        - timeout: script execution timeout in sec
        - silent_mode: Display logs in ACS logger
        - save_as: context variable name where stdout of the command is put
        """
        TestStepBase.run(self, context)

        # Fetch params values
        command = self._pars.command
        timeout = self._pars.timeout
        silent_mode = self._pars.silent_mode
        command_result = self._pars.save_as

        exit_status, output = internal_shell_exec(cmd=command,
                                                  timeout=timeout,
                                                  silent_mode=silent_mode)
        if not silent_mode:
            self._logger.info(
                "RunLocalCommand: output for {0} command: '{1}'".format(
                    command, output))

        if exit_status == Global.FAILURE:
            raise TestEquipmentException(
                TestEquipmentException.COMMAND_LINE_ERROR,
                "RunCommand: Error at execution of '{0}' command: {1}".format(
                    command, output))

        context.set_info(command_result, output)