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