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