2 * This file is part of the sigrok project.
4 * Copyright (C) 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->devices);
80 session->devices = NULL;
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_device_clear(void)
103 sr_err("session: %s: session was NULL", __func__);
107 g_slist_free(session->devices);
108 session->devices = NULL;
114 * Add a device to the current session.
116 * @param device The device to add to the current session. Must not be NULL.
117 * Also, device->plugin and device->plugin->opendev must not
120 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
122 SR_API int sr_session_device_add(struct sr_device *device)
127 sr_err("session: %s: device was NULL", __func__);
131 if (!device->plugin) {
132 sr_err("session: %s: device->plugin was NULL", __func__);
136 if (!device->plugin->opendev) {
137 sr_err("session: %s: device->plugin->opendev was NULL",
143 sr_err("session: %s: session was NULL", __func__);
144 return SR_ERR; /* TODO: SR_ERR_BUG? */
147 if ((ret = device->plugin->opendev(device->plugin_index)) != SR_OK) {
148 sr_err("session: %s: opendev failed (%d)", __func__, ret);
152 session->devices = g_slist_append(session->devices, device);
158 * Clear all datafeed callbacks in the current session.
160 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
162 SR_API int sr_session_datafeed_callback_clear(void)
165 sr_err("session: %s: session was NULL", __func__);
169 g_slist_free(session->datafeed_callbacks);
170 session->datafeed_callbacks = NULL;
176 * Add a datafeed callback to the current session.
178 * @param callback Function to call when a chunk of data is received.
180 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
182 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback callback)
185 sr_err("session: %s: session was NULL", __func__);
189 /* TODO: Is 'callback' allowed to be NULL? */
191 session->datafeed_callbacks =
192 g_slist_append(session->datafeed_callbacks, callback);
200 static int sr_session_run_poll(void)
202 GPollFD *fds, my_gpollfd;
206 while (session->running) {
207 /* TODO: Add comment. */
210 /* Construct g_poll()'s array. */
211 if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) {
212 sr_err("session: %s: fds malloc failed", __func__);
213 return SR_ERR_MALLOC;
215 for (i = 0; i < num_sources; i++) {
217 g_io_channel_win32_make_pollfd(&channels[0],
218 sources[i].events, &my_gpollfd);
220 my_gpollfd.fd = sources[i].fd;
221 my_gpollfd.events = sources[i].events;
226 ret = g_poll(fds, num_sources, source_timeout);
228 for (i = 0; i < num_sources; i++) {
229 if (fds[i].revents > 0 || (ret == 0
230 && source_timeout == sources[i].timeout)) {
232 * Invoke the source's callback on an event,
233 * or if the poll timeout out and this source
234 * asked for that timeout.
236 if (!sources[i].cb(fds[i].fd, fds[i].revents,
237 sources[i].user_data))
238 sr_session_source_remove(sources[i].fd);
250 * There can only be one session at a time.
252 * @return SR_OK upon success, SR_ERR upon errors.
254 SR_API int sr_session_start(void)
256 struct sr_device *device;
261 sr_err("session: %s: session was NULL; a session must be "
262 "created first, before starting it.", __func__);
263 return SR_ERR; /* TODO: SR_ERR_BUG? */
266 if (!session->devices) {
267 /* TODO: Actually the case? */
268 sr_err("session: %s: session->devices was NULL; a session "
269 "cannot be started without devices.", __func__);
270 return SR_ERR; /* TODO: SR_ERR_BUG? */
273 /* TODO: Check plugin_index validity? */
275 sr_info("session: starting");
277 for (l = session->devices; l; l = l->next) {
279 /* TODO: Check for device != NULL. */
280 if ((ret = device->plugin->start_acquisition(
281 device->plugin_index, device)) != SR_OK) {
282 sr_err("session: %s: could not start an acquisition "
283 "(%d)", __func__, ret);
288 /* TODO: What if there are multiple devices? Which return code? */
296 * TODO: Various error checks etc.
298 * @return SR_OK upon success, SR_ERR_BUG upon errors.
300 SR_API int sr_session_run(void)
303 sr_err("session: %s: session was NULL; a session must be "
304 "created first, before running it.", __func__);
308 if (!session->devices) {
309 /* TODO: Actually the case? */
310 sr_err("session: %s: session->devices was NULL; a session "
311 "cannot be run without devices.", __func__);
315 sr_info("session: running");
316 session->running = TRUE;
318 /* Do we have real sources? */
319 if (num_sources == 1 && sources[0].fd == -1) {
320 /* Dummy source, freewheel over it. */
321 while (session->running)
322 sources[0].cb(-1, 0, sources[0].user_data);
324 /* Real sources, use g_poll() main loop. */
325 sr_session_run_poll();
332 * Halt the current session.
334 * This requests the current session be stopped as soon as possible, for example
335 * on receiving an SR_DF_END packet.
337 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
339 SR_API int sr_session_halt(void)
342 sr_err("session: %s: session was NULL", __func__);
346 sr_info("session: halting");
347 session->running = FALSE;
353 * Stop the current session.
355 * The current session is stopped immediately, with all acquisition sessions
356 * being stopped and hardware plugins cleaned up.
358 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
360 SR_API int sr_session_stop(void)
362 struct sr_device *device;
366 sr_err("session: %s: session was NULL", __func__);
370 sr_info("session: stopping");
371 session->running = FALSE;
373 for (l = session->devices; l; l = l->next) {
375 /* Check for device != NULL. */
376 if (device->plugin) {
377 if (device->plugin->stop_acquisition)
378 device->plugin->stop_acquisition(device->plugin_index, device);
379 if (device->plugin->cleanup)
380 device->plugin->cleanup();
390 * @param packet The packet to show debugging information for.
393 static void datafeed_dump(struct sr_datafeed_packet *packet)
395 struct sr_datafeed_logic *logic;
397 switch (packet->type) {
399 sr_dbg("bus: received SR_DF_HEADER");
402 sr_dbg("bus: received SR_DF_TRIGGER");
405 logic = packet->payload;
406 /* TODO: Check for logic != NULL. */
407 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
410 sr_dbg("bus: received SR_DF_END");
413 sr_dbg("bus: received unknown packet type %d", packet->type);
420 * Send a packet to whatever is listening on the datafeed bus.
422 * Hardware drivers use this to send a data packet to the frontend.
424 * @param device TODO.
425 * @param packet TODO.
426 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
428 SR_PRIV int sr_session_bus(struct sr_device *device,
429 struct sr_datafeed_packet *packet)
432 sr_datafeed_callback cb;
435 sr_err("session: %s: device was NULL", __func__);
439 if (!device->plugin) {
440 sr_err("session: %s: device->plugin was NULL", __func__);
445 sr_err("session: %s: packet was NULL", __func__);
449 for (l = session->datafeed_callbacks; l; l = l->next) {
450 if (sr_log_loglevel_get() >= SR_LOG_DBG)
451 datafeed_dump(packet);
453 /* TODO: Check for cb != NULL. */
463 * TODO: More error checks etc.
466 * @param events TODO.
467 * @param timeout TODO.
468 * @param callback TODO.
469 * @param user_data TODO.
470 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
471 * SR_ERR_MALLOC upon memory allocation errors.
473 SR_API int sr_session_source_add(int fd, int events, int timeout,
474 sr_receive_data_callback callback, void *user_data)
476 struct source *new_sources, *s;
479 sr_err("session: %s: callback was NULL", __func__);
483 /* Note: user_data can be NULL, that's not a bug. */
485 new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
487 sr_err("session: %s: new_sources malloc failed", __func__);
488 return SR_ERR_MALLOC;
492 memcpy(new_sources, sources,
493 sizeof(struct source) * num_sources);
497 s = &new_sources[num_sources++];
500 s->timeout = timeout;
502 s->user_data = user_data;
503 sources = new_sources;
505 if (timeout != source_timeout && timeout > 0
506 && (source_timeout == -1 || timeout < source_timeout))
507 source_timeout = timeout;
513 * Remove the source belonging to the specified file descriptor.
515 * 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. */