From dcd78a15d0be143d48fc93af6a9fa5748dbf9790 Mon Sep 17 00:00:00 2001 From: Dan Albert Date: Mon, 18 May 2015 16:43:57 -0700 Subject: [PATCH] Make connection states a proper type. Change-Id: I809f9b327c832b88dd63151bf7dcb012d88e81c4 --- adb/adb.cpp | 38 ++++++++++++++++---------------- adb/adb.h | 26 +++++++++++----------- adb/services.cpp | 8 +++---- adb/sockets.cpp | 5 +++-- adb/transport.cpp | 48 ++++++++++++++++++++--------------------- adb/transport.h | 6 +++--- adb/transport_local.cpp | 2 +- adb/transport_usb.cpp | 2 +- 8 files changed, 68 insertions(+), 67 deletions(-) diff --git a/adb/adb.cpp b/adb/adb.cpp index c4e3434c0..3d1b7b40a 100644 --- a/adb/adb.cpp +++ b/adb/adb.cpp @@ -369,24 +369,24 @@ void parse_banner(const char* banner, atransport* t) { const std::string& type = pieces[0]; if (type == "bootloader") { - D("setting connection_state to CS_BOOTLOADER\n"); - t->connection_state = CS_BOOTLOADER; + D("setting connection_state to kCsBootloader\n"); + t->connection_state = kCsBootloader; update_transports(); } else if (type == "device") { - D("setting connection_state to CS_DEVICE\n"); - t->connection_state = CS_DEVICE; + D("setting connection_state to kCsDevice\n"); + t->connection_state = kCsDevice; update_transports(); } else if (type == "recovery") { - D("setting connection_state to CS_RECOVERY\n"); - t->connection_state = CS_RECOVERY; + D("setting connection_state to kCsRecovery\n"); + t->connection_state = kCsRecovery; update_transports(); } else if (type == "sideload") { - D("setting connection_state to CS_SIDELOAD\n"); - t->connection_state = CS_SIDELOAD; + D("setting connection_state to kCsSideload\n"); + t->connection_state = kCsSideload; update_transports(); } else { - D("setting connection_state to CS_HOST\n"); - t->connection_state = CS_HOST; + D("setting connection_state to kCsHost\n"); + t->connection_state = kCsHost; } } @@ -406,7 +406,7 @@ void handle_packet(apacket *p, atransport *t) send_packet(p, t); if(HOST) send_connect(t); } else { - t->connection_state = CS_OFFLINE; + t->connection_state = kCsOffline; handle_offline(t); send_packet(p, t); } @@ -414,8 +414,8 @@ void handle_packet(apacket *p, atransport *t) case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */ /* XXX verify version, etc */ - if(t->connection_state != CS_OFFLINE) { - t->connection_state = CS_OFFLINE; + if(t->connection_state != kCsOffline) { + t->connection_state = kCsOffline; handle_offline(t); } @@ -431,7 +431,7 @@ void handle_packet(apacket *p, atransport *t) case A_AUTH: if (p->msg.arg0 == ADB_AUTH_TOKEN) { - t->connection_state = CS_UNAUTHORIZED; + t->connection_state = kCsUnauthorized; t->key = adb_auth_nextkey(t->key); if (t->key) { send_auth_response(p->data, p->msg.data_length, t); @@ -757,7 +757,7 @@ int handle_forward_request(const char* service, TransportType type, const char* } std::string error_msg; - transport = acquire_one_transport(CS_ANY, type, serial, &error_msg); + transport = acquire_one_transport(kCsAny, type, serial, &error_msg); if (!transport) { SendFail(reply_fd, error_msg); return 1; @@ -826,7 +826,7 @@ int handle_host_request(const char* service, TransportType type, } std::string error_msg = "unknown failure"; - transport = acquire_one_transport(CS_ANY, type, serial, &error_msg); + transport = acquire_one_transport(kCsAny, type, serial, &error_msg); if (transport) { s->transport = transport; @@ -889,7 +889,7 @@ int handle_host_request(const char* service, TransportType type, if(!strncmp(service,"get-serialno",strlen("get-serialno"))) { const char *out = "unknown"; - transport = acquire_one_transport(CS_ANY, type, serial, NULL); + transport = acquire_one_transport(kCsAny, type, serial, NULL); if (transport && transport->serial) { out = transport->serial; } @@ -899,7 +899,7 @@ int handle_host_request(const char* service, TransportType type, } if(!strncmp(service,"get-devpath",strlen("get-devpath"))) { const char *out = "unknown"; - transport = acquire_one_transport(CS_ANY, type, serial, NULL); + transport = acquire_one_transport(kCsAny, type, serial, NULL); if (transport && transport->devpath) { out = transport->devpath; } @@ -916,7 +916,7 @@ int handle_host_request(const char* service, TransportType type, } if(!strncmp(service,"get-state",strlen("get-state"))) { - transport = acquire_one_transport(CS_ANY, type, serial, NULL); + transport = acquire_one_transport(kCsAny, type, serial, NULL); SendOkay(reply_fd); SendProtocolString(reply_fd, transport->connection_state_name()); return 0; diff --git a/adb/adb.h b/adb/adb.h index 7942a8639..e8960e3b6 100644 --- a/adb/adb.h +++ b/adb/adb.h @@ -170,6 +170,18 @@ enum TransportType { #define TOKEN_SIZE 20 +enum ConnectionState { + kCsAny = -1, + kCsOffline = 0, + kCsBootloader, + kCsDevice, + kCsHost, + kCsRecovery, + kCsNoPerm, // Insufficient permissions to communicate with the device. + kCsSideload, + kCsUnauthorized, +}; + struct atransport { atransport *next; @@ -266,7 +278,7 @@ int adb_main(int is_daemon, int server_port); int get_available_local_transport_index(); #endif int init_socket_transport(atransport *t, int s, int port, int local); -void init_usb_transport(atransport *t, usb_handle *usb, int state); +void init_usb_transport(atransport *t, usb_handle *usb, ConnectionState state); #if ADB_HOST atransport* find_emulator_transport_by_adb_port(int adb_port); @@ -336,17 +348,7 @@ int is_adb_interface(int vid, int pid, int usb_class, int usb_subclass, int usb_ int adb_commandline(int argc, const char **argv); -int connection_state(atransport *t); - -#define CS_ANY -1 -#define CS_OFFLINE 0 -#define CS_BOOTLOADER 1 -#define CS_DEVICE 2 -#define CS_HOST 3 -#define CS_RECOVERY 4 -#define CS_NOPERM 5 /* Insufficient permissions to communicate with the device */ -#define CS_SIDELOAD 6 -#define CS_UNAUTHORIZED 7 +ConnectionState connection_state(atransport *t); extern const char *adb_device_banner; extern int HOST; diff --git a/adb/services.cpp b/adb/services.cpp index 8ce9f7136..a9edbe59f 100644 --- a/adb/services.cpp +++ b/adb/services.cpp @@ -516,7 +516,7 @@ int service_to_fd(const char *name) struct state_info { TransportType transport_type; char* serial; - int state; + ConnectionState state; }; static void wait_for_state(int fd, void* cookie) @@ -665,13 +665,13 @@ asocket* host_service_to_socket(const char* name, const char *serial) if (!strncmp(name, "local", strlen("local"))) { sinfo->transport_type = kTransportLocal; - sinfo->state = CS_DEVICE; + sinfo->state = kCsDevice; } else if (!strncmp(name, "usb", strlen("usb"))) { sinfo->transport_type = kTransportUsb; - sinfo->state = CS_DEVICE; + sinfo->state = kCsDevice; } else if (!strncmp(name, "any", strlen("any"))) { sinfo->transport_type = kTransportAny; - sinfo->state = CS_DEVICE; + sinfo->state = kCsDevice; } else { if (sinfo->serial) { free(sinfo->serial); diff --git a/adb/sockets.cpp b/adb/sockets.cpp index 62cba6d3b..621944e71 100644 --- a/adb/sockets.cpp +++ b/adb/sockets.cpp @@ -815,7 +815,8 @@ static int smart_socket_enqueue(asocket *s, apacket *p) #else /* !ADB_HOST */ if (s->transport == NULL) { std::string error_msg = "unknown failure"; - s->transport = acquire_one_transport(CS_ANY, kTransportAny, NULL, &error_msg); + s->transport = + acquire_one_transport(kCsAny, kTransportAny, NULL, &error_msg); if (s->transport == NULL) { SendFail(s->peer->fd, error_msg); @@ -824,7 +825,7 @@ static int smart_socket_enqueue(asocket *s, apacket *p) } #endif - if(!(s->transport) || (s->transport->connection_state == CS_OFFLINE)) { + if(!(s->transport) || (s->transport->connection_state == kCsOffline)) { /* if there's no remote we fail the connection ** right here and terminate it */ diff --git a/adb/transport.cpp b/adb/transport.cpp index 29cf580c6..818ed97a4 100644 --- a/adb/transport.cpp +++ b/adb/transport.cpp @@ -578,7 +578,7 @@ static void transport_registration_func(int _fd, unsigned ev, void *data) } /* don't create transport threads for inaccessible devices */ - if (t->connection_state != CS_NOPERM) { + if (t->connection_state != kCsNoPerm) { /* initial references are the two threads */ t->ref_count = 2; @@ -738,9 +738,8 @@ static int qual_match(const char *to_test, return !*to_test; } -atransport* acquire_one_transport(int state, TransportType type, - const char* serial, std::string* error_out) -{ +atransport* acquire_one_transport(ConnectionState state, TransportType type, + const char* serial, std::string* error_out) { atransport *t; atransport *result = NULL; int ambiguous = 0; @@ -750,7 +749,7 @@ retry: adb_mutex_lock(&transport_lock); for (t = transport_list.next; t != &transport_list; t = t->next) { - if (t->connection_state == CS_NOPERM) { + if (t->connection_state == kCsNoPerm) { if (error_out) *error_out = "insufficient permissions for device"; continue; } @@ -801,7 +800,7 @@ retry: adb_mutex_unlock(&transport_lock); if (result) { - if (result->connection_state == CS_UNAUTHORIZED) { + if (result->connection_state == kCsUnauthorized) { if (error_out) { *error_out = "device unauthorized.\n"; char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS"); @@ -814,13 +813,13 @@ retry: } /* offline devices are ignored -- they are either being born or dying */ - if (result && result->connection_state == CS_OFFLINE) { + if (result && result->connection_state == kCsOffline) { if (error_out) *error_out = "device offline"; result = NULL; } /* check for required connection state */ - if (result && state != CS_ANY && result->connection_state != state) { + if (result && state != kCsAny && result->connection_state != state) { if (error_out) *error_out = "invalid device state"; result = NULL; } @@ -829,7 +828,7 @@ retry: if (result) { /* found one that we can take */ if (error_out) *error_out = "success"; - } else if (state != CS_ANY && (serial || !ambiguous)) { + } else if (state != kCsAny && (serial || !ambiguous)) { adb_sleep_ms(1000); goto retry; } @@ -839,14 +838,14 @@ retry: const char* atransport::connection_state_name() const { switch (connection_state) { - case CS_OFFLINE: return "offline"; - case CS_BOOTLOADER: return "bootloader"; - case CS_DEVICE: return "device"; - case CS_HOST: return "host"; - case CS_RECOVERY: return "recovery"; - case CS_NOPERM: return "no permissions"; - case CS_SIDELOAD: return "sideload"; - case CS_UNAUTHORIZED: return "unauthorized"; + case kCsOffline: return "offline"; + case kCsBootloader: return "bootloader"; + case kCsDevice: return "device"; + case kCsHost: return "host"; + case kCsRecovery: return "recovery"; + case kCsNoPerm: return "no permissions"; + case kCsSideload: return "sideload"; + case kCsUnauthorized: return "unauthorized"; default: return "unknown"; } } @@ -1021,7 +1020,7 @@ void register_usb_transport(usb_handle *usb, const char *serial, const char *dev if (t == nullptr) fatal("cannot allocate USB atransport"); D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb, serial ? serial : ""); - init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM)); + init_usb_transport(t, usb, (writeable ? kCsOffline : kCsNoPerm)); if(serial) { t->serial = strdup(serial); } @@ -1039,18 +1038,17 @@ void register_usb_transport(usb_handle *usb, const char *serial, const char *dev register_transport(t); } -/* this should only be used for transports with connection_state == CS_NOPERM */ -void unregister_usb_transport(usb_handle *usb) -{ - atransport *t; +// This should only be used for transports with connection_state == kCsNoPerm. +void unregister_usb_transport(usb_handle* usb) { adb_mutex_lock(&transport_lock); - for(t = transport_list.next; t != &transport_list; t = t->next) { - if (t->usb == usb && t->connection_state == CS_NOPERM) { + for (atransport* t = transport_list.next; t != &transport_list; + t = t->next) { + if (t->usb == usb && t->connection_state == kCsNoPerm) { t->next->prev = t->prev; t->prev->next = t->next; break; } - } + } adb_mutex_unlock(&transport_lock); } diff --git a/adb/transport.h b/adb/transport.h index 7b799b95d..538f63ecb 100644 --- a/adb/transport.h +++ b/adb/transport.h @@ -25,11 +25,11 @@ /* * Obtain a transport from the available transports. - * If state is != CS_ANY, only transports in that state are considered. + * If state is != kCsAny, only transports in that state are considered. * If serial is non-NULL then only the device with that serial will be chosen. * If no suitable transport is found, error is set. */ -atransport* acquire_one_transport(int state, TransportType type, +atransport* acquire_one_transport(ConnectionState state, TransportType type, const char* serial, std::string* error_out); void add_transport_disconnect(atransport* t, adisconnect* dis); void remove_transport_disconnect(atransport* t, adisconnect* dis); @@ -50,7 +50,7 @@ void register_usb_transport(usb_handle* h, const char* serial, /* cause new transports to be init'd and added to the list */ int register_socket_transport(int s, const char* serial, int port, int local); -/* this should only be used for transports with connection_state == CS_NOPERM */ +// This should only be used for transports with connection_state == kCsNoPerm. void unregister_usb_transport(usb_handle* usb); /* these should only be used for the "adb disconnect" command */ diff --git a/adb/transport_local.cpp b/adb/transport_local.cpp index 5f7449d22..d3c4c30a3 100644 --- a/adb/transport_local.cpp +++ b/adb/transport_local.cpp @@ -387,7 +387,7 @@ int init_socket_transport(atransport *t, int s, int adb_port, int local) t->write_to_remote = remote_write; t->sfd = s; t->sync_token = 1; - t->connection_state = CS_OFFLINE; + t->connection_state = kCsOffline; t->type = kTransportLocal; t->adb_port = 0; diff --git a/adb/transport_usb.cpp b/adb/transport_usb.cpp index cdabffe1e..eb3454d1c 100644 --- a/adb/transport_usb.cpp +++ b/adb/transport_usb.cpp @@ -80,7 +80,7 @@ static void remote_kick(atransport *t) usb_kick(t->usb); } -void init_usb_transport(atransport *t, usb_handle *h, int state) +void init_usb_transport(atransport *t, usb_handle *h, ConnectionState state) { D("transport: usb\n"); t->close = remote_close;