Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit dfd1a3d4 authored by Dan Albert's avatar Dan Albert Committed by Android Git Automerger
Browse files

am d92fd41f: Merge "Make connection states a proper type."

* commit 'd92fd41f':
  Make connection states a proper type.
parents 3896033f d92fd41f
Loading
Loading
Loading
Loading
+19 −19
Original line number Original line Diff line number Diff line
@@ -369,24 +369,24 @@ void parse_banner(const char* banner, atransport* t) {


    const std::string& type = pieces[0];
    const std::string& type = pieces[0];
    if (type == "bootloader") {
    if (type == "bootloader") {
        D("setting connection_state to CS_BOOTLOADER\n");
        D("setting connection_state to kCsBootloader\n");
        t->connection_state = CS_BOOTLOADER;
        t->connection_state = kCsBootloader;
        update_transports();
        update_transports();
    } else if (type == "device") {
    } else if (type == "device") {
        D("setting connection_state to CS_DEVICE\n");
        D("setting connection_state to kCsDevice\n");
        t->connection_state = CS_DEVICE;
        t->connection_state = kCsDevice;
        update_transports();
        update_transports();
    } else if (type == "recovery") {
    } else if (type == "recovery") {
        D("setting connection_state to CS_RECOVERY\n");
        D("setting connection_state to kCsRecovery\n");
        t->connection_state = CS_RECOVERY;
        t->connection_state = kCsRecovery;
        update_transports();
        update_transports();
    } else if (type == "sideload") {
    } else if (type == "sideload") {
        D("setting connection_state to CS_SIDELOAD\n");
        D("setting connection_state to kCsSideload\n");
        t->connection_state = CS_SIDELOAD;
        t->connection_state = kCsSideload;
        update_transports();
        update_transports();
    } else {
    } else {
        D("setting connection_state to CS_HOST\n");
        D("setting connection_state to kCsHost\n");
        t->connection_state = CS_HOST;
        t->connection_state = kCsHost;
    }
    }
}
}


@@ -406,7 +406,7 @@ void handle_packet(apacket *p, atransport *t)
            send_packet(p, t);
            send_packet(p, t);
            if(HOST) send_connect(t);
            if(HOST) send_connect(t);
        } else {
        } else {
            t->connection_state = CS_OFFLINE;
            t->connection_state = kCsOffline;
            handle_offline(t);
            handle_offline(t);
            send_packet(p, 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") */
    case A_CNXN: /* CONNECT(version, maxdata, "system-id-string") */
            /* XXX verify version, etc */
            /* XXX verify version, etc */
        if(t->connection_state != CS_OFFLINE) {
        if(t->connection_state != kCsOffline) {
            t->connection_state = CS_OFFLINE;
            t->connection_state = kCsOffline;
            handle_offline(t);
            handle_offline(t);
        }
        }


@@ -431,7 +431,7 @@ void handle_packet(apacket *p, atransport *t)


    case A_AUTH:
    case A_AUTH:
        if (p->msg.arg0 == ADB_AUTH_TOKEN) {
        if (p->msg.arg0 == ADB_AUTH_TOKEN) {
            t->connection_state = CS_UNAUTHORIZED;
            t->connection_state = kCsUnauthorized;
            t->key = adb_auth_nextkey(t->key);
            t->key = adb_auth_nextkey(t->key);
            if (t->key) {
            if (t->key) {
                send_auth_response(p->data, p->msg.data_length, t);
                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;
        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) {
        if (!transport) {
            SendFail(reply_fd, error_msg);
            SendFail(reply_fd, error_msg);
            return 1;
            return 1;
@@ -826,7 +826,7 @@ int handle_host_request(const char* service, TransportType type,
        }
        }


        std::string error_msg = "unknown failure";
        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) {
        if (transport) {
            s->transport = 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"))) {
    if(!strncmp(service,"get-serialno",strlen("get-serialno"))) {
        const char *out = "unknown";
        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) {
        if (transport && transport->serial) {
            out = 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"))) {
    if(!strncmp(service,"get-devpath",strlen("get-devpath"))) {
        const char *out = "unknown";
        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) {
        if (transport && transport->devpath) {
            out = 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"))) {
    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);
        SendOkay(reply_fd);
        SendProtocolString(reply_fd, transport->connection_state_name());
        SendProtocolString(reply_fd, transport->connection_state_name());
        return 0;
        return 0;
+14 −12
Original line number Original line Diff line number Diff line
@@ -170,6 +170,18 @@ enum TransportType {


#define TOKEN_SIZE 20
#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
struct atransport
{
{
    atransport *next;
    atransport *next;
@@ -266,7 +278,7 @@ int adb_main(int is_daemon, int server_port);
int get_available_local_transport_index();
int get_available_local_transport_index();
#endif
#endif
int  init_socket_transport(atransport *t, int s, int port, int local);
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
#if ADB_HOST
atransport* find_emulator_transport_by_adb_port(int adb_port);
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 adb_commandline(int argc, const char **argv);


int connection_state(atransport *t);
ConnectionState 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


extern const char *adb_device_banner;
extern const char *adb_device_banner;
extern int HOST;
extern int HOST;
+4 −4
Original line number Original line Diff line number Diff line
@@ -516,7 +516,7 @@ int service_to_fd(const char *name)
struct state_info {
struct state_info {
    TransportType transport_type;
    TransportType transport_type;
    char* serial;
    char* serial;
    int state;
    ConnectionState state;
};
};


static void wait_for_state(int fd, void* cookie)
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"))) {
        if (!strncmp(name, "local", strlen("local"))) {
            sinfo->transport_type = kTransportLocal;
            sinfo->transport_type = kTransportLocal;
            sinfo->state = CS_DEVICE;
            sinfo->state = kCsDevice;
        } else if (!strncmp(name, "usb", strlen("usb"))) {
        } else if (!strncmp(name, "usb", strlen("usb"))) {
            sinfo->transport_type = kTransportUsb;
            sinfo->transport_type = kTransportUsb;
            sinfo->state = CS_DEVICE;
            sinfo->state = kCsDevice;
        } else if (!strncmp(name, "any", strlen("any"))) {
        } else if (!strncmp(name, "any", strlen("any"))) {
            sinfo->transport_type = kTransportAny;
            sinfo->transport_type = kTransportAny;
            sinfo->state = CS_DEVICE;
            sinfo->state = kCsDevice;
        } else {
        } else {
            if (sinfo->serial) {
            if (sinfo->serial) {
                free(sinfo->serial);
                free(sinfo->serial);
+3 −2
Original line number Original line Diff line number Diff line
@@ -815,7 +815,8 @@ static int smart_socket_enqueue(asocket *s, apacket *p)
#else /* !ADB_HOST */
#else /* !ADB_HOST */
    if (s->transport == NULL) {
    if (s->transport == NULL) {
        std::string error_msg = "unknown failure";
        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) {
        if (s->transport == NULL) {
            SendFail(s->peer->fd, error_msg);
            SendFail(s->peer->fd, error_msg);
@@ -824,7 +825,7 @@ static int smart_socket_enqueue(asocket *s, apacket *p)
    }
    }
#endif
#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
           /* if there's no remote we fail the connection
            ** right here and terminate it
            ** right here and terminate it
            */
            */
+23 −25
Original line number Original line Diff line number Diff line
@@ -578,7 +578,7 @@ static void transport_registration_func(int _fd, unsigned ev, void *data)
    }
    }


    /* don't create transport threads for inaccessible devices */
    /* 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 */
        /* initial references are the two threads */
        t->ref_count = 2;
        t->ref_count = 2;


@@ -738,9 +738,8 @@ static int qual_match(const char *to_test,
    return !*to_test;
    return !*to_test;
}
}


atransport* acquire_one_transport(int state, TransportType type,
atransport* acquire_one_transport(ConnectionState state, TransportType type,
                                  const char* serial, std::string* error_out)
                                  const char* serial, std::string* error_out) {
{
    atransport *t;
    atransport *t;
    atransport *result = NULL;
    atransport *result = NULL;
    int ambiguous = 0;
    int ambiguous = 0;
@@ -750,7 +749,7 @@ retry:


    adb_mutex_lock(&transport_lock);
    adb_mutex_lock(&transport_lock);
    for (t = transport_list.next; t != &transport_list; t = t->next) {
    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";
            if (error_out) *error_out = "insufficient permissions for device";
            continue;
            continue;
        }
        }
@@ -801,7 +800,7 @@ retry:
    adb_mutex_unlock(&transport_lock);
    adb_mutex_unlock(&transport_lock);


    if (result) {
    if (result) {
        if (result->connection_state == CS_UNAUTHORIZED) {
        if (result->connection_state == kCsUnauthorized) {
            if (error_out) {
            if (error_out) {
                *error_out = "device unauthorized.\n";
                *error_out = "device unauthorized.\n";
                char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
                char* ADB_VENDOR_KEYS = getenv("ADB_VENDOR_KEYS");
@@ -814,13 +813,13 @@ retry:
        }
        }


        /* offline devices are ignored -- they are either being born or dying */
        /* 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";
            if (error_out) *error_out = "device offline";
            result = NULL;
            result = NULL;
        }
        }


        /* check for required connection state */
        /* 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";
            if (error_out) *error_out = "invalid device state";
            result = NULL;
            result = NULL;
        }
        }
@@ -829,7 +828,7 @@ retry:
    if (result) {
    if (result) {
        /* found one that we can take */
        /* found one that we can take */
        if (error_out) *error_out = "success";
        if (error_out) *error_out = "success";
    } else if (state != CS_ANY && (serial || !ambiguous)) {
    } else if (state != kCsAny && (serial || !ambiguous)) {
        adb_sleep_ms(1000);
        adb_sleep_ms(1000);
        goto retry;
        goto retry;
    }
    }
@@ -839,14 +838,14 @@ retry:


const char* atransport::connection_state_name() const {
const char* atransport::connection_state_name() const {
    switch (connection_state) {
    switch (connection_state) {
    case CS_OFFLINE: return "offline";
    case kCsOffline: return "offline";
    case CS_BOOTLOADER: return "bootloader";
    case kCsBootloader: return "bootloader";
    case CS_DEVICE: return "device";
    case kCsDevice: return "device";
    case CS_HOST: return "host";
    case kCsHost: return "host";
    case CS_RECOVERY: return "recovery";
    case kCsRecovery: return "recovery";
    case CS_NOPERM: return "no permissions";
    case kCsNoPerm: return "no permissions";
    case CS_SIDELOAD: return "sideload";
    case kCsSideload: return "sideload";
    case CS_UNAUTHORIZED: return "unauthorized";
    case kCsUnauthorized: return "unauthorized";
    default: return "unknown";
    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");
    if (t == nullptr) fatal("cannot allocate USB atransport");
    D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb,
    D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb,
      serial ? serial : "");
      serial ? serial : "");
    init_usb_transport(t, usb, (writeable ? CS_OFFLINE : CS_NOPERM));
    init_usb_transport(t, usb, (writeable ? kCsOffline : kCsNoPerm));
    if(serial) {
    if(serial) {
        t->serial = strdup(serial);
        t->serial = strdup(serial);
    }
    }
@@ -1039,13 +1038,12 @@ void register_usb_transport(usb_handle *usb, const char *serial, const char *dev
    register_transport(t);
    register_transport(t);
}
}


/* 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)
void unregister_usb_transport(usb_handle* usb) {
{
    atransport *t;
    adb_mutex_lock(&transport_lock);
    adb_mutex_lock(&transport_lock);
    for(t = transport_list.next; t != &transport_list; t = t->next) {
    for (atransport* t = transport_list.next; t != &transport_list;
        if (t->usb == usb && t->connection_state == CS_NOPERM) {
         t = t->next) {
        if (t->usb == usb && t->connection_state == kCsNoPerm) {
            t->next->prev = t->prev;
            t->next->prev = t->prev;
            t->prev->next = t->next;
            t->prev->next = t->next;
            break;
            break;
Loading