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"
28 /* Message logging helpers with driver-specific prefix string. */
29 #define DRIVER_LOG_DOMAIN "session: "
30 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
40 * Creating, using, or destroying libsigrok sessions.
44 * @defgroup grp_session Session handling
46 * Creating, using, or destroying libsigrok sessions.
53 sr_receive_data_callback_t cb;
56 /* This is used to keep track of the object (fd, pollfd or channel) which is
57 * being polled and will be used to match the source when removing it again.
62 /* There can only be one session at a time. */
63 /* 'session' is not static, it's used elsewhere (via 'extern'). */
64 struct sr_session *session;
67 * Create a new session.
69 * @todo Should it use the file-global "session" variable or take an argument?
70 * The same question applies to all the other session functions.
72 * @return A pointer to the newly allocated session, or NULL upon errors.
74 SR_API struct sr_session *sr_session_new(void)
76 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
77 sr_err("Session malloc failed.");
81 session->source_timeout = -1;
87 * Destroy the current session.
89 * This frees up all memory used by the session.
91 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
93 SR_API int sr_session_destroy(void)
96 sr_err("%s: session was NULL", __func__);
100 sr_session_dev_remove_all();
102 /* TODO: Error checks needed? */
111 * Close a device instance.
113 * @param sdi The device instance to close. Must not be NULL. Also,
114 * sdi->driver, sdi->driver->priv, and sdi->priv must not be NULL.
116 static void sr_dev_close(struct sr_dev_inst *sdi)
121 sr_err("Invalid device instance, can't close device.");
125 /* In the drivers sdi->priv is a 'struct dev_context *devc'. */
128 * Should be sr_err() in theory, but the 'demo' driver has
129 * NULL for sdi->priv, so we use sr_dbg() until that's fixed.
131 sr_dbg("Invalid device context, can't close device.");
136 sr_err("Invalid driver, can't close device.");
140 if (!sdi->driver->priv) {
141 sr_err("Driver not initialized, can't close device.");
145 sr_spew("Closing '%s' device instance %d.", sdi->driver->name,
148 if ((ret = sdi->driver->dev_close(sdi)) < 0)
149 sr_err("Failed to close device instance: %d.", ret);
153 * Remove all the devices from the current session.
155 * The session itself (i.e., the struct sr_session) is not free'd and still
156 * exists after this function returns.
158 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
160 SR_API int sr_session_dev_remove_all(void)
163 sr_err("%s: session was NULL", __func__);
167 g_slist_free_full(session->devs, (GDestroyNotify)sr_dev_close);
168 session->devs = NULL;
174 * Add a device instance to the current session.
176 * @param sdi The device instance to add to the current session. Must not
177 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
180 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
182 SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi)
187 sr_err("%s: sdi was NULL", __func__);
192 sr_err("%s: session was NULL", __func__);
196 /* If sdi->driver is NULL, this is a virtual device. */
198 sr_dbg("%s: sdi->driver was NULL, this seems to be "
199 "a virtual device; continuing", __func__);
200 /* Just add the device, don't run dev_open(). */
201 session->devs = g_slist_append(session->devs, (gpointer)sdi);
205 /* sdi->driver is non-NULL (i.e. we have a real device). */
206 if (!sdi->driver->dev_open) {
207 sr_err("%s: sdi->driver->dev_open was NULL", __func__);
211 if ((ret = sdi->driver->dev_open((struct sr_dev_inst *)sdi)) != SR_OK) {
212 sr_err("%s: dev_open failed (%d)", __func__, ret);
216 session->devs = g_slist_append(session->devs, (gpointer)sdi);
222 * Remove all datafeed callbacks in the current session.
224 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
226 SR_API int sr_session_datafeed_callback_remove_all(void)
229 sr_err("%s: session was NULL", __func__);
233 g_slist_free(session->datafeed_callbacks);
234 session->datafeed_callbacks = NULL;
240 * Add a datafeed callback to the current session.
242 * @param cb Function to call when a chunk of data is received.
245 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
247 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
250 sr_err("%s: session was NULL", __func__);
255 sr_err("%s: cb was NULL", __func__);
259 session->datafeed_callbacks =
260 g_slist_append(session->datafeed_callbacks, cb);
265 static int sr_session_run_poll(void)
270 while (session->num_sources > 0) {
271 ret = g_poll(session->pollfds, session->num_sources,
272 session->source_timeout);
273 for (i = 0; i < session->num_sources; i++) {
274 if (session->pollfds[i].revents > 0 || (ret == 0
275 && session->source_timeout == session->sources[i].timeout)) {
277 * Invoke the source's callback on an event,
278 * or if the poll timed out and this source
279 * asked for that timeout.
281 if (!session->sources[i].cb(session->pollfds[i].fd,
282 session->pollfds[i].revents,
283 session->sources[i].cb_data))
284 sr_session_source_remove(session->sources[i].poll_object);
295 * There can only be one session at a time.
297 * @return SR_OK upon success, SR_ERR upon errors.
299 SR_API int sr_session_start(void)
301 struct sr_dev_inst *sdi;
306 sr_err("%s: session was NULL; a session must be "
307 "created before starting it.", __func__);
311 if (!session->devs) {
312 sr_err("%s: session->devs was NULL; a session "
313 "cannot be started without devices.", __func__);
317 sr_info("Starting.");
320 for (l = session->devs; l; l = l->next) {
322 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
323 sr_err("%s: could not start an acquisition "
324 "(%d)", __func__, ret);
329 /* TODO: What if there are multiple devices? Which return code? */
337 * @return SR_OK upon success, SR_ERR_BUG upon errors.
339 SR_API int sr_session_run(void)
342 sr_err("%s: session was NULL; a session must be "
343 "created first, before running it.", __func__);
347 if (!session->devs) {
348 /* TODO: Actually the case? */
349 sr_err("%s: session->devs was NULL; a session "
350 "cannot be run without devices.", __func__);
356 /* Do we have real sources? */
357 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
358 /* Dummy source, freewheel over it. */
359 while (session->num_sources)
360 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
362 /* Real sources, use g_poll() main loop. */
363 sr_session_run_poll();
370 * Stop the current session.
372 * The current session is stopped immediately, with all acquisition sessions
373 * being stopped and hardware drivers cleaned up.
375 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
377 SR_API int sr_session_stop(void)
379 struct sr_dev_inst *sdi;
383 sr_err("%s: session was NULL", __func__);
387 sr_info("Stopping.");
389 for (l = session->devs; l; l = l->next) {
392 if (sdi->driver->dev_acquisition_stop)
393 sdi->driver->dev_acquisition_stop(sdi, sdi);
398 * Some sources may not be necessarily associated with a device.
399 * Those sources may still be present even after stopping all devices.
400 * We need to make sure all sources are removed, or we risk running the
401 * session in an infinite loop.
403 while (session->num_sources)
404 sr_session_source_remove(session->sources[0].poll_object);
412 * @param packet The packet to show debugging information for.
414 static void datafeed_dump(const struct sr_datafeed_packet *packet)
416 const struct sr_datafeed_logic *logic;
417 const struct sr_datafeed_analog *analog;
419 switch (packet->type) {
421 sr_dbg("bus: Received SR_DF_HEADER packet.");
424 sr_dbg("bus: Received SR_DF_TRIGGER packet.");
427 sr_dbg("bus: Received SR_DF_META packet.");
430 logic = packet->payload;
431 sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes).",
435 analog = packet->payload;
436 sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).",
437 analog->num_samples);
440 sr_dbg("bus: Received SR_DF_END packet.");
442 case SR_DF_FRAME_BEGIN:
443 sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet.");
445 case SR_DF_FRAME_END:
446 sr_dbg("bus: Received SR_DF_FRAME_END packet.");
449 sr_dbg("bus: Received unknown packet type: %d.", packet->type);
455 * Send a packet to whatever is listening on the datafeed bus.
457 * Hardware drivers use this to send a data packet to the frontend.
460 * @param packet The datafeed packet to send to the session bus.
462 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
466 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
467 const struct sr_datafeed_packet *packet)
470 sr_datafeed_callback_t cb;
473 sr_err("%s: sdi was NULL", __func__);
478 sr_err("%s: packet was NULL", __func__);
482 for (l = session->datafeed_callbacks; l; l = l->next) {
483 if (sr_log_loglevel_get() >= SR_LOG_DBG)
484 datafeed_dump(packet);
493 * Add an event source for a file descriptor.
495 * @param pollfd The GPollFD.
496 * @param timeout Max time to wait before the callback is called, ignored if 0.
497 * @param cb Callback function to add. Must not be NULL.
498 * @param cb_data Data for the callback function. Can be NULL.
499 * @param poll_object TODO.
501 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
502 * SR_ERR_MALLOC upon memory allocation errors.
504 static int _sr_session_source_add(GPollFD *pollfd, int timeout,
505 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
507 struct source *new_sources, *s;
508 GPollFD *new_pollfds;
511 sr_err("%s: cb was NULL", __func__);
515 /* Note: cb_data can be NULL, that's not a bug. */
517 new_pollfds = g_try_realloc(session->pollfds,
518 sizeof(GPollFD) * (session->num_sources + 1));
520 sr_err("%s: new_pollfds malloc failed", __func__);
521 return SR_ERR_MALLOC;
524 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
525 (session->num_sources + 1));
527 sr_err("%s: new_sources malloc failed", __func__);
528 return SR_ERR_MALLOC;
531 new_pollfds[session->num_sources] = *pollfd;
532 s = &new_sources[session->num_sources++];
533 s->timeout = timeout;
535 s->cb_data = cb_data;
536 s->poll_object = poll_object;
537 session->pollfds = new_pollfds;
538 session->sources = new_sources;
540 if (timeout != session->source_timeout && timeout > 0
541 && (session->source_timeout == -1 || timeout < session->source_timeout))
542 session->source_timeout = timeout;
548 * Add an event source for a file descriptor.
550 * @param fd The file descriptor.
551 * @param events Events to check for.
552 * @param timeout Max time to wait before the callback is called, ignored if 0.
553 * @param cb Callback function to add. Must not be NULL.
554 * @param cb_data Data for the callback function. Can be NULL.
556 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
557 * SR_ERR_MALLOC upon memory allocation errors.
559 SR_API int sr_session_source_add(int fd, int events, int timeout,
560 sr_receive_data_callback_t cb, void *cb_data)
567 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
571 * Add an event source for a GPollFD.
573 * @param pollfd The GPollFD.
574 * @param timeout Max time to wait before the callback is called, ignored if 0.
575 * @param cb Callback function to add. Must not be NULL.
576 * @param cb_data Data for the callback function. Can be NULL.
578 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
579 * SR_ERR_MALLOC upon memory allocation errors.
581 SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
582 sr_receive_data_callback_t cb, void *cb_data)
584 return _sr_session_source_add(pollfd, timeout, cb,
585 cb_data, (gintptr)pollfd);
589 * Add an event source for a GIOChannel.
591 * @param channel The GIOChannel.
592 * @param events Events to poll on.
593 * @param timeout Max time to wait before the callback is called, ignored if 0.
594 * @param cb Callback function to add. Must not be NULL.
595 * @param cb_data Data for the callback function. Can be NULL.
597 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
598 * SR_ERR_MALLOC upon memory allocation errors.
600 SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
601 int timeout, sr_receive_data_callback_t cb, void *cb_data)
606 g_io_channel_win32_make_pollfd(channel, events, &p);
608 p.fd = g_io_channel_unix_get_fd(channel);
612 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
616 * Remove the source belonging to the specified channel.
618 * @todo Add more error checks and logging.
620 * @param channel The channel for which the source should be removed.
622 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
623 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
626 static int _sr_session_source_remove(gintptr poll_object)
628 struct source *new_sources;
629 GPollFD *new_pollfds;
632 if (!session->sources || !session->num_sources) {
633 sr_err("%s: sources was NULL", __func__);
637 for (old = 0; old < session->num_sources; old++) {
638 if (session->sources[old].poll_object == poll_object)
642 /* fd not found, nothing to do */
643 if (old == session->num_sources)
646 session->num_sources -= 1;
648 if (old != session->num_sources) {
649 memmove(&session->pollfds[old], &session->pollfds[old+1],
650 (session->num_sources - old) * sizeof(GPollFD));
651 memmove(&session->sources[old], &session->sources[old+1],
652 (session->num_sources - old) * sizeof(struct source));
655 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
656 if (!new_pollfds && session->num_sources > 0) {
657 sr_err("%s: new_pollfds malloc failed", __func__);
658 return SR_ERR_MALLOC;
661 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
662 if (!new_sources && session->num_sources > 0) {
663 sr_err("%s: new_sources malloc failed", __func__);
664 return SR_ERR_MALLOC;
667 session->pollfds = new_pollfds;
668 session->sources = new_sources;
674 * Remove the source belonging to the specified file descriptor.
676 * @param fd The file descriptor for which the source should be removed.
678 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
679 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
682 SR_API int sr_session_source_remove(int fd)
684 return _sr_session_source_remove((gintptr)fd);
688 * Remove the source belonging to the specified poll descriptor.
690 * @param pollfd The poll descriptor for which the source should be removed.
692 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
693 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
696 SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
698 return _sr_session_source_remove((gintptr)pollfd);
702 * Remove the source belonging to the specified channel.
704 * @param channel The channel for which the source should be removed.
706 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
707 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
710 SR_API int sr_session_source_remove_channel(GIOChannel *channel)
712 return _sr_session_source_remove((gintptr)channel);