Tuesday, 14 April 2015

adb from tcp client

adb to android from TCP client (Linux)


On a terminal shell type:

    prompt~$ adb start-server
  * daemon not running. starting it now on port 5037 *
  * daemon started successfully * 
  prompt:~$



To connect to ADB shell from a TCP client (as telnet) following sequences are required.

If we connect a telnet to 5037 port we can start sending data as:
####command
The first four #### 'digits' contains the length of the command in hex (but literals). 
For example: (get the device id from adb devices)

001fhost:transport:deviceid
OKAY
006shell:
OKAY
$  <in shell>


To get  adb android shell prompt over a wifi adb
You have to know the adb port and IP address of the android device  ;
Let's say adb on android listens on: 
192.168.1.245:5554

First connect to adb server as in the previous example.
Then we send the host:connect using the same convention 




001Fhost:connect:192.168.1.245:5554
OKAY

Close the connection to adb server running on linux host.Re-open the connection again to adb on 5035 port. Now send

0012host:transport-any
OKAY
0005shell:
 $


The sequence is described down here:

---------------------------------------------
 CLIENT connected to lh:5037       ADB (ETH)

192.168.1.245:5554 is android ip and port
 ---------------------------------------------
001F           --->          

host:connect:192.168.1.245:5554  --->
               <----   OKAY (4 bytes)
# a message coming back with 4 bytes as the length
#  
               <----   0027  (length of message)
               <----   already connected to 192.168.1.245:5554

-- CLOSE SOCKET CONNECTION TO  localhost:5037--
--   reconnect socket to localhost:5037
0012                --->
host:transport-any  --->
                   <---            OKAY
0006                ---->   

shell:              ----->
                                   OKAY
                   <----       promot or  '\r'
 

    <-now     sh commands can be sent->


-----------------------------------------------------



Pseudo code for logging to adb from a tcp client (usb)


// serial usb to device:
 /*  
  all checks for api return codes was removed for clarity  
 */  
 Socket sock;  
 bool adb_send(const char* buffer)  
 {  
   uint8_t  tmp[8] = {0};  
   size_t len = ::strlen(buffer);  
   ::snprintf((char*)tmp, sizeof(tmp), "%04x", len);  
   tmp[4]=0;  
   return sock.send(tmp,4)==4 && sock.esend((uint8_t*)buffer, len)==len;  
 }  
 bool adb_receive(const char* buffer, int size)  
 {  
     ::memset(buffer,0,size);  
   return sock.receive(buffer,size)==size;  
 }  
 char  buffer[512];  
 //  
 // get device id(s)'   
 // { optional  
 //  
 socket.connect("127.0.0.1",5037);  
 socket.set_blocking(true);  
 adb_send("host:devices-l");  
 adb_receive(buffer,4);   //get back 4 bytes. could be 'OKAY' or 'FAIL'. 'OKAY' continue  
 adb_receive(buffer,sizeof(buffer)-1);  
 /*  
  parse line by line and get first token from each line.  
  first token on each line is the device ID. Pick first let's say if  
  there are more than one device on USB  
  buffer for one device looks like  
  0123456789ABCDEF    device usb:2-1.2 product:someprod_dev model:PROD_DEV device:prod_dev  
 */  
 string deviceid ="0123456789ABCDEF";   
 socket.close()  
 // } optional  
 //  
 // connect to shell  
 //  
 socket.connect("127.0.0.1",5037);  
 socket.set_blocking(true);  
 string hoststring = "host:transport:"+deviceid; // would be: "host:transport:0123456789ABCDEF"  
 adb_send(hoststring);  
 adb_receive(buffer,4);   //get back 4 bytes. could be 'OKAY' or 'FAIL'. 'OKAY' continue  
 adb_send("shell:");  
 adb_receive(buffer,4);   //get back 4 bytes. could be 'OKAY' or 'FAIL'. 'OKAY' continue  
 adb_receive(buffer,sizeof(buffer)-1);   
 // the buffer has android prompt, so shell commands can be sent and output received. 
 
 
 
 
 
 

No comments:

Post a Comment