1dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/*
2dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Copyright (C) 2007 The Android Open Source Project
3dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
4dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Licensed under the Apache License, Version 2.0 (the "License");
5dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * you may not use this file except in compliance with the License.
6dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * You may obtain a copy of the License at
7dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
8dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *      http://www.apache.org/licenses/LICENSE-2.0
9dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
10dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Unless required by applicable law or agreed to in writing, software
11dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * distributed under the License is distributed on an "AS IS" BASIS,
12dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * See the License for the specific language governing permissions and
14dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * limitations under the License.
15dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
16dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
17dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdio.h>
18dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdlib.h>
19dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <string.h>
20dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <errno.h>
21dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <unistd.h>
22dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <limits.h>
23dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <stdarg.h>
24dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/types.h>
25dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <sys/stat.h>
26dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <ctype.h>
27dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <assert.h>
28dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
29dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "sysdeps.h"
30dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
31dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_TERMIO_H
32dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include <termios.h>
33dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
34dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
35dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define  TRACE_TAG  TRACE_ADB
36dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "adb.h"
37dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "adb_client.h"
38dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#include "file_sync_service.h"
39dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
40dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int do_cmd(transport_type ttype, char* serial, char *cmd, ...);
41dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
423166410a82f43d39201be98a8d35c51baa86cb53Alexey Tarasovvoid get_my_path(char *s, size_t maxLen);
43dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint find_sync_dirs(const char *srcarg,
44dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char **android_srcdir_out, char **data_srcdir_out);
45dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint install_app(transport_type transport, char* serial, int argc, char** argv);
46dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint uninstall_app(transport_type transport, char* serial, int argc, char** argv);
47dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
48dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic const char *gProductOutPath = NULL;
49dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
50dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char *product_file(const char *extra)
51dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
52dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int n;
53dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *x;
54dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
55dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (gProductOutPath == NULL) {
56dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "adb: Product directory not specified; "
57dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                "use -p or define ANDROID_PRODUCT_OUT\n");
58dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        exit(1);
59dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
60dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
61dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    n = strlen(gProductOutPath) + strlen(extra) + 2;
62dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    x = malloc(n);
63dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (x == 0) {
64dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "adb: Out of memory (product_file())\n");
65dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        exit(1);
66dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
67dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
68dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    snprintf(x, (size_t)n, "%s" OS_PATH_SEPARATOR_STR "%s", gProductOutPath, extra);
69dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return x;
70dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
71dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
72dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid version(FILE * out) {
73dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(out, "Android Debug Bridge version %d.%d.%d\n",
74dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         ADB_VERSION_MAJOR, ADB_VERSION_MINOR, ADB_SERVER_VERSION);
75dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
76dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
77dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectvoid help()
78dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
79dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    version(stderr);
80dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
81dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr,
82dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "\n"
83dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        " -d                            - directs command to the only connected USB device\n"
84dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 returns an error if more than one USB device is present.\n"
85dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        " -e                            - directs command to the only running emulator.\n"
86dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 returns an error if more than one emulator is running.\n"
87e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        " -s <specific device>          - directs command to the device or emulator with the given\n"
882ca3e6b35f79136418ebc32fef57580698dbd045Scott Anderson        "                                 serial number or qualifier. Overrides ANDROID_SERIAL\n"
8931dbed7b60d8237d6d05dc6bf230167a5854b77aElliott Hughes        "                                 environment variable.\n"
90dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        " -p <product name or path>     - simple product name like 'sooner', or\n"
91dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 a relative/absolute path to a product\n"
92dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 out directory like 'out/target/product/sooner'.\n"
93dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 If -p is not specified, the ANDROID_PRODUCT_OUT\n"
94dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 environment variable is used, which must\n"
95dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 be an absolute path.\n"
96e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        " devices [-l]                  - list all connected devices\n"
972ca3e6b35f79136418ebc32fef57580698dbd045Scott Anderson        "                                 ('-l' will also list device qualifiers)\n"
98cbbe79add1410b53ec65936cfb1b74cac68467f0Mike Lockwood        " connect <host>[:<port>]       - connect to a device via TCP/IP\n"
99cbbe79add1410b53ec65936cfb1b74cac68467f0Mike Lockwood        "                                 Port 5555 is used by default if no port number is specified.\n"
100cbbe79add1410b53ec65936cfb1b74cac68467f0Mike Lockwood        " disconnect [<host>[:<port>]]  - disconnect from a TCP/IP device.\n"
101cbbe79add1410b53ec65936cfb1b74cac68467f0Mike Lockwood        "                                 Port 5555 is used by default if no port number is specified.\n"
1026715a4390f482815f51577d25d464537f7e7b823Bernhard Reutner-Fischer        "                                 Using this command with no additional arguments\n"
103cbbe79add1410b53ec65936cfb1b74cac68467f0Mike Lockwood        "                                 will disconnect from all connected TCP/IP devices.\n"
104dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "\n"
105dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "device commands:\n"
106dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb push <local> <remote>    - copy file/dir to device\n"
10700c0eeac6be1597ce24fccf8e2afaaffab223cb3Joe Onorato        "  adb pull <remote> [<local>]  - copy file/dir from device\n"
108dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb sync [ <directory> ]     - copy host->device only if changed\n"
109705c944c0e2a50150ffb41707b6b3daebb45cf79Anthony Newnam        "                                 (-l means list but don't copy)\n"
110dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 (see 'adb help all')\n"
111dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb shell                    - run remote shell interactively\n"
112dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb shell <command>          - run remote shell command\n"
113dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb emu <command>            - run emulator console command\n"
114dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb logcat [ <filter-spec> ] - View device log\n"
115dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb forward <local> <remote> - forward socket connections\n"
116dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 forward specs are one of: \n"
117dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                   tcp:<port>\n"
118dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                   localabstract:<unix domain socket name>\n"
119dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                   localreserved:<unix domain socket name>\n"
120dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                   localfilesystem:<unix domain socket name>\n"
121dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                   dev:<character device name>\n"
122dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                   jdwp:<process pid> (remote only)\n"
123dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb jdwp                     - list PIDs of processes hosting a JDWP transport\n"
1244474ac408105bb31f945ddab53a24ed148e34227Anonymous Coward        "  adb install [-l] [-r] [-s] [--algo <algorithm name> --key <hex-encoded key> --iv <hex-encoded iv>] <file>\n"
1254474ac408105bb31f945ddab53a24ed148e34227Anonymous Coward        "                               - push this package file to the device and install it\n"
126dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 ('-l' means forward-lock the app)\n"
127dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 ('-r' means reinstall the app, keeping its data)\n"
1280ef3fd048aeaeb59467e43e543f66b96e4e6c7f5Mike Lockwood        "                                 ('-s' means install on SD card instead of internal storage)\n"
1294474ac408105bb31f945ddab53a24ed148e34227Anonymous Coward        "                                 ('--algo', '--key', and '--iv' mean the file is encrypted already)\n"
130dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb uninstall [-k] <package> - remove this app package from the device\n"
131dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 ('-k' means keep the data and cache directories)\n"
132dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb bugreport                - return all information from the device\n"
133dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "                                 that should be included in a bug report.\n"
134dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "\n"
13556885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "  adb backup [-f <file>] [-apk|-noapk] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]\n"
13656885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "                               - write an archive of the device's data to <file>.\n"
13756885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "                                 If no -f option is supplied then the data is written\n"
13856885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "                                 to \"backup.ab\" in the current directory.\n"
139d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        "                                 (-apk|-noapk enable/disable backup of the .apks themselves\n"
140de034ec83e2d76db54d1936e706e80853146dcc7Christopher Tate        "                                    in the archive; the default is noapk.)\n"
141d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        "                                 (-shared|-noshared enable/disable backup of the device's\n"
142d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        "                                    shared storage / SD card contents; the default is noshared.)\n"
143d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        "                                 (-all means to back up all installed applications)\n"
14456885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "                                 (-system|-nosystem toggles whether -all automatically includes\n"
14556885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "                                    system applications; the default is to include system apps)\n"
146d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        "                                 (<packages...> is the list of applications to be backed up.  If\n"
147d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        "                                    the -all or -shared flags are passed, then the package\n"
14856885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "                                    list is optional.  Applications explicitly given on the\n"
14956885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "                                    command line will be included even if -nosystem would\n"
15056885094b63af1b0077dec16bc695aa09d8043fcChristopher Tate        "                                    ordinarily cause them to be omitted.)\n"
151d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        "\n"
152de034ec83e2d76db54d1936e706e80853146dcc7Christopher Tate        "  adb restore <file>           - restore device contents from the <file> backup archive\n"
153702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate        "\n"
154dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb help                     - show this help message\n"
155dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb version                  - show version num\n"
156dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "\n"
157dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "scripting:\n"
158dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb wait-for-device          - block until device is online\n"
159dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb start-server             - ensure that there is a server running\n"
160dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb kill-server              - kill the server if it is running\n"
161dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb get-state                - prints: offline | bootloader | device\n"
162dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb get-serialno             - prints: <serial-number>\n"
163e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        "  adb get-devpath              - prints: <device-path>\n"
164dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb status-window            - continuously print device status for a specified device\n"
165dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb remount                  - remounts the /system partition on the device read-write\n"
166ee15662140632672a6878787a1ff26f5ebdc1e68Mike Lockwood        "  adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program\n"
167311add489e4a1712d201c3b83fa364d5569e6788Romain Guy        "  adb reboot-bootloader        - reboots the device into the bootloader\n"
168ff19670d485fd5ee296e23b00b035ac22975c647Mike Lockwood        "  adb root                     - restarts the adbd daemon with root permissions\n"
169311add489e4a1712d201c3b83fa364d5569e6788Romain Guy        "  adb usb                      - restarts the adbd daemon listening on USB\n"
170ff19670d485fd5ee296e23b00b035ac22975c647Mike Lockwood        "  adb tcpip <port>             - restarts the adbd daemon listening on TCP on the specified port"
171dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "\n"
172dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "networking:\n"
173dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  adb ppp <tty> [parameters]   - Run PPP over USB.\n"
174c989199ccfbe745a7109f57d2aee2577d2f72b8dKenny Root        " Note: you should not automatically start a PPP connection.\n"
175dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        " <tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1\n"
176dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        " [parameters] - Eg. defaultroute debug dump local notty usepeerdns\n"
177dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "\n"
178dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "adb sync notes: adb sync [ <directory> ]\n"
179dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  <localdir> can be interpreted in several ways:\n"
180dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "\n"
181dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  - If <directory> is not specified, both /system and /data partitions will be updated.\n"
182dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "\n"
183dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "  - If it is \"system\" or \"data\", only the corresponding partition\n"
184dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "    is updated.\n"
185cd64315f72537359537c66eebe482495ffefba57Tim        "\n"
186cd64315f72537359537c66eebe482495ffefba57Tim        "environmental variables:\n"
187cd64315f72537359537c66eebe482495ffefba57Tim        "  ADB_TRACE                    - Print debug information. A comma separated list of the following values\n"
188cd64315f72537359537c66eebe482495ffefba57Tim        "                                 1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp\n"
189cd64315f72537359537c66eebe482495ffefba57Tim        "  ANDROID_SERIAL               - The serial number to connect to. -s takes priority over this if given.\n"
190cd64315f72537359537c66eebe482495ffefba57Tim        "  ANDROID_LOG_TAGS             - When used with the logcat option, only these debug tags are printed.\n"
191dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        );
192dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
193dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
194dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint usage()
195dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
196dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    help();
197dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 1;
198dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
199dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
200dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_TERMIO_H
201dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic struct termios tio_save;
202dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
203dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void stdin_raw_init(int fd)
204dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
205dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    struct termios tio;
206dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
207dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(tcgetattr(fd, &tio)) return;
208dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(tcgetattr(fd, &tio_save)) return;
209dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
210dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    tio.c_lflag = 0; /* disable CANON, ECHO*, etc */
211dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
212dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* no timeout but request at least one character per read */
213dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    tio.c_cc[VTIME] = 0;
214dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    tio.c_cc[VMIN] = 1;
215dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
216dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    tcsetattr(fd, TCSANOW, &tio);
217dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    tcflush(fd, TCIFLUSH);
218dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
219dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
220dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void stdin_raw_restore(int fd)
221dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
222dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    tcsetattr(fd, TCSANOW, &tio_save);
223dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    tcflush(fd, TCIFLUSH);
224dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
225dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
226dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
227dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void read_and_dump(int fd)
228dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
229dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char buf[4096];
230dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int len;
231dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
232dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(fd >= 0) {
233408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall        D("read_and_dump(): pre adb_read(fd=%d)\n", fd);
234dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        len = adb_read(fd, buf, 4096);
235408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall        D("read_and_dump(): post adb_read(fd=%d): len=%d\n", fd, len);
236dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(len == 0) {
237dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
238dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
239dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
240dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(len < 0) {
241dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(errno == EINTR) continue;
242dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
243dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
244dd6b36e334809e7a99f0bce45353cdd7e4dac4e1Mike Lockwood        fwrite(buf, 1, len, stdout);
245dd6b36e334809e7a99f0bce45353cdd7e4dac4e1Mike Lockwood        fflush(stdout);
246dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
247dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
248dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
249d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tatestatic void copy_to_file(int inFd, int outFd) {
2505b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate    const size_t BUFSIZE = 32 * 1024;
2515b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate    char* buf = (char*) malloc(BUFSIZE);
252d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    int len;
253c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate    long total = 0;
254d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
255d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    D("copy_to_file(%d -> %d)\n", inFd, outFd);
256d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    for (;;) {
2575b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate        len = adb_read(inFd, buf, BUFSIZE);
258d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        if (len == 0) {
2595b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate            D("copy_to_file() : read 0 bytes; exiting\n");
260d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate            break;
261d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        }
262d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        if (len < 0) {
2635b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate            if (errno == EINTR) {
2645b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate                D("copy_to_file() : EINTR, retrying\n");
2655b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate                continue;
2665b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate            }
267d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate            D("copy_to_file() : error %d\n", errno);
268d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate            break;
269d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        }
270d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        adb_write(outFd, buf, len);
271c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate        total += len;
272d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    }
273c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate    D("copy_to_file() finished after %lu bytes\n", total);
2745b811fa5dd00288954f15209a56aea03d5e4a004Christopher Tate    free(buf);
275d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate}
276d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
277dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void *stdin_read_thread(void *x)
278dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
279dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fd, fdi;
280dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    unsigned char buf[1024];
281dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int r, n;
282dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int state = 0;
283dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
284dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int *fds = (int*) x;
285dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd = fds[0];
286dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fdi = fds[1];
287dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    free(fds);
288dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
289dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(;;) {
290dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* fdi is really the client's stdin, so use read, not adb_read here */
291408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall        D("stdin_read_thread(): pre unix_read(fdi=%d,...)\n", fdi);
292dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        r = unix_read(fdi, buf, 1024);
293408fa57864c01113deaa213e5c1848a9c594ae92JP Abgrall        D("stdin_read_thread(): post unix_read(fdi=%d,...)\n", fdi);
294dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(r == 0) break;
295dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(r < 0) {
296dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(errno == EINTR) continue;
297dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
298dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
29967d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood        for(n = 0; n < r; n++){
30067d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood            switch(buf[n]) {
30167d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood            case '\n':
30267d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                state = 1;
30367d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                break;
30467d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood            case '\r':
30567d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                state = 1;
30667d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                break;
30767d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood            case '~':
30867d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                if(state == 1) state++;
30967d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                break;
31067d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood            case '.':
31167d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                if(state == 2) {
31267d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                    fprintf(stderr,"\n* disconnect *\n");
31367d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood#ifdef HAVE_TERMIO_H
31467d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                    stdin_raw_restore(fdi);
315dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
31667d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                    exit(0);
317dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                }
31867d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood            default:
31967d5358e2a870f9e9447517bfa49bf5c6b410a50Mike Lockwood                state = 0;
320dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
321dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
322dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        r = adb_write(fd, buf, r);
323dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(r <= 0) {
324dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
325dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
326dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
327dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
328dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
329dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
330dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint interactive_shell(void)
331dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
332dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_thread_t thr;
333dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fdi, fd;
334dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int *fds;
335dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
336dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd = adb_connect("shell:");
337dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(fd < 0) {
338dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr,"error: %s\n", adb_error());
339dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
340dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
341dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fdi = 0; //dup(0);
342dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
343dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fds = malloc(sizeof(int) * 2);
344dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fds[0] = fd;
345dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fds[1] = fdi;
346dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
347dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_TERMIO_H
348dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    stdin_raw_init(fdi);
349dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
350dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_thread_create(&thr, stdin_read_thread, fds);
351dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    read_and_dump(fd);
352dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_TERMIO_H
353dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    stdin_raw_restore(fdi);
354dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
355dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
356dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
357dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
358dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
359dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void format_host_command(char* buffer, size_t  buflen, const char* command, transport_type ttype, const char* serial)
360dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
361dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (serial) {
362dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        snprintf(buffer, buflen, "host-serial:%s:%s", serial, command);
363dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
364dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        const char* prefix = "host";
365dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (ttype == kTransportUsb)
366dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            prefix = "host-usb";
367dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        else if (ttype == kTransportLocal)
368dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            prefix = "host-local";
369dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
370dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        snprintf(buffer, buflen, "%s:%s", prefix, command);
371dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
372dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
373dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
374447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongkerint adb_download_buffer(const char *service, const void* data, int sz,
375447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker                        unsigned progress)
376447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker{
377447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    char buf[4096];
378447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    unsigned total;
379447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    int fd;
380447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    const unsigned char *ptr;
381447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
382447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    sprintf(buf,"%s:%d", service, sz);
383447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    fd = adb_connect(buf);
384447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    if(fd < 0) {
385447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        fprintf(stderr,"error: %s\n", adb_error());
386447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        return -1;
387447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    }
388447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
389447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    int opt = CHUNK_SIZE;
390447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    opt = setsockopt(fd, SOL_SOCKET, SO_SNDBUF, &opt, sizeof(opt));
391447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
392447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    total = sz;
393447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    ptr = data;
394447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
395447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    if(progress) {
396447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        char *x = strrchr(service, ':');
397447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        if(x) service = x + 1;
398447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    }
399447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
400447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    while(sz > 0) {
401447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        unsigned xfer = (sz > CHUNK_SIZE) ? CHUNK_SIZE : sz;
402447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        if(writex(fd, ptr, xfer)) {
403447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker            adb_status(fd);
404447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker            fprintf(stderr,"* failed to write data '%s' *\n", adb_error());
405447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker            return -1;
406447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        }
407447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        sz -= xfer;
408447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        ptr += xfer;
409447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        if(progress) {
410447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker            printf("sending: '%s' %4d%%    \r", service, (int)(100LL - ((100LL * sz) / (total))));
411447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker            fflush(stdout);
412447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        }
413447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    }
414447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    if(progress) {
415447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        printf("\n");
416447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    }
417447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
418447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    if(readx(fd, buf, 4)){
419447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        fprintf(stderr,"* error reading response *\n");
420447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        adb_close(fd);
421447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        return -1;
422447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    }
423447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    if(memcmp(buf, "OKAY", 4)) {
424447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        buf[4] = 0;
425447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        fprintf(stderr,"* error response '%s' *\n", buf);
426447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        adb_close(fd);
427447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        return -1;
428447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    }
429447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
430447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    adb_close(fd);
431447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    return 0;
432447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker}
433447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
434447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
435447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongkerint adb_download(const char *service, const char *fn, unsigned progress)
436447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker{
437447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    void *data;
438447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    unsigned sz;
439447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
440447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    data = load_file(fn, &sz);
441447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    if(data == 0) {
442447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        fprintf(stderr,"* cannot read '%s' *\n", service);
443447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker        return -1;
444447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    }
445447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
446447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    int status = adb_download_buffer(service, data, sz, progress);
447447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    free(data);
448447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker    return status;
449447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker}
450447f061da19fe46bae35f1cdd93eeb16bc225463Doug Zongker
451dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic void status_window(transport_type ttype, const char* serial)
452dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
453dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char command[4096];
454dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *state = 0;
455dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *laststate = 0;
456dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
457dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* silence stderr */
458dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef _WIN32
459dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* XXX: TODO */
460dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else
461dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int  fd;
462dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd = unix_open("/dev/null", O_WRONLY);
463dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    dup2(fd, 2);
464dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_close(fd);
465dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif
466dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
467dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    format_host_command(command, sizeof command, "get-state", ttype, serial);
468dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
469dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(;;) {
470dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        adb_sleep_ms(250);
471dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
472dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(state) {
473dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            free(state);
474dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            state = 0;
475dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
476dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
477dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        state = adb_query(command);
478dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
479dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(state) {
480dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if(laststate && !strcmp(state,laststate)){
481dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                continue;
482dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            } else {
483dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                if(laststate) free(laststate);
484dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                laststate = strdup(state);
485dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
486dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
487dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
488dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("%c[2J%c[2H", 27, 27);
489dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("Android Debug Bridge\n");
490dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        printf("State: %s\n", state ? state : "offline");
491dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fflush(stdout);
492dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
493dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
494dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
495dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/** duplicate string and quote all \ " ( ) chars + space character. */
496dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char *
497dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectdupAndQuote(const char *s)
498dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
499dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    const char *ts;
500dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    size_t alloc_len;
501dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *ret;
502dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *dest;
503dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
504dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ts = s;
505dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
506dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    alloc_len = 0;
507dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
508dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for( ;*ts != '\0'; ts++) {
509dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        alloc_len++;
510dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
511dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            alloc_len++;
512dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
513dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
514dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
515dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ret = (char *)malloc(alloc_len + 1);
516dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
517dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ts = s;
518dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    dest = ret;
519dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
520dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for ( ;*ts != '\0'; ts++) {
521dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (*ts == ' ' || *ts == '"' || *ts == '\\' || *ts == '(' || *ts == ')') {
522dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            *dest++ = '\\';
523dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
524dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
525dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        *dest++ = *ts;
526dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
527dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
528dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    *dest++ = '\0';
529dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
530dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return ret;
531dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
532dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
533dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/**
534dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Run ppp in "notty" mode against a resource listed as the first parameter
535dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * eg:
536dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
537dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * ppp dev:/dev/omap_csmi_tty0 <ppp options>
538dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
539dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
540dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint ppp(int argc, char **argv)
541dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
542dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#ifdef HAVE_WIN32_PROC
543dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fprintf(stderr, "error: adb %s not implemented on Win32\n", argv[0]);
544dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return -1;
545dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#else
546dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *adb_service_name;
547dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pid_t pid;
548dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fd;
549dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
550dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (argc < 2) {
551dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "usage: adb %s <adb service name> [ppp opts]\n",
552dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                argv[0]);
553dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
554dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
555dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
556dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
557dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_service_name = argv[1];
558dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
559dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    fd = adb_connect(adb_service_name);
560dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
561dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(fd < 0) {
562dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr,"Error: Could not open adb service: %s. Error: %s\n",
563dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                adb_service_name, adb_error());
564dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
565dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
566dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
567dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    pid = fork();
568dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
569dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (pid < 0) {
570dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        perror("from fork()");
571dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 1;
572dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else if (pid == 0) {
573dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int err;
574dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        int i;
575dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        const char **ppp_args;
576dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
577dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        // copy args
578dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ppp_args = (const char **) alloca(sizeof(char *) * argc + 1);
579dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ppp_args[0] = "pppd";
580dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        for (i = 2 ; i < argc ; i++) {
581dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            //argv[2] and beyond become ppp_args[1] and beyond
582dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            ppp_args[i - 1] = argv[i];
583dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
584dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        ppp_args[i-1] = NULL;
585dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
586dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        // child side
587dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
588dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        dup2(fd, STDIN_FILENO);
589dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        dup2(fd, STDOUT_FILENO);
590dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        adb_close(STDERR_FILENO);
591dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        adb_close(fd);
592dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
593dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        err = execvp("pppd", (char * const *)ppp_args);
594dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
595dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (err < 0) {
596dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            perror("execing pppd");
597dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
598dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        exit(-1);
599dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
600dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        // parent side
601dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
602dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        adb_close(fd);
603dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return 0;
604dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
605dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#endif /* !HAVE_WIN32_PROC */
606dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
607dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
608dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int send_shellcommand(transport_type transport, char* serial, char* buf)
609dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
610dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int fd, ret;
611dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
612dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    for(;;) {
613dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fd = adb_connect(buf);
614dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(fd >= 0)
615dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
616dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr,"- waiting for device -\n");
617dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        adb_sleep_ms(1000);
618dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        do_cmd(transport, serial, "wait-for-device", 0);
619dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
620dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
621dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    read_and_dump(fd);
622dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    ret = adb_close(fd);
623dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (ret)
624dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        perror("close");
625dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
626dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return ret;
627dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
628dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
629dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int logcat(transport_type transport, char* serial, int argc, char **argv)
630dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
631dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char buf[4096];
632dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
633dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *log_tags;
634dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *quoted_log_tags;
635dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
636dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    log_tags = getenv("ANDROID_LOG_TAGS");
637dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    quoted_log_tags = dupAndQuote(log_tags == NULL ? "" : log_tags);
638dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
639dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    snprintf(buf, sizeof(buf),
640dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        "shell:export ANDROID_LOG_TAGS=\"\%s\" ; exec logcat",
641dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        quoted_log_tags);
642dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
643dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    free(quoted_log_tags);
644dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
645db0a880c4682a90ddf078f03a0e709ddda07a2fdChristopher Tate    if (!strcmp(argv[0],"longcat")) {
646db0a880c4682a90ddf078f03a0e709ddda07a2fdChristopher Tate        strncat(buf, " -v long", sizeof(buf)-1);
647db0a880c4682a90ddf078f03a0e709ddda07a2fdChristopher Tate    }
648db0a880c4682a90ddf078f03a0e709ddda07a2fdChristopher Tate
649dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    argc -= 1;
650dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    argv += 1;
651dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(argc-- > 0) {
652dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char *quoted;
653dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
654dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        quoted = dupAndQuote (*argv++);
655dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
656dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strncat(buf, " ", sizeof(buf)-1);
657dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strncat(buf, quoted, sizeof(buf)-1);
658dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        free(quoted);
659dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
660dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
661dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    send_shellcommand(transport, serial, buf);
662dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
663dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
664dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
665b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tatestatic int mkdirs(char *path)
666b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate{
667b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    int ret;
668b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    char *x = path + 1;
669b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate
670b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    for(;;) {
671b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate        x = adb_dirstart(x);
672b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate        if(x == 0) return 0;
673b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate        *x = 0;
674b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate        ret = adb_mkdir(path, 0775);
675b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate        *x = OS_PATH_SEPARATOR;
676b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate        if((ret < 0) && (errno != EEXIST)) {
677b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate            return ret;
678b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate        }
679b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate        x++;
680b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    }
681b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    return 0;
682b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate}
683b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate
684d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tatestatic int backup(int argc, char** argv) {
685d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    char buf[4096];
686b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    char default_name[32];
687b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    const char* filename = strcpy(default_name, "./backup.ab");
688d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    int fd, outFd;
689c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate    int i, j;
690d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
691c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate    /* find, extract, and use any -f argument */
692c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate    for (i = 1; i < argc; i++) {
693c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate        if (!strcmp("-f", argv[i])) {
694c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate            if (i == argc-1) {
695c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate                fprintf(stderr, "adb: -f passed with no filename\n");
696c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate                return usage();
697c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate            }
698c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate            filename = argv[i+1];
699c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate            for (j = i+2; j <= argc; ) {
700c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate                argv[i++] = argv[j++];
701c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate            }
702c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate            argc -= 2;
703c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate            argv[argc] = NULL;
704c9cd3b976111d91ede6cd88bd06eb57b10b2f0e7Christopher Tate        }
705d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    }
706d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
707bb86bc5892374f3a41784135bebc7ada3270c8c1Christopher Tate    /* bare "adb backup" or "adb backup -f filename" are not valid invocations */
708bb86bc5892374f3a41784135bebc7ada3270c8c1Christopher Tate    if (argc < 2) return usage();
709bb86bc5892374f3a41784135bebc7ada3270c8c1Christopher Tate
710b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    adb_unlink(filename);
711b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    mkdirs((char *)filename);
712b1dfffe6bb506313a3bc9146d2f6f8c533213193Christopher Tate    outFd = adb_creat(filename, 0640);
713d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    if (outFd < 0) {
714d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        fprintf(stderr, "adb: unable to open file %s\n", filename);
715d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        return -1;
716d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    }
717d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
718d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    snprintf(buf, sizeof(buf), "backup");
719d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    for (argc--, argv++; argc; argc--, argv++) {
720d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        strncat(buf, ":", sizeof(buf) - strlen(buf) - 1);
721d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        strncat(buf, argv[0], sizeof(buf) - strlen(buf) - 1);
722d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    }
723d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
724d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    D("backup. filename=%s buf=%s\n", filename, buf);
725d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    fd = adb_connect(buf);
726d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    if (fd < 0) {
727d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        fprintf(stderr, "adb: unable to connect for backup\n");
728d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        adb_close(outFd);
729d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate        return -1;
730d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    }
731d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
732bffa4ca9ad2702d51f5b391b7a05be0c7c691e4eChristopher Tate    printf("Now unlock your device and confirm the backup operation.\n");
733d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    copy_to_file(fd, outFd);
734d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
735d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    adb_close(fd);
736d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    adb_close(outFd);
737d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate    return 0;
738d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate}
739d2f5415c603f7d9961f7a0b05579a0768e071410Christopher Tate
740702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tatestatic int restore(int argc, char** argv) {
741702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    const char* filename;
742702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    int fd, tarFd;
743702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate
744702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    if (argc != 2) return usage();
745702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate
746702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    filename = argv[1];
747702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    tarFd = adb_open(filename, O_RDONLY);
748702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    if (tarFd < 0) {
749702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate        fprintf(stderr, "adb: unable to open file %s\n", filename);
750702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate        return -1;
751702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    }
752702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate
753702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    fd = adb_connect("restore:");
754702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    if (fd < 0) {
755702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate        fprintf(stderr, "adb: unable to connect for backup\n");
756702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate        adb_close(tarFd);
757702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate        return -1;
758702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    }
759702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate
760bffa4ca9ad2702d51f5b391b7a05be0c7c691e4eChristopher Tate    printf("Now unlock your device and confirm the restore operation.\n");
761702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    copy_to_file(tarFd, fd);
762702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate
763702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    adb_close(fd);
764702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    adb_close(tarFd);
765702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate    return 0;
766702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate}
767702967afb1bebc97c0b8a23c075d4932820ef7a3Christopher Tate
768dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project#define SENTINEL_FILE "config" OS_PATH_SEPARATOR_STR "envsetup.make"
769dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic int top_works(const char *top)
770dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
771dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (top != NULL && adb_is_absolute_host_path(top)) {
772dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char path_buf[PATH_MAX];
773dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        snprintf(path_buf, sizeof(path_buf),
774dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                "%s" OS_PATH_SEPARATOR_STR SENTINEL_FILE, top);
775dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return access(path_buf, F_OK) == 0;
776dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
777dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return 0;
778dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
779dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
780dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char *find_top_from(const char *indir, char path_buf[PATH_MAX])
781dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
782dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    strcpy(path_buf, indir);
783dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while (1) {
784dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (top_works(path_buf)) {
785dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return path_buf;
786dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
787dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char *s = adb_dirstop(path_buf);
788dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (s != NULL) {
789dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            *s = '\0';
790dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
791dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            path_buf[0] = '\0';
792dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return NULL;
793dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
794dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
795dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
796dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
797dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic char *find_top(char path_buf[PATH_MAX])
798dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
799dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char *top = getenv("ANDROID_BUILD_TOP");
800dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (top != NULL && top[0] != '\0') {
801dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (!top_works(top)) {
802dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "adb: bad ANDROID_BUILD_TOP value \"%s\"\n", top);
803dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return NULL;
804dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
805dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    } else {
806dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        top = getenv("TOP");
807dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (top != NULL && top[0] != '\0') {
808dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (!top_works(top)) {
809dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                fprintf(stderr, "adb: bad TOP value \"%s\"\n", top);
810dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                return NULL;
811dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
812dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
813dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            top = NULL;
814dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
815dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
816dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
817dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (top != NULL) {
818dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* The environment pointed to a top directory that works.
819dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         */
820dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strcpy(path_buf, top);
821dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return path_buf;
822dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
823dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
824dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* The environment didn't help.  Walk up the tree from the CWD
825dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     * to see if we can find the top.
826dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     */
827dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char dir[PATH_MAX];
828dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    top = find_top_from(getcwd(dir, sizeof(dir)), path_buf);
829dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (top == NULL) {
830dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* If the CWD isn't under a good-looking top, see if the
831dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         * executable is.
832dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         */
8333166410a82f43d39201be98a8d35c51baa86cb53Alexey Tarasov        get_my_path(dir, PATH_MAX);
834dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        top = find_top_from(dir, path_buf);
835dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
836dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return top;
837dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
838dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
839dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project/* <hint> may be:
840dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * - A simple product name
841dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *   e.g., "sooner"
842dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source ProjectTODO: debug?  sooner-debug, sooner:debug?
843dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * - A relative path from the CWD to the ANDROID_PRODUCT_OUT dir
844dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *   e.g., "out/target/product/sooner"
845dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * - An absolute path to the PRODUCT_OUT dir
846dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *   e.g., "/src/device/out/target/product/sooner"
847dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project *
848dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * Given <hint>, try to construct an absolute path to the
849dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project * ANDROID_PRODUCT_OUT dir.
850dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project */
851dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectstatic const char *find_product_out_path(const char *hint)
852dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
853dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    static char path_buf[PATH_MAX];
854dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
855dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (hint == NULL || hint[0] == '\0') {
856dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return NULL;
857dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
858dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
859dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* If it's already absolute, don't bother doing any work.
860dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     */
861dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (adb_is_absolute_host_path(hint)) {
862dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strcpy(path_buf, hint);
863dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return path_buf;
864dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
865dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
866dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* If there are any slashes in it, assume it's a relative path;
867dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     * make it absolute.
868dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     */
869dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (adb_dirstart(hint) != NULL) {
870dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (getcwd(path_buf, sizeof(path_buf)) == NULL) {
871dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "adb: Couldn't get CWD: %s\n", strerror(errno));
872dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return NULL;
873dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
874dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (strlen(path_buf) + 1 + strlen(hint) >= sizeof(path_buf)) {
875dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr, "adb: Couldn't assemble path\n");
876dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return NULL;
877dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
878dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strcat(path_buf, OS_PATH_SEPARATOR_STR);
879dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        strcat(path_buf, hint);
880dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return path_buf;
881dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
882dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
883dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* It's a string without any slashes.  Try to do something with it.
884dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     *
885dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     * Try to find the root of the build tree, and build a PRODUCT_OUT
886dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     * path from there.
887dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project     */
888dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char top_buf[PATH_MAX];
889dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    const char *top = find_top(top_buf);
890dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (top == NULL) {
891dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "adb: Couldn't find top of build tree\n");
892dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return NULL;
893dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
894dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project//TODO: if we have a way to indicate debug, look in out/debug/target/...
895dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    snprintf(path_buf, sizeof(path_buf),
896dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "%s" OS_PATH_SEPARATOR_STR
897dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "out" OS_PATH_SEPARATOR_STR
898dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "target" OS_PATH_SEPARATOR_STR
899dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "product" OS_PATH_SEPARATOR_STR
900dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            "%s", top_buf, hint);
901dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (access(path_buf, F_OK) < 0) {
902dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        fprintf(stderr, "adb: Couldn't find a product dir "
903dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                "based on \"-p %s\"; \"%s\" doesn't exist\n", hint, path_buf);
904dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return NULL;
905dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
906dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    return path_buf;
907dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project}
908dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
909dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projectint adb_commandline(int argc, char **argv)
910dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project{
911dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char buf[4096];
912dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int no_daemon = 0;
913dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int is_daemon = 0;
914305b4b0f6069d733d3023a57fdc8a6d9cf7de2a6David 'Digit' Turner    int is_server = 0;
915dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int persist = 0;
916dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int r;
917dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    int quote;
918dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    transport_type ttype = kTransportAny;
919dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    char* serial = NULL;
920a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger    char* server_port_str = NULL;
921dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
922dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        /* If defined, this should be an absolute path to
923dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         * the directory containing all of the various system images
924dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         * for a particular product.  If not defined, and the adb
925dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         * command requires this information, then the user must
926dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         * specify the path using "-p".
927dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project         */
928dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    gProductOutPath = getenv("ANDROID_PRODUCT_OUT");
929dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if (gProductOutPath == NULL || gProductOutPath[0] == '\0') {
930dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        gProductOutPath = NULL;
931dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
932dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    // TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
933dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
934db4492640313be9d3fd65ee5bd7648a9570411bbNick Pelly    serial = getenv("ANDROID_SERIAL");
935db4492640313be9d3fd65ee5bd7648a9570411bbNick Pelly
936a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger    /* Validate and assign the server port */
937a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger    server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
938a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger    int server_port = DEFAULT_ADB_PORT;
939a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger    if (server_port_str && strlen(server_port_str) > 0) {
940a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger        server_port = (int) strtol(server_port_str, NULL, 0);
941a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger        if (server_port <= 0) {
942a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger            fprintf(stderr,
943a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger                    "adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number. Got \"%s\"\n",
944a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger                    server_port_str);
945a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger            return usage();
946a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger        }
947a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger    }
948a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger
949a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger    /* modifiers and flags */
950dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    while(argc > 0) {
951305b4b0f6069d733d3023a57fdc8a6d9cf7de2a6David 'Digit' Turner        if(!strcmp(argv[0],"server")) {
952305b4b0f6069d733d3023a57fdc8a6d9cf7de2a6David 'Digit' Turner            is_server = 1;
953305b4b0f6069d733d3023a57fdc8a6d9cf7de2a6David 'Digit' Turner        } else if(!strcmp(argv[0],"nodaemon")) {
954dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            no_daemon = 1;
955dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else if (!strcmp(argv[0], "fork-server")) {
956dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            /* this is a special flag used only when the ADB client launches the ADB Server */
957dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            is_daemon = 1;
958dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else if(!strcmp(argv[0],"persist")) {
959dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            persist = 1;
960dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else if(!strncmp(argv[0], "-p", 2)) {
961dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            const char *product = NULL;
962dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (argv[0][2] == '\0') {
963dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                if (argc < 2) return usage();
964dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                product = argv[1];
965dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                argc--;
966dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                argv++;
967dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            } else {
9688127323f145b2de7e1c30bbb7af0d20d783ac76bVairavan Srinivasan                product = argv[0] + 2;
969dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
970dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            gProductOutPath = find_product_out_path(product);
971dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (gProductOutPath == NULL) {
972dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                fprintf(stderr, "adb: could not resolve \"-p %s\"\n",
973dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                        product);
974dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                return usage();
975dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
976dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else if (argv[0][0]=='-' && argv[0][1]=='s') {
977dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            if (isdigit(argv[0][2])) {
978dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                serial = argv[0] + 2;
979dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            } else {
980a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger                if(argc < 2 || argv[0][2] != '\0') return usage();
981dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                serial = argv[1];
982dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                argc--;
983dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                argv++;
984dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            }
985dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else if (!strcmp(argv[0],"-d")) {
986dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            ttype = kTransportUsb;
987dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else if (!strcmp(argv[0],"-e")) {
988dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            ttype = kTransportLocal;
989dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
990dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project                /* out of recognized modifiers and flags */
991dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            break;
992dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
993dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        argc--;
994dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        argv++;
995dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
996dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
997dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    adb_set_transport(ttype, serial);
998a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger    adb_set_tcp_specifics(server_port);
999dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
1000305b4b0f6069d733d3023a57fdc8a6d9cf7de2a6David 'Digit' Turner    if (is_server) {
1001dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if (no_daemon || is_daemon) {
1002a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger            r = adb_main(is_daemon, server_port);
1003dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
1004a84a42eb20d43ffa2695a69d583a6e09532b49d9Stefan Hilzinger            r = launch_server(server_port);
1005dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
1006dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(r) {
1007dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            fprintf(stderr,"* could not start server *\n");
1008dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
1009dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return r;
1010dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
1011dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
1012dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Projecttop:
1013dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(argc == 0) {
1014dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        return usage();
1015dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
1016dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
1017dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    /* adb_connect() commands */
1018dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
1019dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    if(!strcmp(argv[0], "devices")) {
1020dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        char *tmp;
1021e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        char *listopt;
1022e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        if (argc < 2)
1023e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson            listopt = "";
1024e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        else if (argc == 2 && !strcmp(argv[1], "-l"))
1025e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson            listopt = argv[1];
1026e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        else {
1027e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson            fprintf(stderr, "Usage: adb devices [-l]\n");
1028e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson            return 1;
1029e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        }
1030e109d266c12c5f537d429ca4b892f2719e02c2daScott Anderson        snprintf(buf, sizeof buf, "host:%s%s", argv[0], listopt);
1031dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        tmp = adb_query(buf);
1032dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        if(tmp) {
1033dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            printf("List of devices attached \n");
1034dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            printf("%s\n", tmp);
1035dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 0;
1036dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        } else {
1037dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project            return 1;
1038dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project        }
1039dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project    }
1040dd7bc3319deb2b77c5d07a51b7d6cd7e11b5beb0The Android Open Source Project
1041cbbe79add1410b53ec65936cfb1b74cac68467f0Mike Lockwood    if(