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;
44 * Create a new session.
46 * TODO: Should it use the file-global "session" variable or take an argument?
47 * The same question applies to all the other session functions.
49 * @return A pointer to the newly allocated session, or NULL upon errors.
51 SR_API struct sr_session *sr_session_new(void)
53 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
54 sr_err("session: %s: session malloc failed", __func__);
55 return NULL; /* TODO: SR_ERR_MALLOC? */
58 session->source_timeout = -1;
64 * Destroy the current session.
66 * This frees up all memory used by the session.
68 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
70 SR_API int sr_session_destroy(void)
73 sr_err("session: %s: session was NULL", __func__);
77 sr_session_dev_remove_all();
79 /* TODO: Error checks needed? */
81 /* TODO: Loop over protocol decoders and free them. */
89 static void sr_dev_close(struct sr_dev *dev)
91 if (dev->driver->dev_close)
92 dev->driver->dev_close(dev->driver_index);
96 * Remove all the devices from the current session. TODO?
98 * The session itself (i.e., the struct sr_session) is not free'd and still
99 * exists after this function returns.
101 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
103 SR_API int sr_session_dev_remove_all(void)
106 sr_err("session: %s: session was NULL", __func__);
110 g_slist_free_full(session->devs, (GDestroyNotify)sr_dev_close);
111 session->devs = NULL;
117 * Add a device to the current session.
119 * @param dev The device to add to the current session. Must not be NULL.
120 * Also, dev->driver and dev->driver->dev_open must not be NULL.
122 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
124 SR_API int sr_session_dev_add(struct sr_dev *dev)
129 sr_err("session: %s: dev was NULL", __func__);
134 sr_err("session: %s: session was NULL", __func__);
138 /* If dev->driver is NULL, this is a virtual device. */
140 sr_dbg("session: %s: dev->driver was NULL, this seems to be "
141 "a virtual device; continuing", __func__);
142 /* Just add the device, don't run dev_open(). */
143 session->devs = g_slist_append(session->devs, dev);
147 /* dev->driver is non-NULL (i.e. we have a real device). */
148 if (!dev->driver->dev_open) {
149 sr_err("session: %s: dev->driver->dev_open was NULL",
154 if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) {
155 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
159 session->devs = g_slist_append(session->devs, dev);
165 * Remove all datafeed callbacks in the current session.
167 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
169 SR_API int sr_session_datafeed_callback_remove_all(void)
172 sr_err("session: %s: session was NULL", __func__);
176 g_slist_free(session->datafeed_callbacks);
177 session->datafeed_callbacks = NULL;
183 * Add a datafeed callback to the current session.
185 * @param cb Function to call when a chunk of data is received.
188 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
190 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
193 sr_err("session: %s: session was NULL", __func__);
198 sr_err("session: %s: cb was NULL", __func__);
202 session->datafeed_callbacks =
203 g_slist_append(session->datafeed_callbacks, cb);
211 static int sr_session_run_poll(void)
216 while (session->running) {
217 ret = g_poll(session->pollfds, session->num_sources, session->source_timeout);
219 for (i = 0; i < session->num_sources; i++) {
220 if (session->pollfds[i].revents > 0 || (ret == 0
221 && session->source_timeout == session->sources[i].timeout)) {
223 * Invoke the source's callback on an event,
224 * or if the poll timeout out and this source
225 * asked for that timeout.
227 if (!session->sources[i].cb(session->pollfds[i].fd, session->pollfds[i].revents,
228 session->sources[i].cb_data))
229 sr_session_source_remove(session->sources[i].poll_object);
240 * There can only be one session at a time.
242 * @return SR_OK upon success, SR_ERR upon errors.
244 SR_API int sr_session_start(void)
251 sr_err("session: %s: session was NULL; a session must be "
252 "created first, before starting it.", __func__);
256 if (!session->devs) {
257 /* TODO: Actually the case? */
258 sr_err("session: %s: session->devs was NULL; a session "
259 "cannot be started without devices.", __func__);
263 /* TODO: Check driver_index validity? */
265 sr_info("session: starting");
267 for (l = session->devs; l; l = l->next) {
269 /* TODO: Check for dev != NULL. */
270 if ((ret = dev->driver->dev_acquisition_start(
271 dev->driver_index, dev)) != SR_OK) {
272 sr_err("session: %s: could not start an acquisition "
273 "(%d)", __func__, ret);
278 /* TODO: What if there are multiple devices? Which return code? */
286 * TODO: Various error checks etc.
288 * @return SR_OK upon success, SR_ERR_BUG upon errors.
290 SR_API int sr_session_run(void)
293 sr_err("session: %s: session was NULL; a session must be "
294 "created first, before running it.", __func__);
298 if (!session->devs) {
299 /* TODO: Actually the case? */
300 sr_err("session: %s: session->devs was NULL; a session "
301 "cannot be run without devices.", __func__);
305 sr_info("session: running");
306 session->running = TRUE;
308 /* Do we have real sources? */
309 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
310 /* Dummy source, freewheel over it. */
311 while (session->running)
312 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
314 /* Real sources, use g_poll() main loop. */
315 sr_session_run_poll();
322 * Halt the current session.
324 * This requests the current session be stopped as soon as possible, for
325 * example on receiving an SR_DF_END packet.
327 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
329 SR_API int sr_session_halt(void)
332 sr_err("session: %s: session was NULL", __func__);
336 sr_info("session: halting");
337 session->running = FALSE;
343 * Stop the current session.
345 * The current session is stopped immediately, with all acquisition sessions
346 * being stopped and hardware drivers cleaned up.
348 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
350 SR_API int sr_session_stop(void)
356 sr_err("session: %s: session was NULL", __func__);
360 sr_info("session: stopping");
361 session->running = FALSE;
363 for (l = session->devs; l; l = l->next) {
365 /* Check for dev != NULL. */
367 if (dev->driver->dev_acquisition_stop)
368 dev->driver->dev_acquisition_stop(dev->driver_index, dev);
378 * @param packet The packet to show debugging information for.
380 static void datafeed_dump(struct sr_datafeed_packet *packet)
382 struct sr_datafeed_logic *logic;
383 struct sr_datafeed_analog *analog;
385 switch (packet->type) {
387 sr_dbg("bus: received SR_DF_HEADER");
390 sr_dbg("bus: received SR_DF_TRIGGER");
392 case SR_DF_META_LOGIC:
393 sr_dbg("bus: received SR_DF_META_LOGIC");
396 logic = packet->payload;
397 /* TODO: Check for logic != NULL. */
398 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
400 case SR_DF_META_ANALOG:
401 sr_dbg("bus: received SR_DF_META_LOGIC");
404 analog = packet->payload;
405 /* TODO: Check for analog != NULL. */
406 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
409 sr_dbg("bus: received SR_DF_END");
411 case SR_DF_FRAME_BEGIN:
412 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
414 case SR_DF_FRAME_END:
415 sr_dbg("bus: received SR_DF_FRAME_END");
418 sr_dbg("bus: received unknown packet type %d", packet->type);
424 * Send a packet to whatever is listening on the datafeed bus.
426 * Hardware drivers use this to send a data packet to the frontend.
429 * @param packet The datafeed packet to send to the session bus.
431 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
433 SR_PRIV int sr_session_send(struct sr_dev *dev,
434 struct sr_datafeed_packet *packet)
437 sr_datafeed_callback_t cb;
440 sr_err("session: %s: dev was NULL", __func__);
445 sr_err("session: %s: packet was NULL", __func__);
449 for (l = session->datafeed_callbacks; l; l = l->next) {
450 if (sr_log_loglevel_get() >= SR_LOG_DBG)
451 datafeed_dump(packet);
453 /* TODO: Check for cb != NULL. */
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, sizeof(GPollFD) * (session->num_sources + 1));
475 sr_err("session: %s: new_pollfds malloc failed", __func__);
476 return SR_ERR_MALLOC;
479 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
480 (session->num_sources + 1));
482 sr_err("session: %s: new_sources malloc failed", __func__);
483 return SR_ERR_MALLOC;
486 new_pollfds[session->num_sources] = *pollfd;
487 s = &new_sources[session->num_sources++];
488 s->timeout = timeout;
490 s->cb_data = cb_data;
491 s->poll_object = poll_object;
492 session->pollfds = new_pollfds;
493 session->sources = new_sources;
495 if (timeout != session->source_timeout && timeout > 0
496 && (session->source_timeout == -1 || timeout < session->source_timeout))
497 session->source_timeout = timeout;
503 * Add a event source for a file descriptor.
505 * @param fd The file descriptor.
506 * @param events Events to check for.
507 * @param timeout Max time to wait before the callback is called, ignored if 0.
508 * @param cb Callback function to add. Must not be NULL.
509 * @param cb_data Data for the callback function. Can be NULL.
511 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
512 * SR_ERR_MALLOC upon memory allocation errors.
514 SR_API int sr_session_source_add(int fd, int events, int timeout,
515 sr_receive_data_callback_t cb, void *cb_data)
522 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
526 * Add an event source for a GPollFD.
528 * TODO: More error checks etc.
530 * @param pollfd The GPollFD.
531 * @param timeout Max time to wait before the callback is called, ignored if 0.
532 * @param cb Callback function to add. Must not be NULL.
533 * @param cb_data Data for the callback function. Can be NULL.
535 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
536 * SR_ERR_MALLOC upon memory allocation errors.
538 SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
539 sr_receive_data_callback_t cb, void *cb_data)
541 return _sr_session_source_add(pollfd, timeout, cb,
542 cb_data, (gintptr)pollfd);
546 * Add an event source for a GIOChannel.
548 * TODO: More error checks etc.
550 * @param channel The GIOChannel.
551 * @param events Events to poll on.
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_channel(GIOChannel *channel, int events,
560 int timeout, sr_receive_data_callback_t cb, void *cb_data)
565 g_io_channel_win32_make_pollfd(channel,
568 p.fd = g_io_channel_unix_get_fd(channel);
572 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
576 static int _sr_session_source_remove(gintptr poll_object)
578 struct source *new_sources;
579 GPollFD *new_pollfds;
582 if (!session->sources || !session->num_sources) {
583 sr_err("session: %s: sources was NULL", __func__);
587 for (old = 0; old < session->num_sources; old++) {
588 if (session->sources[old].poll_object == poll_object)
592 /* fd not found, nothing to do */
593 if (old == session->num_sources)
596 session->num_sources -= 1;
598 if (old != session->num_sources) {
599 memmove(&session->pollfds[old], &session->pollfds[old+1],
600 (session->num_sources - old) * sizeof(GPollFD));
601 memmove(&session->sources[old], &session->sources[old+1],
602 (session->num_sources - old) * sizeof(struct source));
605 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
606 if (!new_pollfds && session->num_sources > 0) {
607 sr_err("session: %s: new_pollfds malloc failed", __func__);
608 return SR_ERR_MALLOC;
611 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
612 if (!new_sources && session->num_sources > 0) {
613 sr_err("session: %s: new_sources malloc failed", __func__);
614 return SR_ERR_MALLOC;
617 session->pollfds = new_pollfds;
618 session->sources = new_sources;
624 * Remove the source belonging to the specified file descriptor.
626 * TODO: More error checks.
628 * @param fd The file descriptor for which the source should be removed.
630 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
631 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
634 SR_API int sr_session_source_remove(int fd)
636 return _sr_session_source_remove((gintptr)fd);
640 * Remove the source belonging to the specified poll descriptor.
642 * TODO: More error checks.
644 * @param pollfd The poll descriptor for which the source should be removed.
646 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
647 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
650 SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
652 return _sr_session_source_remove((gintptr)pollfd);
656 * Remove the source belonging to the specified channel.
658 * TODO: More error checks.
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);