]>
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> |
a1bb33af UH |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <unistd.h> | |
23 | #include <string.h> | |
544a4582 | 24 | #include <glib.h> |
45c59c8b BV |
25 | #include "libsigrok.h" |
26 | #include "libsigrok-internal.h" | |
aa4b1107 | 27 | |
2ad1deb8 | 28 | /** @cond PRIVATE */ |
3544f848 | 29 | #define LOG_PREFIX "session" |
2ad1deb8 | 30 | /** @endcond */ |
a421dc1d | 31 | |
393fb9cb UH |
32 | /** |
33 | * @file | |
34 | * | |
35 | * Creating, using, or destroying libsigrok sessions. | |
36 | */ | |
37 | ||
7b870c38 UH |
38 | /** |
39 | * @defgroup grp_session Session handling | |
40 | * | |
41 | * Creating, using, or destroying libsigrok sessions. | |
42 | * | |
43 | * @{ | |
44 | */ | |
45 | ||
544a4582 | 46 | struct source { |
544a4582 | 47 | int timeout; |
144f6660 | 48 | sr_receive_data_callback cb; |
1f9813eb | 49 | void *cb_data; |
aac0ea25 LPC |
50 | |
51 | /* This is used to keep track of the object (fd, pollfd or channel) which is | |
52 | * being polled and will be used to match the source when removing it again. | |
53 | */ | |
54 | gintptr poll_object; | |
544a4582 BV |
55 | }; |
56 | ||
2726474a | 57 | struct datafeed_callback { |
144f6660 | 58 | sr_datafeed_callback cb; |
2726474a ML |
59 | void *cb_data; |
60 | }; | |
61 | ||
9f45fb3a UH |
62 | /** |
63 | * Create a new session. | |
7efe889e | 64 | * |
61e6e2da | 65 | * @param ctx The context in which to create the new session. |
7efe889e UH |
66 | * @param new_session This will contain a pointer to the newly created |
67 | * session if the return value is SR_OK, otherwise the value | |
68 | * is undefined and should not be used. Must not be NULL. | |
9f45fb3a | 69 | * |
0812c40e | 70 | * @retval SR_OK Success. |
41de54ff | 71 | * @retval SR_ERR_ARG Invalid argument. |
47117241 | 72 | * |
0812c40e | 73 | * @since 0.4.0 |
9f45fb3a | 74 | */ |
61e6e2da ML |
75 | SR_API int sr_session_new(struct sr_context *ctx, |
76 | struct sr_session **new_session) | |
a1bb33af | 77 | { |
3337e9a1 | 78 | struct sr_session *session; |
a1bb33af | 79 | |
41de54ff UH |
80 | if (!new_session) |
81 | return SR_ERR_ARG; | |
82 | ||
3337e9a1 | 83 | session = g_malloc0(sizeof(struct sr_session)); |
b7e94111 | 84 | |
4ed5d21d | 85 | session->ctx = ctx; |
3337e9a1 BV |
86 | session->source_timeout = -1; |
87 | session->running = FALSE; | |
88 | session->abort_session = FALSE; | |
89 | g_mutex_init(&session->stop_mutex); | |
0812c40e | 90 | |
3337e9a1 | 91 | *new_session = session; |
0812c40e ML |
92 | |
93 | return SR_OK; | |
a1bb33af UH |
94 | } |
95 | ||
9f45fb3a | 96 | /** |
0812c40e | 97 | * Destroy a session. |
9f45fb3a UH |
98 | * This frees up all memory used by the session. |
99 | * | |
7efe889e UH |
100 | * @param session The session to destroy. Must not be NULL. |
101 | * | |
04cb9157 | 102 | * @retval SR_OK Success. |
0812c40e | 103 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 104 | * |
0812c40e | 105 | * @since 0.4.0 |
9f45fb3a | 106 | */ |
0812c40e | 107 | SR_API int sr_session_destroy(struct sr_session *session) |
a1bb33af | 108 | { |
9f45fb3a | 109 | if (!session) { |
a421dc1d | 110 | sr_err("%s: session was NULL", __func__); |
0812c40e | 111 | return SR_ERR_ARG; |
9f45fb3a UH |
112 | } |
113 | ||
0812c40e | 114 | sr_session_dev_remove_all(session); |
33c6e4c5 | 115 | g_mutex_clear(&session->stop_mutex); |
3d68b612 BV |
116 | if (session->trigger) |
117 | sr_trigger_free(session->trigger); | |
33c6e4c5 | 118 | |
1de3cced ML |
119 | g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free); |
120 | ||
a1bb33af | 121 | g_free(session); |
0812c40e | 122 | |
e0508e67 | 123 | return SR_OK; |
a1bb33af UH |
124 | } |
125 | ||
9f45fb3a | 126 | /** |
0812c40e | 127 | * Remove all the devices from a session. |
9f45fb3a UH |
128 | * |
129 | * The session itself (i.e., the struct sr_session) is not free'd and still | |
130 | * exists after this function returns. | |
131 | * | |
7efe889e UH |
132 | * @param session The session to use. Must not be NULL. |
133 | * | |
04cb9157 | 134 | * @retval SR_OK Success. |
0812c40e | 135 | * @retval SR_ERR_BUG Invalid session passed. |
47117241 | 136 | * |
0812c40e | 137 | * @since 0.4.0 |
9f45fb3a | 138 | */ |
0812c40e | 139 | SR_API int sr_session_dev_remove_all(struct sr_session *session) |
a1bb33af | 140 | { |
0812c40e ML |
141 | struct sr_dev_inst *sdi; |
142 | GSList *l; | |
143 | ||
9f45fb3a | 144 | if (!session) { |
a421dc1d | 145 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
146 | return SR_ERR_ARG; |
147 | } | |
148 | ||
149 | for (l = session->devs; l; l = l->next) { | |
150 | sdi = (struct sr_dev_inst *) l->data; | |
151 | sdi->session = NULL; | |
9f45fb3a UH |
152 | } |
153 | ||
681803df | 154 | g_slist_free(session->devs); |
bb7ef793 | 155 | session->devs = NULL; |
e0508e67 UH |
156 | |
157 | return SR_OK; | |
a1bb33af UH |
158 | } |
159 | ||
9f45fb3a | 160 | /** |
0812c40e | 161 | * Add a device instance to a session. |
9f45fb3a | 162 | * |
7efe889e | 163 | * @param session The session to add to. Must not be NULL. |
0812c40e | 164 | * @param sdi The device instance to add to a session. Must not |
de4d3f99 BV |
165 | * be NULL. Also, sdi->driver and sdi->driver->dev_open must |
166 | * not be NULL. | |
9f45fb3a | 167 | * |
04cb9157 MH |
168 | * @retval SR_OK Success. |
169 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 | 170 | * |
0812c40e | 171 | * @since 0.4.0 |
9f45fb3a | 172 | */ |
0812c40e ML |
173 | SR_API int sr_session_dev_add(struct sr_session *session, |
174 | struct sr_dev_inst *sdi) | |
a1bb33af | 175 | { |
5451816f | 176 | int ret; |
a1bb33af | 177 | |
de4d3f99 | 178 | if (!sdi) { |
a421dc1d | 179 | sr_err("%s: sdi was NULL", __func__); |
9f45fb3a UH |
180 | return SR_ERR_ARG; |
181 | } | |
182 | ||
d6eb0c33 | 183 | if (!session) { |
a421dc1d | 184 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
185 | return SR_ERR_ARG; |
186 | } | |
187 | ||
188 | /* If sdi->session is not NULL, the device is already in this or | |
189 | * another session. */ | |
190 | if (sdi->session) { | |
191 | sr_err("%s: already assigned to session", __func__); | |
192 | return SR_ERR_ARG; | |
d6eb0c33 UH |
193 | } |
194 | ||
de4d3f99 BV |
195 | /* If sdi->driver is NULL, this is a virtual device. */ |
196 | if (!sdi->driver) { | |
d6eb0c33 | 197 | /* Just add the device, don't run dev_open(). */ |
de4d3f99 | 198 | session->devs = g_slist_append(session->devs, (gpointer)sdi); |
0812c40e | 199 | sdi->session = session; |
d6eb0c33 | 200 | return SR_OK; |
9f45fb3a UH |
201 | } |
202 | ||
de4d3f99 BV |
203 | /* sdi->driver is non-NULL (i.e. we have a real device). */ |
204 | if (!sdi->driver->dev_open) { | |
a421dc1d | 205 | sr_err("%s: sdi->driver->dev_open was NULL", __func__); |
8ec95d22 | 206 | return SR_ERR_BUG; |
9f45fb3a UH |
207 | } |
208 | ||
de4d3f99 | 209 | session->devs = g_slist_append(session->devs, (gpointer)sdi); |
0812c40e | 210 | sdi->session = session; |
aa4b1107 | 211 | |
5451816f | 212 | if (session->running) { |
32b7cd4f DE |
213 | /* Adding a device to a running session. Commit settings |
214 | * and start acquisition on that device now. */ | |
215 | if ((ret = sr_config_commit(sdi)) != SR_OK) { | |
216 | sr_err("Failed to commit device settings before " | |
217 | "starting acquisition in running session (%s)", | |
218 | sr_strerror(ret)); | |
219 | return ret; | |
220 | } | |
5451816f | 221 | if ((ret = sdi->driver->dev_acquisition_start(sdi, |
32b7cd4f | 222 | (void *)sdi)) != SR_OK) { |
5451816f | 223 | sr_err("Failed to start acquisition of device in " |
32b7cd4f DE |
224 | "running session (%s)", sr_strerror(ret)); |
225 | return ret; | |
226 | } | |
5451816f BV |
227 | } |
228 | ||
e46b8fb1 | 229 | return SR_OK; |
a1bb33af UH |
230 | } |
231 | ||
2bb311b4 | 232 | /** |
0812c40e | 233 | * List all device instances attached to a session. |
2bb311b4 | 234 | * |
7efe889e | 235 | * @param session The session to use. Must not be NULL. |
2bb311b4 BV |
236 | * @param devlist A pointer where the device instance list will be |
237 | * stored on return. If no devices are in the session, | |
238 | * this will be NULL. Each element in the list points | |
239 | * to a struct sr_dev_inst *. | |
240 | * The list must be freed by the caller, but not the | |
241 | * elements pointed to. | |
242 | * | |
04cb9157 | 243 | * @retval SR_OK Success. |
0812c40e | 244 | * @retval SR_ERR_ARG Invalid argument. |
47117241 | 245 | * |
0812c40e | 246 | * @since 0.4.0 |
2bb311b4 | 247 | */ |
0812c40e | 248 | SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist) |
2bb311b4 | 249 | { |
2bb311b4 | 250 | if (!session) |
0812c40e ML |
251 | return SR_ERR_ARG; |
252 | ||
253 | if (!devlist) | |
254 | return SR_ERR_ARG; | |
2bb311b4 BV |
255 | |
256 | *devlist = g_slist_copy(session->devs); | |
257 | ||
258 | return SR_OK; | |
259 | } | |
260 | ||
9f45fb3a | 261 | /** |
0812c40e | 262 | * Remove all datafeed callbacks in a session. |
9f45fb3a | 263 | * |
7efe889e UH |
264 | * @param session The session to use. Must not be NULL. |
265 | * | |
04cb9157 | 266 | * @retval SR_OK Success. |
0812c40e | 267 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 268 | * |
0812c40e | 269 | * @since 0.4.0 |
9f45fb3a | 270 | */ |
0812c40e | 271 | SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session) |
a1bb33af | 272 | { |
9f45fb3a | 273 | if (!session) { |
a421dc1d | 274 | sr_err("%s: session was NULL", __func__); |
0812c40e | 275 | return SR_ERR_ARG; |
9f45fb3a UH |
276 | } |
277 | ||
2726474a | 278 | g_slist_free_full(session->datafeed_callbacks, g_free); |
a1bb33af | 279 | session->datafeed_callbacks = NULL; |
e0508e67 UH |
280 | |
281 | return SR_OK; | |
a1bb33af UH |
282 | } |
283 | ||
9f45fb3a | 284 | /** |
0812c40e | 285 | * Add a datafeed callback to a session. |
9f45fb3a | 286 | * |
7efe889e | 287 | * @param session The session to use. Must not be NULL. |
d08490aa | 288 | * @param cb Function to call when a chunk of data is received. |
0abee507 | 289 | * Must not be NULL. |
85222791 | 290 | * @param cb_data Opaque pointer passed in by the caller. |
a1645fcd | 291 | * |
04cb9157 MH |
292 | * @retval SR_OK Success. |
293 | * @retval SR_ERR_BUG No session exists. | |
47117241 UH |
294 | * |
295 | * @since 0.3.0 | |
9f45fb3a | 296 | */ |
0812c40e ML |
297 | SR_API int sr_session_datafeed_callback_add(struct sr_session *session, |
298 | sr_datafeed_callback cb, void *cb_data) | |
a1bb33af | 299 | { |
2726474a ML |
300 | struct datafeed_callback *cb_struct; |
301 | ||
9f45fb3a | 302 | if (!session) { |
a421dc1d | 303 | sr_err("%s: session was NULL", __func__); |
e0508e67 | 304 | return SR_ERR_BUG; |
9f45fb3a UH |
305 | } |
306 | ||
0abee507 | 307 | if (!cb) { |
a421dc1d | 308 | sr_err("%s: cb was NULL", __func__); |
0abee507 UH |
309 | return SR_ERR_ARG; |
310 | } | |
9f45fb3a | 311 | |
91219afc | 312 | cb_struct = g_malloc0(sizeof(struct datafeed_callback)); |
2726474a ML |
313 | cb_struct->cb = cb; |
314 | cb_struct->cb_data = cb_data; | |
315 | ||
62c82025 | 316 | session->datafeed_callbacks = |
2726474a | 317 | g_slist_append(session->datafeed_callbacks, cb_struct); |
e0508e67 UH |
318 | |
319 | return SR_OK; | |
a1bb33af UH |
320 | } |
321 | ||
9f42e2e6 UH |
322 | /** |
323 | * Get the trigger assigned to this session. | |
324 | * | |
325 | * @param session The session to use. | |
326 | * | |
327 | * @retval NULL Invalid (NULL) session was passed to the function. | |
328 | * @retval other The trigger assigned to this session (can be NULL). | |
329 | * | |
330 | * @since 0.4.0 | |
331 | */ | |
0812c40e | 332 | SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session) |
7b5e6d29 | 333 | { |
9f42e2e6 UH |
334 | if (!session) |
335 | return NULL; | |
336 | ||
7b5e6d29 BV |
337 | return session->trigger; |
338 | } | |
339 | ||
9f42e2e6 UH |
340 | /** |
341 | * Set the trigger of this session. | |
342 | * | |
343 | * @param session The session to use. Must not be NULL. | |
344 | * @param trig The trigger to assign to this session. Can be NULL. | |
345 | * | |
346 | * @retval SR_OK Success. | |
347 | * @retval SR_ERR_ARG Invalid argument. | |
348 | * | |
349 | * @since 0.4.0 | |
350 | */ | |
0812c40e | 351 | SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig) |
7b5e6d29 | 352 | { |
9f42e2e6 UH |
353 | if (!session) |
354 | return SR_ERR_ARG; | |
355 | ||
7b5e6d29 BV |
356 | session->trigger = trig; |
357 | ||
358 | return SR_OK; | |
359 | } | |
360 | ||
b483be74 | 361 | /** |
0812c40e | 362 | * Call every device in the current session's callback. |
b483be74 BV |
363 | * |
364 | * For sessions not driven by select loops such as sr_session_run(), | |
365 | * but driven by another scheduler, this can be used to poll the devices | |
366 | * from within that scheduler. | |
367 | * | |
7efe889e | 368 | * @param session The session to use. Must not be NULL. |
f6eb2cb5 BV |
369 | * @param block If TRUE, this call will wait for any of the session's |
370 | * sources to fire an event on the file descriptors, or | |
371 | * any of their timeouts to activate. In other words, this | |
372 | * can be used as a select loop. | |
373 | * If FALSE, all sources have their callback run, regardless | |
374 | * of file descriptor or timeout status. | |
375 | * | |
04cb9157 | 376 | * @retval SR_OK Success. |
f3f19d11 | 377 | * @retval SR_ERR Error occurred. |
b483be74 | 378 | */ |
3337e9a1 | 379 | static int sr_session_iteration(struct sr_session *session, gboolean block) |
544a4582 | 380 | { |
b7e94111 | 381 | unsigned int i; |
bb5f6110 ML |
382 | int ret, timeout; |
383 | #ifdef HAVE_LIBUSB_1_0 | |
384 | int usb_timeout; | |
385 | struct timeval tv; | |
386 | #endif | |
387 | ||
388 | timeout = block ? 0 : session->source_timeout; | |
389 | ||
390 | #ifdef HAVE_LIBUSB_1_0 | |
391 | ret = libusb_get_next_timeout(session->ctx->libusb_ctx, &tv); | |
392 | if (ret < 0) { | |
393 | sr_err("Error getting libusb timeout: %s", | |
394 | libusb_error_name(ret)); | |
395 | return SR_ERR; | |
396 | } else if (ret == 1) { | |
397 | usb_timeout = tv.tv_sec * 1000 + tv.tv_usec / 1000; | |
398 | timeout = MIN(timeout, usb_timeout); | |
399 | } | |
400 | #endif | |
544a4582 | 401 | |
bb5f6110 | 402 | ret = g_poll(session->pollfds, session->num_sources, timeout); |
b483be74 BV |
403 | for (i = 0; i < session->num_sources; i++) { |
404 | if (session->pollfds[i].revents > 0 || (ret == 0 | |
405 | && session->source_timeout == session->sources[i].timeout)) { | |
33c6e4c5 | 406 | /* |
b483be74 BV |
407 | * Invoke the source's callback on an event, |
408 | * or if the poll timed out and this source | |
409 | * asked for that timeout. | |
33c6e4c5 | 410 | */ |
b483be74 BV |
411 | if (!session->sources[i].cb(session->pollfds[i].fd, |
412 | session->pollfds[i].revents, | |
413 | session->sources[i].cb_data)) | |
0812c40e ML |
414 | sr_session_source_remove(session, |
415 | session->sources[i].poll_object); | |
b483be74 BV |
416 | } |
417 | /* | |
418 | * We want to take as little time as possible to stop | |
419 | * the session if we have been told to do so. Therefore, | |
420 | * we check the flag after processing every source, not | |
421 | * just once per main event loop. | |
422 | */ | |
423 | g_mutex_lock(&session->stop_mutex); | |
424 | if (session->abort_session) { | |
0812c40e | 425 | sr_session_stop_sync(session); |
b483be74 BV |
426 | /* But once is enough. */ |
427 | session->abort_session = FALSE; | |
544a4582 | 428 | } |
b483be74 | 429 | g_mutex_unlock(&session->stop_mutex); |
544a4582 | 430 | } |
e0508e67 UH |
431 | |
432 | return SR_OK; | |
544a4582 BV |
433 | } |
434 | ||
7b5e6d29 BV |
435 | static int verify_trigger(struct sr_trigger *trigger) |
436 | { | |
437 | struct sr_trigger_stage *stage; | |
438 | struct sr_trigger_match *match; | |
439 | GSList *l, *m; | |
440 | ||
441 | if (!trigger->stages) { | |
442 | sr_err("No trigger stages defined."); | |
443 | return SR_ERR; | |
444 | } | |
445 | ||
446 | sr_spew("Checking trigger:"); | |
447 | for (l = trigger->stages; l; l = l->next) { | |
448 | stage = l->data; | |
449 | if (!stage->matches) { | |
450 | sr_err("Stage %d has no matches defined.", stage->stage); | |
451 | return SR_ERR; | |
452 | } | |
453 | for (m = stage->matches; m; m = m->next) { | |
454 | match = m->data; | |
455 | if (!match->channel) { | |
456 | sr_err("Stage %d match has no channel.", stage->stage); | |
457 | return SR_ERR; | |
458 | } | |
459 | if (!match->match) { | |
460 | sr_err("Stage %d match is not defined.", stage->stage); | |
461 | return SR_ERR; | |
462 | } | |
463 | sr_spew("Stage %d match on channel %s, match %d", stage->stage, | |
464 | match->channel->name, match->match); | |
465 | } | |
466 | } | |
467 | ||
468 | return SR_OK; | |
469 | } | |
1beccaed | 470 | |
9f45fb3a UH |
471 | /** |
472 | * Start a session. | |
473 | * | |
7efe889e UH |
474 | * @param session The session to use. Must not be NULL. |
475 | * | |
04cb9157 | 476 | * @retval SR_OK Success. |
0812c40e | 477 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 478 | * |
0812c40e | 479 | * @since 0.4.0 |
9f45fb3a | 480 | */ |
0812c40e | 481 | SR_API int sr_session_start(struct sr_session *session) |
7d658874 | 482 | { |
de4d3f99 | 483 | struct sr_dev_inst *sdi; |
013ec84b BV |
484 | struct sr_channel *ch; |
485 | GSList *l, *c; | |
486 | int enabled_channels, ret; | |
7d658874 | 487 | |
9f45fb3a | 488 | if (!session) { |
0812c40e ML |
489 | sr_err("%s: session was NULL", __func__); |
490 | return SR_ERR_ARG; | |
9f45fb3a UH |
491 | } |
492 | ||
bb7ef793 | 493 | if (!session->devs) { |
a421dc1d | 494 | sr_err("%s: session->devs was NULL; a session " |
9f45fb3a | 495 | "cannot be started without devices.", __func__); |
0812c40e | 496 | return SR_ERR_ARG; |
9f45fb3a UH |
497 | } |
498 | ||
7b5e6d29 BV |
499 | if (session->trigger && verify_trigger(session->trigger) != SR_OK) |
500 | return SR_ERR; | |
501 | ||
c7142604 | 502 | sr_info("Starting."); |
9f45fb3a | 503 | |
b7c3e849 | 504 | ret = SR_OK; |
bb7ef793 | 505 | for (l = session->devs; l; l = l->next) { |
de4d3f99 | 506 | sdi = l->data; |
013ec84b BV |
507 | enabled_channels = 0; |
508 | for (c = sdi->channels; c; c = c->next) { | |
509 | ch = c->data; | |
510 | if (ch->enabled) { | |
511 | enabled_channels++; | |
512 | break; | |
513 | } | |
514 | } | |
515 | if (enabled_channels == 0) { | |
516 | ret = SR_ERR; | |
1b9e567b SA |
517 | sr_err("%s using connection %s has no enabled channels!", |
518 | sdi->driver->name, sdi->connection_id); | |
013ec84b BV |
519 | break; |
520 | } | |
521 | ||
32b7cd4f DE |
522 | if ((ret = sr_config_commit(sdi)) != SR_OK) { |
523 | sr_err("Failed to commit device settings before " | |
524 | "starting acquisition (%s)", sr_strerror(ret)); | |
525 | break; | |
526 | } | |
de4d3f99 | 527 | if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) { |
a421dc1d | 528 | sr_err("%s: could not start an acquisition " |
568dcacc | 529 | "(%s)", __func__, sr_strerror(ret)); |
7d658874 | 530 | break; |
9f45fb3a | 531 | } |
7d658874 BV |
532 | } |
533 | ||
9f45fb3a UH |
534 | /* TODO: What if there are multiple devices? Which return code? */ |
535 | ||
7d658874 BV |
536 | return ret; |
537 | } | |
538 | ||
9f45fb3a | 539 | /** |
0812c40e | 540 | * Run a session. |
9f45fb3a | 541 | * |
7efe889e UH |
542 | * @param session The session to use. Must not be NULL. |
543 | * | |
04cb9157 | 544 | * @retval SR_OK Success. |
0812c40e | 545 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 546 | * |
0812c40e | 547 | * @since 0.4.0 |
9f45fb3a | 548 | */ |
0812c40e | 549 | SR_API int sr_session_run(struct sr_session *session) |
7d658874 | 550 | { |
9f45fb3a | 551 | if (!session) { |
0812c40e ML |
552 | sr_err("%s: session was NULL", __func__); |
553 | return SR_ERR_ARG; | |
9f45fb3a UH |
554 | } |
555 | ||
bb7ef793 | 556 | if (!session->devs) { |
9f45fb3a | 557 | /* TODO: Actually the case? */ |
a421dc1d | 558 | sr_err("%s: session->devs was NULL; a session " |
9f45fb3a | 559 | "cannot be run without devices.", __func__); |
0812c40e | 560 | return SR_ERR_ARG; |
9f45fb3a | 561 | } |
5451816f | 562 | session->running = TRUE; |
9f45fb3a | 563 | |
a421dc1d | 564 | sr_info("Running."); |
7d658874 | 565 | |
9f45fb3a | 566 | /* Do we have real sources? */ |
b7e94111 | 567 | if (session->num_sources == 1 && session->pollfds[0].fd == -1) { |
9f45fb3a | 568 | /* Dummy source, freewheel over it. */ |
2cbeb2b7 | 569 | while (session->num_sources) |
b7e94111 | 570 | session->sources[0].cb(-1, 0, session->sources[0].cb_data); |
9f45fb3a UH |
571 | } else { |
572 | /* Real sources, use g_poll() main loop. */ | |
b483be74 | 573 | while (session->num_sources) |
3337e9a1 | 574 | sr_session_iteration(session, TRUE); |
9f45fb3a UH |
575 | } |
576 | ||
e0508e67 | 577 | return SR_OK; |
7d658874 BV |
578 | } |
579 | ||
9f45fb3a | 580 | /** |
0812c40e | 581 | * Stop a session. |
9f45fb3a | 582 | * |
0812c40e ML |
583 | * The session is stopped immediately, with all acquisition sessions stopped |
584 | * and hardware drivers cleaned up. | |
9f45fb3a | 585 | * |
33c6e4c5 AG |
586 | * This must be called from within the session thread, to prevent freeing |
587 | * resources that the session thread will try to use. | |
588 | * | |
7efe889e UH |
589 | * @param session The session to use. Must not be NULL. |
590 | * | |
04cb9157 | 591 | * @retval SR_OK Success. |
0812c40e | 592 | * @retval SR_ERR_ARG Invalid session passed. |
72a08bcc BV |
593 | * |
594 | * @private | |
9f45fb3a | 595 | */ |
0812c40e | 596 | SR_PRIV int sr_session_stop_sync(struct sr_session *session) |
a1bb33af | 597 | { |
de4d3f99 | 598 | struct sr_dev_inst *sdi; |
a1bb33af UH |
599 | GSList *l; |
600 | ||
9f45fb3a | 601 | if (!session) { |
a421dc1d | 602 | sr_err("%s: session was NULL", __func__); |
0812c40e | 603 | return SR_ERR_ARG; |
9f45fb3a UH |
604 | } |
605 | ||
a421dc1d | 606 | sr_info("Stopping."); |
e0508e67 | 607 | |
bb7ef793 | 608 | for (l = session->devs; l; l = l->next) { |
de4d3f99 BV |
609 | sdi = l->data; |
610 | if (sdi->driver) { | |
611 | if (sdi->driver->dev_acquisition_stop) | |
612 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
8c76be53 | 613 | } |
a1bb33af | 614 | } |
5451816f | 615 | session->running = FALSE; |
9f45fb3a | 616 | |
e0508e67 | 617 | return SR_OK; |
a1bb33af UH |
618 | } |
619 | ||
33c6e4c5 | 620 | /** |
0812c40e | 621 | * Stop a session. |
33c6e4c5 | 622 | * |
0812c40e ML |
623 | * The session is stopped immediately, with all acquisition sessions being |
624 | * stopped and hardware drivers cleaned up. | |
33c6e4c5 AG |
625 | * |
626 | * If the session is run in a separate thread, this function will not block | |
627 | * until the session is finished executing. It is the caller's responsibility | |
628 | * to wait for the session thread to return before assuming that the session is | |
629 | * completely decommissioned. | |
630 | * | |
7efe889e UH |
631 | * @param session The session to use. Must not be NULL. |
632 | * | |
04cb9157 | 633 | * @retval SR_OK Success. |
0812c40e | 634 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 635 | * |
0812c40e | 636 | * @since 0.4.0 |
33c6e4c5 | 637 | */ |
0812c40e | 638 | SR_API int sr_session_stop(struct sr_session *session) |
33c6e4c5 AG |
639 | { |
640 | if (!session) { | |
641 | sr_err("%s: session was NULL", __func__); | |
642 | return SR_ERR_BUG; | |
643 | } | |
644 | ||
645 | g_mutex_lock(&session->stop_mutex); | |
646 | session->abort_session = TRUE; | |
647 | g_mutex_unlock(&session->stop_mutex); | |
648 | ||
649 | return SR_OK; | |
650 | } | |
651 | ||
9f45fb3a | 652 | /** |
a1645fcd | 653 | * Debug helper. |
9f45fb3a | 654 | * |
996b0c72 | 655 | * @param packet The packet to show debugging information for. |
9f45fb3a | 656 | */ |
bf53457d | 657 | static void datafeed_dump(const struct sr_datafeed_packet *packet) |
7d2afd6c | 658 | { |
bf53457d JH |
659 | const struct sr_datafeed_logic *logic; |
660 | const struct sr_datafeed_analog *analog; | |
1954dfa9 | 661 | const struct sr_datafeed_analog2 *analog2; |
7d2afd6c BV |
662 | |
663 | switch (packet->type) { | |
664 | case SR_DF_HEADER: | |
a421dc1d | 665 | sr_dbg("bus: Received SR_DF_HEADER packet."); |
7d2afd6c BV |
666 | break; |
667 | case SR_DF_TRIGGER: | |
a421dc1d | 668 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); |
7d2afd6c | 669 | break; |
c71bac3b | 670 | case SR_DF_META: |
a421dc1d | 671 | sr_dbg("bus: Received SR_DF_META packet."); |
ee7489d2 | 672 | break; |
7d2afd6c BV |
673 | case SR_DF_LOGIC: |
674 | logic = packet->payload; | |
7ea45862 UH |
675 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " |
676 | "unitsize = %d).", logic->length, logic->unitsize); | |
7d2afd6c | 677 | break; |
ee7489d2 BV |
678 | case SR_DF_ANALOG: |
679 | analog = packet->payload; | |
a421dc1d UH |
680 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", |
681 | analog->num_samples); | |
ee7489d2 | 682 | break; |
1954dfa9 BV |
683 | case SR_DF_ANALOG2: |
684 | analog2 = packet->payload; | |
685 | sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).", | |
686 | analog2->num_samples); | |
687 | break; | |
7d2afd6c | 688 | case SR_DF_END: |
a421dc1d | 689 | sr_dbg("bus: Received SR_DF_END packet."); |
7d2afd6c | 690 | break; |
6ea7669c | 691 | case SR_DF_FRAME_BEGIN: |
a421dc1d | 692 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); |
6ea7669c BV |
693 | break; |
694 | case SR_DF_FRAME_END: | |
a421dc1d | 695 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); |
6ea7669c | 696 | break; |
7d2afd6c | 697 | default: |
a421dc1d | 698 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); |
9f45fb3a | 699 | break; |
7d2afd6c | 700 | } |
7d2afd6c BV |
701 | } |
702 | ||
9f45fb3a | 703 | /** |
a1645fcd BV |
704 | * Send a packet to whatever is listening on the datafeed bus. |
705 | * | |
706 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 707 | * |
6b2d8d3e | 708 | * @param sdi TODO. |
31ccebc4 | 709 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 710 | * |
04cb9157 MH |
711 | * @retval SR_OK Success. |
712 | * @retval SR_ERR_ARG Invalid argument. | |
b4bd7088 UH |
713 | * |
714 | * @private | |
9f45fb3a | 715 | */ |
de4d3f99 | 716 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
8143cfdc | 717 | const struct sr_datafeed_packet *packet) |
a1bb33af UH |
718 | { |
719 | GSList *l; | |
2726474a | 720 | struct datafeed_callback *cb_struct; |
c0a1e532 UH |
721 | struct sr_datafeed_packet *packet_in, *packet_out; |
722 | struct sr_transform *t; | |
723 | int ret; | |
a1bb33af | 724 | |
de4d3f99 | 725 | if (!sdi) { |
a421dc1d | 726 | sr_err("%s: sdi was NULL", __func__); |
e0508e67 | 727 | return SR_ERR_ARG; |
9f45fb3a UH |
728 | } |
729 | ||
e0508e67 | 730 | if (!packet) { |
a421dc1d | 731 | sr_err("%s: packet was NULL", __func__); |
e0508e67 | 732 | return SR_ERR_ARG; |
9f45fb3a UH |
733 | } |
734 | ||
79f92686 BV |
735 | if (!sdi->session) { |
736 | sr_err("%s: session was NULL", __func__); | |
737 | return SR_ERR_BUG; | |
738 | } | |
739 | ||
c0a1e532 UH |
740 | /* |
741 | * Pass the packet to the first transform module. If that returns | |
742 | * another packet (instead of NULL), pass that packet to the next | |
743 | * transform module in the list, and so on. | |
744 | */ | |
745 | packet_in = (struct sr_datafeed_packet *)packet; | |
746 | for (l = sdi->session->transforms; l; l = l->next) { | |
747 | t = l->data; | |
748 | sr_spew("Running transform module '%s'.", t->module->id); | |
749 | ret = t->module->receive(t, packet_in, &packet_out); | |
750 | if (ret < 0) { | |
751 | sr_err("Error while running transform module: %d.", ret); | |
752 | return SR_ERR; | |
753 | } | |
754 | if (!packet_out) { | |
755 | /* | |
756 | * If any of the transforms don't return an output | |
757 | * packet, abort. | |
758 | */ | |
759 | sr_spew("Transform module didn't return a packet, aborting."); | |
760 | return SR_OK; | |
761 | } else { | |
762 | /* | |
763 | * Use this transform module's output packet as input | |
764 | * for the next transform module. | |
765 | */ | |
766 | packet_in = packet_out; | |
767 | } | |
768 | } | |
769 | ||
770 | /* | |
771 | * If the last transform did output a packet, pass it to all datafeed | |
772 | * callbacks. | |
773 | */ | |
3337e9a1 | 774 | for (l = sdi->session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
775 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
776 | datafeed_dump(packet); | |
2726474a ML |
777 | cb_struct = l->data; |
778 | cb_struct->cb(sdi, packet, cb_struct->cb_data); | |
a1bb33af | 779 | } |
9f45fb3a | 780 | |
e0508e67 | 781 | return SR_OK; |
a1bb33af UH |
782 | } |
783 | ||
6b2d8d3e UH |
784 | /** |
785 | * Add an event source for a file descriptor. | |
786 | * | |
7efe889e | 787 | * @param session The session to use. Must not be NULL. |
6b2d8d3e | 788 | * @param pollfd The GPollFD. |
04cb9157 MH |
789 | * @param[in] timeout Max time to wait before the callback is called, |
790 | * ignored if 0. | |
6b2d8d3e UH |
791 | * @param cb Callback function to add. Must not be NULL. |
792 | * @param cb_data Data for the callback function. Can be NULL. | |
793 | * @param poll_object TODO. | |
794 | * | |
04cb9157 MH |
795 | * @retval SR_OK Success. |
796 | * @retval SR_ERR_ARG Invalid argument. | |
6b2d8d3e | 797 | */ |
102f1239 BV |
798 | static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd, |
799 | int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object) | |
544a4582 BV |
800 | { |
801 | struct source *new_sources, *s; | |
aac0ea25 | 802 | GPollFD *new_pollfds; |
544a4582 | 803 | |
d08490aa | 804 | if (!cb) { |
a421dc1d | 805 | sr_err("%s: cb was NULL", __func__); |
e0508e67 | 806 | return SR_ERR_ARG; |
9f45fb3a UH |
807 | } |
808 | ||
1f9813eb | 809 | /* Note: cb_data can be NULL, that's not a bug. */ |
9f45fb3a | 810 | |
91219afc | 811 | new_pollfds = g_realloc(session->pollfds, |
2ac2e629 | 812 | sizeof(GPollFD) * (session->num_sources + 1)); |
91219afc | 813 | new_sources = g_realloc(session->sources, sizeof(struct source) * |
2ac2e629 | 814 | (session->num_sources + 1)); |
544a4582 | 815 | |
b7e94111 LPC |
816 | new_pollfds[session->num_sources] = *pollfd; |
817 | s = &new_sources[session->num_sources++]; | |
544a4582 | 818 | s->timeout = timeout; |
d08490aa | 819 | s->cb = cb; |
1f9813eb | 820 | s->cb_data = cb_data; |
aac0ea25 | 821 | s->poll_object = poll_object; |
b7e94111 LPC |
822 | session->pollfds = new_pollfds; |
823 | session->sources = new_sources; | |
544a4582 | 824 | |
b7e94111 LPC |
825 | if (timeout != session->source_timeout && timeout > 0 |
826 | && (session->source_timeout == -1 || timeout < session->source_timeout)) | |
827 | session->source_timeout = timeout; | |
9f45fb3a | 828 | |
e0508e67 | 829 | return SR_OK; |
544a4582 BV |
830 | } |
831 | ||
9f45fb3a | 832 | /** |
6b2d8d3e | 833 | * Add an event source for a file descriptor. |
9f45fb3a | 834 | * |
7efe889e | 835 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
836 | * @param fd The file descriptor. |
837 | * @param events Events to check for. | |
838 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
839 | * @param cb Callback function to add. Must not be NULL. | |
840 | * @param cb_data Data for the callback function. Can be NULL. | |
9f45fb3a | 841 | * |
04cb9157 MH |
842 | * @retval SR_OK Success. |
843 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
844 | * |
845 | * @since 0.3.0 | |
aac0ea25 | 846 | */ |
0812c40e ML |
847 | SR_API int sr_session_source_add(struct sr_session *session, int fd, |
848 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 LPC |
849 | { |
850 | GPollFD p; | |
851 | ||
aac0ea25 LPC |
852 | p.fd = fd; |
853 | p.events = events; | |
aac0ea25 | 854 | |
102f1239 | 855 | return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd); |
aac0ea25 LPC |
856 | } |
857 | ||
858 | /** | |
1a895c61 | 859 | * Add an event source for a GPollFD. |
aac0ea25 | 860 | * |
7efe889e | 861 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
862 | * @param pollfd The GPollFD. |
863 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
864 | * @param cb Callback function to add. Must not be NULL. | |
865 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 866 | * |
04cb9157 MH |
867 | * @retval SR_OK Success. |
868 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
869 | * |
870 | * @since 0.3.0 | |
9f45fb3a | 871 | */ |
0812c40e ML |
872 | SR_API int sr_session_source_add_pollfd(struct sr_session *session, |
873 | GPollFD *pollfd, int timeout, sr_receive_data_callback cb, | |
874 | void *cb_data) | |
aac0ea25 | 875 | { |
102f1239 | 876 | return _sr_session_source_add(session, pollfd, timeout, cb, |
8143cfdc | 877 | cb_data, (gintptr)pollfd); |
aac0ea25 LPC |
878 | } |
879 | ||
880 | /** | |
1a895c61 | 881 | * Add an event source for a GIOChannel. |
aac0ea25 | 882 | * |
7efe889e | 883 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
884 | * @param channel The GIOChannel. |
885 | * @param events Events to poll on. | |
886 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
887 | * @param cb Callback function to add. Must not be NULL. | |
888 | * @param cb_data Data for the callback function. Can be NULL. | |
889 | * | |
04cb9157 MH |
890 | * @retval SR_OK Success. |
891 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
892 | * |
893 | * @since 0.3.0 | |
aac0ea25 | 894 | */ |
0812c40e ML |
895 | SR_API int sr_session_source_add_channel(struct sr_session *session, |
896 | GIOChannel *channel, int events, int timeout, | |
897 | sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 LPC |
898 | { |
899 | GPollFD p; | |
900 | ||
901 | #ifdef _WIN32 | |
6b2d8d3e | 902 | g_io_channel_win32_make_pollfd(channel, events, &p); |
aac0ea25 LPC |
903 | #else |
904 | p.fd = g_io_channel_unix_get_fd(channel); | |
905 | p.events = events; | |
906 | #endif | |
907 | ||
102f1239 | 908 | return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel); |
aac0ea25 LPC |
909 | } |
910 | ||
6b2d8d3e UH |
911 | /** |
912 | * Remove the source belonging to the specified channel. | |
913 | * | |
7efe889e | 914 | * @param session The session to use. Must not be NULL. |
04cb9157 | 915 | * @param poll_object The channel for which the source should be removed. |
6b2d8d3e | 916 | * |
04cb9157 MH |
917 | * @retval SR_OK Success |
918 | * @retval SR_ERR_ARG Invalid arguments | |
04cb9157 | 919 | * @retval SR_ERR_BUG Internal error |
6b2d8d3e | 920 | */ |
102f1239 | 921 | static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object) |
544a4582 | 922 | { |
b7e94111 | 923 | unsigned int old; |
544a4582 | 924 | |
b7e94111 | 925 | if (!session->sources || !session->num_sources) { |
a421dc1d | 926 | sr_err("%s: sources was NULL", __func__); |
0abee507 | 927 | return SR_ERR_BUG; |
e0508e67 UH |
928 | } |
929 | ||
b7e94111 LPC |
930 | for (old = 0; old < session->num_sources; old++) { |
931 | if (session->sources[old].poll_object == poll_object) | |
2bccd322 | 932 | break; |
9f45fb3a UH |
933 | } |
934 | ||
2bccd322 | 935 | /* fd not found, nothing to do */ |
b7e94111 | 936 | if (old == session->num_sources) |
2bccd322 LPC |
937 | return SR_OK; |
938 | ||
91219afc | 939 | session->num_sources--; |
2bccd322 | 940 | |
b7e94111 | 941 | if (old != session->num_sources) { |
91219afc | 942 | memmove(&session->pollfds[old], &session->pollfds[old + 1], |
b7e94111 | 943 | (session->num_sources - old) * sizeof(GPollFD)); |
91219afc | 944 | memmove(&session->sources[old], &session->sources[old + 1], |
b7e94111 | 945 | (session->num_sources - old) * sizeof(struct source)); |
9f45fb3a | 946 | } |
544a4582 | 947 | |
91219afc UH |
948 | session->pollfds = g_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); |
949 | session->sources = g_realloc(session->sources, sizeof(struct source) * session->num_sources); | |
2bccd322 | 950 | |
e0508e67 | 951 | return SR_OK; |
544a4582 | 952 | } |
aac0ea25 | 953 | |
6b2d8d3e | 954 | /** |
aac0ea25 LPC |
955 | * Remove the source belonging to the specified file descriptor. |
956 | * | |
7efe889e | 957 | * @param session The session to use. Must not be NULL. |
1a895c61 | 958 | * @param fd The file descriptor for which the source should be removed. |
aac0ea25 | 959 | * |
04cb9157 MH |
960 | * @retval SR_OK Success |
961 | * @retval SR_ERR_ARG Invalid argument | |
04cb9157 | 962 | * @retval SR_ERR_BUG Internal error. |
47117241 UH |
963 | * |
964 | * @since 0.3.0 | |
aac0ea25 | 965 | */ |
0812c40e | 966 | SR_API int sr_session_source_remove(struct sr_session *session, int fd) |
aac0ea25 | 967 | { |
102f1239 | 968 | return _sr_session_source_remove(session, (gintptr)fd); |
aac0ea25 LPC |
969 | } |
970 | ||
971 | /** | |
972 | * Remove the source belonging to the specified poll descriptor. | |
973 | * | |
7efe889e | 974 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
975 | * @param pollfd The poll descriptor for which the source should be removed. |
976 | * | |
977 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
978 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
979 | * internal errors. | |
47117241 UH |
980 | * |
981 | * @since 0.2.0 | |
aac0ea25 | 982 | */ |
0812c40e ML |
983 | SR_API int sr_session_source_remove_pollfd(struct sr_session *session, |
984 | GPollFD *pollfd) | |
aac0ea25 | 985 | { |
102f1239 | 986 | return _sr_session_source_remove(session, (gintptr)pollfd); |
aac0ea25 LPC |
987 | } |
988 | ||
6b2d8d3e | 989 | /** |
aac0ea25 LPC |
990 | * Remove the source belonging to the specified channel. |
991 | * | |
7efe889e | 992 | * @param session The session to use. Must not be NULL. |
1a895c61 | 993 | * @param channel The channel for which the source should be removed. |
aac0ea25 | 994 | * |
04cb9157 MH |
995 | * @retval SR_OK Success. |
996 | * @retval SR_ERR_ARG Invalid argument. | |
04cb9157 | 997 | * @return SR_ERR_BUG Internal error. |
47117241 UH |
998 | * |
999 | * @since 0.2.0 | |
aac0ea25 | 1000 | */ |
0812c40e ML |
1001 | SR_API int sr_session_source_remove_channel(struct sr_session *session, |
1002 | GIOChannel *channel) | |
aac0ea25 | 1003 | { |
102f1239 | 1004 | return _sr_session_source_remove(session, (gintptr)channel); |
aac0ea25 | 1005 | } |
7b870c38 | 1006 | |
ee29d92e | 1007 | static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy) |
8143cfdc | 1008 | { |
8143cfdc | 1009 | g_variant_ref(src->data); |
ee29d92e AJ |
1010 | meta_copy->config = g_slist_append(meta_copy->config, |
1011 | g_memdup(src, sizeof(struct sr_config))); | |
8143cfdc BV |
1012 | } |
1013 | ||
1014 | SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, | |
1015 | struct sr_datafeed_packet **copy) | |
1016 | { | |
1017 | const struct sr_datafeed_meta *meta; | |
1018 | struct sr_datafeed_meta *meta_copy; | |
1019 | const struct sr_datafeed_logic *logic; | |
1020 | struct sr_datafeed_logic *logic_copy; | |
1021 | const struct sr_datafeed_analog *analog; | |
1022 | struct sr_datafeed_analog *analog_copy; | |
1023 | uint8_t *payload; | |
1024 | ||
1025 | *copy = g_malloc0(sizeof(struct sr_datafeed_packet)); | |
1026 | (*copy)->type = packet->type; | |
1027 | ||
1028 | switch (packet->type) { | |
1029 | case SR_DF_TRIGGER: | |
1030 | case SR_DF_END: | |
1031 | /* No payload. */ | |
1032 | break; | |
1033 | case SR_DF_HEADER: | |
1034 | payload = g_malloc(sizeof(struct sr_datafeed_header)); | |
1035 | memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header)); | |
1036 | (*copy)->payload = payload; | |
1037 | break; | |
1038 | case SR_DF_META: | |
1039 | meta = packet->payload; | |
ee29d92e AJ |
1040 | meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta)); |
1041 | g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config); | |
8143cfdc BV |
1042 | (*copy)->payload = meta_copy; |
1043 | break; | |
1044 | case SR_DF_LOGIC: | |
1045 | logic = packet->payload; | |
1046 | logic_copy = g_malloc(sizeof(logic)); | |
1047 | logic_copy->length = logic->length; | |
1048 | logic_copy->unitsize = logic->unitsize; | |
1049 | memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize); | |
1050 | (*copy)->payload = logic_copy; | |
1051 | break; | |
1052 | case SR_DF_ANALOG: | |
1053 | analog = packet->payload; | |
1054 | analog_copy = g_malloc(sizeof(analog)); | |
1055 | analog_copy->channels = g_slist_copy(analog->channels); | |
1056 | analog_copy->num_samples = analog->num_samples; | |
1057 | analog_copy->mq = analog->mq; | |
1058 | analog_copy->unit = analog->unit; | |
1059 | analog_copy->mqflags = analog->mqflags; | |
1060 | memcpy(analog_copy->data, analog->data, | |
1061 | analog->num_samples * sizeof(float)); | |
1062 | (*copy)->payload = analog_copy; | |
1063 | break; | |
1064 | default: | |
1065 | sr_err("Unknown packet type %d", packet->type); | |
1066 | return SR_ERR; | |
1067 | } | |
1068 | ||
1069 | return SR_OK; | |
1070 | } | |
1071 | ||
1072 | void sr_packet_free(struct sr_datafeed_packet *packet) | |
1073 | { | |
1074 | const struct sr_datafeed_meta *meta; | |
1075 | const struct sr_datafeed_logic *logic; | |
1076 | const struct sr_datafeed_analog *analog; | |
1077 | struct sr_config *src; | |
1078 | GSList *l; | |
1079 | ||
1080 | switch (packet->type) { | |
1081 | case SR_DF_TRIGGER: | |
1082 | case SR_DF_END: | |
1083 | /* No payload. */ | |
1084 | break; | |
1085 | case SR_DF_HEADER: | |
1086 | /* Payload is a simple struct. */ | |
1087 | g_free((void *)packet->payload); | |
1088 | break; | |
1089 | case SR_DF_META: | |
1090 | meta = packet->payload; | |
1091 | for (l = meta->config; l; l = l->next) { | |
1092 | src = l->data; | |
1093 | g_variant_unref(src->data); | |
1094 | g_free(src); | |
1095 | } | |
1096 | g_slist_free(meta->config); | |
1097 | g_free((void *)packet->payload); | |
1098 | break; | |
1099 | case SR_DF_LOGIC: | |
1100 | logic = packet->payload; | |
1101 | g_free(logic->data); | |
1102 | g_free((void *)packet->payload); | |
1103 | break; | |
1104 | case SR_DF_ANALOG: | |
1105 | analog = packet->payload; | |
1106 | g_slist_free(analog->channels); | |
1107 | g_free(analog->data); | |
1108 | g_free((void *)packet->payload); | |
1109 | break; | |
1110 | default: | |
1111 | sr_err("Unknown packet type %d", packet->type); | |
1112 | } | |
1113 | g_free(packet); | |
1114 | ||
1115 | } | |
1116 | ||
7b870c38 | 1117 | /** @} */ |