]> sigrok.org Git - libsigrok.git/blame - session.c
sr: adjust copyright year
[libsigrok.git] / session.c
CommitLineData
a1bb33af
UH
1/*
2 * This file is part of the sigrok project.
3 *
c73d2ea4 4 * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
a1bb33af
UH
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <string.h>
544a4582 24#include <glib.h>
b7f09cf8
UH
25#include "sigrok.h"
26#include "sigrok-internal.h"
aa4b1107 27
3d2efd7d
UH
28/* demo.c. TODO: Should not be global! */
29extern SR_PRIV GIOChannel channels[2];
a1bb33af 30
544a4582
BV
31struct source {
32 int fd;
33 int events;
34 int timeout;
a887e3da 35 sr_receive_data_callback cb;
544a4582
BV
36 void *user_data;
37};
38
7d658874 39/* There can only be one session at a time. */
a0ecd83b 40/* 'session' is not static, it's used elsewhere (via 'extern'). */
2872d21e 41struct sr_session *session;
a0ecd83b 42static int num_sources = 0;
544a4582 43
a0ecd83b
UH
44static struct source *sources = NULL;
45static int source_timeout = -1;
544a4582 46
9f45fb3a
UH
47/**
48 * Create a new session.
49 *
9f45fb3a
UH
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.
52 *
53 * @return A pointer to the newly allocated session, or NULL upon errors.
54 */
1a081ca6 55SR_API struct sr_session *sr_session_new(void)
a1bb33af 56{
133a37bf 57 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
9f45fb3a
UH
58 sr_err("session: %s: session malloc failed", __func__);
59 return NULL; /* TODO: SR_ERR_MALLOC? */
60 }
a1bb33af
UH
61
62 return session;
63}
64
9f45fb3a
UH
65/**
66 * Destroy the current session.
67 *
68 * This frees up all memory used by the session.
69 *
e0508e67 70 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 71 */
1a081ca6 72SR_API int sr_session_destroy(void)
a1bb33af 73{
9f45fb3a 74 if (!session) {
133a37bf 75 sr_err("session: %s: session was NULL", __func__);
e0508e67 76 return SR_ERR_BUG;
9f45fb3a
UH
77 }
78
a1bb33af 79 g_slist_free(session->devices);
e0508e67 80 session->devices = NULL;
a1bb33af 81
9f45fb3a
UH
82 /* TODO: Error checks needed? */
83
aa4b1107 84 /* TODO: Loop over protocol decoders and free them. */
a1bb33af
UH
85
86 g_free(session);
9f45fb3a 87 session = NULL;
e0508e67
UH
88
89 return SR_OK;
a1bb33af
UH
90}
91
9f45fb3a
UH
92/**
93 * Remove all the devices from the current session. TODO?
94 *
95 * The session itself (i.e., the struct sr_session) is not free'd and still
96 * exists after this function returns.
97 *
e0508e67 98 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 99 */
1a081ca6 100SR_API int sr_session_device_clear(void)
a1bb33af 101{
9f45fb3a 102 if (!session) {
133a37bf 103 sr_err("session: %s: session was NULL", __func__);
e0508e67 104 return SR_ERR_BUG;
9f45fb3a
UH
105 }
106
a1bb33af
UH
107 g_slist_free(session->devices);
108 session->devices = NULL;
e0508e67
UH
109
110 return SR_OK;
a1bb33af
UH
111}
112
9f45fb3a
UH
113/**
114 * Add a device to the current session.
115 *
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
118 * be NULL.
119 *
120 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
121 */
1a081ca6 122SR_API int sr_session_device_add(struct sr_device *device)
a1bb33af
UH
123{
124 int ret;
125
9f45fb3a
UH
126 if (!device) {
127 sr_err("session: %s: device was NULL", __func__);
128 return SR_ERR_ARG;
129 }
130
131 if (!device->plugin) {
132 sr_err("session: %s: device->plugin was NULL", __func__);
133 return SR_ERR_ARG;
134 }
135
136 if (!device->plugin->opendev) {
137 sr_err("session: %s: device->plugin->opendev was NULL",
138 __func__);
139 return SR_ERR_ARG;
140 }
141
142 if (!session) {
143 sr_err("session: %s: session was NULL", __func__);
144 return SR_ERR; /* TODO: SR_ERR_BUG? */
145 }
146
147 if ((ret = device->plugin->opendev(device->plugin_index)) != SR_OK) {
148 sr_err("session: %s: opendev failed (%d)", __func__, ret);
149 return ret;
aa4b1107 150 }
a1bb33af 151
aa4b1107
BV
152 session->devices = g_slist_append(session->devices, device);
153
e46b8fb1 154 return SR_OK;
a1bb33af
UH
155}
156
9f45fb3a
UH
157/**
158 * Clear all datafeed callbacks in the current session.
159 *
e0508e67 160 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 161 */
1a081ca6 162SR_API int sr_session_datafeed_callback_clear(void)
a1bb33af 163{
9f45fb3a
UH
164 if (!session) {
165 sr_err("session: %s: session was NULL", __func__);
e0508e67 166 return SR_ERR_BUG;
9f45fb3a
UH
167 }
168
a1bb33af
UH
169 g_slist_free(session->datafeed_callbacks);
170 session->datafeed_callbacks = NULL;
e0508e67
UH
171
172 return SR_OK;
a1bb33af
UH
173}
174
9f45fb3a
UH
175/**
176 * Add a datafeed callback to the current session.
177 *
a1645fcd
BV
178 * @param callback Function to call when a chunk of data is received.
179 *
e0508e67 180 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 181 */
1a081ca6 182SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback callback)
a1bb33af 183{
9f45fb3a
UH
184 if (!session) {
185 sr_err("session: %s: session was NULL", __func__);
e0508e67 186 return SR_ERR_BUG;
9f45fb3a
UH
187 }
188
189 /* TODO: Is 'callback' allowed to be NULL? */
190
62c82025
UH
191 session->datafeed_callbacks =
192 g_slist_append(session->datafeed_callbacks, callback);
e0508e67
UH
193
194 return SR_OK;
a1bb33af
UH
195}
196
9f45fb3a
UH
197/**
198 * TODO.
199 */
e0508e67 200static int sr_session_run_poll(void)
544a4582
BV
201{
202 GPollFD *fds, my_gpollfd;
203 int ret, i;
204
544a4582
BV
205 fds = NULL;
206 while (session->running) {
133a37bf
UH
207 /* TODO: Add comment. */
208 g_free(fds);
544a4582
BV
209
210 /* Construct g_poll()'s array. */
133a37bf 211 if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) {
e0508e67
UH
212 sr_err("session: %s: fds malloc failed", __func__);
213 return SR_ERR_MALLOC;
214 }
544a4582
BV
215 for (i = 0; i < num_sources; i++) {
216#ifdef _WIN32
217 g_io_channel_win32_make_pollfd(&channels[0],
218 sources[i].events, &my_gpollfd);
219#else
220 my_gpollfd.fd = sources[i].fd;
221 my_gpollfd.events = sources[i].events;
222 fds[i] = my_gpollfd;
223#endif
224 }
225
226 ret = g_poll(fds, num_sources, source_timeout);
227
228 for (i = 0; i < num_sources; i++) {
229 if (fds[i].revents > 0 || (ret == 0
230 && source_timeout == sources[i].timeout)) {
231 /*
232 * Invoke the source's callback on an event,
233 * or if the poll timeout out and this source
234 * asked for that timeout.
235 */
5c582d9f
GM
236 if (!sources[i].cb(fds[i].fd, fds[i].revents,
237 sources[i].user_data))
238 sr_session_source_remove(sources[i].fd);
544a4582
BV
239 }
240 }
241 }
133a37bf 242 g_free(fds);
e0508e67
UH
243
244 return SR_OK;
544a4582
BV
245}
246
9f45fb3a
UH
247/**
248 * Start a session.
249 *
a1645fcd 250 * There can only be one session at a time.
9f45fb3a
UH
251 *
252 * @return SR_OK upon success, SR_ERR upon errors.
253 */
1a081ca6 254SR_API int sr_session_start(void)
7d658874
BV
255{
256 struct sr_device *device;
257 GSList *l;
258 int ret;
259
9f45fb3a
UH
260 if (!session) {
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? */
264 }
265
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? */
271 }
272
273 /* TODO: Check plugin_index validity? */
274
b08024a8 275 sr_info("session: starting");
9f45fb3a 276
7d658874
BV
277 for (l = session->devices; l; l = l->next) {
278 device = l->data;
9f45fb3a 279 /* TODO: Check for device != NULL. */
7d658874 280 if ((ret = device->plugin->start_acquisition(
9f45fb3a 281 device->plugin_index, device)) != SR_OK) {
446a0372 282 sr_err("session: %s: could not start an acquisition "
9f45fb3a 283 "(%d)", __func__, ret);
7d658874 284 break;
9f45fb3a 285 }
7d658874
BV
286 }
287
9f45fb3a
UH
288 /* TODO: What if there are multiple devices? Which return code? */
289
7d658874
BV
290 return ret;
291}
292
9f45fb3a
UH
293/**
294 * Run the session.
295 *
9f45fb3a
UH
296 * TODO: Various error checks etc.
297 *
e0508e67 298 * @return SR_OK upon success, SR_ERR_BUG upon errors.
9f45fb3a 299 */
1a081ca6 300SR_API int sr_session_run(void)
7d658874 301{
9f45fb3a
UH
302 if (!session) {
303 sr_err("session: %s: session was NULL; a session must be "
304 "created first, before running it.", __func__);
e0508e67 305 return SR_ERR_BUG;
9f45fb3a
UH
306 }
307
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__);
e0508e67 312 return SR_ERR_BUG;
9f45fb3a
UH
313 }
314
b08024a8 315 sr_info("session: running");
7d658874
BV
316 session->running = TRUE;
317
9f45fb3a
UH
318 /* Do we have real sources? */
319 if (num_sources == 1 && sources[0].fd == -1) {
320 /* Dummy source, freewheel over it. */
7d658874
BV
321 while (session->running)
322 sources[0].cb(-1, 0, sources[0].user_data);
9f45fb3a
UH
323 } else {
324 /* Real sources, use g_poll() main loop. */
8a2efef2 325 sr_session_run_poll();
9f45fb3a
UH
326 }
327
e0508e67 328 return SR_OK;
7d658874
BV
329}
330
9f45fb3a
UH
331/**
332 * Halt the current session.
333 *
a1645fcd
BV
334 * This requests the current session be stopped as soon as possible, for example
335 * on receiving an SR_DF_END packet.
e0508e67
UH
336 *
337 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 338 */
1a081ca6 339SR_API int sr_session_halt(void)
544a4582 340{
9f45fb3a
UH
341 if (!session) {
342 sr_err("session: %s: session was NULL", __func__);
e0508e67 343 return SR_ERR_BUG;
9f45fb3a
UH
344 }
345
b08024a8 346 sr_info("session: halting");
544a4582 347 session->running = FALSE;
9f45fb3a 348
e0508e67 349 return SR_OK;
544a4582
BV
350}
351
9f45fb3a
UH
352/**
353 * Stop the current session.
354 *
a1645fcd
BV
355 * The current session is stopped immediately, with all acquisition sessions
356 * being stopped and hardware plugins cleaned up.
9f45fb3a 357 *
e0508e67 358 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 359 */
1a081ca6 360SR_API int sr_session_stop(void)
a1bb33af 361{
5c2d46d1 362 struct sr_device *device;
a1bb33af
UH
363 GSList *l;
364
9f45fb3a
UH
365 if (!session) {
366 sr_err("session: %s: session was NULL", __func__);
e0508e67 367 return SR_ERR_BUG;
9f45fb3a
UH
368 }
369
b08024a8 370 sr_info("session: stopping");
544a4582 371 session->running = FALSE;
e0508e67 372
62c82025 373 for (l = session->devices; l; l = l->next) {
a1bb33af 374 device = l->data;
e0508e67 375 /* Check for device != NULL. */
8c76be53
BV
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();
381 }
a1bb33af 382 }
9f45fb3a 383
e0508e67 384 return SR_OK;
a1bb33af
UH
385}
386
9f45fb3a 387/**
a1645fcd 388 * Debug helper.
9f45fb3a 389 *
996b0c72 390 * @param packet The packet to show debugging information for.
18beaeff 391 *
9f45fb3a 392 */
18beaeff 393static void datafeed_dump(struct sr_datafeed_packet *packet)
7d2afd6c
BV
394{
395 struct sr_datafeed_logic *logic;
396
397 switch (packet->type) {
398 case SR_DF_HEADER:
399 sr_dbg("bus: received SR_DF_HEADER");
400 break;
401 case SR_DF_TRIGGER:
01469707 402 sr_dbg("bus: received SR_DF_TRIGGER");
7d2afd6c
BV
403 break;
404 case SR_DF_LOGIC:
405 logic = packet->payload;
e0508e67 406 /* TODO: Check for logic != NULL. */
01469707 407 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
7d2afd6c
BV
408 break;
409 case SR_DF_END:
410 sr_dbg("bus: received SR_DF_END");
411 break;
412 default:
18beaeff 413 sr_dbg("bus: received unknown packet type %d", packet->type);
9f45fb3a 414 break;
7d2afd6c 415 }
e0508e67 416
7d2afd6c
BV
417}
418
9f45fb3a 419/**
a1645fcd
BV
420 * Send a packet to whatever is listening on the datafeed bus.
421 *
422 * Hardware drivers use this to send a data packet to the frontend.
9f45fb3a 423 *
9f45fb3a
UH
424 * @param device TODO.
425 * @param packet TODO.
e0508e67 426 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
9f45fb3a 427 */
a1645fcd 428SR_PRIV int sr_session_bus(struct sr_device *device,
1a081ca6 429 struct sr_datafeed_packet *packet)
a1bb33af
UH
430{
431 GSList *l;
13b05733 432 sr_datafeed_callback cb;
a1bb33af 433
9f45fb3a
UH
434 if (!device) {
435 sr_err("session: %s: device was NULL", __func__);
e0508e67 436 return SR_ERR_ARG;
9f45fb3a
UH
437 }
438
439 if (!device->plugin) {
440 sr_err("session: %s: device->plugin was NULL", __func__);
e0508e67
UH
441 return SR_ERR_ARG;
442 }
443
444 if (!packet) {
445 sr_err("session: %s: packet was NULL", __func__);
446 return SR_ERR_ARG;
9f45fb3a
UH
447 }
448
62c82025 449 for (l = session->datafeed_callbacks; l; l = l->next) {
18beaeff
BV
450 if (sr_log_loglevel_get() >= SR_LOG_DBG)
451 datafeed_dump(packet);
a1bb33af 452 cb = l->data;
9f45fb3a 453 /* TODO: Check for cb != NULL. */
a1bb33af
UH
454 cb(device, packet);
455 }
9f45fb3a 456
e0508e67 457 return SR_OK;
a1bb33af
UH
458}
459
9f45fb3a
UH
460/**
461 * TODO.
462 *
9f45fb3a
UH
463 * TODO: More error checks etc.
464 *
465 * @param fd TODO.
466 * @param events TODO.
467 * @param timeout TODO.
468 * @param callback TODO.
469 * @param user_data TODO.
e0508e67
UH
470 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
471 * SR_ERR_MALLOC upon memory allocation errors.
9f45fb3a 472 */
1a081ca6
UH
473SR_API int sr_session_source_add(int fd, int events, int timeout,
474 sr_receive_data_callback callback, void *user_data)
544a4582
BV
475{
476 struct source *new_sources, *s;
477
9f45fb3a
UH
478 if (!callback) {
479 sr_err("session: %s: callback was NULL", __func__);
e0508e67 480 return SR_ERR_ARG;
9f45fb3a
UH
481 }
482
483 /* Note: user_data can be NULL, that's not a bug. */
484
133a37bf 485 new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
9f45fb3a
UH
486 if (!new_sources) {
487 sr_err("session: %s: new_sources malloc failed", __func__);
e0508e67 488 return SR_ERR_MALLOC;
9f45fb3a 489 }
544a4582
BV
490
491 if (sources) {
492 memcpy(new_sources, sources,
493 sizeof(struct source) * num_sources);
133a37bf 494 g_free(sources);
544a4582
BV
495 }
496
497 s = &new_sources[num_sources++];
498 s->fd = fd;
499 s->events = events;
500 s->timeout = timeout;
501 s->cb = callback;
502 s->user_data = user_data;
503 sources = new_sources;
504
505 if (timeout != source_timeout && timeout > 0
506 && (source_timeout == -1 || timeout < source_timeout))
507 source_timeout = timeout;
9f45fb3a 508
e0508e67 509 return SR_OK;
544a4582
BV
510}
511
9f45fb3a
UH
512/**
513 * Remove the source belonging to the specified file descriptor.
514 *
9f45fb3a 515 * TODO: More error checks.
9f45fb3a
UH
516 *
517 * @param fd TODO.
e0508e67
UH
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
520 * internal errors.
9f45fb3a 521 */
1a081ca6 522SR_API int sr_session_source_remove(int fd)
544a4582
BV
523{
524 struct source *new_sources;
525 int old, new;
526
e0508e67
UH
527 if (!sources) {
528 sr_err("session: %s: sources was NULL", __func__);
529 return SR_ERR_BUG; /* TODO: Other? */
530 }
531
532 /* TODO: Check if 'fd' valid. */
544a4582 533
133a37bf 534 new_sources = g_try_malloc0(sizeof(struct source) * num_sources);
9f45fb3a
UH
535 if (!new_sources) {
536 sr_err("session: %s: new_sources malloc failed", __func__);
e0508e67 537 return SR_ERR_MALLOC;
9f45fb3a
UH
538 }
539
540 for (old = 0, new = 0; old < num_sources; old++) {
544a4582
BV
541 if (sources[old].fd != fd)
542 memcpy(&new_sources[new++], &sources[old],
543 sizeof(struct source));
9f45fb3a 544 }
544a4582
BV
545
546 if (old != new) {
133a37bf 547 g_free(sources);
544a4582
BV
548 sources = new_sources;
549 num_sources--;
550 } else {
551 /* Target fd was not found. */
133a37bf 552 g_free(new_sources);
544a4582 553 }
e0508e67
UH
554
555 return SR_OK;
544a4582 556}