Refactor device configuration

Depending on the parameters passed to scrcpy, either the initial device
serial is necessary or not. Reorganize to simplify the logic.

PR #3005 <https://github.com/Genymobile/scrcpy/pull/3005>
This commit is contained in:
Romain Vimont 2022-02-05 21:20:12 +01:00
parent 700503df6c
commit 85ff70fc95

View file

@ -641,35 +641,24 @@ sc_server_connect_to_tcpip(struct sc_server *server, const char *ip_port) {
return true; return true;
} }
static bool static bool
sc_server_configure_tcpip(struct sc_server *server) { sc_server_configure_tcpip_known_address(struct sc_server *server,
char *ip_port; const char *addr) {
const struct sc_server_params *params = &server->params;
// If tcpip parameter is given, then it must connect to this address.
// Therefore, the device is unknown, so serial is meaningless at this point.
assert(!params->req_serial || !params->tcpip_dst);
if (params->tcpip_dst) {
// Append ":5555" if no port is present // Append ":5555" if no port is present
bool contains_port = strchr(params->tcpip_dst, ':'); bool contains_port = strchr(addr, ':');
ip_port = contains_port ? strdup(params->tcpip_dst) char *ip_port = contains_port ? strdup(addr) : append_port_5555(addr);
: append_port_5555(params->tcpip_dst);
if (!ip_port) { if (!ip_port) {
LOG_OOM(); LOG_OOM();
return false; return false;
} }
} else {
// The device IP address must be retrieved from the current
// connected device
char *serial = sc_server_read_serial(server);
if (!serial) {
LOGE("Could not get device serial");
return false;
}
server->serial = ip_port;
return sc_server_connect_to_tcpip(server, ip_port);
}
static bool
sc_server_configure_tcpip_unknown_address(struct sc_server *server,
const char *serial) {
// The serial is either the real serial when connected via USB, or // The serial is either the real serial when connected via USB, or
// the IP:PORT when connected over TCP/IP. Only the latter contains // the IP:PORT when connected over TCP/IP. Only the latter contains
// a colon. // a colon.
@ -677,16 +666,13 @@ sc_server_configure_tcpip(struct sc_server *server) {
if (is_already_tcpip) { if (is_already_tcpip) {
// Nothing to do // Nothing to do
LOGI("Device already connected via TCP/IP: %s", serial); LOGI("Device already connected via TCP/IP: %s", serial);
free(serial);
return true; return true;
} }
ip_port = sc_server_switch_to_tcpip(server, serial); char *ip_port = sc_server_switch_to_tcpip(server, serial);
free(serial);
if (!ip_port) { if (!ip_port) {
return false; return false;
} }
}
server->serial = ip_port; server->serial = ip_port;
return sc_server_connect_to_tcpip(server, ip_port); return sc_server_connect_to_tcpip(server, ip_port);
@ -698,16 +684,40 @@ run_server(void *data) {
const struct sc_server_params *params = &server->params; const struct sc_server_params *params = &server->params;
// params->tcpip_dst implies params->tcpip
assert(!params->tcpip_dst || params->tcpip);
// If tcpip_dst parameter is given, then it must connect to this address.
// Therefore, the device is unknown, so serial is meaningless at this point.
assert(!params->req_serial || !params->tcpip_dst);
// A device must be selected via a serial in all cases except when --tcpip=
// is called with a parameter (in that case, the device may initially not
// exist, and scrcpy will execute "adb connect").
bool need_initial_serial = !params->tcpip_dst;
bool ok;
if (need_initial_serial) {
char *serial = sc_server_read_serial(server);
if (!serial) {
LOGE("Could not get device serial");
goto error_connection_failed;
}
if (params->tcpip) { if (params->tcpip) {
bool ok = sc_server_configure_tcpip(server); assert(!params->tcpip_dst);
ok = sc_server_configure_tcpip_unknown_address(server, serial);
free(serial);
if (!ok) { if (!ok) {
goto error_connection_failed; goto error_connection_failed;
} }
assert(server->serial); assert(server->serial);
} else { } else {
server->serial = sc_server_read_serial(server); server->serial = serial;
if (!server->serial) { }
LOGD("Could not get device serial"); } else {
ok = sc_server_configure_tcpip_known_address(server, params->tcpip_dst);
if (!ok) {
goto error_connection_failed; goto error_connection_failed;
} }
} }
@ -716,7 +726,7 @@ run_server(void *data) {
assert(serial); assert(serial);
LOGD("Device serial: %s", serial); LOGD("Device serial: %s", serial);
bool ok = push_server(&server->intr, serial); ok = push_server(&server->intr, serial);
if (!ok) { if (!ok) {
goto error_connection_failed; goto error_connection_failed;
} }