]>
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> |
c2bf5506 | 5 | * Copyright (C) 2015 Daniel Elstner <daniel.kitta@gmail.com> |
a1bb33af UH |
6 | * |
7 | * This program is free software: you can redistribute it and/or modify | |
8 | * it under the terms of the GNU General Public License as published by | |
9 | * the Free Software Foundation, either version 3 of the License, or | |
10 | * (at your option) any later version. | |
11 | * | |
12 | * This program is distributed in the hope that it will be useful, | |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | * GNU General Public License for more details. | |
16 | * | |
17 | * You should have received a copy of the GNU General Public License | |
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
20 | ||
6ec6c43b | 21 | #include <config.h> |
32af282c | 22 | #include <errno.h> |
a1bb33af UH |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
25 | #include <unistd.h> | |
26 | #include <string.h> | |
544a4582 | 27 | #include <glib.h> |
c1aae900 | 28 | #include <libsigrok/libsigrok.h> |
45c59c8b | 29 | #include "libsigrok-internal.h" |
aa4b1107 | 30 | |
2ad1deb8 | 31 | /** @cond PRIVATE */ |
3544f848 | 32 | #define LOG_PREFIX "session" |
2ad1deb8 | 33 | /** @endcond */ |
a421dc1d | 34 | |
393fb9cb UH |
35 | /** |
36 | * @file | |
37 | * | |
38 | * Creating, using, or destroying libsigrok sessions. | |
39 | */ | |
40 | ||
7b870c38 UH |
41 | /** |
42 | * @defgroup grp_session Session handling | |
43 | * | |
44 | * Creating, using, or destroying libsigrok sessions. | |
45 | * | |
46 | * @{ | |
47 | */ | |
48 | ||
c2bf5506 DE |
49 | struct datafeed_callback { |
50 | sr_datafeed_callback cb; | |
1f9813eb | 51 | void *cb_data; |
c2bf5506 | 52 | }; |
aac0ea25 | 53 | |
c2bf5506 | 54 | /** Custom GLib event source for generic descriptor I/O. |
5de0fc55 | 55 | * @see https://developer.gnome.org/glib/stable/glib-The-Main-Event-Loop.html |
c2bf5506 DE |
56 | * @internal |
57 | */ | |
58 | struct fd_source { | |
59 | GSource base; | |
62d7945f | 60 | |
c2bf5506 DE |
61 | int64_t timeout_us; |
62 | int64_t due_us; | |
92248e78 | 63 | |
c2bf5506 DE |
64 | /* Meta-data needed to keep track of installed sources */ |
65 | struct sr_session *session; | |
66 | void *key; | |
544a4582 | 67 | |
c2bf5506 | 68 | GPollFD pollfd; |
2726474a ML |
69 | }; |
70 | ||
c2bf5506 | 71 | /** FD event source prepare() method. |
5de0fc55 | 72 | * This is called immediately before poll(). |
c2bf5506 DE |
73 | */ |
74 | static gboolean fd_source_prepare(GSource *source, int *timeout) | |
75 | { | |
76 | int64_t now_us; | |
77 | struct fd_source *fsource; | |
78 | int remaining_ms; | |
79 | ||
80 | fsource = (struct fd_source *)source; | |
81 | ||
82 | if (fsource->timeout_us >= 0) { | |
83 | now_us = g_source_get_time(source); | |
84 | ||
85 | if (fsource->due_us == 0) { | |
86 | /* First-time initialization of the expiration time */ | |
87 | fsource->due_us = now_us + fsource->timeout_us; | |
88 | } | |
89 | remaining_ms = (MAX(0, fsource->due_us - now_us) + 999) / 1000; | |
90 | } else { | |
91 | remaining_ms = -1; | |
92 | } | |
93 | *timeout = remaining_ms; | |
94 | ||
95 | return (remaining_ms == 0); | |
96 | } | |
97 | ||
98 | /** FD event source check() method. | |
5de0fc55 | 99 | * This is called after poll() returns to check whether an event fired. |
c2bf5506 DE |
100 | */ |
101 | static gboolean fd_source_check(GSource *source) | |
102 | { | |
103 | struct fd_source *fsource; | |
104 | unsigned int revents; | |
105 | ||
106 | fsource = (struct fd_source *)source; | |
107 | revents = fsource->pollfd.revents; | |
108 | ||
109 | return (revents != 0 || (fsource->timeout_us >= 0 | |
110 | && fsource->due_us <= g_source_get_time(source))); | |
111 | } | |
112 | ||
113 | /** FD event source dispatch() method. | |
5de0fc55 | 114 | * This is called if either prepare() or check() returned TRUE. |
c2bf5506 DE |
115 | */ |
116 | static gboolean fd_source_dispatch(GSource *source, | |
117 | GSourceFunc callback, void *user_data) | |
118 | { | |
119 | struct fd_source *fsource; | |
120 | const char *name; | |
121 | unsigned int revents; | |
122 | gboolean keep; | |
123 | ||
124 | fsource = (struct fd_source *)source; | |
125 | name = g_source_get_name(source); | |
126 | revents = fsource->pollfd.revents; | |
127 | ||
128 | if (revents != 0) { | |
129 | sr_spew("%s: %s " G_POLLFD_FORMAT ", revents 0x%.2X", | |
130 | __func__, name, fsource->pollfd.fd, revents); | |
131 | } else { | |
132 | sr_spew("%s: %s " G_POLLFD_FORMAT ", timed out", | |
133 | __func__, name, fsource->pollfd.fd); | |
134 | } | |
135 | if (!callback) { | |
136 | sr_err("Callback not set, cannot dispatch event."); | |
137 | return G_SOURCE_REMOVE; | |
138 | } | |
139 | keep = (*(sr_receive_data_callback)callback) | |
140 | (fsource->pollfd.fd, revents, user_data); | |
141 | ||
142 | if (fsource->timeout_us >= 0 && G_LIKELY(keep) | |
143 | && G_LIKELY(!g_source_is_destroyed(source))) | |
144 | fsource->due_us = g_source_get_time(source) | |
145 | + fsource->timeout_us; | |
146 | return keep; | |
147 | } | |
148 | ||
149 | /** FD event source finalize() method. | |
150 | */ | |
151 | static void fd_source_finalize(GSource *source) | |
152 | { | |
153 | struct fd_source *fsource; | |
154 | ||
155 | fsource = (struct fd_source *)source; | |
156 | ||
157 | sr_dbg("%s: key %p", __func__, fsource->key); | |
158 | ||
159 | sr_session_source_destroyed(fsource->session, fsource->key, source); | |
160 | } | |
161 | ||
162 | /** Create an event source for I/O on a file descriptor. | |
163 | * | |
164 | * In order to maintain API compatibility, this event source also doubles | |
165 | * as a timer event source. | |
166 | * | |
167 | * @param session The session the event source belongs to. | |
168 | * @param key The key used to identify this source. | |
169 | * @param fd The file descriptor or HANDLE. | |
170 | * @param timeout_ms The timeout interval in ms, or -1 to wait indefinitely. | |
171 | * @return A new event source object, or NULL on failure. | |
172 | */ | |
173 | static GSource *fd_source_new(struct sr_session *session, void *key, | |
174 | gintptr fd, int events, int timeout_ms) | |
175 | { | |
176 | static GSourceFuncs fd_source_funcs = { | |
177 | .prepare = &fd_source_prepare, | |
178 | .check = &fd_source_check, | |
179 | .dispatch = &fd_source_dispatch, | |
180 | .finalize = &fd_source_finalize | |
181 | }; | |
182 | GSource *source; | |
183 | struct fd_source *fsource; | |
184 | ||
185 | source = g_source_new(&fd_source_funcs, sizeof(struct fd_source)); | |
186 | fsource = (struct fd_source *)source; | |
187 | ||
188 | g_source_set_name(source, (fd < 0) ? "timer" : "fd"); | |
189 | ||
190 | if (timeout_ms >= 0) { | |
191 | fsource->timeout_us = 1000 * (int64_t)timeout_ms; | |
192 | fsource->due_us = 0; | |
193 | } else { | |
194 | fsource->timeout_us = -1; | |
195 | fsource->due_us = INT64_MAX; | |
196 | } | |
197 | fsource->session = session; | |
198 | fsource->key = key; | |
199 | ||
200 | fsource->pollfd.fd = fd; | |
201 | fsource->pollfd.events = events; | |
202 | fsource->pollfd.revents = 0; | |
203 | ||
204 | if (fd >= 0) | |
205 | g_source_add_poll(source, &fsource->pollfd); | |
206 | ||
207 | return source; | |
208 | } | |
209 | ||
9f45fb3a UH |
210 | /** |
211 | * Create a new session. | |
7efe889e | 212 | * |
61e6e2da | 213 | * @param ctx The context in which to create the new session. |
7efe889e UH |
214 | * @param new_session This will contain a pointer to the newly created |
215 | * session if the return value is SR_OK, otherwise the value | |
216 | * is undefined and should not be used. Must not be NULL. | |
9f45fb3a | 217 | * |
0812c40e | 218 | * @retval SR_OK Success. |
41de54ff | 219 | * @retval SR_ERR_ARG Invalid argument. |
47117241 | 220 | * |
0812c40e | 221 | * @since 0.4.0 |
9f45fb3a | 222 | */ |
61e6e2da ML |
223 | SR_API int sr_session_new(struct sr_context *ctx, |
224 | struct sr_session **new_session) | |
a1bb33af | 225 | { |
3337e9a1 | 226 | struct sr_session *session; |
a1bb33af | 227 | |
41de54ff UH |
228 | if (!new_session) |
229 | return SR_ERR_ARG; | |
230 | ||
3337e9a1 | 231 | session = g_malloc0(sizeof(struct sr_session)); |
b7e94111 | 232 | |
4ed5d21d | 233 | session->ctx = ctx; |
faa5d7d9 | 234 | |
c2bf5506 | 235 | g_mutex_init(&session->main_mutex); |
faa5d7d9 | 236 | |
c2bf5506 DE |
237 | /* To maintain API compatibility, we need a lookup table |
238 | * which maps poll_object IDs to GSource* pointers. | |
239 | */ | |
240 | session->event_sources = g_hash_table_new(NULL, NULL); | |
0812c40e | 241 | |
3337e9a1 | 242 | *new_session = session; |
0812c40e ML |
243 | |
244 | return SR_OK; | |
a1bb33af UH |
245 | } |
246 | ||
9f45fb3a | 247 | /** |
0812c40e | 248 | * Destroy a session. |
9f45fb3a UH |
249 | * This frees up all memory used by the session. |
250 | * | |
7efe889e UH |
251 | * @param session The session to destroy. Must not be NULL. |
252 | * | |
04cb9157 | 253 | * @retval SR_OK Success. |
0812c40e | 254 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 255 | * |
0812c40e | 256 | * @since 0.4.0 |
9f45fb3a | 257 | */ |
0812c40e | 258 | SR_API int sr_session_destroy(struct sr_session *session) |
a1bb33af | 259 | { |
9f45fb3a | 260 | if (!session) { |
a421dc1d | 261 | sr_err("%s: session was NULL", __func__); |
0812c40e | 262 | return SR_ERR_ARG; |
9f45fb3a UH |
263 | } |
264 | ||
0812c40e | 265 | sr_session_dev_remove_all(session); |
1de3cced ML |
266 | g_slist_free_full(session->owned_devs, (GDestroyNotify)sr_dev_inst_free); |
267 | ||
c2bf5506 DE |
268 | g_hash_table_unref(session->event_sources); |
269 | ||
270 | g_mutex_clear(&session->main_mutex); | |
faa5d7d9 | 271 | |
a1bb33af | 272 | g_free(session); |
0812c40e | 273 | |
e0508e67 | 274 | return SR_OK; |
a1bb33af UH |
275 | } |
276 | ||
9f45fb3a | 277 | /** |
0812c40e | 278 | * Remove all the devices from a session. |
9f45fb3a UH |
279 | * |
280 | * The session itself (i.e., the struct sr_session) is not free'd and still | |
281 | * exists after this function returns. | |
282 | * | |
7efe889e UH |
283 | * @param session The session to use. Must not be NULL. |
284 | * | |
04cb9157 | 285 | * @retval SR_OK Success. |
0812c40e | 286 | * @retval SR_ERR_BUG Invalid session passed. |
47117241 | 287 | * |
0812c40e | 288 | * @since 0.4.0 |
9f45fb3a | 289 | */ |
0812c40e | 290 | SR_API int sr_session_dev_remove_all(struct sr_session *session) |
a1bb33af | 291 | { |
0812c40e ML |
292 | struct sr_dev_inst *sdi; |
293 | GSList *l; | |
294 | ||
9f45fb3a | 295 | if (!session) { |
a421dc1d | 296 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
297 | return SR_ERR_ARG; |
298 | } | |
299 | ||
300 | for (l = session->devs; l; l = l->next) { | |
301 | sdi = (struct sr_dev_inst *) l->data; | |
302 | sdi->session = NULL; | |
9f45fb3a UH |
303 | } |
304 | ||
681803df | 305 | g_slist_free(session->devs); |
bb7ef793 | 306 | session->devs = NULL; |
e0508e67 UH |
307 | |
308 | return SR_OK; | |
a1bb33af UH |
309 | } |
310 | ||
9f45fb3a | 311 | /** |
0812c40e | 312 | * Add a device instance to a session. |
9f45fb3a | 313 | * |
7efe889e | 314 | * @param session The session to add to. Must not be NULL. |
0812c40e | 315 | * @param sdi The device instance to add to a session. Must not |
de4d3f99 BV |
316 | * be NULL. Also, sdi->driver and sdi->driver->dev_open must |
317 | * not be NULL. | |
9f45fb3a | 318 | * |
04cb9157 MH |
319 | * @retval SR_OK Success. |
320 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 | 321 | * |
0812c40e | 322 | * @since 0.4.0 |
9f45fb3a | 323 | */ |
0812c40e ML |
324 | SR_API int sr_session_dev_add(struct sr_session *session, |
325 | struct sr_dev_inst *sdi) | |
a1bb33af | 326 | { |
5451816f | 327 | int ret; |
a1bb33af | 328 | |
de4d3f99 | 329 | if (!sdi) { |
a421dc1d | 330 | sr_err("%s: sdi was NULL", __func__); |
9f45fb3a UH |
331 | return SR_ERR_ARG; |
332 | } | |
333 | ||
d6eb0c33 | 334 | if (!session) { |
a421dc1d | 335 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
336 | return SR_ERR_ARG; |
337 | } | |
338 | ||
339 | /* If sdi->session is not NULL, the device is already in this or | |
340 | * another session. */ | |
341 | if (sdi->session) { | |
342 | sr_err("%s: already assigned to session", __func__); | |
343 | return SR_ERR_ARG; | |
d6eb0c33 UH |
344 | } |
345 | ||
de4d3f99 BV |
346 | /* If sdi->driver is NULL, this is a virtual device. */ |
347 | if (!sdi->driver) { | |
d6eb0c33 | 348 | /* Just add the device, don't run dev_open(). */ |
5de0fc55 | 349 | session->devs = g_slist_append(session->devs, sdi); |
0812c40e | 350 | sdi->session = session; |
d6eb0c33 | 351 | return SR_OK; |
9f45fb3a UH |
352 | } |
353 | ||
de4d3f99 BV |
354 | /* sdi->driver is non-NULL (i.e. we have a real device). */ |
355 | if (!sdi->driver->dev_open) { | |
a421dc1d | 356 | sr_err("%s: sdi->driver->dev_open was NULL", __func__); |
8ec95d22 | 357 | return SR_ERR_BUG; |
9f45fb3a UH |
358 | } |
359 | ||
5de0fc55 | 360 | session->devs = g_slist_append(session->devs, sdi); |
0812c40e | 361 | sdi->session = session; |
aa4b1107 | 362 | |
5de0fc55 DE |
363 | /* TODO: This is invalid if the session runs in a different thread. |
364 | * The usage semantics and restrictions need to be documented. | |
365 | */ | |
5451816f | 366 | if (session->running) { |
32b7cd4f DE |
367 | /* Adding a device to a running session. Commit settings |
368 | * and start acquisition on that device now. */ | |
369 | if ((ret = sr_config_commit(sdi)) != SR_OK) { | |
370 | sr_err("Failed to commit device settings before " | |
371 | "starting acquisition in running session (%s)", | |
372 | sr_strerror(ret)); | |
373 | return ret; | |
374 | } | |
5451816f | 375 | if ((ret = sdi->driver->dev_acquisition_start(sdi, |
5de0fc55 | 376 | sdi)) != SR_OK) { |
5451816f | 377 | sr_err("Failed to start acquisition of device in " |
32b7cd4f DE |
378 | "running session (%s)", sr_strerror(ret)); |
379 | return ret; | |
380 | } | |
5451816f BV |
381 | } |
382 | ||
e46b8fb1 | 383 | return SR_OK; |
a1bb33af UH |
384 | } |
385 | ||
2bb311b4 | 386 | /** |
0812c40e | 387 | * List all device instances attached to a session. |
2bb311b4 | 388 | * |
7efe889e | 389 | * @param session The session to use. Must not be NULL. |
2bb311b4 BV |
390 | * @param devlist A pointer where the device instance list will be |
391 | * stored on return. If no devices are in the session, | |
392 | * this will be NULL. Each element in the list points | |
393 | * to a struct sr_dev_inst *. | |
394 | * The list must be freed by the caller, but not the | |
395 | * elements pointed to. | |
396 | * | |
04cb9157 | 397 | * @retval SR_OK Success. |
0812c40e | 398 | * @retval SR_ERR_ARG Invalid argument. |
47117241 | 399 | * |
0812c40e | 400 | * @since 0.4.0 |
2bb311b4 | 401 | */ |
0812c40e | 402 | SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist) |
2bb311b4 | 403 | { |
2bb311b4 | 404 | if (!session) |
0812c40e ML |
405 | return SR_ERR_ARG; |
406 | ||
407 | if (!devlist) | |
408 | return SR_ERR_ARG; | |
2bb311b4 BV |
409 | |
410 | *devlist = g_slist_copy(session->devs); | |
411 | ||
412 | return SR_OK; | |
413 | } | |
414 | ||
9f45fb3a | 415 | /** |
0812c40e | 416 | * Remove all datafeed callbacks in a session. |
9f45fb3a | 417 | * |
7efe889e UH |
418 | * @param session The session to use. Must not be NULL. |
419 | * | |
04cb9157 | 420 | * @retval SR_OK Success. |
0812c40e | 421 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 422 | * |
0812c40e | 423 | * @since 0.4.0 |
9f45fb3a | 424 | */ |
0812c40e | 425 | SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session) |
a1bb33af | 426 | { |
9f45fb3a | 427 | if (!session) { |
a421dc1d | 428 | sr_err("%s: session was NULL", __func__); |
0812c40e | 429 | return SR_ERR_ARG; |
9f45fb3a UH |
430 | } |
431 | ||
2726474a | 432 | g_slist_free_full(session->datafeed_callbacks, g_free); |
a1bb33af | 433 | session->datafeed_callbacks = NULL; |
e0508e67 UH |
434 | |
435 | return SR_OK; | |
a1bb33af UH |
436 | } |
437 | ||
9f45fb3a | 438 | /** |
0812c40e | 439 | * Add a datafeed callback to a session. |
9f45fb3a | 440 | * |
7efe889e | 441 | * @param session The session to use. Must not be NULL. |
d08490aa | 442 | * @param cb Function to call when a chunk of data is received. |
0abee507 | 443 | * Must not be NULL. |
85222791 | 444 | * @param cb_data Opaque pointer passed in by the caller. |
a1645fcd | 445 | * |
04cb9157 MH |
446 | * @retval SR_OK Success. |
447 | * @retval SR_ERR_BUG No session exists. | |
47117241 UH |
448 | * |
449 | * @since 0.3.0 | |
9f45fb3a | 450 | */ |
0812c40e ML |
451 | SR_API int sr_session_datafeed_callback_add(struct sr_session *session, |
452 | sr_datafeed_callback cb, void *cb_data) | |
a1bb33af | 453 | { |
2726474a ML |
454 | struct datafeed_callback *cb_struct; |
455 | ||
9f45fb3a | 456 | if (!session) { |
a421dc1d | 457 | sr_err("%s: session was NULL", __func__); |
e0508e67 | 458 | return SR_ERR_BUG; |
9f45fb3a UH |
459 | } |
460 | ||
0abee507 | 461 | if (!cb) { |
a421dc1d | 462 | sr_err("%s: cb was NULL", __func__); |
0abee507 UH |
463 | return SR_ERR_ARG; |
464 | } | |
9f45fb3a | 465 | |
91219afc | 466 | cb_struct = g_malloc0(sizeof(struct datafeed_callback)); |
2726474a ML |
467 | cb_struct->cb = cb; |
468 | cb_struct->cb_data = cb_data; | |
469 | ||
62c82025 | 470 | session->datafeed_callbacks = |
2726474a | 471 | g_slist_append(session->datafeed_callbacks, cb_struct); |
e0508e67 UH |
472 | |
473 | return SR_OK; | |
a1bb33af UH |
474 | } |
475 | ||
9f42e2e6 UH |
476 | /** |
477 | * Get the trigger assigned to this session. | |
478 | * | |
479 | * @param session The session to use. | |
480 | * | |
481 | * @retval NULL Invalid (NULL) session was passed to the function. | |
482 | * @retval other The trigger assigned to this session (can be NULL). | |
483 | * | |
484 | * @since 0.4.0 | |
485 | */ | |
0812c40e | 486 | SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session) |
7b5e6d29 | 487 | { |
9f42e2e6 UH |
488 | if (!session) |
489 | return NULL; | |
490 | ||
7b5e6d29 BV |
491 | return session->trigger; |
492 | } | |
493 | ||
9f42e2e6 UH |
494 | /** |
495 | * Set the trigger of this session. | |
496 | * | |
497 | * @param session The session to use. Must not be NULL. | |
498 | * @param trig The trigger to assign to this session. Can be NULL. | |
499 | * | |
500 | * @retval SR_OK Success. | |
501 | * @retval SR_ERR_ARG Invalid argument. | |
502 | * | |
503 | * @since 0.4.0 | |
504 | */ | |
0812c40e | 505 | SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig) |
7b5e6d29 | 506 | { |
9f42e2e6 UH |
507 | if (!session) |
508 | return SR_ERR_ARG; | |
509 | ||
7b5e6d29 BV |
510 | session->trigger = trig; |
511 | ||
512 | return SR_OK; | |
513 | } | |
514 | ||
7b5e6d29 BV |
515 | static int verify_trigger(struct sr_trigger *trigger) |
516 | { | |
517 | struct sr_trigger_stage *stage; | |
518 | struct sr_trigger_match *match; | |
519 | GSList *l, *m; | |
520 | ||
521 | if (!trigger->stages) { | |
522 | sr_err("No trigger stages defined."); | |
523 | return SR_ERR; | |
524 | } | |
525 | ||
526 | sr_spew("Checking trigger:"); | |
527 | for (l = trigger->stages; l; l = l->next) { | |
528 | stage = l->data; | |
529 | if (!stage->matches) { | |
530 | sr_err("Stage %d has no matches defined.", stage->stage); | |
531 | return SR_ERR; | |
532 | } | |
533 | for (m = stage->matches; m; m = m->next) { | |
534 | match = m->data; | |
535 | if (!match->channel) { | |
536 | sr_err("Stage %d match has no channel.", stage->stage); | |
537 | return SR_ERR; | |
538 | } | |
539 | if (!match->match) { | |
540 | sr_err("Stage %d match is not defined.", stage->stage); | |
541 | return SR_ERR; | |
542 | } | |
543 | sr_spew("Stage %d match on channel %s, match %d", stage->stage, | |
544 | match->channel->name, match->match); | |
545 | } | |
546 | } | |
547 | ||
548 | return SR_OK; | |
549 | } | |
1beccaed | 550 | |
c2bf5506 DE |
551 | /** Set up the main context the session will be executing in. |
552 | * | |
553 | * Must be called just before the session starts, by the thread which | |
554 | * will execute the session main loop. Once acquired, the main context | |
555 | * pointer is immutable for the duration of the session run. | |
556 | */ | |
557 | static int set_main_context(struct sr_session *session) | |
558 | { | |
559 | GMainContext *def_context; | |
560 | ||
5de0fc55 DE |
561 | /* May happen if sr_session_start() is called a second time |
562 | * while the session is still running. | |
c2bf5506 | 563 | */ |
5de0fc55 DE |
564 | if (session->main_context) { |
565 | sr_err("Main context already set."); | |
c2bf5506 DE |
566 | return SR_ERR; |
567 | } | |
568 | ||
569 | g_mutex_lock(&session->main_mutex); | |
570 | ||
571 | def_context = g_main_context_get_thread_default(); | |
572 | ||
573 | if (!def_context) | |
574 | def_context = g_main_context_default(); | |
575 | /* | |
576 | * Try to use an existing main context if possible, but only if we | |
577 | * can make it owned by the current thread. Otherwise, create our | |
578 | * own main context so that event source callbacks can execute in | |
579 | * the session thread. | |
580 | */ | |
581 | if (g_main_context_acquire(def_context)) { | |
582 | g_main_context_release(def_context); | |
583 | ||
584 | sr_dbg("Using thread-default main context."); | |
585 | ||
586 | session->main_context = def_context; | |
587 | session->main_context_is_default = TRUE; | |
588 | } else { | |
589 | sr_dbg("Creating our own main context."); | |
590 | ||
591 | session->main_context = g_main_context_new(); | |
592 | session->main_context_is_default = FALSE; | |
593 | } | |
594 | g_mutex_unlock(&session->main_mutex); | |
595 | ||
596 | return SR_OK; | |
597 | } | |
598 | ||
599 | /** Unset the main context used for the current session run. | |
600 | * | |
601 | * Must be called right after stopping the session. Note that if the | |
602 | * session is stopped asynchronously, the main loop may still be running | |
603 | * after the main context has been unset. This is OK as long as no new | |
604 | * event sources are created -- the main loop holds its own reference | |
605 | * to the main context. | |
606 | */ | |
607 | static int unset_main_context(struct sr_session *session) | |
608 | { | |
609 | int ret; | |
610 | ||
611 | g_mutex_lock(&session->main_mutex); | |
612 | ||
613 | if (session->main_context) { | |
614 | if (!session->main_context_is_default) | |
615 | g_main_context_unref(session->main_context); | |
616 | ||
617 | session->main_context = NULL; | |
618 | ret = SR_OK; | |
619 | } else { | |
620 | /* May happen if the set/unset calls are not matched. | |
621 | */ | |
622 | sr_err("No main context to unset."); | |
623 | ret = SR_ERR; | |
624 | } | |
625 | g_mutex_unlock(&session->main_mutex); | |
626 | ||
627 | return ret; | |
628 | } | |
629 | ||
5de0fc55 DE |
630 | static unsigned int session_source_attach(struct sr_session *session, |
631 | GSource *source) | |
632 | { | |
633 | unsigned int id = 0; | |
634 | ||
635 | g_mutex_lock(&session->main_mutex); | |
636 | ||
637 | if (session->main_context) | |
638 | id = g_source_attach(source, session->main_context); | |
639 | else | |
640 | sr_err("Cannot add event source without main context."); | |
641 | ||
642 | g_mutex_unlock(&session->main_mutex); | |
643 | ||
644 | return id; | |
645 | } | |
646 | ||
647 | /* Idle handler; invoked when the number of registered event sources | |
648 | * for a running session drops to zero. | |
649 | */ | |
650 | static gboolean delayed_stop_check(void *data) | |
651 | { | |
652 | struct sr_session *session; | |
653 | ||
654 | session = data; | |
655 | session->stop_check_id = 0; | |
656 | ||
657 | /* Session already ended? */ | |
658 | if (!session->running) | |
659 | return G_SOURCE_REMOVE; | |
660 | ||
661 | /* New event sources may have been installed in the meantime. */ | |
662 | if (g_hash_table_size(session->event_sources) != 0) | |
663 | return G_SOURCE_REMOVE; | |
664 | ||
665 | session->running = FALSE; | |
666 | unset_main_context(session); | |
667 | ||
668 | sr_info("Stopped."); | |
669 | ||
670 | /* This indicates a bug in user code, since it is not valid to | |
671 | * restart or destroy a session while it may still be running. | |
672 | */ | |
673 | if (!session->main_loop && !session->stopped_callback) { | |
674 | sr_err("BUG: Session stop left unhandled."); | |
675 | return G_SOURCE_REMOVE; | |
676 | } | |
677 | if (session->main_loop) | |
678 | g_main_loop_quit(session->main_loop); | |
679 | ||
680 | if (session->stopped_callback) | |
681 | (*session->stopped_callback)(session->stopped_cb_data); | |
682 | ||
683 | return G_SOURCE_REMOVE; | |
684 | } | |
685 | ||
686 | static int stop_check_later(struct sr_session *session) | |
687 | { | |
688 | GSource *source; | |
689 | unsigned int source_id; | |
690 | ||
691 | if (session->stop_check_id != 0) | |
692 | return SR_OK; /* idle handler already installed */ | |
693 | ||
694 | source = g_idle_source_new(); | |
695 | g_source_set_callback(source, &delayed_stop_check, session, NULL); | |
696 | ||
697 | source_id = session_source_attach(session, source); | |
698 | session->stop_check_id = source_id; | |
699 | ||
700 | g_source_unref(source); | |
701 | ||
702 | return (source_id != 0) ? SR_OK : SR_ERR; | |
703 | } | |
704 | ||
9f45fb3a UH |
705 | /** |
706 | * Start a session. | |
707 | * | |
5de0fc55 DE |
708 | * When this function returns with a status code indicating success, the |
709 | * session is running. Use sr_session_stopped_callback_set() to receive | |
710 | * notification upon completion, or call sr_session_run() to block until | |
711 | * the session stops. | |
712 | * | |
713 | * Session events will be processed in the context of the current thread. | |
714 | * If a thread-default GLib main context has been set, and is not owned by | |
715 | * any other thread, it will be used. Otherwise, libsigrok will create its | |
716 | * own main context for the current thread. | |
717 | * | |
7efe889e UH |
718 | * @param session The session to use. Must not be NULL. |
719 | * | |
04cb9157 | 720 | * @retval SR_OK Success. |
0812c40e | 721 | * @retval SR_ERR_ARG Invalid session passed. |
5de0fc55 | 722 | * @retval SR_ERR Other error. |
47117241 | 723 | * |
0812c40e | 724 | * @since 0.4.0 |
9f45fb3a | 725 | */ |
0812c40e | 726 | SR_API int sr_session_start(struct sr_session *session) |
7d658874 | 727 | { |
de4d3f99 | 728 | struct sr_dev_inst *sdi; |
013ec84b | 729 | struct sr_channel *ch; |
5de0fc55 DE |
730 | GSList *l, *c, *lend; |
731 | int ret; | |
7d658874 | 732 | |
9f45fb3a | 733 | if (!session) { |
0812c40e ML |
734 | sr_err("%s: session was NULL", __func__); |
735 | return SR_ERR_ARG; | |
9f45fb3a UH |
736 | } |
737 | ||
bb7ef793 | 738 | if (!session->devs) { |
a421dc1d | 739 | sr_err("%s: session->devs was NULL; a session " |
9f45fb3a | 740 | "cannot be started without devices.", __func__); |
0812c40e | 741 | return SR_ERR_ARG; |
9f45fb3a UH |
742 | } |
743 | ||
5de0fc55 DE |
744 | if (session->running) { |
745 | sr_err("Cannot (re-)start session while it is still running."); | |
7b5e6d29 | 746 | return SR_ERR; |
5de0fc55 | 747 | } |
7b5e6d29 | 748 | |
5de0fc55 DE |
749 | if (session->trigger) { |
750 | ret = verify_trigger(session->trigger); | |
751 | if (ret != SR_OK) | |
752 | return ret; | |
753 | } | |
9f45fb3a | 754 | |
5de0fc55 | 755 | /* Check enabled channels and commit settings of all devices. */ |
bb7ef793 | 756 | for (l = session->devs; l; l = l->next) { |
de4d3f99 | 757 | sdi = l->data; |
013ec84b BV |
758 | for (c = sdi->channels; c; c = c->next) { |
759 | ch = c->data; | |
5de0fc55 | 760 | if (ch->enabled) |
013ec84b | 761 | break; |
013ec84b | 762 | } |
5de0fc55 DE |
763 | if (!c) { |
764 | sr_err("%s device %s has no enabled channels.", | |
765 | sdi->driver->name, sdi->connection_id); | |
766 | return SR_ERR; | |
013ec84b BV |
767 | } |
768 | ||
5de0fc55 DE |
769 | ret = sr_config_commit(sdi); |
770 | if (ret != SR_OK) { | |
771 | sr_err("Failed to commit %s device %s settings " | |
772 | "before starting acquisition.", | |
773 | sdi->driver->name, sdi->connection_id); | |
774 | return ret; | |
32b7cd4f | 775 | } |
5de0fc55 DE |
776 | } |
777 | ||
778 | ret = set_main_context(session); | |
779 | if (ret != SR_OK) | |
780 | return ret; | |
781 | ||
782 | sr_info("Starting."); | |
783 | ||
784 | session->running = TRUE; | |
785 | ||
786 | /* Have all devices start acquisition. */ | |
787 | for (l = session->devs; l; l = l->next) { | |
788 | sdi = l->data; | |
789 | ret = sdi->driver->dev_acquisition_start(sdi, sdi); | |
790 | if (ret != SR_OK) { | |
791 | sr_err("Could not start %s device %s acquisition.", | |
792 | sdi->driver->name, sdi->connection_id); | |
7d658874 | 793 | break; |
9f45fb3a | 794 | } |
7d658874 BV |
795 | } |
796 | ||
c2bf5506 | 797 | if (ret != SR_OK) { |
5de0fc55 DE |
798 | /* If there are multiple devices, some of them may already have |
799 | * started successfully. Stop them now before returning. */ | |
800 | lend = l->next; | |
801 | for (l = session->devs; l != lend; l = l->next) { | |
802 | sdi = l->data; | |
803 | if (sdi->driver->dev_acquisition_stop) | |
804 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
805 | } | |
806 | /* TODO: Handle delayed stops. Need to iterate the event | |
807 | * sources... */ | |
c2bf5506 | 808 | session->running = FALSE; |
5de0fc55 DE |
809 | |
810 | unset_main_context(session); | |
811 | return ret; | |
c2bf5506 | 812 | } |
9f45fb3a | 813 | |
5de0fc55 DE |
814 | if (g_hash_table_size(session->event_sources) == 0) |
815 | stop_check_later(session); | |
816 | ||
817 | return SR_OK; | |
7d658874 BV |
818 | } |
819 | ||
9f45fb3a | 820 | /** |
5de0fc55 DE |
821 | * Block until the running session stops. |
822 | * | |
823 | * This is a convenience function which creates a GLib main loop and runs | |
824 | * it to process session events until the session stops. | |
825 | * | |
826 | * Instead of using this function, applications may run their own GLib main | |
827 | * loop, and use sr_session_stopped_callback_set() to receive notification | |
828 | * when the session finished running. | |
9f45fb3a | 829 | * |
7efe889e UH |
830 | * @param session The session to use. Must not be NULL. |
831 | * | |
04cb9157 | 832 | * @retval SR_OK Success. |
0812c40e | 833 | * @retval SR_ERR_ARG Invalid session passed. |
5de0fc55 | 834 | * @retval SR_ERR Other error. |
47117241 | 835 | * |
0812c40e | 836 | * @since 0.4.0 |
9f45fb3a | 837 | */ |
0812c40e | 838 | SR_API int sr_session_run(struct sr_session *session) |
7d658874 | 839 | { |
9f45fb3a | 840 | if (!session) { |
0812c40e ML |
841 | sr_err("%s: session was NULL", __func__); |
842 | return SR_ERR_ARG; | |
9f45fb3a | 843 | } |
5de0fc55 DE |
844 | if (!session->running) { |
845 | sr_err("No session running."); | |
846 | return SR_ERR; | |
9f45fb3a | 847 | } |
c2bf5506 DE |
848 | if (session->main_loop) { |
849 | sr_err("Main loop already created."); | |
850 | return SR_ERR; | |
851 | } | |
9f45fb3a | 852 | |
c2bf5506 | 853 | g_mutex_lock(&session->main_mutex); |
5de0fc55 | 854 | |
c2bf5506 DE |
855 | if (!session->main_context) { |
856 | sr_err("Cannot run without main context."); | |
857 | g_mutex_unlock(&session->main_mutex); | |
858 | return SR_ERR; | |
859 | } | |
c2bf5506 | 860 | session->main_loop = g_main_loop_new(session->main_context, FALSE); |
5de0fc55 | 861 | |
c2bf5506 DE |
862 | g_mutex_unlock(&session->main_mutex); |
863 | ||
864 | g_main_loop_run(session->main_loop); | |
865 | ||
866 | g_main_loop_unref(session->main_loop); | |
867 | session->main_loop = NULL; | |
868 | ||
e0508e67 | 869 | return SR_OK; |
7d658874 BV |
870 | } |
871 | ||
c2bf5506 | 872 | static gboolean session_stop_sync(void *user_data) |
a1bb33af | 873 | { |
c2bf5506 | 874 | struct sr_session *session; |
de4d3f99 | 875 | struct sr_dev_inst *sdi; |
c2bf5506 | 876 | GSList *node; |
a1bb33af | 877 | |
c2bf5506 DE |
878 | session = user_data; |
879 | ||
880 | if (!session->running) | |
881 | return G_SOURCE_REMOVE; | |
9f45fb3a | 882 | |
a421dc1d | 883 | sr_info("Stopping."); |
e0508e67 | 884 | |
c2bf5506 DE |
885 | for (node = session->devs; node; node = node->next) { |
886 | sdi = node->data; | |
887 | if (sdi->driver && sdi->driver->dev_acquisition_stop) | |
888 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
a1bb33af | 889 | } |
9f45fb3a | 890 | |
c2bf5506 | 891 | return G_SOURCE_REMOVE; |
a1bb33af UH |
892 | } |
893 | ||
33c6e4c5 | 894 | /** |
0812c40e | 895 | * Stop a session. |
33c6e4c5 | 896 | * |
5de0fc55 DE |
897 | * This requests the drivers of each device participating in the session to |
898 | * abort the acquisition as soon as possible. Even after this function returns, | |
899 | * event processing still continues until all devices have actually stopped. | |
900 | * | |
901 | * Use sr_session_stopped_callback_set() to receive notification when the event | |
902 | * processing finished. | |
33c6e4c5 | 903 | * |
5de0fc55 DE |
904 | * This function is reentrant. That is, it may be called from a different |
905 | * thread than the one executing the session, as long as it can be ensured | |
906 | * that the session object is valid. | |
907 | * | |
908 | * If the session is not running, sr_session_stop() silently does nothing. | |
33c6e4c5 | 909 | * |
7efe889e UH |
910 | * @param session The session to use. Must not be NULL. |
911 | * | |
04cb9157 | 912 | * @retval SR_OK Success. |
0812c40e | 913 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 914 | * |
0812c40e | 915 | * @since 0.4.0 |
33c6e4c5 | 916 | */ |
0812c40e | 917 | SR_API int sr_session_stop(struct sr_session *session) |
33c6e4c5 | 918 | { |
5de0fc55 DE |
919 | GMainContext *main_context; |
920 | ||
33c6e4c5 AG |
921 | if (!session) { |
922 | sr_err("%s: session was NULL", __func__); | |
c2bf5506 | 923 | return SR_ERR_ARG; |
33c6e4c5 | 924 | } |
5de0fc55 | 925 | |
c2bf5506 | 926 | g_mutex_lock(&session->main_mutex); |
33c6e4c5 | 927 | |
5de0fc55 DE |
928 | main_context = (session->main_context) |
929 | ? g_main_context_ref(session->main_context) | |
930 | : NULL; | |
931 | ||
c2bf5506 | 932 | g_mutex_unlock(&session->main_mutex); |
33c6e4c5 | 933 | |
5de0fc55 DE |
934 | if (!main_context) { |
935 | sr_dbg("No main context set; already stopped?"); | |
936 | /* Not an error; as it would be racy. */ | |
937 | return SR_OK; | |
938 | } | |
939 | g_main_context_invoke(main_context, &session_stop_sync, session); | |
940 | g_main_context_unref(main_context); | |
941 | ||
942 | return SR_OK; | |
943 | } | |
944 | ||
945 | /** | |
946 | * Return whether the session is currently running. | |
947 | * | |
948 | * Note that this function should be called from the same thread | |
949 | * the session was started in. | |
950 | * | |
951 | * @param session The session to use. Must not be NULL. | |
952 | * | |
953 | * @retval TRUE Session is running. | |
954 | * @retval FALSE Session is not running. | |
955 | * @retval SR_ERR_ARG Invalid session passed. | |
956 | * | |
957 | * @since 0.4.0 | |
958 | */ | |
959 | SR_API int sr_session_is_running(struct sr_session *session) | |
960 | { | |
961 | if (!session) { | |
962 | sr_err("%s: session was NULL", __func__); | |
963 | return SR_ERR_ARG; | |
964 | } | |
965 | return session->running; | |
966 | } | |
967 | ||
968 | /** | |
969 | * Set the callback to be invoked after a session stopped running. | |
970 | * | |
971 | * Install a callback to receive notification when a session run stopped. | |
972 | * This can be used to integrate session execution with an existing main | |
973 | * loop, without having to block in sr_session_run(). | |
974 | * | |
975 | * Note that the callback will be invoked in the context of the thread | |
976 | * that calls sr_session_start(). | |
977 | * | |
978 | * @param session The session to use. Must not be NULL. | |
979 | * @param cb The callback to invoke on session stop. May be NULL to unset. | |
980 | * @param cb_data User data pointer to be passed to the callback. | |
981 | * | |
982 | * @retval SR_OK Success. | |
983 | * @retval SR_ERR_ARG Invalid session passed. | |
984 | * | |
985 | * @since 0.4.0 | |
986 | */ | |
987 | SR_API int sr_session_stopped_callback_set(struct sr_session *session, | |
988 | sr_session_stopped_callback cb, void *cb_data) | |
989 | { | |
990 | if (!session) { | |
991 | sr_err("%s: session was NULL", __func__); | |
992 | return SR_ERR_ARG; | |
993 | } | |
994 | session->stopped_callback = cb; | |
995 | session->stopped_cb_data = cb_data; | |
996 | ||
997 | return SR_OK; | |
33c6e4c5 AG |
998 | } |
999 | ||
9f45fb3a | 1000 | /** |
a1645fcd | 1001 | * Debug helper. |
9f45fb3a | 1002 | * |
996b0c72 | 1003 | * @param packet The packet to show debugging information for. |
9f45fb3a | 1004 | */ |
bf53457d | 1005 | static void datafeed_dump(const struct sr_datafeed_packet *packet) |
7d2afd6c | 1006 | { |
bf53457d JH |
1007 | const struct sr_datafeed_logic *logic; |
1008 | const struct sr_datafeed_analog *analog; | |
1954dfa9 | 1009 | const struct sr_datafeed_analog2 *analog2; |
7d2afd6c | 1010 | |
ca7dbb56 | 1011 | /* Please use the same order as in libsigrok.h. */ |
7d2afd6c BV |
1012 | switch (packet->type) { |
1013 | case SR_DF_HEADER: | |
a421dc1d | 1014 | sr_dbg("bus: Received SR_DF_HEADER packet."); |
7d2afd6c | 1015 | break; |
55c9f09d UH |
1016 | case SR_DF_END: |
1017 | sr_dbg("bus: Received SR_DF_END packet."); | |
7d2afd6c | 1018 | break; |
c71bac3b | 1019 | case SR_DF_META: |
a421dc1d | 1020 | sr_dbg("bus: Received SR_DF_META packet."); |
ee7489d2 | 1021 | break; |
55c9f09d UH |
1022 | case SR_DF_TRIGGER: |
1023 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); | |
1024 | break; | |
7d2afd6c BV |
1025 | case SR_DF_LOGIC: |
1026 | logic = packet->payload; | |
7ea45862 UH |
1027 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " |
1028 | "unitsize = %d).", logic->length, logic->unitsize); | |
7d2afd6c | 1029 | break; |
ee7489d2 BV |
1030 | case SR_DF_ANALOG: |
1031 | analog = packet->payload; | |
a421dc1d UH |
1032 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", |
1033 | analog->num_samples); | |
ee7489d2 | 1034 | break; |
6ea7669c | 1035 | case SR_DF_FRAME_BEGIN: |
a421dc1d | 1036 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); |
6ea7669c BV |
1037 | break; |
1038 | case SR_DF_FRAME_END: | |
a421dc1d | 1039 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); |
6ea7669c | 1040 | break; |
55c9f09d UH |
1041 | case SR_DF_ANALOG2: |
1042 | analog2 = packet->payload; | |
1043 | sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).", | |
1044 | analog2->num_samples); | |
1045 | break; | |
7d2afd6c | 1046 | default: |
a421dc1d | 1047 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); |
9f45fb3a | 1048 | break; |
7d2afd6c | 1049 | } |
7d2afd6c BV |
1050 | } |
1051 | ||
9f45fb3a | 1052 | /** |
a1645fcd BV |
1053 | * Send a packet to whatever is listening on the datafeed bus. |
1054 | * | |
1055 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 1056 | * |
6b2d8d3e | 1057 | * @param sdi TODO. |
31ccebc4 | 1058 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 1059 | * |
04cb9157 MH |
1060 | * @retval SR_OK Success. |
1061 | * @retval SR_ERR_ARG Invalid argument. | |
b4bd7088 UH |
1062 | * |
1063 | * @private | |
9f45fb3a | 1064 | */ |
de4d3f99 | 1065 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
8143cfdc | 1066 | const struct sr_datafeed_packet *packet) |
a1bb33af UH |
1067 | { |
1068 | GSList *l; | |
2726474a | 1069 | struct datafeed_callback *cb_struct; |
c0a1e532 UH |
1070 | struct sr_datafeed_packet *packet_in, *packet_out; |
1071 | struct sr_transform *t; | |
1072 | int ret; | |
a1bb33af | 1073 | |
de4d3f99 | 1074 | if (!sdi) { |
a421dc1d | 1075 | sr_err("%s: sdi was NULL", __func__); |
e0508e67 | 1076 | return SR_ERR_ARG; |
9f45fb3a UH |
1077 | } |
1078 | ||
e0508e67 | 1079 | if (!packet) { |
a421dc1d | 1080 | sr_err("%s: packet was NULL", __func__); |
e0508e67 | 1081 | return SR_ERR_ARG; |
9f45fb3a UH |
1082 | } |
1083 | ||
79f92686 BV |
1084 | if (!sdi->session) { |
1085 | sr_err("%s: session was NULL", __func__); | |
1086 | return SR_ERR_BUG; | |
1087 | } | |
1088 | ||
c0a1e532 UH |
1089 | /* |
1090 | * Pass the packet to the first transform module. If that returns | |
1091 | * another packet (instead of NULL), pass that packet to the next | |
1092 | * transform module in the list, and so on. | |
1093 | */ | |
1094 | packet_in = (struct sr_datafeed_packet *)packet; | |
1095 | for (l = sdi->session->transforms; l; l = l->next) { | |
1096 | t = l->data; | |
1097 | sr_spew("Running transform module '%s'.", t->module->id); | |
1098 | ret = t->module->receive(t, packet_in, &packet_out); | |
1099 | if (ret < 0) { | |
1100 | sr_err("Error while running transform module: %d.", ret); | |
1101 | return SR_ERR; | |
1102 | } | |
1103 | if (!packet_out) { | |
1104 | /* | |
1105 | * If any of the transforms don't return an output | |
1106 | * packet, abort. | |
1107 | */ | |
1108 | sr_spew("Transform module didn't return a packet, aborting."); | |
1109 | return SR_OK; | |
1110 | } else { | |
1111 | /* | |
1112 | * Use this transform module's output packet as input | |
1113 | * for the next transform module. | |
1114 | */ | |
1115 | packet_in = packet_out; | |
1116 | } | |
1117 | } | |
4ec436c4 | 1118 | packet = packet_in; |
c0a1e532 UH |
1119 | |
1120 | /* | |
1121 | * If the last transform did output a packet, pass it to all datafeed | |
1122 | * callbacks. | |
1123 | */ | |
3337e9a1 | 1124 | for (l = sdi->session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
1125 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
1126 | datafeed_dump(packet); | |
2726474a ML |
1127 | cb_struct = l->data; |
1128 | cb_struct->cb(sdi, packet, cb_struct->cb_data); | |
a1bb33af | 1129 | } |
9f45fb3a | 1130 | |
e0508e67 | 1131 | return SR_OK; |
a1bb33af UH |
1132 | } |
1133 | ||
6b2d8d3e UH |
1134 | /** |
1135 | * Add an event source for a file descriptor. | |
1136 | * | |
7efe889e | 1137 | * @param session The session to use. Must not be NULL. |
c2bf5506 DE |
1138 | * @param key The key which identifies the event source. |
1139 | * @param source An event source object. Must not be NULL. | |
04cb9157 MH |
1140 | * @retval SR_OK Success. |
1141 | * @retval SR_ERR_ARG Invalid argument. | |
c2bf5506 DE |
1142 | * @retval SR_ERR_BUG Event source with @a key already installed. |
1143 | * @retval SR_ERR Other error. | |
6b2d8d3e | 1144 | */ |
62d7945f | 1145 | SR_PRIV int sr_session_source_add_internal(struct sr_session *session, |
c2bf5506 | 1146 | void *key, GSource *source) |
544a4582 | 1147 | { |
c2bf5506 DE |
1148 | /* |
1149 | * This must not ever happen, since the source has already been | |
1150 | * created and its finalize() method will remove the key for the | |
1151 | * already installed source. (Well it would, if we did not have | |
1152 | * another sanity check there.) | |
92248e78 | 1153 | */ |
c2bf5506 DE |
1154 | if (g_hash_table_contains(session->event_sources, key)) { |
1155 | sr_err("Event source with key %p already exists.", key); | |
1156 | return SR_ERR_BUG; | |
92248e78 | 1157 | } |
c2bf5506 DE |
1158 | g_hash_table_insert(session->event_sources, key, source); |
1159 | ||
5de0fc55 DE |
1160 | if (session_source_attach(session, source) == 0) |
1161 | return SR_ERR; | |
faa5d7d9 | 1162 | |
5de0fc55 | 1163 | return SR_OK; |
534b634c DE |
1164 | } |
1165 | ||
cbc1413f DE |
1166 | SR_PRIV int sr_session_fd_source_add(struct sr_session *session, |
1167 | void *key, gintptr fd, int events, int timeout, | |
c2bf5506 | 1168 | sr_receive_data_callback cb, void *cb_data) |
534b634c | 1169 | { |
c2bf5506 DE |
1170 | GSource *source; |
1171 | int ret; | |
534b634c | 1172 | |
c2bf5506 DE |
1173 | source = fd_source_new(session, key, fd, events, timeout); |
1174 | if (!source) | |
534b634c | 1175 | return SR_ERR; |
534b634c | 1176 | |
c2bf5506 | 1177 | g_source_set_callback(source, (GSourceFunc)cb, cb_data, NULL); |
534b634c | 1178 | |
c2bf5506 DE |
1179 | ret = sr_session_source_add_internal(session, key, source); |
1180 | g_source_unref(source); | |
544a4582 | 1181 | |
c2bf5506 | 1182 | return ret; |
544a4582 BV |
1183 | } |
1184 | ||
9f45fb3a | 1185 | /** |
6b2d8d3e | 1186 | * Add an event source for a file descriptor. |
9f45fb3a | 1187 | * |
7efe889e | 1188 | * @param session The session to use. Must not be NULL. |
534b634c | 1189 | * @param fd The file descriptor, or a negative value to create a timer source. |
aac0ea25 | 1190 | * @param events Events to check for. |
faa5d7d9 DE |
1191 | * @param timeout Max time in ms to wait before the callback is called, |
1192 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1193 | * @param cb Callback function to add. Must not be NULL. |
1194 | * @param cb_data Data for the callback function. Can be NULL. | |
9f45fb3a | 1195 | * |
04cb9157 MH |
1196 | * @retval SR_OK Success. |
1197 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1198 | * |
1199 | * @since 0.3.0 | |
aac0ea25 | 1200 | */ |
0812c40e ML |
1201 | SR_API int sr_session_source_add(struct sr_session *session, int fd, |
1202 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 | 1203 | { |
faa5d7d9 | 1204 | if (fd < 0 && timeout < 0) { |
c2bf5506 | 1205 | sr_err("Cannot create timer source without timeout."); |
faa5d7d9 DE |
1206 | return SR_ERR_ARG; |
1207 | } | |
cbc1413f | 1208 | return sr_session_fd_source_add(session, GINT_TO_POINTER(fd), |
c2bf5506 | 1209 | fd, events, timeout, cb, cb_data); |
aac0ea25 LPC |
1210 | } |
1211 | ||
1212 | /** | |
1a895c61 | 1213 | * Add an event source for a GPollFD. |
aac0ea25 | 1214 | * |
7efe889e | 1215 | * @param session The session to use. Must not be NULL. |
faa5d7d9 DE |
1216 | * @param pollfd The GPollFD. Must not be NULL. |
1217 | * @param timeout Max time in ms to wait before the callback is called, | |
1218 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1219 | * @param cb Callback function to add. Must not be NULL. |
1220 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 1221 | * |
04cb9157 MH |
1222 | * @retval SR_OK Success. |
1223 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1224 | * |
1225 | * @since 0.3.0 | |
9f45fb3a | 1226 | */ |
0812c40e ML |
1227 | SR_API int sr_session_source_add_pollfd(struct sr_session *session, |
1228 | GPollFD *pollfd, int timeout, sr_receive_data_callback cb, | |
1229 | void *cb_data) | |
aac0ea25 | 1230 | { |
faa5d7d9 DE |
1231 | if (!pollfd) { |
1232 | sr_err("%s: pollfd was NULL", __func__); | |
1233 | return SR_ERR_ARG; | |
1234 | } | |
cbc1413f | 1235 | return sr_session_fd_source_add(session, pollfd, pollfd->fd, |
c2bf5506 | 1236 | pollfd->events, timeout, cb, cb_data); |
aac0ea25 LPC |
1237 | } |
1238 | ||
1239 | /** | |
1a895c61 | 1240 | * Add an event source for a GIOChannel. |
aac0ea25 | 1241 | * |
7efe889e | 1242 | * @param session The session to use. Must not be NULL. |
aac0ea25 LPC |
1243 | * @param channel The GIOChannel. |
1244 | * @param events Events to poll on. | |
faa5d7d9 DE |
1245 | * @param timeout Max time in ms to wait before the callback is called, |
1246 | * or -1 to wait indefinitely. | |
aac0ea25 LPC |
1247 | * @param cb Callback function to add. Must not be NULL. |
1248 | * @param cb_data Data for the callback function. Can be NULL. | |
1249 | * | |
04cb9157 MH |
1250 | * @retval SR_OK Success. |
1251 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 UH |
1252 | * |
1253 | * @since 0.3.0 | |
aac0ea25 | 1254 | */ |
0812c40e ML |
1255 | SR_API int sr_session_source_add_channel(struct sr_session *session, |
1256 | GIOChannel *channel, int events, int timeout, | |
1257 | sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 | 1258 | { |
c2bf5506 | 1259 | GPollFD pollfd; |
aac0ea25 | 1260 | |
c2bf5506 DE |
1261 | if (!channel) { |
1262 | sr_err("%s: channel was NULL", __func__); | |
1263 | return SR_ERR_ARG; | |
1264 | } | |
1265 | /* We should be using g_io_create_watch(), but can't without | |
1266 | * changing the driver API, as the callback signature is different. | |
1267 | */ | |
faa5d7d9 | 1268 | #ifdef G_OS_WIN32 |
c2bf5506 | 1269 | g_io_channel_win32_make_pollfd(channel, events, &pollfd); |
aac0ea25 | 1270 | #else |
c2bf5506 DE |
1271 | pollfd.fd = g_io_channel_unix_get_fd(channel); |
1272 | pollfd.events = events; | |
aac0ea25 | 1273 | #endif |
cbc1413f | 1274 | return sr_session_fd_source_add(session, channel, pollfd.fd, |
c2bf5506 | 1275 | pollfd.events, timeout, cb, cb_data); |
aac0ea25 LPC |
1276 | } |
1277 | ||
6b2d8d3e | 1278 | /** |
92248e78 | 1279 | * Remove the source identified by the specified poll object. |
6b2d8d3e | 1280 | * |
7efe889e | 1281 | * @param session The session to use. Must not be NULL. |
c2bf5506 | 1282 | * @param key The key by which the source is identified. |
6b2d8d3e | 1283 | * |
04cb9157 | 1284 | * @retval SR_OK Success |
92248e78 | 1285 | * @retval SR_ERR_BUG No event source for poll_object found. |
6b2d8d3e | 1286 | */ |
92248e78 | 1287 | SR_PRIV int sr_session_source_remove_internal(struct sr_session *session, |
c2bf5506 | 1288 | void *key) |
544a4582 | 1289 | { |
c2bf5506 | 1290 | GSource *source; |
544a4582 | 1291 | |
c2bf5506 DE |
1292 | source = g_hash_table_lookup(session->event_sources, key); |
1293 | /* | |
1294 | * Trying to remove an already removed event source is problematic | |
92248e78 DE |
1295 | * since the poll_object handle may have been reused in the meantime. |
1296 | */ | |
534b634c | 1297 | if (!source) { |
c2bf5506 DE |
1298 | sr_warn("Cannot remove non-existing event source %p.", key); |
1299 | return SR_ERR_BUG; | |
534b634c | 1300 | } |
c2bf5506 | 1301 | g_source_destroy(source); |
534b634c | 1302 | |
c2bf5506 | 1303 | return SR_OK; |
534b634c DE |
1304 | } |
1305 | ||
6b2d8d3e | 1306 | /** |
aac0ea25 LPC |
1307 | * Remove the source belonging to the specified file descriptor. |
1308 | * | |
7efe889e | 1309 | * @param session The session to use. Must not be NULL. |
1a895c61 | 1310 | * @param fd The file descriptor for which the source should be removed. |
aac0ea25 | 1311 | * |
04cb9157 MH |
1312 | * @retval SR_OK Success |
1313 | * @retval SR_ERR_ARG Invalid argument | |
04cb9157 | 1314 | * @retval SR_ERR_BUG Internal error. |
47117241 UH |
1315 | * |
1316 | * @since 0.3.0 | |
aac0ea25 | 1317 | */ |
0812c40e | 1318 | SR_API int sr_session_source_remove(struct sr_session *session, int fd) |
aac0ea25 | 1319 | { |
c2bf5506 | 1320 | return sr_session_source_remove_internal(session, GINT_TO_POINTER(fd)); |
aac0ea25 LPC |
1321 | } |
1322 | ||
1323 | /** | |
1324 | * Remove the source belonging to the specified poll descriptor. | |
1325 | * | |
7efe889e | 1326 | * @param session The session to use. Must not be NULL. |
aac0ea25 | 1327 | * @param pollfd The poll descriptor for which the source should be removed. |
faa5d7d9 | 1328 | * Must not be NULL. |
aac0ea25 LPC |
1329 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
1330 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
1331 | * internal errors. | |
47117241 UH |
1332 | * |
1333 | * @since 0.2.0 | |
aac0ea25 | 1334 | */ |
0812c40e ML |
1335 | SR_API int sr_session_source_remove_pollfd(struct sr_session *session, |
1336 | GPollFD *pollfd) | |
aac0ea25 | 1337 | { |
faa5d7d9 DE |
1338 | if (!pollfd) { |
1339 | sr_err("%s: pollfd was NULL", __func__); | |
1340 | return SR_ERR_ARG; | |
1341 | } | |
c2bf5506 | 1342 | return sr_session_source_remove_internal(session, pollfd); |
aac0ea25 LPC |
1343 | } |
1344 | ||
6b2d8d3e | 1345 | /** |
aac0ea25 LPC |
1346 | * Remove the source belonging to the specified channel. |
1347 | * | |
7efe889e | 1348 | * @param session The session to use. Must not be NULL. |
1a895c61 | 1349 | * @param channel The channel for which the source should be removed. |
faa5d7d9 | 1350 | * Must not be NULL. |
04cb9157 MH |
1351 | * @retval SR_OK Success. |
1352 | * @retval SR_ERR_ARG Invalid argument. | |
04cb9157 | 1353 | * @return SR_ERR_BUG Internal error. |
47117241 UH |
1354 | * |
1355 | * @since 0.2.0 | |
aac0ea25 | 1356 | */ |
0812c40e ML |
1357 | SR_API int sr_session_source_remove_channel(struct sr_session *session, |
1358 | GIOChannel *channel) | |
aac0ea25 | 1359 | { |
faa5d7d9 DE |
1360 | if (!channel) { |
1361 | sr_err("%s: channel was NULL", __func__); | |
1362 | return SR_ERR_ARG; | |
1363 | } | |
c2bf5506 DE |
1364 | return sr_session_source_remove_internal(session, channel); |
1365 | } | |
1366 | ||
1367 | /** Unregister an event source that has been destroyed. | |
1368 | * | |
1369 | * This is intended to be called from a source's finalize() method. | |
1370 | * | |
1371 | * @param session The session to use. Must not be NULL. | |
1372 | * @param key The key used to identify @a source. | |
1373 | * @param source The source object that was destroyed. | |
1374 | * | |
1375 | * @retval SR_OK Success. | |
1376 | * @retval SR_ERR_BUG Event source for @a key does not match @a source. | |
5de0fc55 | 1377 | * @retval SR_ERR Other error. |
c2bf5506 DE |
1378 | */ |
1379 | SR_PRIV int sr_session_source_destroyed(struct sr_session *session, | |
1380 | void *key, GSource *source) | |
1381 | { | |
1382 | GSource *registered_source; | |
1383 | ||
1384 | registered_source = g_hash_table_lookup(session->event_sources, key); | |
1385 | /* | |
1386 | * Trying to remove an already removed event source is problematic | |
1387 | * since the poll_object handle may have been reused in the meantime. | |
1388 | */ | |
1389 | if (!registered_source) { | |
1390 | sr_err("No event source for key %p found.", key); | |
1391 | return SR_ERR_BUG; | |
1392 | } | |
1393 | if (registered_source != source) { | |
1394 | sr_err("Event source for key %p does not match" | |
1395 | " destroyed source.", key); | |
1396 | return SR_ERR_BUG; | |
1397 | } | |
1398 | g_hash_table_remove(session->event_sources, key); | |
5de0fc55 DE |
1399 | |
1400 | if (g_hash_table_size(session->event_sources) > 0) | |
1401 | return SR_OK; | |
1402 | ||
1403 | /* If no event sources are left, consider the acquisition finished. | |
1404 | * This is pretty crude, as it requires all event sources to be | |
1405 | * registered via the libsigrok API. | |
c2bf5506 | 1406 | */ |
5de0fc55 | 1407 | return stop_check_later(session); |
aac0ea25 | 1408 | } |
7b870c38 | 1409 | |
ee29d92e | 1410 | static void copy_src(struct sr_config *src, struct sr_datafeed_meta *meta_copy) |
8143cfdc | 1411 | { |
8143cfdc | 1412 | g_variant_ref(src->data); |
ee29d92e AJ |
1413 | meta_copy->config = g_slist_append(meta_copy->config, |
1414 | g_memdup(src, sizeof(struct sr_config))); | |
8143cfdc BV |
1415 | } |
1416 | ||
1417 | SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, | |
1418 | struct sr_datafeed_packet **copy) | |
1419 | { | |
1420 | const struct sr_datafeed_meta *meta; | |
1421 | struct sr_datafeed_meta *meta_copy; | |
1422 | const struct sr_datafeed_logic *logic; | |
1423 | struct sr_datafeed_logic *logic_copy; | |
1424 | const struct sr_datafeed_analog *analog; | |
1425 | struct sr_datafeed_analog *analog_copy; | |
dbdfa4fb ML |
1426 | const struct sr_datafeed_analog2 *analog2; |
1427 | struct sr_datafeed_analog2 *analog2_copy; | |
8143cfdc BV |
1428 | uint8_t *payload; |
1429 | ||
1430 | *copy = g_malloc0(sizeof(struct sr_datafeed_packet)); | |
1431 | (*copy)->type = packet->type; | |
1432 | ||
1433 | switch (packet->type) { | |
1434 | case SR_DF_TRIGGER: | |
1435 | case SR_DF_END: | |
1436 | /* No payload. */ | |
1437 | break; | |
1438 | case SR_DF_HEADER: | |
1439 | payload = g_malloc(sizeof(struct sr_datafeed_header)); | |
1440 | memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header)); | |
1441 | (*copy)->payload = payload; | |
1442 | break; | |
1443 | case SR_DF_META: | |
1444 | meta = packet->payload; | |
ee29d92e AJ |
1445 | meta_copy = g_malloc0(sizeof(struct sr_datafeed_meta)); |
1446 | g_slist_foreach(meta->config, (GFunc)copy_src, meta_copy->config); | |
8143cfdc BV |
1447 | (*copy)->payload = meta_copy; |
1448 | break; | |
1449 | case SR_DF_LOGIC: | |
1450 | logic = packet->payload; | |
1451 | logic_copy = g_malloc(sizeof(logic)); | |
1452 | logic_copy->length = logic->length; | |
1453 | logic_copy->unitsize = logic->unitsize; | |
1454 | memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize); | |
1455 | (*copy)->payload = logic_copy; | |
1456 | break; | |
1457 | case SR_DF_ANALOG: | |
1458 | analog = packet->payload; | |
1459 | analog_copy = g_malloc(sizeof(analog)); | |
1460 | analog_copy->channels = g_slist_copy(analog->channels); | |
1461 | analog_copy->num_samples = analog->num_samples; | |
1462 | analog_copy->mq = analog->mq; | |
1463 | analog_copy->unit = analog->unit; | |
1464 | analog_copy->mqflags = analog->mqflags; | |
39e01135 | 1465 | analog_copy->data = g_malloc(analog->num_samples * sizeof(float)); |
8143cfdc BV |
1466 | memcpy(analog_copy->data, analog->data, |
1467 | analog->num_samples * sizeof(float)); | |
1468 | (*copy)->payload = analog_copy; | |
1469 | break; | |
dbdfa4fb ML |
1470 | case SR_DF_ANALOG2: |
1471 | analog2 = packet->payload; | |
1472 | analog2_copy = g_malloc(sizeof(analog2)); | |
1473 | analog2_copy->data = g_malloc( | |
1474 | analog2->encoding->unitsize * analog2->num_samples); | |
1475 | memcpy(analog2_copy->data, analog2->data, | |
1476 | analog2->encoding->unitsize * analog2->num_samples); | |
1477 | analog2_copy->num_samples = analog2->num_samples; | |
1478 | analog2_copy->encoding = g_memdup(analog2->encoding, | |
1479 | sizeof(struct sr_analog_encoding)); | |
1480 | analog2_copy->meaning = g_memdup(analog2->meaning, | |
1481 | sizeof(struct sr_analog_meaning)); | |
1482 | analog2_copy->meaning->channels = g_slist_copy( | |
1483 | analog2->meaning->channels); | |
1484 | analog2_copy->spec = g_memdup(analog2->spec, | |
1485 | sizeof(struct sr_analog_spec)); | |
1486 | (*copy)->payload = analog2_copy; | |
1487 | break; | |
8143cfdc BV |
1488 | default: |
1489 | sr_err("Unknown packet type %d", packet->type); | |
1490 | return SR_ERR; | |
1491 | } | |
1492 | ||
1493 | return SR_OK; | |
1494 | } | |
1495 | ||
1496 | void sr_packet_free(struct sr_datafeed_packet *packet) | |
1497 | { | |
1498 | const struct sr_datafeed_meta *meta; | |
1499 | const struct sr_datafeed_logic *logic; | |
1500 | const struct sr_datafeed_analog *analog; | |
83c1dbd9 | 1501 | const struct sr_datafeed_analog2 *analog2; |
8143cfdc BV |
1502 | struct sr_config *src; |
1503 | GSList *l; | |
1504 | ||
1505 | switch (packet->type) { | |
1506 | case SR_DF_TRIGGER: | |
1507 | case SR_DF_END: | |
1508 | /* No payload. */ | |
1509 | break; | |
1510 | case SR_DF_HEADER: | |
1511 | /* Payload is a simple struct. */ | |
1512 | g_free((void *)packet->payload); | |
1513 | break; | |
1514 | case SR_DF_META: | |
1515 | meta = packet->payload; | |
1516 | for (l = meta->config; l; l = l->next) { | |
1517 | src = l->data; | |
1518 | g_variant_unref(src->data); | |
1519 | g_free(src); | |
1520 | } | |
1521 | g_slist_free(meta->config); | |
1522 | g_free((void *)packet->payload); | |
1523 | break; | |
1524 | case SR_DF_LOGIC: | |
1525 | logic = packet->payload; | |
1526 | g_free(logic->data); | |
1527 | g_free((void *)packet->payload); | |
1528 | break; | |
1529 | case SR_DF_ANALOG: | |
1530 | analog = packet->payload; | |
1531 | g_slist_free(analog->channels); | |
1532 | g_free(analog->data); | |
1533 | g_free((void *)packet->payload); | |
1534 | break; | |
83c1dbd9 ML |
1535 | case SR_DF_ANALOG2: |
1536 | analog2 = packet->payload; | |
1537 | g_free(analog2->data); | |
1538 | g_free(analog2->encoding); | |
1539 | g_slist_free(analog2->meaning->channels); | |
1540 | g_free(analog2->meaning); | |
1541 | g_free(analog2->spec); | |
1542 | g_free((void *)packet->payload); | |
1543 | break; | |
8143cfdc BV |
1544 | default: |
1545 | sr_err("Unknown packet type %d", packet->type); | |
1546 | } | |
1547 | g_free(packet); | |
1548 | ||
1549 | } | |
1550 | ||
7b870c38 | 1551 | /** @} */ |