]> sigrok.org Git - libsigrokdecode.git/blob - session.c
Doxyfile: Update to a more recent Doxygen version.
[libsigrokdecode.git] / session.c
1 /*
2  * This file is part of the libsigrokdecode project.
3  *
4  * Copyright (C) 2010 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include "libsigrokdecode-internal.h" /* First, so we avoid a _POSIX_C_SOURCE warning. */
23 #include "libsigrokdecode.h"
24 #include <inttypes.h>
25 #include <glib.h>
26
27 /**
28  * @file
29  *
30  * Session handling.
31  */
32
33 /**
34  * @defgroup grp_session Session handling
35  *
36  * Starting and handling decoding sessions.
37  *
38  * @{
39  */
40
41 /** @cond PRIVATE */
42
43 SRD_PRIV GSList *sessions = NULL;
44 SRD_PRIV int max_session_id = -1;
45
46 /** @endcond */
47
48 /** @private */
49 SRD_PRIV int session_is_valid(struct srd_session *sess)
50 {
51         if (!sess || sess->session_id < 1)
52                 return SRD_ERR;
53
54         return SRD_OK;
55 }
56
57 /**
58  * Create a decoding session.
59  *
60  * A session holds all decoder instances, their stack relationships and
61  * output callbacks.
62  *
63  * @param sess A pointer which will hold a pointer to a newly
64  *             initialized session on return.
65  *
66  * @return SRD_OK upon success, a (negative) error code otherwise.
67  *
68  * @since 0.3.0
69  */
70 SRD_API int srd_session_new(struct srd_session **sess)
71 {
72         if (!sess) {
73                 srd_err("Invalid session pointer.");
74                 return SRD_ERR_ARG;
75         }
76
77         *sess = g_malloc(sizeof(struct srd_session));
78         (*sess)->session_id = ++max_session_id;
79         (*sess)->di_list = (*sess)->callbacks = NULL;
80
81         /* Keep a list of all sessions, so we can clean up as needed. */
82         sessions = g_slist_append(sessions, *sess);
83
84         srd_dbg("Created session %d.", (*sess)->session_id);
85
86         return SRD_OK;
87 }
88
89 /**
90  * Start a decoding session.
91  *
92  * Decoders, instances and stack must have been prepared beforehand,
93  * and all SRD_CONF parameters set.
94  *
95  * @param sess The session to start.
96  *
97  * @return SRD_OK upon success, a (negative) error code otherwise.
98  *
99  * @since 0.3.0
100  */
101 SRD_API int srd_session_start(struct srd_session *sess)
102 {
103         GSList *d;
104         struct srd_decoder_inst *di;
105         int ret;
106
107         if (session_is_valid(sess) != SRD_OK) {
108                 srd_err("Invalid session pointer.");
109                 return SRD_ERR;
110         }
111
112         srd_dbg("Calling start() on all instances in session %d.", sess->session_id);
113
114         /* Run the start() method on all decoders receiving frontend data. */
115         ret = SRD_OK;
116         for (d = sess->di_list; d; d = d->next) {
117                 di = d->data;
118                 if ((ret = srd_inst_start(di)) != SRD_OK)
119                         break;
120         }
121
122         return ret;
123 }
124
125 static int srd_inst_send_meta(struct srd_decoder_inst *di, int key,
126                 GVariant *data)
127 {
128         PyObject *py_ret;
129         GSList *l;
130         struct srd_decoder_inst *next_di;
131         int ret;
132         PyGILState_STATE gstate;
133
134         if (key != SRD_CONF_SAMPLERATE)
135                 /* This is the only key we pass on to the decoder for now. */
136                 return SRD_OK;
137
138         gstate = PyGILState_Ensure();
139
140         if (PyObject_HasAttrString(di->py_inst, "metadata")) {
141                 py_ret = PyObject_CallMethod(di->py_inst, "metadata", "lK",
142                                 (long)SRD_CONF_SAMPLERATE,
143                                 (unsigned long long)g_variant_get_uint64(data));
144                 Py_XDECREF(py_ret);
145         }
146
147         PyGILState_Release(gstate);
148
149         /* Push metadata to all the PDs stacked on top of this one. */
150         for (l = di->next_di; l; l = l->next) {
151                 next_di = l->data;
152                 if ((ret = srd_inst_send_meta(next_di, key, data)) != SRD_OK)
153                         return ret;
154         }
155
156         return SRD_OK;
157 }
158
159 /**
160  * Set a metadata configuration key in a session.
161  *
162  * @param sess The session to configure.
163  * @param key The configuration key (SRD_CONF_*).
164  * @param data The new value for the key, as a GVariant with GVariantType
165  *             appropriate to that key. A floating reference can be passed
166  *             in; its refcount will be sunk and unreferenced after use.
167  *
168  * @return SRD_OK upon success, a (negative) error code otherwise.
169  *
170  * @since 0.3.0
171  */
172 SRD_API int srd_session_metadata_set(struct srd_session *sess, int key,
173                 GVariant *data)
174 {
175         GSList *l;
176         int ret;
177
178         if (session_is_valid(sess) != SRD_OK) {
179                 srd_err("Invalid session.");
180                 return SRD_ERR_ARG;
181         }
182
183         if (!key) {
184                 srd_err("Invalid key.");
185                 return SRD_ERR_ARG;
186         }
187
188         if (!data) {
189                 srd_err("Invalid value.");
190                 return SRD_ERR_ARG;
191         }
192
193         /* Hardcoded to samplerate/uint64 for now. */
194
195         if (key != SRD_CONF_SAMPLERATE) {
196                 srd_err("Unknown config key %d.", key);
197                 return SRD_ERR_ARG;
198         }
199         if (!g_variant_is_of_type(data, G_VARIANT_TYPE_UINT64)) {
200                 srd_err("Invalid value type: expected uint64, got %s",
201                                 g_variant_get_type_string(data));
202                 return SRD_ERR_ARG;
203         }
204
205         srd_dbg("Setting session %d samplerate to %"G_GUINT64_FORMAT".",
206                         sess->session_id, g_variant_get_uint64(data));
207
208         ret = SRD_OK;
209         for (l = sess->di_list; l; l = l->next) {
210                 if ((ret = srd_inst_send_meta(l->data, key, data)) != SRD_OK)
211                         break;
212         }
213
214         g_variant_unref(data);
215
216         return ret;
217 }
218
219 /**
220  * Send a chunk of logic sample data to a running decoder session.
221  *
222  * If no channel map has been set up, the logic samples must be arranged
223  * in channel order, in the least amount of space possible. The default
224  * channel set consists of all required channels + all optional channels.
225  *
226  * The size of a sample in inbuf is 'unitsize' bytes. If no channel map
227  * has been configured, it is the minimum number of bytes needed to store
228  * the default channels.
229  *
230  * The calls to this function must provide the samples that shall be
231  * used by the protocol decoder
232  *  - in the correct order ([...]5, 6, 4, 7, 8[...] is a bug),
233  *  - starting from sample zero (2, 3, 4, 5, 6[...] is a bug),
234  *  - consecutively, with no gaps (0, 1, 2, 4, 5[...] is a bug).
235  *
236  * The start- and end-sample numbers are absolute sample numbers (relative
237  * to the start of the whole capture/file/stream), i.e. they are not relative
238  * sample numbers within the chunk specified by 'inbuf' and 'inbuflen'.
239  *
240  * Correct example (4096 samples total, 4 chunks @ 1024 samples each):
241  *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
242  *   srd_session_send(s, 1024, 2047, inbuf, 1024, 1);
243  *   srd_session_send(s, 2048, 3071, inbuf, 1024, 1);
244  *   srd_session_send(s, 3072, 4095, inbuf, 1024, 1);
245  *
246  * The chunk size ('inbuflen') can be arbitrary and can differ between calls.
247  *
248  * Correct example (4096 samples total, 7 chunks @ various samples each):
249  *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
250  *   srd_session_send(s, 1024, 1123, inbuf,  100, 1);
251  *   srd_session_send(s, 1124, 1423, inbuf,  300, 1);
252  *   srd_session_send(s, 1424, 1642, inbuf,  219, 1);
253  *   srd_session_send(s, 1643, 2047, inbuf,  405, 1);
254  *   srd_session_send(s, 2048, 3071, inbuf, 1024, 1);
255  *   srd_session_send(s, 3072, 4095, inbuf, 1024, 1);
256  *
257  * INCORRECT example (4096 samples total, 4 chunks @ 1024 samples each, but
258  * the start- and end-samplenumbers are not absolute):
259  *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
260  *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
261  *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
262  *   srd_session_send(s, 0,    1023, inbuf, 1024, 1);
263  *
264  * @param sess The session to use. Must not be NULL.
265  * @param abs_start_samplenum The absolute starting sample number for the
266  *              buffer's sample set, relative to the start of capture.
267  * @param abs_end_samplenum The absolute ending sample number for the
268  *              buffer's sample set, relative to the start of capture.
269  * @param inbuf Pointer to sample data. Must not be NULL.
270  * @param inbuflen Length in bytes of the buffer. Must be > 0.
271  * @param unitsize The number of bytes per sample. Must be > 0.
272  *
273  * @return SRD_OK upon success, a (negative) error code otherwise.
274  *
275  * @since 0.4.0
276  */
277 SRD_API int srd_session_send(struct srd_session *sess,
278                 uint64_t abs_start_samplenum, uint64_t abs_end_samplenum,
279                 const uint8_t *inbuf, uint64_t inbuflen, uint64_t unitsize)
280 {
281         GSList *d;
282         int ret;
283
284         if (session_is_valid(sess) != SRD_OK) {
285                 srd_err("Invalid session.");
286                 return SRD_ERR_ARG;
287         }
288
289         for (d = sess->di_list; d; d = d->next) {
290                 if ((ret = srd_inst_decode(d->data, abs_start_samplenum,
291                                 abs_end_samplenum, inbuf, inbuflen, unitsize)) != SRD_OK)
292                         return ret;
293         }
294
295         return SRD_OK;
296 }
297
298 /**
299  * Terminate currently executing decoders in a session, reset internal state.
300  *
301  * All decoder instances have their .wait() method terminated, which
302  * shall terminate .decode() as well. Afterwards the decoders' optional
303  * .reset() method gets executed.
304  *
305  * This routine allows callers to abort pending expensive operations,
306  * when they are no longer interested in the decoders' results. Note
307  * that the decoder state is lost and aborted work cannot resume.
308  *
309  * This routine also allows callers to re-use previously created decoder
310  * stacks to process new input data which is not related to previously
311  * processed input data. This avoids the necessity to re-construct the
312  * decoder stack.
313  *
314  * @param sess The session in which to terminate decoders.
315  * @return SRD_OK upon success, a (negative) error code otherwise.
316  *
317  * @since 0.6.0
318  */
319 SRD_API int srd_session_terminate_reset(struct srd_session *sess)
320 {
321         GSList *d;
322         int ret;
323
324         if (session_is_valid(sess) != SRD_OK) {
325                 srd_err("Invalid session.");
326                 return SRD_ERR_ARG;
327         }
328
329         for (d = sess->di_list; d; d = d->next) {
330                 ret = srd_inst_terminate_reset(d->data);
331                 if (ret != SRD_OK)
332                         return ret;
333         }
334
335         return SRD_OK;
336 }
337
338 /**
339  * Destroy a decoding session.
340  *
341  * All decoder instances and output callbacks are properly released.
342  *
343  * @param sess The session to be destroyed.
344  *
345  * @return SRD_OK upon success, a (negative) error code otherwise.
346  *
347  * @since 0.3.0
348  */
349 SRD_API int srd_session_destroy(struct srd_session *sess)
350 {
351         int session_id;
352
353         if (!sess) {
354                 srd_err("Invalid session.");
355                 return SRD_ERR_ARG;
356         }
357
358         session_id = sess->session_id;
359         if (sess->di_list)
360                 srd_inst_free_all(sess);
361         if (sess->callbacks)
362                 g_slist_free_full(sess->callbacks, g_free);
363         sessions = g_slist_remove(sessions, sess);
364         g_free(sess);
365
366         srd_dbg("Destroyed session %d.", session_id);
367
368         return SRD_OK;
369 }
370
371 /**
372  * Register/add a decoder output callback function.
373  *
374  * The function will be called when a protocol decoder sends output back
375  * to the PD controller (except for Python objects, which only go up the
376  * stack).
377  *
378  * @param sess The output session in which to register the callback.
379  * @param output_type The output type this callback will receive. Only one
380  *                    callback per output type can be registered.
381  * @param cb The function to call. Must not be NULL.
382  * @param cb_data Private data for the callback function. Can be NULL.
383  *
384  * @since 0.3.0
385  */
386 SRD_API int srd_pd_output_callback_add(struct srd_session *sess,
387                 int output_type, srd_pd_output_callback cb, void *cb_data)
388 {
389         struct srd_pd_callback *pd_cb;
390
391         if (session_is_valid(sess) != SRD_OK) {
392                 srd_err("Invalid session.");
393                 return SRD_ERR_ARG;
394         }
395
396         srd_dbg("Registering new callback for output type %d.", output_type);
397
398         pd_cb = g_malloc(sizeof(struct srd_pd_callback));
399         pd_cb->output_type = output_type;
400         pd_cb->cb = cb;
401         pd_cb->cb_data = cb_data;
402         sess->callbacks = g_slist_append(sess->callbacks, pd_cb);
403
404         return SRD_OK;
405 }
406
407 /** @private */
408 SRD_PRIV struct srd_pd_callback *srd_pd_output_callback_find(
409                 struct srd_session *sess, int output_type)
410 {
411         GSList *l;
412         struct srd_pd_callback *tmp, *pd_cb;
413
414         if (session_is_valid(sess) != SRD_OK) {
415                 srd_err("Invalid session.");
416                 return NULL;
417         }
418
419         pd_cb = NULL;
420         for (l = sess->callbacks; l; l = l->next) {
421                 tmp = l->data;
422                 if (tmp->output_type == output_type) {
423                         pd_cb = tmp;
424                         break;
425                 }
426         }
427
428         return pd_cb;
429 }
430
431 /** @} */