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.
52 * TODO: Should return int?
53 * TODO: Should it use the file-global "session" variable or take an argument?
54 * The same question applies to all the other session functions.
56 * @return A pointer to the newly allocated session, or NULL upon errors.
58 SR_API struct sr_session *sr_session_new(void)
60 if (!(session = calloc(1, sizeof(struct sr_session)))) {
61 sr_err("session: %s: session malloc failed", __func__);
62 return NULL; /* TODO: SR_ERR_MALLOC? */
69 * Destroy the current session.
71 * This frees up all memory used by the session.
73 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
75 SR_API int sr_session_destroy(void)
78 sr_warn("session: %s: session was NULL", __func__);
82 g_slist_free(session->devices);
83 session->devices = NULL;
85 /* TODO: Error checks needed? */
87 /* TODO: Loop over protocol decoders and free them. */
96 * Remove all the devices from the current session. TODO?
98 * The session itself (i.e., the struct sr_session) is not free'd and still
99 * exists after this function returns.
101 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
103 SR_API int sr_session_device_clear(void)
106 sr_warn("session: %s: session was NULL", __func__);
110 g_slist_free(session->devices);
111 session->devices = NULL;
117 * Add a device to the current session.
119 * @param device The device to add to the current session. Must not be NULL.
120 * Also, device->plugin and device->plugin->opendev must not
123 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
125 SR_API int sr_session_device_add(struct sr_device *device)
130 sr_err("session: %s: device was NULL", __func__);
134 if (!device->plugin) {
135 sr_err("session: %s: device->plugin was NULL", __func__);
139 if (!device->plugin->opendev) {
140 sr_err("session: %s: device->plugin->opendev was NULL",
146 sr_err("session: %s: session was NULL", __func__);
147 return SR_ERR; /* TODO: SR_ERR_BUG? */
150 if ((ret = device->plugin->opendev(device->plugin_index)) != SR_OK) {
151 sr_err("session: %s: opendev failed (%d)", __func__, ret);
155 session->devices = g_slist_append(session->devices, device);
161 * Clear all datafeed callbacks in the current session.
165 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
167 SR_API int sr_session_datafeed_callback_clear(void)
170 sr_err("session: %s: session was NULL", __func__);
174 g_slist_free(session->datafeed_callbacks);
175 session->datafeed_callbacks = NULL;
181 * Add a datafeed callback to the current session.
183 * @param callback TODO.
184 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
186 SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback callback)
189 sr_err("session: %s: session was NULL", __func__);
193 /* TODO: Is 'callback' allowed to be NULL? */
195 session->datafeed_callbacks =
196 g_slist_append(session->datafeed_callbacks, callback);
204 static int sr_session_run_poll(void)
206 GPollFD *fds, my_gpollfd;
210 while (session->running) {
214 /* Construct g_poll()'s array. */
215 /* TODO: Check malloc return value. */
216 fds = malloc(sizeof(GPollFD) * num_sources);
218 sr_err("session: %s: fds malloc failed", __func__);
219 return SR_ERR_MALLOC;
221 for (i = 0; i < num_sources; i++) {
223 g_io_channel_win32_make_pollfd(&channels[0],
224 sources[i].events, &my_gpollfd);
226 my_gpollfd.fd = sources[i].fd;
227 my_gpollfd.events = sources[i].events;
232 ret = g_poll(fds, num_sources, source_timeout);
234 for (i = 0; i < num_sources; i++) {
235 if (fds[i].revents > 0 || (ret == 0
236 && source_timeout == sources[i].timeout)) {
238 * Invoke the source's callback on an event,
239 * or if the poll timeout out and this source
240 * asked for that timeout.
242 if (!sources[i].cb(fds[i].fd, fds[i].revents,
243 sources[i].user_data))
244 sr_session_source_remove(sources[i].fd);
256 * There can only be one session at a time. TODO
258 * @return SR_OK upon success, SR_ERR upon errors.
260 SR_API int sr_session_start(void)
262 struct sr_device *device;
267 sr_err("session: %s: session was NULL; a session must be "
268 "created first, before starting it.", __func__);
269 return SR_ERR; /* TODO: SR_ERR_BUG? */
272 if (!session->devices) {
273 /* TODO: Actually the case? */
274 sr_err("session: %s: session->devices was NULL; a session "
275 "cannot be started without devices.", __func__);
276 return SR_ERR; /* TODO: SR_ERR_BUG? */
279 /* TODO: Check plugin_index validity? */
281 sr_info("session: starting");
283 for (l = session->devices; l; l = l->next) {
285 /* TODO: Check for device != NULL. */
286 if ((ret = device->plugin->start_acquisition(
287 device->plugin_index, device)) != SR_OK) {
288 sr_err("session: %s: could not start an acquisition "
289 "(%d)", __func__, ret);
294 /* TODO: What if there are multiple devices? Which return code? */
302 * TODO: Various error checks etc.
304 * @return SR_OK upon success, SR_ERR_BUG upon errors.
306 SR_API int sr_session_run(void)
309 sr_err("session: %s: session was NULL; a session must be "
310 "created first, before running it.", __func__);
314 if (!session->devices) {
315 /* TODO: Actually the case? */
316 sr_err("session: %s: session->devices was NULL; a session "
317 "cannot be run without devices.", __func__);
321 sr_info("session: running");
322 session->running = TRUE;
324 /* Do we have real sources? */
325 if (num_sources == 1 && sources[0].fd == -1) {
326 /* Dummy source, freewheel over it. */
327 while (session->running)
328 sources[0].cb(-1, 0, sources[0].user_data);
330 /* Real sources, use g_poll() main loop. */
331 sr_session_run_poll();
338 * Halt the current session.
342 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
344 SR_API int sr_session_halt(void)
347 sr_err("session: %s: session was NULL", __func__);
351 sr_info("session: halting");
352 session->running = FALSE;
358 * Stop the current session.
360 * TODO: Difference to halt?
362 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
364 SR_API int sr_session_stop(void)
366 struct sr_device *device;
370 sr_err("session: %s: session was NULL", __func__);
374 sr_info("session: stopping");
375 session->running = FALSE;
377 for (l = session->devices; l; l = l->next) {
379 /* Check for device != NULL. */
380 if (device->plugin) {
381 if (device->plugin->stop_acquisition)
382 device->plugin->stop_acquisition(device->plugin_index, device);
383 if (device->plugin->cleanup)
384 device->plugin->cleanup();
394 * TODO: Various error checks.
396 * @param packet TODO.
397 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
399 static int datafeed_dump(struct sr_datafeed_packet *packet)
401 struct sr_datafeed_logic *logic;
404 sr_err("session: %s: packet was NULL", __func__);
408 /* TODO: Validity checks for packet contents. */
410 switch (packet->type) {
412 sr_dbg("bus: received SR_DF_HEADER");
415 sr_dbg("bus: received SR_DF_TRIGGER");
418 logic = packet->payload;
419 /* TODO: Check for logic != NULL. */
420 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
423 sr_dbg("bus: received SR_DF_END");
427 sr_err("bus: received unknown packet type %d", packet->type);
437 * @param device TODO.
438 * @param packet TODO.
439 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
441 SR_API int sr_session_bus(struct sr_device *device,
442 struct sr_datafeed_packet *packet)
445 sr_datafeed_callback cb;
448 sr_err("session: %s: device was NULL", __func__);
452 if (!device->plugin) {
453 sr_err("session: %s: device->plugin was NULL", __func__);
458 sr_err("session: %s: packet was NULL", __func__);
463 * TODO: Send packet through PD pipe, and send the output of that to
464 * the callbacks as well.
466 for (l = session->datafeed_callbacks; l; l = l->next) {
468 /* TODO: Check for cb != NULL. */
469 datafeed_dump(packet);
479 * TODO: Switch to g_try_malloc0() / g_free().
480 * TODO: More error checks etc.
483 * @param events TODO.
484 * @param timeout TODO.
485 * @param callback TODO.
486 * @param user_data TODO.
487 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
488 * SR_ERR_MALLOC upon memory allocation errors.
490 SR_API int sr_session_source_add(int fd, int events, int timeout,
491 sr_receive_data_callback callback, void *user_data)
493 struct source *new_sources, *s;
496 sr_err("session: %s: callback was NULL", __func__);
500 /* Note: user_data can be NULL, that's not a bug. */
502 new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
504 sr_err("session: %s: new_sources malloc failed", __func__);
505 return SR_ERR_MALLOC;
509 memcpy(new_sources, sources,
510 sizeof(struct source) * num_sources);
514 s = &new_sources[num_sources++];
517 s->timeout = timeout;
519 s->user_data = user_data;
520 sources = new_sources;
522 if (timeout != source_timeout && timeout > 0
523 && (source_timeout == -1 || timeout < source_timeout))
524 source_timeout = timeout;
530 * Remove the source belonging to the specified file descriptor.
532 * TODO: More error checks.
533 * TODO: Switch to g_try_malloc0() / g_free().
536 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
537 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
540 SR_API int sr_session_source_remove(int fd)
542 struct source *new_sources;
546 sr_err("session: %s: sources was NULL", __func__);
547 return SR_ERR_BUG; /* TODO: Other? */
550 /* TODO: Check if 'fd' valid. */
552 new_sources = calloc(1, sizeof(struct source) * num_sources);
554 sr_err("session: %s: new_sources malloc failed", __func__);
555 return SR_ERR_MALLOC;
558 for (old = 0, new = 0; old < num_sources; old++) {
559 if (sources[old].fd != fd)
560 memcpy(&new_sources[new++], &sources[old],
561 sizeof(struct source));
566 sources = new_sources;
569 /* Target fd was not found. */