📄 transport.c
字号:
}}#elsevoid update_transports(void){ // nothing to do on the device side}#endif // ADB_HOSTtypedef struct tmsg tmsg;struct tmsg{ atransport *transport; int action;};static inttransport_read_action(int fd, struct tmsg* m){ char *p = (char*)m; int len = sizeof(*m); int r; while(len > 0) { r = adb_read(fd, p, len); if(r > 0) { len -= r; p += r; } else { if((r < 0) && (errno == EINTR)) continue; D("transport_read_action: on fd %d, error %d: %s\n", fd, errno, strerror(errno)); return -1; } } return 0;}static inttransport_write_action(int fd, struct tmsg* m){ char *p = (char*)m; int len = sizeof(*m); int r; while(len > 0) { r = adb_write(fd, p, len); if(r > 0) { len -= r; p += r; } else { if((r < 0) && (errno == EINTR)) continue; D("transport_write_action: on fd %d, error %d: %s\n", fd, errno, strerror(errno)); return -1; } } return 0;}static void transport_registration_func(int _fd, unsigned ev, void *data){ tmsg m; adb_thread_t output_thread_ptr; adb_thread_t input_thread_ptr; int s[2]; atransport *t; if(!(ev & FDE_READ)) { return; } if(transport_read_action(_fd, &m)) { fatal_errno("cannot read transport registration socket"); } t = m.transport; if(m.action == 0){ D("transport: %p removing and free'ing %d\n", t, t->transport_socket); /* IMPORTANT: the remove closes one half of the ** socket pair. The close closes the other half. */ fdevent_remove(&(t->transport_fde)); adb_close(t->fd); adb_mutex_lock(&transport_lock); t->next->prev = t->prev; t->prev->next = t->next; adb_mutex_unlock(&transport_lock); run_transport_disconnects(t); if (t->product) free(t->product); if (t->serial) free(t->serial); memset(t,0xee,sizeof(atransport)); free(t); update_transports(); return; } /* initial references are the two threads */ t->ref_count = 2; if(adb_socketpair(s)) { fatal_errno("cannot open transport socketpair"); } D("transport: %p (%d,%d) starting\n", t, s[0], s[1]); t->transport_socket = s[0]; t->fd = s[1]; /* put us on the master device list */ adb_mutex_lock(&transport_lock); t->next = &transport_list; t->prev = transport_list.prev; t->next->prev = t; t->prev->next = t; adb_mutex_unlock(&transport_lock); D("transport: %p install %d\n", t, t->transport_socket ); fdevent_install(&(t->transport_fde), t->transport_socket, transport_socket_events, t); fdevent_set(&(t->transport_fde), FDE_READ); if(adb_thread_create(&input_thread_ptr, input_thread, t)){ fatal_errno("cannot create input thread"); } if(adb_thread_create(&output_thread_ptr, output_thread, t)){ fatal_errno("cannot create output thread"); } t->disconnects.next = t->disconnects.prev = &t->disconnects; update_transports();}void init_transport_registration(void){ int s[2]; if(adb_socketpair(s)){ fatal_errno("cannot open transport registration socketpair"); } transport_registration_send = s[0]; transport_registration_recv = s[1]; fdevent_install(&transport_registration_fde, transport_registration_recv, transport_registration_func, 0); fdevent_set(&transport_registration_fde, FDE_READ);}/* the fdevent select pump is single threaded */static void register_transport(atransport *transport){ tmsg m; m.transport = transport; m.action = 1; D("transport: %p registered\n", transport); if(transport_write_action(transport_registration_send, &m)) { fatal_errno("cannot write transport registration socket\n"); }}static void remove_transport(atransport *transport){ tmsg m; m.transport = transport; m.action = 0; D("transport: %p removed\n", transport); if(transport_write_action(transport_registration_send, &m)) { fatal_errno("cannot write transport registration socket\n"); }}static void transport_unref(atransport *t){ if (t) { adb_mutex_lock(&transport_lock); t->ref_count--; D("transport: %p R- (ref=%d)\n", t, t->ref_count); if (t->ref_count == 0) { D("transport: %p kicking and closing\n", t); if (!t->kicked) { t->kicked = 1; t->kick(t); } t->close(t); remove_transport(t); } adb_mutex_unlock(&transport_lock); }}void add_transport_disconnect(atransport* t, adisconnect* dis){ adb_mutex_lock(&transport_lock); dis->next = &t->disconnects; dis->prev = dis->next->prev; dis->prev->next = dis; dis->next->prev = dis; adb_mutex_unlock(&transport_lock);}void remove_transport_disconnect(atransport* t, adisconnect* dis){ dis->prev->next = dis->next; dis->next->prev = dis->prev; dis->next = dis->prev = dis;}atransport *acquire_one_transport(int state, transport_type ttype, const char* serial, char** error_out){ atransport *t; atransport *result = NULL; int ambiguous = 0;retry: if (error_out) *error_out = "device not found"; adb_mutex_lock(&transport_lock); for (t = transport_list.next; t != &transport_list; t = t->next) { /* check for matching serial number */ if (serial) { if (t->serial && !strcmp(serial, t->serial)) { result = t; break; } } else { if (ttype == kTransportUsb && t->type == kTransportUsb) { if (result) { if (error_out) *error_out = "more than one device"; ambiguous = 1; result = NULL; break; } result = t; } else if (ttype == kTransportLocal && t->type == kTransportLocal) { if (result) { if (error_out) *error_out = "more than one emulator"; ambiguous = 1; result = NULL; break; } result = t; } else if (ttype == kTransportAny) { if (result) { if (error_out) *error_out = "more than one device and emulator"; ambiguous = 1; result = NULL; break; } result = t; } } } adb_mutex_unlock(&transport_lock); if (result) { /* offline devices are ignored -- they are either being born or dying */ if (result && result->connection_state == CS_OFFLINE) { if (error_out) *error_out = "device offline"; result = NULL; } /* check for required connection state */ if (result && state != CS_ANY && result->connection_state != state) { if (error_out) *error_out = "invalid device state"; result = NULL; } } if (result) { /* found one that we can take */ if (error_out) *error_out = NULL; } else if (state != CS_ANY && (serial || !ambiguous)) { adb_sleep_ms(1000); goto retry; } return result;}#if ADB_HOSTstatic const char *statename(atransport *t){ switch(t->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"; default: return "unknown"; }}int list_transports(char *buf, size_t bufsize){ char* p = buf; char* end = buf + bufsize; int len; atransport *t; /* XXX OVERRUN PROBLEMS XXX */ adb_mutex_lock(&transport_lock); for(t = transport_list.next; t != &transport_list; t = t->next) { len = snprintf(p, end - p, "%s\t%s\n", t->serial ? t->serial : "", statename(t)); if (p + len >= end) { /* discard last line if buffer is too short */ break; } p += len; } p[0] = 0; adb_mutex_unlock(&transport_lock); return p - buf;}/* hack for osx */void close_usb_devices(){ atransport *t; adb_mutex_lock(&transport_lock); for(t = transport_list.next; t != &transport_list; t = t->next) { if ( !t->kicked ) { t->kicked = 1; t->kick(t); } } adb_mutex_unlock(&transport_lock);}#endif // ADB_HOSTvoid register_socket_transport(int s, const char *serial, int port){ atransport *t = calloc(1, sizeof(atransport)); D("transport: %p init'ing for socket %d, on port %d\n", t, s, port); if ( init_socket_transport(t, s, port) < 0 ) { adb_close(s); free(t); return; } if(serial) { t->serial = strdup(serial); } register_transport(t);}void register_usb_transport(usb_handle *usb, const char *serial){ atransport *t = calloc(1, sizeof(atransport)); D("transport: %p init'ing for usb_handle %p (sn='%s')\n", t, usb, serial ? serial : ""); init_usb_transport(t, usb); if(serial) { t->serial = strdup(serial); } register_transport(t);}#undef TRACE_TAG#define TRACE_TAG TRACE_RWXint readx(int fd, void *ptr, size_t len){ char *p = ptr; int r;#if ADB_TRACE int len0 = len;#endif D("readx: %d %p %d\n", fd, ptr, (int)len); while(len > 0) { r = adb_read(fd, p, len); if(r > 0) { len -= r; p += r; } else { D("readx: %d %d %s\n", fd, r, strerror(errno)); if((r < 0) && (errno == EINTR)) continue; return -1; } }#if ADB_TRACE D("readx: %d ok: ", fd); dump_hex( ptr, len0 );#endif return 0;}int writex(int fd, const void *ptr, size_t len){ char *p = (char*) ptr; int r;#if ADB_TRACE D("writex: %d %p %d: ", fd, ptr, (int)len); dump_hex( ptr, len );#endif while(len > 0) { r = adb_write(fd, p, len); if(r > 0) { len -= r; p += r; } else { D("writex: %d %d %s\n", fd, r, strerror(errno)); if((r < 0) && (errno == EINTR)) continue; return -1; } } D("writex: %d ok\n", fd); return 0;}int check_header(apacket *p){ if(p->msg.magic != (p->msg.command ^ 0xffffffff)) { D("check_header(): invalid magic\n"); return -1; } if(p->msg.data_length > MAX_PAYLOAD) { D("check_header(): %d > MAX_PAYLOAD\n", p->msg.data_length); return -1; } return 0;}int check_data(apacket *p){ unsigned count, sum; unsigned char *x; count = p->msg.data_length; x = p->data; sum = 0; while(count-- > 0) { sum += *x++; } if(sum != p->msg.data_check) { return -1; } else { return 0; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -