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_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->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: dev->driver was NULL", __func__);
135 if (!dev->driver->dev_open) {
136 sr_err("session: %s: dev->driver->dev_open was NULL",
142 sr_err("session: %s: session was NULL", __func__);
146 if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) {
147 sr_err("session: %s: dev_open 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 cb 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_t cb)
185 sr_err("session: %s: session was NULL", __func__);
190 sr_err("session: %s: cb was NULL", __func__);
194 session->datafeed_callbacks =
195 g_slist_append(session->datafeed_callbacks, cb);
203 static int sr_session_run_poll(void)
205 GPollFD *fds, my_gpollfd;
209 while (session->running) {
210 /* TODO: Add comment. */
213 /* Construct g_poll()'s array. */
214 if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) {
215 sr_err("session: %s: fds malloc failed", __func__);
216 return SR_ERR_MALLOC;
218 for (i = 0; i < num_sources; i++) {
220 g_io_channel_win32_make_pollfd(&channels[0],
221 sources[i].events, &my_gpollfd);
223 my_gpollfd.fd = sources[i].fd;
224 my_gpollfd.events = sources[i].events;
229 ret = g_poll(fds, num_sources, source_timeout);
231 for (i = 0; i < num_sources; i++) {
232 if (fds[i].revents > 0 || (ret == 0
233 && source_timeout == sources[i].timeout)) {
235 * Invoke the source's callback on an event,
236 * or if the poll timeout out and this source
237 * asked for that timeout.
239 if (!sources[i].cb(fds[i].fd, fds[i].revents,
241 sr_session_source_remove(sources[i].fd);
253 * There can only be one session at a time.
255 * @return SR_OK upon success, SR_ERR upon errors.
257 SR_API int sr_session_start(void)
264 sr_err("session: %s: session was NULL; a session must be "
265 "created first, before starting it.", __func__);
269 if (!session->devs) {
270 /* TODO: Actually the case? */
271 sr_err("session: %s: session->devs was NULL; a session "
272 "cannot be started without devices.", __func__);
276 /* TODO: Check driver_index validity? */
278 sr_info("session: starting");
280 for (l = session->devs; l; l = l->next) {
282 /* TODO: Check for dev != NULL. */
283 if ((ret = dev->driver->dev_acquisition_start(
284 dev->driver_index, dev)) != SR_OK) {
285 sr_err("session: %s: could not start an acquisition "
286 "(%d)", __func__, ret);
291 /* TODO: What if there are multiple devices? Which return code? */
299 * TODO: Various error checks etc.
301 * @return SR_OK upon success, SR_ERR_BUG upon errors.
303 SR_API int sr_session_run(void)
306 sr_err("session: %s: session was NULL; a session must be "
307 "created first, before running it.", __func__);
311 if (!session->devs) {
312 /* TODO: Actually the case? */
313 sr_err("session: %s: session->devs was NULL; a session "
314 "cannot be run without devices.", __func__);
318 sr_info("session: running");
319 session->running = TRUE;
321 /* Do we have real sources? */
322 if (num_sources == 1 && sources[0].fd == -1) {
323 /* Dummy source, freewheel over it. */
324 while (session->running)
325 sources[0].cb(-1, 0, sources[0].cb_data);
327 /* Real sources, use g_poll() main loop. */
328 sr_session_run_poll();
335 * Halt the current session.
337 * This requests the current session be stopped as soon as possible, for
338 * example on receiving an SR_DF_END packet.
340 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
342 SR_API int sr_session_halt(void)
345 sr_err("session: %s: session was NULL", __func__);
349 sr_info("session: halting");
350 session->running = FALSE;
356 * Stop the current session.
358 * The current session is stopped immediately, with all acquisition sessions
359 * being stopped and hardware drivers cleaned up.
361 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
363 SR_API int sr_session_stop(void)
369 sr_err("session: %s: session was NULL", __func__);
373 sr_info("session: stopping");
374 session->running = FALSE;
376 for (l = session->devs; l; l = l->next) {
378 /* Check for dev != NULL. */
380 if (dev->driver->dev_acquisition_stop)
381 dev->driver->dev_acquisition_stop(dev->driver_index, dev);
382 if (dev->driver->cleanup)
383 dev->driver->cleanup();
393 * @param packet The packet to show debugging information for.
395 static void datafeed_dump(struct sr_datafeed_packet *packet)
397 struct sr_datafeed_logic *logic;
399 switch (packet->type) {
401 sr_dbg("bus: received SR_DF_HEADER");
404 sr_dbg("bus: received SR_DF_TRIGGER");
407 logic = packet->payload;
408 /* TODO: Check for logic != NULL. */
409 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
412 sr_dbg("bus: received SR_DF_END");
415 sr_dbg("bus: received unknown packet type %d", packet->type);
421 * Send a packet to whatever is listening on the datafeed bus.
423 * Hardware drivers use this to send a data packet to the frontend.
426 * @param packet The datafeed packet to send to the session bus.
428 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
430 SR_PRIV int sr_session_send(struct sr_dev *dev,
431 struct sr_datafeed_packet *packet)
434 sr_datafeed_callback_t cb;
437 sr_err("session: %s: dev was NULL", __func__);
442 sr_err("session: %s: dev->driver was NULL", __func__);
447 sr_err("session: %s: packet was NULL", __func__);
451 for (l = session->datafeed_callbacks; l; l = l->next) {
452 if (sr_log_loglevel_get() >= SR_LOG_DBG)
453 datafeed_dump(packet);
455 /* TODO: Check for cb != NULL. */
465 * TODO: More error checks etc.
468 * @param events TODO.
469 * @param timeout TODO.
470 * @param cb Callback function to add. Must not be NULL.
471 * @param cb_data Data for the callback function. Can be NULL.
473 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
474 * SR_ERR_MALLOC upon memory allocation errors.
476 SR_API int sr_session_source_add(int fd, int events, int timeout,
477 sr_receive_data_callback_t cb, void *cb_data)
479 struct source *new_sources, *s;
482 sr_err("session: %s: cb was NULL", __func__);
486 /* Note: cb_data can be NULL, that's not a bug. */
488 new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
490 sr_err("session: %s: new_sources malloc failed", __func__);
491 return SR_ERR_MALLOC;
495 memcpy(new_sources, sources,
496 sizeof(struct source) * num_sources);
500 s = &new_sources[num_sources++];
503 s->timeout = timeout;
505 s->cb_data = cb_data;
506 sources = new_sources;
508 if (timeout != source_timeout && timeout > 0
509 && (source_timeout == -1 || timeout < source_timeout))
510 source_timeout = timeout;
516 * Remove the source belonging to the specified file descriptor.
518 * TODO: More error checks.
522 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
523 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
526 SR_API int sr_session_source_remove(int fd)
528 struct source *new_sources;
532 sr_err("session: %s: sources was NULL", __func__);
536 /* TODO: Check if 'fd' valid. */
538 new_sources = g_try_malloc0(sizeof(struct source) * num_sources);
540 sr_err("session: %s: new_sources malloc failed", __func__);
541 return SR_ERR_MALLOC;
544 for (old = 0, new = 0; old < num_sources; old++) {
545 if (sources[old].fd != fd)
546 memcpy(&new_sources[new++], &sources[old],
547 sizeof(struct source));
552 sources = new_sources;
555 /* Target fd was not found. */