2021-01-03 21:55:15 +08:00
|
|
|
#include "adb.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2019-05-12 21:11:16 +08:00
|
|
|
#include <assert.h>
|
2017-12-12 22:12:07 +08:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2018-02-13 17:10:18 +08:00
|
|
|
|
2021-11-11 23:12:17 +08:00
|
|
|
#include "util/file.h"
|
2019-11-24 18:53:00 +08:00
|
|
|
#include "util/log.h"
|
2021-11-13 06:12:51 +08:00
|
|
|
#include "util/str.h"
|
2017-12-12 22:12:07 +08:00
|
|
|
|
2018-02-01 01:46:56 +08:00
|
|
|
static const char *adb_command;
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static inline const char *
|
|
|
|
get_adb_command(void) {
|
2018-02-01 01:46:56 +08:00
|
|
|
if (!adb_command) {
|
|
|
|
adb_command = getenv("ADB");
|
|
|
|
if (!adb_command)
|
|
|
|
adb_command = "adb";
|
|
|
|
}
|
|
|
|
return adb_command;
|
|
|
|
}
|
|
|
|
|
2019-05-12 21:11:16 +08:00
|
|
|
// serialize argv to string "[arg1], [arg2], [arg3]"
|
|
|
|
static size_t
|
|
|
|
argv_to_string(const char *const *argv, char *buf, size_t bufsize) {
|
|
|
|
size_t idx = 0;
|
|
|
|
bool first = true;
|
|
|
|
while (*argv) {
|
|
|
|
const char *arg = *argv;
|
|
|
|
size_t len = strlen(arg);
|
|
|
|
// count space for "[], ...\0"
|
|
|
|
if (idx + len + 8 >= bufsize) {
|
|
|
|
// not enough space, truncate
|
|
|
|
assert(idx < bufsize - 4);
|
|
|
|
memcpy(&buf[idx], "...", 3);
|
|
|
|
idx += 3;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (first) {
|
|
|
|
first = false;
|
|
|
|
} else {
|
|
|
|
buf[idx++] = ',';
|
|
|
|
buf[idx++] = ' ';
|
|
|
|
}
|
|
|
|
buf[idx++] = '[';
|
|
|
|
memcpy(&buf[idx], arg, len);
|
|
|
|
idx += len;
|
|
|
|
buf[idx++] = ']';
|
|
|
|
argv++;
|
|
|
|
}
|
|
|
|
assert(idx < bufsize);
|
|
|
|
buf[idx] = '\0';
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2020-01-16 02:47:34 +08:00
|
|
|
static void
|
|
|
|
show_adb_installation_msg() {
|
|
|
|
#ifndef __WINDOWS__
|
|
|
|
static const struct {
|
|
|
|
const char *binary;
|
|
|
|
const char *command;
|
|
|
|
} pkg_managers[] = {
|
|
|
|
{"apt", "apt install adb"},
|
|
|
|
{"apt-get", "apt-get install adb"},
|
|
|
|
{"brew", "brew cask install android-platform-tools"},
|
|
|
|
{"dnf", "dnf install android-tools"},
|
|
|
|
{"emerge", "emerge dev-util/android-tools"},
|
|
|
|
{"pacman", "pacman -S android-tools"},
|
|
|
|
};
|
|
|
|
for (size_t i = 0; i < ARRAY_LEN(pkg_managers); ++i) {
|
2021-11-11 23:21:07 +08:00
|
|
|
if (sc_file_executable_exists(pkg_managers[i].binary)) {
|
2020-01-16 02:47:34 +08:00
|
|
|
LOGI("You may install 'adb' by \"%s\"", pkg_managers[i].command);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
LOGI("You may download and install 'adb' from "
|
|
|
|
"https://developer.android.com/studio/releases/platform-tools");
|
|
|
|
}
|
|
|
|
|
2019-03-03 03:09:56 +08:00
|
|
|
static void
|
2021-11-12 00:48:41 +08:00
|
|
|
show_adb_err_msg(enum sc_process_result err, const char *const argv[]) {
|
2021-06-19 23:47:57 +08:00
|
|
|
#define MAX_COMMAND_STRING_LEN 1024
|
|
|
|
char *buf = malloc(MAX_COMMAND_STRING_LEN);
|
|
|
|
if (!buf) {
|
|
|
|
LOGE("Failed to execute (could not allocate error message)");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-09-01 09:18:06 +08:00
|
|
|
switch (err) {
|
2021-11-12 00:48:41 +08:00
|
|
|
case SC_PROCESS_ERROR_GENERIC:
|
2021-06-19 23:47:57 +08:00
|
|
|
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
|
2019-05-12 21:11:16 +08:00
|
|
|
LOGE("Failed to execute: %s", buf);
|
2018-09-01 09:18:06 +08:00
|
|
|
break;
|
2021-11-12 00:48:41 +08:00
|
|
|
case SC_PROCESS_ERROR_MISSING_BINARY:
|
2021-06-19 23:47:57 +08:00
|
|
|
argv_to_string(argv, buf, MAX_COMMAND_STRING_LEN);
|
2019-05-12 21:11:16 +08:00
|
|
|
LOGE("Command not found: %s", buf);
|
|
|
|
LOGE("(make 'adb' accessible from your PATH or define its full"
|
|
|
|
"path in the ADB environment variable)");
|
2020-01-16 02:47:34 +08:00
|
|
|
show_adb_installation_msg();
|
2018-09-01 09:18:06 +08:00
|
|
|
break;
|
2021-11-12 00:48:41 +08:00
|
|
|
case SC_PROCESS_SUCCESS:
|
2019-06-01 04:33:39 +08:00
|
|
|
// do nothing
|
2018-09-01 09:18:06 +08:00
|
|
|
break;
|
|
|
|
}
|
2021-06-19 23:47:57 +08:00
|
|
|
|
|
|
|
free(buf);
|
2018-09-01 09:18:06 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid
|
|
|
|
adb_execute_p(const char *serial, const char *const adb_cmd[],
|
|
|
|
size_t len, sc_pipe *pin, sc_pipe *pout, sc_pipe *perr) {
|
2017-12-12 22:12:07 +08:00
|
|
|
int i;
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid pid;
|
2021-06-19 23:47:57 +08:00
|
|
|
|
|
|
|
const char **argv = malloc((len + 4) * sizeof(*argv));
|
|
|
|
if (!argv) {
|
2021-11-12 00:48:41 +08:00
|
|
|
return SC_PROCESS_NONE;
|
2021-06-19 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
2021-06-19 23:47:57 +08:00
|
|
|
argv[0] = get_adb_command();
|
2017-12-12 22:12:07 +08:00
|
|
|
if (serial) {
|
2021-06-19 23:47:57 +08:00
|
|
|
argv[1] = "-s";
|
|
|
|
argv[2] = serial;
|
2017-12-12 22:12:07 +08:00
|
|
|
i = 3;
|
|
|
|
} else {
|
|
|
|
i = 1;
|
|
|
|
}
|
|
|
|
|
2021-06-19 23:47:57 +08:00
|
|
|
memcpy(&argv[i], adb_cmd, len * sizeof(const char *));
|
|
|
|
argv[len + i] = NULL;
|
2021-11-12 00:48:41 +08:00
|
|
|
enum sc_process_result r =
|
|
|
|
sc_process_execute_p(argv, &pid, pin, pout, perr);
|
|
|
|
if (r != SC_PROCESS_SUCCESS) {
|
2021-06-19 23:47:57 +08:00
|
|
|
show_adb_err_msg(r, argv);
|
2021-11-12 00:48:41 +08:00
|
|
|
pid = SC_PROCESS_NONE;
|
2018-09-01 09:18:06 +08:00
|
|
|
}
|
2021-06-19 23:47:57 +08:00
|
|
|
|
|
|
|
free(argv);
|
2021-11-12 00:48:41 +08:00
|
|
|
return pid;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid
|
2021-10-17 22:14:33 +08:00
|
|
|
adb_execute(const char *serial, const char *const adb_cmd[], size_t len) {
|
2021-11-12 00:48:41 +08:00
|
|
|
return adb_execute_p(serial, adb_cmd, len, NULL, NULL, NULL);
|
2021-10-17 22:14:33 +08:00
|
|
|
}
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid
|
2019-03-03 03:09:56 +08:00
|
|
|
adb_forward(const char *serial, uint16_t local_port,
|
|
|
|
const char *device_socket_name) {
|
2017-12-12 22:12:07 +08:00
|
|
|
char local[4 + 5 + 1]; // tcp:PORT
|
|
|
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
|
|
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
|
|
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
|
|
|
const char *const adb_cmd[] = {"forward", local, remote};
|
|
|
|
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
|
|
|
}
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid
|
2019-03-03 03:09:56 +08:00
|
|
|
adb_forward_remove(const char *serial, uint16_t local_port) {
|
2018-03-12 15:35:51 +08:00
|
|
|
char local[4 + 5 + 1]; // tcp:PORT
|
|
|
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
|
|
|
const char *const adb_cmd[] = {"forward", "--remove", local};
|
|
|
|
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
|
|
|
}
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid
|
2019-03-03 03:09:56 +08:00
|
|
|
adb_reverse(const char *serial, const char *device_socket_name,
|
|
|
|
uint16_t local_port) {
|
2017-12-12 22:12:07 +08:00
|
|
|
char local[4 + 5 + 1]; // tcp:PORT
|
|
|
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
|
|
|
sprintf(local, "tcp:%" PRIu16, local_port);
|
|
|
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
|
|
|
const char *const adb_cmd[] = {"reverse", remote, local};
|
|
|
|
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
|
|
|
}
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid
|
2019-03-03 03:09:56 +08:00
|
|
|
adb_reverse_remove(const char *serial, const char *device_socket_name) {
|
2017-12-12 22:12:07 +08:00
|
|
|
char remote[108 + 14 + 1]; // localabstract:NAME
|
|
|
|
snprintf(remote, sizeof(remote), "localabstract:%s", device_socket_name);
|
|
|
|
const char *const adb_cmd[] = {"reverse", "--remove", remote};
|
|
|
|
return adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
|
|
|
}
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid
|
2019-03-03 03:09:56 +08:00
|
|
|
adb_push(const char *serial, const char *local, const char *remote) {
|
2018-10-05 02:47:53 +08:00
|
|
|
#ifdef __WINDOWS__
|
|
|
|
// Windows will parse the string, so the paths must be quoted
|
|
|
|
// (see sys/win/command.c)
|
2021-11-13 06:08:19 +08:00
|
|
|
local = sc_str_quote(local);
|
2018-10-05 02:47:53 +08:00
|
|
|
if (!local) {
|
2021-11-12 00:48:41 +08:00
|
|
|
return SC_PROCESS_NONE;
|
2018-10-05 02:47:53 +08:00
|
|
|
}
|
2021-11-13 06:08:19 +08:00
|
|
|
remote = sc_str_quote(remote);
|
2018-10-05 02:47:53 +08:00
|
|
|
if (!remote) {
|
2021-01-24 22:14:53 +08:00
|
|
|
free((void *) local);
|
2021-11-12 00:48:41 +08:00
|
|
|
return SC_PROCESS_NONE;
|
2018-10-05 02:47:53 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2018-02-28 22:02:51 +08:00
|
|
|
const char *const adb_cmd[] = {"push", local, remote};
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
2018-10-05 02:47:53 +08:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2021-01-24 22:14:53 +08:00
|
|
|
free((void *) remote);
|
|
|
|
free((void *) local);
|
2018-10-05 02:47:53 +08:00
|
|
|
#endif
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
return pid;
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2018-02-08 22:16:27 +08:00
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid
|
2019-03-03 03:09:56 +08:00
|
|
|
adb_install(const char *serial, const char *local) {
|
2018-05-26 20:43:42 +08:00
|
|
|
#ifdef __WINDOWS__
|
2018-10-05 02:47:53 +08:00
|
|
|
// Windows will parse the string, so the local name must be quoted
|
|
|
|
// (see sys/win/command.c)
|
2021-11-13 06:08:19 +08:00
|
|
|
local = sc_str_quote(local);
|
2018-10-05 02:47:53 +08:00
|
|
|
if (!local) {
|
2021-11-12 00:48:41 +08:00
|
|
|
return SC_PROCESS_NONE;
|
2018-10-05 02:47:53 +08:00
|
|
|
}
|
2018-05-26 20:43:42 +08:00
|
|
|
#endif
|
2018-10-05 02:47:53 +08:00
|
|
|
|
2018-04-29 06:17:34 +08:00
|
|
|
const char *const adb_cmd[] = {"install", "-r", local};
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd));
|
2018-10-05 02:47:53 +08:00
|
|
|
|
|
|
|
#ifdef __WINDOWS__
|
2021-01-24 22:14:53 +08:00
|
|
|
free((void *) local);
|
2018-10-05 02:47:53 +08:00
|
|
|
#endif
|
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
return pid;
|
2018-04-29 06:17:34 +08:00
|
|
|
}
|
2021-10-17 22:30:07 +08:00
|
|
|
|
|
|
|
static ssize_t
|
|
|
|
adb_execute_for_output(const char *serial, const char *const adb_cmd[],
|
|
|
|
size_t adb_cmd_len, char *buf, size_t buf_len,
|
|
|
|
const char *name) {
|
2021-11-12 00:48:41 +08:00
|
|
|
sc_pipe pout;
|
|
|
|
sc_pid pid = adb_execute_p(serial, adb_cmd, adb_cmd_len, NULL, &pout, NULL);
|
2021-11-18 01:36:03 +08:00
|
|
|
if (pid == SC_PROCESS_NONE) {
|
|
|
|
return -1;
|
|
|
|
}
|
2021-10-17 22:30:07 +08:00
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
ssize_t r = sc_pipe_read_all(pout, buf, buf_len);
|
|
|
|
sc_pipe_close(pout);
|
2021-10-17 22:30:07 +08:00
|
|
|
|
2021-11-12 00:48:41 +08:00
|
|
|
if (!sc_process_check_success(pid, name, true)) {
|
2021-10-17 22:30:07 +08:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
|
|
|
truncate_first_line(char *data, size_t len) {
|
|
|
|
data[len - 1] = '\0';
|
|
|
|
char *eol = strpbrk(data, "\r\n");
|
|
|
|
if (eol) {
|
|
|
|
*eol = '\0';
|
|
|
|
len = eol - data;
|
|
|
|
}
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
|
|
|
char *
|
|
|
|
adb_get_serialno(void) {
|
|
|
|
char buf[128];
|
|
|
|
|
|
|
|
const char *const adb_cmd[] = {"get-serialno"};
|
|
|
|
ssize_t r = adb_execute_for_output(NULL, adb_cmd, ARRAY_LEN(adb_cmd),
|
|
|
|
buf, sizeof(buf), "get-serialno");
|
|
|
|
if (r <= 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
truncate_first_line(buf, r);
|
|
|
|
return strdup(buf);
|
|
|
|
}
|