2 * This file is part of the sigrok project.
4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
29 * @defgroup grp_session Session handling
31 * Creating, using, or destroying libsigrok sessions.
38 sr_receive_data_callback_t cb;
41 /* This is used to keep track of the object (fd, pollfd or channel) which is
42 * being polled and will be used to match the source when removing it again.
47 /* There can only be one session at a time. */
48 /* 'session' is not static, it's used elsewhere (via 'extern'). */
49 struct sr_session *session;
52 * Create a new session.
54 * @todo Should it use the file-global "session" variable or take an argument?
55 * The same question applies to all the other session functions.
57 * @return A pointer to the newly allocated session, or NULL upon errors.
59 SR_API struct sr_session *sr_session_new(void)
61 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
62 sr_err("session: %s: session malloc failed", __func__);
63 return NULL; /* TODO: SR_ERR_MALLOC? */
66 session->source_timeout = -1;
72 * Destroy the current session.
74 * This frees up all memory used by the session.
76 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
78 SR_API int sr_session_destroy(void)
81 sr_err("session: %s: session was NULL", __func__);
85 sr_session_dev_remove_all();
87 /* TODO: Error checks needed? */
95 static void sr_dev_close(struct sr_dev_inst *sdi)
97 if (sdi->driver && sdi->driver->dev_close)
98 sdi->driver->dev_close(sdi);
102 * Remove all the devices from the current session.
104 * The session itself (i.e., the struct sr_session) is not free'd and still
105 * exists after this function returns.
107 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
109 SR_API int sr_session_dev_remove_all(void)
112 sr_err("session: %s: session was NULL", __func__);
116 g_slist_free_full(session->devs, (GDestroyNotify)sr_dev_close);
117 session->devs = NULL;
123 * Add a device instance to the current session.
125 * @param sdi The device instance to add to the current session. Must not
126 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
129 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
131 SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi)
136 sr_err("session: %s: sdi was NULL", __func__);
141 sr_err("session: %s: session was NULL", __func__);
145 /* If sdi->driver is NULL, this is a virtual device. */
147 sr_dbg("session: %s: sdi->driver was NULL, this seems to be "
148 "a virtual device; continuing", __func__);
149 /* Just add the device, don't run dev_open(). */
150 session->devs = g_slist_append(session->devs, (gpointer)sdi);
154 /* sdi->driver is non-NULL (i.e. we have a real device). */
155 if (!sdi->driver->dev_open) {
156 sr_err("session: %s: sdi->driver->dev_open was NULL", __func__);
160 if ((ret = sdi->driver->dev_open((struct sr_dev_inst *)sdi)) != SR_OK) {
161 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
165 session->devs = g_slist_append(session->devs, (gpointer)sdi);
171 * Remove all datafeed callbacks in the current session.
173 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
175 SR_API int sr_session_datafeed_callback_remove_all(void)
178 sr_err("session: %s: session was NULL", __func__);
182 g_slist_free(session->datafeed_callbacks);
183 session->datafeed_callbacks = NULL;
189 * Add a datafeed callback to the current session.
191 * @param cb Function to call when a chunk of data is received.
194 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
196 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
199 sr_err("session: %s: session was NULL", __func__);
204 sr_err("session: %s: cb was NULL", __func__);
208 session->datafeed_callbacks =
209 g_slist_append(session->datafeed_callbacks, cb);
214 static int sr_session_run_poll(void)
219 while (session->num_sources > 0) {
220 ret = g_poll(session->pollfds, session->num_sources,
221 session->source_timeout);
222 for (i = 0; i < session->num_sources; i++) {
223 if (session->pollfds[i].revents > 0 || (ret == 0
224 && session->source_timeout == session->sources[i].timeout)) {
226 * Invoke the source's callback on an event,
227 * or if the poll timed out and this source
228 * asked for that timeout.
230 if (!session->sources[i].cb(session->pollfds[i].fd,
231 session->pollfds[i].revents,
232 session->sources[i].cb_data))
233 sr_session_source_remove(session->sources[i].poll_object);
244 * There can only be one session at a time.
246 * @return SR_OK upon success, SR_ERR upon errors.
248 SR_API int sr_session_start(void)
250 struct sr_dev_inst *sdi;
255 sr_err("session: %s: session was NULL; a session must be "
256 "created before starting it.", __func__);
260 if (!session->devs) {
261 sr_err("session: %s: session->devs was NULL; a session "
262 "cannot be started without devices.", __func__);
266 sr_info("session: starting");
268 for (l = session->devs; l; l = l->next) {
270 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
271 sr_err("session: %s: could not start an acquisition "
272 "(%d)", __func__, ret);
277 /* TODO: What if there are multiple devices? Which return code? */
285 * @return SR_OK upon success, SR_ERR_BUG upon errors.
287 SR_API int sr_session_run(void)
290 sr_err("session: %s: session was NULL; a session must be "
291 "created first, before running it.", __func__);
295 if (!session->devs) {
296 /* TODO: Actually the case? */
297 sr_err("session: %s: session->devs was NULL; a session "
298 "cannot be run without devices.", __func__);
302 sr_info("session: running");
304 /* Do we have real sources? */
305 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
306 /* Dummy source, freewheel over it. */
307 while (session->num_sources)
308 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
310 /* Real sources, use g_poll() main loop. */
311 sr_session_run_poll();
318 * Halt the current session.
320 * This function is deprecated and should not be used in new code, use
321 * sr_session_stop() instead. The behaviour of this function is identical to
324 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
326 SR_API int sr_session_halt(void)
328 return sr_session_stop();
332 * Stop the current session.
334 * The current session is stopped immediately, with all acquisition sessions
335 * being stopped and hardware drivers cleaned up.
337 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
339 SR_API int sr_session_stop(void)
341 struct sr_dev_inst *sdi;
345 sr_err("session: %s: session was NULL", __func__);
349 sr_info("session: stopping");
351 for (l = session->devs; l; l = l->next) {
354 if (sdi->driver->dev_acquisition_stop)
355 sdi->driver->dev_acquisition_stop(sdi, sdi);
365 * @param packet The packet to show debugging information for.
367 static void datafeed_dump(struct sr_datafeed_packet *packet)
369 struct sr_datafeed_logic *logic;
370 struct sr_datafeed_analog *analog;
372 switch (packet->type) {
374 sr_dbg("bus: received SR_DF_HEADER");
377 sr_dbg("bus: received SR_DF_TRIGGER");
379 case SR_DF_META_LOGIC:
380 sr_dbg("bus: received SR_DF_META_LOGIC");
383 logic = packet->payload;
384 /* TODO: Check for logic != NULL. */
385 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
387 case SR_DF_META_ANALOG:
388 sr_dbg("bus: received SR_DF_META_ANALOG");
391 analog = packet->payload;
392 /* TODO: Check for analog != NULL. */
393 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
396 sr_dbg("bus: received SR_DF_END");
398 case SR_DF_FRAME_BEGIN:
399 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
401 case SR_DF_FRAME_END:
402 sr_dbg("bus: received SR_DF_FRAME_END");
405 sr_dbg("bus: received unknown packet type %d", packet->type);
411 * Send a packet to whatever is listening on the datafeed bus.
413 * Hardware drivers use this to send a data packet to the frontend.
416 * @param packet The datafeed packet to send to the session bus.
418 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
422 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
423 struct sr_datafeed_packet *packet)
426 sr_datafeed_callback_t cb;
429 sr_err("session: %s: sdi was NULL", __func__);
434 sr_err("session: %s: packet was NULL", __func__);
438 for (l = session->datafeed_callbacks; l; l = l->next) {
439 if (sr_log_loglevel_get() >= SR_LOG_DBG)
440 datafeed_dump(packet);
449 * Add an event source for a file descriptor.
451 * @param pollfd The GPollFD.
452 * @param timeout Max time to wait before the callback is called, ignored if 0.
453 * @param cb Callback function to add. Must not be NULL.
454 * @param cb_data Data for the callback function. Can be NULL.
455 * @param poll_object TODO.
457 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
458 * SR_ERR_MALLOC upon memory allocation errors.
460 static int _sr_session_source_add(GPollFD *pollfd, int timeout,
461 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
463 struct source *new_sources, *s;
464 GPollFD *new_pollfds;
467 sr_err("session: %s: cb was NULL", __func__);
471 /* Note: cb_data can be NULL, that's not a bug. */
473 new_pollfds = g_try_realloc(session->pollfds,
474 sizeof(GPollFD) * (session->num_sources + 1));
476 sr_err("session: %s: new_pollfds malloc failed", __func__);
477 return SR_ERR_MALLOC;
480 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
481 (session->num_sources + 1));
483 sr_err("session: %s: new_sources malloc failed", __func__);
484 return SR_ERR_MALLOC;
487 new_pollfds[session->num_sources] = *pollfd;
488 s = &new_sources[session->num_sources++];
489 s->timeout = timeout;
491 s->cb_data = cb_data;
492 s->poll_object = poll_object;
493 session->pollfds = new_pollfds;
494 session->sources = new_sources;
496 if (timeout != session->source_timeout && timeout > 0
497 && (session->source_timeout == -1 || timeout < session->source_timeout))
498 session->source_timeout = timeout;
504 * Add an event source for a file descriptor.
506 * @param fd The file descriptor.
507 * @param events Events to check for.
508 * @param timeout Max time to wait before the callback is called, ignored if 0.
509 * @param cb Callback function to add. Must not be NULL.
510 * @param cb_data Data for the callback function. Can be NULL.
512 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
513 * SR_ERR_MALLOC upon memory allocation errors.
515 SR_API int sr_session_source_add(int fd, int events, int timeout,
516 sr_receive_data_callback_t cb, void *cb_data)
523 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
527 * Add an event source for a GPollFD.
529 * @param pollfd The GPollFD.
530 * @param timeout Max time to wait before the callback is called, ignored if 0.
531 * @param cb Callback function to add. Must not be NULL.
532 * @param cb_data Data for the callback function. Can be NULL.
534 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
535 * SR_ERR_MALLOC upon memory allocation errors.
537 SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
538 sr_receive_data_callback_t cb, void *cb_data)
540 return _sr_session_source_add(pollfd, timeout, cb,
541 cb_data, (gintptr)pollfd);
545 * Add an event source for a GIOChannel.
547 * @param channel The GIOChannel.
548 * @param events Events to poll on.
549 * @param timeout Max time to wait before the callback is called, ignored if 0.
550 * @param cb Callback function to add. Must not be NULL.
551 * @param cb_data Data for the callback function. Can be NULL.
553 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
554 * SR_ERR_MALLOC upon memory allocation errors.
556 SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
557 int timeout, sr_receive_data_callback_t cb, void *cb_data)
562 g_io_channel_win32_make_pollfd(channel, events, &p);
564 p.fd = g_io_channel_unix_get_fd(channel);
568 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
572 * Remove the source belonging to the specified channel.
574 * @todo Add more error checks and logging.
576 * @param channel The channel for which the source should be removed.
578 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
579 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
582 static int _sr_session_source_remove(gintptr poll_object)
584 struct source *new_sources;
585 GPollFD *new_pollfds;
588 if (!session->sources || !session->num_sources) {
589 sr_err("session: %s: sources was NULL", __func__);
593 for (old = 0; old < session->num_sources; old++) {
594 if (session->sources[old].poll_object == poll_object)
598 /* fd not found, nothing to do */
599 if (old == session->num_sources)
602 session->num_sources -= 1;
604 if (old != session->num_sources) {
605 memmove(&session->pollfds[old], &session->pollfds[old+1],
606 (session->num_sources - old) * sizeof(GPollFD));
607 memmove(&session->sources[old], &session->sources[old+1],
608 (session->num_sources - old) * sizeof(struct source));
611 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
612 if (!new_pollfds && session->num_sources > 0) {
613 sr_err("session: %s: new_pollfds malloc failed", __func__);
614 return SR_ERR_MALLOC;
617 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
618 if (!new_sources && session->num_sources > 0) {
619 sr_err("session: %s: new_sources malloc failed", __func__);
620 return SR_ERR_MALLOC;
623 session->pollfds = new_pollfds;
624 session->sources = new_sources;
630 * Remove the source belonging to the specified file descriptor.
632 * @param fd The file descriptor for which the source should be removed.
634 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
635 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
638 SR_API int sr_session_source_remove(int fd)
640 return _sr_session_source_remove((gintptr)fd);
644 * Remove the source belonging to the specified poll descriptor.
646 * @param pollfd The poll descriptor for which the source should be removed.
648 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
649 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
652 SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
654 return _sr_session_source_remove((gintptr)pollfd);
658 * Remove the source belonging to the specified channel.
660 * @param channel The channel for which the source should be removed.
662 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
663 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
666 SR_API int sr_session_source_remove_channel(GIOChannel *channel)
668 return _sr_session_source_remove((gintptr)channel);