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

Commit d92fd41f authored by Dan Albert's avatar Dan Albert Committed by Gerrit Code Review
Browse files

Merge "Make connection states a proper type."

parents 155f7ac7 dcd78a15
Loading
Loading
Loading
Loading
+19 −19
Original line number Diff line number Diff line
@@ -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;
+14 −12
Original line number Diff line number Diff line
@@ -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;
+4 −4
Original line number Diff line number Diff line
@@ -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);
+3 −2
Original line number Diff line number Diff line
@@ -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
            */
+23 −25
Original line number 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 */
    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,13 +1038,12 @@ 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;
Loading