]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
a1bb33af UH |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <unistd.h> | |
23 | #include <string.h> | |
544a4582 | 24 | #include <glib.h> |
45c59c8b BV |
25 | #include "libsigrok.h" |
26 | #include "libsigrok-internal.h" | |
aa4b1107 | 27 | |
2ad1deb8 | 28 | /** @cond PRIVATE */ |
3544f848 | 29 | #define LOG_PREFIX "session" |
2ad1deb8 | 30 | /** @endcond */ |
a421dc1d | 31 | |
393fb9cb UH |
32 | /** |
33 | * @file | |
34 | * | |
35 | * Creating, using, or destroying libsigrok sessions. | |
36 | */ | |
37 | ||
7b870c38 UH |
38 | /** |
39 | * @defgroup grp_session Session handling | |
40 | * | |
41 | * Creating, using, or destroying libsigrok sessions. | |
42 | * | |
43 | * @{ | |
44 | */ | |
45 | ||
544a4582 | 46 | struct source { |
544a4582 | 47 | int timeout; |
144f6660 | 48 | sr_receive_data_callback cb; |
1f9813eb | 49 | void *cb_data; |
aac0ea25 LPC |
50 | |
51 | /* This is used to keep track of the object (fd, pollfd or channel) which is | |
52 | * being polled and will be used to match the source when removing it again. | |
53 | */ | |
54 | gintptr poll_object; | |
544a4582 BV |
55 | }; |
56 | ||
2726474a | 57 | struct datafeed_callback { |
144f6660 | 58 | sr_datafeed_callback cb; |
2726474a ML |
59 | void *cb_data; |
60 | }; | |
61 | ||
9f45fb3a UH |
62 | /** |
63 | * Create a new session. | |
0812c40e | 64 | * Currently, there can be only one session at a time within the same process. |
9f45fb3a | 65 | * |
0812c40e ML |
66 | * @retval SR_OK Success. |
67 | * @retval SR_ERR_BUG A session exists already. | |
47117241 | 68 | * |
0812c40e | 69 | * @since 0.4.0 |
9f45fb3a | 70 | */ |
3337e9a1 | 71 | SR_API int sr_session_new(struct sr_session **new_session) |
a1bb33af | 72 | { |
3337e9a1 | 73 | struct sr_session *session; |
a1bb33af | 74 | |
3337e9a1 | 75 | session = g_malloc0(sizeof(struct sr_session)); |
b7e94111 | 76 | |
3337e9a1 BV |
77 | session->source_timeout = -1; |
78 | session->running = FALSE; | |
79 | session->abort_session = FALSE; | |
80 | g_mutex_init(&session->stop_mutex); | |
0812c40e | 81 | |
3337e9a1 | 82 | *new_session = session; |
0812c40e ML |
83 | |
84 | return SR_OK; | |
a1bb33af UH |
85 | } |
86 | ||
9f45fb3a | 87 | /** |
0812c40e | 88 | * Destroy a session. |
9f45fb3a UH |
89 | * This frees up all memory used by the session. |
90 | * | |
04cb9157 | 91 | * @retval SR_OK Success. |
0812c40e | 92 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 93 | * |
0812c40e | 94 | * @since 0.4.0 |
9f45fb3a | 95 | */ |
0812c40e | 96 | SR_API int sr_session_destroy(struct sr_session *session) |
a1bb33af | 97 | { |
9f45fb3a | 98 | if (!session) { |
a421dc1d | 99 | sr_err("%s: session was NULL", __func__); |
0812c40e | 100 | return SR_ERR_ARG; |
9f45fb3a UH |
101 | } |
102 | ||
0812c40e | 103 | sr_session_dev_remove_all(session); |
33c6e4c5 | 104 | g_mutex_clear(&session->stop_mutex); |
3d68b612 BV |
105 | if (session->trigger) |
106 | sr_trigger_free(session->trigger); | |
33c6e4c5 | 107 | |
a1bb33af | 108 | g_free(session); |
0812c40e | 109 | |
e0508e67 | 110 | return SR_OK; |
a1bb33af UH |
111 | } |
112 | ||
9f45fb3a | 113 | /** |
0812c40e | 114 | * Remove all the devices from a session. |
9f45fb3a UH |
115 | * |
116 | * The session itself (i.e., the struct sr_session) is not free'd and still | |
117 | * exists after this function returns. | |
118 | * | |
04cb9157 | 119 | * @retval SR_OK Success. |
0812c40e | 120 | * @retval SR_ERR_BUG Invalid session passed. |
47117241 | 121 | * |
0812c40e | 122 | * @since 0.4.0 |
9f45fb3a | 123 | */ |
0812c40e | 124 | SR_API int sr_session_dev_remove_all(struct sr_session *session) |
a1bb33af | 125 | { |
0812c40e ML |
126 | struct sr_dev_inst *sdi; |
127 | GSList *l; | |
128 | ||
9f45fb3a | 129 | if (!session) { |
a421dc1d | 130 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
131 | return SR_ERR_ARG; |
132 | } | |
133 | ||
134 | for (l = session->devs; l; l = l->next) { | |
135 | sdi = (struct sr_dev_inst *) l->data; | |
136 | sdi->session = NULL; | |
9f45fb3a UH |
137 | } |
138 | ||
681803df | 139 | g_slist_free(session->devs); |
bb7ef793 | 140 | session->devs = NULL; |
e0508e67 UH |
141 | |
142 | return SR_OK; | |
a1bb33af UH |
143 | } |
144 | ||
9f45fb3a | 145 | /** |
0812c40e | 146 | * Add a device instance to a session. |
9f45fb3a | 147 | * |
0812c40e | 148 | * @param sdi The device instance to add to a session. Must not |
de4d3f99 BV |
149 | * be NULL. Also, sdi->driver and sdi->driver->dev_open must |
150 | * not be NULL. | |
9f45fb3a | 151 | * |
04cb9157 MH |
152 | * @retval SR_OK Success. |
153 | * @retval SR_ERR_ARG Invalid argument. | |
47117241 | 154 | * |
0812c40e | 155 | * @since 0.4.0 |
9f45fb3a | 156 | */ |
0812c40e ML |
157 | SR_API int sr_session_dev_add(struct sr_session *session, |
158 | struct sr_dev_inst *sdi) | |
a1bb33af | 159 | { |
5451816f | 160 | int ret; |
a1bb33af | 161 | |
de4d3f99 | 162 | if (!sdi) { |
a421dc1d | 163 | sr_err("%s: sdi was NULL", __func__); |
9f45fb3a UH |
164 | return SR_ERR_ARG; |
165 | } | |
166 | ||
d6eb0c33 | 167 | if (!session) { |
a421dc1d | 168 | sr_err("%s: session was NULL", __func__); |
0812c40e ML |
169 | return SR_ERR_ARG; |
170 | } | |
171 | ||
172 | /* If sdi->session is not NULL, the device is already in this or | |
173 | * another session. */ | |
174 | if (sdi->session) { | |
175 | sr_err("%s: already assigned to session", __func__); | |
176 | return SR_ERR_ARG; | |
d6eb0c33 UH |
177 | } |
178 | ||
de4d3f99 BV |
179 | /* If sdi->driver is NULL, this is a virtual device. */ |
180 | if (!sdi->driver) { | |
a421dc1d | 181 | sr_dbg("%s: sdi->driver was NULL, this seems to be " |
d6eb0c33 UH |
182 | "a virtual device; continuing", __func__); |
183 | /* Just add the device, don't run dev_open(). */ | |
de4d3f99 | 184 | session->devs = g_slist_append(session->devs, (gpointer)sdi); |
0812c40e | 185 | sdi->session = session; |
d6eb0c33 | 186 | return SR_OK; |
9f45fb3a UH |
187 | } |
188 | ||
de4d3f99 BV |
189 | /* sdi->driver is non-NULL (i.e. we have a real device). */ |
190 | if (!sdi->driver->dev_open) { | |
a421dc1d | 191 | sr_err("%s: sdi->driver->dev_open was NULL", __func__); |
8ec95d22 | 192 | return SR_ERR_BUG; |
9f45fb3a UH |
193 | } |
194 | ||
de4d3f99 | 195 | session->devs = g_slist_append(session->devs, (gpointer)sdi); |
0812c40e | 196 | sdi->session = session; |
aa4b1107 | 197 | |
5451816f | 198 | if (session->running) { |
32b7cd4f DE |
199 | /* Adding a device to a running session. Commit settings |
200 | * and start acquisition on that device now. */ | |
201 | if ((ret = sr_config_commit(sdi)) != SR_OK) { | |
202 | sr_err("Failed to commit device settings before " | |
203 | "starting acquisition in running session (%s)", | |
204 | sr_strerror(ret)); | |
205 | return ret; | |
206 | } | |
5451816f | 207 | if ((ret = sdi->driver->dev_acquisition_start(sdi, |
32b7cd4f | 208 | (void *)sdi)) != SR_OK) { |
5451816f | 209 | sr_err("Failed to start acquisition of device in " |
32b7cd4f DE |
210 | "running session (%s)", sr_strerror(ret)); |
211 | return ret; | |
212 | } | |
5451816f BV |
213 | } |
214 | ||
e46b8fb1 | 215 | return SR_OK; |
a1bb33af UH |
216 | } |
217 | ||
2bb311b4 | 218 | /** |
0812c40e | 219 | * List all device instances attached to a session. |
2bb311b4 BV |
220 | * |
221 | * @param devlist A pointer where the device instance list will be | |
222 | * stored on return. If no devices are in the session, | |
223 | * this will be NULL. Each element in the list points | |
224 | * to a struct sr_dev_inst *. | |
225 | * The list must be freed by the caller, but not the | |
226 | * elements pointed to. | |
227 | * | |
04cb9157 | 228 | * @retval SR_OK Success. |
0812c40e | 229 | * @retval SR_ERR_ARG Invalid argument. |
47117241 | 230 | * |
0812c40e | 231 | * @since 0.4.0 |
2bb311b4 | 232 | */ |
0812c40e | 233 | SR_API int sr_session_dev_list(struct sr_session *session, GSList **devlist) |
2bb311b4 | 234 | { |
2bb311b4 | 235 | if (!session) |
0812c40e ML |
236 | return SR_ERR_ARG; |
237 | ||
238 | if (!devlist) | |
239 | return SR_ERR_ARG; | |
2bb311b4 BV |
240 | |
241 | *devlist = g_slist_copy(session->devs); | |
242 | ||
243 | return SR_OK; | |
244 | } | |
245 | ||
9f45fb3a | 246 | /** |
0812c40e | 247 | * Remove all datafeed callbacks in a session. |
9f45fb3a | 248 | * |
04cb9157 | 249 | * @retval SR_OK Success. |
0812c40e | 250 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 251 | * |
0812c40e | 252 | * @since 0.4.0 |
9f45fb3a | 253 | */ |
0812c40e | 254 | SR_API int sr_session_datafeed_callback_remove_all(struct sr_session *session) |
a1bb33af | 255 | { |
9f45fb3a | 256 | if (!session) { |
a421dc1d | 257 | sr_err("%s: session was NULL", __func__); |
0812c40e | 258 | return SR_ERR_ARG; |
9f45fb3a UH |
259 | } |
260 | ||
2726474a | 261 | g_slist_free_full(session->datafeed_callbacks, g_free); |
a1bb33af | 262 | session->datafeed_callbacks = NULL; |
e0508e67 UH |
263 | |
264 | return SR_OK; | |
a1bb33af UH |
265 | } |
266 | ||
9f45fb3a | 267 | /** |
0812c40e | 268 | * Add a datafeed callback to a session. |
9f45fb3a | 269 | * |
d08490aa | 270 | * @param cb Function to call when a chunk of data is received. |
0abee507 | 271 | * Must not be NULL. |
85222791 | 272 | * @param cb_data Opaque pointer passed in by the caller. |
a1645fcd | 273 | * |
04cb9157 MH |
274 | * @retval SR_OK Success. |
275 | * @retval SR_ERR_BUG No session exists. | |
47117241 UH |
276 | * |
277 | * @since 0.3.0 | |
9f45fb3a | 278 | */ |
0812c40e ML |
279 | SR_API int sr_session_datafeed_callback_add(struct sr_session *session, |
280 | sr_datafeed_callback cb, void *cb_data) | |
a1bb33af | 281 | { |
2726474a ML |
282 | struct datafeed_callback *cb_struct; |
283 | ||
9f45fb3a | 284 | if (!session) { |
a421dc1d | 285 | sr_err("%s: session was NULL", __func__); |
e0508e67 | 286 | return SR_ERR_BUG; |
9f45fb3a UH |
287 | } |
288 | ||
0abee507 | 289 | if (!cb) { |
a421dc1d | 290 | sr_err("%s: cb was NULL", __func__); |
0abee507 UH |
291 | return SR_ERR_ARG; |
292 | } | |
9f45fb3a | 293 | |
2726474a ML |
294 | if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback)))) |
295 | return SR_ERR_MALLOC; | |
296 | ||
297 | cb_struct->cb = cb; | |
298 | cb_struct->cb_data = cb_data; | |
299 | ||
62c82025 | 300 | session->datafeed_callbacks = |
2726474a | 301 | g_slist_append(session->datafeed_callbacks, cb_struct); |
e0508e67 UH |
302 | |
303 | return SR_OK; | |
a1bb33af UH |
304 | } |
305 | ||
0812c40e | 306 | SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session) |
7b5e6d29 BV |
307 | { |
308 | return session->trigger; | |
309 | } | |
310 | ||
0812c40e | 311 | SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig) |
7b5e6d29 BV |
312 | { |
313 | session->trigger = trig; | |
314 | ||
315 | return SR_OK; | |
316 | } | |
317 | ||
b483be74 | 318 | /** |
0812c40e | 319 | * Call every device in the current session's callback. |
b483be74 BV |
320 | * |
321 | * For sessions not driven by select loops such as sr_session_run(), | |
322 | * but driven by another scheduler, this can be used to poll the devices | |
323 | * from within that scheduler. | |
324 | * | |
f6eb2cb5 BV |
325 | * @param block If TRUE, this call will wait for any of the session's |
326 | * sources to fire an event on the file descriptors, or | |
327 | * any of their timeouts to activate. In other words, this | |
328 | * can be used as a select loop. | |
329 | * If FALSE, all sources have their callback run, regardless | |
330 | * of file descriptor or timeout status. | |
331 | * | |
04cb9157 MH |
332 | * @retval SR_OK Success. |
333 | * @retval SR_ERR Error occured. | |
b483be74 | 334 | */ |
3337e9a1 | 335 | static int sr_session_iteration(struct sr_session *session, gboolean block) |
544a4582 | 336 | { |
b7e94111 LPC |
337 | unsigned int i; |
338 | int ret; | |
544a4582 | 339 | |
b483be74 BV |
340 | ret = g_poll(session->pollfds, session->num_sources, |
341 | block ? session->source_timeout : 0); | |
342 | for (i = 0; i < session->num_sources; i++) { | |
343 | if (session->pollfds[i].revents > 0 || (ret == 0 | |
344 | && session->source_timeout == session->sources[i].timeout)) { | |
33c6e4c5 | 345 | /* |
b483be74 BV |
346 | * Invoke the source's callback on an event, |
347 | * or if the poll timed out and this source | |
348 | * asked for that timeout. | |
33c6e4c5 | 349 | */ |
b483be74 BV |
350 | if (!session->sources[i].cb(session->pollfds[i].fd, |
351 | session->pollfds[i].revents, | |
352 | session->sources[i].cb_data)) | |
0812c40e ML |
353 | sr_session_source_remove(session, |
354 | session->sources[i].poll_object); | |
b483be74 BV |
355 | } |
356 | /* | |
357 | * We want to take as little time as possible to stop | |
358 | * the session if we have been told to do so. Therefore, | |
359 | * we check the flag after processing every source, not | |
360 | * just once per main event loop. | |
361 | */ | |
362 | g_mutex_lock(&session->stop_mutex); | |
363 | if (session->abort_session) { | |
0812c40e | 364 | sr_session_stop_sync(session); |
b483be74 BV |
365 | /* But once is enough. */ |
366 | session->abort_session = FALSE; | |
544a4582 | 367 | } |
b483be74 | 368 | g_mutex_unlock(&session->stop_mutex); |
544a4582 | 369 | } |
e0508e67 UH |
370 | |
371 | return SR_OK; | |
544a4582 BV |
372 | } |
373 | ||
7b5e6d29 BV |
374 | |
375 | static int verify_trigger(struct sr_trigger *trigger) | |
376 | { | |
377 | struct sr_trigger_stage *stage; | |
378 | struct sr_trigger_match *match; | |
379 | GSList *l, *m; | |
380 | ||
381 | if (!trigger->stages) { | |
382 | sr_err("No trigger stages defined."); | |
383 | return SR_ERR; | |
384 | } | |
385 | ||
386 | sr_spew("Checking trigger:"); | |
387 | for (l = trigger->stages; l; l = l->next) { | |
388 | stage = l->data; | |
389 | if (!stage->matches) { | |
390 | sr_err("Stage %d has no matches defined.", stage->stage); | |
391 | return SR_ERR; | |
392 | } | |
393 | for (m = stage->matches; m; m = m->next) { | |
394 | match = m->data; | |
395 | if (!match->channel) { | |
396 | sr_err("Stage %d match has no channel.", stage->stage); | |
397 | return SR_ERR; | |
398 | } | |
399 | if (!match->match) { | |
400 | sr_err("Stage %d match is not defined.", stage->stage); | |
401 | return SR_ERR; | |
402 | } | |
403 | sr_spew("Stage %d match on channel %s, match %d", stage->stage, | |
404 | match->channel->name, match->match); | |
405 | } | |
406 | } | |
407 | ||
408 | return SR_OK; | |
409 | } | |
9f45fb3a UH |
410 | /** |
411 | * Start a session. | |
412 | * | |
04cb9157 | 413 | * @retval SR_OK Success. |
0812c40e | 414 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 415 | * |
0812c40e | 416 | * @since 0.4.0 |
9f45fb3a | 417 | */ |
0812c40e | 418 | SR_API int sr_session_start(struct sr_session *session) |
7d658874 | 419 | { |
de4d3f99 | 420 | struct sr_dev_inst *sdi; |
7d658874 BV |
421 | GSList *l; |
422 | int ret; | |
423 | ||
9f45fb3a | 424 | if (!session) { |
0812c40e ML |
425 | sr_err("%s: session was NULL", __func__); |
426 | return SR_ERR_ARG; | |
9f45fb3a UH |
427 | } |
428 | ||
bb7ef793 | 429 | if (!session->devs) { |
a421dc1d | 430 | sr_err("%s: session->devs was NULL; a session " |
9f45fb3a | 431 | "cannot be started without devices.", __func__); |
0812c40e | 432 | return SR_ERR_ARG; |
9f45fb3a UH |
433 | } |
434 | ||
7b5e6d29 BV |
435 | if (session->trigger && verify_trigger(session->trigger) != SR_OK) |
436 | return SR_ERR; | |
437 | ||
c7142604 | 438 | sr_info("Starting."); |
9f45fb3a | 439 | |
b7c3e849 | 440 | ret = SR_OK; |
bb7ef793 | 441 | for (l = session->devs; l; l = l->next) { |
de4d3f99 | 442 | sdi = l->data; |
32b7cd4f DE |
443 | if ((ret = sr_config_commit(sdi)) != SR_OK) { |
444 | sr_err("Failed to commit device settings before " | |
445 | "starting acquisition (%s)", sr_strerror(ret)); | |
446 | break; | |
447 | } | |
de4d3f99 | 448 | if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) { |
a421dc1d | 449 | sr_err("%s: could not start an acquisition " |
568dcacc | 450 | "(%s)", __func__, sr_strerror(ret)); |
7d658874 | 451 | break; |
9f45fb3a | 452 | } |
7d658874 BV |
453 | } |
454 | ||
9f45fb3a UH |
455 | /* TODO: What if there are multiple devices? Which return code? */ |
456 | ||
7d658874 BV |
457 | return ret; |
458 | } | |
459 | ||
9f45fb3a | 460 | /** |
0812c40e | 461 | * Run a session. |
9f45fb3a | 462 | * |
04cb9157 | 463 | * @retval SR_OK Success. |
0812c40e | 464 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 465 | * |
0812c40e | 466 | * @since 0.4.0 |
9f45fb3a | 467 | */ |
0812c40e | 468 | SR_API int sr_session_run(struct sr_session *session) |
7d658874 | 469 | { |
9f45fb3a | 470 | if (!session) { |
0812c40e ML |
471 | sr_err("%s: session was NULL", __func__); |
472 | return SR_ERR_ARG; | |
9f45fb3a UH |
473 | } |
474 | ||
bb7ef793 | 475 | if (!session->devs) { |
9f45fb3a | 476 | /* TODO: Actually the case? */ |
a421dc1d | 477 | sr_err("%s: session->devs was NULL; a session " |
9f45fb3a | 478 | "cannot be run without devices.", __func__); |
0812c40e | 479 | return SR_ERR_ARG; |
9f45fb3a | 480 | } |
5451816f | 481 | session->running = TRUE; |
9f45fb3a | 482 | |
a421dc1d | 483 | sr_info("Running."); |
7d658874 | 484 | |
9f45fb3a | 485 | /* Do we have real sources? */ |
b7e94111 | 486 | if (session->num_sources == 1 && session->pollfds[0].fd == -1) { |
9f45fb3a | 487 | /* Dummy source, freewheel over it. */ |
2cbeb2b7 | 488 | while (session->num_sources) |
b7e94111 | 489 | session->sources[0].cb(-1, 0, session->sources[0].cb_data); |
9f45fb3a UH |
490 | } else { |
491 | /* Real sources, use g_poll() main loop. */ | |
b483be74 | 492 | while (session->num_sources) |
3337e9a1 | 493 | sr_session_iteration(session, TRUE); |
9f45fb3a UH |
494 | } |
495 | ||
e0508e67 | 496 | return SR_OK; |
7d658874 BV |
497 | } |
498 | ||
9f45fb3a | 499 | /** |
0812c40e | 500 | * Stop a session. |
9f45fb3a | 501 | * |
0812c40e ML |
502 | * The session is stopped immediately, with all acquisition sessions stopped |
503 | * and hardware drivers cleaned up. | |
9f45fb3a | 504 | * |
33c6e4c5 AG |
505 | * This must be called from within the session thread, to prevent freeing |
506 | * resources that the session thread will try to use. | |
507 | * | |
04cb9157 | 508 | * @retval SR_OK Success. |
0812c40e | 509 | * @retval SR_ERR_ARG Invalid session passed. |
72a08bcc BV |
510 | * |
511 | * @private | |
9f45fb3a | 512 | */ |
0812c40e | 513 | SR_PRIV int sr_session_stop_sync(struct sr_session *session) |
a1bb33af | 514 | { |
de4d3f99 | 515 | struct sr_dev_inst *sdi; |
a1bb33af UH |
516 | GSList *l; |
517 | ||
9f45fb3a | 518 | if (!session) { |
a421dc1d | 519 | sr_err("%s: session was NULL", __func__); |
0812c40e | 520 | return SR_ERR_ARG; |
9f45fb3a UH |
521 | } |
522 | ||
a421dc1d | 523 | sr_info("Stopping."); |
e0508e67 | 524 | |
bb7ef793 | 525 | for (l = session->devs; l; l = l->next) { |
de4d3f99 BV |
526 | sdi = l->data; |
527 | if (sdi->driver) { | |
528 | if (sdi->driver->dev_acquisition_stop) | |
529 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
8c76be53 | 530 | } |
a1bb33af | 531 | } |
5451816f | 532 | session->running = FALSE; |
9f45fb3a | 533 | |
e0508e67 | 534 | return SR_OK; |
a1bb33af UH |
535 | } |
536 | ||
33c6e4c5 | 537 | /** |
0812c40e | 538 | * Stop a session. |
33c6e4c5 | 539 | * |
0812c40e ML |
540 | * The session is stopped immediately, with all acquisition sessions being |
541 | * stopped and hardware drivers cleaned up. | |
33c6e4c5 AG |
542 | * |
543 | * If the session is run in a separate thread, this function will not block | |
544 | * until the session is finished executing. It is the caller's responsibility | |
545 | * to wait for the session thread to return before assuming that the session is | |
546 | * completely decommissioned. | |
547 | * | |
04cb9157 | 548 | * @retval SR_OK Success. |
0812c40e | 549 | * @retval SR_ERR_ARG Invalid session passed. |
47117241 | 550 | * |
0812c40e | 551 | * @since 0.4.0 |
33c6e4c5 | 552 | */ |
0812c40e | 553 | SR_API int sr_session_stop(struct sr_session *session) |
33c6e4c5 AG |
554 | { |
555 | if (!session) { | |
556 | sr_err("%s: session was NULL", __func__); | |
557 | return SR_ERR_BUG; | |
558 | } | |
559 | ||
560 | g_mutex_lock(&session->stop_mutex); | |
561 | session->abort_session = TRUE; | |
562 | g_mutex_unlock(&session->stop_mutex); | |
563 | ||
564 | return SR_OK; | |
565 | } | |
566 | ||
9f45fb3a | 567 | /** |
a1645fcd | 568 | * Debug helper. |
9f45fb3a | 569 | * |
996b0c72 | 570 | * @param packet The packet to show debugging information for. |
9f45fb3a | 571 | */ |
bf53457d | 572 | static void datafeed_dump(const struct sr_datafeed_packet *packet) |
7d2afd6c | 573 | { |
bf53457d JH |
574 | const struct sr_datafeed_logic *logic; |
575 | const struct sr_datafeed_analog *analog; | |
7d2afd6c BV |
576 | |
577 | switch (packet->type) { | |
578 | case SR_DF_HEADER: | |
a421dc1d | 579 | sr_dbg("bus: Received SR_DF_HEADER packet."); |
7d2afd6c BV |
580 | break; |
581 | case SR_DF_TRIGGER: | |
a421dc1d | 582 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); |
7d2afd6c | 583 | break; |
c71bac3b | 584 | case SR_DF_META: |
a421dc1d | 585 | sr_dbg("bus: Received SR_DF_META packet."); |
ee7489d2 | 586 | break; |
7d2afd6c BV |
587 | case SR_DF_LOGIC: |
588 | logic = packet->payload; | |
7ea45862 UH |
589 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " |
590 | "unitsize = %d).", logic->length, logic->unitsize); | |
7d2afd6c | 591 | break; |
ee7489d2 BV |
592 | case SR_DF_ANALOG: |
593 | analog = packet->payload; | |
a421dc1d UH |
594 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", |
595 | analog->num_samples); | |
ee7489d2 | 596 | break; |
7d2afd6c | 597 | case SR_DF_END: |
a421dc1d | 598 | sr_dbg("bus: Received SR_DF_END packet."); |
7d2afd6c | 599 | break; |
6ea7669c | 600 | case SR_DF_FRAME_BEGIN: |
a421dc1d | 601 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); |
6ea7669c BV |
602 | break; |
603 | case SR_DF_FRAME_END: | |
a421dc1d | 604 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); |
6ea7669c | 605 | break; |
7d2afd6c | 606 | default: |
a421dc1d | 607 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); |
9f45fb3a | 608 | break; |
7d2afd6c | 609 | } |
7d2afd6c BV |
610 | } |
611 | ||
9f45fb3a | 612 | /** |
a1645fcd BV |
613 | * Send a packet to whatever is listening on the datafeed bus. |
614 | * | |
615 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 616 | * |
6b2d8d3e | 617 | * @param sdi TODO. |
31ccebc4 | 618 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 619 | * |
04cb9157 MH |
620 | * @retval SR_OK Success. |
621 | * @retval SR_ERR_ARG Invalid argument. | |
b4bd7088 UH |
622 | * |
623 | * @private | |
9f45fb3a | 624 | */ |
de4d3f99 | 625 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
bf53457d | 626 | const struct sr_datafeed_packet *packet) |
a1bb33af UH |
627 | { |
628 | GSList *l; | |
2726474a | 629 | struct datafeed_callback *cb_struct; |
a1bb33af | 630 | |
de4d3f99 | 631 | if (!sdi) { |
a421dc1d | 632 | sr_err("%s: sdi was NULL", __func__); |
e0508e67 | 633 | return SR_ERR_ARG; |
9f45fb3a UH |
634 | } |
635 | ||
e0508e67 | 636 | if (!packet) { |
a421dc1d | 637 | sr_err("%s: packet was NULL", __func__); |
e0508e67 | 638 | return SR_ERR_ARG; |
9f45fb3a UH |
639 | } |
640 | ||
3337e9a1 | 641 | for (l = sdi->session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
642 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
643 | datafeed_dump(packet); | |
2726474a ML |
644 | cb_struct = l->data; |
645 | cb_struct->cb(sdi, packet, cb_struct->cb_data); | |
a1bb33af | 646 | } |
9f45fb3a | 647 | |
e0508e67 | 648 | return SR_OK; |
a1bb33af UH |
649 | } |
650 | ||
6b2d8d3e UH |
651 | /** |
652 | * Add an event source for a file descriptor. | |
653 | * | |
654 | * @param pollfd The GPollFD. | |
04cb9157 MH |
655 | * @param[in] timeout Max time to wait before the callback is called, |
656 | * ignored if 0. | |
6b2d8d3e UH |
657 | * @param cb Callback function to add. Must not be NULL. |
658 | * @param cb_data Data for the callback function. Can be NULL. | |
659 | * @param poll_object TODO. | |
660 | * | |
04cb9157 MH |
661 | * @retval SR_OK Success. |
662 | * @retval SR_ERR_ARG Invalid argument. | |
663 | * @retval SR_ERR_MALLOC Memory allocation error. | |
6b2d8d3e | 664 | */ |
102f1239 BV |
665 | static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd, |
666 | int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object) | |
544a4582 BV |
667 | { |
668 | struct source *new_sources, *s; | |
aac0ea25 | 669 | GPollFD *new_pollfds; |
544a4582 | 670 | |
d08490aa | 671 | if (!cb) { |
a421dc1d | 672 | sr_err("%s: cb was NULL", __func__); |
e0508e67 | 673 | return SR_ERR_ARG; |
9f45fb3a UH |
674 | } |
675 | ||
1f9813eb | 676 | /* Note: cb_data can be NULL, that's not a bug. */ |
9f45fb3a | 677 | |
2ac2e629 BV |
678 | new_pollfds = g_try_realloc(session->pollfds, |
679 | sizeof(GPollFD) * (session->num_sources + 1)); | |
0687dfcd | 680 | if (!new_pollfds) { |
a421dc1d | 681 | sr_err("%s: new_pollfds malloc failed", __func__); |
0687dfcd LPC |
682 | return SR_ERR_MALLOC; |
683 | } | |
684 | ||
b7e94111 | 685 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * |
2ac2e629 | 686 | (session->num_sources + 1)); |
9f45fb3a | 687 | if (!new_sources) { |
a421dc1d | 688 | sr_err("%s: new_sources malloc failed", __func__); |
e0508e67 | 689 | return SR_ERR_MALLOC; |
9f45fb3a | 690 | } |
544a4582 | 691 | |
b7e94111 LPC |
692 | new_pollfds[session->num_sources] = *pollfd; |
693 | s = &new_sources[session->num_sources++]; | |
544a4582 | 694 | s->timeout = timeout; |
d08490aa | 695 | s->cb = cb; |
1f9813eb | 696 | s->cb_data = cb_data; |
aac0ea25 | 697 | s->poll_object = poll_object; |
b7e94111 LPC |
698 | session->pollfds = new_pollfds; |
699 | session->sources = new_sources; | |
544a4582 | 700 | |
b7e94111 LPC |
701 | if (timeout != session->source_timeout && timeout > 0 |
702 | && (session->source_timeout == -1 || timeout < session->source_timeout)) | |
703 | session->source_timeout = timeout; | |
9f45fb3a | 704 | |
e0508e67 | 705 | return SR_OK; |
544a4582 BV |
706 | } |
707 | ||
9f45fb3a | 708 | /** |
6b2d8d3e | 709 | * Add an event source for a file descriptor. |
9f45fb3a | 710 | * |
aac0ea25 LPC |
711 | * @param fd The file descriptor. |
712 | * @param events Events to check for. | |
713 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
714 | * @param cb Callback function to add. Must not be NULL. | |
715 | * @param cb_data Data for the callback function. Can be NULL. | |
9f45fb3a | 716 | * |
04cb9157 MH |
717 | * @retval SR_OK Success. |
718 | * @retval SR_ERR_ARG Invalid argument. | |
719 | * @retval SR_ERR_MALLOC Memory allocation error. | |
47117241 UH |
720 | * |
721 | * @since 0.3.0 | |
aac0ea25 | 722 | */ |
0812c40e ML |
723 | SR_API int sr_session_source_add(struct sr_session *session, int fd, |
724 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 LPC |
725 | { |
726 | GPollFD p; | |
727 | ||
0812c40e ML |
728 | (void) session; |
729 | ||
aac0ea25 LPC |
730 | p.fd = fd; |
731 | p.events = events; | |
aac0ea25 | 732 | |
102f1239 | 733 | return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd); |
aac0ea25 LPC |
734 | } |
735 | ||
736 | /** | |
1a895c61 | 737 | * Add an event source for a GPollFD. |
aac0ea25 | 738 | * |
aac0ea25 LPC |
739 | * @param pollfd The GPollFD. |
740 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
741 | * @param cb Callback function to add. Must not be NULL. | |
742 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 743 | * |
04cb9157 MH |
744 | * @retval SR_OK Success. |
745 | * @retval SR_ERR_ARG Invalid argument. | |
746 | * @retval SR_ERR_MALLOC Memory allocation error. | |
47117241 UH |
747 | * |
748 | * @since 0.3.0 | |
9f45fb3a | 749 | */ |
0812c40e ML |
750 | SR_API int sr_session_source_add_pollfd(struct sr_session *session, |
751 | GPollFD *pollfd, int timeout, sr_receive_data_callback cb, | |
752 | void *cb_data) | |
aac0ea25 | 753 | { |
0812c40e ML |
754 | (void) session; |
755 | ||
102f1239 | 756 | return _sr_session_source_add(session, pollfd, timeout, cb, |
1a895c61 | 757 | cb_data, (gintptr)pollfd); |
aac0ea25 LPC |
758 | } |
759 | ||
760 | /** | |
1a895c61 | 761 | * Add an event source for a GIOChannel. |
aac0ea25 | 762 | * |
aac0ea25 LPC |
763 | * @param channel The GIOChannel. |
764 | * @param events Events to poll on. | |
765 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
766 | * @param cb Callback function to add. Must not be NULL. | |
767 | * @param cb_data Data for the callback function. Can be NULL. | |
768 | * | |
04cb9157 MH |
769 | * @retval SR_OK Success. |
770 | * @retval SR_ERR_ARG Invalid argument. | |
771 | * @retval SR_ERR_MALLOC Memory allocation error. | |
47117241 UH |
772 | * |
773 | * @since 0.3.0 | |
aac0ea25 | 774 | */ |
0812c40e ML |
775 | SR_API int sr_session_source_add_channel(struct sr_session *session, |
776 | GIOChannel *channel, int events, int timeout, | |
777 | sr_receive_data_callback cb, void *cb_data) | |
aac0ea25 LPC |
778 | { |
779 | GPollFD p; | |
780 | ||
0812c40e ML |
781 | (void) session; |
782 | ||
aac0ea25 | 783 | #ifdef _WIN32 |
6b2d8d3e | 784 | g_io_channel_win32_make_pollfd(channel, events, &p); |
aac0ea25 LPC |
785 | #else |
786 | p.fd = g_io_channel_unix_get_fd(channel); | |
787 | p.events = events; | |
788 | #endif | |
789 | ||
102f1239 | 790 | return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel); |
aac0ea25 LPC |
791 | } |
792 | ||
6b2d8d3e UH |
793 | /** |
794 | * Remove the source belonging to the specified channel. | |
795 | * | |
796 | * @todo Add more error checks and logging. | |
797 | * | |
04cb9157 | 798 | * @param poll_object The channel for which the source should be removed. |
6b2d8d3e | 799 | * |
04cb9157 MH |
800 | * @retval SR_OK Success |
801 | * @retval SR_ERR_ARG Invalid arguments | |
802 | * @retval SR_ERR_MALLOC Memory allocation error | |
803 | * @retval SR_ERR_BUG Internal error | |
6b2d8d3e | 804 | */ |
102f1239 | 805 | static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object) |
544a4582 BV |
806 | { |
807 | struct source *new_sources; | |
0687dfcd | 808 | GPollFD *new_pollfds; |
b7e94111 | 809 | unsigned int old; |
544a4582 | 810 | |
b7e94111 | 811 | if (!session->sources || !session->num_sources) { |
a421dc1d | 812 | sr_err("%s: sources was NULL", __func__); |
0abee507 | 813 | return SR_ERR_BUG; |
e0508e67 UH |
814 | } |
815 | ||
b7e94111 LPC |
816 | for (old = 0; old < session->num_sources; old++) { |
817 | if (session->sources[old].poll_object == poll_object) | |
2bccd322 | 818 | break; |
9f45fb3a UH |
819 | } |
820 | ||
2bccd322 | 821 | /* fd not found, nothing to do */ |
b7e94111 | 822 | if (old == session->num_sources) |
2bccd322 LPC |
823 | return SR_OK; |
824 | ||
b7e94111 | 825 | session->num_sources -= 1; |
2bccd322 | 826 | |
b7e94111 LPC |
827 | if (old != session->num_sources) { |
828 | memmove(&session->pollfds[old], &session->pollfds[old+1], | |
829 | (session->num_sources - old) * sizeof(GPollFD)); | |
830 | memmove(&session->sources[old], &session->sources[old+1], | |
831 | (session->num_sources - old) * sizeof(struct source)); | |
9f45fb3a | 832 | } |
544a4582 | 833 | |
b7e94111 LPC |
834 | new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); |
835 | if (!new_pollfds && session->num_sources > 0) { | |
a421dc1d | 836 | sr_err("%s: new_pollfds malloc failed", __func__); |
0687dfcd LPC |
837 | return SR_ERR_MALLOC; |
838 | } | |
839 | ||
b7e94111 LPC |
840 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources); |
841 | if (!new_sources && session->num_sources > 0) { | |
a421dc1d | 842 | sr_err("%s: new_sources malloc failed", __func__); |
2bccd322 | 843 | return SR_ERR_MALLOC; |
544a4582 | 844 | } |
e0508e67 | 845 | |
b7e94111 LPC |
846 | session->pollfds = new_pollfds; |
847 | session->sources = new_sources; | |
2bccd322 | 848 | |
e0508e67 | 849 | return SR_OK; |
544a4582 | 850 | } |
aac0ea25 | 851 | |
6b2d8d3e | 852 | /** |
aac0ea25 LPC |
853 | * Remove the source belonging to the specified file descriptor. |
854 | * | |
1a895c61 | 855 | * @param fd The file descriptor for which the source should be removed. |
aac0ea25 | 856 | * |
04cb9157 MH |
857 | * @retval SR_OK Success |
858 | * @retval SR_ERR_ARG Invalid argument | |
859 | * @retval SR_ERR_MALLOC Memory allocation error. | |
860 | * @retval SR_ERR_BUG Internal error. | |
47117241 UH |
861 | * |
862 | * @since 0.3.0 | |
aac0ea25 | 863 | */ |
0812c40e | 864 | SR_API int sr_session_source_remove(struct sr_session *session, int fd) |
aac0ea25 | 865 | { |
0812c40e ML |
866 | (void) session; |
867 | ||
102f1239 | 868 | return _sr_session_source_remove(session, (gintptr)fd); |
aac0ea25 LPC |
869 | } |
870 | ||
871 | /** | |
872 | * Remove the source belonging to the specified poll descriptor. | |
873 | * | |
aac0ea25 LPC |
874 | * @param pollfd The poll descriptor for which the source should be removed. |
875 | * | |
876 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
877 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
878 | * internal errors. | |
47117241 UH |
879 | * |
880 | * @since 0.2.0 | |
aac0ea25 | 881 | */ |
0812c40e ML |
882 | SR_API int sr_session_source_remove_pollfd(struct sr_session *session, |
883 | GPollFD *pollfd) | |
aac0ea25 | 884 | { |
0812c40e ML |
885 | (void) session; |
886 | ||
102f1239 | 887 | return _sr_session_source_remove(session, (gintptr)pollfd); |
aac0ea25 LPC |
888 | } |
889 | ||
6b2d8d3e | 890 | /** |
aac0ea25 LPC |
891 | * Remove the source belonging to the specified channel. |
892 | * | |
1a895c61 | 893 | * @param channel The channel for which the source should be removed. |
aac0ea25 | 894 | * |
04cb9157 MH |
895 | * @retval SR_OK Success. |
896 | * @retval SR_ERR_ARG Invalid argument. | |
897 | * @retval SR_ERR_MALLOC Memory allocation error. | |
898 | * @return SR_ERR_BUG Internal error. | |
47117241 UH |
899 | * |
900 | * @since 0.2.0 | |
aac0ea25 | 901 | */ |
0812c40e ML |
902 | SR_API int sr_session_source_remove_channel(struct sr_session *session, |
903 | GIOChannel *channel) | |
aac0ea25 | 904 | { |
0812c40e ML |
905 | (void) session; |
906 | ||
102f1239 | 907 | return _sr_session_source_remove(session, (gintptr)channel); |
aac0ea25 | 908 | } |
7b870c38 UH |
909 | |
910 | /** @} */ |