X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=src%2Fsession.c;h=158b33cf856a21e8f547982a4742d9cab918b4e2;hb=bbb3996c06f2b1ebc9529d1e2181ddc55f70d9da;hp=765a20c451471b1266be4103544ad77f0979c1d1;hpb=f3f19d1131025b68d29a11273b627c83d748e7ea;p=libsigrok.git diff --git a/src/session.c b/src/session.c index 765a20c4..158b33cf 100644 --- a/src/session.c +++ b/src/session.c @@ -22,7 +22,7 @@ #include #include #include -#include "libsigrok.h" +#include #include "libsigrok-internal.h" /** @cond PRIVATE */ @@ -62,6 +62,7 @@ struct datafeed_callback { /** * Create a new session. * + * @param ctx The context in which to create the new session. * @param new_session This will contain a pointer to the newly created * session if the return value is SR_OK, otherwise the value * is undefined and should not be used. Must not be NULL. @@ -71,7 +72,8 @@ struct datafeed_callback { * * @since 0.4.0 */ -SR_API int sr_session_new(struct sr_session **new_session) +SR_API int sr_session_new(struct sr_context *ctx, + struct sr_session **new_session) { struct sr_session *session; @@ -80,6 +82,7 @@ SR_API int sr_session_new(struct sr_session **new_session) session = g_malloc0(sizeof(struct sr_session)); + session->ctx = ctx; session->source_timeout = -1; session->running = FALSE; session->abort_session = FALSE; @@ -376,10 +379,30 @@ SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger static int sr_session_iteration(struct sr_session *session, gboolean block) { unsigned int i; - int ret; + int ret, timeout; +#ifdef HAVE_LIBUSB_1_0 + int usb_timeout; + struct timeval tv; +#endif - ret = g_poll(session->pollfds, session->num_sources, - block ? session->source_timeout : 0); + timeout = block ? session->source_timeout : 0; + +#ifdef HAVE_LIBUSB_1_0 + if (session->ctx->usb_source_present) { + timeout = block ? 0 : session->source_timeout; + ret = libusb_get_next_timeout(session->ctx->libusb_ctx, &tv); + if (ret < 0) { + sr_err("Error getting libusb timeout: %s", + libusb_error_name(ret)); + return SR_ERR; + } else if (ret == 1) { + usb_timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000; + timeout = MIN(timeout, usb_timeout); + } + } +#endif + + ret = g_poll(session->pollfds, session->num_sources, timeout); for (i = 0; i < session->num_sources; i++) { if (session->pollfds[i].revents > 0 || (ret == 0 && session->source_timeout == session->sources[i].timeout)) { @@ -640,16 +663,20 @@ static void datafeed_dump(const struct sr_datafeed_packet *packet) const struct sr_datafeed_analog *analog; const struct sr_datafeed_analog2 *analog2; + /* Please use the same order as in libsigrok.h. */ switch (packet->type) { case SR_DF_HEADER: sr_dbg("bus: Received SR_DF_HEADER packet."); break; - case SR_DF_TRIGGER: - sr_dbg("bus: Received SR_DF_TRIGGER packet."); + case SR_DF_END: + sr_dbg("bus: Received SR_DF_END packet."); break; case SR_DF_META: sr_dbg("bus: Received SR_DF_META packet."); break; + case SR_DF_TRIGGER: + sr_dbg("bus: Received SR_DF_TRIGGER packet."); + break; case SR_DF_LOGIC: logic = packet->payload; sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " @@ -660,20 +687,17 @@ static void datafeed_dump(const struct sr_datafeed_packet *packet) sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", analog->num_samples); break; - case SR_DF_ANALOG2: - analog2 = packet->payload; - sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).", - analog2->num_samples); - break; - case SR_DF_END: - sr_dbg("bus: Received SR_DF_END packet."); - break; case SR_DF_FRAME_BEGIN: sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); break; case SR_DF_FRAME_END: sr_dbg("bus: Received SR_DF_FRAME_END packet."); break; + case SR_DF_ANALOG2: + analog2 = packet->payload; + sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).", + analog2->num_samples); + break; default: sr_dbg("bus: Received unknown packet type: %d.", packet->type); break;