1 // TODO: replace this with a shell/python script. 2 3 /* a simple test program, connects to ADB server, and opens a track-devices session */ 4 #include <errno.h> 5 #include <memory.h> 6 #include <netdb.h> 7 #include <stdio.h> 8 #include <stdlib.h> 9 #include <sys/socket.h> 10 #include <unistd.h> 11 12 #include <android-base/file.h> 13 14 static void 15 panic( const char* msg ) 16 { 17 fprintf(stderr, "PANIC: %s: %s\n", msg, strerror(errno)); 18 exit(1); 19 } 20 21 int main(int argc, char* argv[]) { 22 const char* request = "host:track-devices"; 23 24 if (argv[1] && strcmp(argv[1], "--jdwp") == 0) { 25 request = "track-jdwp"; 26 } 27 28 int ret; 29 struct sockaddr_in server; 30 char buffer[1024]; 31 32 memset( &server, 0, sizeof(server) ); 33 server.sin_family = AF_INET; 34 server.sin_port = htons(5037); 35 server.sin_addr.s_addr = htonl(INADDR_LOOPBACK); 36 37 int s = socket( PF_INET, SOCK_STREAM, 0 ); 38 ret = connect( s, (struct sockaddr*) &server, sizeof(server) ); 39 if (ret < 0) panic( "could not connect to server" ); 40 41 /* send the request */ 42 int len = snprintf(buffer, sizeof(buffer), "%04zx%s", strlen(request), request); 43 if (!android::base::WriteFully(s, buffer, len)) 44 panic( "could not send request" ); 45 46 /* read the OKAY answer */ 47 if (!android::base::ReadFully(s, buffer, 4)) 48 panic( "could not read request" ); 49 50 printf( "server answer: %.*s\n", 4, buffer ); 51 52 /* now loop */ 53 while (true) { 54 char head[5] = "0000"; 55 56 if (!android::base::ReadFully(s, head, 4)) 57 panic("could not read length"); 58 59 int len; 60 if (sscanf(head, "%04x", &len) != 1 ) 61 panic("could not decode length"); 62 63 if (!android::base::ReadFully(s, buffer, len)) 64 panic("could not read data"); 65 66 printf( "received header %.*s (%d bytes):\n%.*s----\n", 4, head, len, len, buffer ); 67 } 68 close(s); 69 } 70