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