]> sigrok.org Git - libsigrok.git/blame - session.c
Merge remote-tracking branch 'joel/public'
[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;
d08490aa 35 sr_receive_data_callback_t cb;
1f9813eb 36 void *cb_data;
544a4582
BV
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
bb7ef793
UH
79 g_slist_free(session->devs);
80 session->devs = 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 */
bb7ef793 100SR_API int sr_session_dev_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
bb7ef793
UH
107 g_slist_free(session->devs);
108 session->devs = NULL;
e0508e67
UH
109
110 return SR_OK;
a1bb33af
UH
111}
112
9f45fb3a
UH
113/**
114 * Add a device to the current session.
115 *
bb7ef793 116 * @param dev The device to add to the current session. Must not be NULL.
c09f0b57 117 * Also, dev->driver and dev->driver->dev_open must not be NULL.
9f45fb3a
UH
118 *
119 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
120 */
bb7ef793 121SR_API int sr_session_dev_add(struct sr_dev *dev)
a1bb33af
UH
122{
123 int ret;
124
bb7ef793
UH
125 if (!dev) {
126 sr_err("session: %s: dev was NULL", __func__);
9f45fb3a
UH
127 return SR_ERR_ARG;
128 }
129
c09f0b57
UH
130 if (!dev->driver) {
131 sr_err("session: %s: dev->driver was NULL", __func__);
9f45fb3a
UH
132 return SR_ERR_ARG;
133 }
134
c09f0b57
UH
135 if (!dev->driver->dev_open) {
136 sr_err("session: %s: dev->driver->dev_open was NULL",
9f45fb3a
UH
137 __func__);
138 return SR_ERR_ARG;
139 }
140
141 if (!session) {
142 sr_err("session: %s: session was NULL", __func__);
0abee507 143 return SR_ERR_BUG;
9f45fb3a
UH
144 }
145
c09f0b57 146 if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) {
e7eb703f 147 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
9f45fb3a 148 return ret;
aa4b1107 149 }
a1bb33af 150
bb7ef793 151 session->devs = g_slist_append(session->devs, dev);
aa4b1107 152
e46b8fb1 153 return SR_OK;
a1bb33af
UH
154}
155
9f45fb3a
UH
156/**
157 * Clear all datafeed callbacks in the current session.
158 *
e0508e67 159 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 160 */
1a081ca6 161SR_API int sr_session_datafeed_callback_clear(void)
a1bb33af 162{
9f45fb3a
UH
163 if (!session) {
164 sr_err("session: %s: session was NULL", __func__);
e0508e67 165 return SR_ERR_BUG;
9f45fb3a
UH
166 }
167
a1bb33af
UH
168 g_slist_free(session->datafeed_callbacks);
169 session->datafeed_callbacks = NULL;
e0508e67
UH
170
171 return SR_OK;
a1bb33af
UH
172}
173
9f45fb3a
UH
174/**
175 * Add a datafeed callback to the current session.
176 *
d08490aa 177 * @param cb Function to call when a chunk of data is received.
0abee507 178 * Must not be NULL.
a1645fcd 179 *
e0508e67 180 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 181 */
d08490aa 182SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
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
0abee507
UH
189 if (!cb) {
190 sr_err("session: %s: cb was NULL", __func__);
191 return SR_ERR_ARG;
192 }
9f45fb3a 193
62c82025 194 session->datafeed_callbacks =
d08490aa 195 g_slist_append(session->datafeed_callbacks, cb);
e0508e67
UH
196
197 return SR_OK;
a1bb33af
UH
198}
199
9f45fb3a
UH
200/**
201 * TODO.
202 */
e0508e67 203static int sr_session_run_poll(void)
544a4582
BV
204{
205 GPollFD *fds, my_gpollfd;
206 int ret, i;
207
544a4582
BV
208 fds = NULL;
209 while (session->running) {
133a37bf
UH
210 /* TODO: Add comment. */
211 g_free(fds);
544a4582
BV
212
213 /* Construct g_poll()'s array. */
133a37bf 214 if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) {
e0508e67
UH
215 sr_err("session: %s: fds malloc failed", __func__);
216 return SR_ERR_MALLOC;
217 }
544a4582
BV
218 for (i = 0; i < num_sources; i++) {
219#ifdef _WIN32
220 g_io_channel_win32_make_pollfd(&channels[0],
221 sources[i].events, &my_gpollfd);
222#else
223 my_gpollfd.fd = sources[i].fd;
224 my_gpollfd.events = sources[i].events;
225 fds[i] = my_gpollfd;
226#endif
227 }
228
229 ret = g_poll(fds, num_sources, source_timeout);
230
231 for (i = 0; i < num_sources; i++) {
232 if (fds[i].revents > 0 || (ret == 0
233 && source_timeout == sources[i].timeout)) {
234 /*
235 * Invoke the source's callback on an event,
236 * or if the poll timeout out and this source
237 * asked for that timeout.
238 */
5c582d9f 239 if (!sources[i].cb(fds[i].fd, fds[i].revents,
1f9813eb 240 sources[i].cb_data))
5c582d9f 241 sr_session_source_remove(sources[i].fd);
544a4582
BV
242 }
243 }
244 }
133a37bf 245 g_free(fds);
e0508e67
UH
246
247 return SR_OK;
544a4582
BV
248}
249
9f45fb3a
UH
250/**
251 * Start a session.
252 *
a1645fcd 253 * There can only be one session at a time.
9f45fb3a
UH
254 *
255 * @return SR_OK upon success, SR_ERR upon errors.
256 */
1a081ca6 257SR_API int sr_session_start(void)
7d658874 258{
bb7ef793 259 struct sr_dev *dev;
7d658874
BV
260 GSList *l;
261 int ret;
262
9f45fb3a
UH
263 if (!session) {
264 sr_err("session: %s: session was NULL; a session must be "
265 "created first, before starting it.", __func__);
0abee507 266 return SR_ERR_BUG;
9f45fb3a
UH
267 }
268
bb7ef793 269 if (!session->devs) {
9f45fb3a 270 /* TODO: Actually the case? */
bb7ef793 271 sr_err("session: %s: session->devs was NULL; a session "
9f45fb3a 272 "cannot be started without devices.", __func__);
0abee507 273 return SR_ERR_BUG;
9f45fb3a
UH
274 }
275
c09f0b57 276 /* TODO: Check driver_index validity? */
9f45fb3a 277
b08024a8 278 sr_info("session: starting");
9f45fb3a 279
bb7ef793
UH
280 for (l = session->devs; l; l = l->next) {
281 dev = l->data;
282 /* TODO: Check for dev != NULL. */
c09f0b57
UH
283 if ((ret = dev->driver->dev_acquisition_start(
284 dev->driver_index, dev)) != SR_OK) {
446a0372 285 sr_err("session: %s: could not start an acquisition "
9f45fb3a 286 "(%d)", __func__, ret);
7d658874 287 break;
9f45fb3a 288 }
7d658874
BV
289 }
290
9f45fb3a
UH
291 /* TODO: What if there are multiple devices? Which return code? */
292
7d658874
BV
293 return ret;
294}
295
9f45fb3a
UH
296/**
297 * Run the session.
298 *
9f45fb3a
UH
299 * TODO: Various error checks etc.
300 *
e0508e67 301 * @return SR_OK upon success, SR_ERR_BUG upon errors.
9f45fb3a 302 */
1a081ca6 303SR_API int sr_session_run(void)
7d658874 304{
9f45fb3a
UH
305 if (!session) {
306 sr_err("session: %s: session was NULL; a session must be "
307 "created first, before running it.", __func__);
e0508e67 308 return SR_ERR_BUG;
9f45fb3a
UH
309 }
310
bb7ef793 311 if (!session->devs) {
9f45fb3a 312 /* TODO: Actually the case? */
bb7ef793 313 sr_err("session: %s: session->devs was NULL; a session "
9f45fb3a 314 "cannot be run without devices.", __func__);
e0508e67 315 return SR_ERR_BUG;
9f45fb3a
UH
316 }
317
b08024a8 318 sr_info("session: running");
7d658874
BV
319 session->running = TRUE;
320
9f45fb3a
UH
321 /* Do we have real sources? */
322 if (num_sources == 1 && sources[0].fd == -1) {
323 /* Dummy source, freewheel over it. */
7d658874 324 while (session->running)
1f9813eb 325 sources[0].cb(-1, 0, sources[0].cb_data);
9f45fb3a
UH
326 } else {
327 /* Real sources, use g_poll() main loop. */
8a2efef2 328 sr_session_run_poll();
9f45fb3a
UH
329 }
330
e0508e67 331 return SR_OK;
7d658874
BV
332}
333
9f45fb3a
UH
334/**
335 * Halt the current session.
336 *
44dae539
UH
337 * This requests the current session be stopped as soon as possible, for
338 * example on receiving an SR_DF_END packet.
e0508e67
UH
339 *
340 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 341 */
1a081ca6 342SR_API int sr_session_halt(void)
544a4582 343{
9f45fb3a
UH
344 if (!session) {
345 sr_err("session: %s: session was NULL", __func__);
e0508e67 346 return SR_ERR_BUG;
9f45fb3a
UH
347 }
348
b08024a8 349 sr_info("session: halting");
544a4582 350 session->running = FALSE;
9f45fb3a 351
e0508e67 352 return SR_OK;
544a4582
BV
353}
354
9f45fb3a
UH
355/**
356 * Stop the current session.
357 *
a1645fcd 358 * The current session is stopped immediately, with all acquisition sessions
c09f0b57 359 * being stopped and hardware drivers cleaned up.
9f45fb3a 360 *
e0508e67 361 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 362 */
1a081ca6 363SR_API int sr_session_stop(void)
a1bb33af 364{
bb7ef793 365 struct sr_dev *dev;
a1bb33af
UH
366 GSList *l;
367
9f45fb3a
UH
368 if (!session) {
369 sr_err("session: %s: session was NULL", __func__);
e0508e67 370 return SR_ERR_BUG;
9f45fb3a
UH
371 }
372
b08024a8 373 sr_info("session: stopping");
544a4582 374 session->running = FALSE;
e0508e67 375
bb7ef793
UH
376 for (l = session->devs; l; l = l->next) {
377 dev = l->data;
378 /* Check for dev != NULL. */
c09f0b57
UH
379 if (dev->driver) {
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();
8c76be53 384 }
a1bb33af 385 }
9f45fb3a 386
e0508e67 387 return SR_OK;
a1bb33af
UH
388}
389
9f45fb3a 390/**
a1645fcd 391 * Debug helper.
9f45fb3a 392 *
996b0c72 393 * @param packet The packet to show debugging information for.
9f45fb3a 394 */
18beaeff 395static void datafeed_dump(struct sr_datafeed_packet *packet)
7d2afd6c
BV
396{
397 struct sr_datafeed_logic *logic;
398
399 switch (packet->type) {
400 case SR_DF_HEADER:
401 sr_dbg("bus: received SR_DF_HEADER");
402 break;
403 case SR_DF_TRIGGER:
01469707 404 sr_dbg("bus: received SR_DF_TRIGGER");
7d2afd6c
BV
405 break;
406 case SR_DF_LOGIC:
407 logic = packet->payload;
e0508e67 408 /* TODO: Check for logic != NULL. */
01469707 409 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
7d2afd6c
BV
410 break;
411 case SR_DF_END:
412 sr_dbg("bus: received SR_DF_END");
413 break;
414 default:
18beaeff 415 sr_dbg("bus: received unknown packet type %d", packet->type);
9f45fb3a 416 break;
7d2afd6c 417 }
7d2afd6c
BV
418}
419
9f45fb3a 420/**
a1645fcd
BV
421 * Send a packet to whatever is listening on the datafeed bus.
422 *
423 * Hardware drivers use this to send a data packet to the frontend.
9f45fb3a 424 *
bb7ef793 425 * @param dev TODO.
31ccebc4 426 * @param packet The datafeed packet to send to the session bus.
44dae539 427 *
e0508e67 428 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
9f45fb3a 429 */
31ccebc4
UH
430SR_PRIV int sr_session_send(struct sr_dev *dev,
431 struct sr_datafeed_packet *packet)
a1bb33af
UH
432{
433 GSList *l;
d08490aa 434 sr_datafeed_callback_t cb;
a1bb33af 435
bb7ef793
UH
436 if (!dev) {
437 sr_err("session: %s: dev was NULL", __func__);
e0508e67 438 return SR_ERR_ARG;
9f45fb3a
UH
439 }
440
c09f0b57
UH
441 if (!dev->driver) {
442 sr_err("session: %s: dev->driver was NULL", __func__);
e0508e67
UH
443 return SR_ERR_ARG;
444 }
445
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;
480
d08490aa
UH
481 if (!cb) {
482 sr_err("session: %s: cb was NULL", __func__);
e0508e67 483 return SR_ERR_ARG;
9f45fb3a
UH
484 }
485
1f9813eb 486 /* Note: cb_data can be NULL, that's not a bug. */
9f45fb3a 487
133a37bf 488 new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
9f45fb3a
UH
489 if (!new_sources) {
490 sr_err("session: %s: new_sources malloc failed", __func__);
e0508e67 491 return SR_ERR_MALLOC;
9f45fb3a 492 }
544a4582
BV
493
494 if (sources) {
495 memcpy(new_sources, sources,
496 sizeof(struct source) * num_sources);
133a37bf 497 g_free(sources);
544a4582
BV
498 }
499
500 s = &new_sources[num_sources++];
501 s->fd = fd;
502 s->events = events;
503 s->timeout = timeout;
d08490aa 504 s->cb = cb;
1f9813eb 505 s->cb_data = cb_data;
544a4582
BV
506 sources = new_sources;
507
508 if (timeout != source_timeout && timeout > 0
509 && (source_timeout == -1 || timeout < source_timeout))
510 source_timeout = timeout;
9f45fb3a 511
e0508e67 512 return SR_OK;
544a4582
BV
513}
514
9f45fb3a
UH
515/**
516 * Remove the source belonging to the specified file descriptor.
517 *
9f45fb3a 518 * TODO: More error checks.
9f45fb3a
UH
519 *
520 * @param fd TODO.
44dae539 521 *
e0508e67
UH
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
524 * internal errors.
9f45fb3a 525 */
1a081ca6 526SR_API int sr_session_source_remove(int fd)
544a4582
BV
527{
528 struct source *new_sources;
529 int old, new;
530
e0508e67
UH
531 if (!sources) {
532 sr_err("session: %s: sources was NULL", __func__);
0abee507 533 return SR_ERR_BUG;
e0508e67
UH
534 }
535
536 /* TODO: Check if 'fd' valid. */
544a4582 537
133a37bf 538 new_sources = g_try_malloc0(sizeof(struct source) * num_sources);
9f45fb3a
UH
539 if (!new_sources) {
540 sr_err("session: %s: new_sources malloc failed", __func__);
e0508e67 541 return SR_ERR_MALLOC;
9f45fb3a
UH
542 }
543
544 for (old = 0, new = 0; old < num_sources; old++) {
544a4582
BV
545 if (sources[old].fd != fd)
546 memcpy(&new_sources[new++], &sources[old],
547 sizeof(struct source));
9f45fb3a 548 }
544a4582
BV
549
550 if (old != new) {
133a37bf 551 g_free(sources);
544a4582
BV
552 sources = new_sources;
553 num_sources--;
554 } else {
555 /* Target fd was not found. */
133a37bf 556 g_free(new_sources);
544a4582 557 }
e0508e67
UH
558
559 return SR_OK;
544a4582 560}