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/>.
26 #include "sigrok-internal.h"
28 /* demo.c. TODO: Should not be global! */
29 extern SR_PRIV GIOChannel channels[2];
35 sr_receive_data_callback_t cb;
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 static struct source *sources = NULL;
45 static int source_timeout = -1;
48 * Create a new session.
50 * TODO: Should it use the file-global "session" variable or take an argument?
51 * The same question applies to all the other session functions.
53 * @return A pointer to the newly allocated session, or NULL upon errors.
55 SR_API struct sr_session *sr_session_new(void)
57 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
58 sr_err("session: %s: session malloc failed", __func__);
59 return NULL; /* TODO: SR_ERR_MALLOC? */
66 * Destroy the current session.
68 * This frees up all memory used by the session.
70 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
72 SR_API int sr_session_destroy(void)
75 sr_err("session: %s: session was NULL", __func__);
79 g_slist_free(session->devs);
82 /* TODO: Error checks needed? */
84 /* TODO: Loop over protocol decoders and free them. */
93 * Remove all the devices from the current session. TODO?
95 * The session itself (i.e., the struct sr_session) is not free'd and still
96 * exists after this function returns.
98 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
100 SR_API int sr_session_dev_remove_all(void)
103 sr_err("session: %s: session was NULL", __func__);
107 g_slist_free(session->devs);
108 session->devs = NULL;
114 * Add a device to the current session.
116 * @param dev The device to add to the current session. Must not be NULL.
117 * Also, dev->driver and dev->driver->dev_open must not be NULL.
119 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
121 SR_API int sr_session_dev_add(struct sr_dev *dev)
126 sr_err("session: %s: dev was NULL", __func__);
131 sr_err("session: %s: session was NULL", __func__);
135 /* If dev->driver is NULL, this is a virtual device. */
137 sr_dbg("session: %s: dev->driver was NULL, this seems to be "
138 "a virtual device; continuing", __func__);
139 /* Just add the device, don't run dev_open(). */
140 session->devs = g_slist_append(session->devs, dev);
144 /* dev->driver is non-NULL (i.e. we have a real device). */
145 if (!dev->driver->dev_open) {
146 sr_err("session: %s: dev->driver->dev_open was NULL",
151 if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) {
152 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
156 session->devs = g_slist_append(session->devs, dev);
162 * Remove all datafeed callbacks in the current session.
164 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
166 SR_API int sr_session_datafeed_callback_remove_all(void)
169 sr_err("session: %s: session was NULL", __func__);
173 g_slist_free(session->datafeed_callbacks);
174 session->datafeed_callbacks = NULL;
180 * Add a datafeed callback to the current session.
182 * @param cb Function to call when a chunk of data is received.
185 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
187 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
190 sr_err("session: %s: session was NULL", __func__);
195 sr_err("session: %s: cb was NULL", __func__);
199 session->datafeed_callbacks =
200 g_slist_append(session->datafeed_callbacks, cb);
208 static int sr_session_run_poll(void)
210 GPollFD *fds, my_gpollfd;
214 while (session->running) {
215 /* TODO: Add comment. */
218 /* Construct g_poll()'s array. */
219 if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) {
220 sr_err("session: %s: fds malloc failed", __func__);
221 return SR_ERR_MALLOC;
223 for (i = 0; i < num_sources; i++) {
225 g_io_channel_win32_make_pollfd(&channels[0],
226 sources[i].events, &my_gpollfd);
228 my_gpollfd.fd = sources[i].fd;
229 my_gpollfd.events = sources[i].events;
234 ret = g_poll(fds, num_sources, source_timeout);
236 for (i = 0; i < num_sources; i++) {
237 if (fds[i].revents > 0 || (ret == 0
238 && source_timeout == sources[i].timeout)) {
240 * Invoke the source's callback on an event,
241 * or if the poll timeout out and this source
242 * asked for that timeout.
244 if (!sources[i].cb(fds[i].fd, fds[i].revents,
246 sr_session_source_remove(sources[i].fd);
258 * There can only be one session at a time.
260 * @return SR_OK upon success, SR_ERR upon errors.
262 SR_API int sr_session_start(void)
269 sr_err("session: %s: session was NULL; a session must be "
270 "created first, before starting it.", __func__);
274 if (!session->devs) {
275 /* TODO: Actually the case? */
276 sr_err("session: %s: session->devs was NULL; a session "
277 "cannot be started without devices.", __func__);
281 /* TODO: Check driver_index validity? */
283 sr_info("session: starting");
285 for (l = session->devs; l; l = l->next) {
287 /* TODO: Check for dev != NULL. */
288 if ((ret = dev->driver->dev_acquisition_start(
289 dev->driver_index, dev)) != SR_OK) {
290 sr_err("session: %s: could not start an acquisition "
291 "(%d)", __func__, ret);
296 /* TODO: What if there are multiple devices? Which return code? */
304 * TODO: Various error checks etc.
306 * @return SR_OK upon success, SR_ERR_BUG upon errors.
308 SR_API int sr_session_run(void)
311 sr_err("session: %s: session was NULL; a session must be "
312 "created first, before running it.", __func__);
316 if (!session->devs) {
317 /* TODO: Actually the case? */
318 sr_err("session: %s: session->devs was NULL; a session "
319 "cannot be run without devices.", __func__);
323 sr_info("session: running");
324 session->running = TRUE;
326 /* Do we have real sources? */
327 if (num_sources == 1 && sources[0].fd == -1) {
328 /* Dummy source, freewheel over it. */
329 while (session->running)
330 sources[0].cb(-1, 0, sources[0].cb_data);
332 /* Real sources, use g_poll() main loop. */
333 sr_session_run_poll();
340 * Halt the current session.
342 * This requests the current session be stopped as soon as possible, for
343 * example on receiving an SR_DF_END packet.
345 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
347 SR_API int sr_session_halt(void)
350 sr_err("session: %s: session was NULL", __func__);
354 sr_info("session: halting");
355 session->running = FALSE;
361 * Stop the current session.
363 * The current session is stopped immediately, with all acquisition sessions
364 * being stopped and hardware drivers cleaned up.
366 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
368 SR_API int sr_session_stop(void)
374 sr_err("session: %s: session was NULL", __func__);
378 sr_info("session: stopping");
379 session->running = FALSE;
381 for (l = session->devs; l; l = l->next) {
383 /* Check for dev != NULL. */
385 if (dev->driver->dev_acquisition_stop)
386 dev->driver->dev_acquisition_stop(dev->driver_index, dev);
387 if (dev->driver->cleanup)
388 dev->driver->cleanup();
398 * @param packet The packet to show debugging information for.
400 static void datafeed_dump(struct sr_datafeed_packet *packet)
402 struct sr_datafeed_logic *logic;
403 struct sr_datafeed_analog *analog;
405 switch (packet->type) {
407 sr_dbg("bus: received SR_DF_HEADER");
410 sr_dbg("bus: received SR_DF_TRIGGER");
412 case SR_DF_META_LOGIC:
413 sr_dbg("bus: received SR_DF_META_LOGIC");
416 logic = packet->payload;
417 /* TODO: Check for logic != NULL. */
418 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
420 case SR_DF_META_ANALOG:
421 sr_dbg("bus: received SR_DF_META_LOGIC");
424 analog = packet->payload;
425 /* TODO: Check for analog != NULL. */
426 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
429 sr_dbg("bus: received SR_DF_END");
431 case SR_DF_FRAME_BEGIN:
432 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
434 case SR_DF_FRAME_END:
435 sr_dbg("bus: received SR_DF_FRAME_END");
438 sr_dbg("bus: received unknown packet type %d", packet->type);
444 * Send a packet to whatever is listening on the datafeed bus.
446 * Hardware drivers use this to send a data packet to the frontend.
449 * @param packet The datafeed packet to send to the session bus.
451 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
453 SR_PRIV int sr_session_send(struct sr_dev *dev,
454 struct sr_datafeed_packet *packet)
457 sr_datafeed_callback_t cb;
460 sr_err("session: %s: dev was NULL", __func__);
465 sr_err("session: %s: packet was NULL", __func__);
469 for (l = session->datafeed_callbacks; l; l = l->next) {
470 if (sr_log_loglevel_get() >= SR_LOG_DBG)
471 datafeed_dump(packet);
473 /* TODO: Check for cb != NULL. */
483 * TODO: More error checks etc.
486 * @param events TODO.
487 * @param timeout TODO.
488 * @param cb Callback function to add. Must not be NULL.
489 * @param cb_data Data for the callback function. Can be NULL.
491 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
492 * SR_ERR_MALLOC upon memory allocation errors.
494 SR_API int sr_session_source_add(int fd, int events, int timeout,
495 sr_receive_data_callback_t cb, void *cb_data)
497 struct source *new_sources, *s;
500 sr_err("session: %s: cb was NULL", __func__);
504 /* Note: cb_data can be NULL, that's not a bug. */
506 new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
508 sr_err("session: %s: new_sources malloc failed", __func__);
509 return SR_ERR_MALLOC;
513 memcpy(new_sources, sources,
514 sizeof(struct source) * num_sources);
518 s = &new_sources[num_sources++];
521 s->timeout = timeout;
523 s->cb_data = cb_data;
524 sources = new_sources;
526 if (timeout != source_timeout && timeout > 0
527 && (source_timeout == -1 || timeout < source_timeout))
528 source_timeout = timeout;
534 * Remove the source belonging to the specified file descriptor.
536 * TODO: More error checks.
540 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
541 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
544 SR_API int sr_session_source_remove(int fd)
546 struct source *new_sources;
550 sr_err("session: %s: sources was NULL", __func__);
554 /* TODO: Check if 'fd' valid. */
556 new_sources = g_try_malloc0(sizeof(struct source) * num_sources);
558 sr_err("session: %s: new_sources malloc failed", __func__);
559 return SR_ERR_MALLOC;
562 for (old = 0, new = 0; old < num_sources; old++) {
563 if (sources[old].fd != fd)
564 memcpy(&new_sources[new++], &sources[old],
565 sizeof(struct source));
570 sources = new_sources;
573 /* Target fd was not found. */