]> sigrok.org Git - libsigrok.git/blob - session.c
0cd4e2000387c8591900e9495a3cc6d1700802e9
[libsigrok.git] / session.c
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 struct 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'). */
41 struct 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  */
51 SR_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  */
70 SR_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
89 static void sr_dev_close(struct sr_dev *dev)
90 {
91         if (dev->driver->dev_close)
92                 dev->driver->dev_close(dev->driver_index);
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  */
103 SR_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 to add to the current session. Must not be NULL.
120  *            Also, dev->driver and dev->driver->dev_open must not be NULL.
121  *
122  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
123  */
124 SR_API int sr_session_dev_add(struct sr_dev *dev)
125 {
126         int ret;
127
128         if (!dev) {
129                 sr_err("session: %s: dev was NULL", __func__);
130                 return SR_ERR_ARG;
131         }
132
133         if (!session) {
134                 sr_err("session: %s: session was NULL", __func__);
135                 return SR_ERR_BUG;
136         }
137
138         /* If dev->driver is NULL, this is a virtual device. */
139         if (!dev->driver) {
140                 sr_dbg("session: %s: dev->driver was NULL, this seems to be "
141                        "a virtual device; continuing", __func__);
142                 /* Just add the device, don't run dev_open(). */
143                 session->devs = g_slist_append(session->devs, dev);
144                 return SR_OK;
145         }
146
147         /* dev->driver is non-NULL (i.e. we have a real device). */
148         if (!dev->driver->dev_open) {
149                 sr_err("session: %s: dev->driver->dev_open was NULL",
150                        __func__);
151                 return SR_ERR_BUG;
152         }
153
154         if ((ret = dev->driver->dev_open(dev->driver_index)) != 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, dev);
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  */
169 SR_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  */
190 SR_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
208 /**
209  * TODO.
210  */
211 static int sr_session_run_poll(void)
212 {
213         unsigned int i;
214         int ret;
215
216         while (session->running) {
217                 ret = g_poll(session->pollfds, session->num_sources, session->source_timeout);
218
219                 for (i = 0; i < session->num_sources; i++) {
220                         if (session->pollfds[i].revents > 0 || (ret == 0
221                                 && session->source_timeout == session->sources[i].timeout)) {
222                                 /*
223                                  * Invoke the source's callback on an event,
224                                  * or if the poll timeout out and this source
225                                  * asked for that timeout.
226                                  */
227                                 if (!session->sources[i].cb(session->pollfds[i].fd, session->pollfds[i].revents,
228                                                   session->sources[i].cb_data))
229                                         sr_session_source_remove(session->sources[i].poll_object);
230                         }
231                 }
232         }
233
234         return SR_OK;
235 }
236
237 /**
238  * Start a session.
239  *
240  * There can only be one session at a time.
241  *
242  * @return SR_OK upon success, SR_ERR upon errors.
243  */
244 SR_API int sr_session_start(void)
245 {
246         struct sr_dev *dev;
247         GSList *l;
248         int ret;
249
250         if (!session) {
251                 sr_err("session: %s: session was NULL; a session must be "
252                        "created first, before starting it.", __func__);
253                 return SR_ERR_BUG;
254         }
255
256         if (!session->devs) {
257                 /* TODO: Actually the case? */
258                 sr_err("session: %s: session->devs was NULL; a session "
259                        "cannot be started without devices.", __func__);
260                 return SR_ERR_BUG;
261         }
262
263         /* TODO: Check driver_index validity? */
264
265         sr_info("session: starting");
266
267         for (l = session->devs; l; l = l->next) {
268                 dev = l->data;
269                 /* TODO: Check for dev != NULL. */
270                 if ((ret = dev->driver->dev_acquisition_start(
271                                 dev->driver_index, dev)) != SR_OK) {
272                         sr_err("session: %s: could not start an acquisition "
273                                "(%d)", __func__, ret);
274                         break;
275                 }
276         }
277
278         /* TODO: What if there are multiple devices? Which return code? */
279
280         return ret;
281 }
282
283 /**
284  * Run the session.
285  *
286  * TODO: Various error checks etc.
287  *
288  * @return SR_OK upon success, SR_ERR_BUG upon errors.
289  */
290 SR_API int sr_session_run(void)
291 {
292         if (!session) {
293                 sr_err("session: %s: session was NULL; a session must be "
294                        "created first, before running it.", __func__);
295                 return SR_ERR_BUG;
296         }
297
298         if (!session->devs) {
299                 /* TODO: Actually the case? */
300                 sr_err("session: %s: session->devs was NULL; a session "
301                        "cannot be run without devices.", __func__);
302                 return SR_ERR_BUG;
303         }
304
305         sr_info("session: running");
306         session->running = TRUE;
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->running)
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 requests the current session be stopped as soon as possible, for
325  * example on receiving an SR_DF_END packet.
326  *
327  * @return SR_OK upon success, SR_ERR_BUG if no session exists.
328  */
329 SR_API int sr_session_halt(void)
330 {
331         if (!session) {
332                 sr_err("session: %s: session was NULL", __func__);
333                 return SR_ERR_BUG;
334         }
335
336         sr_info("session: halting");
337         session->running = FALSE;
338
339         return SR_OK;
340 }
341
342 /**
343  * Stop the current session.
344  *
345  * The current session is stopped immediately, with all acquisition sessions
346  * being stopped and hardware drivers cleaned up.
347  *
348  * @return SR_OK upon success, SR_ERR_BUG if no session exists.
349  */
350 SR_API int sr_session_stop(void)
351 {
352         struct sr_dev *dev;
353         GSList *l;
354
355         if (!session) {
356                 sr_err("session: %s: session was NULL", __func__);
357                 return SR_ERR_BUG;
358         }
359
360         sr_info("session: stopping");
361         session->running = FALSE;
362
363         for (l = session->devs; l; l = l->next) {
364                 dev = l->data;
365                 /* Check for dev != NULL. */
366                 if (dev->driver) {
367                         if (dev->driver->dev_acquisition_stop)
368                                 dev->driver->dev_acquisition_stop(dev->driver_index, dev);
369                         if (dev->driver->cleanup)
370                                 dev->driver->cleanup();
371                 }
372         }
373
374         return SR_OK;
375 }
376
377 /**
378  * Debug helper.
379  *
380  * @param packet The packet to show debugging information for.
381  */
382 static void datafeed_dump(struct sr_datafeed_packet *packet)
383 {
384         struct sr_datafeed_logic *logic;
385         struct sr_datafeed_analog *analog;
386
387         switch (packet->type) {
388         case SR_DF_HEADER:
389                 sr_dbg("bus: received SR_DF_HEADER");
390                 break;
391         case SR_DF_TRIGGER:
392                 sr_dbg("bus: received SR_DF_TRIGGER");
393                 break;
394         case SR_DF_META_LOGIC:
395                 sr_dbg("bus: received SR_DF_META_LOGIC");
396                 break;
397         case SR_DF_LOGIC:
398                 logic = packet->payload;
399                 /* TODO: Check for logic != NULL. */
400                 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
401                 break;
402         case SR_DF_META_ANALOG:
403                 sr_dbg("bus: received SR_DF_META_LOGIC");
404                 break;
405         case SR_DF_ANALOG:
406                 analog = packet->payload;
407                 /* TODO: Check for analog != NULL. */
408                 sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples);
409                 break;
410         case SR_DF_END:
411                 sr_dbg("bus: received SR_DF_END");
412                 break;
413         case SR_DF_FRAME_BEGIN:
414                 sr_dbg("bus: received SR_DF_FRAME_BEGIN");
415                 break;
416         case SR_DF_FRAME_END:
417                 sr_dbg("bus: received SR_DF_FRAME_END");
418                 break;
419         default:
420                 sr_dbg("bus: received unknown packet type %d", packet->type);
421                 break;
422         }
423 }
424
425 /**
426  * Send a packet to whatever is listening on the datafeed bus.
427  *
428  * Hardware drivers use this to send a data packet to the frontend.
429  *
430  * @param dev TODO.
431  * @param packet The datafeed packet to send to the session bus.
432  *
433  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
434  */
435 SR_PRIV int sr_session_send(struct sr_dev *dev,
436                             struct sr_datafeed_packet *packet)
437 {
438         GSList *l;
439         sr_datafeed_callback_t cb;
440
441         if (!dev) {
442                 sr_err("session: %s: dev was NULL", __func__);
443                 return SR_ERR_ARG;
444         }
445
446         if (!packet) {
447                 sr_err("session: %s: packet was NULL", __func__);
448                 return SR_ERR_ARG;
449         }
450
451         for (l = session->datafeed_callbacks; l; l = l->next) {
452                 if (sr_log_loglevel_get() >= SR_LOG_DBG)
453                         datafeed_dump(packet);
454                 cb = l->data;
455                 /* TODO: Check for cb != NULL. */
456                 cb(dev, packet);
457         }
458
459         return SR_OK;
460 }
461
462 static int _sr_session_source_add(GPollFD *pollfd, int timeout,
463         sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object)
464 {
465         struct source *new_sources, *s;
466         GPollFD *new_pollfds;
467
468         if (!cb) {
469                 sr_err("session: %s: cb was NULL", __func__);
470                 return SR_ERR_ARG;
471         }
472
473         /* Note: cb_data can be NULL, that's not a bug. */
474
475         new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * (session->num_sources + 1));
476         if (!new_pollfds) {
477                 sr_err("session: %s: new_pollfds malloc failed", __func__);
478                 return SR_ERR_MALLOC;
479         }
480
481         new_sources = g_try_realloc(session->sources, sizeof(struct source) *
482         (session->num_sources + 1));
483         if (!new_sources) {
484                 sr_err("session: %s: new_sources malloc failed", __func__);
485                 return SR_ERR_MALLOC;
486         }
487
488         new_pollfds[session->num_sources] = *pollfd;
489         s = &new_sources[session->num_sources++];
490         s->timeout = timeout;
491         s->cb = cb;
492         s->cb_data = cb_data;
493         s->poll_object = poll_object;
494         session->pollfds = new_pollfds;
495         session->sources = new_sources;
496
497         if (timeout != session->source_timeout && timeout > 0
498             && (session->source_timeout == -1 || timeout < session->source_timeout))
499                 session->source_timeout = timeout;
500
501         return SR_OK;
502 }
503
504 /**
505  * Add a event source for a file descriptor.
506  *
507  * @param fd The file descriptor.
508  * @param events Events to check for.
509  * @param timeout Max time to wait before the callback is called, ignored if 0.
510  * @param cb Callback function to add. Must not be NULL.
511  * @param cb_data Data for the callback function. Can be NULL.
512  *
513  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
514  *         SR_ERR_MALLOC upon memory allocation errors.
515  */
516 SR_API int sr_session_source_add(int fd, int events, int timeout,
517                 sr_receive_data_callback_t cb, void *cb_data)
518 {
519         GPollFD p;
520
521         p.fd = fd;
522         p.events = events;
523
524         return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd);
525 }
526
527 /**
528  * Add an event source for a GPollFD.
529  *
530  * TODO: More error checks etc.
531  *
532  * @param pollfd The GPollFD.
533  * @param timeout Max time to wait before the callback is called, ignored if 0.
534  * @param cb Callback function to add. Must not be NULL.
535  * @param cb_data Data for the callback function. Can be NULL.
536  *
537  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
538  *         SR_ERR_MALLOC upon memory allocation errors.
539  */
540 SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout,
541                 sr_receive_data_callback_t cb, void *cb_data)
542 {
543         return _sr_session_source_add(pollfd, timeout, cb,
544                                       cb_data, (gintptr)pollfd);
545 }
546
547 /**
548  * Add an event source for a GIOChannel.
549  *
550  * TODO: More error checks etc.
551  *
552  * @param channel The GIOChannel.
553  * @param events Events to poll on.
554  * @param timeout Max time to wait before the callback is called, ignored if 0.
555  * @param cb Callback function to add. Must not be NULL.
556  * @param cb_data Data for the callback function. Can be NULL.
557  *
558  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
559  *         SR_ERR_MALLOC upon memory allocation errors.
560  */
561 SR_API int sr_session_source_add_channel(GIOChannel *channel, int events,
562                 int timeout, sr_receive_data_callback_t cb, void *cb_data)
563 {
564         GPollFD p;
565
566 #ifdef _WIN32
567         g_io_channel_win32_make_pollfd(channel,
568                         events, &p);
569 #else
570         p.fd = g_io_channel_unix_get_fd(channel);
571         p.events = events;
572 #endif
573
574         return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel);
575 }
576
577
578 static int _sr_session_source_remove(gintptr poll_object)
579 {
580         struct source *new_sources;
581         GPollFD *new_pollfds;
582         unsigned int old;
583
584         if (!session->sources || !session->num_sources) {
585                 sr_err("session: %s: sources was NULL", __func__);
586                 return SR_ERR_BUG;
587         }
588
589         for (old = 0; old < session->num_sources; old++) {
590                 if (session->sources[old].poll_object == poll_object)
591                         break;
592         }
593
594         /* fd not found, nothing to do */
595         if (old == session->num_sources)
596                 return SR_OK;
597
598         session->num_sources -= 1;
599
600         if (old != session->num_sources) {
601                 memmove(&session->pollfds[old], &session->pollfds[old+1],
602                         (session->num_sources - old) * sizeof(GPollFD));
603                 memmove(&session->sources[old], &session->sources[old+1],
604                         (session->num_sources - old) * sizeof(struct source));
605         }
606
607         new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources);
608         if (!new_pollfds && session->num_sources > 0) {
609                 sr_err("session: %s: new_pollfds malloc failed", __func__);
610                 return SR_ERR_MALLOC;
611         }
612
613         new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources);
614         if (!new_sources && session->num_sources > 0) {
615                 sr_err("session: %s: new_sources malloc failed", __func__);
616                 return SR_ERR_MALLOC;
617         }
618
619         session->pollfds = new_pollfds;
620         session->sources = new_sources;
621
622         return SR_OK;
623 }
624
625 /*
626  * Remove the source belonging to the specified file descriptor.
627  *
628  * TODO: More error checks.
629  *
630  * @param fd The file descriptor for which the source should be removed.
631  *
632  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
633  *         SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
634  *         internal errors.
635  */
636 SR_API int sr_session_source_remove(int fd)
637 {
638         return _sr_session_source_remove((gintptr)fd);
639 }
640
641 /**
642  * Remove the source belonging to the specified poll descriptor.
643  *
644  * TODO: More error checks.
645  *
646  * @param pollfd The poll descriptor for which the source should be removed.
647  *
648  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
649  *         SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
650  *         internal errors.
651  */
652 SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd)
653 {
654         return _sr_session_source_remove((gintptr)pollfd);
655 }
656
657 /*
658  * Remove the source belonging to the specified channel.
659  *
660  * TODO: More error checks.
661  *
662  * @param channel The channel for which the source should be removed.
663  *
664  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
665  *         SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
666  *         internal errors.
667  */
668 SR_API int sr_session_source_remove_channel(GIOChannel *channel)
669 {
670         return _sr_session_source_remove((gintptr)channel);
671 }