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_inst *sdi)
91 if (sdi->driver && sdi->driver->dev_close)
92 sdi->driver->dev_close(sdi);
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 instance to add to the current session. Must not
120 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
123 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
125 SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi)
130 sr_err("session: %s: sdi was NULL", __func__);
135 sr_err("session: %s: session was NULL", __func__);
139 /* If sdi->driver is NULL, this is a virtual device. */
141 sr_dbg("session: %s: sdi->driver was NULL, this seems to be "
142 "a virtual device; continuing", __func__);
143 /* Just add the device, don't run dev_open(). */
144 session->devs = g_slist_append(session->devs, (gpointer)sdi);
148 /* sdi->driver is non-NULL (i.e. we have a real device). */
149 if (!sdi->driver->dev_open) {
150 sr_err("session: %s: sdi->driver->dev_open was NULL", __func__);
154 if ((ret = sdi->driver->dev_open((struct sr_dev_inst *)sdi)) != SR_OK) {
155 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
159 session->devs = g_slist_append(session->devs, (gpointer)sdi);
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);
208 static int sr_session_run_poll(void)
213 while (session->num_sources > 0) {
214 ret = g_poll(session->pollfds, session->num_sources,
215 session->source_timeout);
216 for (i = 0; i < session->num_sources; i++) {
217 if (session->pollfds[i].revents > 0 || (ret == 0
218 && session->source_timeout == session->sources[i].timeout)) {
220 * Invoke the source's callback on an event,
221 * or if the poll timed out and this source
222 * asked for that timeout.
224 if (!session->sources[i].cb(session->pollfds[i].fd,
225 session->pollfds[i].revents,
226 session->sources[i].cb_data))
227 sr_session_source_remove(session->sources[i].poll_object);
238 * There can only be one session at a time.
240 * @return SR_OK upon success, SR_ERR upon errors.
242 SR_API int sr_session_start(void)
244 struct sr_dev_inst *sdi;
249 sr_err("session: %s: session was NULL; a session must be "
250 "created before starting it.", __func__);
254 if (!session->devs) {
255 sr_err("session: %s: session->devs was NULL; a session "
256 "cannot be started without devices.", __func__);
260 sr_info("session: starting");
262 for (l = session->devs; l; l = l->next) {
264 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
265 sr_err("session: %s: could not start an acquisition "
266 "(%d)", __func__, ret);
271 /* TODO: What if there are multiple devices? Which return code? */
279 * TODO: Various error checks etc.
281 * @return SR_OK upon success, SR_ERR_BUG upon errors.
283 SR_API int sr_session_run(void)
286 sr_err("session: %s: session was NULL; a session must be "
287 "created first, before running it.", __func__);
291 if (!session->devs) {
292 /* TODO: Actually the case? */
293 sr_err("session: %s: session->devs was NULL; a session "
294 "cannot be run without devices.", __func__);
298 sr_info("session: running");
300 /* Do we have real sources? */
301 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
302 /* Dummy source, freewheel over it. */
303 while (session->num_sources)
304 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
306 /* Real sources, use g_poll() main loop. */
307 sr_session_run_poll();
314 * Halt the current session.
316 * This function is deprecated and should not be used in new code, use
317 * sr_session_stop() instead. The behaviour of this function is identical to
320 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
322 SR_API int sr_session_halt(void)
324 return sr_session_stop();
328 * Stop the current session.
330 * The current session is stopped immediately, with all acquisition sessions
331 * being stopped and hardware drivers cleaned up.
333 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
335 SR_API int sr_session_stop(void)
337 struct sr_dev_inst *sdi;
341 sr_err("session: %s: session was NULL", __func__);
345 sr_info("session: stopping");
347 for (l = session->devs; l; l = l->next) {
350 if (sdi->driver->dev_acquisition_stop)
351 sdi->driver->dev_acquisition_stop(sdi, sdi);
361 * @param packet The packet to show debugging information for.
363 static void datafeed_dump(struct sr_datafeed_packet *packet)
365 struct sr_datafeed_logic *logic;
366 struct sr_datafeed_analog *analog;
368 switch (packet->type) {
370 sr_dbg("bus: received SR_DF_HEADER");
373 sr_dbg("bus: received SR_DF_TRIGGER");
375 case SR_DF_META_LOGIC:
376 sr_dbg("bus: received SR_DF_META_LOGIC");
379 logic = packet->payload;
380 /* TODO: Check for logic != NULL. */
381 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
383 case SR_DF_META_ANALOG:
384 sr_dbg("bus: received SR_DF_META_LOGIC");
387 analog = packet->payload;
388 /* TODO: Check for analog != NULL. */
389 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
392 sr_dbg("bus: received SR_DF_END");
394 case SR_DF_FRAME_BEGIN:
395 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
397 case SR_DF_FRAME_END:
398 sr_dbg("bus: received SR_DF_FRAME_END");
401 sr_dbg("bus: received unknown packet type %d", packet->type);
407 * Send a packet to whatever is listening on the datafeed bus.
409 * Hardware drivers use this to send a data packet to the frontend.
412 * @param packet The datafeed packet to send to the session bus.
414 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
416 SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
417 struct sr_datafeed_packet *packet)
420 sr_datafeed_callback_t cb;
423 sr_err("session: %s: sdi was NULL", __func__);
428 sr_err("session: %s: packet was NULL", __func__);
432 for (l = session->datafeed_callbacks; l; l = l->next) {
433 if (sr_log_loglevel_get() >= SR_LOG_DBG)
434 datafeed_dump(packet);
442 static int _sr_session_source_add(GPollFD *pollfd, int timeout,
443 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
445 struct source *new_sources, *s;
446 GPollFD *new_pollfds;
449 sr_err("session: %s: cb was NULL", __func__);
453 /* Note: cb_data can be NULL, that's not a bug. */
455 new_pollfds = g_try_realloc(session->pollfds,
456 sizeof(GPollFD) * (session->num_sources + 1));
458 sr_err("session: %s: new_pollfds malloc failed", __func__);
459 return SR_ERR_MALLOC;
462 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
463 (session->num_sources + 1));
465 sr_err("session: %s: new_sources malloc failed", __func__);
466 return SR_ERR_MALLOC;
469 new_pollfds[session->num_sources] = *pollfd;
470 s = &new_sources[session->num_sources++];
471 s->timeout = timeout;
473 s->cb_data = cb_data;
474 s->poll_object = poll_object;
475 session->pollfds = new_pollfds;
476 session->sources = new_sources;
478 if (timeout != session->source_timeout && timeout > 0
479 && (session->source_timeout == -1 || timeout < session->source_timeout))
480 session->source_timeout = timeout;
486 * Add a event source for a file descriptor.
488 * @param fd The file descriptor.
489 * @param events Events to check for.
490 * @param timeout Max time to wait before the callback is called, ignored if 0.
491 * @param cb Callback function to add. Must not be NULL.
492 * @param cb_data Data for the callback function. Can be NULL.
494 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
495 * SR_ERR_MALLOC upon memory allocation errors.
497 SR_API int sr_session_source_add(int fd, int events, int timeout,
498 sr_receive_data_callback_t cb, void *cb_data)
505 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
509 * Add an event source for a GPollFD.
511 * TODO: More error checks etc.
513 * @param pollfd The GPollFD.
514 * @param timeout Max time to wait before the callback is called, ignored if 0.
515 * @param cb Callback function to add. Must not be NULL.
516 * @param cb_data Data for the callback function. Can be NULL.
518 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
519 * SR_ERR_MALLOC upon memory allocation errors.
521 SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
522 sr_receive_data_callback_t cb, void *cb_data)
524 return _sr_session_source_add(pollfd, timeout, cb,
525 cb_data, (gintptr)pollfd);
529 * Add an event source for a GIOChannel.
531 * TODO: More error checks etc.
533 * @param channel The GIOChannel.
534 * @param events Events to poll on.
535 * @param timeout Max time to wait before the callback is called, ignored if 0.
536 * @param cb Callback function to add. Must not be NULL.
537 * @param cb_data Data for the callback function. Can be NULL.
539 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
540 * SR_ERR_MALLOC upon memory allocation errors.
542 SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
543 int timeout, sr_receive_data_callback_t cb, void *cb_data)
548 g_io_channel_win32_make_pollfd(channel,
551 p.fd = g_io_channel_unix_get_fd(channel);
555 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
559 static int _sr_session_source_remove(gintptr poll_object)
561 struct source *new_sources;
562 GPollFD *new_pollfds;
565 if (!session->sources || !session->num_sources) {
566 sr_err("session: %s: sources was NULL", __func__);
570 for (old = 0; old < session->num_sources; old++) {
571 if (session->sources[old].poll_object == poll_object)
575 /* fd not found, nothing to do */
576 if (old == session->num_sources)
579 session->num_sources -= 1;
581 if (old != session->num_sources) {
582 memmove(&session->pollfds[old], &session->pollfds[old+1],
583 (session->num_sources - old) * sizeof(GPollFD));
584 memmove(&session->sources[old], &session->sources[old+1],
585 (session->num_sources - old) * sizeof(struct source));
588 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
589 if (!new_pollfds && session->num_sources > 0) {
590 sr_err("session: %s: new_pollfds malloc failed", __func__);
591 return SR_ERR_MALLOC;
594 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
595 if (!new_sources && session->num_sources > 0) {
596 sr_err("session: %s: new_sources malloc failed", __func__);
597 return SR_ERR_MALLOC;
600 session->pollfds = new_pollfds;
601 session->sources = new_sources;
607 * Remove the source belonging to the specified file descriptor.
609 * TODO: More error checks.
611 * @param fd The file descriptor for which the source should be removed.
613 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
614 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
617 SR_API int sr_session_source_remove(int fd)
619 return _sr_session_source_remove((gintptr)fd);
623 * Remove the source belonging to the specified poll descriptor.
625 * TODO: More error checks.
627 * @param pollfd The poll descriptor for which the source should be removed.
629 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
630 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
633 SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
635 return _sr_session_source_remove((gintptr)pollfd);
639 * Remove the source belonging to the specified channel.
641 * TODO: More error checks.
643 * @param channel The channel for which the source should be removed.
645 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
646 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
649 SR_API int sr_session_source_remove_channel(GIOChannel *channel)
651 return _sr_session_source_remove((gintptr)channel);