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