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-26 05:11:59 +08:00
|
|
|
#include "adb_parser.h"
|
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-19 05:08:15 +08:00
|
|
|
#include "util/process_intr.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) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
|
|
|
LOGE("Failed to execute");
|
2021-06-19 23:47:57 +08:00
|
|
|
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-25 16:37:53 +08:00
|
|
|
static bool
|
2021-11-26 05:05:38 +08:00
|
|
|
process_check_success_internal(sc_pid pid, const char *name, bool close,
|
|
|
|
unsigned flags) {
|
|
|
|
bool log_errors = !(flags & SC_ADB_NO_LOGERR);
|
|
|
|
|
2021-11-25 16:37:53 +08:00
|
|
|
if (pid == SC_PROCESS_NONE) {
|
2021-11-26 05:05:38 +08:00
|
|
|
if (log_errors) {
|
|
|
|
LOGE("Could not execute \"%s\"", name);
|
|
|
|
}
|
2021-11-25 16:37:53 +08:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
sc_exit_code exit_code = sc_process_wait(pid, close);
|
|
|
|
if (exit_code) {
|
2021-11-26 05:05:38 +08:00
|
|
|
if (log_errors) {
|
|
|
|
if (exit_code != SC_EXIT_CODE_NONE) {
|
|
|
|
LOGE("\"%s\" returned with value %" SC_PRIexitcode, name,
|
|
|
|
exit_code);
|
|
|
|
} else {
|
|
|
|
LOGE("\"%s\" exited unexpectedly", name);
|
|
|
|
}
|
2021-11-25 16:37:53 +08:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2021-11-26 05:05:38 +08:00
|
|
|
process_check_success_intr(struct sc_intr *intr, sc_pid pid, const char *name,
|
|
|
|
unsigned flags) {
|
2021-11-25 16:37:53 +08:00
|
|
|
if (!sc_intr_set_process(intr, pid)) {
|
|
|
|
// Already interrupted
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Always pass close=false, interrupting would be racy otherwise
|
2021-11-26 05:05:38 +08:00
|
|
|
bool ret = process_check_success_internal(pid, name, false, flags);
|
2021-11-25 16:37:53 +08:00
|
|
|
|
|
|
|
sc_intr_set_process(intr, SC_PROCESS_NONE);
|
|
|
|
|
|
|
|
// Close separately
|
|
|
|
sc_process_close(pid);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2021-11-25 15:57:31 +08:00
|
|
|
static const char **
|
|
|
|
adb_create_argv(const char *serial, const char *const adb_cmd[], size_t len) {
|
2021-06-19 23:47:57 +08:00
|
|
|
const char **argv = malloc((len + 4) * sizeof(*argv));
|
|
|
|
if (!argv) {
|
2021-11-25 05:06:11 +08:00
|
|
|
LOG_OOM();
|
2021-11-25 15:57:31 +08:00
|
|
|
return NULL;
|
2021-06-19 23:47:57 +08:00
|
|
|
}
|
|
|
|
|
2021-06-19 23:47:57 +08:00
|
|
|
argv[0] = get_adb_command();
|
2021-11-25 15:57:31 +08:00
|
|
|
int i;
|
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-25 15:57:31 +08:00
|
|
|
return argv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static sc_pid
|
|
|
|
adb_execute_p(const char *serial, const char *const adb_cmd[], size_t len,
|
2021-11-25 05:27:28 +08:00
|
|
|
unsigned flags, sc_pipe *pout) {
|
2021-11-25 15:57:31 +08:00
|
|
|
const char **argv = adb_create_argv(serial, adb_cmd, len);
|
|
|
|
if (!argv) {
|
|
|
|
return SC_PROCESS_NONE;
|
|
|
|
}
|
|
|
|
|
2021-11-25 05:27:28 +08:00
|
|
|
unsigned process_flags = 0;
|
|
|
|
if (flags & SC_ADB_NO_STDOUT) {
|
|
|
|
process_flags |= SC_PROCESS_NO_STDOUT;
|
|
|
|
}
|
|
|
|
if (flags & SC_ADB_NO_STDERR) {
|
|
|
|
process_flags |= SC_PROCESS_NO_STDERR;
|
|
|
|
}
|
|
|
|
|
2021-11-25 15:57:31 +08:00
|
|
|
sc_pid pid;
|
2021-11-12 00:48:41 +08:00
|
|
|
enum sc_process_result r =
|
2021-11-25 05:27:28 +08:00
|
|
|
sc_process_execute_p(argv, &pid, process_flags, NULL, pout, NULL);
|
2021-11-12 00:48:41 +08:00
|
|
|
if (r != SC_PROCESS_SUCCESS) {
|
2021-11-25 05:27:28 +08:00
|
|
|
// If the execution itself failed (not the command exit code), log the
|
|
|
|
// error in all cases
|
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-11-25 05:27:28 +08:00
|
|
|
adb_execute(const char *serial, const char *const adb_cmd[], size_t len,
|
|
|
|
unsigned flags) {
|
|
|
|
return adb_execute_p(serial, adb_cmd, len, flags, NULL);
|
2021-10-17 22:14:33 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 04:58:51 +08:00
|
|
|
bool
|
|
|
|
adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port,
|
2021-11-25 05:27:28 +08:00
|
|
|
const char *device_socket_name, unsigned flags) {
|
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};
|
2021-11-26 04:58:51 +08:00
|
|
|
|
2021-11-25 05:27:28 +08:00
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
2021-11-26 05:05:38 +08:00
|
|
|
return process_check_success_intr(intr, pid, "adb forward", flags);
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 04:58:51 +08:00
|
|
|
bool
|
|
|
|
adb_forward_remove(struct sc_intr *intr, const char *serial,
|
2021-11-25 05:27:28 +08:00
|
|
|
uint16_t local_port, unsigned flags) {
|
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};
|
2021-11-26 04:58:51 +08:00
|
|
|
|
2021-11-25 05:27:28 +08:00
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
2021-11-26 05:05:38 +08:00
|
|
|
return process_check_success_intr(intr, pid, "adb forward --remove", flags);
|
2018-03-12 15:35:51 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 04:58:51 +08:00
|
|
|
bool
|
|
|
|
adb_reverse(struct sc_intr *intr, const char *serial,
|
2021-11-25 05:27:28 +08:00
|
|
|
const char *device_socket_name, uint16_t local_port,
|
|
|
|
unsigned flags) {
|
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};
|
2021-11-26 04:58:51 +08:00
|
|
|
|
2021-11-25 05:27:28 +08:00
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
2021-11-26 05:05:38 +08:00
|
|
|
return process_check_success_intr(intr, pid, "adb reverse", flags);
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 04:58:51 +08:00
|
|
|
bool
|
|
|
|
adb_reverse_remove(struct sc_intr *intr, const char *serial,
|
2021-11-25 05:27:28 +08:00
|
|
|
const char *device_socket_name, unsigned flags) {
|
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};
|
2021-11-26 04:58:51 +08:00
|
|
|
|
2021-11-25 05:27:28 +08:00
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
2021-11-26 05:05:38 +08:00
|
|
|
return process_check_success_intr(intr, pid, "adb reverse --remove", flags);
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 04:58:51 +08:00
|
|
|
bool
|
|
|
|
adb_push(struct sc_intr *intr, const char *serial, const char *local,
|
2021-11-25 05:27:28 +08:00
|
|
|
const char *remote, unsigned flags) {
|
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-25 05:27:28 +08:00
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
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-26 05:05:38 +08:00
|
|
|
return process_check_success_intr(intr, pid, "adb push", flags);
|
2017-12-12 22:12:07 +08:00
|
|
|
}
|
2018-02-08 22:16:27 +08:00
|
|
|
|
2021-11-26 04:58:51 +08:00
|
|
|
bool
|
2021-11-25 05:27:28 +08:00
|
|
|
adb_install(struct sc_intr *intr, const char *serial, const char *local,
|
|
|
|
unsigned flags) {
|
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-25 05:27:28 +08:00
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
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-26 05:05:38 +08:00
|
|
|
return process_check_success_intr(intr, pid, "adb install", flags);
|
2021-11-19 05:08:15 +08:00
|
|
|
}
|
|
|
|
|
2021-11-26 05:13:39 +08:00
|
|
|
bool
|
|
|
|
adb_tcpip(struct sc_intr *intr, const char *serial, uint16_t port,
|
|
|
|
unsigned flags) {
|
|
|
|
char port_string[5 + 1];
|
|
|
|
sprintf(port_string, "%" PRIu16, port);
|
|
|
|
const char *const adb_cmd[] = {"tcpip", port_string};
|
|
|
|
|
|
|
|
sc_pid pid = adb_execute(serial, adb_cmd, ARRAY_LEN(adb_cmd), flags);
|
|
|
|
return process_check_success_intr(intr, pid, "adb tcpip", flags);
|
|
|
|
}
|
|
|
|
|
2021-11-25 05:39:46 +08:00
|
|
|
bool
|
|
|
|
adb_connect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
|
|
|
const char *const adb_cmd[] = {"connect", ip_port};
|
|
|
|
|
2021-11-26 04:33:50 +08:00
|
|
|
sc_pipe pout;
|
|
|
|
sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
|
|
|
|
if (pid == SC_PROCESS_NONE) {
|
|
|
|
LOGE("Could not execute \"adb connect\"");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "adb connect" always returns successfully (with exit code 0), even in
|
|
|
|
// case of failure. As a workaround, check if its output starts with
|
|
|
|
// "connected".
|
|
|
|
char buf[128];
|
|
|
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
|
|
|
sc_pipe_close(pout);
|
|
|
|
|
|
|
|
bool ok = process_check_success_intr(intr, pid, "adb connect", flags);
|
|
|
|
if (!ok) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ok = !strncmp("connected", buf, sizeof("connected") - 1);
|
|
|
|
if (!ok && !(flags & SC_ADB_NO_STDERR)) {
|
|
|
|
// "adb connect" also prints errors to stdout. Since we capture it,
|
|
|
|
// re-print the error to stderr.
|
|
|
|
sc_str_truncate(buf, r, "\r\n");
|
|
|
|
fprintf(stderr, "%s\n", buf);
|
|
|
|
}
|
|
|
|
return ok;
|
2021-11-25 05:39:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
adb_disconnect(struct sc_intr *intr, const char *ip_port, unsigned flags) {
|
|
|
|
const char *const adb_cmd[] = {"disconnect", ip_port};
|
|
|
|
size_t len = ip_port ? ARRAY_LEN(adb_cmd)
|
|
|
|
: ARRAY_LEN(adb_cmd) - 1;
|
|
|
|
|
|
|
|
sc_pid pid = adb_execute(NULL, adb_cmd, len, flags);
|
|
|
|
return process_check_success_intr(intr, pid, "adb disconnect", flags);
|
|
|
|
}
|
|
|
|
|
2021-11-19 05:08:15 +08:00
|
|
|
char *
|
2021-11-25 05:27:28 +08:00
|
|
|
adb_get_serialno(struct sc_intr *intr, unsigned flags) {
|
2021-11-26 04:58:51 +08:00
|
|
|
const char *const adb_cmd[] = {"get-serialno"};
|
|
|
|
|
2021-11-19 05:08:15 +08:00
|
|
|
sc_pipe pout;
|
2021-11-25 05:27:28 +08:00
|
|
|
sc_pid pid = adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), flags, &pout);
|
2021-11-19 05:08:15 +08:00
|
|
|
if (pid == SC_PROCESS_NONE) {
|
|
|
|
LOGE("Could not execute \"adb get-serialno\"");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[128];
|
|
|
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
|
|
|
sc_pipe_close(pout);
|
|
|
|
|
2021-11-26 05:05:38 +08:00
|
|
|
bool ok = process_check_success_intr(intr, pid, "adb get-serialno", flags);
|
2021-11-19 05:08:15 +08:00
|
|
|
if (!ok) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-11-26 04:35:07 +08:00
|
|
|
if (r == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-19 05:08:15 +08:00
|
|
|
sc_str_truncate(buf, r, " \r\n");
|
|
|
|
|
|
|
|
return strdup(buf);
|
|
|
|
}
|
2021-11-26 05:11:59 +08:00
|
|
|
|
|
|
|
char *
|
|
|
|
adb_get_device_ip(struct sc_intr *intr, const char *serial, unsigned flags) {
|
|
|
|
const char *const cmd[] = {"shell", "ip", "route"};
|
|
|
|
|
|
|
|
sc_pipe pout;
|
|
|
|
sc_pid pid = adb_execute_p(serial, cmd, ARRAY_LEN(cmd), flags, &pout);
|
|
|
|
if (pid == SC_PROCESS_NONE) {
|
|
|
|
LOGD("Could not execute \"ip route\"");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
// "adb shell ip route" output should contain only a few lines
|
|
|
|
char buf[1024];
|
|
|
|
ssize_t r = sc_pipe_read_all_intr(intr, pid, pout, buf, sizeof(buf));
|
|
|
|
sc_pipe_close(pout);
|
|
|
|
|
|
|
|
bool ok = process_check_success_intr(intr, pid, "ip route", flags);
|
|
|
|
if (!ok) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (r == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert((size_t) r <= sizeof(buf));
|
|
|
|
if (r == sizeof(buf) && buf[sizeof(buf) - 1] != '\0') {
|
|
|
|
// The implementation assumes that the output of "ip route" fits in the
|
|
|
|
// buffer in a single pass
|
|
|
|
LOGW("Result of \"ip route\" does not fit in 1Kb. "
|
|
|
|
"Please report an issue.\n");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return sc_adb_parse_device_ip_from_output(buf, r);
|
|
|
|
}
|