]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
c2bf5506 | 5 | * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com> |
a1bb33af UH |
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 | ||
6ec6c43b | 21 | #include <config.h> |
32af282c | 22 | #include <errno.h> |
a1bb33af UH |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
26 | #include <string.h> | |
544a4582 | 27 | #include <glib.h> |
c1aae900 | 28 | #include <libsigrok/libsigrok.h> |
45c59c8b | 29 | #include "libsigrok-internal.h" |
aa4b1107 | 30 | |
2ad1deb8 | 31 | /** @cond PRIVATE */ |
3544f848 | 32 | #define LOG_PREFIX "session" |
2ad1deb8 | 33 | /** @endcond */ |
a421dc1d | 34 | |
393fb9cb UH |
35 | /** |
36 | * @file | |
37 | * | |
38 | * Creating, using, or destroying libsigrok sessions. | |
39 | */ | |
40 | ||
7b870c38 UH |
41 | /** |
42 | * @defgroup grp_session Session handling | |
43 | * | |
44 | * Creating, using, or destroying libsigrok sessions. | |
45 | * | |
46 | * @{ | |
47 | */ | |
48 | ||
c2bf5506 DE |
49 | struct datafeed_callback { |
50 | sr_datafeed_callback cb; | |
1f9813eb | 51 | void *cb_data; |
c2bf5506 | 52 | }; |
aac0ea25 | 53 | |
c2bf5506 | 54 | /** Custom GLib event source for generic descriptor I/O. |
5de0fc55 | 55 | * @see https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html |
c2bf5506 DE |
56 | * @internal |
57 | */ | |
58 | struct fd_source { | |
59 | GSource base; | |
62d7945f | 60 | |
c2bf5506 DE |
61 | int64_t timeout_us; |
62 | int64_t due_us; | |
92248e78 | 63 | |
c2bf5506 DE |
64 | /* Meta-data needed to keep track of installed sources */ |
65 | struct sr_session *session; | |
66 | void *key; | |
544a4582 | 67 | |
c2bf5506 | 68 | GPollFD pollfd; |
2726474a ML |
69 | }; |
70 | ||
c2bf5506 | 71 | /** FD event source prepare() method. |
5de0fc55 | 72 | * This is called immediately before poll(). |
c2bf5506 DE |
73 | */ |
74 | static gboolean fd_source_prepare(GSource *source, int *timeout) | |
75 | { | |
76 | int64_t now_us; | |
77 | struct fd_source *fsource; | |
78 | int remaining_ms; | |
79 | ||
80 | fsource = (struct fd_source *)source; | |
81 | ||
82 | if (fsource->timeout_us >= 0) { | |
83 | now_us = g_source_get_time(source); | |
84 | ||
85 | if (fsource->due_us == 0) { | |
86 | /* First-time initialization of the expiration time */ | |
87 | fsource->due_us = now_us + fsource->timeout_us; | |
88 | } | |
89 | remaining_ms = (MAX(0, fsource->due_us - now_us) + 999) / 1000; | |
90 | } else { | |
91 | remaining_ms = -1; | |
92 | } | |
93 | *timeout = remaining_ms; | |
94 | ||
95 | return (remaining_ms == 0); | |
96 | } | |
97 | ||
98 | /** FD event source check() method. | |
5de0fc55 | 99 | * This is called after poll() returns to check whether an event fired. |
c2bf5506 DE |
100 | */ |
101 | static gboolean fd_source_check(GSource *source) | |
102 | { | |
103 | struct fd_source *fsource; | |
104 | unsigned int revents; | |
105 | ||
106 | fsource = (struct fd_source *)source; | |
107 | revents = fsource->pollfd.revents; | |
108 | ||
109 | return (revents != 0 || (fsource->timeout_us >= 0 | |
110 | && fsource->due_us <= g_source_get_time(source))); | |
111 | } | |
112 | ||
113 | /** FD event source dispatch() method. | |
5de0fc55 | 114 | * This is called if either prepare() or check() returned TRUE. |
c2bf5506 DE |
115 | */ |
116 | static gboolean fd_source_dispatch(GSource *source, | |
117 | GSourceFunc callback, void *user_data) | |
118 | { | |
119 | struct fd_source *fsource; | |
c2bf5506 DE |
120 | unsigned int revents; |
121 | gboolean keep; | |
122 | ||
123 | fsource = (struct fd_source *)source; | |
c2bf5506 DE |
124 | revents = fsource->pollfd.revents; |
125 | ||
c2bf5506 DE |
126 | if (!callback) { |
127 | sr_err("Callback not set, cannot dispatch event."); | |
128 | return G_SOURCE_REMOVE; | |
129 | } | |
130 | keep = (*(sr_receive_data_callback)callback) | |
131 | (fsource->pollfd.fd, revents, user_data); | |
132 | ||
133 | if (fsource->timeout_us >= 0 && G_LIKELY(keep) | |
134 | && G_LIKELY(!g_source_is_destroyed(source))) | |
135 | fsource->due_us = g_source_get_time(source) | |
136 | + fsource->timeout_us; | |
137 | return keep; | |
138 | } | |
139 | ||
140 | /** FD event source finalize() method. | |
141 | */ | |
142 | static void fd_source_finalize(GSource *source) | |
143 | { | |
144 | struct fd_source *fsource; | |
145 | ||
146 | fsource = (struct fd_source *)source; | |
147 | ||
148 | sr_dbg("%s: key %p", __func__, fsource->key); | |
149 | ||
150 | sr_session_source_destroyed(fsource->session, fsource->key, source); | |
151 | } | |
152 | ||
153 | /** Create an event source for I/O on a file descriptor. | |
154 | * | |
155 | * In order to maintain API compatibility, this event source also doubles | |
156 | * as a timer event source. | |
157 | * | |
158 | * @param session The session the event source belongs to. | |
159 | * @param key The key used to identify this source. | |
160 | * @param fd The file descriptor or HANDLE. | |
161 | * @param timeout_ms The timeout interval in ms, or -1 to wait indefinitely. | |
162 | * @return A new event source object, or NULL on failure. | |
163 | */ | |
164 | static GSource *fd_source_new(struct sr_session *session, void *key, | |
165 | gintptr fd, int events, int timeout_ms) | |
166 | { | |
167 | static GSourceFuncs fd_source_funcs = { | |
168 | .prepare = &fd_source_prepare, | |
169 | .check = &fd_source_check, | |
170 | .dispatch = &fd_source_dispatch, | |
171 | .finalize = &fd_source_finalize | |
172 | }; | |
173 | GSource *source; | |
174 | struct fd_source *fsource; | |
175 | ||
176 | source = g_source_new(&fd_source_funcs, sizeof(struct fd_source)); | |
177 | fsource = (struct fd_source *)source; | |
178 | ||
179 | g_source_set_name(source, (fd < 0) ? "timer" : "fd"); | |
180 | ||
181 | if (timeout_ms >= 0) { | |
182 | fsource->timeout_us = 1000 * (int64_t)timeout_ms; | |
183 | fsource->due_us = 0; | |
184 | } else { | |
185 | fsource->timeout_us = -1; | |
186 | fsource->due_us = INT64_MAX; | |
187 | } | |
188 | fsource->session = session; | |
189 | fsource->key = key; | |
190 | ||
191 | fsource->pollfd.fd = fd; | |
192 | fsource->pollfd.events = events; | |
193 | fsource->pollfd.revents = 0; | |
194 | ||
195 | if (fd >= 0) | |
196 | g_source_add_poll(source, &fsource->pollfd); | |
197 | ||
198 | return source; | |
199 | } | |
200 | ||
9f45fb3a UH |
201 | /** |
202 | * Create a new session. | |
7efe889e | 203 | * |
61e6e2da | 204 | * @param ctx The context in which to create the new session. |
7efe889e UH |
205 | * @param new_session This will contain a pointer to the newly created |
206 | * session if the return value is SR_OK, otherwise the value | |
207 | * is undefined and should not be used. Must not be NULL. | |
9f45fb3a | 208 | * |
0812c40e | 209 | * @retval SR_OK Success. |
41de54ff | 210 | * @retval SR_ERR_ARG Invalid argument. |
47117241 | 211 | * |
0812c40e | 212 | * @since 0.4.0 |
9f45fb3a | 213 | */ |
61e6e2da ML |
214 | SR_API int sr_session_new(struct sr_context *ctx, |
215 | struct sr_session **new_session) | |
a1bb33af | 216 | { |
3337e9a1 | 217 | struct sr_session *session; |
a1bb33af | 218 | |
41de54ff UH |
219 | if (!new_session) |
220 | return SR_ERR_ARG; | |
221 | ||
3337e9a1 | 222 | session = g_malloc0(sizeof(struct sr_session)); |
b7e94111 | 223 | |
4ed5d21d | 224 | session->ctx = ctx; |
faa5d7d9 | 225 | |
c2bf5506 | 226 | g_mutex_init(&session->main_mutex); |
faa5d7d9 | 227 | |
c2bf5506 DE |
228 | /* To maintain API compatibility, we need a lookup table |
229 | * which maps poll_object IDs to GSource* pointers. | |
230 | */ | |
231 | session->event_sources = g_hash_table_new(NULL, NULL); | |
0812c40e | 232 | |
3337e9a1 | 233 | *new_session = session; |
0812c40e ML |
234 | |
235 | return SR_OK; | |
a1bb33af UH |
236 | } |
237 | ||
9f45fb3a | 238 | /** |
0812c40e | 239 | * Destroy a session. |
9f45fb3a UH |
240 | * This frees up all memory used by the session. |
241 | * | |
7efe889e UH |
242 | * @param session The session to destroy. Must not be NULL. |
243 | * | |
04cb9157 | 244 | * @retval SR_OK Success. |
0812c40e | 245 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 246 | * |
0812c40e | 247 | * @since 0.4.0 |
9f45fb3a | 248 | */ |
0812c40e | 249 | SR_API int sr_session_destroy(struct sr_session *session) |
a1bb33af | 250 | { |
9f45fb3a | 251 | if (!session) { |
a421dc1d | 252 | sr_err("%s: session was NULL", __func__); |
0812c40e | 253 | return SR_ERR_ARG; |
9f45fb3a UH |
254 | } |
255 | ||
0812c40e | 256 | sr_session_dev_remove_all(session); |
1de3cced ML |
257 | g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free); |
258 | ||
c2bf5506 DE |
259 | g_hash_table_unref(session->event_sources); |
260 | ||
261 | g_mutex_clear(&session->main_mutex); | |
faa5d7d9 | 262 | |
a1bb33af | 263 | g_free(session); |
0812c40e | 264 | |
e0508e67 | 265 | return SR_OK; |
a1bb33af UH |
266 | } |
267 | ||
9f45fb3a | 268 | /** |
0812c40e | 269 | * Remove all the devices from a session. |
9f45fb3a UH |
270 | * |
271 | * The session itself (i.e., the struct sr_session) is not free'd and still | |
272 | * exists after this function returns. | |
273 | * | |
7efe889e UH |
274 | * @param session The session to use. Must not be NULL. |
275 | * | |
04cb9157 | 276 | * @retval SR_OK Success. |
0812c40e | 277 | * @retval SR_ERR_BUG Invalid session passed. |
47117241 | 278 | * |
0812c40e | 279 | * @since 0.4.0 |
9f45fb3a | 280 | */ |
0812c40e | 281 | SR_API int sr_session_dev_remove_all(struct sr_session *session) |
a1bb33af | 282 | { |
0812c40e ML |
283 | struct sr_dev_inst *sdi; |
284 | GSList *l; | |
285 | ||
9f45fb3a | 286 | if (!session) { |
a421dc1d | 287 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
288 | return SR_ERR_ARG; |
289 | } | |
290 | ||
291 | for (l = session->devs; l; l = l->next) { | |
292 | sdi = (struct sr_dev_inst *) l->data; | |
293 | sdi->session = NULL; | |
9f45fb3a UH |
294 | } |
295 | ||
681803df | 296 | g_slist_free(session->devs); |
bb7ef793 | 297 | session->devs = NULL; |
e0508e67 UH |
298 | |
299 | return SR_OK; | |
a1bb33af UH |
300 | } |
301 | ||
9f45fb3a | 302 | /** |
0812c40e | 303 | * Add a device instance to a session. |
9f45fb3a | 304 | * |
7efe889e | 305 | * @param session The session to add to. Must not be NULL. |
0812c40e | 306 | * @param sdi The device instance to add to a session. Must not |
de4d3f99 BV |
307 | * be NULL. Also, sdi->driver and sdi->driver->dev_open must |
308 | * not be NULL. | |
9f45fb3a | 309 | * |
04cb9157 MH |
310 | * @retval SR_OK Success. |
311 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 | 312 | * |
0812c40e | 313 | * @since 0.4.0 |
9f45fb3a | 314 | */ |
0812c40e ML |
315 | SR_API int sr_session_dev_add(struct sr_session *session, |
316 | struct sr_dev_inst *sdi) | |
a1bb33af | 317 | { |
5451816f | 318 | int ret; |
a1bb33af | 319 | |
de4d3f99 | 320 | if (!sdi) { |
a421dc1d | 321 | sr_err("%s: sdi was NULL", __func__); |
9f45fb3a UH |
322 | return SR_ERR_ARG; |
323 | } | |
324 | ||
d6eb0c33 | 325 | if (!session) { |
a421dc1d | 326 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
327 | return SR_ERR_ARG; |
328 | } | |
329 | ||
330 | /* If sdi->session is not NULL, the device is already in this or | |
331 | * another session. */ | |
332 | if (sdi->session) { | |
333 | sr_err("%s: already assigned to session", __func__); | |
334 | return SR_ERR_ARG; | |
d6eb0c33 UH |
335 | } |
336 | ||
de4d3f99 BV |
337 | /* If sdi->driver is NULL, this is a virtual device. */ |
338 | if (!sdi->driver) { | |
d6eb0c33 | 339 | /* Just add the device, don't run dev_open(). */ |
5de0fc55 | 340 | session->devs = g_slist_append(session->devs, sdi); |
0812c40e | 341 | sdi->session = session; |
d6eb0c33 | 342 | return SR_OK; |
9f45fb3a UH |
343 | } |
344 | ||
de4d3f99 BV |
345 | /* sdi->driver is non-NULL (i.e. we have a real device). */ |
346 | if (!sdi->driver->dev_open) { | |
a421dc1d | 347 | sr_err("%s: sdi->driver->dev_open was NULL", __func__); |
8ec95d22 | 348 | return SR_ERR_BUG; |
9f45fb3a UH |
349 | } |
350 | ||
5de0fc55 | 351 | session->devs = g_slist_append(session->devs, sdi); |
0812c40e | 352 | sdi->session = session; |
aa4b1107 | 353 | |
5de0fc55 DE |
354 | /* TODO: This is invalid if the session runs in a different thread. |
355 | * The usage semantics and restrictions need to be documented. | |
356 | */ | |
5451816f | 357 | if (session->running) { |
32b7cd4f DE |
358 | /* Adding a device to a running session. Commit settings |
359 | * and start acquisition on that device now. */ | |
360 | if ((ret = sr_config_commit(sdi)) != SR_OK) { | |
361 | sr_err("Failed to commit device settings before " | |
362 | "starting acquisition in running session (%s)", | |
363 | sr_strerror(ret)); | |
364 | return ret; | |
365 | } | |
5451816f | 366 | if ((ret = sdi->driver->dev_acquisition_start(sdi, |
5de0fc55 | 367 | sdi)) != SR_OK) { |
5451816f | 368 | sr_err("Failed to start acquisition of device in " |
32b7cd4f DE |
369 | "running session (%s)", sr_strerror(ret)); |
370 | return ret; | |
371 | } | |
5451816f BV |
372 | } |
373 | ||
e46b8fb1 | 374 | return SR_OK; |
a1bb33af UH |
375 | } |
376 | ||
2bb311b4 | 377 | /** |
0812c40e | 378 | * List all device instances attached to a session. |
2bb311b4 | 379 | * |
7efe889e | 380 | * @param session The session to use. Must not be NULL. |
2bb311b4 BV |
381 | * @param devlist A pointer where the device instance list will be |
382 | * stored on return. If no devices are in the session, | |
383 | * this will be NULL. Each element in the list points | |
384 | * to a struct sr_dev_inst *. | |
385 | * The list must be freed by the caller, but not the | |
386 | * elements pointed to. | |
387 | * | |
04cb9157 | 388 | * @retval SR_OK Success. |
0812c40e | 389 | * @retval SR_ERR_ARG Invalid argument. |
47117241 | 390 | * |
0812c40e | 391 | * @since 0.4.0 |
2bb311b4 | 392 | */ |
0812c40e | 393 | SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist) |
2bb311b4 | 394 | { |
2bb311b4 | 395 | if (!session) |
0812c40e ML |
396 | return SR_ERR_ARG; |
397 | ||
398 | if (!devlist) | |
399 | return SR_ERR_ARG; | |
2bb311b4 BV |
400 | |
401 | *devlist = g_slist_copy(session->devs); | |
402 | ||
403 | return SR_OK; | |
404 | } | |
405 | ||
9f45fb3a | 406 | /** |
0812c40e | 407 | * Remove all datafeed callbacks in a session. |
9f45fb3a | 408 | * |
7efe889e UH |
409 | * @param session The session to use. Must not be NULL. |
410 | * | |
04cb9157 | 411 | * @retval SR_OK Success. |
0812c40e | 412 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 413 | * |
0812c40e | 414 | * @since 0.4.0 |
9f45fb3a | 415 | */ |
0812c40e | 416 | SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session) |
a1bb33af | 417 | { |
9f45fb3a | 418 | if (!session) { |
a421dc1d | 419 | sr_err("%s: session was NULL", __func__); |
0812c40e | 420 | return SR_ERR_ARG; |
9f45fb3a UH |
421 | } |
422 | ||
2726474a | 423 | g_slist_free_full(session->datafeed_callbacks, g_free); |
a1bb33af | 424 | session->datafeed_callbacks = NULL; |
e0508e67 UH |
425 | |
426 | return SR_OK; | |
a1bb33af UH |
427 | } |
428 | ||
9f45fb3a | 429 | /** |
0812c40e | 430 | * Add a datafeed callback to a session. |
9f45fb3a | 431 | * |
7efe889e | 432 | * @param session The session to use. Must not be NULL. |
d08490aa | 433 | * @param cb Function to call when a chunk of data is received. |
0abee507 | 434 | * Must not be NULL. |
85222791 | 435 | * @param cb_data Opaque pointer passed in by the caller. |
a1645fcd | 436 | * |
04cb9157 MH |
437 | * @retval SR_OK Success. |
438 | * @retval SR_ERR_BUG No session exists. | |
47117241 UH |
439 | * |
440 | * @since 0.3.0 | |
9f45fb3a | 441 | */ |
0812c40e ML |
442 | SR_API int sr_session_datafeed_callback_add(struct sr_session *session, |
443 | sr_datafeed_callback cb, void *cb_data) | |
a1bb33af | 444 | { |
2726474a ML |
445 | struct datafeed_callback *cb_struct; |
446 | ||
9f45fb3a | 447 | if (!session) { |
a421dc1d | 448 | sr_err("%s: session was NULL", __func__); |
e0508e67 | 449 | return SR_ERR_BUG; |
9f45fb3a UH |
450 | } |
451 | ||
0abee507 | 452 | if (!cb) { |
a421dc1d | 453 | sr_err("%s: cb was NULL", __func__); |
0abee507 UH |
454 | return SR_ERR_ARG; |
455 | } | |
9f45fb3a | 456 | |
91219afc | 457 | cb_struct = g_malloc0(sizeof(struct datafeed_callback)); |
2726474a ML |
458 | cb_struct->cb = cb; |
459 | cb_struct->cb_data = cb_data; | |
460 | ||
62c82025 | 461 | session->datafeed_callbacks = |
2726474a | 462 | g_slist_append(session->datafeed_callbacks, cb_struct); |
e0508e67 UH |
463 | |
464 | return SR_OK; | |
a1bb33af UH |
465 | } |
466 | ||
9f42e2e6 UH |
467 | /** |
468 | * Get the trigger assigned to this session. | |
469 | * | |
470 | * @param session The session to use. | |
471 | * | |
472 | * @retval NULL Invalid (NULL) session was passed to the function. | |
473 | * @retval other The trigger assigned to this session (can be NULL). | |
474 | * | |
475 | * @since 0.4.0 | |
476 | */ | |
0812c40e | 477 | SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session) |
7b5e6d29 | 478 | { |
9f42e2e6 UH |
479 | if (!session) |
480 | return NULL; | |
481 | ||
7b5e6d29 BV |
482 | return session->trigger; |
483 | } | |
484 | ||
9f42e2e6 UH |
485 | /** |
486 | * Set the trigger of this session. | |
487 | * | |
488 | * @param session The session to use. Must not be NULL. | |
489 | * @param trig The trigger to assign to this session. Can be NULL. | |
490 | * | |
491 | * @retval SR_OK Success. | |
492 | * @retval SR_ERR_ARG Invalid argument. | |
493 | * | |
494 | * @since 0.4.0 | |
495 | */ | |
0812c40e | 496 | SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig) |
7b5e6d29 | 497 | { |
9f42e2e6 UH |
498 | if (!session) |
499 | return SR_ERR_ARG; | |
500 | ||
7b5e6d29 BV |
501 | session->trigger = trig; |
502 | ||
503 | return SR_OK; | |
504 | } | |
505 | ||
7b5e6d29 BV |
506 | static int verify_trigger(struct sr_trigger *trigger) |
507 | { | |
508 | struct sr_trigger_stage *stage; | |
509 | struct sr_trigger_match *match; | |
510 | GSList *l, *m; | |
511 | ||
512 | if (!trigger->stages) { | |
513 | sr_err("No trigger stages defined."); | |
514 | return SR_ERR; | |
515 | } | |
516 | ||
517 | sr_spew("Checking trigger:"); | |
518 | for (l = trigger->stages; l; l = l->next) { | |
519 | stage = l->data; | |
520 | if (!stage->matches) { | |
521 | sr_err("Stage %d has no matches defined.", stage->stage); | |
522 | return SR_ERR; | |
523 | } | |
524 | for (m = stage->matches; m; m = m->next) { | |
525 | match = m->data; | |
526 | if (!match->channel) { | |
527 | sr_err("Stage %d match has no channel.", stage->stage); | |
528 | return SR_ERR; | |
529 | } | |
530 | if (!match->match) { | |
531 | sr_err("Stage %d match is not defined.", stage->stage); | |
532 | return SR_ERR; | |
533 | } | |
534 | sr_spew("Stage %d match on channel %s, match %d", stage->stage, | |
535 | match->channel->name, match->match); | |
536 | } | |
537 | } | |
538 | ||
539 | return SR_OK; | |
540 | } | |
1beccaed | 541 | |
c2bf5506 DE |
542 | /** Set up the main context the session will be executing in. |
543 | * | |
544 | * Must be called just before the session starts, by the thread which | |
545 | * will execute the session main loop. Once acquired, the main context | |
546 | * pointer is immutable for the duration of the session run. | |
547 | */ | |
548 | static int set_main_context(struct sr_session *session) | |
549 | { | |
2e5e3df4 DE |
550 | GMainContext *main_context; |
551 | ||
552 | g_mutex_lock(&session->main_mutex); | |
c2bf5506 | 553 | |
5de0fc55 DE |
554 | /* May happen if sr_session_start() is called a second time |
555 | * while the session is still running. | |
c2bf5506 | 556 | */ |
5de0fc55 DE |
557 | if (session->main_context) { |
558 | sr_err("Main context already set."); | |
2e5e3df4 DE |
559 | |
560 | g_mutex_unlock(&session->main_mutex); | |
c2bf5506 DE |
561 | return SR_ERR; |
562 | } | |
2e5e3df4 | 563 | main_context = g_main_context_ref_thread_default(); |
c2bf5506 DE |
564 | /* |
565 | * Try to use an existing main context if possible, but only if we | |
566 | * can make it owned by the current thread. Otherwise, create our | |
567 | * own main context so that event source callbacks can execute in | |
568 | * the session thread. | |
569 | */ | |
2e5e3df4 DE |
570 | if (g_main_context_acquire(main_context)) { |
571 | g_main_context_release(main_context); | |
c2bf5506 DE |
572 | |
573 | sr_dbg("Using thread-default main context."); | |
c2bf5506 | 574 | } else { |
2e5e3df4 | 575 | g_main_context_unref(main_context); |
c2bf5506 | 576 | |
2e5e3df4 DE |
577 | sr_dbg("Creating our own main context."); |
578 | main_context = g_main_context_new(); | |
c2bf5506 | 579 | } |
2e5e3df4 DE |
580 | session->main_context = main_context; |
581 | ||
c2bf5506 DE |
582 | g_mutex_unlock(&session->main_mutex); |
583 | ||
584 | return SR_OK; | |
585 | } | |
586 | ||
587 | /** Unset the main context used for the current session run. | |
588 | * | |
589 | * Must be called right after stopping the session. Note that if the | |
590 | * session is stopped asynchronously, the main loop may still be running | |
591 | * after the main context has been unset. This is OK as long as no new | |
592 | * event sources are created -- the main loop holds its own reference | |
593 | * to the main context. | |
594 | */ | |
595 | static int unset_main_context(struct sr_session *session) | |
596 | { | |
597 | int ret; | |
598 | ||
599 | g_mutex_lock(&session->main_mutex); | |
600 | ||
601 | if (session->main_context) { | |
2e5e3df4 | 602 | g_main_context_unref(session->main_context); |
c2bf5506 DE |
603 | session->main_context = NULL; |
604 | ret = SR_OK; | |
605 | } else { | |
606 | /* May happen if the set/unset calls are not matched. | |
607 | */ | |
608 | sr_err("No main context to unset."); | |
609 | ret = SR_ERR; | |
610 | } | |
611 | g_mutex_unlock(&session->main_mutex); | |
612 | ||
613 | return ret; | |
614 | } | |
615 | ||
5de0fc55 DE |
616 | static unsigned int session_source_attach(struct sr_session *session, |
617 | GSource *source) | |
618 | { | |
619 | unsigned int id = 0; | |
620 | ||
621 | g_mutex_lock(&session->main_mutex); | |
622 | ||
623 | if (session->main_context) | |
624 | id = g_source_attach(source, session->main_context); | |
625 | else | |
626 | sr_err("Cannot add event source without main context."); | |
627 | ||
628 | g_mutex_unlock(&session->main_mutex); | |
629 | ||
630 | return id; | |
631 | } | |
632 | ||
633 | /* Idle handler; invoked when the number of registered event sources | |
634 | * for a running session drops to zero. | |
635 | */ | |
636 | static gboolean delayed_stop_check(void *data) | |
637 | { | |
638 | struct sr_session *session; | |
639 | ||
640 | session = data; | |
641 | session->stop_check_id = 0; | |
642 | ||
643 | /* Session already ended? */ | |
644 | if (!session->running) | |
645 | return G_SOURCE_REMOVE; | |
646 | ||
647 | /* New event sources may have been installed in the meantime. */ | |
648 | if (g_hash_table_size(session->event_sources) != 0) | |
649 | return G_SOURCE_REMOVE; | |
650 | ||
651 | session->running = FALSE; | |
652 | unset_main_context(session); | |
653 | ||
654 | sr_info("Stopped."); | |
655 | ||
656 | /* This indicates a bug in user code, since it is not valid to | |
657 | * restart or destroy a session while it may still be running. | |
658 | */ | |
659 | if (!session->main_loop && !session->stopped_callback) { | |
660 | sr_err("BUG: Session stop left unhandled."); | |
661 | return G_SOURCE_REMOVE; | |
662 | } | |
663 | if (session->main_loop) | |
664 | g_main_loop_quit(session->main_loop); | |
665 | ||
666 | if (session->stopped_callback) | |
667 | (*session->stopped_callback)(session->stopped_cb_data); | |
668 | ||
669 | return G_SOURCE_REMOVE; | |
670 | } | |
671 | ||
672 | static int stop_check_later(struct sr_session *session) | |
673 | { | |
674 | GSource *source; | |
675 | unsigned int source_id; | |
676 | ||
677 | if (session->stop_check_id != 0) | |
678 | return SR_OK; /* idle handler already installed */ | |
679 | ||
680 | source = g_idle_source_new(); | |
681 | g_source_set_callback(source, &delayed_stop_check, session, NULL); | |
682 | ||
683 | source_id = session_source_attach(session, source); | |
684 | session->stop_check_id = source_id; | |
685 | ||
686 | g_source_unref(source); | |
687 | ||
688 | return (source_id != 0) ? SR_OK : SR_ERR; | |
689 | } | |
690 | ||
9f45fb3a UH |
691 | /** |
692 | * Start a session. | |
693 | * | |
5de0fc55 DE |
694 | * When this function returns with a status code indicating success, the |
695 | * session is running. Use sr_session_stopped_callback_set() to receive | |
696 | * notification upon completion, or call sr_session_run() to block until | |
697 | * the session stops. | |
698 | * | |
699 | * Session events will be processed in the context of the current thread. | |
700 | * If a thread-default GLib main context has been set, and is not owned by | |
701 | * any other thread, it will be used. Otherwise, libsigrok will create its | |
702 | * own main context for the current thread. | |
703 | * | |
7efe889e UH |
704 | * @param session The session to use. Must not be NULL. |
705 | * | |
04cb9157 | 706 | * @retval SR_OK Success. |
0812c40e | 707 | * @retval SR_ERR_ARG Invalid session passed. |
5de0fc55 | 708 | * @retval SR_ERR Other error. |
47117241 | 709 | * |
0812c40e | 710 | * @since 0.4.0 |
9f45fb3a | 711 | */ |
0812c40e | 712 | SR_API int sr_session_start(struct sr_session *session) |
7d658874 | 713 | { |
de4d3f99 | 714 | struct sr_dev_inst *sdi; |
013ec84b | 715 | struct sr_channel *ch; |
5de0fc55 DE |
716 | GSList *l, *c, *lend; |
717 | int ret; | |
7d658874 | 718 | |
9f45fb3a | 719 | if (!session) { |
0812c40e ML |
720 | sr_err("%s: session was NULL", __func__); |
721 | return SR_ERR_ARG; | |
9f45fb3a UH |
722 | } |
723 | ||
bb7ef793 | 724 | if (!session->devs) { |
a421dc1d | 725 | sr_err("%s: session->devs was NULL; a session " |
9f45fb3a | 726 | "cannot be started without devices.", __func__); |
0812c40e | 727 | return SR_ERR_ARG; |
9f45fb3a UH |
728 | } |
729 | ||
5de0fc55 DE |
730 | if (session->running) { |
731 | sr_err("Cannot (re-)start session while it is still running."); | |
7b5e6d29 | 732 | return SR_ERR; |
5de0fc55 | 733 | } |
7b5e6d29 | 734 | |
5de0fc55 DE |
735 | if (session->trigger) { |
736 | ret = verify_trigger(session->trigger); | |
737 | if (ret != SR_OK) | |
738 | return ret; | |
739 | } | |
9f45fb3a | 740 | |
5de0fc55 | 741 | /* Check enabled channels and commit settings of all devices. */ |
bb7ef793 | 742 | for (l = session->devs; l; l = l->next) { |
de4d3f99 | 743 | sdi = l->data; |
013ec84b BV |
744 | for (c = sdi->channels; c; c = c->next) { |
745 | ch = c->data; | |
5de0fc55 | 746 | if (ch->enabled) |
013ec84b | 747 | break; |
013ec84b | 748 | } |
5de0fc55 DE |
749 | if (!c) { |
750 | sr_err("%s device %s has no enabled channels.", | |
751 | sdi->driver->name, sdi->connection_id); | |
752 | return SR_ERR; | |
013ec84b BV |
753 | } |
754 | ||
5de0fc55 DE |
755 | ret = sr_config_commit(sdi); |
756 | if (ret != SR_OK) { | |
757 | sr_err("Failed to commit %s device %s settings " | |
758 | "before starting acquisition.", | |
759 | sdi->driver->name, sdi->connection_id); | |
760 | return ret; | |
32b7cd4f | 761 | } |
5de0fc55 DE |
762 | } |
763 | ||
764 | ret = set_main_context(session); | |
765 | if (ret != SR_OK) | |
766 | return ret; | |
767 | ||
768 | sr_info("Starting."); | |
769 | ||
770 | session->running = TRUE; | |
771 | ||
772 | /* Have all devices start acquisition. */ | |
773 | for (l = session->devs; l; l = l->next) { | |
774 | sdi = l->data; | |
775 | ret = sdi->driver->dev_acquisition_start(sdi, sdi); | |
776 | if (ret != SR_OK) { | |
777 | sr_err("Could not start %s device %s acquisition.", | |
778 | sdi->driver->name, sdi->connection_id); | |
7d658874 | 779 | break; |
9f45fb3a | 780 | } |
7d658874 BV |
781 | } |
782 | ||
c2bf5506 | 783 | if (ret != SR_OK) { |
5de0fc55 DE |
784 | /* If there are multiple devices, some of them may already have |
785 | * started successfully. Stop them now before returning. */ | |
786 | lend = l->next; | |
787 | for (l = session->devs; l != lend; l = l->next) { | |
788 | sdi = l->data; | |
789 | if (sdi->driver->dev_acquisition_stop) | |
790 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
791 | } | |
792 | /* TODO: Handle delayed stops. Need to iterate the event | |
793 | * sources... */ | |
c2bf5506 | 794 | session->running = FALSE; |
5de0fc55 DE |
795 | |
796 | unset_main_context(session); | |
797 | return ret; | |
c2bf5506 | 798 | } |
9f45fb3a | 799 | |
5de0fc55 DE |
800 | if (g_hash_table_size(session->event_sources) == 0) |
801 | stop_check_later(session); | |
802 | ||
803 | return SR_OK; | |
7d658874 BV |
804 | } |
805 | ||
9f45fb3a | 806 | /** |
5de0fc55 DE |
807 | * Block until the running session stops. |
808 | * | |
809 | * This is a convenience function which creates a GLib main loop and runs | |
810 | * it to process session events until the session stops. | |
811 | * | |
812 | * Instead of using this function, applications may run their own GLib main | |
813 | * loop, and use sr_session_stopped_callback_set() to receive notification | |
814 | * when the session finished running. | |
9f45fb3a | 815 | * |
7efe889e UH |
816 | * @param session The session to use. Must not be NULL. |
817 | * | |
04cb9157 | 818 | * @retval SR_OK Success. |
0812c40e | 819 | * @retval SR_ERR_ARG Invalid session passed. |
5de0fc55 | 820 | * @retval SR_ERR Other error. |
47117241 | 821 | * |
0812c40e | 822 | * @since 0.4.0 |
9f45fb3a | 823 | */ |
0812c40e | 824 | SR_API int sr_session_run(struct sr_session *session) |
7d658874 | 825 | { |
9f45fb3a | 826 | if (!session) { |
0812c40e ML |
827 | sr_err("%s: session was NULL", __func__); |
828 | return SR_ERR_ARG; | |
9f45fb3a | 829 | } |
5de0fc55 DE |
830 | if (!session->running) { |
831 | sr_err("No session running."); | |
832 | return SR_ERR; | |
9f45fb3a | 833 | } |
c2bf5506 DE |
834 | if (session->main_loop) { |
835 | sr_err("Main loop already created."); | |
836 | return SR_ERR; | |
837 | } | |
9f45fb3a | 838 | |
c2bf5506 | 839 | g_mutex_lock(&session->main_mutex); |
5de0fc55 | 840 | |
c2bf5506 DE |
841 | if (!session->main_context) { |
842 | sr_err("Cannot run without main context."); | |
843 | g_mutex_unlock(&session->main_mutex); | |
844 | return SR_ERR; | |
845 | } | |
c2bf5506 | 846 | session->main_loop = g_main_loop_new(session->main_context, FALSE); |
5de0fc55 | 847 | |
c2bf5506 DE |
848 | g_mutex_unlock(&session->main_mutex); |
849 | ||
850 | g_main_loop_run(session->main_loop); | |
851 | ||
852 | g_main_loop_unref(session->main_loop); | |
853 | session->main_loop = NULL; | |
854 | ||
e0508e67 | 855 | return SR_OK; |
7d658874 BV |
856 | } |
857 | ||
c2bf5506 | 858 | static gboolean session_stop_sync(void *user_data) |
a1bb33af | 859 | { |
c2bf5506 | 860 | struct sr_session *session; |
de4d3f99 | 861 | struct sr_dev_inst *sdi; |
c2bf5506 | 862 | GSList *node; |
a1bb33af | 863 | |
c2bf5506 DE |
864 | session = user_data; |
865 | ||
866 | if (!session->running) | |
867 | return G_SOURCE_REMOVE; | |
9f45fb3a | 868 | |
a421dc1d | 869 | sr_info("Stopping."); |
e0508e67 | 870 | |
c2bf5506 DE |
871 | for (node = session->devs; node; node = node->next) { |
872 | sdi = node->data; | |
873 | if (sdi->driver && sdi->driver->dev_acquisition_stop) | |
874 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
a1bb33af | 875 | } |
9f45fb3a | 876 | |
c2bf5506 | 877 | return G_SOURCE_REMOVE; |
a1bb33af UH |
878 | } |
879 | ||
33c6e4c5 | 880 | /** |
0812c40e | 881 | * Stop a session. |
33c6e4c5 | 882 | * |
5de0fc55 DE |
883 | * This requests the drivers of each device participating in the session to |
884 | * abort the acquisition as soon as possible. Even after this function returns, | |
885 | * event processing still continues until all devices have actually stopped. | |
886 | * | |
887 | * Use sr_session_stopped_callback_set() to receive notification when the event | |
888 | * processing finished. | |
33c6e4c5 | 889 | * |
5de0fc55 DE |
890 | * This function is reentrant. That is, it may be called from a different |
891 | * thread than the one executing the session, as long as it can be ensured | |
892 | * that the session object is valid. | |
893 | * | |
894 | * If the session is not running, sr_session_stop() silently does nothing. | |
33c6e4c5 | 895 | * |
7efe889e UH |
896 | * @param session The session to use. Must not be NULL. |
897 | * | |
04cb9157 | 898 | * @retval SR_OK Success. |
0812c40e | 899 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 900 | * |
0812c40e | 901 | * @since 0.4.0 |
33c6e4c5 | 902 | */ |
0812c40e | 903 | SR_API int sr_session_stop(struct sr_session *session) |
33c6e4c5 | 904 | { |
5de0fc55 DE |
905 | GMainContext *main_context; |
906 | ||
33c6e4c5 AG |
907 | if (!session) { |
908 | sr_err("%s: session was NULL", __func__); | |
c2bf5506 | 909 | return SR_ERR_ARG; |
33c6e4c5 | 910 | } |
5de0fc55 | 911 | |
c2bf5506 | 912 | g_mutex_lock(&session->main_mutex); |
33c6e4c5 | 913 | |
5de0fc55 DE |
914 | main_context = (session->main_context) |
915 | ? g_main_context_ref(session->main_context) | |
916 | : NULL; | |
917 | ||
c2bf5506 | 918 | g_mutex_unlock(&session->main_mutex); |
33c6e4c5 | 919 | |
5de0fc55 DE |
920 | if (!main_context) { |
921 | sr_dbg("No main context set; already stopped?"); | |
922 | /* Not an error; as it would be racy. */ | |
923 | return SR_OK; | |
924 | } | |
925 | g_main_context_invoke(main_context, &session_stop_sync, session); | |
926 | g_main_context_unref(main_context); | |
927 | ||
928 | return SR_OK; | |
929 | } | |
930 | ||
931 | /** | |
932 | * Return whether the session is currently running. | |
933 | * | |
934 | * Note that this function should be called from the same thread | |
935 | * the session was started in. | |
936 | * | |
937 | * @param session The session to use. Must not be NULL. | |
938 | * | |
939 | * @retval TRUE Session is running. | |
940 | * @retval FALSE Session is not running. | |
941 | * @retval SR_ERR_ARG Invalid session passed. | |
942 | * | |
943 | * @since 0.4.0 | |
944 | */ | |
945 | SR_API int sr_session_is_running(struct sr_session *session) | |
946 | { | |
947 | if (!session) { | |
948 | sr_err("%s: session was NULL", __func__); | |
949 | return SR_ERR_ARG; | |
950 | } | |
951 | return session->running; | |
952 | } | |
953 | ||
954 | /** | |
955 | * Set the callback to be invoked after a session stopped running. | |
956 | * | |
957 | * Install a callback to receive notification when a session run stopped. | |
958 | * This can be used to integrate session execution with an existing main | |
959 | * loop, without having to block in sr_session_run(). | |
960 | * | |
961 | * Note that the callback will be invoked in the context of the thread | |
962 | * that calls sr_session_start(). | |
963 | * | |
964 | * @param session The session to use. Must not be NULL. | |
965 | * @param cb The callback to invoke on session stop. May be NULL to unset. | |
966 | * @param cb_data User data pointer to be passed to the callback. | |
967 | * | |
968 | * @retval SR_OK Success. | |
969 | * @retval SR_ERR_ARG Invalid session passed. | |
970 | * | |
971 | * @since 0.4.0 | |
972 | */ | |
973 | SR_API int sr_session_stopped_callback_set(struct sr_session *session, | |
974 | sr_session_stopped_callback cb, void *cb_data) | |
975 | { | |
976 | if (!session) { | |
977 | sr_err("%s: session was NULL", __func__); | |
978 | return SR_ERR_ARG; | |
979 | } | |
980 | session->stopped_callback = cb; | |
981 | session->stopped_cb_data = cb_data; | |
982 | ||
983 | return SR_OK; | |
33c6e4c5 AG |
984 | } |
985 | ||
9f45fb3a | 986 | /** |
a1645fcd | 987 | * Debug helper. |
9f45fb3a | 988 | * |
996b0c72 | 989 | * @param packet The packet to show debugging information for. |
9f45fb3a | 990 | */ |
bf53457d | 991 | static void datafeed_dump(const struct sr_datafeed_packet *packet) |
7d2afd6c | 992 | { |
bf53457d | 993 | const struct sr_datafeed_logic *logic; |
5faebab2 | 994 | const struct sr_datafeed_analog_old *analog_old; |
edb691fc | 995 | const struct sr_datafeed_analog *analog; |
7d2afd6c | 996 | |
ca7dbb56 | 997 | /* Please use the same order as in libsigrok.h. */ |
7d2afd6c BV |
998 | switch (packet->type) { |
999 | case SR_DF_HEADER: | |
a421dc1d | 1000 | sr_dbg("bus: Received SR_DF_HEADER packet."); |
7d2afd6c | 1001 | break; |
55c9f09d UH |
1002 | case SR_DF_END: |
1003 | sr_dbg("bus: Received SR_DF_END packet."); | |
7d2afd6c | 1004 | break; |
c71bac3b | 1005 | case SR_DF_META: |
a421dc1d | 1006 | sr_dbg("bus: Received SR_DF_META packet."); |
ee7489d2 | 1007 | break; |
55c9f09d UH |
1008 | case SR_DF_TRIGGER: |
1009 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); | |
1010 | break; | |
7d2afd6c BV |
1011 | case SR_DF_LOGIC: |
1012 | logic = packet->payload; | |
7ea45862 UH |
1013 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " |
1014 | "unitsize = %d).", logic->length, logic->unitsize); | |
7d2afd6c | 1015 | break; |
5faebab2 UH |
1016 | case SR_DF_ANALOG_OLD: |
1017 | analog_old = packet->payload; | |
1018 | sr_dbg("bus: Received SR_DF_ANALOG_OLD packet (%d samples).", | |
1019 | analog_old->num_samples); | |
ee7489d2 | 1020 | break; |
6ea7669c | 1021 | case SR_DF_FRAME_BEGIN: |
a421dc1d | 1022 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); |
6ea7669c BV |
1023 | break; |
1024 | case SR_DF_FRAME_END: | |
a421dc1d | 1025 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); |
6ea7669c | 1026 | break; |
edb691fc UH |
1027 | case SR_DF_ANALOG: |
1028 | analog = packet->payload; | |
1029 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", | |
1030 | analog->num_samples); | |
55c9f09d | 1031 | break; |
7d2afd6c | 1032 | default: |
a421dc1d | 1033 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); |
9f45fb3a | 1034 | break; |
7d2afd6c | 1035 | } |
7d2afd6c BV |
1036 | } |
1037 | ||
9f45fb3a | 1038 | /** |
a1645fcd BV |
1039 | * Send a packet to whatever is listening on the datafeed bus. |
1040 | * | |
1041 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 1042 | * |
6b2d8d3e | 1043 | * @param sdi TODO. |
31ccebc4 | 1044 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 1045 | * |
04cb9157 MH |
1046 | * @retval SR_OK Success. |
1047 | * @retval SR_ERR_ARG Invalid argument. | |
b4bd7088 UH |
1048 | * |
1049 | * @private | |
9f45fb3a | 1050 | */ |
de4d3f99 | 1051 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
8143cfdc | 1052 | const struct sr_datafeed_packet *packet) |
a1bb33af UH |
1053 | { |
1054 | GSList *l; | |
2726474a | 1055 | struct datafeed_callback *cb_struct; |
c0a1e532 UH |
1056 | struct sr_datafeed_packet *packet_in, *packet_out; |
1057 | struct sr_transform *t; | |
1058 | int ret; | |
a1bb33af | 1059 | |
de4d3f99 | 1060 | if (!sdi) { |
a421dc1d | 1061 | sr_err("%s: sdi was NULL", __func__); |
e0508e67 | 1062 | return SR_ERR_ARG; |
9f45fb3a UH |
1063 | } |
1064 | ||
e0508e67 | 1065 | if (!packet) { |
a421dc1d | 1066 | sr_err("%s: packet was NULL", __func__); |
e0508e67 | 1067 | return SR_ERR_ARG; |
9f45fb3a UH |
1068 | } |
1069 | ||
79f92686 BV |
1070 | if (!sdi->session) { |
1071 | sr_err("%s: session was NULL", __func__); | |
1072 | return SR_ERR_BUG; | |
1073 | } | |
1074 | ||
5faebab2 | 1075 | if (packet->type == SR_DF_ANALOG_OLD) { |
edb691fc | 1076 | /* Convert to SR_DF_ANALOG. */ |
5faebab2 | 1077 | const struct sr_datafeed_analog_old *analog_old = packet->payload; |
ca79993b ML |
1078 | struct sr_analog_encoding encoding; |
1079 | struct sr_analog_meaning meaning; | |
1080 | struct sr_analog_spec spec; | |
edb691fc UH |
1081 | struct sr_datafeed_analog analog; |
1082 | struct sr_datafeed_packet new_packet; | |
1083 | new_packet.type = SR_DF_ANALOG; | |
1084 | new_packet.payload = &analog; | |
1085 | analog.data = analog_old->data; | |
1086 | analog.num_samples = analog_old->num_samples; | |
1087 | analog.encoding = &encoding; | |
1088 | analog.meaning = &meaning; | |
1089 | analog.spec = &spec; | |
ca79993b ML |
1090 | encoding.unitsize = sizeof(float); |
1091 | encoding.is_signed = TRUE; | |
1092 | encoding.is_float = TRUE; | |
1093 | #ifdef WORDS_BIGENDIAN | |
1094 | encoding.is_bigendian = TRUE; | |
1095 | #else | |
1096 | encoding.is_bigendian = FALSE; | |
1097 | #endif | |
1098 | encoding.digits = 0; | |
1099 | encoding.is_digits_decimal = FALSE; | |
1100 | encoding.scale.p = 1; | |
1101 | encoding.scale.q = 1; | |
1102 | encoding.offset.p = 0; | |
1103 | encoding.offset.q = 1; | |
5faebab2 UH |
1104 | meaning.mq = analog_old->mq; |
1105 | meaning.unit = analog_old->unit; | |
1106 | meaning.mqflags = analog_old->mqflags; | |
1107 | meaning.channels = analog_old->channels; | |
ca79993b | 1108 | spec.spec_digits = 0; |
edb691fc | 1109 | return sr_session_send(sdi, &new_packet); |
ca79993b ML |
1110 | } |
1111 | ||
c0a1e532 UH |
1112 | /* |
1113 | * Pass the packet to the first transform module. If that returns | |
1114 | * another packet (instead of NULL), pass that packet to the next | |
1115 | * transform module in the list, and so on. | |
1116 | */ | |
1117 | packet_in = (struct sr_datafeed_packet *)packet; | |
1118 | for (l = sdi->session->transforms; l; l = l->next) { | |
1119 | t = l->data; | |
1120 | sr_spew("Running transform module '%s'.", t->module->id); | |
1121 | ret = t->module->receive(t, packet_in, &packet_out); | |
1122 | if (ret < 0) { | |
1123 | sr_err("Error while running transform module: %d.", ret); | |
1124 | return SR_ERR; | |
1125 | } | |
1126 | if (!packet_out) { | |
1127 | /* | |
1128 | * If any of the transforms don't return an output | |
1129 | * packet, abort. | |
1130 | */ | |
1131 | sr_spew("Transform module didn't return a packet, aborting."); | |
1132 | return SR_OK; | |
1133 | } else { | |
1134 | /* | |
1135 | * Use this transform module's output packet as input | |
1136 | * for the next transform module. | |
1137 | */ | |
1138 | packet_in = packet_out; | |
1139 | } | |
1140 | } | |
4ec436c4 | 1141 | packet = packet_in; |
c0a1e532 UH |
1142 | |
1143 | /* | |
1144 | * If the last transform did output a packet, pass it to all datafeed | |
1145 | * callbacks. | |
1146 | */ | |
3337e9a1 | 1147 | for (l = sdi->session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
1148 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
1149 | datafeed_dump(packet); | |
2726474a ML |
1150 | cb_struct = l->data; |
1151 | cb_struct->cb(sdi, packet, cb_struct->cb_data); | |
a1bb33af | 1152 | } |
9f45fb3a | 1153 | |
e0508e67 | 1154 | return SR_OK; |
a1bb33af UH |
1155 | } |
1156 | ||
6b2d8d3e UH |
1157 | /** |
1158 | * Add an event source for a file descriptor. | |
1159 | * | |
7efe889e | 1160 | * @param session The session to use. Must not be NULL. |
c2bf5506 DE |
1161 | * @param key The key which identifies the event source. |
1162 | * @param source An event source object. Must not be NULL. | |
ee9953ef | 1163 | * |
04cb9157 MH |
1164 | * @retval SR_OK Success. |
1165 | * @retval SR_ERR_ARG Invalid argument. | |
c2bf5506 DE |
1166 | * @retval SR_ERR_BUG Event source with @a key already installed. |
1167 | * @retval SR_ERR Other error. | |
ee9953ef DE |
1168 | * |
1169 | * @private | |
6b2d8d3e | 1170 | */ |
62d7945f | 1171 | SR_PRIV int sr_session_source_add_internal(struct sr_session *session, |
c2bf5506 | 1172 | void *key, GSource *source) |
544a4582 | 1173 | { |
c2bf5506 DE |
1174 | /* |
1175 | * This must not ever happen, since the source has already been | |
1176 | * created and its finalize() method will remove the key for the | |
1177 | * already installed source. (Well it would, if we did not have | |
1178 | * another sanity check there.) | |
92248e78 | 1179 | */ |
c2bf5506 DE |
1180 | if (g_hash_table_contains(session->event_sources, key)) { |
1181 | sr_err("Event source with key %p already exists.", key); | |
1182 | return SR_ERR_BUG; | |
92248e78 | 1183 | } |
c2bf5506 DE |
1184 | g_hash_table_insert(session->event_sources, key, source); |
1185 | ||
5de0fc55 DE |
1186 | if (session_source_attach(session, source) == 0) |
1187 | return SR_ERR; | |
faa5d7d9 | 1188 | |
5de0fc55 | 1189 | return SR_OK; |
534b634c DE |
1190 | } |
1191 | ||
cbc1413f DE |
1192 | SR_PRIV int sr_session_fd_source_add(struct sr_session *session, |
1193 | void *key, gintptr fd, int events, int timeout, | |
c2bf5506 | 1194 | sr_receive_data_callback cb, void *cb_data) |
534b634c | 1195 | { |
c2bf5506 DE |
1196 | GSource *source; |
1197 | int ret; | |
534b634c | 1198 | |
c2bf5506 DE |
1199 | source = fd_source_new(session, key, fd, events, timeout); |
1200 | if (!source) | |
534b634c | 1201 | return SR_ERR; |
534b634c | 1202 | |
c2bf5506 | 1203 | g_source_set_callback(source, (GSourceFunc)cb, cb_data, NULL); |
534b634c | 1204 | |
c2bf5506 DE |
1205 | ret = sr_session_source_add_internal(session, key, source); |
1206 | g_source_unref(source); | |
544a4582 | 1207 | |
c2bf5506 | 1208 | return ret; |
544a4582 BV |
1209 | } |
1210 | ||
9f45fb3a | 1211 | /** |
6b2d8d3e | 1212 | * Add an event source for a file descriptor. |
9f45fb3a | 1213 | * |
7efe889e | 1214 | * @param session The session to use. Must not be NULL. |
534b634c | 1215 | * @param fd The file descriptor, or a negative value to create a timer source. |
aac0ea25 | 1216 | * @param events Events to check for. |
faa5d7d9 DE |
1217 | * @param timeout Max time in ms to wait before the callback is called, |
1218 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1219 | * @param cb Callback function to add. Must not be NULL. |
1220 | * @param cb_data Data for the callback function. Can be NULL. | |
9f45fb3a | 1221 | * |
04cb9157 MH |
1222 | * @retval SR_OK Success. |
1223 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1224 | * |
1225 | * @since 0.3.0 | |
ee9953ef | 1226 | * @private |
aac0ea25 | 1227 | */ |
ee9953ef | 1228 | SR_PRIV int sr_session_source_add(struct sr_session *session, int fd, |
0812c40e | 1229 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) |
aac0ea25 | 1230 | { |
faa5d7d9 | 1231 | if (fd < 0 && timeout < 0) { |
c2bf5506 | 1232 | sr_err("Cannot create timer source without timeout."); |
faa5d7d9 DE |
1233 | return SR_ERR_ARG; |
1234 | } | |
cbc1413f | 1235 | return sr_session_fd_source_add(session, GINT_TO_POINTER(fd), |
c2bf5506 | 1236 | fd, events, timeout, cb, cb_data); |
aac0ea25 LPC |
1237 | } |
1238 | ||
1239 | /** | |
1a895c61 | 1240 | * Add an event source for a GPollFD. |
aac0ea25 | 1241 | * |
7efe889e | 1242 | * @param session The session to use. Must not be NULL. |
faa5d7d9 DE |
1243 | * @param pollfd The GPollFD. Must not be NULL. |
1244 | * @param timeout Max time in ms to wait before the callback is called, | |
1245 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1246 | * @param cb Callback function to add. Must not be NULL. |
1247 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 1248 | * |
04cb9157 MH |
1249 | * @retval SR_OK Success. |
1250 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1251 | * |
1252 | * @since 0.3.0 | |
ee9953ef | 1253 | * @private |
9f45fb3a | 1254 | */ |
ee9953ef | 1255 | SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session, |
0812c40e ML |
1256 | GPollFD *pollfd, int timeout, sr_receive_data_callback cb, |
1257 | void *cb_data) | |
aac0ea25 | 1258 | { |
faa5d7d9 DE |
1259 | if (!pollfd) { |
1260 | sr_err("%s: pollfd was NULL", __func__); | |
1261 | return SR_ERR_ARG; | |
1262 | } | |
cbc1413f | 1263 | return sr_session_fd_source_add(session, pollfd, pollfd->fd, |
c2bf5506 | 1264 | pollfd->events, timeout, cb, cb_data); |
aac0ea25 LPC |
1265 | } |
1266 | ||
1267 | /** | |
1a895c61 | 1268 | * Add an event source for a GIOChannel. |
aac0ea25 | 1269 | * |
7efe889e | 1270 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
1271 | * @param channel The GIOChannel. |
1272 | * @param events Events to poll on. | |
faa5d7d9 DE |
1273 | * @param timeout Max time in ms to wait before the callback is called, |
1274 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1275 | * @param cb Callback function to add. Must not be NULL. |
1276 | * @param cb_data Data for the callback function. Can be NULL. | |
1277 | * | |
04cb9157 MH |
1278 | * @retval SR_OK Success. |
1279 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1280 | * |
1281 | * @since 0.3.0 | |
ee9953ef | 1282 | * @private |
aac0ea25 | 1283 | */ |
ee9953ef | 1284 | SR_PRIV int sr_session_source_add_channel(struct sr_session *session, |
0812c40e ML |
1285 | GIOChannel *channel, int events, int timeout, |
1286 | sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 | 1287 | { |
c2bf5506 | 1288 | GPollFD pollfd; |
aac0ea25 | 1289 | |
c2bf5506 DE |
1290 | if (!channel) { |
1291 | sr_err("%s: channel was NULL", __func__); | |
1292 | return SR_ERR_ARG; | |
1293 | } | |
1294 | /* We should be using g_io_create_watch(), but can't without | |
1295 | * changing the driver API, as the callback signature is different. | |
1296 | */ | |
faa5d7d9 | 1297 | #ifdef G_OS_WIN32 |
c2bf5506 | 1298 | g_io_channel_win32_make_pollfd(channel, events, &pollfd); |
aac0ea25 | 1299 | #else |
c2bf5506 DE |
1300 | pollfd.fd = g_io_channel_unix_get_fd(channel); |
1301 | pollfd.events = events; | |
aac0ea25 | 1302 | #endif |
cbc1413f | 1303 | return sr_session_fd_source_add(session, channel, pollfd.fd, |
c2bf5506 | 1304 | pollfd.events, timeout, cb, cb_data); |
aac0ea25 LPC |
1305 | } |
1306 | ||
6b2d8d3e | 1307 | /** |
92248e78 | 1308 | * Remove the source identified by the specified poll object. |
6b2d8d3e | 1309 | * |
7efe889e | 1310 | * @param session The session to use. Must not be NULL. |
c2bf5506 | 1311 | * @param key The key by which the source is identified. |
6b2d8d3e | 1312 | * |
04cb9157 | 1313 | * @retval SR_OK Success |
92248e78 | 1314 | * @retval SR_ERR_BUG No event source for poll_object found. |
ee9953ef DE |
1315 | * |
1316 | * @private | |
6b2d8d3e | 1317 | */ |
92248e78 | 1318 | SR_PRIV int sr_session_source_remove_internal(struct sr_session *session, |
c2bf5506 | 1319 | void *key) |
544a4582 | 1320 | { |
c2bf5506 | 1321 | GSource *source; |
544a4582 | 1322 | |
c2bf5506 DE |
1323 | source = g_hash_table_lookup(session->event_sources, key); |
1324 | /* | |
1325 | * Trying to remove an already removed event source is problematic | |
92248e78 DE |
1326 | * since the poll_object handle may have been reused in the meantime. |
1327 | */ | |
534b634c | 1328 | if (!source) { |
c2bf5506 DE |
1329 | sr_warn("Cannot remove non-existing event source %p.", key); |
1330 | return SR_ERR_BUG; | |
534b634c | 1331 | } |
c2bf5506 | 1332 | g_source_destroy(source); |
534b634c | 1333 | |
c2bf5506 | 1334 | return SR_OK; |
534b634c DE |
1335 | } |
1336 | ||
6b2d8d3e | 1337 | /** |
aac0ea25 LPC |
1338 | * Remove the source belonging to the specified file descriptor. |
1339 | * | |
7efe889e | 1340 | * @param session The session to use. Must not be NULL. |
1a895c61 | 1341 | * @param fd The file descriptor for which the source should be removed. |
aac0ea25 | 1342 | * |
04cb9157 MH |
1343 | * @retval SR_OK Success |
1344 | * @retval SR_ERR_ARG Invalid argument | |
04cb9157 | 1345 | * @retval SR_ERR_BUG Internal error. |
47117241 UH |
1346 | * |
1347 | * @since 0.3.0 | |
ee9953ef | 1348 | * @private |
aac0ea25 | 1349 | */ |
ee9953ef | 1350 | SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd) |
aac0ea25 | 1351 | { |
c2bf5506 | 1352 | return sr_session_source_remove_internal(session, GINT_TO_POINTER(fd)); |
aac0ea25 LPC |
1353 | } |
1354 | ||
1355 | /** | |
1356 | * Remove the source belonging to the specified poll descriptor. | |
1357 | * | |
7efe889e | 1358 | * @param session The session to use. Must not be NULL. |
aac0ea25 | 1359 | * @param pollfd The poll descriptor for which the source should be removed. |
faa5d7d9 | 1360 | * Must not be NULL. |
aac0ea25 LPC |
1361 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
1362 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
1363 | * internal errors. | |
47117241 UH |
1364 | * |
1365 | * @since 0.2.0 | |
ee9953ef | 1366 | * @private |
aac0ea25 | 1367 | */ |
ee9953ef | 1368 | SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session, |
0812c40e | 1369 | GPollFD *pollfd) |
aac0ea25 | 1370 | { |
faa5d7d9 DE |
1371 | if (!pollfd) { |
1372 | sr_err("%s: pollfd was NULL", __func__); | |
1373 | return SR_ERR_ARG; | |
1374 | } | |
c2bf5506 | 1375 | return sr_session_source_remove_internal(session, pollfd); |
aac0ea25 LPC |
1376 | } |
1377 | ||
6b2d8d3e | 1378 | /** |
aac0ea25 LPC |
1379 | * Remove the source belonging to the specified channel. |
1380 | * | |
7efe889e | 1381 | * @param session The session to use. Must not be NULL. |
1a895c61 | 1382 | * @param channel The channel for which the source should be removed. |
faa5d7d9 | 1383 | * Must not be NULL. |
04cb9157 MH |
1384 | * @retval SR_OK Success. |
1385 | * @retval SR_ERR_ARG Invalid argument. | |
04cb9157 | 1386 | * @return SR_ERR_BUG Internal error. |
47117241 UH |
1387 | * |
1388 | * @since 0.2.0 | |
ee9953ef | 1389 | * @private |
aac0ea25 | 1390 | */ |
ee9953ef | 1391 | SR_PRIV int sr_session_source_remove_channel(struct sr_session *session, |
0812c40e | 1392 | GIOChannel *channel) |
aac0ea25 | 1393 | { |
faa5d7d9 DE |
1394 | if (!channel) { |
1395 | sr_err("%s: channel was NULL", __func__); | |
1396 | return SR_ERR_ARG; | |
1397 | } | |
c2bf5506 DE |
1398 | return sr_session_source_remove_internal(session, channel); |
1399 | } | |
1400 | ||
1401 | /** Unregister an event source that has been destroyed. | |
1402 | * | |
1403 | * This is intended to be called from a source's finalize() method. | |
1404 | * | |
1405 | * @param session The session to use. Must not be NULL. | |
1406 | * @param key The key used to identify @a source. | |
1407 | * @param source The source object that was destroyed. | |
1408 | * | |
1409 | * @retval SR_OK Success. | |
1410 | * @retval SR_ERR_BUG Event source for @a key does not match @a source. | |
5de0fc55 | 1411 | * @retval SR_ERR Other error. |
ee9953ef DE |
1412 | * |
1413 | * @private | |
c2bf5506 DE |
1414 | */ |
1415 | SR_PRIV int sr_session_source_destroyed(struct sr_session *session, | |
1416 | void *key, GSource *source) | |
1417 | { | |
1418 | GSource *registered_source; | |
1419 | ||
1420 | registered_source = g_hash_table_lookup(session->event_sources, key); | |
1421 | /* | |
1422 | * Trying to remove an already removed event source is problematic | |
1423 | * since the poll_object handle may have been reused in the meantime. | |
1424 | */ | |
1425 | if (!registered_source) { | |
1426 | sr_err("No event source for key %p found.", key); | |
1427 | return SR_ERR_BUG; | |
1428 | } | |
1429 | if (registered_source != source) { | |
1430 | sr_err("Event source for key %p does not match" | |
1431 | " destroyed source.", key); | |
1432 | return SR_ERR_BUG; | |
1433 | } | |
1434 | g_hash_table_remove(session->event_sources, key); | |
5de0fc55 DE |
1435 | |
1436 | if (g_hash_table_size(session->event_sources) > 0) | |
1437 | return SR_OK; | |
1438 | ||
1439 | /* If no event sources are left, consider the acquisition finished. | |
1440 | * This is pretty crude, as it requires all event sources to be | |
1441 | * registered via the libsigrok API. | |
c2bf5506 | 1442 | */ |
5de0fc55 | 1443 | return stop_check_later(session); |
aac0ea25 | 1444 | } |
7b870c38 | 1445 | |
ee29d92e | 1446 | static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy) |
8143cfdc | 1447 | { |
8143cfdc | 1448 | g_variant_ref(src->data); |
ee29d92e AJ |
1449 | meta_copy->config = g_slist_append(meta_copy->config, |
1450 | g_memdup(src, sizeof(struct sr_config))); | |
8143cfdc BV |
1451 | } |
1452 | ||
1453 | SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, | |
1454 | struct sr_datafeed_packet **copy) | |
1455 | { | |
1456 | const struct sr_datafeed_meta *meta; | |
1457 | struct sr_datafeed_meta *meta_copy; | |
1458 | const struct sr_datafeed_logic *logic; | |
1459 | struct sr_datafeed_logic *logic_copy; | |
5faebab2 UH |
1460 | const struct sr_datafeed_analog_old *analog_old; |
1461 | struct sr_datafeed_analog_old *analog_old_copy; | |
edb691fc UH |
1462 | const struct sr_datafeed_analog *analog; |
1463 | struct sr_datafeed_analog *analog_copy; | |
8143cfdc BV |
1464 | uint8_t *payload; |
1465 | ||
1466 | *copy = g_malloc0(sizeof(struct sr_datafeed_packet)); | |
1467 | (*copy)->type = packet->type; | |
1468 | ||
1469 | switch (packet->type) { | |
1470 | case SR_DF_TRIGGER: | |
1471 | case SR_DF_END: | |
1472 | /* No payload. */ | |
1473 | break; | |
1474 | case SR_DF_HEADER: | |
1475 | payload = g_malloc(sizeof(struct sr_datafeed_header)); | |
1476 | memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header)); | |
1477 | (*copy)->payload = payload; | |
1478 | break; | |
1479 | case SR_DF_META: | |
1480 | meta = packet->payload; | |
ee29d92e AJ |
1481 | meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta)); |
1482 | g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config); | |
8143cfdc BV |
1483 | (*copy)->payload = meta_copy; |
1484 | break; | |
1485 | case SR_DF_LOGIC: | |
1486 | logic = packet->payload; | |
1487 | logic_copy = g_malloc(sizeof(logic)); | |
1488 | logic_copy->length = logic->length; | |
1489 | logic_copy->unitsize = logic->unitsize; | |
1490 | memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize); | |
1491 | (*copy)->payload = logic_copy; | |
1492 | break; | |
5faebab2 UH |
1493 | case SR_DF_ANALOG_OLD: |
1494 | analog_old = packet->payload; | |
1495 | analog_old_copy = g_malloc(sizeof(analog_old)); | |
1496 | analog_old_copy->channels = g_slist_copy(analog_old->channels); | |
1497 | analog_old_copy->num_samples = analog_old->num_samples; | |
1498 | analog_old_copy->mq = analog_old->mq; | |
1499 | analog_old_copy->unit = analog_old->unit; | |
1500 | analog_old_copy->mqflags = analog_old->mqflags; | |
1501 | analog_old_copy->data = g_malloc(analog_old->num_samples * sizeof(float)); | |
1502 | memcpy(analog_old_copy->data, analog_old->data, | |
1503 | analog_old->num_samples * sizeof(float)); | |
1504 | (*copy)->payload = analog_old_copy; | |
8143cfdc | 1505 | break; |
edb691fc UH |
1506 | case SR_DF_ANALOG: |
1507 | analog = packet->payload; | |
1508 | analog_copy = g_malloc(sizeof(analog)); | |
1509 | analog_copy->data = g_malloc( | |
1510 | analog->encoding->unitsize * analog->num_samples); | |
1511 | memcpy(analog_copy->data, analog->data, | |
1512 | analog->encoding->unitsize * analog->num_samples); | |
1513 | analog_copy->num_samples = analog->num_samples; | |
1514 | analog_copy->encoding = g_memdup(analog->encoding, | |
dbdfa4fb | 1515 | sizeof(struct sr_analog_encoding)); |
edb691fc | 1516 | analog_copy->meaning = g_memdup(analog->meaning, |
dbdfa4fb | 1517 | sizeof(struct sr_analog_meaning)); |
edb691fc UH |
1518 | analog_copy->meaning->channels = g_slist_copy( |
1519 | analog->meaning->channels); | |
1520 | analog_copy->spec = g_memdup(analog->spec, | |
dbdfa4fb | 1521 | sizeof(struct sr_analog_spec)); |
edb691fc | 1522 | (*copy)->payload = analog_copy; |
dbdfa4fb | 1523 | break; |
8143cfdc BV |
1524 | default: |
1525 | sr_err("Unknown packet type %d", packet->type); | |
1526 | return SR_ERR; | |
1527 | } | |
1528 | ||
1529 | return SR_OK; | |
1530 | } | |
1531 | ||
1532 | void sr_packet_free(struct sr_datafeed_packet *packet) | |
1533 | { | |
1534 | const struct sr_datafeed_meta *meta; | |
1535 | const struct sr_datafeed_logic *logic; | |
5faebab2 | 1536 | const struct sr_datafeed_analog_old *analog_old; |
edb691fc | 1537 | const struct sr_datafeed_analog *analog; |
8143cfdc BV |
1538 | struct sr_config *src; |
1539 | GSList *l; | |
1540 | ||
1541 | switch (packet->type) { | |
1542 | case SR_DF_TRIGGER: | |
1543 | case SR_DF_END: | |
1544 | /* No payload. */ | |
1545 | break; | |
1546 | case SR_DF_HEADER: | |
1547 | /* Payload is a simple struct. */ | |
1548 | g_free((void *)packet->payload); | |
1549 | break; | |
1550 | case SR_DF_META: | |
1551 | meta = packet->payload; | |
1552 | for (l = meta->config; l; l = l->next) { | |
1553 | src = l->data; | |
1554 | g_variant_unref(src->data); | |
1555 | g_free(src); | |
1556 | } | |
1557 | g_slist_free(meta->config); | |
1558 | g_free((void *)packet->payload); | |
1559 | break; | |
1560 | case SR_DF_LOGIC: | |
1561 | logic = packet->payload; | |
1562 | g_free(logic->data); | |
1563 | g_free((void *)packet->payload); | |
1564 | break; | |
5faebab2 UH |
1565 | case SR_DF_ANALOG_OLD: |
1566 | analog_old = packet->payload; | |
1567 | g_slist_free(analog_old->channels); | |
1568 | g_free(analog_old->data); | |
8143cfdc BV |
1569 | g_free((void *)packet->payload); |
1570 | break; | |
edb691fc UH |
1571 | case SR_DF_ANALOG: |
1572 | analog = packet->payload; | |
1573 | g_free(analog->data); | |
1574 | g_free(analog->encoding); | |
1575 | g_slist_free(analog->meaning->channels); | |
1576 | g_free(analog->meaning); | |
1577 | g_free(analog->spec); | |
83c1dbd9 ML |
1578 | g_free((void *)packet->payload); |
1579 | break; | |
8143cfdc BV |
1580 | default: |
1581 | sr_err("Unknown packet type %d", packet->type); | |
1582 | } | |
1583 | g_free(packet); | |
1584 | ||
1585 | } | |
1586 | ||
7b870c38 | 1587 | /** @} */ |