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