]>
Commit | Line | Data |
---|---|---|
a1bb33af UH |
1 | /* |
2 | * This file is part of the sigrok project. | |
3 | * | |
c73d2ea4 | 4 | * Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com> |
a1bb33af UH |
5 | * |
6 | * This program is free software: you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
20 | #include <stdio.h> | |
21 | #include <stdlib.h> | |
22 | #include <unistd.h> | |
23 | #include <string.h> | |
544a4582 | 24 | #include <glib.h> |
45c59c8b BV |
25 | #include "libsigrok.h" |
26 | #include "libsigrok-internal.h" | |
aa4b1107 | 27 | |
544a4582 | 28 | struct source { |
544a4582 | 29 | int timeout; |
d08490aa | 30 | sr_receive_data_callback_t cb; |
1f9813eb | 31 | void *cb_data; |
aac0ea25 LPC |
32 | |
33 | /* This is used to keep track of the object (fd, pollfd or channel) which is | |
34 | * being polled and will be used to match the source when removing it again. | |
35 | */ | |
36 | gintptr poll_object; | |
544a4582 BV |
37 | }; |
38 | ||
7d658874 | 39 | /* There can only be one session at a time. */ |
a0ecd83b | 40 | /* 'session' is not static, it's used elsewhere (via 'extern'). */ |
2872d21e | 41 | struct sr_session *session; |
544a4582 | 42 | |
9f45fb3a UH |
43 | /** |
44 | * Create a new session. | |
45 | * | |
9f45fb3a UH |
46 | * TODO: Should it use the file-global "session" variable or take an argument? |
47 | * The same question applies to all the other session functions. | |
48 | * | |
49 | * @return A pointer to the newly allocated session, or NULL upon errors. | |
50 | */ | |
1a081ca6 | 51 | SR_API struct sr_session *sr_session_new(void) |
a1bb33af | 52 | { |
133a37bf | 53 | if (!(session = g_try_malloc0(sizeof(struct sr_session)))) { |
9f45fb3a UH |
54 | sr_err("session: %s: session malloc failed", __func__); |
55 | return NULL; /* TODO: SR_ERR_MALLOC? */ | |
56 | } | |
a1bb33af | 57 | |
b7e94111 LPC |
58 | session->source_timeout = -1; |
59 | ||
a1bb33af UH |
60 | return session; |
61 | } | |
62 | ||
9f45fb3a UH |
63 | /** |
64 | * Destroy the current session. | |
65 | * | |
66 | * This frees up all memory used by the session. | |
67 | * | |
e0508e67 | 68 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 69 | */ |
1a081ca6 | 70 | SR_API int sr_session_destroy(void) |
a1bb33af | 71 | { |
9f45fb3a | 72 | if (!session) { |
133a37bf | 73 | sr_err("session: %s: session was NULL", __func__); |
e0508e67 | 74 | return SR_ERR_BUG; |
9f45fb3a UH |
75 | } |
76 | ||
ed229aaa | 77 | sr_session_dev_remove_all(); |
a1bb33af | 78 | |
9f45fb3a UH |
79 | /* TODO: Error checks needed? */ |
80 | ||
aa4b1107 | 81 | /* TODO: Loop over protocol decoders and free them. */ |
a1bb33af UH |
82 | |
83 | g_free(session); | |
9f45fb3a | 84 | session = NULL; |
e0508e67 UH |
85 | |
86 | return SR_OK; | |
a1bb33af UH |
87 | } |
88 | ||
de4d3f99 | 89 | static void sr_dev_close(struct sr_dev_inst *sdi) |
ed229aaa | 90 | { |
de4d3f99 BV |
91 | if (sdi->driver->dev_close) |
92 | sdi->driver->dev_close(sdi); | |
ed229aaa LPC |
93 | } |
94 | ||
9f45fb3a UH |
95 | /** |
96 | * Remove all the devices from the current session. TODO? | |
97 | * | |
98 | * The session itself (i.e., the struct sr_session) is not free'd and still | |
99 | * exists after this function returns. | |
100 | * | |
e0508e67 | 101 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 102 | */ |
01c3e9db | 103 | SR_API int sr_session_dev_remove_all(void) |
a1bb33af | 104 | { |
9f45fb3a | 105 | if (!session) { |
133a37bf | 106 | sr_err("session: %s: session was NULL", __func__); |
e0508e67 | 107 | return SR_ERR_BUG; |
9f45fb3a UH |
108 | } |
109 | ||
ed229aaa | 110 | g_slist_free_full(session->devs, (GDestroyNotify)sr_dev_close); |
bb7ef793 | 111 | session->devs = NULL; |
e0508e67 UH |
112 | |
113 | return SR_OK; | |
a1bb33af UH |
114 | } |
115 | ||
9f45fb3a UH |
116 | /** |
117 | * Add a device to the current session. | |
118 | * | |
de4d3f99 BV |
119 | * @param dev The device instance to add to the current session. Must not |
120 | * be NULL. Also, sdi->driver and sdi->driver->dev_open must | |
121 | * not be NULL. | |
9f45fb3a UH |
122 | * |
123 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. | |
124 | */ | |
de4d3f99 | 125 | SR_API int sr_session_dev_add(const struct sr_dev_inst *sdi) |
a1bb33af UH |
126 | { |
127 | int ret; | |
128 | ||
de4d3f99 BV |
129 | if (!sdi) { |
130 | sr_err("session: %s: sdi was NULL", __func__); | |
9f45fb3a UH |
131 | return SR_ERR_ARG; |
132 | } | |
133 | ||
d6eb0c33 UH |
134 | if (!session) { |
135 | sr_err("session: %s: session was NULL", __func__); | |
136 | return SR_ERR_BUG; | |
137 | } | |
138 | ||
de4d3f99 BV |
139 | /* If sdi->driver is NULL, this is a virtual device. */ |
140 | if (!sdi->driver) { | |
141 | sr_dbg("session: %s: sdi->driver was NULL, this seems to be " | |
d6eb0c33 UH |
142 | "a virtual device; continuing", __func__); |
143 | /* Just add the device, don't run dev_open(). */ | |
de4d3f99 | 144 | session->devs = g_slist_append(session->devs, (gpointer)sdi); |
d6eb0c33 | 145 | return SR_OK; |
9f45fb3a UH |
146 | } |
147 | ||
de4d3f99 BV |
148 | /* sdi->driver is non-NULL (i.e. we have a real device). */ |
149 | if (!sdi->driver->dev_open) { | |
150 | sr_err("session: %s: sdi->driver->dev_open was NULL", __func__); | |
8ec95d22 | 151 | return SR_ERR_BUG; |
9f45fb3a UH |
152 | } |
153 | ||
de4d3f99 | 154 | if ((ret = sdi->driver->dev_open((struct sr_dev_inst *)sdi)) != SR_OK) { |
e7eb703f | 155 | sr_err("session: %s: dev_open failed (%d)", __func__, ret); |
9f45fb3a | 156 | return ret; |
aa4b1107 | 157 | } |
a1bb33af | 158 | |
de4d3f99 | 159 | session->devs = g_slist_append(session->devs, (gpointer)sdi); |
aa4b1107 | 160 | |
e46b8fb1 | 161 | return SR_OK; |
a1bb33af UH |
162 | } |
163 | ||
9f45fb3a | 164 | /** |
01c3e9db | 165 | * Remove all datafeed callbacks in the current session. |
9f45fb3a | 166 | * |
e0508e67 | 167 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 168 | */ |
01c3e9db | 169 | SR_API int sr_session_datafeed_callback_remove_all(void) |
a1bb33af | 170 | { |
9f45fb3a UH |
171 | if (!session) { |
172 | sr_err("session: %s: session was NULL", __func__); | |
e0508e67 | 173 | return SR_ERR_BUG; |
9f45fb3a UH |
174 | } |
175 | ||
a1bb33af UH |
176 | g_slist_free(session->datafeed_callbacks); |
177 | session->datafeed_callbacks = NULL; | |
e0508e67 UH |
178 | |
179 | return SR_OK; | |
a1bb33af UH |
180 | } |
181 | ||
9f45fb3a UH |
182 | /** |
183 | * Add a datafeed callback to the current session. | |
184 | * | |
d08490aa | 185 | * @param cb Function to call when a chunk of data is received. |
0abee507 | 186 | * Must not be NULL. |
a1645fcd | 187 | * |
e0508e67 | 188 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 189 | */ |
d08490aa | 190 | SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb) |
a1bb33af | 191 | { |
9f45fb3a UH |
192 | if (!session) { |
193 | sr_err("session: %s: session was NULL", __func__); | |
e0508e67 | 194 | return SR_ERR_BUG; |
9f45fb3a UH |
195 | } |
196 | ||
0abee507 UH |
197 | if (!cb) { |
198 | sr_err("session: %s: cb was NULL", __func__); | |
199 | return SR_ERR_ARG; | |
200 | } | |
9f45fb3a | 201 | |
62c82025 | 202 | session->datafeed_callbacks = |
d08490aa | 203 | g_slist_append(session->datafeed_callbacks, cb); |
e0508e67 UH |
204 | |
205 | return SR_OK; | |
a1bb33af UH |
206 | } |
207 | ||
9f45fb3a UH |
208 | /** |
209 | * TODO. | |
210 | */ | |
e0508e67 | 211 | static int sr_session_run_poll(void) |
544a4582 | 212 | { |
b7e94111 LPC |
213 | unsigned int i; |
214 | int ret; | |
544a4582 | 215 | |
544a4582 | 216 | while (session->running) { |
b7e94111 | 217 | ret = g_poll(session->pollfds, session->num_sources, session->source_timeout); |
544a4582 | 218 | |
b7e94111 LPC |
219 | for (i = 0; i < session->num_sources; i++) { |
220 | if (session->pollfds[i].revents > 0 || (ret == 0 | |
221 | && session->source_timeout == session->sources[i].timeout)) { | |
544a4582 BV |
222 | /* |
223 | * Invoke the source's callback on an event, | |
224 | * or if the poll timeout out and this source | |
225 | * asked for that timeout. | |
226 | */ | |
b7e94111 LPC |
227 | if (!session->sources[i].cb(session->pollfds[i].fd, session->pollfds[i].revents, |
228 | session->sources[i].cb_data)) | |
229 | sr_session_source_remove(session->sources[i].poll_object); | |
544a4582 BV |
230 | } |
231 | } | |
232 | } | |
e0508e67 UH |
233 | |
234 | return SR_OK; | |
544a4582 BV |
235 | } |
236 | ||
9f45fb3a UH |
237 | /** |
238 | * Start a session. | |
239 | * | |
a1645fcd | 240 | * There can only be one session at a time. |
9f45fb3a UH |
241 | * |
242 | * @return SR_OK upon success, SR_ERR upon errors. | |
243 | */ | |
1a081ca6 | 244 | SR_API int sr_session_start(void) |
7d658874 | 245 | { |
de4d3f99 | 246 | struct sr_dev_inst *sdi; |
7d658874 BV |
247 | GSList *l; |
248 | int ret; | |
249 | ||
9f45fb3a UH |
250 | if (!session) { |
251 | sr_err("session: %s: session was NULL; a session must be " | |
de4d3f99 | 252 | "created before starting it.", __func__); |
0abee507 | 253 | return SR_ERR_BUG; |
9f45fb3a UH |
254 | } |
255 | ||
bb7ef793 | 256 | if (!session->devs) { |
bb7ef793 | 257 | sr_err("session: %s: session->devs was NULL; a session " |
9f45fb3a | 258 | "cannot be started without devices.", __func__); |
0abee507 | 259 | return SR_ERR_BUG; |
9f45fb3a UH |
260 | } |
261 | ||
b08024a8 | 262 | sr_info("session: starting"); |
9f45fb3a | 263 | |
bb7ef793 | 264 | for (l = session->devs; l; l = l->next) { |
de4d3f99 BV |
265 | sdi = l->data; |
266 | if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) { | |
446a0372 | 267 | sr_err("session: %s: could not start an acquisition " |
9f45fb3a | 268 | "(%d)", __func__, ret); |
7d658874 | 269 | break; |
9f45fb3a | 270 | } |
7d658874 BV |
271 | } |
272 | ||
9f45fb3a UH |
273 | /* TODO: What if there are multiple devices? Which return code? */ |
274 | ||
7d658874 BV |
275 | return ret; |
276 | } | |
277 | ||
9f45fb3a UH |
278 | /** |
279 | * Run the session. | |
280 | * | |
9f45fb3a UH |
281 | * TODO: Various error checks etc. |
282 | * | |
e0508e67 | 283 | * @return SR_OK upon success, SR_ERR_BUG upon errors. |
9f45fb3a | 284 | */ |
1a081ca6 | 285 | SR_API int sr_session_run(void) |
7d658874 | 286 | { |
9f45fb3a UH |
287 | if (!session) { |
288 | sr_err("session: %s: session was NULL; a session must be " | |
289 | "created first, before running it.", __func__); | |
e0508e67 | 290 | return SR_ERR_BUG; |
9f45fb3a UH |
291 | } |
292 | ||
bb7ef793 | 293 | if (!session->devs) { |
9f45fb3a | 294 | /* TODO: Actually the case? */ |
bb7ef793 | 295 | sr_err("session: %s: session->devs was NULL; a session " |
9f45fb3a | 296 | "cannot be run without devices.", __func__); |
e0508e67 | 297 | return SR_ERR_BUG; |
9f45fb3a UH |
298 | } |
299 | ||
b08024a8 | 300 | sr_info("session: running"); |
7d658874 BV |
301 | session->running = TRUE; |
302 | ||
9f45fb3a | 303 | /* Do we have real sources? */ |
b7e94111 | 304 | if (session->num_sources == 1 && session->pollfds[0].fd == -1) { |
9f45fb3a | 305 | /* Dummy source, freewheel over it. */ |
7d658874 | 306 | while (session->running) |
b7e94111 | 307 | session->sources[0].cb(-1, 0, session->sources[0].cb_data); |
9f45fb3a UH |
308 | } else { |
309 | /* Real sources, use g_poll() main loop. */ | |
8a2efef2 | 310 | sr_session_run_poll(); |
9f45fb3a UH |
311 | } |
312 | ||
e0508e67 | 313 | return SR_OK; |
7d658874 BV |
314 | } |
315 | ||
9f45fb3a UH |
316 | /** |
317 | * Halt the current session. | |
318 | * | |
9ffbde0e LPC |
319 | * This function is deprecated and should not be used in new code, use |
320 | * sr_session_stop() instead. The behaviour of this function is identical to | |
321 | * sr_session_stop(). | |
e0508e67 UH |
322 | * |
323 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. | |
9f45fb3a | 324 | */ |
1a081ca6 | 325 | SR_API int sr_session_halt(void) |
544a4582 | 326 | { |
9ffbde0e | 327 | return sr_session_stop(); |
544a4582 BV |
328 | } |
329 | ||
9f45fb3a UH |
330 | /** |
331 | * Stop the current session. | |
332 | * | |
a1645fcd | 333 | * The current session is stopped immediately, with all acquisition sessions |
c09f0b57 | 334 | * being stopped and hardware drivers cleaned up. |
9f45fb3a | 335 | * |
e0508e67 | 336 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 337 | */ |
1a081ca6 | 338 | SR_API int sr_session_stop(void) |
a1bb33af | 339 | { |
de4d3f99 | 340 | struct sr_dev_inst *sdi; |
a1bb33af UH |
341 | GSList *l; |
342 | ||
9f45fb3a UH |
343 | if (!session) { |
344 | sr_err("session: %s: session was NULL", __func__); | |
e0508e67 | 345 | return SR_ERR_BUG; |
9f45fb3a UH |
346 | } |
347 | ||
b08024a8 | 348 | sr_info("session: stopping"); |
544a4582 | 349 | session->running = FALSE; |
e0508e67 | 350 | |
bb7ef793 | 351 | for (l = session->devs; l; l = l->next) { |
de4d3f99 BV |
352 | sdi = l->data; |
353 | if (sdi->driver) { | |
354 | if (sdi->driver->dev_acquisition_stop) | |
355 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
8c76be53 | 356 | } |
a1bb33af | 357 | } |
9f45fb3a | 358 | |
e0508e67 | 359 | return SR_OK; |
a1bb33af UH |
360 | } |
361 | ||
9f45fb3a | 362 | /** |
a1645fcd | 363 | * Debug helper. |
9f45fb3a | 364 | * |
996b0c72 | 365 | * @param packet The packet to show debugging information for. |
9f45fb3a | 366 | */ |
18beaeff | 367 | static void datafeed_dump(struct sr_datafeed_packet *packet) |
7d2afd6c BV |
368 | { |
369 | struct sr_datafeed_logic *logic; | |
ee7489d2 | 370 | struct sr_datafeed_analog *analog; |
7d2afd6c BV |
371 | |
372 | switch (packet->type) { | |
373 | case SR_DF_HEADER: | |
374 | sr_dbg("bus: received SR_DF_HEADER"); | |
375 | break; | |
376 | case SR_DF_TRIGGER: | |
01469707 | 377 | sr_dbg("bus: received SR_DF_TRIGGER"); |
7d2afd6c | 378 | break; |
ee7489d2 BV |
379 | case SR_DF_META_LOGIC: |
380 | sr_dbg("bus: received SR_DF_META_LOGIC"); | |
381 | break; | |
7d2afd6c BV |
382 | case SR_DF_LOGIC: |
383 | logic = packet->payload; | |
e0508e67 | 384 | /* TODO: Check for logic != NULL. */ |
01469707 | 385 | sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length); |
7d2afd6c | 386 | break; |
ee7489d2 BV |
387 | case SR_DF_META_ANALOG: |
388 | sr_dbg("bus: received SR_DF_META_LOGIC"); | |
389 | break; | |
390 | case SR_DF_ANALOG: | |
391 | analog = packet->payload; | |
392 | /* TODO: Check for analog != NULL. */ | |
393 | sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples); | |
394 | break; | |
7d2afd6c BV |
395 | case SR_DF_END: |
396 | sr_dbg("bus: received SR_DF_END"); | |
397 | break; | |
6ea7669c BV |
398 | case SR_DF_FRAME_BEGIN: |
399 | sr_dbg("bus: received SR_DF_FRAME_BEGIN"); | |
400 | break; | |
401 | case SR_DF_FRAME_END: | |
402 | sr_dbg("bus: received SR_DF_FRAME_END"); | |
403 | break; | |
7d2afd6c | 404 | default: |
18beaeff | 405 | sr_dbg("bus: received unknown packet type %d", packet->type); |
9f45fb3a | 406 | break; |
7d2afd6c | 407 | } |
7d2afd6c BV |
408 | } |
409 | ||
9f45fb3a | 410 | /** |
a1645fcd BV |
411 | * Send a packet to whatever is listening on the datafeed bus. |
412 | * | |
413 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 414 | * |
bb7ef793 | 415 | * @param dev TODO. |
31ccebc4 | 416 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 417 | * |
e0508e67 | 418 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
9f45fb3a | 419 | */ |
de4d3f99 | 420 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
31ccebc4 | 421 | struct sr_datafeed_packet *packet) |
a1bb33af UH |
422 | { |
423 | GSList *l; | |
d08490aa | 424 | sr_datafeed_callback_t cb; |
a1bb33af | 425 | |
de4d3f99 BV |
426 | if (!sdi) { |
427 | sr_err("session: %s: sdi was NULL", __func__); | |
e0508e67 | 428 | return SR_ERR_ARG; |
9f45fb3a UH |
429 | } |
430 | ||
e0508e67 UH |
431 | if (!packet) { |
432 | sr_err("session: %s: packet was NULL", __func__); | |
433 | return SR_ERR_ARG; | |
9f45fb3a UH |
434 | } |
435 | ||
62c82025 | 436 | for (l = session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
437 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
438 | datafeed_dump(packet); | |
a1bb33af | 439 | cb = l->data; |
de4d3f99 | 440 | cb(sdi, packet); |
a1bb33af | 441 | } |
9f45fb3a | 442 | |
e0508e67 | 443 | return SR_OK; |
a1bb33af UH |
444 | } |
445 | ||
aac0ea25 | 446 | static int _sr_session_source_add(GPollFD *pollfd, int timeout, |
1a895c61 | 447 | sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object) |
544a4582 BV |
448 | { |
449 | struct source *new_sources, *s; | |
aac0ea25 | 450 | GPollFD *new_pollfds; |
544a4582 | 451 | |
d08490aa UH |
452 | if (!cb) { |
453 | sr_err("session: %s: cb was NULL", __func__); | |
e0508e67 | 454 | return SR_ERR_ARG; |
9f45fb3a UH |
455 | } |
456 | ||
1f9813eb | 457 | /* Note: cb_data can be NULL, that's not a bug. */ |
9f45fb3a | 458 | |
2ac2e629 BV |
459 | new_pollfds = g_try_realloc(session->pollfds, |
460 | sizeof(GPollFD) * (session->num_sources + 1)); | |
0687dfcd | 461 | if (!new_pollfds) { |
1a895c61 | 462 | sr_err("session: %s: new_pollfds malloc failed", __func__); |
0687dfcd LPC |
463 | return SR_ERR_MALLOC; |
464 | } | |
465 | ||
b7e94111 | 466 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * |
2ac2e629 | 467 | (session->num_sources + 1)); |
9f45fb3a UH |
468 | if (!new_sources) { |
469 | sr_err("session: %s: new_sources malloc failed", __func__); | |
e0508e67 | 470 | return SR_ERR_MALLOC; |
9f45fb3a | 471 | } |
544a4582 | 472 | |
b7e94111 LPC |
473 | new_pollfds[session->num_sources] = *pollfd; |
474 | s = &new_sources[session->num_sources++]; | |
544a4582 | 475 | s->timeout = timeout; |
d08490aa | 476 | s->cb = cb; |
1f9813eb | 477 | s->cb_data = cb_data; |
aac0ea25 | 478 | s->poll_object = poll_object; |
b7e94111 LPC |
479 | session->pollfds = new_pollfds; |
480 | session->sources = new_sources; | |
544a4582 | 481 | |
b7e94111 LPC |
482 | if (timeout != session->source_timeout && timeout > 0 |
483 | && (session->source_timeout == -1 || timeout < session->source_timeout)) | |
484 | session->source_timeout = timeout; | |
9f45fb3a | 485 | |
e0508e67 | 486 | return SR_OK; |
544a4582 BV |
487 | } |
488 | ||
9f45fb3a | 489 | /** |
aac0ea25 | 490 | * Add a event source for a file descriptor. |
9f45fb3a | 491 | * |
aac0ea25 LPC |
492 | * @param fd The file descriptor. |
493 | * @param events Events to check for. | |
494 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
495 | * @param cb Callback function to add. Must not be NULL. | |
496 | * @param cb_data Data for the callback function. Can be NULL. | |
9f45fb3a | 497 | * |
aac0ea25 LPC |
498 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
499 | * SR_ERR_MALLOC upon memory allocation errors. | |
500 | */ | |
501 | SR_API int sr_session_source_add(int fd, int events, int timeout, | |
502 | sr_receive_data_callback_t cb, void *cb_data) | |
503 | { | |
504 | GPollFD p; | |
505 | ||
aac0ea25 LPC |
506 | p.fd = fd; |
507 | p.events = events; | |
aac0ea25 LPC |
508 | |
509 | return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd); | |
510 | } | |
511 | ||
512 | /** | |
1a895c61 | 513 | * Add an event source for a GPollFD. |
aac0ea25 LPC |
514 | * |
515 | * TODO: More error checks etc. | |
516 | * | |
517 | * @param pollfd The GPollFD. | |
518 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
519 | * @param cb Callback function to add. Must not be NULL. | |
520 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 521 | * |
e0508e67 | 522 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
aac0ea25 | 523 | * SR_ERR_MALLOC upon memory allocation errors. |
9f45fb3a | 524 | */ |
aac0ea25 LPC |
525 | SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout, |
526 | sr_receive_data_callback_t cb, void *cb_data) | |
527 | { | |
1a895c61 UH |
528 | return _sr_session_source_add(pollfd, timeout, cb, |
529 | cb_data, (gintptr)pollfd); | |
aac0ea25 LPC |
530 | } |
531 | ||
532 | /** | |
1a895c61 | 533 | * Add an event source for a GIOChannel. |
aac0ea25 LPC |
534 | * |
535 | * TODO: More error checks etc. | |
536 | * | |
537 | * @param channel The GIOChannel. | |
538 | * @param events Events to poll on. | |
539 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
540 | * @param cb Callback function to add. Must not be NULL. | |
541 | * @param cb_data Data for the callback function. Can be NULL. | |
542 | * | |
543 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
544 | * SR_ERR_MALLOC upon memory allocation errors. | |
545 | */ | |
1a895c61 UH |
546 | SR_API int sr_session_source_add_channel(GIOChannel *channel, int events, |
547 | int timeout, sr_receive_data_callback_t cb, void *cb_data) | |
aac0ea25 LPC |
548 | { |
549 | GPollFD p; | |
550 | ||
551 | #ifdef _WIN32 | |
552 | g_io_channel_win32_make_pollfd(channel, | |
553 | events, &p); | |
554 | #else | |
555 | p.fd = g_io_channel_unix_get_fd(channel); | |
556 | p.events = events; | |
557 | #endif | |
558 | ||
559 | return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel); | |
560 | } | |
561 | ||
562 | ||
563 | static int _sr_session_source_remove(gintptr poll_object) | |
544a4582 BV |
564 | { |
565 | struct source *new_sources; | |
0687dfcd | 566 | GPollFD *new_pollfds; |
b7e94111 | 567 | unsigned int old; |
544a4582 | 568 | |
b7e94111 | 569 | if (!session->sources || !session->num_sources) { |
e0508e67 | 570 | sr_err("session: %s: sources was NULL", __func__); |
0abee507 | 571 | return SR_ERR_BUG; |
e0508e67 UH |
572 | } |
573 | ||
b7e94111 LPC |
574 | for (old = 0; old < session->num_sources; old++) { |
575 | if (session->sources[old].poll_object == poll_object) | |
2bccd322 | 576 | break; |
9f45fb3a UH |
577 | } |
578 | ||
2bccd322 | 579 | /* fd not found, nothing to do */ |
b7e94111 | 580 | if (old == session->num_sources) |
2bccd322 LPC |
581 | return SR_OK; |
582 | ||
b7e94111 | 583 | session->num_sources -= 1; |
2bccd322 | 584 | |
b7e94111 LPC |
585 | if (old != session->num_sources) { |
586 | memmove(&session->pollfds[old], &session->pollfds[old+1], | |
587 | (session->num_sources - old) * sizeof(GPollFD)); | |
588 | memmove(&session->sources[old], &session->sources[old+1], | |
589 | (session->num_sources - old) * sizeof(struct source)); | |
9f45fb3a | 590 | } |
544a4582 | 591 | |
b7e94111 LPC |
592 | new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); |
593 | if (!new_pollfds && session->num_sources > 0) { | |
1a895c61 | 594 | sr_err("session: %s: new_pollfds malloc failed", __func__); |
0687dfcd LPC |
595 | return SR_ERR_MALLOC; |
596 | } | |
597 | ||
b7e94111 LPC |
598 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources); |
599 | if (!new_sources && session->num_sources > 0) { | |
2bccd322 LPC |
600 | sr_err("session: %s: new_sources malloc failed", __func__); |
601 | return SR_ERR_MALLOC; | |
544a4582 | 602 | } |
e0508e67 | 603 | |
b7e94111 LPC |
604 | session->pollfds = new_pollfds; |
605 | session->sources = new_sources; | |
2bccd322 | 606 | |
e0508e67 | 607 | return SR_OK; |
544a4582 | 608 | } |
aac0ea25 LPC |
609 | |
610 | /* | |
611 | * Remove the source belonging to the specified file descriptor. | |
612 | * | |
613 | * TODO: More error checks. | |
614 | * | |
1a895c61 | 615 | * @param fd The file descriptor for which the source should be removed. |
aac0ea25 LPC |
616 | * |
617 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
618 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
619 | * internal errors. | |
620 | */ | |
621 | SR_API int sr_session_source_remove(int fd) | |
622 | { | |
623 | return _sr_session_source_remove((gintptr)fd); | |
624 | } | |
625 | ||
626 | /** | |
627 | * Remove the source belonging to the specified poll descriptor. | |
628 | * | |
629 | * TODO: More error checks. | |
630 | * | |
631 | * @param pollfd The poll descriptor for which the source should be removed. | |
632 | * | |
633 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
634 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
635 | * internal errors. | |
636 | */ | |
637 | SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd) | |
638 | { | |
639 | return _sr_session_source_remove((gintptr)pollfd); | |
640 | } | |
641 | ||
642 | /* | |
643 | * Remove the source belonging to the specified channel. | |
644 | * | |
645 | * TODO: More error checks. | |
646 | * | |
1a895c61 | 647 | * @param channel The channel for which the source should be removed. |
aac0ea25 LPC |
648 | * |
649 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
650 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
651 | * internal errors. | |
652 | */ | |
653 | SR_API int sr_session_source_remove_channel(GIOChannel *channel) | |
654 | { | |
655 | return _sr_session_source_remove((gintptr)channel); | |
656 | } |