1#!/usr/bin/env python3 2# -*- coding: utf-8 -*- 3""" 4Copyright (c) 2024 Huawei Device Co., Ltd. 5Licensed under the Apache License, Version 2.0 (the "License"); 6you may not use this file except in compliance with the License. 7You may obtain a copy of the License at 8 9 http://www.apache.org/licenses/LICENSE-2.0 10 11Unless required by applicable law or agreed to in writing, software 12distributed under the License is distributed on an "AS IS" BASIS, 13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14See the License for the specific language governing permissions and 15limitations under the License. 16 17Description: Action words of hdc fport. 18""" 19 20 21class Fport(object): 22 def __init__(self, driver): 23 self.driver = driver 24 self.retry_times = 3 25 self.increase_step = 7 26 27 def fport_connect_server(self, port, pid, bundle_name): 28 for _ in range(Fport.retry_times): 29 cmd = f"fport tcp:{port} ark:{pid}@{bundle_name}" 30 self.driver.log_info('fport connect server: ' + cmd) 31 result = self.driver.hdc(cmd) 32 self.driver.log_info(result) 33 if result == 'Forwardport result:OK': 34 return port 35 else: # The port may be occupied 36 port += Fport.increase_step 37 return -1 38 39 def fport_debugger_server(self, port, pid, tid=0): 40 for _ in range(Fport.retry_times): 41 if tid == 0: 42 cmd = f"fport tcp:{port} ark:{pid}@Debugger" 43 else: 44 cmd = f"fport tcp:{port} ark:{pid}@{tid}@Debugger" 45 self.driver.log_info('fport_debugger_server: ' + cmd) 46 result = self.driver.hdc(cmd) 47 self.driver.log_info(result) 48 if result == 'Forwardport result:OK': 49 return port 50 else: # The port may be occupied 51 port += Fport.increase_step 52 return -1 53 54 def clear_fport(self): 55 list_fport_cmd = 'fport ls' 56 list_fport_result = self.driver.hdc(list_fport_cmd) 57 self.driver.log_info(list_fport_result) 58 if 'Empty' in list_fport_result: 59 return 60 for fport_item in [item for item in list_fport_result.split('[Forward]') if 'ark' in item]: 61 un_fport_command = (f"fport rm {fport_item.split(' ')[1].split(' ')[0]} " 62 f"{fport_item.split(' ')[1].split(' ')[1]}") 63 un_fport_result = self.driver.hdc(un_fport_command) 64 self.driver.log_info(un_fport_command) 65 self.driver.log_info(un_fport_result) 66 assert 'success' in un_fport_result, un_fport_result 67