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