void usb_init() { if (!initialized) { adb_thread_t tid; notificationIterators = (io_iterator_t*)malloc( vendorIdCount * sizeof(io_iterator_t)); adb_mutex_init(&start_lock, NULL); adb_cond_init(&start_cond, NULL); if(adb_thread_create(&tid, RunLoopThread, NULL)) fatal_errno("cannot create input thread"); // Wait for initialization to finish adb_mutex_lock(&start_lock); adb_cond_wait(&start_cond, &start_lock); adb_mutex_unlock(&start_lock); adb_mutex_destroy(&start_lock); adb_cond_destroy(&start_cond); initialized = 1; } }
void* RunLoopThread(void* args) { unsigned i; InitUSB(); currentRunLoop = CFRunLoopGetCurrent(); CFRunLoopTimerContext context = {0, NULL, NULL, NULL, NULL}; CFRunLoopTimerRef timer = CFRunLoopTimerCreate(kCFAllocatorDefault, 0.1, 0.1, 0, 0, &onTimerFired, &context); CFRunLoopAddTimer(currentRunLoop, timer, kCFRunLoopCommonModes); // Signal the parent that we are running adb_mutex_lock(&start_lock); adb_cond_signal(&start_cond); adb_mutex_unlock(&start_lock); CFRunLoopRun(); currentRunLoop = 0; for (i = 0; i < vendorIdCount; i++) { IOObjectRelease(notificationIterators[i]); } IONotificationPortDestroy(notificationPort); usb_cleanup(); DBG("RunLoopThread done\n"); return NULL; }
// unregisters all non-emulator TCP transports void unregister_all_tcp_transports() { #ifdef WIN32 printf("Legacy\n"); abort(); #else atransport *t, *next; adb_mutex_lock(&transport_lock); for (t = transport_list.next; t != &transport_list; t = next) { next = t->next; if (t->type == kTransportLocal && t->adb_port == 0) { t->next->prev = t->prev; t->prev->next = next; // we cannot call kick_transport when holding transport_lock if (!t->kicked) { t->kicked = 1; t->kick(t); } transport_unref_locked(t); } } adb_mutex_unlock(&transport_lock); #endif }
void usb_init(int (*spawnD)()) { if (!initialized) { notificationIterators = (io_iterator_t*)malloc( vendorIdCount * sizeof(io_iterator_t)); adb_mutex_init(&start_lock, NULL); adb_cond_init(&start_cond, NULL); D("Before thread_create"); if(spawnD()) fatal_errno("cannot create RunLoopThread"); D("After thread_create"); // Wait for initialization to finish adb_mutex_lock(&start_lock); adb_cond_wait(&start_cond, &start_lock); adb_mutex_unlock(&start_lock); adb_mutex_destroy(&start_lock); adb_cond_destroy(&start_cond); initialized = 1; } }
static FH _fh_alloc( FHClass clazz ) { int nn; FH f = NULL; adb_mutex_lock( &_win32_lock ); if (_win32_fh_count < WIN32_MAX_FHS) { f = &_win32_fhs[ _win32_fh_count++ ]; goto Exit; } for (nn = 0; nn < WIN32_MAX_FHS; nn++) { if ( _win32_fhs[nn].clazz == NULL) { f = &_win32_fhs[nn]; goto Exit; } } D( "_fh_alloc: no more free file descriptors\n" ); Exit: if (f) { f->clazz = clazz; f->used = 1; f->eof = 0; clazz->_fh_init(f); } adb_mutex_unlock( &_win32_lock ); return f; }
atransport* find_emulator_transport_by_adb_port(int adb_port) { adb_mutex_lock( &local_transports_lock ); atransport* result = find_emulator_transport_by_adb_port_locked(adb_port); adb_mutex_unlock( &local_transports_lock ); return result; }
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) { const char* serial = t->serial; if (!serial || !serial[0]) serial = "????????????"; len = snprintf(p, end - p, "%s\t%s\n", 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; }
// This should only be used for transports with connection_state == kCsNoPerm. void unregister_usb_transport(usb_handle *usb) { adb_mutex_lock(&transport_lock); transport_list.remove_if([usb](atransport* t) { return t->usb == usb && t->connection_state == kCsNoPerm; }); adb_mutex_unlock(&transport_lock); }
int get_available_local_transport_index() { adb_mutex_lock( &local_transports_lock ); int result = get_available_local_transport_index_locked(); adb_mutex_unlock( &local_transports_lock ); return result; }
static void transport_unref(atransport *t) { if (t) { adb_mutex_lock(&transport_lock); transport_unref_locked(t); adb_mutex_unlock(&transport_lock); } }
std::string list_transports(bool long_listing) { std::string result; adb_mutex_lock(&transport_lock); for (const auto& t : transport_list) { append_transport(t, &result, long_listing); } adb_mutex_unlock(&transport_lock); return result; }
static void dump_fde(fdevent *fde, const char *info) { adb_mutex_lock(&D_lock); fprintf(stderr,"FDE #%03d %c%c%c %s\n", fde->fd, fde->state & FDE_READ ? 'R' : ' ', fde->state & FDE_WRITE ? 'W' : ' ', fde->state & FDE_ERROR ? 'E' : ' ', info); adb_mutex_unlock(&D_lock); }
void install_local_socket(asocket *s) { adb_mutex_lock(&socket_list_lock); s->id = local_socket_next_id++; insert_local_socket(s, &local_socket_list); adb_mutex_unlock(&socket_list_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); }
std::string list_transports(bool long_listing) { std::string result; adb_mutex_lock(&transport_lock); for (atransport* t = transport_list.next; t != &transport_list; t = t->next) { append_transport(t, &result, long_listing); } adb_mutex_unlock(&transport_lock); return result; }
static void transport_unref(atransport *t) { D("in unref transport\n"); if (t) { adb_mutex_lock(&transport_lock); transport_unref_locked(t); adb_mutex_unlock(&transport_lock); } }
/* hack for osx */ void close_usb_devices() { adb_mutex_lock(&transport_lock); for (const auto& t : transport_list) { if (!t->kicked) { t->kicked = 1; t->kick(t); } } adb_mutex_unlock(&transport_lock); }
void unregister_transport(atransport *t) { adb_mutex_lock(&transport_lock); t->next->prev = t->prev; t->prev->next = t->next; adb_mutex_unlock(&transport_lock); kick_transport(t); transport_unref(t); }
static void clear_received_packet() { adb_mutex_lock(&packet_lock); while(received != NULL) { apacket* packet = received; received = received->next; put_apacket(packet); } received = last = NULL; adb_mutex_unlock(&packet_lock); }
void usb_kick(usb_handle *h) { D("usb_kick\n"); adb_mutex_lock(&h->lock); adb_close(h->fd); h->fd = -1; // notify usb_open_thread that we are disconnected adb_cond_signal(&h->notify); adb_mutex_unlock(&h->lock); }
/* hack for osx */ void close_usb_devices() { adb_mutex_lock(&transport_lock); for (atransport* t = transport_list.next; t != &transport_list; t = t->next) { if ( !t->kicked ) { t->kicked = 1; t->kick(t); } } adb_mutex_unlock(&transport_lock); }
int known_device(const char* dev_name) { int ret = 0; if (NULL != dev_name) { adb_mutex_lock(&usb_lock); ret = known_device_locked(dev_name); adb_mutex_unlock(&usb_lock); } return ret; }
void usb_kick(usb_handle *handle, bool (*close_handle_func)(ADBAPIHANDLE)) { if (NULL != handle) { adb_mutex_lock(&usb_lock); usb_cleanup_handle(handle, close_handle_func, "usb_kick2"); adb_mutex_unlock(&usb_lock); } else { SetLastError(ERROR_INVALID_HANDLE); errno = ERROR_INVALID_HANDLE; } }
// This should only be used for transports with connection_state == kCsNoPerm. void unregister_usb_transport(usb_handle* usb) { adb_mutex_lock(&transport_lock); 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); }
static void store_received_packet(apacket *p) { adb_mutex_lock(&packet_lock); if (received == NULL) { received = p; last = p; } else { last->next = p; last = p; } last->next = NULL; adb_mutex_unlock(&packet_lock); }
atransport *find_transport(const char *serial) { atransport* result = nullptr; adb_mutex_lock(&transport_lock); for (auto& t : transport_list) { if (t->serial && strcmp(serial, t->serial) == 0) { result = t; break; } } adb_mutex_unlock(&transport_lock); return result; }
void install_local_socket(asocket *s) { adb_mutex_lock(&socket_list_lock); s->id = local_socket_next_id++; // Socket ids should never be 0. if (local_socket_next_id == 0) local_socket_next_id = 1; insert_local_socket(s, &local_socket_list); adb_mutex_unlock(&socket_list_lock); }
void kick_all_tcp_devices() { adb_mutex_lock(&transport_lock); for (auto& t : transport_list) { // TCP/IP devices have adb_port == 0. if (t->type == kTransportLocal && t->adb_port == 0) { // Kicking breaks the read_transport thread of this transport out of any read, then // the read_transport thread will notify the main thread to make this transport // offline. Then the main thread will notify the write_transport thread to exit. // Finally, this transport will be closed and freed in the main thread. kick_transport_locked(t); } } adb_mutex_unlock(&transport_lock); }
static void transport_unref(atransport* t) { CHECK(t != nullptr); adb_mutex_lock(&transport_lock); CHECK_GT(t->ref_count, 0u); t->ref_count--; if (t->ref_count == 0) { D("transport: %s unref (kicking and closing)", t->serial); kick_transport_locked(t); t->close(t); remove_transport(t); } else { D("transport: %s unref (count=%zu)", t->serial, t->ref_count); } adb_mutex_unlock(&transport_lock); }
void kick_transport(atransport* t) { if (t && !t->kicked) { int kicked; adb_mutex_lock(&transport_lock); kicked = t->kicked; if (!kicked) t->kicked = 1; adb_mutex_unlock(&transport_lock); if (!kicked) t->kick(t); } }