]>
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 | |
2726474a ML |
309 | if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback)))) |
310 | return SR_ERR_MALLOC; | |
311 | ||
312 | cb_struct->cb = cb; | |
313 | cb_struct->cb_data = cb_data; | |
314 | ||
62c82025 | 315 | session->datafeed_callbacks = |
2726474a | 316 | g_slist_append(session->datafeed_callbacks, cb_struct); |
e0508e67 UH |
317 | |
318 | return SR_OK; | |
a1bb33af UH |
319 | } |
320 | ||
0812c40e | 321 | SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session) |
7b5e6d29 BV |
322 | { |
323 | return session->trigger; | |
324 | } | |
325 | ||
0812c40e | 326 | SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig) |
7b5e6d29 BV |
327 | { |
328 | session->trigger = trig; | |
329 | ||
330 | return SR_OK; | |
331 | } | |
332 | ||
b483be74 | 333 | /** |
0812c40e | 334 | * Call every device in the current session's callback. |
b483be74 BV |
335 | * |
336 | * For sessions not driven by select loops such as sr_session_run(), | |
337 | * but driven by another scheduler, this can be used to poll the devices | |
338 | * from within that scheduler. | |
339 | * | |
7efe889e | 340 | * @param session The session to use. Must not be NULL. |
f6eb2cb5 BV |
341 | * @param block If TRUE, this call will wait for any of the session's |
342 | * sources to fire an event on the file descriptors, or | |
343 | * any of their timeouts to activate. In other words, this | |
344 | * can be used as a select loop. | |
345 | * If FALSE, all sources have their callback run, regardless | |
346 | * of file descriptor or timeout status. | |
347 | * | |
04cb9157 MH |
348 | * @retval SR_OK Success. |
349 | * @retval SR_ERR Error occured. | |
b483be74 | 350 | */ |
3337e9a1 | 351 | static int sr_session_iteration(struct sr_session *session, gboolean block) |
544a4582 | 352 | { |
b7e94111 LPC |
353 | unsigned int i; |
354 | int ret; | |
544a4582 | 355 | |
b483be74 BV |
356 | ret = g_poll(session->pollfds, session->num_sources, |
357 | block ? session->source_timeout : 0); | |
358 | for (i = 0; i < session->num_sources; i++) { | |
359 | if (session->pollfds[i].revents > 0 || (ret == 0 | |
360 | && session->source_timeout == session->sources[i].timeout)) { | |
33c6e4c5 | 361 | /* |
b483be74 BV |
362 | * Invoke the source's callback on an event, |
363 | * or if the poll timed out and this source | |
364 | * asked for that timeout. | |
33c6e4c5 | 365 | */ |
b483be74 BV |
366 | if (!session->sources[i].cb(session->pollfds[i].fd, |
367 | session->pollfds[i].revents, | |
368 | session->sources[i].cb_data)) | |
0812c40e ML |
369 | sr_session_source_remove(session, |
370 | session->sources[i].poll_object); | |
b483be74 BV |
371 | } |
372 | /* | |
373 | * We want to take as little time as possible to stop | |
374 | * the session if we have been told to do so. Therefore, | |
375 | * we check the flag after processing every source, not | |
376 | * just once per main event loop. | |
377 | */ | |
378 | g_mutex_lock(&session->stop_mutex); | |
379 | if (session->abort_session) { | |
0812c40e | 380 | sr_session_stop_sync(session); |
b483be74 BV |
381 | /* But once is enough. */ |
382 | session->abort_session = FALSE; | |
544a4582 | 383 | } |
b483be74 | 384 | g_mutex_unlock(&session->stop_mutex); |
544a4582 | 385 | } |
e0508e67 UH |
386 | |
387 | return SR_OK; | |
544a4582 BV |
388 | } |
389 | ||
7b5e6d29 BV |
390 | |
391 | static int verify_trigger(struct sr_trigger *trigger) | |
392 | { | |
393 | struct sr_trigger_stage *stage; | |
394 | struct sr_trigger_match *match; | |
395 | GSList *l, *m; | |
396 | ||
397 | if (!trigger->stages) { | |
398 | sr_err("No trigger stages defined."); | |
399 | return SR_ERR; | |
400 | } | |
401 | ||
402 | sr_spew("Checking trigger:"); | |
403 | for (l = trigger->stages; l; l = l->next) { | |
404 | stage = l->data; | |
405 | if (!stage->matches) { | |
406 | sr_err("Stage %d has no matches defined.", stage->stage); | |
407 | return SR_ERR; | |
408 | } | |
409 | for (m = stage->matches; m; m = m->next) { | |
410 | match = m->data; | |
411 | if (!match->channel) { | |
412 | sr_err("Stage %d match has no channel.", stage->stage); | |
413 | return SR_ERR; | |
414 | } | |
415 | if (!match->match) { | |
416 | sr_err("Stage %d match is not defined.", stage->stage); | |
417 | return SR_ERR; | |
418 | } | |
419 | sr_spew("Stage %d match on channel %s, match %d", stage->stage, | |
420 | match->channel->name, match->match); | |
421 | } | |
422 | } | |
423 | ||
424 | return SR_OK; | |
425 | } | |
9f45fb3a UH |
426 | /** |
427 | * Start a session. | |
428 | * | |
7efe889e UH |
429 | * @param session The session to use. Must not be NULL. |
430 | * | |
04cb9157 | 431 | * @retval SR_OK Success. |
0812c40e | 432 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 433 | * |
0812c40e | 434 | * @since 0.4.0 |
9f45fb3a | 435 | */ |
0812c40e | 436 | SR_API int sr_session_start(struct sr_session *session) |
7d658874 | 437 | { |
de4d3f99 | 438 | struct sr_dev_inst *sdi; |
013ec84b BV |
439 | struct sr_channel *ch; |
440 | GSList *l, *c; | |
441 | int enabled_channels, ret; | |
7d658874 | 442 | |
9f45fb3a | 443 | if (!session) { |
0812c40e ML |
444 | sr_err("%s: session was NULL", __func__); |
445 | return SR_ERR_ARG; | |
9f45fb3a UH |
446 | } |
447 | ||
bb7ef793 | 448 | if (!session->devs) { |
a421dc1d | 449 | sr_err("%s: session->devs was NULL; a session " |
9f45fb3a | 450 | "cannot be started without devices.", __func__); |
0812c40e | 451 | return SR_ERR_ARG; |
9f45fb3a UH |
452 | } |
453 | ||
7b5e6d29 BV |
454 | if (session->trigger && verify_trigger(session->trigger) != SR_OK) |
455 | return SR_ERR; | |
456 | ||
c7142604 | 457 | sr_info("Starting."); |
9f45fb3a | 458 | |
b7c3e849 | 459 | ret = SR_OK; |
bb7ef793 | 460 | for (l = session->devs; l; l = l->next) { |
de4d3f99 | 461 | sdi = l->data; |
013ec84b BV |
462 | enabled_channels = 0; |
463 | for (c = sdi->channels; c; c = c->next) { | |
464 | ch = c->data; | |
465 | if (ch->enabled) { | |
466 | enabled_channels++; | |
467 | break; | |
468 | } | |
469 | } | |
470 | if (enabled_channels == 0) { | |
471 | ret = SR_ERR; | |
1b9e567b SA |
472 | sr_err("%s using connection %s has no enabled channels!", |
473 | sdi->driver->name, sdi->connection_id); | |
013ec84b BV |
474 | break; |
475 | } | |
476 | ||
32b7cd4f DE |
477 | if ((ret = sr_config_commit(sdi)) != SR_OK) { |
478 | sr_err("Failed to commit device settings before " | |
479 | "starting acquisition (%s)", sr_strerror(ret)); | |
480 | break; | |
481 | } | |
de4d3f99 | 482 | if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) { |
a421dc1d | 483 | sr_err("%s: could not start an acquisition " |
568dcacc | 484 | "(%s)", __func__, sr_strerror(ret)); |
7d658874 | 485 | break; |
9f45fb3a | 486 | } |
7d658874 BV |
487 | } |
488 | ||
9f45fb3a UH |
489 | /* TODO: What if there are multiple devices? Which return code? */ |
490 | ||
7d658874 BV |
491 | return ret; |
492 | } | |
493 | ||
9f45fb3a | 494 | /** |
0812c40e | 495 | * Run a session. |
9f45fb3a | 496 | * |
7efe889e UH |
497 | * @param session The session to use. Must not be NULL. |
498 | * | |
04cb9157 | 499 | * @retval SR_OK Success. |
0812c40e | 500 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 501 | * |
0812c40e | 502 | * @since 0.4.0 |
9f45fb3a | 503 | */ |
0812c40e | 504 | SR_API int sr_session_run(struct sr_session *session) |
7d658874 | 505 | { |
9f45fb3a | 506 | if (!session) { |
0812c40e ML |
507 | sr_err("%s: session was NULL", __func__); |
508 | return SR_ERR_ARG; | |
9f45fb3a UH |
509 | } |
510 | ||
bb7ef793 | 511 | if (!session->devs) { |
9f45fb3a | 512 | /* TODO: Actually the case? */ |
a421dc1d | 513 | sr_err("%s: session->devs was NULL; a session " |
9f45fb3a | 514 | "cannot be run without devices.", __func__); |
0812c40e | 515 | return SR_ERR_ARG; |
9f45fb3a | 516 | } |
5451816f | 517 | session->running = TRUE; |
9f45fb3a | 518 | |
a421dc1d | 519 | sr_info("Running."); |
7d658874 | 520 | |
9f45fb3a | 521 | /* Do we have real sources? */ |
b7e94111 | 522 | if (session->num_sources == 1 && session->pollfds[0].fd == -1) { |
9f45fb3a | 523 | /* Dummy source, freewheel over it. */ |
2cbeb2b7 | 524 | while (session->num_sources) |
b7e94111 | 525 | session->sources[0].cb(-1, 0, session->sources[0].cb_data); |
9f45fb3a UH |
526 | } else { |
527 | /* Real sources, use g_poll() main loop. */ | |
b483be74 | 528 | while (session->num_sources) |
3337e9a1 | 529 | sr_session_iteration(session, TRUE); |
9f45fb3a UH |
530 | } |
531 | ||
e0508e67 | 532 | return SR_OK; |
7d658874 BV |
533 | } |
534 | ||
9f45fb3a | 535 | /** |
0812c40e | 536 | * Stop a session. |
9f45fb3a | 537 | * |
0812c40e ML |
538 | * The session is stopped immediately, with all acquisition sessions stopped |
539 | * and hardware drivers cleaned up. | |
9f45fb3a | 540 | * |
33c6e4c5 AG |
541 | * This must be called from within the session thread, to prevent freeing |
542 | * resources that the session thread will try to use. | |
543 | * | |
7efe889e UH |
544 | * @param session The session to use. Must not be NULL. |
545 | * | |
04cb9157 | 546 | * @retval SR_OK Success. |
0812c40e | 547 | * @retval SR_ERR_ARG Invalid session passed. |
72a08bcc BV |
548 | * |
549 | * @private | |
9f45fb3a | 550 | */ |
0812c40e | 551 | SR_PRIV int sr_session_stop_sync(struct sr_session *session) |
a1bb33af | 552 | { |
de4d3f99 | 553 | struct sr_dev_inst *sdi; |
a1bb33af UH |
554 | GSList *l; |
555 | ||
9f45fb3a | 556 | if (!session) { |
a421dc1d | 557 | sr_err("%s: session was NULL", __func__); |
0812c40e | 558 | return SR_ERR_ARG; |
9f45fb3a UH |
559 | } |
560 | ||
a421dc1d | 561 | sr_info("Stopping."); |
e0508e67 | 562 | |
bb7ef793 | 563 | for (l = session->devs; l; l = l->next) { |
de4d3f99 BV |
564 | sdi = l->data; |
565 | if (sdi->driver) { | |
566 | if (sdi->driver->dev_acquisition_stop) | |
567 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
8c76be53 | 568 | } |
a1bb33af | 569 | } |
5451816f | 570 | session->running = FALSE; |
9f45fb3a | 571 | |
e0508e67 | 572 | return SR_OK; |
a1bb33af UH |
573 | } |
574 | ||
33c6e4c5 | 575 | /** |
0812c40e | 576 | * Stop a session. |
33c6e4c5 | 577 | * |
0812c40e ML |
578 | * The session is stopped immediately, with all acquisition sessions being |
579 | * stopped and hardware drivers cleaned up. | |
33c6e4c5 AG |
580 | * |
581 | * If the session is run in a separate thread, this function will not block | |
582 | * until the session is finished executing. It is the caller's responsibility | |
583 | * to wait for the session thread to return before assuming that the session is | |
584 | * completely decommissioned. | |
585 | * | |
7efe889e UH |
586 | * @param session The session to use. Must not be NULL. |
587 | * | |
04cb9157 | 588 | * @retval SR_OK Success. |
0812c40e | 589 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 590 | * |
0812c40e | 591 | * @since 0.4.0 |
33c6e4c5 | 592 | */ |
0812c40e | 593 | SR_API int sr_session_stop(struct sr_session *session) |
33c6e4c5 AG |
594 | { |
595 | if (!session) { | |
596 | sr_err("%s: session was NULL", __func__); | |
597 | return SR_ERR_BUG; | |
598 | } | |
599 | ||
600 | g_mutex_lock(&session->stop_mutex); | |
601 | session->abort_session = TRUE; | |
602 | g_mutex_unlock(&session->stop_mutex); | |
603 | ||
604 | return SR_OK; | |
605 | } | |
606 | ||
9f45fb3a | 607 | /** |
a1645fcd | 608 | * Debug helper. |
9f45fb3a | 609 | * |
996b0c72 | 610 | * @param packet The packet to show debugging information for. |
9f45fb3a | 611 | */ |
bf53457d | 612 | static void datafeed_dump(const struct sr_datafeed_packet *packet) |
7d2afd6c | 613 | { |
bf53457d JH |
614 | const struct sr_datafeed_logic *logic; |
615 | const struct sr_datafeed_analog *analog; | |
7d2afd6c BV |
616 | |
617 | switch (packet->type) { | |
618 | case SR_DF_HEADER: | |
a421dc1d | 619 | sr_dbg("bus: Received SR_DF_HEADER packet."); |
7d2afd6c BV |
620 | break; |
621 | case SR_DF_TRIGGER: | |
a421dc1d | 622 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); |
7d2afd6c | 623 | break; |
c71bac3b | 624 | case SR_DF_META: |
a421dc1d | 625 | sr_dbg("bus: Received SR_DF_META packet."); |
ee7489d2 | 626 | break; |
7d2afd6c BV |
627 | case SR_DF_LOGIC: |
628 | logic = packet->payload; | |
7ea45862 UH |
629 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " |
630 | "unitsize = %d).", logic->length, logic->unitsize); | |
7d2afd6c | 631 | break; |
ee7489d2 BV |
632 | case SR_DF_ANALOG: |
633 | analog = packet->payload; | |
a421dc1d UH |
634 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", |
635 | analog->num_samples); | |
ee7489d2 | 636 | break; |
7d2afd6c | 637 | case SR_DF_END: |
a421dc1d | 638 | sr_dbg("bus: Received SR_DF_END packet."); |
7d2afd6c | 639 | break; |
6ea7669c | 640 | case SR_DF_FRAME_BEGIN: |
a421dc1d | 641 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); |
6ea7669c BV |
642 | break; |
643 | case SR_DF_FRAME_END: | |
a421dc1d | 644 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); |
6ea7669c | 645 | break; |
7d2afd6c | 646 | default: |
a421dc1d | 647 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); |
9f45fb3a | 648 | break; |
7d2afd6c | 649 | } |
7d2afd6c BV |
650 | } |
651 | ||
9f45fb3a | 652 | /** |
a1645fcd BV |
653 | * Send a packet to whatever is listening on the datafeed bus. |
654 | * | |
655 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 656 | * |
6b2d8d3e | 657 | * @param sdi TODO. |
31ccebc4 | 658 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 659 | * |
04cb9157 MH |
660 | * @retval SR_OK Success. |
661 | * @retval SR_ERR_ARG Invalid argument. | |
b4bd7088 UH |
662 | * |
663 | * @private | |
9f45fb3a | 664 | */ |
de4d3f99 | 665 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
8143cfdc | 666 | const struct sr_datafeed_packet *packet) |
a1bb33af UH |
667 | { |
668 | GSList *l; | |
2726474a | 669 | struct datafeed_callback *cb_struct; |
a1bb33af | 670 | |
de4d3f99 | 671 | if (!sdi) { |
a421dc1d | 672 | sr_err("%s: sdi was NULL", __func__); |
e0508e67 | 673 | return SR_ERR_ARG; |
9f45fb3a UH |
674 | } |
675 | ||
e0508e67 | 676 | if (!packet) { |
a421dc1d | 677 | sr_err("%s: packet was NULL", __func__); |
e0508e67 | 678 | return SR_ERR_ARG; |
9f45fb3a UH |
679 | } |
680 | ||
79f92686 BV |
681 | if (!sdi->session) { |
682 | sr_err("%s: session was NULL", __func__); | |
683 | return SR_ERR_BUG; | |
684 | } | |
685 | ||
3337e9a1 | 686 | for (l = sdi->session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
687 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
688 | datafeed_dump(packet); | |
2726474a ML |
689 | cb_struct = l->data; |
690 | cb_struct->cb(sdi, packet, cb_struct->cb_data); | |
a1bb33af | 691 | } |
9f45fb3a | 692 | |
e0508e67 | 693 | return SR_OK; |
a1bb33af UH |
694 | } |
695 | ||
6b2d8d3e UH |
696 | /** |
697 | * Add an event source for a file descriptor. | |
698 | * | |
7efe889e | 699 | * @param session The session to use. Must not be NULL. |
6b2d8d3e | 700 | * @param pollfd The GPollFD. |
04cb9157 MH |
701 | * @param[in] timeout Max time to wait before the callback is called, |
702 | * ignored if 0. | |
6b2d8d3e UH |
703 | * @param cb Callback function to add. Must not be NULL. |
704 | * @param cb_data Data for the callback function. Can be NULL. | |
705 | * @param poll_object TODO. | |
706 | * | |
04cb9157 MH |
707 | * @retval SR_OK Success. |
708 | * @retval SR_ERR_ARG Invalid argument. | |
709 | * @retval SR_ERR_MALLOC Memory allocation error. | |
6b2d8d3e | 710 | */ |
102f1239 BV |
711 | static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd, |
712 | int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object) | |
544a4582 BV |
713 | { |
714 | struct source *new_sources, *s; | |
aac0ea25 | 715 | GPollFD *new_pollfds; |
544a4582 | 716 | |
d08490aa | 717 | if (!cb) { |
a421dc1d | 718 | sr_err("%s: cb was NULL", __func__); |
e0508e67 | 719 | return SR_ERR_ARG; |
9f45fb3a UH |
720 | } |
721 | ||
1f9813eb | 722 | /* Note: cb_data can be NULL, that's not a bug. */ |
9f45fb3a | 723 | |
2ac2e629 BV |
724 | new_pollfds = g_try_realloc(session->pollfds, |
725 | sizeof(GPollFD) * (session->num_sources + 1)); | |
0687dfcd | 726 | if (!new_pollfds) { |
a421dc1d | 727 | sr_err("%s: new_pollfds malloc failed", __func__); |
0687dfcd LPC |
728 | return SR_ERR_MALLOC; |
729 | } | |
730 | ||
b7e94111 | 731 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * |
2ac2e629 | 732 | (session->num_sources + 1)); |
9f45fb3a | 733 | if (!new_sources) { |
a421dc1d | 734 | sr_err("%s: new_sources malloc failed", __func__); |
e0508e67 | 735 | return SR_ERR_MALLOC; |
9f45fb3a | 736 | } |
544a4582 | 737 | |
b7e94111 LPC |
738 | new_pollfds[session->num_sources] = *pollfd; |
739 | s = &new_sources[session->num_sources++]; | |
544a4582 | 740 | s->timeout = timeout; |
d08490aa | 741 | s->cb = cb; |
1f9813eb | 742 | s->cb_data = cb_data; |
aac0ea25 | 743 | s->poll_object = poll_object; |
b7e94111 LPC |
744 | session->pollfds = new_pollfds; |
745 | session->sources = new_sources; | |
544a4582 | 746 | |
b7e94111 LPC |
747 | if (timeout != session->source_timeout && timeout > 0 |
748 | && (session->source_timeout == -1 || timeout < session->source_timeout)) | |
749 | session->source_timeout = timeout; | |
9f45fb3a | 750 | |
e0508e67 | 751 | return SR_OK; |
544a4582 BV |
752 | } |
753 | ||
9f45fb3a | 754 | /** |
6b2d8d3e | 755 | * Add an event source for a file descriptor. |
9f45fb3a | 756 | * |
7efe889e | 757 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
758 | * @param fd The file descriptor. |
759 | * @param events Events to check for. | |
760 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
761 | * @param cb Callback function to add. Must not be NULL. | |
762 | * @param cb_data Data for the callback function. Can be NULL. | |
9f45fb3a | 763 | * |
04cb9157 MH |
764 | * @retval SR_OK Success. |
765 | * @retval SR_ERR_ARG Invalid argument. | |
766 | * @retval SR_ERR_MALLOC Memory allocation error. | |
47117241 UH |
767 | * |
768 | * @since 0.3.0 | |
aac0ea25 | 769 | */ |
0812c40e ML |
770 | SR_API int sr_session_source_add(struct sr_session *session, int fd, |
771 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 LPC |
772 | { |
773 | GPollFD p; | |
774 | ||
aac0ea25 LPC |
775 | p.fd = fd; |
776 | p.events = events; | |
aac0ea25 | 777 | |
102f1239 | 778 | return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd); |
aac0ea25 LPC |
779 | } |
780 | ||
781 | /** | |
1a895c61 | 782 | * Add an event source for a GPollFD. |
aac0ea25 | 783 | * |
7efe889e | 784 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
785 | * @param pollfd The GPollFD. |
786 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
787 | * @param cb Callback function to add. Must not be NULL. | |
788 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 789 | * |
04cb9157 MH |
790 | * @retval SR_OK Success. |
791 | * @retval SR_ERR_ARG Invalid argument. | |
792 | * @retval SR_ERR_MALLOC Memory allocation error. | |
47117241 UH |
793 | * |
794 | * @since 0.3.0 | |
9f45fb3a | 795 | */ |
0812c40e ML |
796 | SR_API int sr_session_source_add_pollfd(struct sr_session *session, |
797 | GPollFD *pollfd, int timeout, sr_receive_data_callback cb, | |
798 | void *cb_data) | |
aac0ea25 | 799 | { |
102f1239 | 800 | return _sr_session_source_add(session, pollfd, timeout, cb, |
8143cfdc | 801 | cb_data, (gintptr)pollfd); |
aac0ea25 LPC |
802 | } |
803 | ||
804 | /** | |
1a895c61 | 805 | * Add an event source for a GIOChannel. |
aac0ea25 | 806 | * |
7efe889e | 807 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
808 | * @param channel The GIOChannel. |
809 | * @param events Events to poll on. | |
810 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
811 | * @param cb Callback function to add. Must not be NULL. | |
812 | * @param cb_data Data for the callback function. Can be NULL. | |
813 | * | |
04cb9157 MH |
814 | * @retval SR_OK Success. |
815 | * @retval SR_ERR_ARG Invalid argument. | |
816 | * @retval SR_ERR_MALLOC Memory allocation error. | |
47117241 UH |
817 | * |
818 | * @since 0.3.0 | |
aac0ea25 | 819 | */ |
0812c40e ML |
820 | SR_API int sr_session_source_add_channel(struct sr_session *session, |
821 | GIOChannel *channel, int events, int timeout, | |
822 | sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 LPC |
823 | { |
824 | GPollFD p; | |
825 | ||
826 | #ifdef _WIN32 | |
6b2d8d3e | 827 | g_io_channel_win32_make_pollfd(channel, events, &p); |
aac0ea25 LPC |
828 | #else |
829 | p.fd = g_io_channel_unix_get_fd(channel); | |
830 | p.events = events; | |
831 | #endif | |
832 | ||
102f1239 | 833 | return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel); |
aac0ea25 LPC |
834 | } |
835 | ||
6b2d8d3e UH |
836 | /** |
837 | * Remove the source belonging to the specified channel. | |
838 | * | |
839 | * @todo Add more error checks and logging. | |
840 | * | |
7efe889e | 841 | * @param session The session to use. Must not be NULL. |
04cb9157 | 842 | * @param poll_object The channel for which the source should be removed. |
6b2d8d3e | 843 | * |
04cb9157 MH |
844 | * @retval SR_OK Success |
845 | * @retval SR_ERR_ARG Invalid arguments | |
846 | * @retval SR_ERR_MALLOC Memory allocation error | |
847 | * @retval SR_ERR_BUG Internal error | |
6b2d8d3e | 848 | */ |
102f1239 | 849 | static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object) |
544a4582 BV |
850 | { |
851 | struct source *new_sources; | |
0687dfcd | 852 | GPollFD *new_pollfds; |
b7e94111 | 853 | unsigned int old; |
544a4582 | 854 | |
b7e94111 | 855 | if (!session->sources || !session->num_sources) { |
a421dc1d | 856 | sr_err("%s: sources was NULL", __func__); |
0abee507 | 857 | return SR_ERR_BUG; |
e0508e67 UH |
858 | } |
859 | ||
b7e94111 LPC |
860 | for (old = 0; old < session->num_sources; old++) { |
861 | if (session->sources[old].poll_object == poll_object) | |
2bccd322 | 862 | break; |
9f45fb3a UH |
863 | } |
864 | ||
2bccd322 | 865 | /* fd not found, nothing to do */ |
b7e94111 | 866 | if (old == session->num_sources) |
2bccd322 LPC |
867 | return SR_OK; |
868 | ||
b7e94111 | 869 | session->num_sources -= 1; |
2bccd322 | 870 | |
b7e94111 LPC |
871 | if (old != session->num_sources) { |
872 | memmove(&session->pollfds[old], &session->pollfds[old+1], | |
873 | (session->num_sources - old) * sizeof(GPollFD)); | |
874 | memmove(&session->sources[old], &session->sources[old+1], | |
875 | (session->num_sources - old) * sizeof(struct source)); | |
9f45fb3a | 876 | } |
544a4582 | 877 | |
b7e94111 LPC |
878 | new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); |
879 | if (!new_pollfds && session->num_sources > 0) { | |
a421dc1d | 880 | sr_err("%s: new_pollfds malloc failed", __func__); |
0687dfcd LPC |
881 | return SR_ERR_MALLOC; |
882 | } | |
883 | ||
b7e94111 LPC |
884 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources); |
885 | if (!new_sources && session->num_sources > 0) { | |
a421dc1d | 886 | sr_err("%s: new_sources malloc failed", __func__); |
2bccd322 | 887 | return SR_ERR_MALLOC; |
544a4582 | 888 | } |
e0508e67 | 889 | |
b7e94111 LPC |
890 | session->pollfds = new_pollfds; |
891 | session->sources = new_sources; | |
2bccd322 | 892 | |
e0508e67 | 893 | return SR_OK; |
544a4582 | 894 | } |
aac0ea25 | 895 | |
6b2d8d3e | 896 | /** |
aac0ea25 LPC |
897 | * Remove the source belonging to the specified file descriptor. |
898 | * | |
7efe889e | 899 | * @param session The session to use. Must not be NULL. |
1a895c61 | 900 | * @param fd The file descriptor for which the source should be removed. |
aac0ea25 | 901 | * |
04cb9157 MH |
902 | * @retval SR_OK Success |
903 | * @retval SR_ERR_ARG Invalid argument | |
904 | * @retval SR_ERR_MALLOC Memory allocation error. | |
905 | * @retval SR_ERR_BUG Internal error. | |
47117241 UH |
906 | * |
907 | * @since 0.3.0 | |
aac0ea25 | 908 | */ |
0812c40e | 909 | SR_API int sr_session_source_remove(struct sr_session *session, int fd) |
aac0ea25 | 910 | { |
102f1239 | 911 | return _sr_session_source_remove(session, (gintptr)fd); |
aac0ea25 LPC |
912 | } |
913 | ||
914 | /** | |
915 | * Remove the source belonging to the specified poll descriptor. | |
916 | * | |
7efe889e | 917 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
918 | * @param pollfd The poll descriptor for which the source should be removed. |
919 | * | |
920 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
921 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
922 | * internal errors. | |
47117241 UH |
923 | * |
924 | * @since 0.2.0 | |
aac0ea25 | 925 | */ |
0812c40e ML |
926 | SR_API int sr_session_source_remove_pollfd(struct sr_session *session, |
927 | GPollFD *pollfd) | |
aac0ea25 | 928 | { |
102f1239 | 929 | return _sr_session_source_remove(session, (gintptr)pollfd); |
aac0ea25 LPC |
930 | } |
931 | ||
6b2d8d3e | 932 | /** |
aac0ea25 LPC |
933 | * Remove the source belonging to the specified channel. |
934 | * | |
7efe889e | 935 | * @param session The session to use. Must not be NULL. |
1a895c61 | 936 | * @param channel The channel for which the source should be removed. |
aac0ea25 | 937 | * |
04cb9157 MH |
938 | * @retval SR_OK Success. |
939 | * @retval SR_ERR_ARG Invalid argument. | |
940 | * @retval SR_ERR_MALLOC Memory allocation error. | |
941 | * @return SR_ERR_BUG Internal error. | |
47117241 UH |
942 | * |
943 | * @since 0.2.0 | |
aac0ea25 | 944 | */ |
0812c40e ML |
945 | SR_API int sr_session_source_remove_channel(struct sr_session *session, |
946 | GIOChannel *channel) | |
aac0ea25 | 947 | { |
102f1239 | 948 | return _sr_session_source_remove(session, (gintptr)channel); |
aac0ea25 | 949 | } |
7b870c38 | 950 | |
8143cfdc BV |
951 | static void *copy_src(struct sr_config *src) |
952 | { | |
953 | struct sr_config *new_src; | |
954 | ||
955 | new_src = g_malloc(sizeof(struct sr_config)); | |
956 | memcpy(new_src, src, sizeof(struct sr_config)); | |
957 | g_variant_ref(src->data); | |
958 | ||
959 | return new_src; | |
960 | } | |
961 | ||
962 | SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, | |
963 | struct sr_datafeed_packet **copy) | |
964 | { | |
965 | const struct sr_datafeed_meta *meta; | |
966 | struct sr_datafeed_meta *meta_copy; | |
967 | const struct sr_datafeed_logic *logic; | |
968 | struct sr_datafeed_logic *logic_copy; | |
969 | const struct sr_datafeed_analog *analog; | |
970 | struct sr_datafeed_analog *analog_copy; | |
971 | uint8_t *payload; | |
972 | ||
973 | *copy = g_malloc0(sizeof(struct sr_datafeed_packet)); | |
974 | (*copy)->type = packet->type; | |
975 | ||
976 | switch (packet->type) { | |
977 | case SR_DF_TRIGGER: | |
978 | case SR_DF_END: | |
979 | /* No payload. */ | |
980 | break; | |
981 | case SR_DF_HEADER: | |
982 | payload = g_malloc(sizeof(struct sr_datafeed_header)); | |
983 | memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header)); | |
984 | (*copy)->payload = payload; | |
985 | break; | |
986 | case SR_DF_META: | |
987 | meta = packet->payload; | |
988 | meta_copy = g_malloc(sizeof(struct sr_datafeed_meta)); | |
989 | meta_copy->config = g_slist_copy_deep(meta->config, | |
990 | (GCopyFunc)copy_src, NULL); | |
991 | (*copy)->payload = meta_copy; | |
992 | break; | |
993 | case SR_DF_LOGIC: | |
994 | logic = packet->payload; | |
995 | logic_copy = g_malloc(sizeof(logic)); | |
996 | logic_copy->length = logic->length; | |
997 | logic_copy->unitsize = logic->unitsize; | |
998 | memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize); | |
999 | (*copy)->payload = logic_copy; | |
1000 | break; | |
1001 | case SR_DF_ANALOG: | |
1002 | analog = packet->payload; | |
1003 | analog_copy = g_malloc(sizeof(analog)); | |
1004 | analog_copy->channels = g_slist_copy(analog->channels); | |
1005 | analog_copy->num_samples = analog->num_samples; | |
1006 | analog_copy->mq = analog->mq; | |
1007 | analog_copy->unit = analog->unit; | |
1008 | analog_copy->mqflags = analog->mqflags; | |
1009 | memcpy(analog_copy->data, analog->data, | |
1010 | analog->num_samples * sizeof(float)); | |
1011 | (*copy)->payload = analog_copy; | |
1012 | break; | |
1013 | default: | |
1014 | sr_err("Unknown packet type %d", packet->type); | |
1015 | return SR_ERR; | |
1016 | } | |
1017 | ||
1018 | return SR_OK; | |
1019 | } | |
1020 | ||
1021 | void sr_packet_free(struct sr_datafeed_packet *packet) | |
1022 | { | |
1023 | const struct sr_datafeed_meta *meta; | |
1024 | const struct sr_datafeed_logic *logic; | |
1025 | const struct sr_datafeed_analog *analog; | |
1026 | struct sr_config *src; | |
1027 | GSList *l; | |
1028 | ||
1029 | switch (packet->type) { | |
1030 | case SR_DF_TRIGGER: | |
1031 | case SR_DF_END: | |
1032 | /* No payload. */ | |
1033 | break; | |
1034 | case SR_DF_HEADER: | |
1035 | /* Payload is a simple struct. */ | |
1036 | g_free((void *)packet->payload); | |
1037 | break; | |
1038 | case SR_DF_META: | |
1039 | meta = packet->payload; | |
1040 | for (l = meta->config; l; l = l->next) { | |
1041 | src = l->data; | |
1042 | g_variant_unref(src->data); | |
1043 | g_free(src); | |
1044 | } | |
1045 | g_slist_free(meta->config); | |
1046 | g_free((void *)packet->payload); | |
1047 | break; | |
1048 | case SR_DF_LOGIC: | |
1049 | logic = packet->payload; | |
1050 | g_free(logic->data); | |
1051 | g_free((void *)packet->payload); | |
1052 | break; | |
1053 | case SR_DF_ANALOG: | |
1054 | analog = packet->payload; | |
1055 | g_slist_free(analog->channels); | |
1056 | g_free(analog->data); | |
1057 | g_free((void *)packet->payload); | |
1058 | break; | |
1059 | default: | |
1060 | sr_err("Unknown packet type %d", packet->type); | |
1061 | } | |
1062 | g_free(packet); | |
1063 | ||
1064 | } | |
1065 | ||
7b870c38 | 1066 | /** @} */ |