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"
30 sr_receive_data_callback_t cb;
33 /* This is used to keep track of the object (fd, pollfd or channel) which is
34 * being polled and will be used to match the source when removing it again.
39 /* There can only be one session at a time. */
40 /* 'session' is not static, it's used elsewhere (via 'extern'). */
41 struct sr_session *session;
42 static int num_sources = 0;
44 /* Both "sources" and "pollfds" are of the same size and contain pairs of
45 * descriptor and callback function. We can not embed the GPollFD into the
46 * source struct since we want to be able to pass the array of all poll
47 * descriptors to g_poll.
49 static struct source *sources = NULL;
50 static GPollFD *pollfds;
51 static int source_timeout = -1;
54 * Create a new session.
56 * TODO: Should it use the file-global "session" variable or take an argument?
57 * The same question applies to all the other session functions.
59 * @return A pointer to the newly allocated session, or NULL upon errors.
61 SR_API struct sr_session *sr_session_new(void)
63 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
64 sr_err("session: %s: session malloc failed", __func__);
65 return NULL; /* TODO: SR_ERR_MALLOC? */
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 g_slist_free(session->devs);
88 /* TODO: Error checks needed? */
90 /* TODO: Loop over protocol decoders and free them. */
99 * Remove all the devices from the current session. TODO?
101 * The session itself (i.e., the struct sr_session) is not free'd and still
102 * exists after this function returns.
104 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
106 SR_API int sr_session_dev_remove_all(void)
109 sr_err("session: %s: session was NULL", __func__);
113 g_slist_free(session->devs);
114 session->devs = NULL;
120 * Add a device to the current session.
122 * @param dev The device to add to the current session. Must not be NULL.
123 * Also, dev->driver and dev->driver->dev_open must not be NULL.
125 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
127 SR_API int sr_session_dev_add(struct sr_dev *dev)
132 sr_err("session: %s: dev was NULL", __func__);
137 sr_err("session: %s: session was NULL", __func__);
141 /* If dev->driver is NULL, this is a virtual device. */
143 sr_dbg("session: %s: dev->driver was NULL, this seems to be "
144 "a virtual device; continuing", __func__);
145 /* Just add the device, don't run dev_open(). */
146 session->devs = g_slist_append(session->devs, dev);
150 /* dev->driver is non-NULL (i.e. we have a real device). */
151 if (!dev->driver->dev_open) {
152 sr_err("session: %s: dev->driver->dev_open was NULL",
157 if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) {
158 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
162 session->devs = g_slist_append(session->devs, dev);
168 * Remove all datafeed callbacks in the current session.
170 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
172 SR_API int sr_session_datafeed_callback_remove_all(void)
175 sr_err("session: %s: session was NULL", __func__);
179 g_slist_free(session->datafeed_callbacks);
180 session->datafeed_callbacks = NULL;
186 * Add a datafeed callback to the current session.
188 * @param cb Function to call when a chunk of data is received.
191 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
193 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
196 sr_err("session: %s: session was NULL", __func__);
201 sr_err("session: %s: cb was NULL", __func__);
205 session->datafeed_callbacks =
206 g_slist_append(session->datafeed_callbacks, cb);
214 static int sr_session_run_poll(void)
218 while (session->running) {
219 ret = g_poll(pollfds, num_sources, source_timeout);
221 for (i = 0; i < num_sources; i++) {
222 if (pollfds[i].revents > 0 || (ret == 0
223 && source_timeout == sources[i].timeout)) {
225 * Invoke the source's callback on an event,
226 * or if the poll timeout out and this source
227 * asked for that timeout.
229 if (!sources[i].cb(pollfds[i].fd, pollfds[i].revents,
231 sr_session_source_remove(pollfds[i].fd);
242 * There can only be one session at a time.
244 * @return SR_OK upon success, SR_ERR upon errors.
246 SR_API int sr_session_start(void)
253 sr_err("session: %s: session was NULL; a session must be "
254 "created first, before starting it.", __func__);
258 if (!session->devs) {
259 /* TODO: Actually the case? */
260 sr_err("session: %s: session->devs was NULL; a session "
261 "cannot be started without devices.", __func__);
265 /* TODO: Check driver_index validity? */
267 sr_info("session: starting");
269 for (l = session->devs; l; l = l->next) {
271 /* TODO: Check for dev != NULL. */
272 if ((ret = dev->driver->dev_acquisition_start(
273 dev->driver_index, dev)) != SR_OK) {
274 sr_err("session: %s: could not start an acquisition "
275 "(%d)", __func__, ret);
280 /* TODO: What if there are multiple devices? Which return code? */
288 * TODO: Various error checks etc.
290 * @return SR_OK upon success, SR_ERR_BUG upon errors.
292 SR_API int sr_session_run(void)
295 sr_err("session: %s: session was NULL; a session must be "
296 "created first, before running it.", __func__);
300 if (!session->devs) {
301 /* TODO: Actually the case? */
302 sr_err("session: %s: session->devs was NULL; a session "
303 "cannot be run without devices.", __func__);
307 sr_info("session: running");
308 session->running = TRUE;
310 /* Do we have real sources? */
311 if (num_sources == 1 && pollfds[0].fd == -1) {
312 /* Dummy source, freewheel over it. */
313 while (session->running)
314 sources[0].cb(-1, 0, sources[0].cb_data);
316 /* Real sources, use g_poll() main loop. */
317 sr_session_run_poll();
324 * Halt the current session.
326 * This requests the current session be stopped as soon as possible, for
327 * example on receiving an SR_DF_END packet.
329 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
331 SR_API int sr_session_halt(void)
334 sr_err("session: %s: session was NULL", __func__);
338 sr_info("session: halting");
339 session->running = FALSE;
345 * Stop the current session.
347 * The current session is stopped immediately, with all acquisition sessions
348 * being stopped and hardware drivers cleaned up.
350 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
352 SR_API int sr_session_stop(void)
358 sr_err("session: %s: session was NULL", __func__);
362 sr_info("session: stopping");
363 session->running = FALSE;
365 for (l = session->devs; l; l = l->next) {
367 /* Check for dev != NULL. */
369 if (dev->driver->dev_acquisition_stop)
370 dev->driver->dev_acquisition_stop(dev->driver_index, dev);
371 if (dev->driver->cleanup)
372 dev->driver->cleanup();
382 * @param packet The packet to show debugging information for.
384 static void datafeed_dump(struct sr_datafeed_packet *packet)
386 struct sr_datafeed_logic *logic;
387 struct sr_datafeed_analog *analog;
389 switch (packet->type) {
391 sr_dbg("bus: received SR_DF_HEADER");
394 sr_dbg("bus: received SR_DF_TRIGGER");
396 case SR_DF_META_LOGIC:
397 sr_dbg("bus: received SR_DF_META_LOGIC");
400 logic = packet->payload;
401 /* TODO: Check for logic != NULL. */
402 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
404 case SR_DF_META_ANALOG:
405 sr_dbg("bus: received SR_DF_META_LOGIC");
408 analog = packet->payload;
409 /* TODO: Check for analog != NULL. */
410 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
413 sr_dbg("bus: received SR_DF_END");
415 case SR_DF_FRAME_BEGIN:
416 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
418 case SR_DF_FRAME_END:
419 sr_dbg("bus: received SR_DF_FRAME_END");
422 sr_dbg("bus: received unknown packet type %d", packet->type);
428 * Send a packet to whatever is listening on the datafeed bus.
430 * Hardware drivers use this to send a data packet to the frontend.
433 * @param packet The datafeed packet to send to the session bus.
435 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
437 SR_PRIV int sr_session_send(struct sr_dev *dev,
438 struct sr_datafeed_packet *packet)
441 sr_datafeed_callback_t cb;
444 sr_err("session: %s: dev was NULL", __func__);
449 sr_err("session: %s: packet was NULL", __func__);
453 for (l = session->datafeed_callbacks; l; l = l->next) {
454 if (sr_log_loglevel_get() >= SR_LOG_DBG)
455 datafeed_dump(packet);
457 /* TODO: Check for cb != NULL. */
464 static int _sr_session_source_add(GPollFD *pollfd, int timeout,
465 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
467 struct source *new_sources, *s;
468 GPollFD *new_pollfds;
471 sr_err("session: %s: cb was NULL", __func__);
475 /* Note: cb_data can be NULL, that's not a bug. */
477 new_pollfds = g_try_realloc(pollfds, sizeof(GPollFD) * (num_sources + 1));
479 sr_err("session: %s: new_pollfds malloc failed", __func__);
480 return SR_ERR_MALLOC;
483 new_sources = g_try_realloc(sources, sizeof(struct source) * (num_sources + 1));
485 sr_err("session: %s: new_sources malloc failed", __func__);
486 return SR_ERR_MALLOC;
489 new_pollfds[num_sources] = *pollfd;
490 s = &new_sources[num_sources++];
491 s->timeout = timeout;
493 s->cb_data = cb_data;
494 s->poll_object = poll_object;
495 pollfds = new_pollfds;
496 sources = new_sources;
498 if (timeout != source_timeout && timeout > 0
499 && (source_timeout == -1 || timeout < source_timeout))
500 source_timeout = timeout;
506 * Add a event source for a file descriptor.
508 * @param fd The file descriptor.
509 * @param events Events to check for.
510 * @param timeout Max time to wait before the callback is called, ignored if 0.
511 * @param cb Callback function to add. Must not be NULL.
512 * @param cb_data Data for the callback function. Can be NULL.
514 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
515 * SR_ERR_MALLOC upon memory allocation errors.
517 SR_API int sr_session_source_add(int fd, int events, int timeout,
518 sr_receive_data_callback_t cb, void *cb_data)
525 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
529 * Add an event source for a GPollFD.
531 * TODO: More error checks etc.
533 * @param pollfd The GPollFD.
534 * @param timeout Max time to wait before the callback is called, ignored if 0.
535 * @param cb Callback function to add. Must not be NULL.
536 * @param cb_data Data for the callback function. Can be NULL.
538 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
539 * SR_ERR_MALLOC upon memory allocation errors.
541 SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
542 sr_receive_data_callback_t cb, void *cb_data)
544 return _sr_session_source_add(pollfd, timeout, cb,
545 cb_data, (gintptr)pollfd);
549 * Add an event source for a GIOChannel.
551 * TODO: More error checks etc.
553 * @param channel The GIOChannel.
554 * @param events Events to poll on.
555 * @param timeout Max time to wait before the callback is called, ignored if 0.
556 * @param cb Callback function to add. Must not be NULL.
557 * @param cb_data Data for the callback function. Can be NULL.
559 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
560 * SR_ERR_MALLOC upon memory allocation errors.
562 SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
563 int timeout, sr_receive_data_callback_t cb, void *cb_data)
568 g_io_channel_win32_make_pollfd(channel,
571 p.fd = g_io_channel_unix_get_fd(channel);
575 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
579 static int _sr_session_source_remove(gintptr poll_object)
581 struct source *new_sources;
582 GPollFD *new_pollfds;
586 sr_err("session: %s: sources was NULL", __func__);
590 for (old = 0; old < num_sources; old++) {
591 if (sources[old].poll_object == poll_object)
595 /* fd not found, nothing to do */
596 if (old == num_sources)
601 if (old != num_sources) {
602 memmove(&pollfds[old], &pollfds[old+1],
603 (num_sources - old) * sizeof(GPollFD));
604 memmove(&sources[old], &sources[old+1],
605 (num_sources - old) * sizeof(struct source));
608 new_pollfds = g_try_realloc(pollfds, sizeof(GPollFD) * num_sources);
609 if (!new_pollfds && num_sources > 0) {
610 sr_err("session: %s: new_pollfds malloc failed", __func__);
611 return SR_ERR_MALLOC;
614 new_sources = g_try_realloc(sources, sizeof(struct source) * num_sources);
615 if (!new_sources && num_sources > 0) {
616 sr_err("session: %s: new_sources malloc failed", __func__);
617 return SR_ERR_MALLOC;
620 pollfds = new_pollfds;
621 sources = new_sources;
627 * Remove the source belonging to the specified file descriptor.
629 * TODO: More error checks.
631 * @param fd The file descriptor for which the source should be removed.
633 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
634 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
637 SR_API int sr_session_source_remove(int fd)
639 return _sr_session_source_remove((gintptr)fd);
643 * Remove the source belonging to the specified poll descriptor.
645 * TODO: More error checks.
647 * @param pollfd The poll descriptor for which the source should be removed.
649 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
650 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
653 SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
655 return _sr_session_source_remove((gintptr)pollfd);
659 * Remove the source belonging to the specified channel.
661 * TODO: More error checks.
663 * @param channel The channel for which the source should be removed.
665 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
666 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
669 SR_API int sr_session_source_remove_channel(GIOChannel *channel)
671 return _sr_session_source_remove((gintptr)channel);