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