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