Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 1 | // TODO: replace this with a shell/python script. |
| 2 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 3 | /* a simple test program, connects to ADB server, and opens a track-devices session */ |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 4 | #include <errno.h> |
| 5 | #include <memory.h> |
Dan Albert | 80e45e7 | 2015-08-11 16:37:04 -0700 | [diff] [blame] | 6 | #include <netdb.h> |
| 7 | #include <stdio.h> |
| 8 | #include <stdlib.h> |
| 9 | #include <sys/socket.h> |
| 10 | #include <unistd.h> |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 11 | |
Elliott Hughes | f55ead9 | 2015-12-04 22:00:26 -0800 | [diff] [blame] | 12 | #include <android-base/file.h> |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 13 | |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 14 | static void |
| 15 | panic( const char* msg ) |
| 16 | { |
| 17 | fprintf(stderr, "PANIC: %s: %s\n", msg, strerror(errno)); |
| 18 | exit(1); |
| 19 | } |
| 20 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 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"; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 26 | } |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 27 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 28 | int ret; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 29 | struct sockaddr_in server; |
| 30 | char buffer[1024]; |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 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 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 37 | int s = socket( PF_INET, SOCK_STREAM, 0 ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 38 | ret = connect( s, (struct sockaddr*) &server, sizeof(server) ); |
| 39 | if (ret < 0) panic( "could not connect to server" ); |
| 40 | |
| 41 | /* send the request */ |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 42 | int len = snprintf(buffer, sizeof(buffer), "%04zx%s", strlen(request), request); |
| 43 | if (!android::base::WriteFully(s, buffer, len)) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 44 | panic( "could not send request" ); |
| 45 | |
| 46 | /* read the OKAY answer */ |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 47 | if (!android::base::ReadFully(s, buffer, 4)) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 48 | panic( "could not read request" ); |
| 49 | |
| 50 | printf( "server answer: %.*s\n", 4, buffer ); |
| 51 | |
| 52 | /* now loop */ |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 53 | while (true) { |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 54 | char head[5] = "0000"; |
| 55 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 56 | if (!android::base::ReadFully(s, head, 4)) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 57 | panic("could not read length"); |
| 58 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 59 | int len; |
| 60 | if (sscanf(head, "%04x", &len) != 1 ) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 61 | panic("could not decode length"); |
| 62 | |
Elliott Hughes | 88b4c85 | 2015-04-30 17:32:03 -0700 | [diff] [blame] | 63 | if (!android::base::ReadFully(s, buffer, len)) |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 64 | panic("could not read data"); |
| 65 | |
Elliott Hughes | ef83600 | 2015-05-04 15:51:50 -0700 | [diff] [blame] | 66 | printf( "received header %.*s (%d bytes):\n%.*s----\n", 4, head, len, len, buffer ); |
The Android Open Source Project | 9ca14dc | 2009-03-03 19:32:55 -0800 | [diff] [blame] | 67 | } |
| 68 | close(s); |
| 69 | } |