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 /* Not enough memory, or num_sources was 0. */
221 sr_err("session: %s: fds malloc failed "
222 "(num_sources was %d).", __func__, num_sources);
223 return SR_ERR_MALLOC;
225 for (i = 0; i < num_sources; i++) {
227 g_io_channel_win32_make_pollfd(&channels[0],
228 sources[i].events, &my_gpollfd);
230 my_gpollfd.fd = sources[i].fd;
231 my_gpollfd.events = sources[i].events;
236 ret = g_poll(fds, num_sources, source_timeout);
238 for (i = 0; i < num_sources; i++) {
239 if (fds[i].revents > 0 || (ret == 0
240 && source_timeout == sources[i].timeout)) {
242 * Invoke the source's callback on an event,
243 * or if the poll timeout out and this source
244 * asked for that timeout.
246 if (!sources[i].cb(fds[i].fd, fds[i].revents,
248 sr_session_source_remove(sources[i].fd);
260 * There can only be one session at a time.
262 * @return SR_OK upon success, SR_ERR upon errors.
264 SR_API int sr_session_start(void)
271 sr_err("session: %s: session was NULL; a session must be "
272 "created first, before starting it.", __func__);
276 if (!session->devs) {
277 /* TODO: Actually the case? */
278 sr_err("session: %s: session->devs was NULL; a session "
279 "cannot be started without devices.", __func__);
283 /* TODO: Check driver_index validity? */
285 sr_info("session: starting");
287 for (l = session->devs; l; l = l->next) {
289 /* TODO: Check for dev != NULL. */
290 if ((ret = dev->driver->dev_acquisition_start(
291 dev->driver_index, dev)) != SR_OK) {
292 sr_err("session: %s: could not start an acquisition "
293 "(%d)", __func__, ret);
298 /* TODO: What if there are multiple devices? Which return code? */
306 * TODO: Various error checks etc.
308 * @return SR_OK upon success, SR_ERR_BUG upon errors.
310 SR_API int sr_session_run(void)
313 sr_err("session: %s: session was NULL; a session must be "
314 "created first, before running it.", __func__);
318 if (!session->devs) {
319 /* TODO: Actually the case? */
320 sr_err("session: %s: session->devs was NULL; a session "
321 "cannot be run without devices.", __func__);
325 sr_info("session: running");
326 session->running = TRUE;
328 /* Do we have real sources? */
329 if (num_sources == 1 && sources[0].fd == -1) {
330 /* Dummy source, freewheel over it. */
331 while (session->running)
332 sources[0].cb(-1, 0, sources[0].cb_data);
334 /* Real sources, use g_poll() main loop. */
335 sr_session_run_poll();
342 * Halt the current session.
344 * This requests the current session be stopped as soon as possible, for
345 * example on receiving an SR_DF_END packet.
347 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
349 SR_API int sr_session_halt(void)
352 sr_err("session: %s: session was NULL", __func__);
356 sr_info("session: halting");
357 session->running = FALSE;
363 * Stop the current session.
365 * The current session is stopped immediately, with all acquisition sessions
366 * being stopped and hardware drivers cleaned up.
368 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
370 SR_API int sr_session_stop(void)
376 sr_err("session: %s: session was NULL", __func__);
380 sr_info("session: stopping");
381 session->running = FALSE;
383 for (l = session->devs; l; l = l->next) {
385 /* Check for dev != NULL. */
387 if (dev->driver->dev_acquisition_stop)
388 dev->driver->dev_acquisition_stop(dev->driver_index, dev);
389 if (dev->driver->cleanup)
390 dev->driver->cleanup();
400 * @param packet The packet to show debugging information for.
402 static void datafeed_dump(struct sr_datafeed_packet *packet)
404 struct sr_datafeed_logic *logic;
405 struct sr_datafeed_analog *analog;
407 switch (packet->type) {
409 sr_dbg("bus: received SR_DF_HEADER");
412 sr_dbg("bus: received SR_DF_TRIGGER");
414 case SR_DF_META_LOGIC:
415 sr_dbg("bus: received SR_DF_META_LOGIC");
418 logic = packet->payload;
419 /* TODO: Check for logic != NULL. */
420 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
422 case SR_DF_META_ANALOG:
423 sr_dbg("bus: received SR_DF_META_LOGIC");
426 analog = packet->payload;
427 /* TODO: Check for analog != NULL. */
428 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
431 sr_dbg("bus: received SR_DF_END");
433 case SR_DF_FRAME_BEGIN:
434 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
436 case SR_DF_FRAME_END:
437 sr_dbg("bus: received SR_DF_FRAME_END");
440 sr_dbg("bus: received unknown packet type %d", packet->type);
446 * Send a packet to whatever is listening on the datafeed bus.
448 * Hardware drivers use this to send a data packet to the frontend.
451 * @param packet The datafeed packet to send to the session bus.
453 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
455 SR_PRIV int sr_session_send(struct sr_dev *dev,
456 struct sr_datafeed_packet *packet)
459 sr_datafeed_callback_t cb;
462 sr_err("session: %s: dev was NULL", __func__);
467 sr_err("session: %s: packet was NULL", __func__);
471 for (l = session->datafeed_callbacks; l; l = l->next) {
472 if (sr_log_loglevel_get() >= SR_LOG_DBG)
473 datafeed_dump(packet);
475 /* TODO: Check for cb != NULL. */
485 * TODO: More error checks etc.
488 * @param events TODO.
489 * @param timeout TODO.
490 * @param cb Callback function to add. Must not be NULL.
491 * @param cb_data Data for the callback function. Can be NULL.
493 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
494 * SR_ERR_MALLOC upon memory allocation errors.
496 SR_API int sr_session_source_add(int fd, int events, int timeout,
497 sr_receive_data_callback_t cb, void *cb_data)
499 struct source *new_sources, *s;
502 sr_err("session: %s: cb was NULL", __func__);
506 /* Note: cb_data can be NULL, that's not a bug. */
508 new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
510 sr_err("session: %s: new_sources malloc failed", __func__);
511 return SR_ERR_MALLOC;
515 memcpy(new_sources, sources,
516 sizeof(struct source) * num_sources);
520 s = &new_sources[num_sources++];
523 s->timeout = timeout;
525 s->cb_data = cb_data;
526 sources = new_sources;
528 if (timeout != source_timeout && timeout > 0
529 && (source_timeout == -1 || timeout < source_timeout))
530 source_timeout = timeout;
536 * Remove the source belonging to the specified file descriptor.
538 * TODO: More error checks.
542 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
543 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
546 SR_API int sr_session_source_remove(int fd)
548 struct source *new_sources;
552 sr_err("session: %s: sources was NULL", __func__);
556 /* TODO: Check if 'fd' valid. */
558 new_sources = g_try_malloc0(sizeof(struct source) * num_sources);
560 sr_err("session: %s: new_sources malloc failed", __func__);
561 return SR_ERR_MALLOC;
564 for (old = 0, new = 0; old < num_sources; old++) {
565 if (sources[old].fd != fd)
566 memcpy(&new_sources[new++], &sources[old],
567 sizeof(struct source));
572 sources = new_sources;
575 /* Target fd was not found. */