]> sigrok.org Git - libsigrok.git/blame_incremental - session.c
Doxygen: Mark non-public stuff for exclusion.
[libsigrok.git] / session.c
... / ...
CommitLineData
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010-2012 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
20#include <stdio.h>
21#include <stdlib.h>
22#include <unistd.h>
23#include <string.h>
24#include <glib.h>
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
27
28struct source {
29 int timeout;
30 sr_receive_data_callback_t cb;
31 void *cb_data;
32
33 /* This is used to keep track of the object (fd, pollfd or channel) which is
34 * being polled and will be used to match the source when removing it again.
35 */
36 gintptr poll_object;
37};
38
39/* There can only be one session at a time. */
40/* 'session' is not static, it's used elsewhere (via 'extern'). */
41struct sr_session *session;
42
43/**
44 * Create a new session.
45 *
46 * TODO: Should it use the file-global "session" variable or take an argument?
47 * The same question applies to all the other session functions.
48 *
49 * @return A pointer to the newly allocated session, or NULL upon errors.
50 */
51SR_API struct sr_session *sr_session_new(void)
52{
53 if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
54 sr_err("session: %s: session malloc failed", __func__);
55 return NULL; /* TODO: SR_ERR_MALLOC? */
56 }
57
58 session->source_timeout = -1;
59
60 return session;
61}
62
63/**
64 * Destroy the current session.
65 *
66 * This frees up all memory used by the session.
67 *
68 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
69 */
70SR_API int sr_session_destroy(void)
71{
72 if (!session) {
73 sr_err("session: %s: session was NULL", __func__);
74 return SR_ERR_BUG;
75 }
76
77 sr_session_dev_remove_all();
78
79 /* TODO: Error checks needed? */
80
81 /* TODO: Loop over protocol decoders and free them. */
82
83 g_free(session);
84 session = NULL;
85
86 return SR_OK;
87}
88
89static void sr_dev_close(struct sr_dev_inst *sdi)
90{
91 if (sdi->driver && sdi->driver->dev_close)
92 sdi->driver->dev_close(sdi);
93}
94
95/**
96 * Remove all the devices from the current session. TODO?
97 *
98 * The session itself (i.e., the struct sr_session) is not free'd and still
99 * exists after this function returns.
100 *
101 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
102 */
103SR_API int sr_session_dev_remove_all(void)
104{
105 if (!session) {
106 sr_err("session: %s: session was NULL", __func__);
107 return SR_ERR_BUG;
108 }
109
110 g_slist_free_full(session->devs, (GDestroyNotify)sr_dev_close);
111 session->devs = NULL;
112
113 return SR_OK;
114}
115
116/**
117 * Add a device to the current session.
118 *
119 * @param dev The device instance to add to the current session. Must not
120 * be NULL. Also, sdi->driver and sdi->driver->dev_open must
121 * not be NULL.
122 *
123 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
124 */
125SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi)
126{
127 int ret;
128
129 if (!sdi) {
130 sr_err("session: %s: sdi was NULL", __func__);
131 return SR_ERR_ARG;
132 }
133
134 if (!session) {
135 sr_err("session: %s: session was NULL", __func__);
136 return SR_ERR_BUG;
137 }
138
139 /* If sdi->driver is NULL, this is a virtual device. */
140 if (!sdi->driver) {
141 sr_dbg("session: %s: sdi->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, (gpointer)sdi);
145 return SR_OK;
146 }
147
148 /* sdi->driver is non-NULL (i.e. we have a real device). */
149 if (!sdi->driver->dev_open) {
150 sr_err("session: %s: sdi->driver->dev_open was NULL", __func__);
151 return SR_ERR_BUG;
152 }
153
154 if ((ret = sdi->driver->dev_open((struct sr_dev_inst *)sdi)) != SR_OK) {
155 sr_err("session: %s: dev_open failed (%d)", __func__, ret);
156 return ret;
157 }
158
159 session->devs = g_slist_append(session->devs, (gpointer)sdi);
160
161 return SR_OK;
162}
163
164/**
165 * Remove all datafeed callbacks in the current session.
166 *
167 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
168 */
169SR_API int sr_session_datafeed_callback_remove_all(void)
170{
171 if (!session) {
172 sr_err("session: %s: session was NULL", __func__);
173 return SR_ERR_BUG;
174 }
175
176 g_slist_free(session->datafeed_callbacks);
177 session->datafeed_callbacks = NULL;
178
179 return SR_OK;
180}
181
182/**
183 * Add a datafeed callback to the current session.
184 *
185 * @param cb Function to call when a chunk of data is received.
186 * Must not be NULL.
187 *
188 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
189 */
190SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
191{
192 if (!session) {
193 sr_err("session: %s: session was NULL", __func__);
194 return SR_ERR_BUG;
195 }
196
197 if (!cb) {
198 sr_err("session: %s: cb was NULL", __func__);
199 return SR_ERR_ARG;
200 }
201
202 session->datafeed_callbacks =
203 g_slist_append(session->datafeed_callbacks, cb);
204
205 return SR_OK;
206}
207
208static int sr_session_run_poll(void)
209{
210 unsigned int i;
211 int ret;
212
213 while (session->num_sources > 0) {
214 ret = g_poll(session->pollfds, session->num_sources,
215 session->source_timeout);
216 for (i = 0; i < session->num_sources; i++) {
217 if (session->pollfds[i].revents > 0 || (ret == 0
218 && session->source_timeout == session->sources[i].timeout)) {
219 /*
220 * Invoke the source's callback on an event,
221 * or if the poll timed out and this source
222 * asked for that timeout.
223 */
224 if (!session->sources[i].cb(session->pollfds[i].fd,
225 session->pollfds[i].revents,
226 session->sources[i].cb_data))
227 sr_session_source_remove(session->sources[i].poll_object);
228 }
229 }
230 }
231
232 return SR_OK;
233}
234
235/**
236 * Start a session.
237 *
238 * There can only be one session at a time.
239 *
240 * @return SR_OK upon success, SR_ERR upon errors.
241 */
242SR_API int sr_session_start(void)
243{
244 struct sr_dev_inst *sdi;
245 GSList *l;
246 int ret;
247
248 if (!session) {
249 sr_err("session: %s: session was NULL; a session must be "
250 "created before starting it.", __func__);
251 return SR_ERR_BUG;
252 }
253
254 if (!session->devs) {
255 sr_err("session: %s: session->devs was NULL; a session "
256 "cannot be started without devices.", __func__);
257 return SR_ERR_BUG;
258 }
259
260 sr_info("session: starting");
261
262 for (l = session->devs; l; l = l->next) {
263 sdi = l->data;
264 if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) {
265 sr_err("session: %s: could not start an acquisition "
266 "(%d)", __func__, ret);
267 break;
268 }
269 }
270
271 /* TODO: What if there are multiple devices? Which return code? */
272
273 return ret;
274}
275
276/**
277 * Run the session.
278 *
279 * TODO: Various error checks etc.
280 *
281 * @return SR_OK upon success, SR_ERR_BUG upon errors.
282 */
283SR_API int sr_session_run(void)
284{
285 if (!session) {
286 sr_err("session: %s: session was NULL; a session must be "
287 "created first, before running it.", __func__);
288 return SR_ERR_BUG;
289 }
290
291 if (!session->devs) {
292 /* TODO: Actually the case? */
293 sr_err("session: %s: session->devs was NULL; a session "
294 "cannot be run without devices.", __func__);
295 return SR_ERR_BUG;
296 }
297
298 sr_info("session: running");
299
300 /* Do we have real sources? */
301 if (session->num_sources == 1 && session->pollfds[0].fd == -1) {
302 /* Dummy source, freewheel over it. */
303 while (session->num_sources)
304 session->sources[0].cb(-1, 0, session->sources[0].cb_data);
305 } else {
306 /* Real sources, use g_poll() main loop. */
307 sr_session_run_poll();
308 }
309
310 return SR_OK;
311}
312
313/**
314 * Halt the current session.
315 *
316 * This function is deprecated and should not be used in new code, use
317 * sr_session_stop() instead. The behaviour of this function is identical to
318 * sr_session_stop().
319 *
320 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
321 */
322SR_API int sr_session_halt(void)
323{
324 return sr_session_stop();
325}
326
327/**
328 * Stop the current session.
329 *
330 * The current session is stopped immediately, with all acquisition sessions
331 * being stopped and hardware drivers cleaned up.
332 *
333 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
334 */
335SR_API int sr_session_stop(void)
336{
337 struct sr_dev_inst *sdi;
338 GSList *l;
339
340 if (!session) {
341 sr_err("session: %s: session was NULL", __func__);
342 return SR_ERR_BUG;
343 }
344
345 sr_info("session: stopping");
346
347 for (l = session->devs; l; l = l->next) {
348 sdi = l->data;
349 if (sdi->driver) {
350 if (sdi->driver->dev_acquisition_stop)
351 sdi->driver->dev_acquisition_stop(sdi, sdi);
352 }
353 }
354
355 return SR_OK;
356}
357
358/**
359 * Debug helper.
360 *
361 * @param packet The packet to show debugging information for.
362 */
363static void datafeed_dump(struct sr_datafeed_packet *packet)
364{
365 struct sr_datafeed_logic *logic;
366 struct sr_datafeed_analog *analog;
367
368 switch (packet->type) {
369 case SR_DF_HEADER:
370 sr_dbg("bus: received SR_DF_HEADER");
371 break;
372 case SR_DF_TRIGGER:
373 sr_dbg("bus: received SR_DF_TRIGGER");
374 break;
375 case SR_DF_META_LOGIC:
376 sr_dbg("bus: received SR_DF_META_LOGIC");
377 break;
378 case SR_DF_LOGIC:
379 logic = packet->payload;
380 /* TODO: Check for logic != NULL. */
381 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
382 break;
383 case SR_DF_META_ANALOG:
384 sr_dbg("bus: received SR_DF_META_ANALOG");
385 break;
386 case SR_DF_ANALOG:
387 analog = packet->payload;
388 /* TODO: Check for analog != NULL. */
389 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
390 break;
391 case SR_DF_END:
392 sr_dbg("bus: received SR_DF_END");
393 break;
394 case SR_DF_FRAME_BEGIN:
395 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
396 break;
397 case SR_DF_FRAME_END:
398 sr_dbg("bus: received SR_DF_FRAME_END");
399 break;
400 default:
401 sr_dbg("bus: received unknown packet type %d", packet->type);
402 break;
403 }
404}
405
406/**
407 * Send a packet to whatever is listening on the datafeed bus.
408 *
409 * Hardware drivers use this to send a data packet to the frontend.
410 *
411 * @param dev TODO.
412 * @param packet The datafeed packet to send to the session bus.
413 *
414 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
415 *
416 * @private
417 */
418SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi,
419 struct sr_datafeed_packet *packet)
420{
421 GSList *l;
422 sr_datafeed_callback_t cb;
423
424 if (!sdi) {
425 sr_err("session: %s: sdi was NULL", __func__);
426 return SR_ERR_ARG;
427 }
428
429 if (!packet) {
430 sr_err("session: %s: packet was NULL", __func__);
431 return SR_ERR_ARG;
432 }
433
434 for (l = session->datafeed_callbacks; l; l = l->next) {
435 if (sr_log_loglevel_get() >= SR_LOG_DBG)
436 datafeed_dump(packet);
437 cb = l->data;
438 cb(sdi, packet);
439 }
440
441 return SR_OK;
442}
443
444static int _sr_session_source_add(GPollFD *pollfd, int timeout,
445 sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
446{
447 struct source *new_sources, *s;
448 GPollFD *new_pollfds;
449
450 if (!cb) {
451 sr_err("session: %s: cb was NULL", __func__);
452 return SR_ERR_ARG;
453 }
454
455 /* Note: cb_data can be NULL, that's not a bug. */
456
457 new_pollfds = g_try_realloc(session->pollfds,
458 sizeof(GPollFD) * (session->num_sources + 1));
459 if (!new_pollfds) {
460 sr_err("session: %s: new_pollfds malloc failed", __func__);
461 return SR_ERR_MALLOC;
462 }
463
464 new_sources = g_try_realloc(session->sources, sizeof(struct source) *
465 (session->num_sources + 1));
466 if (!new_sources) {
467 sr_err("session: %s: new_sources malloc failed", __func__);
468 return SR_ERR_MALLOC;
469 }
470
471 new_pollfds[session->num_sources] = *pollfd;
472 s = &new_sources[session->num_sources++];
473 s->timeout = timeout;
474 s->cb = cb;
475 s->cb_data = cb_data;
476 s->poll_object = poll_object;
477 session->pollfds = new_pollfds;
478 session->sources = new_sources;
479
480 if (timeout != session->source_timeout && timeout > 0
481 && (session->source_timeout == -1 || timeout < session->source_timeout))
482 session->source_timeout = timeout;
483
484 return SR_OK;
485}
486
487/**
488 * Add a event source for a file descriptor.
489 *
490 * @param fd The file descriptor.
491 * @param events Events to check for.
492 * @param timeout Max time to wait before the callback is called, ignored if 0.
493 * @param cb Callback function to add. Must not be NULL.
494 * @param cb_data Data for the callback function. Can be NULL.
495 *
496 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
497 * SR_ERR_MALLOC upon memory allocation errors.
498 */
499SR_API int sr_session_source_add(int fd, int events, int timeout,
500 sr_receive_data_callback_t cb, void *cb_data)
501{
502 GPollFD p;
503
504 p.fd = fd;
505 p.events = events;
506
507 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
508}
509
510/**
511 * Add an event source for a GPollFD.
512 *
513 * TODO: More error checks etc.
514 *
515 * @param pollfd The GPollFD.
516 * @param timeout Max time to wait before the callback is called, ignored if 0.
517 * @param cb Callback function to add. Must not be NULL.
518 * @param cb_data Data for the callback function. Can be NULL.
519 *
520 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
521 * SR_ERR_MALLOC upon memory allocation errors.
522 */
523SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
524 sr_receive_data_callback_t cb, void *cb_data)
525{
526 return _sr_session_source_add(pollfd, timeout, cb,
527 cb_data, (gintptr)pollfd);
528}
529
530/**
531 * Add an event source for a GIOChannel.
532 *
533 * TODO: More error checks etc.
534 *
535 * @param channel The GIOChannel.
536 * @param events Events to poll on.
537 * @param timeout Max time to wait before the callback is called, ignored if 0.
538 * @param cb Callback function to add. Must not be NULL.
539 * @param cb_data Data for the callback function. Can be NULL.
540 *
541 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
542 * SR_ERR_MALLOC upon memory allocation errors.
543 */
544SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
545 int timeout, sr_receive_data_callback_t cb, void *cb_data)
546{
547 GPollFD p;
548
549#ifdef _WIN32
550 g_io_channel_win32_make_pollfd(channel,
551 events, &p);
552#else
553 p.fd = g_io_channel_unix_get_fd(channel);
554 p.events = events;
555#endif
556
557 return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
558}
559
560
561static int _sr_session_source_remove(gintptr poll_object)
562{
563 struct source *new_sources;
564 GPollFD *new_pollfds;
565 unsigned int old;
566
567 if (!session->sources || !session->num_sources) {
568 sr_err("session: %s: sources was NULL", __func__);
569 return SR_ERR_BUG;
570 }
571
572 for (old = 0; old < session->num_sources; old++) {
573 if (session->sources[old].poll_object == poll_object)
574 break;
575 }
576
577 /* fd not found, nothing to do */
578 if (old == session->num_sources)
579 return SR_OK;
580
581 session->num_sources -= 1;
582
583 if (old != session->num_sources) {
584 memmove(&session->pollfds[old], &session->pollfds[old+1],
585 (session->num_sources - old) * sizeof(GPollFD));
586 memmove(&session->sources[old], &session->sources[old+1],
587 (session->num_sources - old) * sizeof(struct source));
588 }
589
590 new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
591 if (!new_pollfds && session->num_sources > 0) {
592 sr_err("session: %s: new_pollfds malloc failed", __func__);
593 return SR_ERR_MALLOC;
594 }
595
596 new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
597 if (!new_sources && session->num_sources > 0) {
598 sr_err("session: %s: new_sources malloc failed", __func__);
599 return SR_ERR_MALLOC;
600 }
601
602 session->pollfds = new_pollfds;
603 session->sources = new_sources;
604
605 return SR_OK;
606}
607
608/*
609 * Remove the source belonging to the specified file descriptor.
610 *
611 * TODO: More error checks.
612 *
613 * @param fd The file descriptor for which the source should be removed.
614 *
615 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
616 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
617 * internal errors.
618 */
619SR_API int sr_session_source_remove(int fd)
620{
621 return _sr_session_source_remove((gintptr)fd);
622}
623
624/**
625 * Remove the source belonging to the specified poll descriptor.
626 *
627 * TODO: More error checks.
628 *
629 * @param pollfd The poll descriptor for which the source should be removed.
630 *
631 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
632 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
633 * internal errors.
634 */
635SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
636{
637 return _sr_session_source_remove((gintptr)pollfd);
638}
639
640/*
641 * Remove the source belonging to the specified channel.
642 *
643 * TODO: More error checks.
644 *
645 * @param channel The channel for which the source should be removed.
646 *
647 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
648 * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
649 * internal errors.
650 */
651SR_API int sr_session_source_remove_channel(GIOChannel *channel)
652{
653 return _sr_session_source_remove((gintptr)channel);
654}