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 20import logging 21import subprocess 22 23 24class Fport(object): 25 @classmethod 26 def fport_connect_server(cls, port, pid, bundle_name): 27 cmd = ['hdc', 'fport', f'tcp:{port}', f'ark:{pid}@{bundle_name}'] 28 logging.info('fport connect server: ' + ' '.join(cmd)) 29 result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 30 logging.info(result.stdout.strip()) 31 assert result.stdout.decode('utf-8').strip() == 'Forwardport result:OK' 32 33 @classmethod 34 def fport_debugger_server(cls, port, pid, tid=0): 35 if tid == 0: 36 cmd = ['hdc', 'fport', f'tcp:{port}', f'ark:{pid}@Debugger'] 37 else: 38 cmd = ['hdc', 'fport', f'tcp:{port}', f'ark:{pid}@{tid}@Debugger'] 39 logging.info('fport_debugger_server: ' + ' '.join(cmd)) 40 result = subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 41 logging.info(result.stdout.strip()) 42 assert result.stdout.decode('utf-8').strip() == 'Forwardport result:OK' 43 44 @classmethod 45 def clear_fport(cls): 46 list_fport_cmd = ['hdc', 'fport', 'ls'] 47 list_fport_result = subprocess.run(list_fport_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 48 logging.info(list_fport_result.stdout.strip()) 49 list_fport_out = list_fport_result.stdout.decode('utf-8') 50 if 'Empty' in list_fport_out: 51 return 52 for fport_item in [item for item in list_fport_out.split('[Forward]') if item != '\r\n']: 53 un_fport_command = (['hdc', 'fport', 'rm'] + [fport_item.split(' ')[1].split(' ')[0]] + 54 [fport_item.split(' ')[1].split(' ')[1]]) 55 un_fport_result = subprocess.run(un_fport_command, stdout=subprocess.PIPE, stderr=subprocess.PIPE) 56 logging.info(un_fport_result.stdout.strip()) 57 assert 'success' in un_fport_result.stdout.decode('utf-8') 58