26 #include "libsigrok-internal.h"
29 #define LOG_PREFIX "session: "
30 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
31 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
33 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
34 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
35 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
62 struct datafeed_callback {
81 if (!(session = g_try_malloc0(
sizeof(
struct sr_session)))) {
82 sr_err(
"Session malloc failed.");
104 sr_err(
"%s: session was NULL", __func__);
131 sr_err(
"%s: session was NULL", __func__);
135 g_slist_free(session->
devs);
136 session->
devs = NULL;
155 sr_err(
"%s: sdi was NULL", __func__);
160 sr_err(
"%s: session was NULL", __func__);
166 sr_dbg(
"%s: sdi->driver was NULL, this seems to be "
167 "a virtual device; continuing", __func__);
169 session->
devs = g_slist_append(session->
devs, (gpointer)sdi);
175 sr_err(
"%s: sdi->driver->dev_open was NULL", __func__);
179 session->
devs = g_slist_append(session->
devs, (gpointer)sdi);
184 if ((ret = sdi->driver->dev_acquisition_start(sdi,
185 (
void *)sdi)) !=
SR_OK)
186 sr_err(
"Failed to start acquisition of device in "
187 "running session: %d", ret);
201 sr_err(
"%s: session was NULL", __func__);
222 struct datafeed_callback *cb_struct;
225 sr_err(
"%s: session was NULL", __func__);
230 sr_err(
"%s: cb was NULL", __func__);
234 if (!(cb_struct = g_try_malloc0(
sizeof(
struct datafeed_callback))))
238 cb_struct->cb_data = cb_data;
262 static int sr_session_iteration(gboolean block)
270 if (session->
pollfds[i].revents > 0 || (ret == 0
290 sr_session_stop_sync();
314 sr_err(
"%s: session was NULL; a session must be "
315 "created before starting it.", __func__);
319 if (!session->
devs) {
320 sr_err(
"%s: session->devs was NULL; a session "
321 "cannot be started without devices.", __func__);
328 for (l = session->
devs; l; l = l->next) {
331 sr_err(
"%s: could not start an acquisition "
350 sr_err(
"%s: session was NULL; a session must be "
351 "created first, before running it.", __func__);
355 if (!session->
devs) {
357 sr_err(
"%s: session->devs was NULL; a session "
358 "cannot be run without devices.", __func__);
373 sr_session_iteration(TRUE);
392 SR_PRIV int sr_session_stop_sync(
void)
398 sr_err(
"%s: session was NULL", __func__);
404 for (l = session->
devs; l; l = l->next) {
432 sr_err(
"%s: session was NULL", __func__);
453 switch (packet->
type) {
455 sr_dbg(
"bus: Received SR_DF_HEADER packet.");
458 sr_dbg(
"bus: Received SR_DF_TRIGGER packet.");
461 sr_dbg(
"bus: Received SR_DF_META packet.");
465 sr_dbg(
"bus: Received SR_DF_LOGIC packet (%" PRIu64
" bytes).",
470 sr_dbg(
"bus: Received SR_DF_ANALOG packet (%d samples).",
474 sr_dbg(
"bus: Received SR_DF_END packet.");
477 sr_dbg(
"bus: Received SR_DF_FRAME_BEGIN packet.");
480 sr_dbg(
"bus: Received SR_DF_FRAME_END packet.");
483 sr_dbg(
"bus: Received unknown packet type: %d.", packet->
type);
504 struct datafeed_callback *cb_struct;
507 sr_err(
"%s: sdi was NULL", __func__);
512 sr_err(
"%s: packet was NULL", __func__);
518 datafeed_dump(packet);
520 cb_struct->cb(sdi, packet, cb_struct->cb_data);
538 static int _sr_session_source_add(GPollFD *pollfd,
int timeout,
541 struct source *new_sources, *s;
542 GPollFD *new_pollfds;
545 sr_err(
"%s: cb was NULL", __func__);
551 new_pollfds = g_try_realloc(session->
pollfds,
554 sr_err(
"%s: new_pollfds malloc failed", __func__);
558 new_sources = g_try_realloc(session->
sources,
sizeof(
struct source) *
561 sr_err(
"%s: new_sources malloc failed", __func__);
567 s->timeout = timeout;
569 s->cb_data = cb_data;
570 s->poll_object = poll_object;
571 session->
pollfds = new_pollfds;
572 session->
sources = new_sources;
575 && (session->
source_timeout == -1 || timeout < session->source_timeout))
601 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
618 return _sr_session_source_add(pollfd, timeout, cb,
619 cb_data, (gintptr)pollfd);
640 g_io_channel_win32_make_pollfd(channel, events, &p);
642 p.fd = g_io_channel_unix_get_fd(channel);
646 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
660 static int _sr_session_source_remove(gintptr poll_object)
662 struct source *new_sources;
663 GPollFD *new_pollfds;
667 sr_err(
"%s: sources was NULL", __func__);
672 if (session->
sources[old].poll_object == poll_object)
686 (session->
num_sources - old) *
sizeof(
struct source));
689 new_pollfds = g_try_realloc(session->
pollfds,
sizeof(GPollFD) * session->
num_sources);
691 sr_err(
"%s: new_pollfds malloc failed", __func__);
695 new_sources = g_try_realloc(session->
sources,
sizeof(
struct source) * session->
num_sources);
697 sr_err(
"%s: new_sources malloc failed", __func__);
701 session->
pollfds = new_pollfds;
702 session->
sources = new_sources;
718 return _sr_session_source_remove((gintptr)fd);
732 return _sr_session_source_remove((gintptr)pollfd);
746 return _sr_session_source_remove((gintptr)channel);