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;
37 /* There can only be one session at a time. */
38 /* 'session' is not static, it's used elsewhere (via 'extern'). */
39 struct sr_session *session;
40 static int num_sources = 0;
42 /* Both "sources" and "pollfds" are of the same size and contain pairs of
43 * descriptor and callback function. We can not embed the GPollFD into the
44 * source struct since we want to be able to pass the array of all poll
45 * descriptors to g_poll.
47 static struct source *sources = NULL;
48 static GPollFD *pollfds;
49 static int source_timeout = -1;
52 * Create a new session.
54 * TODO: Should it use the file-global "session" variable or take an argument?
55 * The same question applies to all the other session functions.
57 * @return A pointer to the newly allocated session, or NULL upon errors.
59 SR_API struct sr_session *sr_session_new(void)
61 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
62 sr_err("session: %s: session malloc failed", __func__);
63 return NULL; /* TODO: SR_ERR_MALLOC? */
70 * Destroy the current session.
72 * This frees up all memory used by the session.
74 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
76 SR_API int sr_session_destroy(void)
79 sr_err("session: %s: session was NULL", __func__);
83 g_slist_free(session->devs);
86 /* TODO: Error checks needed? */
88 /* TODO: Loop over protocol decoders and free them. */
97 * Remove all the devices from the current session. TODO?
99 * The session itself (i.e., the struct sr_session) is not free'd and still
100 * exists after this function returns.
102 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
104 SR_API int sr_session_dev_remove_all(void)
107 sr_err("session: %s: session was NULL", __func__);
111 g_slist_free(session->devs);
112 session->devs = NULL;
118 * Add a device to the current session.
120 * @param dev The device to add to the current session. Must not be NULL.
121 * Also, dev->driver and dev->driver->dev_open must not be NULL.
123 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
125 SR_API int sr_session_dev_add(struct sr_dev *dev)
130 sr_err("session: %s: dev was NULL", __func__);
135 sr_err("session: %s: session was NULL", __func__);
139 /* If dev->driver is NULL, this is a virtual device. */
141 sr_dbg("session: %s: dev->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, dev);
148 /* dev->driver is non-NULL (i.e. we have a real device). */
149 if (!dev->driver->dev_open) {
150 sr_err("session: %s: dev->driver->dev_open was NULL",
155 if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) {
156 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
160 session->devs = g_slist_append(session->devs, dev);
166 * Remove all datafeed callbacks in the current session.
168 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
170 SR_API int sr_session_datafeed_callback_remove_all(void)
173 sr_err("session: %s: session was NULL", __func__);
177 g_slist_free(session->datafeed_callbacks);
178 session->datafeed_callbacks = NULL;
184 * Add a datafeed callback to the current session.
186 * @param cb Function to call when a chunk of data is received.
189 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
191 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
194 sr_err("session: %s: session was NULL", __func__);
199 sr_err("session: %s: cb was NULL", __func__);
203 session->datafeed_callbacks =
204 g_slist_append(session->datafeed_callbacks, cb);
212 static int sr_session_run_poll(void)
216 while (session->running) {
217 ret = g_poll(pollfds, num_sources, source_timeout);
219 for (i = 0; i < num_sources; i++) {
220 if (pollfds[i].revents > 0 || (ret == 0
221 && source_timeout == 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 (!sources[i].cb(pollfds[i].fd, pollfds[i].revents,
229 sr_session_source_remove(pollfds[i].fd);
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 (num_sources == 1 && pollfds[0].fd == -1) {
310 /* Dummy source, freewheel over it. */
311 while (session->running)
312 sources[0].cb(-1, 0, 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);
369 if (dev->driver->cleanup)
370 dev->driver->cleanup();
380 * @param packet The packet to show debugging information for.
382 static void datafeed_dump(struct sr_datafeed_packet *packet)
384 struct sr_datafeed_logic *logic;
385 struct sr_datafeed_analog *analog;
387 switch (packet->type) {
389 sr_dbg("bus: received SR_DF_HEADER");
392 sr_dbg("bus: received SR_DF_TRIGGER");
394 case SR_DF_META_LOGIC:
395 sr_dbg("bus: received SR_DF_META_LOGIC");
398 logic = packet->payload;
399 /* TODO: Check for logic != NULL. */
400 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
402 case SR_DF_META_ANALOG:
403 sr_dbg("bus: received SR_DF_META_LOGIC");
406 analog = packet->payload;
407 /* TODO: Check for analog != NULL. */
408 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
411 sr_dbg("bus: received SR_DF_END");
413 case SR_DF_FRAME_BEGIN:
414 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
416 case SR_DF_FRAME_END:
417 sr_dbg("bus: received SR_DF_FRAME_END");
420 sr_dbg("bus: received unknown packet type %d", packet->type);
426 * Send a packet to whatever is listening on the datafeed bus.
428 * Hardware drivers use this to send a data packet to the frontend.
431 * @param packet The datafeed packet to send to the session bus.
433 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
435 SR_PRIV int sr_session_send(struct sr_dev *dev,
436 struct sr_datafeed_packet *packet)
439 sr_datafeed_callback_t cb;
442 sr_err("session: %s: dev was NULL", __func__);
447 sr_err("session: %s: packet was NULL", __func__);
451 for (l = session->datafeed_callbacks; l; l = l->next) {
452 if (sr_log_loglevel_get() >= SR_LOG_DBG)
453 datafeed_dump(packet);
455 /* TODO: Check for cb != NULL. */
465 * TODO: More error checks etc.
468 * @param events TODO.
469 * @param timeout TODO.
470 * @param cb Callback function to add. Must not be NULL.
471 * @param cb_data Data for the callback function. Can be NULL.
473 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
474 * SR_ERR_MALLOC upon memory allocation errors.
476 SR_API int sr_session_source_add(int fd, int events, int timeout,
477 sr_receive_data_callback_t cb, void *cb_data)
479 struct source *new_sources, *s;
480 GPollFD *new_pollfds, *p;
483 sr_err("session: %s: cb was NULL", __func__);
487 /* Note: cb_data can be NULL, that's not a bug. */
489 new_pollfds = g_try_realloc(pollfds, sizeof(GPollFD) * (num_sources + 1));
491 sr_err("session: %s: new_sources malloc failed", __func__);
492 return SR_ERR_MALLOC;
495 new_sources = g_try_realloc(sources, sizeof(struct source) * (num_sources + 1));
497 sr_err("session: %s: new_sources malloc failed", __func__);
498 return SR_ERR_MALLOC;
501 p = &new_pollfds[num_sources];
502 s = &new_sources[num_sources++];
504 g_io_channel_win32_make_pollfd(&channels[0],
510 s->timeout = timeout;
512 s->cb_data = cb_data;
513 pollfds = new_pollfds;
514 sources = new_sources;
516 if (timeout != source_timeout && timeout > 0
517 && (source_timeout == -1 || timeout < source_timeout))
518 source_timeout = timeout;
524 * Remove the source belonging to the specified file descriptor.
526 * TODO: More error checks.
530 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
531 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
534 SR_API int sr_session_source_remove(int fd)
536 struct source *new_sources;
537 GPollFD *new_pollfds;
541 sr_err("session: %s: sources was NULL", __func__);
545 for (old = 0; old < num_sources; old++) {
546 if (pollfds[old].fd == fd)
550 /* fd not found, nothing to do */
551 if (old == num_sources)
556 if (old != num_sources) {
557 memmove(&pollfds[old], &pollfds[old+1],
558 (num_sources - old) * sizeof(GPollFD));
559 memmove(&sources[old], &sources[old+1],
560 (num_sources - old) * sizeof(struct source));
563 new_pollfds = g_try_realloc(sources, sizeof(GPollFD) * num_sources);
564 if (!new_pollfds && num_sources > 0) {
565 sr_err("session: %s: new_sources malloc failed", __func__);
566 return SR_ERR_MALLOC;
569 new_sources = g_try_realloc(sources, sizeof(struct source) * num_sources);
570 if (!new_sources && num_sources > 0) {
571 sr_err("session: %s: new_sources malloc failed", __func__);
572 return SR_ERR_MALLOC;
575 pollfds = new_pollfds;
576 sources = new_sources;