]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
c2bf5506 | 5 | * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com> |
a1bb33af UH |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
32af282c | 22 | #include <errno.h> |
a1bb33af UH |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
26 | #include <string.h> | |
544a4582 | 27 | #include <glib.h> |
c1aae900 | 28 | #include <libsigrok/libsigrok.h> |
45c59c8b | 29 | #include "libsigrok-internal.h" |
aa4b1107 | 30 | |
2ad1deb8 | 31 | /** @cond PRIVATE */ |
3544f848 | 32 | #define LOG_PREFIX "session" |
2ad1deb8 | 33 | /** @endcond */ |
a421dc1d | 34 | |
393fb9cb UH |
35 | /** |
36 | * @file | |
37 | * | |
38 | * Creating, using, or destroying libsigrok sessions. | |
39 | */ | |
40 | ||
7b870c38 UH |
41 | /** |
42 | * @defgroup grp_session Session handling | |
43 | * | |
44 | * Creating, using, or destroying libsigrok sessions. | |
45 | * | |
46 | * @{ | |
47 | */ | |
48 | ||
c2bf5506 DE |
49 | struct datafeed_callback { |
50 | sr_datafeed_callback cb; | |
1f9813eb | 51 | void *cb_data; |
c2bf5506 | 52 | }; |
aac0ea25 | 53 | |
c2bf5506 | 54 | /** Custom GLib event source for generic descriptor I/O. |
5de0fc55 | 55 | * @see https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html |
c2bf5506 DE |
56 | * @internal |
57 | */ | |
58 | struct fd_source { | |
59 | GSource base; | |
62d7945f | 60 | |
c2bf5506 DE |
61 | int64_t timeout_us; |
62 | int64_t due_us; | |
92248e78 | 63 | |
c2bf5506 DE |
64 | /* Meta-data needed to keep track of installed sources */ |
65 | struct sr_session *session; | |
66 | void *key; | |
544a4582 | 67 | |
c2bf5506 | 68 | GPollFD pollfd; |
2726474a ML |
69 | }; |
70 | ||
c2bf5506 | 71 | /** FD event source prepare() method. |
5de0fc55 | 72 | * This is called immediately before poll(). |
c2bf5506 DE |
73 | */ |
74 | static gboolean fd_source_prepare(GSource *source, int *timeout) | |
75 | { | |
76 | int64_t now_us; | |
77 | struct fd_source *fsource; | |
78 | int remaining_ms; | |
79 | ||
80 | fsource = (struct fd_source *)source; | |
81 | ||
82 | if (fsource->timeout_us >= 0) { | |
83 | now_us = g_source_get_time(source); | |
84 | ||
85 | if (fsource->due_us == 0) { | |
86 | /* First-time initialization of the expiration time */ | |
87 | fsource->due_us = now_us + fsource->timeout_us; | |
88 | } | |
89 | remaining_ms = (MAX(0, fsource->due_us - now_us) + 999) / 1000; | |
90 | } else { | |
91 | remaining_ms = -1; | |
92 | } | |
93 | *timeout = remaining_ms; | |
94 | ||
95 | return (remaining_ms == 0); | |
96 | } | |
97 | ||
98 | /** FD event source check() method. | |
5de0fc55 | 99 | * This is called after poll() returns to check whether an event fired. |
c2bf5506 DE |
100 | */ |
101 | static gboolean fd_source_check(GSource *source) | |
102 | { | |
103 | struct fd_source *fsource; | |
104 | unsigned int revents; | |
105 | ||
106 | fsource = (struct fd_source *)source; | |
107 | revents = fsource->pollfd.revents; | |
108 | ||
109 | return (revents != 0 || (fsource->timeout_us >= 0 | |
110 | && fsource->due_us <= g_source_get_time(source))); | |
111 | } | |
112 | ||
113 | /** FD event source dispatch() method. | |
5de0fc55 | 114 | * This is called if either prepare() or check() returned TRUE. |
c2bf5506 DE |
115 | */ |
116 | static gboolean fd_source_dispatch(GSource *source, | |
117 | GSourceFunc callback, void *user_data) | |
118 | { | |
119 | struct fd_source *fsource; | |
c2bf5506 DE |
120 | unsigned int revents; |
121 | gboolean keep; | |
122 | ||
123 | fsource = (struct fd_source *)source; | |
c2bf5506 DE |
124 | revents = fsource->pollfd.revents; |
125 | ||
c2bf5506 DE |
126 | if (!callback) { |
127 | sr_err("Callback not set, cannot dispatch event."); | |
128 | return G_SOURCE_REMOVE; | |
129 | } | |
130 | keep = (*(sr_receive_data_callback)callback) | |
131 | (fsource->pollfd.fd, revents, user_data); | |
132 | ||
133 | if (fsource->timeout_us >= 0 && G_LIKELY(keep) | |
134 | && G_LIKELY(!g_source_is_destroyed(source))) | |
135 | fsource->due_us = g_source_get_time(source) | |
136 | + fsource->timeout_us; | |
137 | return keep; | |
138 | } | |
139 | ||
140 | /** FD event source finalize() method. | |
141 | */ | |
142 | static void fd_source_finalize(GSource *source) | |
143 | { | |
144 | struct fd_source *fsource; | |
145 | ||
146 | fsource = (struct fd_source *)source; | |
147 | ||
148 | sr_dbg("%s: key %p", __func__, fsource->key); | |
149 | ||
150 | sr_session_source_destroyed(fsource->session, fsource->key, source); | |
151 | } | |
152 | ||
153 | /** Create an event source for I/O on a file descriptor. | |
154 | * | |
155 | * In order to maintain API compatibility, this event source also doubles | |
156 | * as a timer event source. | |
157 | * | |
158 | * @param session The session the event source belongs to. | |
159 | * @param key The key used to identify this source. | |
160 | * @param fd The file descriptor or HANDLE. | |
161 | * @param timeout_ms The timeout interval in ms, or -1 to wait indefinitely. | |
162 | * @return A new event source object, or NULL on failure. | |
163 | */ | |
164 | static GSource *fd_source_new(struct sr_session *session, void *key, | |
165 | gintptr fd, int events, int timeout_ms) | |
166 | { | |
167 | static GSourceFuncs fd_source_funcs = { | |
168 | .prepare = &fd_source_prepare, | |
169 | .check = &fd_source_check, | |
170 | .dispatch = &fd_source_dispatch, | |
171 | .finalize = &fd_source_finalize | |
172 | }; | |
173 | GSource *source; | |
174 | struct fd_source *fsource; | |
175 | ||
176 | source = g_source_new(&fd_source_funcs, sizeof(struct fd_source)); | |
177 | fsource = (struct fd_source *)source; | |
178 | ||
179 | g_source_set_name(source, (fd < 0) ? "timer" : "fd"); | |
180 | ||
181 | if (timeout_ms >= 0) { | |
182 | fsource->timeout_us = 1000 * (int64_t)timeout_ms; | |
183 | fsource->due_us = 0; | |
184 | } else { | |
185 | fsource->timeout_us = -1; | |
186 | fsource->due_us = INT64_MAX; | |
187 | } | |
188 | fsource->session = session; | |
189 | fsource->key = key; | |
190 | ||
191 | fsource->pollfd.fd = fd; | |
192 | fsource->pollfd.events = events; | |
193 | fsource->pollfd.revents = 0; | |
194 | ||
195 | if (fd >= 0) | |
196 | g_source_add_poll(source, &fsource->pollfd); | |
197 | ||
198 | return source; | |
199 | } | |
200 | ||
9f45fb3a UH |
201 | /** |
202 | * Create a new session. | |
7efe889e | 203 | * |
61e6e2da | 204 | * @param ctx The context in which to create the new session. |
7efe889e UH |
205 | * @param new_session This will contain a pointer to the newly created |
206 | * session if the return value is SR_OK, otherwise the value | |
207 | * is undefined and should not be used. Must not be NULL. | |
9f45fb3a | 208 | * |
0812c40e | 209 | * @retval SR_OK Success. |
41de54ff | 210 | * @retval SR_ERR_ARG Invalid argument. |
47117241 | 211 | * |
0812c40e | 212 | * @since 0.4.0 |
9f45fb3a | 213 | */ |
61e6e2da ML |
214 | SR_API int sr_session_new(struct sr_context *ctx, |
215 | struct sr_session **new_session) | |
a1bb33af | 216 | { |
3337e9a1 | 217 | struct sr_session *session; |
a1bb33af | 218 | |
41de54ff UH |
219 | if (!new_session) |
220 | return SR_ERR_ARG; | |
221 | ||
3337e9a1 | 222 | session = g_malloc0(sizeof(struct sr_session)); |
b7e94111 | 223 | |
4ed5d21d | 224 | session->ctx = ctx; |
faa5d7d9 | 225 | |
c2bf5506 | 226 | g_mutex_init(&session->main_mutex); |
faa5d7d9 | 227 | |
c2bf5506 DE |
228 | /* To maintain API compatibility, we need a lookup table |
229 | * which maps poll_object IDs to GSource* pointers. | |
230 | */ | |
231 | session->event_sources = g_hash_table_new(NULL, NULL); | |
0812c40e | 232 | |
3337e9a1 | 233 | *new_session = session; |
0812c40e ML |
234 | |
235 | return SR_OK; | |
a1bb33af UH |
236 | } |
237 | ||
9f45fb3a | 238 | /** |
0812c40e | 239 | * Destroy a session. |
9f45fb3a UH |
240 | * This frees up all memory used by the session. |
241 | * | |
7efe889e UH |
242 | * @param session The session to destroy. Must not be NULL. |
243 | * | |
04cb9157 | 244 | * @retval SR_OK Success. |
0812c40e | 245 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 246 | * |
0812c40e | 247 | * @since 0.4.0 |
9f45fb3a | 248 | */ |
0812c40e | 249 | SR_API int sr_session_destroy(struct sr_session *session) |
a1bb33af | 250 | { |
9f45fb3a | 251 | if (!session) { |
a421dc1d | 252 | sr_err("%s: session was NULL", __func__); |
0812c40e | 253 | return SR_ERR_ARG; |
9f45fb3a UH |
254 | } |
255 | ||
0812c40e | 256 | sr_session_dev_remove_all(session); |
1de3cced ML |
257 | g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free); |
258 | ||
877a6d09 SB |
259 | sr_session_datafeed_callback_remove_all(session); |
260 | ||
c2bf5506 DE |
261 | g_hash_table_unref(session->event_sources); |
262 | ||
263 | g_mutex_clear(&session->main_mutex); | |
faa5d7d9 | 264 | |
a1bb33af | 265 | g_free(session); |
0812c40e | 266 | |
e0508e67 | 267 | return SR_OK; |
a1bb33af UH |
268 | } |
269 | ||
9f45fb3a | 270 | /** |
0812c40e | 271 | * Remove all the devices from a session. |
9f45fb3a UH |
272 | * |
273 | * The session itself (i.e., the struct sr_session) is not free'd and still | |
274 | * exists after this function returns. | |
275 | * | |
7efe889e UH |
276 | * @param session The session to use. Must not be NULL. |
277 | * | |
04cb9157 | 278 | * @retval SR_OK Success. |
0812c40e | 279 | * @retval SR_ERR_BUG Invalid session passed. |
47117241 | 280 | * |
0812c40e | 281 | * @since 0.4.0 |
9f45fb3a | 282 | */ |
0812c40e | 283 | SR_API int sr_session_dev_remove_all(struct sr_session *session) |
a1bb33af | 284 | { |
0812c40e ML |
285 | struct sr_dev_inst *sdi; |
286 | GSList *l; | |
287 | ||
9f45fb3a | 288 | if (!session) { |
a421dc1d | 289 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
290 | return SR_ERR_ARG; |
291 | } | |
292 | ||
293 | for (l = session->devs; l; l = l->next) { | |
294 | sdi = (struct sr_dev_inst *) l->data; | |
295 | sdi->session = NULL; | |
9f45fb3a UH |
296 | } |
297 | ||
681803df | 298 | g_slist_free(session->devs); |
bb7ef793 | 299 | session->devs = NULL; |
e0508e67 UH |
300 | |
301 | return SR_OK; | |
a1bb33af UH |
302 | } |
303 | ||
9f45fb3a | 304 | /** |
0812c40e | 305 | * Add a device instance to a session. |
9f45fb3a | 306 | * |
7efe889e | 307 | * @param session The session to add to. Must not be NULL. |
0812c40e | 308 | * @param sdi The device instance to add to a session. Must not |
de4d3f99 BV |
309 | * be NULL. Also, sdi->driver and sdi->driver->dev_open must |
310 | * not be NULL. | |
9f45fb3a | 311 | * |
04cb9157 MH |
312 | * @retval SR_OK Success. |
313 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 | 314 | * |
0812c40e | 315 | * @since 0.4.0 |
9f45fb3a | 316 | */ |
0812c40e ML |
317 | SR_API int sr_session_dev_add(struct sr_session *session, |
318 | struct sr_dev_inst *sdi) | |
a1bb33af | 319 | { |
5451816f | 320 | int ret; |
a1bb33af | 321 | |
de4d3f99 | 322 | if (!sdi) { |
a421dc1d | 323 | sr_err("%s: sdi was NULL", __func__); |
9f45fb3a UH |
324 | return SR_ERR_ARG; |
325 | } | |
326 | ||
d6eb0c33 | 327 | if (!session) { |
a421dc1d | 328 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
329 | return SR_ERR_ARG; |
330 | } | |
331 | ||
332 | /* If sdi->session is not NULL, the device is already in this or | |
333 | * another session. */ | |
334 | if (sdi->session) { | |
335 | sr_err("%s: already assigned to session", __func__); | |
336 | return SR_ERR_ARG; | |
d6eb0c33 UH |
337 | } |
338 | ||
de4d3f99 BV |
339 | /* If sdi->driver is NULL, this is a virtual device. */ |
340 | if (!sdi->driver) { | |
d6eb0c33 | 341 | /* Just add the device, don't run dev_open(). */ |
5de0fc55 | 342 | session->devs = g_slist_append(session->devs, sdi); |
0812c40e | 343 | sdi->session = session; |
d6eb0c33 | 344 | return SR_OK; |
9f45fb3a UH |
345 | } |
346 | ||
de4d3f99 BV |
347 | /* sdi->driver is non-NULL (i.e. we have a real device). */ |
348 | if (!sdi->driver->dev_open) { | |
a421dc1d | 349 | sr_err("%s: sdi->driver->dev_open was NULL", __func__); |
8ec95d22 | 350 | return SR_ERR_BUG; |
9f45fb3a UH |
351 | } |
352 | ||
5de0fc55 | 353 | session->devs = g_slist_append(session->devs, sdi); |
0812c40e | 354 | sdi->session = session; |
aa4b1107 | 355 | |
5de0fc55 DE |
356 | /* TODO: This is invalid if the session runs in a different thread. |
357 | * The usage semantics and restrictions need to be documented. | |
358 | */ | |
5451816f | 359 | if (session->running) { |
32b7cd4f DE |
360 | /* Adding a device to a running session. Commit settings |
361 | * and start acquisition on that device now. */ | |
362 | if ((ret = sr_config_commit(sdi)) != SR_OK) { | |
363 | sr_err("Failed to commit device settings before " | |
364 | "starting acquisition in running session (%s)", | |
365 | sr_strerror(ret)); | |
366 | return ret; | |
367 | } | |
5451816f | 368 | if ((ret = sdi->driver->dev_acquisition_start(sdi, |
5de0fc55 | 369 | 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) { | |
815 | sdi = l->data; | |
816 | ret = sdi->driver->dev_acquisition_start(sdi, sdi); | |
817 | if (ret != SR_OK) { | |
818 | sr_err("Could not start %s device %s acquisition.", | |
819 | sdi->driver->name, sdi->connection_id); | |
7d658874 | 820 | break; |
9f45fb3a | 821 | } |
7d658874 BV |
822 | } |
823 | ||
c2bf5506 | 824 | if (ret != SR_OK) { |
5de0fc55 DE |
825 | /* If there are multiple devices, some of them may already have |
826 | * started successfully. Stop them now before returning. */ | |
827 | lend = l->next; | |
828 | for (l = session->devs; l != lend; l = l->next) { | |
829 | sdi = l->data; | |
830 | if (sdi->driver->dev_acquisition_stop) | |
831 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
832 | } | |
833 | /* TODO: Handle delayed stops. Need to iterate the event | |
834 | * sources... */ | |
c2bf5506 | 835 | session->running = FALSE; |
5de0fc55 DE |
836 | |
837 | unset_main_context(session); | |
838 | return ret; | |
c2bf5506 | 839 | } |
9f45fb3a | 840 | |
5de0fc55 DE |
841 | if (g_hash_table_size(session->event_sources) == 0) |
842 | stop_check_later(session); | |
843 | ||
844 | return SR_OK; | |
7d658874 BV |
845 | } |
846 | ||
9f45fb3a | 847 | /** |
5de0fc55 DE |
848 | * Block until the running session stops. |
849 | * | |
850 | * This is a convenience function which creates a GLib main loop and runs | |
851 | * it to process session events until the session stops. | |
852 | * | |
853 | * Instead of using this function, applications may run their own GLib main | |
854 | * loop, and use sr_session_stopped_callback_set() to receive notification | |
855 | * when the session finished running. | |
9f45fb3a | 856 | * |
7efe889e UH |
857 | * @param session The session to use. Must not be NULL. |
858 | * | |
04cb9157 | 859 | * @retval SR_OK Success. |
0812c40e | 860 | * @retval SR_ERR_ARG Invalid session passed. |
5de0fc55 | 861 | * @retval SR_ERR Other error. |
47117241 | 862 | * |
0812c40e | 863 | * @since 0.4.0 |
9f45fb3a | 864 | */ |
0812c40e | 865 | SR_API int sr_session_run(struct sr_session *session) |
7d658874 | 866 | { |
9f45fb3a | 867 | if (!session) { |
0812c40e ML |
868 | sr_err("%s: session was NULL", __func__); |
869 | return SR_ERR_ARG; | |
9f45fb3a | 870 | } |
5de0fc55 DE |
871 | if (!session->running) { |
872 | sr_err("No session running."); | |
873 | return SR_ERR; | |
9f45fb3a | 874 | } |
c2bf5506 DE |
875 | if (session->main_loop) { |
876 | sr_err("Main loop already created."); | |
877 | return SR_ERR; | |
878 | } | |
9f45fb3a | 879 | |
c2bf5506 | 880 | g_mutex_lock(&session->main_mutex); |
5de0fc55 | 881 | |
c2bf5506 DE |
882 | if (!session->main_context) { |
883 | sr_err("Cannot run without main context."); | |
884 | g_mutex_unlock(&session->main_mutex); | |
885 | return SR_ERR; | |
886 | } | |
c2bf5506 | 887 | session->main_loop = g_main_loop_new(session->main_context, FALSE); |
5de0fc55 | 888 | |
c2bf5506 DE |
889 | g_mutex_unlock(&session->main_mutex); |
890 | ||
891 | g_main_loop_run(session->main_loop); | |
892 | ||
893 | g_main_loop_unref(session->main_loop); | |
894 | session->main_loop = NULL; | |
895 | ||
e0508e67 | 896 | return SR_OK; |
7d658874 BV |
897 | } |
898 | ||
c2bf5506 | 899 | static gboolean session_stop_sync(void *user_data) |
a1bb33af | 900 | { |
c2bf5506 | 901 | struct sr_session *session; |
de4d3f99 | 902 | struct sr_dev_inst *sdi; |
c2bf5506 | 903 | GSList *node; |
a1bb33af | 904 | |
c2bf5506 DE |
905 | session = user_data; |
906 | ||
907 | if (!session->running) | |
908 | return G_SOURCE_REMOVE; | |
9f45fb3a | 909 | |
a421dc1d | 910 | sr_info("Stopping."); |
e0508e67 | 911 | |
c2bf5506 DE |
912 | for (node = session->devs; node; node = node->next) { |
913 | sdi = node->data; | |
914 | if (sdi->driver && sdi->driver->dev_acquisition_stop) | |
915 | sdi->driver->dev_acquisition_stop(sdi, 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; |
5faebab2 | 1035 | const struct sr_datafeed_analog_old *analog_old; |
edb691fc | 1036 | const struct sr_datafeed_analog *analog; |
7d2afd6c | 1037 | |
ca7dbb56 | 1038 | /* Please use the same order as in libsigrok.h. */ |
7d2afd6c BV |
1039 | switch (packet->type) { |
1040 | case SR_DF_HEADER: | |
a421dc1d | 1041 | sr_dbg("bus: Received SR_DF_HEADER packet."); |
7d2afd6c | 1042 | break; |
55c9f09d UH |
1043 | case SR_DF_END: |
1044 | sr_dbg("bus: Received SR_DF_END packet."); | |
7d2afd6c | 1045 | break; |
c71bac3b | 1046 | case SR_DF_META: |
a421dc1d | 1047 | sr_dbg("bus: Received SR_DF_META packet."); |
ee7489d2 | 1048 | break; |
55c9f09d UH |
1049 | case SR_DF_TRIGGER: |
1050 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); | |
1051 | break; | |
7d2afd6c BV |
1052 | case SR_DF_LOGIC: |
1053 | logic = packet->payload; | |
7ea45862 UH |
1054 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " |
1055 | "unitsize = %d).", logic->length, logic->unitsize); | |
7d2afd6c | 1056 | break; |
5faebab2 UH |
1057 | case SR_DF_ANALOG_OLD: |
1058 | analog_old = packet->payload; | |
1059 | sr_dbg("bus: Received SR_DF_ANALOG_OLD packet (%d samples).", | |
1060 | analog_old->num_samples); | |
ee7489d2 | 1061 | break; |
6ea7669c | 1062 | case SR_DF_FRAME_BEGIN: |
a421dc1d | 1063 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); |
6ea7669c BV |
1064 | break; |
1065 | case SR_DF_FRAME_END: | |
a421dc1d | 1066 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); |
6ea7669c | 1067 | break; |
edb691fc UH |
1068 | case SR_DF_ANALOG: |
1069 | analog = packet->payload; | |
1070 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", | |
1071 | analog->num_samples); | |
55c9f09d | 1072 | break; |
7d2afd6c | 1073 | default: |
a421dc1d | 1074 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); |
9f45fb3a | 1075 | break; |
7d2afd6c | 1076 | } |
7d2afd6c BV |
1077 | } |
1078 | ||
9f45fb3a | 1079 | /** |
a1645fcd BV |
1080 | * Send a packet to whatever is listening on the datafeed bus. |
1081 | * | |
1082 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 1083 | * |
6b2d8d3e | 1084 | * @param sdi TODO. |
31ccebc4 | 1085 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 1086 | * |
04cb9157 MH |
1087 | * @retval SR_OK Success. |
1088 | * @retval SR_ERR_ARG Invalid argument. | |
b4bd7088 UH |
1089 | * |
1090 | * @private | |
9f45fb3a | 1091 | */ |
de4d3f99 | 1092 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
8143cfdc | 1093 | const struct sr_datafeed_packet *packet) |
a1bb33af UH |
1094 | { |
1095 | GSList *l; | |
2726474a | 1096 | struct datafeed_callback *cb_struct; |
c0a1e532 UH |
1097 | struct sr_datafeed_packet *packet_in, *packet_out; |
1098 | struct sr_transform *t; | |
1099 | int ret; | |
a1bb33af | 1100 | |
de4d3f99 | 1101 | if (!sdi) { |
a421dc1d | 1102 | sr_err("%s: sdi was NULL", __func__); |
e0508e67 | 1103 | return SR_ERR_ARG; |
9f45fb3a UH |
1104 | } |
1105 | ||
e0508e67 | 1106 | if (!packet) { |
a421dc1d | 1107 | sr_err("%s: packet was NULL", __func__); |
e0508e67 | 1108 | return SR_ERR_ARG; |
9f45fb3a UH |
1109 | } |
1110 | ||
79f92686 BV |
1111 | if (!sdi->session) { |
1112 | sr_err("%s: session was NULL", __func__); | |
1113 | return SR_ERR_BUG; | |
1114 | } | |
1115 | ||
5faebab2 | 1116 | if (packet->type == SR_DF_ANALOG_OLD) { |
edb691fc | 1117 | /* Convert to SR_DF_ANALOG. */ |
5faebab2 | 1118 | const struct sr_datafeed_analog_old *analog_old = packet->payload; |
ca79993b ML |
1119 | struct sr_analog_encoding encoding; |
1120 | struct sr_analog_meaning meaning; | |
1121 | struct sr_analog_spec spec; | |
edb691fc UH |
1122 | struct sr_datafeed_analog analog; |
1123 | struct sr_datafeed_packet new_packet; | |
1124 | new_packet.type = SR_DF_ANALOG; | |
1125 | new_packet.payload = &analog; | |
1126 | analog.data = analog_old->data; | |
1127 | analog.num_samples = analog_old->num_samples; | |
1128 | analog.encoding = &encoding; | |
1129 | analog.meaning = &meaning; | |
1130 | analog.spec = &spec; | |
ca79993b ML |
1131 | encoding.unitsize = sizeof(float); |
1132 | encoding.is_signed = TRUE; | |
1133 | encoding.is_float = TRUE; | |
1134 | #ifdef WORDS_BIGENDIAN | |
1135 | encoding.is_bigendian = TRUE; | |
1136 | #else | |
1137 | encoding.is_bigendian = FALSE; | |
1138 | #endif | |
1139 | encoding.digits = 0; | |
1140 | encoding.is_digits_decimal = FALSE; | |
1141 | encoding.scale.p = 1; | |
1142 | encoding.scale.q = 1; | |
1143 | encoding.offset.p = 0; | |
1144 | encoding.offset.q = 1; | |
5faebab2 UH |
1145 | meaning.mq = analog_old->mq; |
1146 | meaning.unit = analog_old->unit; | |
1147 | meaning.mqflags = analog_old->mqflags; | |
1148 | meaning.channels = analog_old->channels; | |
ca79993b | 1149 | spec.spec_digits = 0; |
edb691fc | 1150 | return sr_session_send(sdi, &new_packet); |
ca79993b ML |
1151 | } |
1152 | ||
c0a1e532 UH |
1153 | /* |
1154 | * Pass the packet to the first transform module. If that returns | |
1155 | * another packet (instead of NULL), pass that packet to the next | |
1156 | * transform module in the list, and so on. | |
1157 | */ | |
1158 | packet_in = (struct sr_datafeed_packet *)packet; | |
1159 | for (l = sdi->session->transforms; l; l = l->next) { | |
1160 | t = l->data; | |
1161 | sr_spew("Running transform module '%s'.", t->module->id); | |
1162 | ret = t->module->receive(t, packet_in, &packet_out); | |
1163 | if (ret < 0) { | |
1164 | sr_err("Error while running transform module: %d.", ret); | |
1165 | return SR_ERR; | |
1166 | } | |
1167 | if (!packet_out) { | |
1168 | /* | |
1169 | * If any of the transforms don't return an output | |
1170 | * packet, abort. | |
1171 | */ | |
1172 | sr_spew("Transform module didn't return a packet, aborting."); | |
1173 | return SR_OK; | |
1174 | } else { | |
1175 | /* | |
1176 | * Use this transform module's output packet as input | |
1177 | * for the next transform module. | |
1178 | */ | |
1179 | packet_in = packet_out; | |
1180 | } | |
1181 | } | |
4ec436c4 | 1182 | packet = packet_in; |
c0a1e532 UH |
1183 | |
1184 | /* | |
1185 | * If the last transform did output a packet, pass it to all datafeed | |
1186 | * callbacks. | |
1187 | */ | |
3337e9a1 | 1188 | for (l = sdi->session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
1189 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
1190 | datafeed_dump(packet); | |
2726474a ML |
1191 | cb_struct = l->data; |
1192 | cb_struct->cb(sdi, packet, cb_struct->cb_data); | |
a1bb33af | 1193 | } |
9f45fb3a | 1194 | |
e0508e67 | 1195 | return SR_OK; |
a1bb33af UH |
1196 | } |
1197 | ||
6b2d8d3e UH |
1198 | /** |
1199 | * Add an event source for a file descriptor. | |
1200 | * | |
7efe889e | 1201 | * @param session The session to use. Must not be NULL. |
c2bf5506 DE |
1202 | * @param key The key which identifies the event source. |
1203 | * @param source An event source object. Must not be NULL. | |
ee9953ef | 1204 | * |
04cb9157 MH |
1205 | * @retval SR_OK Success. |
1206 | * @retval SR_ERR_ARG Invalid argument. | |
c2bf5506 DE |
1207 | * @retval SR_ERR_BUG Event source with @a key already installed. |
1208 | * @retval SR_ERR Other error. | |
ee9953ef DE |
1209 | * |
1210 | * @private | |
6b2d8d3e | 1211 | */ |
62d7945f | 1212 | SR_PRIV int sr_session_source_add_internal(struct sr_session *session, |
c2bf5506 | 1213 | void *key, GSource *source) |
544a4582 | 1214 | { |
c2bf5506 DE |
1215 | /* |
1216 | * This must not ever happen, since the source has already been | |
1217 | * created and its finalize() method will remove the key for the | |
1218 | * already installed source. (Well it would, if we did not have | |
1219 | * another sanity check there.) | |
92248e78 | 1220 | */ |
c2bf5506 DE |
1221 | if (g_hash_table_contains(session->event_sources, key)) { |
1222 | sr_err("Event source with key %p already exists.", key); | |
1223 | return SR_ERR_BUG; | |
92248e78 | 1224 | } |
c2bf5506 DE |
1225 | g_hash_table_insert(session->event_sources, key, source); |
1226 | ||
5de0fc55 DE |
1227 | if (session_source_attach(session, source) == 0) |
1228 | return SR_ERR; | |
faa5d7d9 | 1229 | |
5de0fc55 | 1230 | return SR_OK; |
534b634c DE |
1231 | } |
1232 | ||
cbc1413f DE |
1233 | SR_PRIV int sr_session_fd_source_add(struct sr_session *session, |
1234 | void *key, gintptr fd, int events, int timeout, | |
c2bf5506 | 1235 | sr_receive_data_callback cb, void *cb_data) |
534b634c | 1236 | { |
c2bf5506 DE |
1237 | GSource *source; |
1238 | int ret; | |
534b634c | 1239 | |
c2bf5506 DE |
1240 | source = fd_source_new(session, key, fd, events, timeout); |
1241 | if (!source) | |
534b634c | 1242 | return SR_ERR; |
534b634c | 1243 | |
c2bf5506 | 1244 | g_source_set_callback(source, (GSourceFunc)cb, cb_data, NULL); |
534b634c | 1245 | |
c2bf5506 DE |
1246 | ret = sr_session_source_add_internal(session, key, source); |
1247 | g_source_unref(source); | |
544a4582 | 1248 | |
c2bf5506 | 1249 | return ret; |
544a4582 BV |
1250 | } |
1251 | ||
9f45fb3a | 1252 | /** |
6b2d8d3e | 1253 | * Add an event source for a file descriptor. |
9f45fb3a | 1254 | * |
7efe889e | 1255 | * @param session The session to use. Must not be NULL. |
534b634c | 1256 | * @param fd The file descriptor, or a negative value to create a timer source. |
aac0ea25 | 1257 | * @param events Events to check for. |
faa5d7d9 DE |
1258 | * @param timeout Max time in ms to wait before the callback is called, |
1259 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1260 | * @param cb Callback function to add. Must not be NULL. |
1261 | * @param cb_data Data for the callback function. Can be NULL. | |
9f45fb3a | 1262 | * |
04cb9157 MH |
1263 | * @retval SR_OK Success. |
1264 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1265 | * |
1266 | * @since 0.3.0 | |
ee9953ef | 1267 | * @private |
aac0ea25 | 1268 | */ |
ee9953ef | 1269 | SR_PRIV int sr_session_source_add(struct sr_session *session, int fd, |
0812c40e | 1270 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) |
aac0ea25 | 1271 | { |
faa5d7d9 | 1272 | if (fd < 0 && timeout < 0) { |
c2bf5506 | 1273 | sr_err("Cannot create timer source without timeout."); |
faa5d7d9 DE |
1274 | return SR_ERR_ARG; |
1275 | } | |
cbc1413f | 1276 | return sr_session_fd_source_add(session, GINT_TO_POINTER(fd), |
c2bf5506 | 1277 | fd, events, timeout, cb, cb_data); |
aac0ea25 LPC |
1278 | } |
1279 | ||
1280 | /** | |
1a895c61 | 1281 | * Add an event source for a GPollFD. |
aac0ea25 | 1282 | * |
7efe889e | 1283 | * @param session The session to use. Must not be NULL. |
faa5d7d9 DE |
1284 | * @param pollfd The GPollFD. Must not be NULL. |
1285 | * @param timeout Max time in ms to wait before the callback is called, | |
1286 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1287 | * @param cb Callback function to add. Must not be NULL. |
1288 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 1289 | * |
04cb9157 MH |
1290 | * @retval SR_OK Success. |
1291 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1292 | * |
1293 | * @since 0.3.0 | |
ee9953ef | 1294 | * @private |
9f45fb3a | 1295 | */ |
ee9953ef | 1296 | SR_PRIV int sr_session_source_add_pollfd(struct sr_session *session, |
0812c40e ML |
1297 | GPollFD *pollfd, int timeout, sr_receive_data_callback cb, |
1298 | void *cb_data) | |
aac0ea25 | 1299 | { |
faa5d7d9 DE |
1300 | if (!pollfd) { |
1301 | sr_err("%s: pollfd was NULL", __func__); | |
1302 | return SR_ERR_ARG; | |
1303 | } | |
cbc1413f | 1304 | return sr_session_fd_source_add(session, pollfd, pollfd->fd, |
c2bf5506 | 1305 | pollfd->events, timeout, cb, cb_data); |
aac0ea25 LPC |
1306 | } |
1307 | ||
1308 | /** | |
1a895c61 | 1309 | * Add an event source for a GIOChannel. |
aac0ea25 | 1310 | * |
7efe889e | 1311 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
1312 | * @param channel The GIOChannel. |
1313 | * @param events Events to poll on. | |
faa5d7d9 DE |
1314 | * @param timeout Max time in ms to wait before the callback is called, |
1315 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1316 | * @param cb Callback function to add. Must not be NULL. |
1317 | * @param cb_data Data for the callback function. Can be NULL. | |
1318 | * | |
04cb9157 MH |
1319 | * @retval SR_OK Success. |
1320 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1321 | * |
1322 | * @since 0.3.0 | |
ee9953ef | 1323 | * @private |
aac0ea25 | 1324 | */ |
ee9953ef | 1325 | SR_PRIV int sr_session_source_add_channel(struct sr_session *session, |
0812c40e ML |
1326 | GIOChannel *channel, int events, int timeout, |
1327 | sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 | 1328 | { |
c2bf5506 | 1329 | GPollFD pollfd; |
aac0ea25 | 1330 | |
c2bf5506 DE |
1331 | if (!channel) { |
1332 | sr_err("%s: channel was NULL", __func__); | |
1333 | return SR_ERR_ARG; | |
1334 | } | |
1335 | /* We should be using g_io_create_watch(), but can't without | |
1336 | * changing the driver API, as the callback signature is different. | |
1337 | */ | |
faa5d7d9 | 1338 | #ifdef G_OS_WIN32 |
c2bf5506 | 1339 | g_io_channel_win32_make_pollfd(channel, events, &pollfd); |
aac0ea25 | 1340 | #else |
c2bf5506 DE |
1341 | pollfd.fd = g_io_channel_unix_get_fd(channel); |
1342 | pollfd.events = events; | |
aac0ea25 | 1343 | #endif |
cbc1413f | 1344 | return sr_session_fd_source_add(session, channel, pollfd.fd, |
c2bf5506 | 1345 | pollfd.events, timeout, cb, cb_data); |
aac0ea25 LPC |
1346 | } |
1347 | ||
6b2d8d3e | 1348 | /** |
92248e78 | 1349 | * Remove the source identified by the specified poll object. |
6b2d8d3e | 1350 | * |
7efe889e | 1351 | * @param session The session to use. Must not be NULL. |
c2bf5506 | 1352 | * @param key The key by which the source is identified. |
6b2d8d3e | 1353 | * |
04cb9157 | 1354 | * @retval SR_OK Success |
92248e78 | 1355 | * @retval SR_ERR_BUG No event source for poll_object found. |
ee9953ef DE |
1356 | * |
1357 | * @private | |
6b2d8d3e | 1358 | */ |
92248e78 | 1359 | SR_PRIV int sr_session_source_remove_internal(struct sr_session *session, |
c2bf5506 | 1360 | void *key) |
544a4582 | 1361 | { |
c2bf5506 | 1362 | GSource *source; |
544a4582 | 1363 | |
c2bf5506 DE |
1364 | source = g_hash_table_lookup(session->event_sources, key); |
1365 | /* | |
1366 | * Trying to remove an already removed event source is problematic | |
92248e78 DE |
1367 | * since the poll_object handle may have been reused in the meantime. |
1368 | */ | |
534b634c | 1369 | if (!source) { |
c2bf5506 DE |
1370 | sr_warn("Cannot remove non-existing event source %p.", key); |
1371 | return SR_ERR_BUG; | |
534b634c | 1372 | } |
c2bf5506 | 1373 | g_source_destroy(source); |
534b634c | 1374 | |
c2bf5506 | 1375 | return SR_OK; |
534b634c DE |
1376 | } |
1377 | ||
6b2d8d3e | 1378 | /** |
aac0ea25 LPC |
1379 | * Remove the source belonging to the specified file descriptor. |
1380 | * | |
7efe889e | 1381 | * @param session The session to use. Must not be NULL. |
1a895c61 | 1382 | * @param fd The file descriptor for which the source should be removed. |
aac0ea25 | 1383 | * |
04cb9157 MH |
1384 | * @retval SR_OK Success |
1385 | * @retval SR_ERR_ARG Invalid argument | |
04cb9157 | 1386 | * @retval SR_ERR_BUG Internal error. |
47117241 UH |
1387 | * |
1388 | * @since 0.3.0 | |
ee9953ef | 1389 | * @private |
aac0ea25 | 1390 | */ |
ee9953ef | 1391 | SR_PRIV int sr_session_source_remove(struct sr_session *session, int fd) |
aac0ea25 | 1392 | { |
c2bf5506 | 1393 | return sr_session_source_remove_internal(session, GINT_TO_POINTER(fd)); |
aac0ea25 LPC |
1394 | } |
1395 | ||
1396 | /** | |
1397 | * Remove the source belonging to the specified poll descriptor. | |
1398 | * | |
7efe889e | 1399 | * @param session The session to use. Must not be NULL. |
aac0ea25 | 1400 | * @param pollfd The poll descriptor for which the source should be removed. |
faa5d7d9 | 1401 | * Must not be NULL. |
aac0ea25 LPC |
1402 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
1403 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
1404 | * internal errors. | |
47117241 UH |
1405 | * |
1406 | * @since 0.2.0 | |
ee9953ef | 1407 | * @private |
aac0ea25 | 1408 | */ |
ee9953ef | 1409 | SR_PRIV int sr_session_source_remove_pollfd(struct sr_session *session, |
0812c40e | 1410 | GPollFD *pollfd) |
aac0ea25 | 1411 | { |
faa5d7d9 DE |
1412 | if (!pollfd) { |
1413 | sr_err("%s: pollfd was NULL", __func__); | |
1414 | return SR_ERR_ARG; | |
1415 | } | |
c2bf5506 | 1416 | return sr_session_source_remove_internal(session, pollfd); |
aac0ea25 LPC |
1417 | } |
1418 | ||
6b2d8d3e | 1419 | /** |
aac0ea25 LPC |
1420 | * Remove the source belonging to the specified channel. |
1421 | * | |
7efe889e | 1422 | * @param session The session to use. Must not be NULL. |
1a895c61 | 1423 | * @param channel The channel for which the source should be removed. |
faa5d7d9 | 1424 | * Must not be NULL. |
04cb9157 MH |
1425 | * @retval SR_OK Success. |
1426 | * @retval SR_ERR_ARG Invalid argument. | |
04cb9157 | 1427 | * @return SR_ERR_BUG Internal error. |
47117241 UH |
1428 | * |
1429 | * @since 0.2.0 | |
ee9953ef | 1430 | * @private |
aac0ea25 | 1431 | */ |
ee9953ef | 1432 | SR_PRIV int sr_session_source_remove_channel(struct sr_session *session, |
0812c40e | 1433 | GIOChannel *channel) |
aac0ea25 | 1434 | { |
faa5d7d9 DE |
1435 | if (!channel) { |
1436 | sr_err("%s: channel was NULL", __func__); | |
1437 | return SR_ERR_ARG; | |
1438 | } | |
c2bf5506 DE |
1439 | return sr_session_source_remove_internal(session, channel); |
1440 | } | |
1441 | ||
1442 | /** Unregister an event source that has been destroyed. | |
1443 | * | |
1444 | * This is intended to be called from a source's finalize() method. | |
1445 | * | |
1446 | * @param session The session to use. Must not be NULL. | |
1447 | * @param key The key used to identify @a source. | |
1448 | * @param source The source object that was destroyed. | |
1449 | * | |
1450 | * @retval SR_OK Success. | |
1451 | * @retval SR_ERR_BUG Event source for @a key does not match @a source. | |
5de0fc55 | 1452 | * @retval SR_ERR Other error. |
ee9953ef DE |
1453 | * |
1454 | * @private | |
c2bf5506 DE |
1455 | */ |
1456 | SR_PRIV int sr_session_source_destroyed(struct sr_session *session, | |
1457 | void *key, GSource *source) | |
1458 | { | |
1459 | GSource *registered_source; | |
1460 | ||
1461 | registered_source = g_hash_table_lookup(session->event_sources, key); | |
1462 | /* | |
1463 | * Trying to remove an already removed event source is problematic | |
1464 | * since the poll_object handle may have been reused in the meantime. | |
1465 | */ | |
1466 | if (!registered_source) { | |
1467 | sr_err("No event source for key %p found.", key); | |
1468 | return SR_ERR_BUG; | |
1469 | } | |
1470 | if (registered_source != source) { | |
1471 | sr_err("Event source for key %p does not match" | |
1472 | " destroyed source.", key); | |
1473 | return SR_ERR_BUG; | |
1474 | } | |
1475 | g_hash_table_remove(session->event_sources, key); | |
5de0fc55 DE |
1476 | |
1477 | if (g_hash_table_size(session->event_sources) > 0) | |
1478 | return SR_OK; | |
1479 | ||
1480 | /* If no event sources are left, consider the acquisition finished. | |
1481 | * This is pretty crude, as it requires all event sources to be | |
1482 | * registered via the libsigrok API. | |
c2bf5506 | 1483 | */ |
5de0fc55 | 1484 | return stop_check_later(session); |
aac0ea25 | 1485 | } |
7b870c38 | 1486 | |
ee29d92e | 1487 | static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy) |
8143cfdc | 1488 | { |
8143cfdc | 1489 | g_variant_ref(src->data); |
ee29d92e AJ |
1490 | meta_copy->config = g_slist_append(meta_copy->config, |
1491 | g_memdup(src, sizeof(struct sr_config))); | |
8143cfdc BV |
1492 | } |
1493 | ||
1494 | SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, | |
1495 | struct sr_datafeed_packet **copy) | |
1496 | { | |
1497 | const struct sr_datafeed_meta *meta; | |
1498 | struct sr_datafeed_meta *meta_copy; | |
1499 | const struct sr_datafeed_logic *logic; | |
1500 | struct sr_datafeed_logic *logic_copy; | |
5faebab2 UH |
1501 | const struct sr_datafeed_analog_old *analog_old; |
1502 | struct sr_datafeed_analog_old *analog_old_copy; | |
edb691fc UH |
1503 | const struct sr_datafeed_analog *analog; |
1504 | struct sr_datafeed_analog *analog_copy; | |
8143cfdc BV |
1505 | uint8_t *payload; |
1506 | ||
1507 | *copy = g_malloc0(sizeof(struct sr_datafeed_packet)); | |
1508 | (*copy)->type = packet->type; | |
1509 | ||
1510 | switch (packet->type) { | |
1511 | case SR_DF_TRIGGER: | |
1512 | case SR_DF_END: | |
1513 | /* No payload. */ | |
1514 | break; | |
1515 | case SR_DF_HEADER: | |
1516 | payload = g_malloc(sizeof(struct sr_datafeed_header)); | |
1517 | memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header)); | |
1518 | (*copy)->payload = payload; | |
1519 | break; | |
1520 | case SR_DF_META: | |
1521 | meta = packet->payload; | |
ee29d92e AJ |
1522 | meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta)); |
1523 | g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config); | |
8143cfdc BV |
1524 | (*copy)->payload = meta_copy; |
1525 | break; | |
1526 | case SR_DF_LOGIC: | |
1527 | logic = packet->payload; | |
b84f91ff | 1528 | logic_copy = g_malloc(sizeof(*logic_copy)); |
8143cfdc BV |
1529 | logic_copy->length = logic->length; |
1530 | logic_copy->unitsize = logic->unitsize; | |
1531 | memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize); | |
1532 | (*copy)->payload = logic_copy; | |
1533 | break; | |
5faebab2 UH |
1534 | case SR_DF_ANALOG_OLD: |
1535 | analog_old = packet->payload; | |
b84f91ff | 1536 | analog_old_copy = g_malloc(sizeof(*analog_old_copy)); |
5faebab2 UH |
1537 | analog_old_copy->channels = g_slist_copy(analog_old->channels); |
1538 | analog_old_copy->num_samples = analog_old->num_samples; | |
1539 | analog_old_copy->mq = analog_old->mq; | |
1540 | analog_old_copy->unit = analog_old->unit; | |
1541 | analog_old_copy->mqflags = analog_old->mqflags; | |
1542 | analog_old_copy->data = g_malloc(analog_old->num_samples * sizeof(float)); | |
1543 | memcpy(analog_old_copy->data, analog_old->data, | |
1544 | analog_old->num_samples * sizeof(float)); | |
1545 | (*copy)->payload = analog_old_copy; | |
8143cfdc | 1546 | break; |
edb691fc UH |
1547 | case SR_DF_ANALOG: |
1548 | analog = packet->payload; | |
b84f91ff | 1549 | analog_copy = g_malloc(sizeof(*analog_copy)); |
edb691fc UH |
1550 | analog_copy->data = g_malloc( |
1551 | analog->encoding->unitsize * analog->num_samples); | |
1552 | memcpy(analog_copy->data, analog->data, | |
1553 | analog->encoding->unitsize * analog->num_samples); | |
1554 | analog_copy->num_samples = analog->num_samples; | |
1555 | analog_copy->encoding = g_memdup(analog->encoding, | |
dbdfa4fb | 1556 | sizeof(struct sr_analog_encoding)); |
edb691fc | 1557 | analog_copy->meaning = g_memdup(analog->meaning, |
dbdfa4fb | 1558 | sizeof(struct sr_analog_meaning)); |
edb691fc UH |
1559 | analog_copy->meaning->channels = g_slist_copy( |
1560 | analog->meaning->channels); | |
1561 | analog_copy->spec = g_memdup(analog->spec, | |
dbdfa4fb | 1562 | sizeof(struct sr_analog_spec)); |
edb691fc | 1563 | (*copy)->payload = analog_copy; |
dbdfa4fb | 1564 | break; |
8143cfdc BV |
1565 | default: |
1566 | sr_err("Unknown packet type %d", packet->type); | |
1567 | return SR_ERR; | |
1568 | } | |
1569 | ||
1570 | return SR_OK; | |
1571 | } | |
1572 | ||
1573 | void sr_packet_free(struct sr_datafeed_packet *packet) | |
1574 | { | |
1575 | const struct sr_datafeed_meta *meta; | |
1576 | const struct sr_datafeed_logic *logic; | |
5faebab2 | 1577 | const struct sr_datafeed_analog_old *analog_old; |
edb691fc | 1578 | const struct sr_datafeed_analog *analog; |
8143cfdc BV |
1579 | struct sr_config *src; |
1580 | GSList *l; | |
1581 | ||
1582 | switch (packet->type) { | |
1583 | case SR_DF_TRIGGER: | |
1584 | case SR_DF_END: | |
1585 | /* No payload. */ | |
1586 | break; | |
1587 | case SR_DF_HEADER: | |
1588 | /* Payload is a simple struct. */ | |
1589 | g_free((void *)packet->payload); | |
1590 | break; | |
1591 | case SR_DF_META: | |
1592 | meta = packet->payload; | |
1593 | for (l = meta->config; l; l = l->next) { | |
1594 | src = l->data; | |
1595 | g_variant_unref(src->data); | |
1596 | g_free(src); | |
1597 | } | |
1598 | g_slist_free(meta->config); | |
1599 | g_free((void *)packet->payload); | |
1600 | break; | |
1601 | case SR_DF_LOGIC: | |
1602 | logic = packet->payload; | |
1603 | g_free(logic->data); | |
1604 | g_free((void *)packet->payload); | |
1605 | break; | |
5faebab2 UH |
1606 | case SR_DF_ANALOG_OLD: |
1607 | analog_old = packet->payload; | |
1608 | g_slist_free(analog_old->channels); | |
1609 | g_free(analog_old->data); | |
8143cfdc BV |
1610 | g_free((void *)packet->payload); |
1611 | break; | |
edb691fc UH |
1612 | case SR_DF_ANALOG: |
1613 | analog = packet->payload; | |
1614 | g_free(analog->data); | |
1615 | g_free(analog->encoding); | |
1616 | g_slist_free(analog->meaning->channels); | |
1617 | g_free(analog->meaning); | |
1618 | g_free(analog->spec); | |
83c1dbd9 ML |
1619 | g_free((void *)packet->payload); |
1620 | break; | |
8143cfdc BV |
1621 | default: |
1622 | sr_err("Unknown packet type %d", packet->type); | |
1623 | } | |
1624 | g_free(packet); | |
1625 | ||
1626 | } | |
1627 | ||
7b870c38 | 1628 | /** @} */ |