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