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 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_clear(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->plugin and dev->plugin->opendev 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: dev->plugin was NULL", __func__);
135 if (!dev->plugin->opendev) {
136 sr_err("session: %s: dev->plugin->opendev was NULL",
142 sr_err("session: %s: session was NULL", __func__);
143 return SR_ERR; /* TODO: SR_ERR_BUG? */
146 if ((ret = dev->plugin->opendev(dev->plugin_index)) != SR_OK) {
147 sr_err("session: %s: opendev failed (%d)", __func__, ret);
151 session->devs = g_slist_append(session->devs, dev);
157 * Clear all datafeed callbacks in the current session.
159 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
161 SR_API int sr_session_datafeed_callback_clear(void)
164 sr_err("session: %s: session was NULL", __func__);
168 g_slist_free(session->datafeed_callbacks);
169 session->datafeed_callbacks = NULL;
175 * Add a datafeed callback to the current session.
177 * @param callback Function to call when a chunk of data is received.
179 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
181 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback callback)
184 sr_err("session: %s: session was NULL", __func__);
188 /* TODO: Is 'callback' allowed to be NULL? */
190 session->datafeed_callbacks =
191 g_slist_append(session->datafeed_callbacks, callback);
199 static int sr_session_run_poll(void)
201 GPollFD *fds, my_gpollfd;
205 while (session->running) {
206 /* TODO: Add comment. */
209 /* Construct g_poll()'s array. */
210 if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) {
211 sr_err("session: %s: fds malloc failed", __func__);
212 return SR_ERR_MALLOC;
214 for (i = 0; i < num_sources; i++) {
216 g_io_channel_win32_make_pollfd(&channels[0],
217 sources[i].events, &my_gpollfd);
219 my_gpollfd.fd = sources[i].fd;
220 my_gpollfd.events = sources[i].events;
225 ret = g_poll(fds, num_sources, source_timeout);
227 for (i = 0; i < num_sources; i++) {
228 if (fds[i].revents > 0 || (ret == 0
229 && source_timeout == sources[i].timeout)) {
231 * Invoke the source's callback on an event,
232 * or if the poll timeout out and this source
233 * asked for that timeout.
235 if (!sources[i].cb(fds[i].fd, fds[i].revents,
236 sources[i].user_data))
237 sr_session_source_remove(sources[i].fd);
249 * There can only be one session at a time.
251 * @return SR_OK upon success, SR_ERR upon errors.
253 SR_API int sr_session_start(void)
260 sr_err("session: %s: session was NULL; a session must be "
261 "created first, before starting it.", __func__);
262 return SR_ERR; /* TODO: SR_ERR_BUG? */
265 if (!session->devs) {
266 /* TODO: Actually the case? */
267 sr_err("session: %s: session->devs was NULL; a session "
268 "cannot be started without devices.", __func__);
269 return SR_ERR; /* TODO: SR_ERR_BUG? */
272 /* TODO: Check plugin_index validity? */
274 sr_info("session: starting");
276 for (l = session->devs; l; l = l->next) {
278 /* TODO: Check for dev != NULL. */
279 if ((ret = dev->plugin->acquisition_start(
280 dev->plugin_index, dev)) != SR_OK) {
281 sr_err("session: %s: could not start an acquisition "
282 "(%d)", __func__, ret);
287 /* TODO: What if there are multiple devices? Which return code? */
295 * TODO: Various error checks etc.
297 * @return SR_OK upon success, SR_ERR_BUG upon errors.
299 SR_API int sr_session_run(void)
302 sr_err("session: %s: session was NULL; a session must be "
303 "created first, before running it.", __func__);
307 if (!session->devs) {
308 /* TODO: Actually the case? */
309 sr_err("session: %s: session->devs was NULL; a session "
310 "cannot be run without devices.", __func__);
314 sr_info("session: running");
315 session->running = TRUE;
317 /* Do we have real sources? */
318 if (num_sources == 1 && sources[0].fd == -1) {
319 /* Dummy source, freewheel over it. */
320 while (session->running)
321 sources[0].cb(-1, 0, sources[0].user_data);
323 /* Real sources, use g_poll() main loop. */
324 sr_session_run_poll();
331 * Halt the current session.
333 * This requests the current session be stopped as soon as possible, for
334 * example on receiving an SR_DF_END packet.
336 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
338 SR_API int sr_session_halt(void)
341 sr_err("session: %s: session was NULL", __func__);
345 sr_info("session: halting");
346 session->running = FALSE;
352 * Stop the current session.
354 * The current session is stopped immediately, with all acquisition sessions
355 * being stopped and hardware plugins cleaned up.
357 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
359 SR_API int sr_session_stop(void)
365 sr_err("session: %s: session was NULL", __func__);
369 sr_info("session: stopping");
370 session->running = FALSE;
372 for (l = session->devs; l; l = l->next) {
374 /* Check for dev != NULL. */
376 if (dev->plugin->acquisition_stop)
377 dev->plugin->acquisition_stop(dev->plugin_index, dev);
378 if (dev->plugin->cleanup)
379 dev->plugin->cleanup();
389 * @param packet The packet to show debugging information for.
391 static void datafeed_dump(struct sr_datafeed_packet *packet)
393 struct sr_datafeed_logic *logic;
395 switch (packet->type) {
397 sr_dbg("bus: received SR_DF_HEADER");
400 sr_dbg("bus: received SR_DF_TRIGGER");
403 logic = packet->payload;
404 /* TODO: Check for logic != NULL. */
405 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
408 sr_dbg("bus: received SR_DF_END");
411 sr_dbg("bus: received unknown packet type %d", packet->type);
417 * Send a packet to whatever is listening on the datafeed bus.
419 * Hardware drivers use this to send a data packet to the frontend.
422 * @param packet TODO.
424 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
426 SR_PRIV int sr_session_bus(struct sr_dev *dev,
427 struct sr_datafeed_packet *packet)
430 sr_datafeed_callback cb;
433 sr_err("session: %s: dev was NULL", __func__);
438 sr_err("session: %s: dev->plugin was NULL", __func__);
443 sr_err("session: %s: packet was NULL", __func__);
447 for (l = session->datafeed_callbacks; l; l = l->next) {
448 if (sr_log_loglevel_get() >= SR_LOG_DBG)
449 datafeed_dump(packet);
451 /* TODO: Check for cb != NULL. */
461 * TODO: More error checks etc.
464 * @param events TODO.
465 * @param timeout TODO.
466 * @param callback TODO.
467 * @param user_data TODO.
469 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
470 * SR_ERR_MALLOC upon memory allocation errors.
472 SR_API int sr_session_source_add(int fd, int events, int timeout,
473 sr_receive_data_callback callback, void *user_data)
475 struct source *new_sources, *s;
478 sr_err("session: %s: callback was NULL", __func__);
482 /* Note: user_data can be NULL, that's not a bug. */
484 new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
486 sr_err("session: %s: new_sources malloc failed", __func__);
487 return SR_ERR_MALLOC;
491 memcpy(new_sources, sources,
492 sizeof(struct source) * num_sources);
496 s = &new_sources[num_sources++];
499 s->timeout = timeout;
501 s->user_data = user_data;
502 sources = new_sources;
504 if (timeout != source_timeout && timeout > 0
505 && (source_timeout == -1 || timeout < source_timeout))
506 source_timeout = timeout;
512 * Remove the source belonging to the specified file descriptor.
514 * TODO: More error checks.
518 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
519 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
522 SR_API int sr_session_source_remove(int fd)
524 struct source *new_sources;
528 sr_err("session: %s: sources was NULL", __func__);
529 return SR_ERR_BUG; /* TODO: Other? */
532 /* TODO: Check if 'fd' valid. */
534 new_sources = g_try_malloc0(sizeof(struct source) * num_sources);
536 sr_err("session: %s: new_sources malloc failed", __func__);
537 return SR_ERR_MALLOC;
540 for (old = 0, new = 0; old < num_sources; old++) {
541 if (sources[old].fd != fd)
542 memcpy(&new_sources[new++], &sources[old],
543 sizeof(struct source));
548 sources = new_sources;
551 /* Target fd was not found. */