]>
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 | if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback)))) | |
310 | return SR_ERR_MALLOC; | |
311 | ||
312 | cb_struct->cb = cb; | |
313 | cb_struct->cb_data = cb_data; | |
314 | ||
315 | session->datafeed_callbacks = | |
316 | g_slist_append(session->datafeed_callbacks, cb_struct); | |
317 | ||
318 | return SR_OK; | |
319 | } | |
320 | ||
321 | SR_API struct sr_trigger *sr_session_trigger_get(struct sr_session *session) | |
322 | { | |
323 | return session->trigger; | |
324 | } | |
325 | ||
326 | SR_API int sr_session_trigger_set(struct sr_session *session, struct sr_trigger *trig) | |
327 | { | |
328 | session->trigger = trig; | |
329 | ||
330 | return SR_OK; | |
331 | } | |
332 | ||
333 | /** | |
334 | * Call every device in the current session's callback. | |
335 | * | |
336 | * For sessions not driven by select loops such as sr_session_run(), | |
337 | * but driven by another scheduler, this can be used to poll the devices | |
338 | * from within that scheduler. | |
339 | * | |
340 | * @param session The session to use. Must not be NULL. | |
341 | * @param block If TRUE, this call will wait for any of the session's | |
342 | * sources to fire an event on the file descriptors, or | |
343 | * any of their timeouts to activate. In other words, this | |
344 | * can be used as a select loop. | |
345 | * If FALSE, all sources have their callback run, regardless | |
346 | * of file descriptor or timeout status. | |
347 | * | |
348 | * @retval SR_OK Success. | |
349 | * @retval SR_ERR Error occured. | |
350 | */ | |
351 | static int sr_session_iteration(struct sr_session *session, gboolean block) | |
352 | { | |
353 | unsigned int i; | |
354 | int ret; | |
355 | ||
356 | ret = g_poll(session->pollfds, session->num_sources, | |
357 | block ? session->source_timeout : 0); | |
358 | for (i = 0; i < session->num_sources; i++) { | |
359 | if (session->pollfds[i].revents > 0 || (ret == 0 | |
360 | && session->source_timeout == session->sources[i].timeout)) { | |
361 | /* | |
362 | * Invoke the source's callback on an event, | |
363 | * or if the poll timed out and this source | |
364 | * asked for that timeout. | |
365 | */ | |
366 | if (!session->sources[i].cb(session->pollfds[i].fd, | |
367 | session->pollfds[i].revents, | |
368 | session->sources[i].cb_data)) | |
369 | sr_session_source_remove(session, | |
370 | session->sources[i].poll_object); | |
371 | } | |
372 | /* | |
373 | * We want to take as little time as possible to stop | |
374 | * the session if we have been told to do so. Therefore, | |
375 | * we check the flag after processing every source, not | |
376 | * just once per main event loop. | |
377 | */ | |
378 | g_mutex_lock(&session->stop_mutex); | |
379 | if (session->abort_session) { | |
380 | sr_session_stop_sync(session); | |
381 | /* But once is enough. */ | |
382 | session->abort_session = FALSE; | |
383 | } | |
384 | g_mutex_unlock(&session->stop_mutex); | |
385 | } | |
386 | ||
387 | return SR_OK; | |
388 | } | |
389 | ||
390 | ||
391 | static int verify_trigger(struct sr_trigger *trigger) | |
392 | { | |
393 | struct sr_trigger_stage *stage; | |
394 | struct sr_trigger_match *match; | |
395 | GSList *l, *m; | |
396 | ||
397 | if (!trigger->stages) { | |
398 | sr_err("No trigger stages defined."); | |
399 | return SR_ERR; | |
400 | } | |
401 | ||
402 | sr_spew("Checking trigger:"); | |
403 | for (l = trigger->stages; l; l = l->next) { | |
404 | stage = l->data; | |
405 | if (!stage->matches) { | |
406 | sr_err("Stage %d has no matches defined.", stage->stage); | |
407 | return SR_ERR; | |
408 | } | |
409 | for (m = stage->matches; m; m = m->next) { | |
410 | match = m->data; | |
411 | if (!match->channel) { | |
412 | sr_err("Stage %d match has no channel.", stage->stage); | |
413 | return SR_ERR; | |
414 | } | |
415 | if (!match->match) { | |
416 | sr_err("Stage %d match is not defined.", stage->stage); | |
417 | return SR_ERR; | |
418 | } | |
419 | sr_spew("Stage %d match on channel %s, match %d", stage->stage, | |
420 | match->channel->name, match->match); | |
421 | } | |
422 | } | |
423 | ||
424 | return SR_OK; | |
425 | } | |
426 | /** | |
427 | * Start a session. | |
428 | * | |
429 | * @param session The session to use. Must not be NULL. | |
430 | * | |
431 | * @retval SR_OK Success. | |
432 | * @retval SR_ERR_ARG Invalid session passed. | |
433 | * | |
434 | * @since 0.4.0 | |
435 | */ | |
436 | SR_API int sr_session_start(struct sr_session *session) | |
437 | { | |
438 | struct sr_dev_inst *sdi; | |
439 | struct sr_channel *ch; | |
440 | GSList *l, *c; | |
441 | int enabled_channels, ret; | |
442 | ||
443 | if (!session) { | |
444 | sr_err("%s: session was NULL", __func__); | |
445 | return SR_ERR_ARG; | |
446 | } | |
447 | ||
448 | if (!session->devs) { | |
449 | sr_err("%s: session->devs was NULL; a session " | |
450 | "cannot be started without devices.", __func__); | |
451 | return SR_ERR_ARG; | |
452 | } | |
453 | ||
454 | if (session->trigger && verify_trigger(session->trigger) != SR_OK) | |
455 | return SR_ERR; | |
456 | ||
457 | sr_info("Starting."); | |
458 | ||
459 | ret = SR_OK; | |
460 | for (l = session->devs; l; l = l->next) { | |
461 | sdi = l->data; | |
462 | enabled_channels = 0; | |
463 | for (c = sdi->channels; c; c = c->next) { | |
464 | ch = c->data; | |
465 | if (ch->enabled) { | |
466 | enabled_channels++; | |
467 | break; | |
468 | } | |
469 | } | |
470 | if (enabled_channels == 0) { | |
471 | ret = SR_ERR; | |
472 | sr_err("%s using connection %s has no enabled channels!", | |
473 | sdi->driver->name, sdi->connection_id); | |
474 | break; | |
475 | } | |
476 | ||
477 | if ((ret = sr_config_commit(sdi)) != SR_OK) { | |
478 | sr_err("Failed to commit device settings before " | |
479 | "starting acquisition (%s)", sr_strerror(ret)); | |
480 | break; | |
481 | } | |
482 | if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) { | |
483 | sr_err("%s: could not start an acquisition " | |
484 | "(%s)", __func__, sr_strerror(ret)); | |
485 | break; | |
486 | } | |
487 | } | |
488 | ||
489 | /* TODO: What if there are multiple devices? Which return code? */ | |
490 | ||
491 | return ret; | |
492 | } | |
493 | ||
494 | /** | |
495 | * Run a session. | |
496 | * | |
497 | * @param session The session to use. Must not be NULL. | |
498 | * | |
499 | * @retval SR_OK Success. | |
500 | * @retval SR_ERR_ARG Invalid session passed. | |
501 | * | |
502 | * @since 0.4.0 | |
503 | */ | |
504 | SR_API int sr_session_run(struct sr_session *session) | |
505 | { | |
506 | if (!session) { | |
507 | sr_err("%s: session was NULL", __func__); | |
508 | return SR_ERR_ARG; | |
509 | } | |
510 | ||
511 | if (!session->devs) { | |
512 | /* TODO: Actually the case? */ | |
513 | sr_err("%s: session->devs was NULL; a session " | |
514 | "cannot be run without devices.", __func__); | |
515 | return SR_ERR_ARG; | |
516 | } | |
517 | session->running = TRUE; | |
518 | ||
519 | sr_info("Running."); | |
520 | ||
521 | /* Do we have real sources? */ | |
522 | if (session->num_sources == 1 && session->pollfds[0].fd == -1) { | |
523 | /* Dummy source, freewheel over it. */ | |
524 | while (session->num_sources) | |
525 | session->sources[0].cb(-1, 0, session->sources[0].cb_data); | |
526 | } else { | |
527 | /* Real sources, use g_poll() main loop. */ | |
528 | while (session->num_sources) | |
529 | sr_session_iteration(session, TRUE); | |
530 | } | |
531 | ||
532 | return SR_OK; | |
533 | } | |
534 | ||
535 | /** | |
536 | * Stop a session. | |
537 | * | |
538 | * The session is stopped immediately, with all acquisition sessions stopped | |
539 | * and hardware drivers cleaned up. | |
540 | * | |
541 | * This must be called from within the session thread, to prevent freeing | |
542 | * resources that the session thread will try to use. | |
543 | * | |
544 | * @param session The session to use. Must not be NULL. | |
545 | * | |
546 | * @retval SR_OK Success. | |
547 | * @retval SR_ERR_ARG Invalid session passed. | |
548 | * | |
549 | * @private | |
550 | */ | |
551 | SR_PRIV int sr_session_stop_sync(struct sr_session *session) | |
552 | { | |
553 | struct sr_dev_inst *sdi; | |
554 | GSList *l; | |
555 | ||
556 | if (!session) { | |
557 | sr_err("%s: session was NULL", __func__); | |
558 | return SR_ERR_ARG; | |
559 | } | |
560 | ||
561 | sr_info("Stopping."); | |
562 | ||
563 | for (l = session->devs; l; l = l->next) { | |
564 | sdi = l->data; | |
565 | if (sdi->driver) { | |
566 | if (sdi->driver->dev_acquisition_stop) | |
567 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
568 | } | |
569 | } | |
570 | session->running = FALSE; | |
571 | ||
572 | return SR_OK; | |
573 | } | |
574 | ||
575 | /** | |
576 | * Stop a session. | |
577 | * | |
578 | * The session is stopped immediately, with all acquisition sessions being | |
579 | * stopped and hardware drivers cleaned up. | |
580 | * | |
581 | * If the session is run in a separate thread, this function will not block | |
582 | * until the session is finished executing. It is the caller's responsibility | |
583 | * to wait for the session thread to return before assuming that the session is | |
584 | * completely decommissioned. | |
585 | * | |
586 | * @param session The session to use. Must not be NULL. | |
587 | * | |
588 | * @retval SR_OK Success. | |
589 | * @retval SR_ERR_ARG Invalid session passed. | |
590 | * | |
591 | * @since 0.4.0 | |
592 | */ | |
593 | SR_API int sr_session_stop(struct sr_session *session) | |
594 | { | |
595 | if (!session) { | |
596 | sr_err("%s: session was NULL", __func__); | |
597 | return SR_ERR_BUG; | |
598 | } | |
599 | ||
600 | g_mutex_lock(&session->stop_mutex); | |
601 | session->abort_session = TRUE; | |
602 | g_mutex_unlock(&session->stop_mutex); | |
603 | ||
604 | return SR_OK; | |
605 | } | |
606 | ||
607 | /** | |
608 | * Debug helper. | |
609 | * | |
610 | * @param packet The packet to show debugging information for. | |
611 | */ | |
612 | static void datafeed_dump(const struct sr_datafeed_packet *packet) | |
613 | { | |
614 | const struct sr_datafeed_logic *logic; | |
615 | const struct sr_datafeed_analog *analog; | |
616 | const struct sr_datafeed_analog2 *analog2; | |
617 | ||
618 | switch (packet->type) { | |
619 | case SR_DF_HEADER: | |
620 | sr_dbg("bus: Received SR_DF_HEADER packet."); | |
621 | break; | |
622 | case SR_DF_TRIGGER: | |
623 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); | |
624 | break; | |
625 | case SR_DF_META: | |
626 | sr_dbg("bus: Received SR_DF_META packet."); | |
627 | break; | |
628 | case SR_DF_LOGIC: | |
629 | logic = packet->payload; | |
630 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " | |
631 | "unitsize = %d).", logic->length, logic->unitsize); | |
632 | break; | |
633 | case SR_DF_ANALOG: | |
634 | analog = packet->payload; | |
635 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", | |
636 | analog->num_samples); | |
637 | break; | |
638 | case SR_DF_ANALOG2: | |
639 | analog2 = packet->payload; | |
640 | sr_dbg("bus: Received SR_DF_ANALOG2 packet (%d samples).", | |
641 | analog2->num_samples); | |
642 | break; | |
643 | case SR_DF_END: | |
644 | sr_dbg("bus: Received SR_DF_END packet."); | |
645 | break; | |
646 | case SR_DF_FRAME_BEGIN: | |
647 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); | |
648 | break; | |
649 | case SR_DF_FRAME_END: | |
650 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); | |
651 | break; | |
652 | default: | |
653 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); | |
654 | break; | |
655 | } | |
656 | } | |
657 | ||
658 | /** | |
659 | * Send a packet to whatever is listening on the datafeed bus. | |
660 | * | |
661 | * Hardware drivers use this to send a data packet to the frontend. | |
662 | * | |
663 | * @param sdi TODO. | |
664 | * @param packet The datafeed packet to send to the session bus. | |
665 | * | |
666 | * @retval SR_OK Success. | |
667 | * @retval SR_ERR_ARG Invalid argument. | |
668 | * | |
669 | * @private | |
670 | */ | |
671 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, | |
672 | const struct sr_datafeed_packet *packet) | |
673 | { | |
674 | GSList *l; | |
675 | struct datafeed_callback *cb_struct; | |
676 | ||
677 | if (!sdi) { | |
678 | sr_err("%s: sdi was NULL", __func__); | |
679 | return SR_ERR_ARG; | |
680 | } | |
681 | ||
682 | if (!packet) { | |
683 | sr_err("%s: packet was NULL", __func__); | |
684 | return SR_ERR_ARG; | |
685 | } | |
686 | ||
687 | if (!sdi->session) { | |
688 | sr_err("%s: session was NULL", __func__); | |
689 | return SR_ERR_BUG; | |
690 | } | |
691 | ||
692 | for (l = sdi->session->datafeed_callbacks; l; l = l->next) { | |
693 | if (sr_log_loglevel_get() >= SR_LOG_DBG) | |
694 | datafeed_dump(packet); | |
695 | cb_struct = l->data; | |
696 | cb_struct->cb(sdi, packet, cb_struct->cb_data); | |
697 | } | |
698 | ||
699 | return SR_OK; | |
700 | } | |
701 | ||
702 | /** | |
703 | * Add an event source for a file descriptor. | |
704 | * | |
705 | * @param session The session to use. Must not be NULL. | |
706 | * @param pollfd The GPollFD. | |
707 | * @param[in] timeout Max time to wait before the callback is called, | |
708 | * ignored if 0. | |
709 | * @param cb Callback function to add. Must not be NULL. | |
710 | * @param cb_data Data for the callback function. Can be NULL. | |
711 | * @param poll_object TODO. | |
712 | * | |
713 | * @retval SR_OK Success. | |
714 | * @retval SR_ERR_ARG Invalid argument. | |
715 | * @retval SR_ERR_MALLOC Memory allocation error. | |
716 | */ | |
717 | static int _sr_session_source_add(struct sr_session *session, GPollFD *pollfd, | |
718 | int timeout, sr_receive_data_callback cb, void *cb_data, gintptr poll_object) | |
719 | { | |
720 | struct source *new_sources, *s; | |
721 | GPollFD *new_pollfds; | |
722 | ||
723 | if (!cb) { | |
724 | sr_err("%s: cb was NULL", __func__); | |
725 | return SR_ERR_ARG; | |
726 | } | |
727 | ||
728 | /* Note: cb_data can be NULL, that's not a bug. */ | |
729 | ||
730 | new_pollfds = g_try_realloc(session->pollfds, | |
731 | sizeof(GPollFD) * (session->num_sources + 1)); | |
732 | if (!new_pollfds) { | |
733 | sr_err("%s: new_pollfds malloc failed", __func__); | |
734 | return SR_ERR_MALLOC; | |
735 | } | |
736 | ||
737 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * | |
738 | (session->num_sources + 1)); | |
739 | if (!new_sources) { | |
740 | sr_err("%s: new_sources malloc failed", __func__); | |
741 | return SR_ERR_MALLOC; | |
742 | } | |
743 | ||
744 | new_pollfds[session->num_sources] = *pollfd; | |
745 | s = &new_sources[session->num_sources++]; | |
746 | s->timeout = timeout; | |
747 | s->cb = cb; | |
748 | s->cb_data = cb_data; | |
749 | s->poll_object = poll_object; | |
750 | session->pollfds = new_pollfds; | |
751 | session->sources = new_sources; | |
752 | ||
753 | if (timeout != session->source_timeout && timeout > 0 | |
754 | && (session->source_timeout == -1 || timeout < session->source_timeout)) | |
755 | session->source_timeout = timeout; | |
756 | ||
757 | return SR_OK; | |
758 | } | |
759 | ||
760 | /** | |
761 | * Add an event source for a file descriptor. | |
762 | * | |
763 | * @param session The session to use. Must not be NULL. | |
764 | * @param fd The file descriptor. | |
765 | * @param events Events to check for. | |
766 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
767 | * @param cb Callback function to add. Must not be NULL. | |
768 | * @param cb_data Data for the callback function. Can be NULL. | |
769 | * | |
770 | * @retval SR_OK Success. | |
771 | * @retval SR_ERR_ARG Invalid argument. | |
772 | * @retval SR_ERR_MALLOC Memory allocation error. | |
773 | * | |
774 | * @since 0.3.0 | |
775 | */ | |
776 | SR_API int sr_session_source_add(struct sr_session *session, int fd, | |
777 | int events, int timeout, sr_receive_data_callback cb, void *cb_data) | |
778 | { | |
779 | GPollFD p; | |
780 | ||
781 | p.fd = fd; | |
782 | p.events = events; | |
783 | ||
784 | return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)fd); | |
785 | } | |
786 | ||
787 | /** | |
788 | * Add an event source for a GPollFD. | |
789 | * | |
790 | * @param session The session to use. Must not be NULL. | |
791 | * @param pollfd The GPollFD. | |
792 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
793 | * @param cb Callback function to add. Must not be NULL. | |
794 | * @param cb_data Data for the callback function. Can be NULL. | |
795 | * | |
796 | * @retval SR_OK Success. | |
797 | * @retval SR_ERR_ARG Invalid argument. | |
798 | * @retval SR_ERR_MALLOC Memory allocation error. | |
799 | * | |
800 | * @since 0.3.0 | |
801 | */ | |
802 | SR_API int sr_session_source_add_pollfd(struct sr_session *session, | |
803 | GPollFD *pollfd, int timeout, sr_receive_data_callback cb, | |
804 | void *cb_data) | |
805 | { | |
806 | return _sr_session_source_add(session, pollfd, timeout, cb, | |
807 | cb_data, (gintptr)pollfd); | |
808 | } | |
809 | ||
810 | /** | |
811 | * Add an event source for a GIOChannel. | |
812 | * | |
813 | * @param session The session to use. Must not be NULL. | |
814 | * @param channel The GIOChannel. | |
815 | * @param events Events to poll on. | |
816 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
817 | * @param cb Callback function to add. Must not be NULL. | |
818 | * @param cb_data Data for the callback function. Can be NULL. | |
819 | * | |
820 | * @retval SR_OK Success. | |
821 | * @retval SR_ERR_ARG Invalid argument. | |
822 | * @retval SR_ERR_MALLOC Memory allocation error. | |
823 | * | |
824 | * @since 0.3.0 | |
825 | */ | |
826 | SR_API int sr_session_source_add_channel(struct sr_session *session, | |
827 | GIOChannel *channel, int events, int timeout, | |
828 | sr_receive_data_callback cb, void *cb_data) | |
829 | { | |
830 | GPollFD p; | |
831 | ||
832 | #ifdef _WIN32 | |
833 | g_io_channel_win32_make_pollfd(channel, events, &p); | |
834 | #else | |
835 | p.fd = g_io_channel_unix_get_fd(channel); | |
836 | p.events = events; | |
837 | #endif | |
838 | ||
839 | return _sr_session_source_add(session, &p, timeout, cb, cb_data, (gintptr)channel); | |
840 | } | |
841 | ||
842 | /** | |
843 | * Remove the source belonging to the specified channel. | |
844 | * | |
845 | * @todo Add more error checks and logging. | |
846 | * | |
847 | * @param session The session to use. Must not be NULL. | |
848 | * @param poll_object The channel for which the source should be removed. | |
849 | * | |
850 | * @retval SR_OK Success | |
851 | * @retval SR_ERR_ARG Invalid arguments | |
852 | * @retval SR_ERR_MALLOC Memory allocation error | |
853 | * @retval SR_ERR_BUG Internal error | |
854 | */ | |
855 | static int _sr_session_source_remove(struct sr_session *session, gintptr poll_object) | |
856 | { | |
857 | struct source *new_sources; | |
858 | GPollFD *new_pollfds; | |
859 | unsigned int old; | |
860 | ||
861 | if (!session->sources || !session->num_sources) { | |
862 | sr_err("%s: sources was NULL", __func__); | |
863 | return SR_ERR_BUG; | |
864 | } | |
865 | ||
866 | for (old = 0; old < session->num_sources; old++) { | |
867 | if (session->sources[old].poll_object == poll_object) | |
868 | break; | |
869 | } | |
870 | ||
871 | /* fd not found, nothing to do */ | |
872 | if (old == session->num_sources) | |
873 | return SR_OK; | |
874 | ||
875 | session->num_sources -= 1; | |
876 | ||
877 | if (old != session->num_sources) { | |
878 | memmove(&session->pollfds[old], &session->pollfds[old+1], | |
879 | (session->num_sources - old) * sizeof(GPollFD)); | |
880 | memmove(&session->sources[old], &session->sources[old+1], | |
881 | (session->num_sources - old) * sizeof(struct source)); | |
882 | } | |
883 | ||
884 | new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); | |
885 | if (!new_pollfds && session->num_sources > 0) { | |
886 | sr_err("%s: new_pollfds malloc failed", __func__); | |
887 | return SR_ERR_MALLOC; | |
888 | } | |
889 | ||
890 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources); | |
891 | if (!new_sources && session->num_sources > 0) { | |
892 | sr_err("%s: new_sources malloc failed", __func__); | |
893 | return SR_ERR_MALLOC; | |
894 | } | |
895 | ||
896 | session->pollfds = new_pollfds; | |
897 | session->sources = new_sources; | |
898 | ||
899 | return SR_OK; | |
900 | } | |
901 | ||
902 | /** | |
903 | * Remove the source belonging to the specified file descriptor. | |
904 | * | |
905 | * @param session The session to use. Must not be NULL. | |
906 | * @param fd The file descriptor for which the source should be removed. | |
907 | * | |
908 | * @retval SR_OK Success | |
909 | * @retval SR_ERR_ARG Invalid argument | |
910 | * @retval SR_ERR_MALLOC Memory allocation error. | |
911 | * @retval SR_ERR_BUG Internal error. | |
912 | * | |
913 | * @since 0.3.0 | |
914 | */ | |
915 | SR_API int sr_session_source_remove(struct sr_session *session, int fd) | |
916 | { | |
917 | return _sr_session_source_remove(session, (gintptr)fd); | |
918 | } | |
919 | ||
920 | /** | |
921 | * Remove the source belonging to the specified poll descriptor. | |
922 | * | |
923 | * @param session The session to use. Must not be NULL. | |
924 | * @param pollfd The poll descriptor for which the source should be removed. | |
925 | * | |
926 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
927 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
928 | * internal errors. | |
929 | * | |
930 | * @since 0.2.0 | |
931 | */ | |
932 | SR_API int sr_session_source_remove_pollfd(struct sr_session *session, | |
933 | GPollFD *pollfd) | |
934 | { | |
935 | return _sr_session_source_remove(session, (gintptr)pollfd); | |
936 | } | |
937 | ||
938 | /** | |
939 | * Remove the source belonging to the specified channel. | |
940 | * | |
941 | * @param session The session to use. Must not be NULL. | |
942 | * @param channel The channel for which the source should be removed. | |
943 | * | |
944 | * @retval SR_OK Success. | |
945 | * @retval SR_ERR_ARG Invalid argument. | |
946 | * @retval SR_ERR_MALLOC Memory allocation error. | |
947 | * @return SR_ERR_BUG Internal error. | |
948 | * | |
949 | * @since 0.2.0 | |
950 | */ | |
951 | SR_API int sr_session_source_remove_channel(struct sr_session *session, | |
952 | GIOChannel *channel) | |
953 | { | |
954 | return _sr_session_source_remove(session, (gintptr)channel); | |
955 | } | |
956 | ||
957 | static void *copy_src(struct sr_config *src) | |
958 | { | |
959 | struct sr_config *new_src; | |
960 | ||
961 | new_src = g_malloc(sizeof(struct sr_config)); | |
962 | memcpy(new_src, src, sizeof(struct sr_config)); | |
963 | g_variant_ref(src->data); | |
964 | ||
965 | return new_src; | |
966 | } | |
967 | ||
968 | SR_PRIV int sr_packet_copy(const struct sr_datafeed_packet *packet, | |
969 | struct sr_datafeed_packet **copy) | |
970 | { | |
971 | const struct sr_datafeed_meta *meta; | |
972 | struct sr_datafeed_meta *meta_copy; | |
973 | const struct sr_datafeed_logic *logic; | |
974 | struct sr_datafeed_logic *logic_copy; | |
975 | const struct sr_datafeed_analog *analog; | |
976 | struct sr_datafeed_analog *analog_copy; | |
977 | uint8_t *payload; | |
978 | ||
979 | *copy = g_malloc0(sizeof(struct sr_datafeed_packet)); | |
980 | (*copy)->type = packet->type; | |
981 | ||
982 | switch (packet->type) { | |
983 | case SR_DF_TRIGGER: | |
984 | case SR_DF_END: | |
985 | /* No payload. */ | |
986 | break; | |
987 | case SR_DF_HEADER: | |
988 | payload = g_malloc(sizeof(struct sr_datafeed_header)); | |
989 | memcpy(payload, packet->payload, sizeof(struct sr_datafeed_header)); | |
990 | (*copy)->payload = payload; | |
991 | break; | |
992 | case SR_DF_META: | |
993 | meta = packet->payload; | |
994 | meta_copy = g_malloc(sizeof(struct sr_datafeed_meta)); | |
995 | meta_copy->config = g_slist_copy_deep(meta->config, | |
996 | (GCopyFunc)copy_src, NULL); | |
997 | (*copy)->payload = meta_copy; | |
998 | break; | |
999 | case SR_DF_LOGIC: | |
1000 | logic = packet->payload; | |
1001 | logic_copy = g_malloc(sizeof(logic)); | |
1002 | logic_copy->length = logic->length; | |
1003 | logic_copy->unitsize = logic->unitsize; | |
1004 | memcpy(logic_copy->data, logic->data, logic->length * logic->unitsize); | |
1005 | (*copy)->payload = logic_copy; | |
1006 | break; | |
1007 | case SR_DF_ANALOG: | |
1008 | analog = packet->payload; | |
1009 | analog_copy = g_malloc(sizeof(analog)); | |
1010 | analog_copy->channels = g_slist_copy(analog->channels); | |
1011 | analog_copy->num_samples = analog->num_samples; | |
1012 | analog_copy->mq = analog->mq; | |
1013 | analog_copy->unit = analog->unit; | |
1014 | analog_copy->mqflags = analog->mqflags; | |
1015 | memcpy(analog_copy->data, analog->data, | |
1016 | analog->num_samples * sizeof(float)); | |
1017 | (*copy)->payload = analog_copy; | |
1018 | break; | |
1019 | default: | |
1020 | sr_err("Unknown packet type %d", packet->type); | |
1021 | return SR_ERR; | |
1022 | } | |
1023 | ||
1024 | return SR_OK; | |
1025 | } | |
1026 | ||
1027 | void sr_packet_free(struct sr_datafeed_packet *packet) | |
1028 | { | |
1029 | const struct sr_datafeed_meta *meta; | |
1030 | const struct sr_datafeed_logic *logic; | |
1031 | const struct sr_datafeed_analog *analog; | |
1032 | struct sr_config *src; | |
1033 | GSList *l; | |
1034 | ||
1035 | switch (packet->type) { | |
1036 | case SR_DF_TRIGGER: | |
1037 | case SR_DF_END: | |
1038 | /* No payload. */ | |
1039 | break; | |
1040 | case SR_DF_HEADER: | |
1041 | /* Payload is a simple struct. */ | |
1042 | g_free((void *)packet->payload); | |
1043 | break; | |
1044 | case SR_DF_META: | |
1045 | meta = packet->payload; | |
1046 | for (l = meta->config; l; l = l->next) { | |
1047 | src = l->data; | |
1048 | g_variant_unref(src->data); | |
1049 | g_free(src); | |
1050 | } | |
1051 | g_slist_free(meta->config); | |
1052 | g_free((void *)packet->payload); | |
1053 | break; | |
1054 | case SR_DF_LOGIC: | |
1055 | logic = packet->payload; | |
1056 | g_free(logic->data); | |
1057 | g_free((void *)packet->payload); | |
1058 | break; | |
1059 | case SR_DF_ANALOG: | |
1060 | analog = packet->payload; | |
1061 | g_slist_free(analog->channels); | |
1062 | g_free(analog->data); | |
1063 | g_free((void *)packet->payload); | |
1064 | break; | |
1065 | default: | |
1066 | sr_err("Unknown packet type %d", packet->type); | |
1067 | } | |
1068 | g_free(packet); | |
1069 | ||
1070 | } | |
1071 | ||
1072 | /** @} */ |