]>
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 | /* There can only be one session at a time. */ | |
63 | /* 'session' is not static, it's used elsewhere (via 'extern'). */ | |
64 | struct sr_session *session; | |
65 | ||
66 | /** | |
67 | * Create a new session. | |
68 | * | |
69 | * @todo Should it use the file-global "session" variable or take an argument? | |
70 | * The same question applies to all the other session functions. | |
71 | * | |
72 | * @retval NULL Error. | |
73 | * @retval other A pointer to the newly allocated session. | |
74 | */ | |
75 | SR_API struct sr_session *sr_session_new(void) | |
76 | { | |
77 | if (!(session = g_try_malloc0(sizeof(struct sr_session)))) { | |
78 | sr_err("Session malloc failed."); | |
79 | return NULL; | |
80 | } | |
81 | ||
82 | session->source_timeout = -1; | |
83 | session->running = FALSE; | |
84 | session->abort_session = FALSE; | |
85 | g_mutex_init(&session->stop_mutex); | |
86 | ||
87 | return session; | |
88 | } | |
89 | ||
90 | /** | |
91 | * Destroy the current session. | |
92 | * This frees up all memory used by the session. | |
93 | * | |
94 | * @retval SR_OK Success. | |
95 | * @retval SR_ERR_BUG No session exists. | |
96 | */ | |
97 | SR_API int sr_session_destroy(void) | |
98 | { | |
99 | if (!session) { | |
100 | sr_err("%s: session was NULL", __func__); | |
101 | return SR_ERR_BUG; | |
102 | } | |
103 | ||
104 | sr_session_dev_remove_all(); | |
105 | ||
106 | /* TODO: Error checks needed? */ | |
107 | ||
108 | g_mutex_clear(&session->stop_mutex); | |
109 | ||
110 | g_free(session); | |
111 | session = NULL; | |
112 | ||
113 | return SR_OK; | |
114 | } | |
115 | ||
116 | /** | |
117 | * Remove all the devices from the current session. | |
118 | * | |
119 | * The session itself (i.e., the struct sr_session) is not free'd and still | |
120 | * exists after this function returns. | |
121 | * | |
122 | * @retval SR_OK Success. | |
123 | * @retval SR_ERR_BUG No session exists. | |
124 | */ | |
125 | SR_API int sr_session_dev_remove_all(void) | |
126 | { | |
127 | if (!session) { | |
128 | sr_err("%s: session was NULL", __func__); | |
129 | return SR_ERR_BUG; | |
130 | } | |
131 | ||
132 | g_slist_free(session->devs); | |
133 | session->devs = NULL; | |
134 | ||
135 | return SR_OK; | |
136 | } | |
137 | ||
138 | /** | |
139 | * Add a device instance to the current session. | |
140 | * | |
141 | * @param sdi The device instance to add to the current session. Must not | |
142 | * be NULL. Also, sdi->driver and sdi->driver->dev_open must | |
143 | * not be NULL. | |
144 | * | |
145 | * @retval SR_OK Success. | |
146 | * @retval SR_ERR_ARG Invalid argument. | |
147 | * @retval SR_ERR_BUG No session exists. | |
148 | */ | |
149 | SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi) | |
150 | { | |
151 | int ret; | |
152 | ||
153 | if (!sdi) { | |
154 | sr_err("%s: sdi was NULL", __func__); | |
155 | return SR_ERR_ARG; | |
156 | } | |
157 | ||
158 | if (!session) { | |
159 | sr_err("%s: session was NULL", __func__); | |
160 | return SR_ERR_BUG; | |
161 | } | |
162 | ||
163 | /* If sdi->driver is NULL, this is a virtual device. */ | |
164 | if (!sdi->driver) { | |
165 | sr_dbg("%s: sdi->driver was NULL, this seems to be " | |
166 | "a virtual device; continuing", __func__); | |
167 | /* Just add the device, don't run dev_open(). */ | |
168 | session->devs = g_slist_append(session->devs, (gpointer)sdi); | |
169 | return SR_OK; | |
170 | } | |
171 | ||
172 | /* sdi->driver is non-NULL (i.e. we have a real device). */ | |
173 | if (!sdi->driver->dev_open) { | |
174 | sr_err("%s: sdi->driver->dev_open was NULL", __func__); | |
175 | return SR_ERR_BUG; | |
176 | } | |
177 | ||
178 | session->devs = g_slist_append(session->devs, (gpointer)sdi); | |
179 | ||
180 | if (session->running) { | |
181 | /* Adding a device to a running session. Commit settings | |
182 | * and start acquisition on that device now. */ | |
183 | if ((ret = sr_config_commit(sdi)) != SR_OK) { | |
184 | sr_err("Failed to commit device settings before " | |
185 | "starting acquisition in running session (%s)", | |
186 | sr_strerror(ret)); | |
187 | return ret; | |
188 | } | |
189 | if ((ret = sdi->driver->dev_acquisition_start(sdi, | |
190 | (void *)sdi)) != SR_OK) { | |
191 | sr_err("Failed to start acquisition of device in " | |
192 | "running session (%s)", sr_strerror(ret)); | |
193 | return ret; | |
194 | } | |
195 | } | |
196 | ||
197 | return SR_OK; | |
198 | } | |
199 | ||
200 | /** | |
201 | * List all device instances attached to the current session. | |
202 | * | |
203 | * @param devlist A pointer where the device instance list will be | |
204 | * stored on return. If no devices are in the session, | |
205 | * this will be NULL. Each element in the list points | |
206 | * to a struct sr_dev_inst *. | |
207 | * The list must be freed by the caller, but not the | |
208 | * elements pointed to. | |
209 | * | |
210 | * @retval SR_OK Success. | |
211 | * @retval SR_ERR Invalid argument. | |
212 | */ | |
213 | SR_API int sr_session_dev_list(GSList **devlist) | |
214 | { | |
215 | ||
216 | *devlist = NULL; | |
217 | ||
218 | if (!session) | |
219 | return SR_ERR; | |
220 | ||
221 | *devlist = g_slist_copy(session->devs); | |
222 | ||
223 | return SR_OK; | |
224 | } | |
225 | ||
226 | /** | |
227 | * Remove all datafeed callbacks in the current session. | |
228 | * | |
229 | * @retval SR_OK Success. | |
230 | * @retval SR_ERR_BUG No session exists. | |
231 | */ | |
232 | SR_API int sr_session_datafeed_callback_remove_all(void) | |
233 | { | |
234 | if (!session) { | |
235 | sr_err("%s: session was NULL", __func__); | |
236 | return SR_ERR_BUG; | |
237 | } | |
238 | ||
239 | g_slist_free_full(session->datafeed_callbacks, g_free); | |
240 | session->datafeed_callbacks = NULL; | |
241 | ||
242 | return SR_OK; | |
243 | } | |
244 | ||
245 | /** | |
246 | * Add a datafeed callback to the current session. | |
247 | * | |
248 | * @param cb Function to call when a chunk of data is received. | |
249 | * Must not be NULL. | |
250 | * @param cb_data Opaque pointer passed in by the caller. | |
251 | * | |
252 | * @retval SR_OK Success. | |
253 | * @retval SR_ERR_BUG No session exists. | |
254 | */ | |
255 | SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback cb, void *cb_data) | |
256 | { | |
257 | struct datafeed_callback *cb_struct; | |
258 | ||
259 | if (!session) { | |
260 | sr_err("%s: session was NULL", __func__); | |
261 | return SR_ERR_BUG; | |
262 | } | |
263 | ||
264 | if (!cb) { | |
265 | sr_err("%s: cb was NULL", __func__); | |
266 | return SR_ERR_ARG; | |
267 | } | |
268 | ||
269 | if (!(cb_struct = g_try_malloc0(sizeof(struct datafeed_callback)))) | |
270 | return SR_ERR_MALLOC; | |
271 | ||
272 | cb_struct->cb = cb; | |
273 | cb_struct->cb_data = cb_data; | |
274 | ||
275 | session->datafeed_callbacks = | |
276 | g_slist_append(session->datafeed_callbacks, cb_struct); | |
277 | ||
278 | return SR_OK; | |
279 | } | |
280 | ||
281 | /** | |
282 | * Call every device in the session's callback. | |
283 | * | |
284 | * For sessions not driven by select loops such as sr_session_run(), | |
285 | * but driven by another scheduler, this can be used to poll the devices | |
286 | * from within that scheduler. | |
287 | * | |
288 | * @param block If TRUE, this call will wait for any of the session's | |
289 | * sources to fire an event on the file descriptors, or | |
290 | * any of their timeouts to activate. In other words, this | |
291 | * can be used as a select loop. | |
292 | * If FALSE, all sources have their callback run, regardless | |
293 | * of file descriptor or timeout status. | |
294 | * | |
295 | * @retval SR_OK Success. | |
296 | * @retval SR_ERR Error occured. | |
297 | */ | |
298 | static int sr_session_iteration(gboolean block) | |
299 | { | |
300 | unsigned int i; | |
301 | int ret; | |
302 | ||
303 | ret = g_poll(session->pollfds, session->num_sources, | |
304 | block ? session->source_timeout : 0); | |
305 | for (i = 0; i < session->num_sources; i++) { | |
306 | if (session->pollfds[i].revents > 0 || (ret == 0 | |
307 | && session->source_timeout == session->sources[i].timeout)) { | |
308 | /* | |
309 | * Invoke the source's callback on an event, | |
310 | * or if the poll timed out and this source | |
311 | * asked for that timeout. | |
312 | */ | |
313 | if (!session->sources[i].cb(session->pollfds[i].fd, | |
314 | session->pollfds[i].revents, | |
315 | session->sources[i].cb_data)) | |
316 | sr_session_source_remove(session->sources[i].poll_object); | |
317 | } | |
318 | /* | |
319 | * We want to take as little time as possible to stop | |
320 | * the session if we have been told to do so. Therefore, | |
321 | * we check the flag after processing every source, not | |
322 | * just once per main event loop. | |
323 | */ | |
324 | g_mutex_lock(&session->stop_mutex); | |
325 | if (session->abort_session) { | |
326 | sr_session_stop_sync(); | |
327 | /* But once is enough. */ | |
328 | session->abort_session = FALSE; | |
329 | } | |
330 | g_mutex_unlock(&session->stop_mutex); | |
331 | } | |
332 | ||
333 | return SR_OK; | |
334 | } | |
335 | ||
336 | /** | |
337 | * Start a session. | |
338 | * | |
339 | * There can only be one session at a time. | |
340 | * | |
341 | * @retval SR_OK Success. | |
342 | * @retval SR_ERR Error occured. | |
343 | */ | |
344 | SR_API int sr_session_start(void) | |
345 | { | |
346 | struct sr_dev_inst *sdi; | |
347 | GSList *l; | |
348 | int ret; | |
349 | ||
350 | if (!session) { | |
351 | sr_err("%s: session was NULL; a session must be " | |
352 | "created before starting it.", __func__); | |
353 | return SR_ERR_BUG; | |
354 | } | |
355 | ||
356 | if (!session->devs) { | |
357 | sr_err("%s: session->devs was NULL; a session " | |
358 | "cannot be started without devices.", __func__); | |
359 | return SR_ERR_BUG; | |
360 | } | |
361 | ||
362 | sr_info("Starting."); | |
363 | ||
364 | ret = SR_OK; | |
365 | for (l = session->devs; l; l = l->next) { | |
366 | sdi = l->data; | |
367 | if ((ret = sr_config_commit(sdi)) != SR_OK) { | |
368 | sr_err("Failed to commit device settings before " | |
369 | "starting acquisition (%s)", sr_strerror(ret)); | |
370 | break; | |
371 | } | |
372 | if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) { | |
373 | sr_err("%s: could not start an acquisition " | |
374 | "(%s)", __func__, sr_strerror(ret)); | |
375 | break; | |
376 | } | |
377 | } | |
378 | ||
379 | /* TODO: What if there are multiple devices? Which return code? */ | |
380 | ||
381 | return ret; | |
382 | } | |
383 | ||
384 | /** | |
385 | * Run the session. | |
386 | * | |
387 | * @retval SR_OK Success. | |
388 | * @retval SR_ERR_BUG Error occured. | |
389 | */ | |
390 | SR_API int sr_session_run(void) | |
391 | { | |
392 | if (!session) { | |
393 | sr_err("%s: session was NULL; a session must be " | |
394 | "created first, before running it.", __func__); | |
395 | return SR_ERR_BUG; | |
396 | } | |
397 | ||
398 | if (!session->devs) { | |
399 | /* TODO: Actually the case? */ | |
400 | sr_err("%s: session->devs was NULL; a session " | |
401 | "cannot be run without devices.", __func__); | |
402 | return SR_ERR_BUG; | |
403 | } | |
404 | session->running = TRUE; | |
405 | ||
406 | sr_info("Running."); | |
407 | ||
408 | /* Do we have real sources? */ | |
409 | if (session->num_sources == 1 && session->pollfds[0].fd == -1) { | |
410 | /* Dummy source, freewheel over it. */ | |
411 | while (session->num_sources) | |
412 | session->sources[0].cb(-1, 0, session->sources[0].cb_data); | |
413 | } else { | |
414 | /* Real sources, use g_poll() main loop. */ | |
415 | while (session->num_sources) | |
416 | sr_session_iteration(TRUE); | |
417 | } | |
418 | ||
419 | return SR_OK; | |
420 | } | |
421 | ||
422 | /** | |
423 | * Stop the current session. | |
424 | * | |
425 | * The current session is stopped immediately, with all acquisition sessions | |
426 | * being stopped and hardware drivers cleaned up. | |
427 | * | |
428 | * This must be called from within the session thread, to prevent freeing | |
429 | * resources that the session thread will try to use. | |
430 | * | |
431 | * @retval SR_OK Success. | |
432 | * @retval SR_ERR_BUG No session exists. | |
433 | * | |
434 | * @private | |
435 | */ | |
436 | SR_PRIV int sr_session_stop_sync(void) | |
437 | { | |
438 | struct sr_dev_inst *sdi; | |
439 | GSList *l; | |
440 | ||
441 | if (!session) { | |
442 | sr_err("%s: session was NULL", __func__); | |
443 | return SR_ERR_BUG; | |
444 | } | |
445 | ||
446 | sr_info("Stopping."); | |
447 | ||
448 | for (l = session->devs; l; l = l->next) { | |
449 | sdi = l->data; | |
450 | if (sdi->driver) { | |
451 | if (sdi->driver->dev_acquisition_stop) | |
452 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
453 | } | |
454 | } | |
455 | session->running = FALSE; | |
456 | ||
457 | return SR_OK; | |
458 | } | |
459 | ||
460 | /** | |
461 | * Stop the current session. | |
462 | * | |
463 | * The current session is stopped immediately, with all acquisition sessions | |
464 | * being stopped and hardware drivers cleaned up. | |
465 | * | |
466 | * If the session is run in a separate thread, this function will not block | |
467 | * until the session is finished executing. It is the caller's responsibility | |
468 | * to wait for the session thread to return before assuming that the session is | |
469 | * completely decommissioned. | |
470 | * | |
471 | * @retval SR_OK Success. | |
472 | * @retval SR_ERR_BUG No session exists. | |
473 | */ | |
474 | SR_API int sr_session_stop(void) | |
475 | { | |
476 | if (!session) { | |
477 | sr_err("%s: session was NULL", __func__); | |
478 | return SR_ERR_BUG; | |
479 | } | |
480 | ||
481 | g_mutex_lock(&session->stop_mutex); | |
482 | session->abort_session = TRUE; | |
483 | g_mutex_unlock(&session->stop_mutex); | |
484 | ||
485 | return SR_OK; | |
486 | } | |
487 | ||
488 | /** | |
489 | * Debug helper. | |
490 | * | |
491 | * @param packet The packet to show debugging information for. | |
492 | */ | |
493 | static void datafeed_dump(const struct sr_datafeed_packet *packet) | |
494 | { | |
495 | const struct sr_datafeed_logic *logic; | |
496 | const struct sr_datafeed_analog *analog; | |
497 | ||
498 | switch (packet->type) { | |
499 | case SR_DF_HEADER: | |
500 | sr_dbg("bus: Received SR_DF_HEADER packet."); | |
501 | break; | |
502 | case SR_DF_TRIGGER: | |
503 | sr_dbg("bus: Received SR_DF_TRIGGER packet."); | |
504 | break; | |
505 | case SR_DF_META: | |
506 | sr_dbg("bus: Received SR_DF_META packet."); | |
507 | break; | |
508 | case SR_DF_LOGIC: | |
509 | logic = packet->payload; | |
510 | sr_dbg("bus: Received SR_DF_LOGIC packet (%" PRIu64 " bytes, " | |
511 | "unitsize = %d).", logic->length, logic->unitsize); | |
512 | break; | |
513 | case SR_DF_ANALOG: | |
514 | analog = packet->payload; | |
515 | sr_dbg("bus: Received SR_DF_ANALOG packet (%d samples).", | |
516 | analog->num_samples); | |
517 | break; | |
518 | case SR_DF_END: | |
519 | sr_dbg("bus: Received SR_DF_END packet."); | |
520 | break; | |
521 | case SR_DF_FRAME_BEGIN: | |
522 | sr_dbg("bus: Received SR_DF_FRAME_BEGIN packet."); | |
523 | break; | |
524 | case SR_DF_FRAME_END: | |
525 | sr_dbg("bus: Received SR_DF_FRAME_END packet."); | |
526 | break; | |
527 | default: | |
528 | sr_dbg("bus: Received unknown packet type: %d.", packet->type); | |
529 | break; | |
530 | } | |
531 | } | |
532 | ||
533 | /** | |
534 | * Send a packet to whatever is listening on the datafeed bus. | |
535 | * | |
536 | * Hardware drivers use this to send a data packet to the frontend. | |
537 | * | |
538 | * @param sdi TODO. | |
539 | * @param packet The datafeed packet to send to the session bus. | |
540 | * | |
541 | * @retval SR_OK Success. | |
542 | * @retval SR_ERR_ARG Invalid argument. | |
543 | * | |
544 | * @private | |
545 | */ | |
546 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, | |
547 | const struct sr_datafeed_packet *packet) | |
548 | { | |
549 | GSList *l; | |
550 | struct datafeed_callback *cb_struct; | |
551 | ||
552 | if (!sdi) { | |
553 | sr_err("%s: sdi was NULL", __func__); | |
554 | return SR_ERR_ARG; | |
555 | } | |
556 | ||
557 | if (!packet) { | |
558 | sr_err("%s: packet was NULL", __func__); | |
559 | return SR_ERR_ARG; | |
560 | } | |
561 | ||
562 | for (l = session->datafeed_callbacks; l; l = l->next) { | |
563 | if (sr_log_loglevel_get() >= SR_LOG_DBG) | |
564 | datafeed_dump(packet); | |
565 | cb_struct = l->data; | |
566 | cb_struct->cb(sdi, packet, cb_struct->cb_data); | |
567 | } | |
568 | ||
569 | return SR_OK; | |
570 | } | |
571 | ||
572 | /** | |
573 | * Add an event source for a file descriptor. | |
574 | * | |
575 | * @param pollfd The GPollFD. | |
576 | * @param[in] timeout Max time to wait before the callback is called, | |
577 | * ignored if 0. | |
578 | * @param cb Callback function to add. Must not be NULL. | |
579 | * @param cb_data Data for the callback function. Can be NULL. | |
580 | * @param poll_object TODO. | |
581 | * | |
582 | * @retval SR_OK Success. | |
583 | * @retval SR_ERR_ARG Invalid argument. | |
584 | * @retval SR_ERR_MALLOC Memory allocation error. | |
585 | */ | |
586 | static int _sr_session_source_add(GPollFD *pollfd, int timeout, | |
587 | sr_receive_data_callback cb, void *cb_data, gintptr poll_object) | |
588 | { | |
589 | struct source *new_sources, *s; | |
590 | GPollFD *new_pollfds; | |
591 | ||
592 | if (!cb) { | |
593 | sr_err("%s: cb was NULL", __func__); | |
594 | return SR_ERR_ARG; | |
595 | } | |
596 | ||
597 | /* Note: cb_data can be NULL, that's not a bug. */ | |
598 | ||
599 | new_pollfds = g_try_realloc(session->pollfds, | |
600 | sizeof(GPollFD) * (session->num_sources + 1)); | |
601 | if (!new_pollfds) { | |
602 | sr_err("%s: new_pollfds malloc failed", __func__); | |
603 | return SR_ERR_MALLOC; | |
604 | } | |
605 | ||
606 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * | |
607 | (session->num_sources + 1)); | |
608 | if (!new_sources) { | |
609 | sr_err("%s: new_sources malloc failed", __func__); | |
610 | return SR_ERR_MALLOC; | |
611 | } | |
612 | ||
613 | new_pollfds[session->num_sources] = *pollfd; | |
614 | s = &new_sources[session->num_sources++]; | |
615 | s->timeout = timeout; | |
616 | s->cb = cb; | |
617 | s->cb_data = cb_data; | |
618 | s->poll_object = poll_object; | |
619 | session->pollfds = new_pollfds; | |
620 | session->sources = new_sources; | |
621 | ||
622 | if (timeout != session->source_timeout && timeout > 0 | |
623 | && (session->source_timeout == -1 || timeout < session->source_timeout)) | |
624 | session->source_timeout = timeout; | |
625 | ||
626 | return SR_OK; | |
627 | } | |
628 | ||
629 | /** | |
630 | * Add an event source for a file descriptor. | |
631 | * | |
632 | * @param fd The file descriptor. | |
633 | * @param events Events to check for. | |
634 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
635 | * @param cb Callback function to add. Must not be NULL. | |
636 | * @param cb_data Data for the callback function. Can be NULL. | |
637 | * | |
638 | * @retval SR_OK Success. | |
639 | * @retval SR_ERR_ARG Invalid argument. | |
640 | * @retval SR_ERR_MALLOC Memory allocation error. | |
641 | */ | |
642 | SR_API int sr_session_source_add(int fd, int events, int timeout, | |
643 | sr_receive_data_callback cb, void *cb_data) | |
644 | { | |
645 | GPollFD p; | |
646 | ||
647 | p.fd = fd; | |
648 | p.events = events; | |
649 | ||
650 | return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd); | |
651 | } | |
652 | ||
653 | /** | |
654 | * Add an event source for a GPollFD. | |
655 | * | |
656 | * @param pollfd The GPollFD. | |
657 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
658 | * @param cb Callback function to add. Must not be NULL. | |
659 | * @param cb_data Data for the callback function. Can be NULL. | |
660 | * | |
661 | * @retval SR_OK Success. | |
662 | * @retval SR_ERR_ARG Invalid argument. | |
663 | * @retval SR_ERR_MALLOC Memory allocation error. | |
664 | */ | |
665 | SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout, | |
666 | sr_receive_data_callback cb, void *cb_data) | |
667 | { | |
668 | return _sr_session_source_add(pollfd, timeout, cb, | |
669 | cb_data, (gintptr)pollfd); | |
670 | } | |
671 | ||
672 | /** | |
673 | * Add an event source for a GIOChannel. | |
674 | * | |
675 | * @param channel The GIOChannel. | |
676 | * @param events Events to poll on. | |
677 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
678 | * @param cb Callback function to add. Must not be NULL. | |
679 | * @param cb_data Data for the callback function. Can be NULL. | |
680 | * | |
681 | * @retval SR_OK Success. | |
682 | * @retval SR_ERR_ARG Invalid argument. | |
683 | * @retval SR_ERR_MALLOC Memory allocation error. | |
684 | */ | |
685 | SR_API int sr_session_source_add_channel(GIOChannel *channel, int events, | |
686 | int timeout, sr_receive_data_callback cb, void *cb_data) | |
687 | { | |
688 | GPollFD p; | |
689 | ||
690 | #ifdef _WIN32 | |
691 | g_io_channel_win32_make_pollfd(channel, events, &p); | |
692 | #else | |
693 | p.fd = g_io_channel_unix_get_fd(channel); | |
694 | p.events = events; | |
695 | #endif | |
696 | ||
697 | return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel); | |
698 | } | |
699 | ||
700 | /** | |
701 | * Remove the source belonging to the specified channel. | |
702 | * | |
703 | * @todo Add more error checks and logging. | |
704 | * | |
705 | * @param poll_object The channel for which the source should be removed. | |
706 | * | |
707 | * @retval SR_OK Success | |
708 | * @retval SR_ERR_ARG Invalid arguments | |
709 | * @retval SR_ERR_MALLOC Memory allocation error | |
710 | * @retval SR_ERR_BUG Internal error | |
711 | */ | |
712 | static int _sr_session_source_remove(gintptr poll_object) | |
713 | { | |
714 | struct source *new_sources; | |
715 | GPollFD *new_pollfds; | |
716 | unsigned int old; | |
717 | ||
718 | if (!session->sources || !session->num_sources) { | |
719 | sr_err("%s: sources was NULL", __func__); | |
720 | return SR_ERR_BUG; | |
721 | } | |
722 | ||
723 | for (old = 0; old < session->num_sources; old++) { | |
724 | if (session->sources[old].poll_object == poll_object) | |
725 | break; | |
726 | } | |
727 | ||
728 | /* fd not found, nothing to do */ | |
729 | if (old == session->num_sources) | |
730 | return SR_OK; | |
731 | ||
732 | session->num_sources -= 1; | |
733 | ||
734 | if (old != session->num_sources) { | |
735 | memmove(&session->pollfds[old], &session->pollfds[old+1], | |
736 | (session->num_sources - old) * sizeof(GPollFD)); | |
737 | memmove(&session->sources[old], &session->sources[old+1], | |
738 | (session->num_sources - old) * sizeof(struct source)); | |
739 | } | |
740 | ||
741 | new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); | |
742 | if (!new_pollfds && session->num_sources > 0) { | |
743 | sr_err("%s: new_pollfds malloc failed", __func__); | |
744 | return SR_ERR_MALLOC; | |
745 | } | |
746 | ||
747 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources); | |
748 | if (!new_sources && session->num_sources > 0) { | |
749 | sr_err("%s: new_sources malloc failed", __func__); | |
750 | return SR_ERR_MALLOC; | |
751 | } | |
752 | ||
753 | session->pollfds = new_pollfds; | |
754 | session->sources = new_sources; | |
755 | ||
756 | return SR_OK; | |
757 | } | |
758 | ||
759 | /** | |
760 | * Remove the source belonging to the specified file descriptor. | |
761 | * | |
762 | * @param fd The file descriptor for which the source should be removed. | |
763 | * | |
764 | * @retval SR_OK Success | |
765 | * @retval SR_ERR_ARG Invalid argument | |
766 | * @retval SR_ERR_MALLOC Memory allocation error. | |
767 | * @retval SR_ERR_BUG Internal error. | |
768 | */ | |
769 | SR_API int sr_session_source_remove(int fd) | |
770 | { | |
771 | return _sr_session_source_remove((gintptr)fd); | |
772 | } | |
773 | ||
774 | /** | |
775 | * Remove the source belonging to the specified poll descriptor. | |
776 | * | |
777 | * @param pollfd The poll descriptor for which the source should be removed. | |
778 | * | |
779 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
780 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
781 | * internal errors. | |
782 | */ | |
783 | SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd) | |
784 | { | |
785 | return _sr_session_source_remove((gintptr)pollfd); | |
786 | } | |
787 | ||
788 | /** | |
789 | * Remove the source belonging to the specified channel. | |
790 | * | |
791 | * @param channel The channel for which the source should be removed. | |
792 | * | |
793 | * @retval SR_OK Success. | |
794 | * @retval SR_ERR_ARG Invalid argument. | |
795 | * @retval SR_ERR_MALLOC Memory allocation error. | |
796 | * @return SR_ERR_BUG Internal error. | |
797 | */ | |
798 | SR_API int sr_session_source_remove_channel(GIOChannel *channel) | |
799 | { | |
800 | return _sr_session_source_remove((gintptr)channel); | |
801 | } | |
802 | ||
803 | /** @} */ |