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 /* demo.c. TODO: Should not be global! */
29 extern SR_PRIV GIOChannel channels[2];
33 sr_receive_data_callback_t cb;
36 /* This is used to keep track of the object (fd, pollfd or channel) which is
37 * being polled and will be used to match the source when removing it again.
42 /* There can only be one session at a time. */
43 /* 'session' is not static, it's used elsewhere (via 'extern'). */
44 struct sr_session *session;
45 static int num_sources = 0;
47 /* Both "sources" and "pollfds" are of the same size and contain pairs of
48 * descriptor and callback function. We can not embed the GPollFD into the
49 * source struct since we want to be able to pass the array of all poll
50 * descriptors to g_poll.
52 static struct source *sources = NULL;
53 static GPollFD *pollfds;
54 static int source_timeout = -1;
57 * Create a new session.
59 * TODO: Should it use the file-global "session" variable or take an argument?
60 * The same question applies to all the other session functions.
62 * @return A pointer to the newly allocated session, or NULL upon errors.
64 SR_API struct sr_session *sr_session_new(void)
66 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
67 sr_err("session: %s: session malloc failed", __func__);
68 return NULL; /* TODO: SR_ERR_MALLOC? */
75 * Destroy the current session.
77 * This frees up all memory used by the session.
79 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
81 SR_API int sr_session_destroy(void)
84 sr_err("session: %s: session was NULL", __func__);
88 g_slist_free(session->devs);
91 /* TODO: Error checks needed? */
93 /* TODO: Loop over protocol decoders and free them. */
102 * Remove all the devices from the current session. TODO?
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(session->devs);
117 session->devs = NULL;
123 * Add a device to the current session.
125 * @param dev The device to add to the current session. Must not be NULL.
126 * Also, dev->driver and dev->driver->dev_open must not be NULL.
128 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
130 SR_API int sr_session_dev_add(struct sr_dev *dev)
135 sr_err("session: %s: dev was NULL", __func__);
140 sr_err("session: %s: session was NULL", __func__);
144 /* If dev->driver is NULL, this is a virtual device. */
146 sr_dbg("session: %s: dev->driver was NULL, this seems to be "
147 "a virtual device; continuing", __func__);
148 /* Just add the device, don't run dev_open(). */
149 session->devs = g_slist_append(session->devs, dev);
153 /* dev->driver is non-NULL (i.e. we have a real device). */
154 if (!dev->driver->dev_open) {
155 sr_err("session: %s: dev->driver->dev_open was NULL",
160 if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) {
161 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
165 session->devs = g_slist_append(session->devs, dev);
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);
217 static int sr_session_run_poll(void)
221 while (session->running) {
222 ret = g_poll(pollfds, num_sources, source_timeout);
224 for (i = 0; i < num_sources; i++) {
225 if (pollfds[i].revents > 0 || (ret == 0
226 && source_timeout == sources[i].timeout)) {
228 * Invoke the source's callback on an event,
229 * or if the poll timeout out and this source
230 * asked for that timeout.
232 if (!sources[i].cb(pollfds[i].fd, pollfds[i].revents,
234 sr_session_source_remove(pollfds[i].fd);
245 * There can only be one session at a time.
247 * @return SR_OK upon success, SR_ERR upon errors.
249 SR_API int sr_session_start(void)
256 sr_err("session: %s: session was NULL; a session must be "
257 "created first, before starting it.", __func__);
261 if (!session->devs) {
262 /* TODO: Actually the case? */
263 sr_err("session: %s: session->devs was NULL; a session "
264 "cannot be started without devices.", __func__);
268 /* TODO: Check driver_index validity? */
270 sr_info("session: starting");
272 for (l = session->devs; l; l = l->next) {
274 /* TODO: Check for dev != NULL. */
275 if ((ret = dev->driver->dev_acquisition_start(
276 dev->driver_index, dev)) != SR_OK) {
277 sr_err("session: %s: could not start an acquisition "
278 "(%d)", __func__, ret);
283 /* TODO: What if there are multiple devices? Which return code? */
291 * TODO: Various error checks etc.
293 * @return SR_OK upon success, SR_ERR_BUG upon errors.
295 SR_API int sr_session_run(void)
298 sr_err("session: %s: session was NULL; a session must be "
299 "created first, before running it.", __func__);
303 if (!session->devs) {
304 /* TODO: Actually the case? */
305 sr_err("session: %s: session->devs was NULL; a session "
306 "cannot be run without devices.", __func__);
310 sr_info("session: running");
311 session->running = TRUE;
313 /* Do we have real sources? */
314 if (num_sources == 1 && pollfds[0].fd == -1) {
315 /* Dummy source, freewheel over it. */
316 while (session->running)
317 sources[0].cb(-1, 0, sources[0].cb_data);
319 /* Real sources, use g_poll() main loop. */
320 sr_session_run_poll();
327 * Halt the current session.
329 * This requests the current session be stopped as soon as possible, for
330 * example on receiving an SR_DF_END packet.
332 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
334 SR_API int sr_session_halt(void)
337 sr_err("session: %s: session was NULL", __func__);
341 sr_info("session: halting");
342 session->running = FALSE;
348 * Stop the current session.
350 * The current session is stopped immediately, with all acquisition sessions
351 * being stopped and hardware drivers cleaned up.
353 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
355 SR_API int sr_session_stop(void)
361 sr_err("session: %s: session was NULL", __func__);
365 sr_info("session: stopping");
366 session->running = FALSE;
368 for (l = session->devs; l; l = l->next) {
370 /* Check for dev != NULL. */
372 if (dev->driver->dev_acquisition_stop)
373 dev->driver->dev_acquisition_stop(dev->driver_index, dev);
374 if (dev->driver->cleanup)
375 dev->driver->cleanup();
385 * @param packet The packet to show debugging information for.
387 static void datafeed_dump(struct sr_datafeed_packet *packet)
389 struct sr_datafeed_logic *logic;
390 struct sr_datafeed_analog *analog;
392 switch (packet->type) {
394 sr_dbg("bus: received SR_DF_HEADER");
397 sr_dbg("bus: received SR_DF_TRIGGER");
399 case SR_DF_META_LOGIC:
400 sr_dbg("bus: received SR_DF_META_LOGIC");
403 logic = packet->payload;
404 /* TODO: Check for logic != NULL. */
405 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
407 case SR_DF_META_ANALOG:
408 sr_dbg("bus: received SR_DF_META_LOGIC");
411 analog = packet->payload;
412 /* TODO: Check for analog != NULL. */
413 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
416 sr_dbg("bus: received SR_DF_END");
418 case SR_DF_FRAME_BEGIN:
419 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
421 case SR_DF_FRAME_END:
422 sr_dbg("bus: received SR_DF_FRAME_END");
425 sr_dbg("bus: received unknown packet type %d", packet->type);
431 * Send a packet to whatever is listening on the datafeed bus.
433 * Hardware drivers use this to send a data packet to the frontend.
436 * @param packet The datafeed packet to send to the session bus.
438 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
440 SR_PRIV int sr_session_send(struct sr_dev *dev,
441 struct sr_datafeed_packet *packet)
444 sr_datafeed_callback_t cb;
447 sr_err("session: %s: dev was NULL", __func__);
452 sr_err("session: %s: packet was NULL", __func__);
456 for (l = session->datafeed_callbacks; l; l = l->next) {
457 if (sr_log_loglevel_get() >= SR_LOG_DBG)
458 datafeed_dump(packet);
460 /* TODO: Check for cb != NULL. */
467 static int _sr_session_source_add(GPollFD *pollfd, int timeout,
468 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
470 struct source *new_sources, *s;
471 GPollFD *new_pollfds;
474 sr_err("session: %s: cb was NULL", __func__);
478 /* Note: cb_data can be NULL, that's not a bug. */
480 new_pollfds = g_try_realloc(pollfds, sizeof(GPollFD) * (num_sources + 1));
482 sr_err("session: %s: new_sources malloc failed", __func__);
483 return SR_ERR_MALLOC;
486 new_sources = g_try_realloc(sources, sizeof(struct source) * (num_sources + 1));
488 sr_err("session: %s: new_sources malloc failed", __func__);
489 return SR_ERR_MALLOC;
492 new_pollfds[num_sources] = *pollfd;
493 s = &new_sources[num_sources++];
494 s->timeout = timeout;
496 s->cb_data = cb_data;
497 s->poll_object = poll_object;
498 pollfds = new_pollfds;
499 sources = new_sources;
501 if (timeout != source_timeout && timeout > 0
502 && (source_timeout == -1 || timeout < source_timeout))
503 source_timeout = timeout;
509 * Add a event source for a file descriptor.
511 * @param fd The file descriptor.
512 * @param events Events to check for.
513 * @param timeout Max time to wait before the callback is called, ignored if 0.
514 * @param cb Callback function to add. Must not be NULL.
515 * @param cb_data Data for the callback function. Can be NULL.
517 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
518 * SR_ERR_MALLOC upon memory allocation errors.
520 SR_API int sr_session_source_add(int fd, int events, int timeout,
521 sr_receive_data_callback_t cb, void *cb_data)
526 g_io_channel_win32_make_pollfd(&channels[0],
533 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
537 * Add a event source for a GPollFD
539 * TODO: More error checks etc.
541 * @param pollfd The GPollFD.
542 * @param timeout Max time to wait before the callback is called, ignored if 0.
543 * @param cb Callback function to add. Must not be NULL.
544 * @param cb_data Data for the callback function. Can be NULL.
546 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
547 * SR_ERR_MALLOC upon memory allocation errors.
549 SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
550 sr_receive_data_callback_t cb, void *cb_data)
552 return _sr_session_source_add(pollfd, timeout, cb, cb_data, (gintptr)pollfd);
556 * Add a event source for a GIOChannel
558 * TODO: More error checks etc.
560 * @param channel The GIOChannel.
561 * @param events Events to poll on.
562 * @param timeout Max time to wait before the callback is called, ignored if 0.
563 * @param cb Callback function to add. Must not be NULL.
564 * @param cb_data Data for the callback function. Can be NULL.
566 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
567 * SR_ERR_MALLOC upon memory allocation errors.
569 SR_API int sr_session_source_add_channel(GIOChannel *channel, int events, int timeout,
570 sr_receive_data_callback_t cb, void *cb_data)
575 g_io_channel_win32_make_pollfd(channel,
578 p.fd = g_io_channel_unix_get_fd(channel);
582 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
586 static int _sr_session_source_remove(gintptr poll_object)
588 struct source *new_sources;
589 GPollFD *new_pollfds;
593 sr_err("session: %s: sources was NULL", __func__);
597 for (old = 0; old < num_sources; old++) {
598 if (sources[old].poll_object == poll_object)
602 /* fd not found, nothing to do */
603 if (old == num_sources)
608 if (old != num_sources) {
609 memmove(&pollfds[old], &pollfds[old+1],
610 (num_sources - old) * sizeof(GPollFD));
611 memmove(&sources[old], &sources[old+1],
612 (num_sources - old) * sizeof(struct source));
615 new_pollfds = g_try_realloc(sources, sizeof(GPollFD) * num_sources);
616 if (!new_pollfds && num_sources > 0) {
617 sr_err("session: %s: new_sources malloc failed", __func__);
618 return SR_ERR_MALLOC;
621 new_sources = g_try_realloc(sources, sizeof(struct source) * num_sources);
622 if (!new_sources && num_sources > 0) {
623 sr_err("session: %s: new_sources malloc failed", __func__);
624 return SR_ERR_MALLOC;
627 pollfds = new_pollfds;
628 sources = new_sources;
634 * Remove the source belonging to the specified file descriptor.
636 * TODO: More error checks.
638 * @param fd: The file descriptor for which the source should be removed.
640 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
641 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
644 SR_API int sr_session_source_remove(int fd)
646 return _sr_session_source_remove((gintptr)fd);
650 * Remove the source belonging to the specified poll descriptor.
652 * TODO: More error checks.
654 * @param pollfd The poll descriptor for which the source should be removed.
656 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
657 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
660 SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
662 return _sr_session_source_remove((gintptr)pollfd);
666 * Remove the source belonging to the specified channel.
668 * TODO: More error checks.
670 * @parama channel: The channel for which the source should be removed.
672 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
673 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
676 SR_API int sr_session_source_remove_channel(GIOChannel *channel)
678 return _sr_session_source_remove((gintptr)channel);