SR_PRIV int usb_source_add(struct sr_context *ctx, int timeout, sr_receive_data_callback cb, void *cb_data) { if (ctx->usb_source_present) { sr_err("A USB event source is already present."); return SR_ERR; } #ifdef _WIN32 ctx->usb_event = CreateEvent(NULL, TRUE, FALSE, NULL); g_mutex_init(&ctx->usb_mutex); ctx->usb_thread_running = TRUE; ctx->usb_thread = g_thread_new("usb", usb_thread, ctx); ctx->usb_pollfd.fd = ctx->usb_event; ctx->usb_pollfd.events = G_IO_IN; ctx->usb_cb = cb; ctx->usb_cb_data = cb_data; sr_session_source_add_pollfd(&ctx->usb_pollfd, timeout, usb_callback, ctx); #else const struct libusb_pollfd **lupfd; unsigned int i; lupfd = libusb_get_pollfds(ctx->libusb_ctx); for (i = 0; lupfd[i]; i++) sr_source_add(lupfd[i]->fd, lupfd[i]->events, timeout, cb, cb_data); free(lupfd); #endif ctx->usb_source_present = TRUE; return SR_OK; }
SR_PRIV int serial_source_add(struct sr_session *session, struct sr_serial_dev_inst *serial, int events, int timeout, sr_receive_data_callback cb, void *cb_data) { enum sp_event mask = 0; unsigned int i; if (sp_new_event_set(&serial->event_set) != SP_OK) return SR_ERR; if (events & G_IO_IN) mask |= SP_EVENT_RX_READY; if (events & G_IO_OUT) mask |= SP_EVENT_TX_READY; if (events & G_IO_ERR) mask |= SP_EVENT_ERROR; if (sp_add_port_events(serial->event_set, serial->data, mask) != SP_OK) { sp_free_event_set(serial->event_set); return SR_ERR; } serial->pollfds = (GPollFD *) g_malloc0(sizeof(GPollFD) * serial->event_set->count); for (i = 0; i < serial->event_set->count; i++) { serial->pollfds[i].fd = ((event_handle *) serial->event_set->handles)[i]; mask = serial->event_set->masks[i]; if (mask & SP_EVENT_RX_READY) serial->pollfds[i].events |= G_IO_IN; if (mask & SP_EVENT_TX_READY) serial->pollfds[i].events |= G_IO_OUT; if (mask & SP_EVENT_ERROR) serial->pollfds[i].events |= G_IO_ERR; if (sr_session_source_add_pollfd(session, &serial->pollfds[i], timeout, cb, cb_data) != SR_OK) return SR_ERR; } return SR_OK; }