android (22)
int main(int argc, char **argv)
{
adb_trace_init();
#if ADB_HOST
作为adbd 来编译
adb_sysdeps_init();
return adb_commandline(argc - 1, argv + 1);
#else
if((argc > 1) && (!strcmp(argv[1],"recovery"))) {
adb_device_banner = "recovery";
recovery_mode = 1;
}
start_device_log();
return adb_main(0, DEFAULT_ADB_PORT);
#endif
}
/* read a comma/space/colum/semi-column separated list of tags
* from the ADB_TRACE environment variable and build the trace
* mask from it. note that '1' and 'all' are special cases to
* enable all tracing
*/
读取环境变量,建立掩码
void adb_trace_init(void)
{
const char* p = getenv("ADB_TRACE");
const char* q;
static const struct {
const char* tag;
int flag;
} tags[] = {
{ "1", 0 },
{ "all", 0 },
{ "adb", TRACE_ADB },
{ "sockets", TRACE_SOCKETS },
{ "packets", TRACE_PACKETS },
{ "rwx", TRACE_RWX },
{ "usb", TRACE_USB },
{ "sync", TRACE_SYNC },
{ "sysdeps", TRACE_SYSDEPS },
{ "transport", TRACE_TRANSPORT },
{ "jdwp", TRACE_JDWP },
{ NULL, 0 }
};
if (p == NULL)
return;
/* use a comma/column/semi-colum/space separated list */
while (*p) {
int len, tagn;
q = strpbrk(p, " ,:;");
if (q == NULL) {
q = p + strlen(p);
}
len = q - p;
for (tagn = 0; tags[tagn].tag != NULL; tagn++)
{
int taglen = strlen(tags[tagn].tag);
if (len == taglen && !memcmp(tags[tagn].tag, p, len) )
{
int flag = tags[tagn].flag;
if (flag == 0) {
adb_trace_mask = ~0;
return;
}
adb_trace_mask |= (1 << flag);
break;
}
}
p = q;
if (*p)
p++;
}
}
int adb_trace_mask;
为一个全局变量
int adb_commandline(int argc, char **argv)
{
char buf[4096];
int no_daemon = 0;
int is_daemon = 0;
int persist = 0;
int r;
int quote;
transport_type ttype = kTransportAny;
char* serial = NULL;
char* server_port_str = NULL;
/* If defined, this should be an absolute path to
* the directory containing all of the various system images
* for a particular product. If not defined, and the adb
* command requires this information, then the user must
* specify the path using "-p".
*/
gProductOutPath = getenv("ANDROID_PRODUCT_OUT");
if (gProductOutPath == NULL || gProductOutPath[0] == '\0') {
gProductOutPath = NULL;
}
// TODO: also try TARGET_PRODUCT/TARGET_DEVICE as a hint
serial = getenv("ANDROID_SERIAL");
/* Validate and assign the server port */
server_port_str = getenv("ANDROID_ADB_SERVER_PORT");
int server_port = DEFAULT_ADB_PORT;
if (server_port_str && strlen(server_port_str) > 0) {
server_port = (int) strtol(server_port_str, NULL, 0);
if (server_port <= 0) {
fprintf(stderr,
"adb: Env var ANDROID_ADB_SERVER_PORT must be a positive number. Got \"%s\"\n",
server_port_str);
return usage();
}
}
如果没有指定,默认就使用5037
/* modifiers and flags */
while(argc > 0) {
if(!strcmp(argv[0],"nodaemon")) {
no_daemon = 1;
是否以服务的形式启动
} else if (!strcmp(argv[0], "fork-server")) {
/* this is a special flag used only when the ADB client launches the ADB Server */
is_daemon = 1;
} else if(!strcmp(argv[0],"persist")) {
persist = 1;
} else if(!strncmp(argv[0], "-p", 2)) {
const char *product = NULL;
if (argv[0][2] == '\0') {
if (argc < 2) return usage();
product = argv[1];
argc--;
argv++;
} else {
product = argv[1] + 2;
}
gProductOutPath = find_product_out_path(product);
if (gProductOutPath == NULL) {
fprintf(stderr, "adb: could not resolve \"-p %s\"\n",
product);
return usage();
}
} else if (argv[0][0]=='-' && argv[0][1]=='s') {
if (isdigit(argv[0][2])) {
serial = argv[0] + 2;
} else {
if(argc < 2 || argv[0][2] != '\0') return usage();
serial = argv[1];
argc--;
argv++;
}
} else if (!strcmp(argv[0],"-d")) {
ttype = kTransportUsb;
} else if (!strcmp(argv[0],"-e")) {
ttype = kTransportLocal;
} else {
/* out of recognized modifiers and flags */
break;
}
argc--;
argv++;
}
解析所有的参数
/* a transport object models the connection to a remote device or emulator
** there is one transport per connected device/emulator. a "local transport"
** connects through TCP (for the emulator), while a "usb transport" through
** USB (for real devices)
**
** note that kTransportHost doesn't really correspond to a real transport
** object, it's a special value used to indicate that a client wants to
** connect to a service implemented within the ADB server itself.
*/
typedef enum transport_type {
kTransportUsb,
kTransportLocal,
kTransportAny,
kTransportHost,
} transport_type;
如果是模拟器就使用TCP
如果是真实设备就使用usb
ttype 如果是-d 就是真实设备
如果是-e 就是模拟器
adb_set_transport(ttype, serial);
将上面2个值保存在下面2个全局变量中
static transport_type __adb_transport = kTransportAny;
static const char* __adb_serial = NULL;
static int __adb_server_port = DEFAULT_ADB_PORT;
adb_set_tcp_specifics(server_port);
if ((argc > 0) && (!strcmp(argv[0],"server"))) {
if (no_daemon || is_daemon) {
r = adb_main(is_daemon, server_port);
} else {
r = launch_server(server_port);
}
if(r) {
fprintf(stderr,"* could not start server *\n");
}
return r;
}
top:
if(argc == 0) {
return usage();
}
/* adb_connect() commands */
if(!strcmp(argv[0], "devices")) {
char *tmp;
snprintf(buf, sizeof buf, "host:%s", argv[0]);
tmp = adb_query(buf);
if(tmp) {
printf("List of devices attached \n");
printf("%s\n", tmp);
return 0;
} else {
return 1;
}
}
if(!strcmp(argv[0], "connect")) {
char *tmp;
if (argc != 2) {
fprintf(stderr, "Usage: adb connect <host>[:<port>]\n");
return 1;
}
snprintf(buf, sizeof buf, "host:connect:%s", argv[1]);
tmp = adb_query(buf);
if(tmp) {
printf("%s\n", tmp);
return 0;
} else {
return 1;
}
}
if(!strcmp(argv[0], "disconnect")) {
char *tmp;
if (argc > 2) {
fprintf(stderr, "Usage: adb disconnect [<host>[:<port>]]\n");
return 1;
}
if (argc == 2) {
snprintf(buf, sizeof buf, "host:disconnect:%s", argv[1]);
} else {
snprintf(buf, sizeof buf, "host:disconnect:");
}
tmp = adb_query(buf);
if(tmp) {
printf("%s\n", tmp);
return 0;
} else {
return 1;
}
}
if (!strcmp(argv[0], "emu")) {
return adb_send_emulator_command(argc, argv);
}
if(!strcmp(argv[0], "shell")) {
int r;
int fd;
if(argc < 2) {
return interactive_shell();
}
snprintf(buf, sizeof buf, "shell:%s", argv[1]);
argc -= 2;
argv += 2;
while(argc-- > 0) {
strcat(buf, " ");
/* quote empty strings and strings with spaces */
quote = (**argv == 0 || strchr(*argv, ' '));
if (quote)
strcat(buf, "\"");
strcat(buf, *argv++);
if (quote)
strcat(buf, "\"");
}
for(;;) {
fd = adb_connect(buf);
if(fd >= 0) {
read_and_dump(fd);
adb_close(fd);
r = 0;
} else {
fprintf(stderr,"error: %s\n", adb_error());
r = -1;
}
if(persist) {
fprintf(stderr,"\n- waiting for device -\n");
adb_sleep_ms(1000);
do_cmd(ttype, serial, "wait-for-device", 0);
} else {
return r;
}
}
}
if(!strcmp(argv[0], "kill-server")) {
int fd;
fd = _adb_connect("host:kill");
if(fd == -1) {
fprintf(stderr,"* server not running *\n");
return 1;
}
return 0;
}
if(!strcmp(argv[0], "remount") || !strcmp(argv[0], "reboot")
|| !strcmp(argv[0], "reboot-bootloader")
|| !strcmp(argv[0], "tcpip") || !strcmp(argv[0], "usb")
|| !strcmp(argv[0], "root")) {
char command[100];
if (!strcmp(argv[0], "reboot-bootloader"))
snprintf(command, sizeof(command), "reboot:bootloader");
else if (argc > 1)
snprintf(command, sizeof(command), "%s:%s", argv[0], argv[1]);
else
snprintf(command, sizeof(command), "%s:", argv[0]);
int fd = adb_connect(command);
if(fd >= 0) {
read_and_dump(fd);
adb_close(fd);
return 0;
}
fprintf(stderr,"error: %s\n", adb_error());
return 1;
}
if(!strcmp(argv[0], "bugreport")) {
if (argc != 1) return usage();
do_cmd(ttype, serial, "shell", "bugreport", 0);
return 0;
}
/* adb_command() wrapper commands */
if(!strncmp(argv[0], "wait-for-", strlen("wait-for-"))) {
char* service = argv[0];
if (!strncmp(service, "wait-for-device", strlen("wait-for-device"))) {
if (ttype == kTransportUsb) {
service = "wait-for-usb";
} else if (ttype == kTransportLocal) {
service = "wait-for-local";
} else {
service = "wait-for-any";
}
}
format_host_command(buf, sizeof buf, service, ttype, serial);
if (adb_command(buf)) {
D("failure: %s *\n",adb_error());
fprintf(stderr,"error: %s\n", adb_error());
return 1;
}
/* Allow a command to be run after wait-for-device,
* e.g. 'adb wait-for-device shell'.
*/
if(argc > 1) {
argc--;
argv++;
goto top;
}
return 0;
}
if(!strcmp(argv[0], "forward")) {
if(argc != 3) return usage();
if (serial) {
snprintf(buf, sizeof buf, "host-serial:%s:forward:%s;%s",serial, argv[1], argv[2]);
} else if (ttype == kTransportUsb) {
snprintf(buf, sizeof buf, "host-usb:forward:%s;%s", argv[1], argv[2]);
} else if (ttype == kTransportLocal) {
snprintf(buf, sizeof buf, "host-local:forward:%s;%s", argv[1], argv[2]);
} else {
snprintf(buf, sizeof buf, "host:forward:%s;%s", argv[1], argv[2]);
}
if(adb_command(buf)) {
fprintf(stderr,"error: %s\n", adb_error());
return 1;
}
return 0;
}
/* do_sync_*() commands */
if(!strcmp(argv[0], "ls")) {
if(argc != 2) return usage();
return do_sync_ls(argv[1]);
}
if(!strcmp(argv[0], "push")) {
if(argc != 3) return usage();
return do_sync_push(argv[1], argv[2], 0 /* no verify APK */);
}
if(!strcmp(argv[0], "pull")) {
if (argc == 2) {
return do_sync_pull(argv[1], ".");
} else if (argc == 3) {
return do_sync_pull(argv[1], argv[2]);
} else {
return usage();
}
}
if(!strcmp(argv[0], "install")) {
if (argc < 2) return usage();
return install_app(ttype, serial, argc, argv);
}
if(!strcmp(argv[0], "uninstall")) {
if (argc < 2) return usage();
return uninstall_app(ttype, serial, argc, argv);
}
if(!strcmp(argv[0], "sync")) {
char *srcarg, *android_srcpath, *data_srcpath;
int listonly = 0;
int ret;
if(argc < 2) {
/* No local path was specified. */
srcarg = NULL;
} else if (argc >= 2 && strcmp(argv[1], "-l") == 0) {
listonly = 1;
if (argc == 3) {
srcarg = argv[2];
} else {
srcarg = NULL;
}
} else if(argc == 2) {
/* A local path or "android"/"data" arg was specified. */
srcarg = argv[1];
} else {
return usage();
}
ret = find_sync_dirs(srcarg, &android_srcpath, &data_srcpath);
if(ret != 0) return usage();
if(android_srcpath != NULL)
ret = do_sync_sync(android_srcpath, "/system", listonly);
if(ret == 0 && data_srcpath != NULL)
ret = do_sync_sync(data_srcpath, "/data", listonly);
free(android_srcpath);
free(data_srcpath);
return ret;
}
/* passthrough commands */
if(!strcmp(argv[0],"get-state") ||
!strcmp(argv[0],"get-serialno"))
{
char *tmp;
format_host_command(buf, sizeof buf, argv[0], ttype, serial);
tmp = adb_query(buf);
if(tmp) {
printf("%s\n", tmp);
return 0;
} else {
return 1;
}
}
/* other commands */
if(!strcmp(argv[0],"status-window")) {
status_window(ttype, serial);
return 0;
}
if(!strcmp(argv[0],"logcat") || !strcmp(argv[0],"lolcat")) {
return logcat(ttype, serial, argc, argv);
}
if(!strcmp(argv[0],"ppp")) {
return ppp(argc, argv);
}
if (!strcmp(argv[0], "start-server")) {
return adb_connect("host:start-server");
}
if (!strcmp(argv[0], "jdwp")) {
int fd = adb_connect("jdwp");
if (fd >= 0) {
read_and_dump(fd);
adb_close(fd);
return 0;
} else {
fprintf(stderr, "error: %s\n", adb_error());
return -1;
}
}
/* "adb /?" is a common idiom under Windows */
if(!strcmp(argv[0], "help") || !strcmp(argv[0], "/?")) {
help();
return 0;
}
if(!strcmp(argv[0], "version")) {
version(stdout);
return 0;
}
usage();
return 1;
}
ADB由两个物理文件组成:
adb或adb.exe,运行于PC端,包括Linux、Windows、Mac OS等系统之中,通常是x86架构上(下文中,ADB指整个模块,而adb单独指这一部分);
adbd,运行于Android设备的底层Linux之中,ARMv5架构上。
为了保持两者代码一致,Google并未将其做源码文件级别的分离,而是统一为一份代码,通过传入Android.mk的$(BUILD_SIMULATOR)变量是否为真,来构建不同的文件。
对源码而言,由ADB_HOST宏是否预定义为真来区分。
而adb本身还需要估计多个操作系统平台,这由传入Android.mk的$(HOST_OS)来控制,它的有效取值包括linux、darwin、freebsd和windows。不同平台的主要差异是USB的控制方法和文件路径。
除了物理文件上的区别,adb本身还会在PC中产生两类不同的进程。
其中一个进程将由命令行“adb fork-server server”产生(给用户的调用接口是adb start-server),这个守护进程长期运行于后台,没有控制台界面,称之为adb server(adb服务端),
其主要工作有两部分:
管理PC中的Android模拟器,以及通过USB线连接到PC的Android设备,负责维持运行于其中的adbd进程与自身的数据通道;
实现PC与设备/模拟器之间的数据拷贝。
adb的另一类进程是提供给用户的命令行工具,对用户暴露了上述install、push、shell等接口,与用户交互,称之为adb client(adb客户端)。其主要工作是解析这些命令的参数,
做必要预处理,然后转移为指令或数据,发送给adb服务端。adb服务端再将指令数据转发到模拟器或设备中,由adbd处理,产生结果,再通过adb服务端接收回来。
事实上,当adb客户端运行时,会自动检查当前系统中是否存在adb服务端,如果不存在,则通过“adb fork-server server”启动一个服务端,然后再与之建立连接。这样,用户就不需要关心这些概念和差异了。
因此,从运行实体上看,ADB由三部分构成:
adbd
adb server
adb client
Google还引入了一个adb service(adb服务,注意与adb服务端的区别)的概念,指adbd提供的功能。这就构成了ADB的四个模块。
在源码的OVERVIEW.txt文件中对它们的关系进行了描述。而protocol.txt描述了各模块之间通信协作的协议格式。