]> sigrok.org Git - libsigrok.git/blame - session.c
sr: Prefix log messages with subsystem string.
[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 }
7d2afd6c
BV
416}
417
9f45fb3a 418/**
a1645fcd
BV
419 * Send a packet to whatever is listening on the datafeed bus.
420 *
421 * Hardware drivers use this to send a data packet to the frontend.
9f45fb3a 422 *
9f45fb3a
UH
423 * @param device TODO.
424 * @param packet TODO.
e0508e67 425 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
9f45fb3a 426 */
a1645fcd 427SR_PRIV int sr_session_bus(struct sr_device *device,
7b48d6e1 428 struct sr_datafeed_packet *packet)
a1bb33af
UH
429{
430 GSList *l;
13b05733 431 sr_datafeed_callback cb;
a1bb33af 432
9f45fb3a
UH
433 if (!device) {
434 sr_err("session: %s: device was NULL", __func__);
e0508e67 435 return SR_ERR_ARG;
9f45fb3a
UH
436 }
437
438 if (!device->plugin) {
439 sr_err("session: %s: device->plugin was NULL", __func__);
e0508e67
UH
440 return SR_ERR_ARG;
441 }
442
443 if (!packet) {
444 sr_err("session: %s: packet was NULL", __func__);
445 return SR_ERR_ARG;
9f45fb3a
UH
446 }
447
62c82025 448 for (l = session->datafeed_callbacks; l; l = l->next) {
18beaeff
BV
449 if (sr_log_loglevel_get() >= SR_LOG_DBG)
450 datafeed_dump(packet);
a1bb33af 451 cb = l->data;
9f45fb3a 452 /* TODO: Check for cb != NULL. */
a1bb33af
UH
453 cb(device, packet);
454 }
9f45fb3a 455
e0508e67 456 return SR_OK;
a1bb33af
UH
457}
458
9f45fb3a
UH
459/**
460 * TODO.
461 *
9f45fb3a
UH
462 * TODO: More error checks etc.
463 *
464 * @param fd TODO.
465 * @param events TODO.
466 * @param timeout TODO.
467 * @param callback TODO.
468 * @param user_data TODO.
e0508e67
UH
469 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
470 * SR_ERR_MALLOC upon memory allocation errors.
9f45fb3a 471 */
1a081ca6
UH
472SR_API int sr_session_source_add(int fd, int events, int timeout,
473 sr_receive_data_callback callback, void *user_data)
544a4582
BV
474{
475 struct source *new_sources, *s;
476
9f45fb3a
UH
477 if (!callback) {
478 sr_err("session: %s: callback was NULL", __func__);
e0508e67 479 return SR_ERR_ARG;
9f45fb3a
UH
480 }
481
482 /* Note: user_data can be NULL, that's not a bug. */
483
133a37bf 484 new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
9f45fb3a
UH
485 if (!new_sources) {
486 sr_err("session: %s: new_sources malloc failed", __func__);
e0508e67 487 return SR_ERR_MALLOC;
9f45fb3a 488 }
544a4582
BV
489
490 if (sources) {
491 memcpy(new_sources, sources,
492 sizeof(struct source) * num_sources);
133a37bf 493 g_free(sources);
544a4582
BV
494 }
495
496 s = &new_sources[num_sources++];
497 s->fd = fd;
498 s->events = events;
499 s->timeout = timeout;
500 s->cb = callback;
501 s->user_data = user_data;
502 sources = new_sources;
503
504 if (timeout != source_timeout && timeout > 0
505 && (source_timeout == -1 || timeout < source_timeout))
506 source_timeout = timeout;
9f45fb3a 507
e0508e67 508 return SR_OK;
544a4582
BV
509}
510
9f45fb3a
UH
511/**
512 * Remove the source belonging to the specified file descriptor.
513 *
9f45fb3a 514 * TODO: More error checks.
9f45fb3a
UH
515 *
516 * @param fd TODO.
e0508e67
UH
517 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
518 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
519 * internal errors.
9f45fb3a 520 */
1a081ca6 521SR_API int sr_session_source_remove(int fd)
544a4582
BV
522{
523 struct source *new_sources;
524 int old, new;
525
e0508e67
UH
526 if (!sources) {
527 sr_err("session: %s: sources was NULL", __func__);
528 return SR_ERR_BUG; /* TODO: Other? */
529 }
530
531 /* TODO: Check if 'fd' valid. */
544a4582 532
133a37bf 533 new_sources = g_try_malloc0(sizeof(struct source) * num_sources);
9f45fb3a
UH
534 if (!new_sources) {
535 sr_err("session: %s: new_sources malloc failed", __func__);
e0508e67 536 return SR_ERR_MALLOC;
9f45fb3a
UH
537 }
538
539 for (old = 0, new = 0; old < num_sources; old++) {
544a4582
BV
540 if (sources[old].fd != fd)
541 memcpy(&new_sources[new++], &sources[old],
542 sizeof(struct source));
9f45fb3a 543 }
544a4582
BV
544
545 if (old != new) {
133a37bf 546 g_free(sources);
544a4582
BV
547 sources = new_sources;
548 num_sources--;
549 } else {
550 /* Target fd was not found. */
133a37bf 551 g_free(new_sources);
544a4582 552 }
e0508e67
UH
553
554 return SR_OK;
544a4582 555}