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