From b7559744a7164124e7d358d2cb3bd744993487a2 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Thu, 18 Nov 2021 22:08:15 +0100 Subject: [PATCH] Expose new user-friendly adb functions Expose interruptible adb functions which return the expected result directly, without exposing the process (sc_pid) to the caller. --- app/src/adb.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++ app/src/adb.h | 32 ++++++++++++++++++++++++ 2 files changed, 100 insertions(+) diff --git a/app/src/adb.c b/app/src/adb.c index 608abca1..92a52b5b 100644 --- a/app/src/adb.c +++ b/app/src/adb.c @@ -7,6 +7,7 @@ #include "util/file.h" #include "util/log.h" +#include "util/process_intr.h" #include "util/str.h" static const char *adb_command; @@ -238,3 +239,70 @@ adb_exec_get_serialno(sc_pipe *pout) { const char *const adb_cmd[] = {"get-serialno"}; return adb_execute_p(NULL, adb_cmd, ARRAY_LEN(adb_cmd), pout); } + +bool +adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port, + const char *device_socket_name) { + sc_pid pid = adb_exec_forward(serial, local_port, device_socket_name); + return sc_process_check_success_intr(intr, pid, "adb forward", true); +} + +bool +adb_forward_remove(struct sc_intr *intr, const char *serial, + uint16_t local_port) { + sc_pid pid = adb_exec_forward_remove(serial, local_port); + return sc_process_check_success_intr(intr, pid, "adb forward --remove", + true); +} + +bool +adb_reverse(struct sc_intr *intr, const char *serial, + const char *device_socket_name, uint16_t local_port) { + sc_pid pid = adb_exec_reverse(serial, device_socket_name, local_port); + return sc_process_check_success_intr(intr, pid, "adb reverse", true); +} + +bool +adb_reverse_remove(struct sc_intr *intr, const char *serial, + const char *device_socket_name) { + sc_pid pid = adb_exec_reverse_remove(serial, device_socket_name); + return sc_process_check_success_intr(intr, pid, "adb reverse --remove", + true); +} + +bool +adb_push(struct sc_intr *intr, const char *serial, const char *local, + const char *remote) { + sc_pid pid = adb_exec_push(serial, local, remote); + return sc_process_check_success_intr(intr, pid, "adb push", true); +} + +bool +adb_install(struct sc_intr *intr, const char *serial, const char *local) { + sc_pid pid = adb_exec_install(serial, local); + return sc_process_check_success_intr(intr, pid, "adb install", true); +} + +char * +adb_get_serialno(struct sc_intr *intr) { + sc_pipe pout; + sc_pid pid = adb_exec_get_serialno(&pout); + 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); + + bool ok = + sc_process_check_success_intr(intr, pid, "adb get-serialno", true); + if (!ok) { + return NULL; + } + + sc_str_truncate(buf, r, " \r\n"); + + return strdup(buf); +} diff --git a/app/src/adb.h b/app/src/adb.h index c2c21343..3714c17f 100644 --- a/app/src/adb.h +++ b/app/src/adb.h @@ -7,6 +7,7 @@ #include #include "util/process.h" +#include "util/intr.h" sc_pid adb_execute(const char *serial, const char *const adb_cmd[], size_t len); @@ -43,4 +44,35 @@ adb_exec_install(const char *serial, const char *local); sc_pid adb_exec_get_serialno(sc_pipe *pout); +bool +adb_forward(struct sc_intr *intr, const char *serial, uint16_t local_port, + const char *device_socket_name); + +bool +adb_forward_remove(struct sc_intr *intr, const char *serial, + uint16_t local_port); + +bool +adb_reverse(struct sc_intr *intr, const char *serial, + const char *device_socket_name, uint16_t local_port); + +bool +adb_reverse_remove(struct sc_intr *intr, const char *serial, + const char *device_socket_name); + +bool +adb_push(struct sc_intr *intr, const char *serial, const char *local, + const char *remote); + +bool +adb_install(struct sc_intr *intr, const char *serial, const char *local); + +/** + * Execute `adb get-serialno` + * + * Return the result, to be freed by the caller, or NULL on error. + */ +char * +adb_get_serialno(struct sc_intr *intr); + #endif