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