AdbUtils.java revision 56d7e400ece64591685c8a21dbb82a94a7bd8010
1/* 2 * Copyright (C) 2010 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 17package com.android.dumprendertree2.forwarder; 18 19import android.util.Log; 20 21import java.io.IOException; 22import java.io.InputStream; 23import java.io.OutputStream; 24import java.net.Socket; 25 26/** 27 * The utility class that can setup a socket allowing the device to communicate with remote 28 * machines through the machine that the device is connected to via adb. 29 */ 30public class AdbUtils { 31 private static final String LOG_TAG = "AdbUtils"; 32 33 private static final String ADB_OK = "OKAY"; 34 private static final int ADB_PORT = 5037; 35 private static final String ADB_HOST = "127.0.0.1"; 36 private static final int ADB_RESPONSE_SIZE = 4; 37 38 /** 39 * Send an ADB command using existing socket connection 40 * 41 * The streams provided must be from a socket connected to adb already 42 * 43 * @param is input stream of the socket connection 44 * @param os output stream of the socket 45 * @param cmd the adb command to send 46 * @return if adb gave a success response 47 * @throws IOException 48 */ 49 private static boolean sendAdbCmd(InputStream is, OutputStream os, String cmd) 50 throws IOException { 51 byte[] buf = new byte[ADB_RESPONSE_SIZE]; 52 53 cmd = String.format("%04X", cmd.length()) + cmd; 54 os.write(cmd.getBytes()); 55 int read = is.read(buf); 56 if (read != ADB_RESPONSE_SIZE || !ADB_OK.equals(new String(buf))) { 57 Log.w(LOG_TAG, "adb cmd faild."); 58 return false; 59 } 60 return true; 61 } 62 63 /** 64 * Get a tcp socket connection to specified IP address and port proxied by adb 65 * 66 * The proxying is transparent, e.g. if a socket is returned, then it can be written to and 67 * read from as if it is directly connected to the target 68 * 69 * @param remoteAddress IP address of the host to connect to 70 * @param remotePort port of the host to connect to 71 * @return a valid Socket instance if successful, null otherwise 72 */ 73 public static Socket getSocketToRemoteMachine(String remoteAddress, int remotePort) { 74 try { 75 Socket socket = new Socket(ADB_HOST, ADB_PORT); 76 String cmd = "tcp:" + remotePort + ":" + remoteAddress; 77 if (!sendAdbCmd(socket.getInputStream(), socket.getOutputStream(), cmd)) { 78 socket.close(); 79 return null; 80 } 81 return socket; 82 } catch (IOException ioe) { 83 Log.w(LOG_TAG, "error creating adb socket", ioe); 84 return null; 85 } 86 } 87}