]>
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 | { |
e8d3d6c8 | 91 | if (sdi->driver && sdi->driver->dev_close) |
de4d3f99 | 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 | ||
e0508e67 | 208 | static int sr_session_run_poll(void) |
544a4582 | 209 | { |
b7e94111 LPC |
210 | unsigned int i; |
211 | int ret; | |
544a4582 | 212 | |
2cbeb2b7 BV |
213 | while (session->num_sources > 0) { |
214 | ret = g_poll(session->pollfds, session->num_sources, | |
215 | session->source_timeout); | |
b7e94111 LPC |
216 | for (i = 0; i < session->num_sources; i++) { |
217 | if (session->pollfds[i].revents > 0 || (ret == 0 | |
218 | && session->source_timeout == session->sources[i].timeout)) { | |
544a4582 BV |
219 | /* |
220 | * Invoke the source's callback on an event, | |
2cbeb2b7 | 221 | * or if the poll timed out and this source |
544a4582 BV |
222 | * asked for that timeout. |
223 | */ | |
2cbeb2b7 BV |
224 | if (!session->sources[i].cb(session->pollfds[i].fd, |
225 | session->pollfds[i].revents, | |
226 | session->sources[i].cb_data)) | |
b7e94111 | 227 | sr_session_source_remove(session->sources[i].poll_object); |
544a4582 BV |
228 | } |
229 | } | |
230 | } | |
e0508e67 UH |
231 | |
232 | return SR_OK; | |
544a4582 BV |
233 | } |
234 | ||
9f45fb3a UH |
235 | /** |
236 | * Start a session. | |
237 | * | |
a1645fcd | 238 | * There can only be one session at a time. |
9f45fb3a UH |
239 | * |
240 | * @return SR_OK upon success, SR_ERR upon errors. | |
241 | */ | |
1a081ca6 | 242 | SR_API int sr_session_start(void) |
7d658874 | 243 | { |
de4d3f99 | 244 | struct sr_dev_inst *sdi; |
7d658874 BV |
245 | GSList *l; |
246 | int ret; | |
247 | ||
9f45fb3a UH |
248 | if (!session) { |
249 | sr_err("session: %s: session was NULL; a session must be " | |
de4d3f99 | 250 | "created before starting it.", __func__); |
0abee507 | 251 | return SR_ERR_BUG; |
9f45fb3a UH |
252 | } |
253 | ||
bb7ef793 | 254 | if (!session->devs) { |
bb7ef793 | 255 | sr_err("session: %s: session->devs was NULL; a session " |
9f45fb3a | 256 | "cannot be started without devices.", __func__); |
0abee507 | 257 | return SR_ERR_BUG; |
9f45fb3a UH |
258 | } |
259 | ||
b08024a8 | 260 | sr_info("session: starting"); |
9f45fb3a | 261 | |
bb7ef793 | 262 | for (l = session->devs; l; l = l->next) { |
de4d3f99 BV |
263 | sdi = l->data; |
264 | if ((ret = sdi->driver->dev_acquisition_start(sdi, sdi)) != SR_OK) { | |
446a0372 | 265 | sr_err("session: %s: could not start an acquisition " |
9f45fb3a | 266 | "(%d)", __func__, ret); |
7d658874 | 267 | break; |
9f45fb3a | 268 | } |
7d658874 BV |
269 | } |
270 | ||
9f45fb3a UH |
271 | /* TODO: What if there are multiple devices? Which return code? */ |
272 | ||
7d658874 BV |
273 | return ret; |
274 | } | |
275 | ||
9f45fb3a UH |
276 | /** |
277 | * Run the session. | |
278 | * | |
9f45fb3a UH |
279 | * TODO: Various error checks etc. |
280 | * | |
e0508e67 | 281 | * @return SR_OK upon success, SR_ERR_BUG upon errors. |
9f45fb3a | 282 | */ |
1a081ca6 | 283 | SR_API int sr_session_run(void) |
7d658874 | 284 | { |
9f45fb3a UH |
285 | if (!session) { |
286 | sr_err("session: %s: session was NULL; a session must be " | |
287 | "created first, before running it.", __func__); | |
e0508e67 | 288 | return SR_ERR_BUG; |
9f45fb3a UH |
289 | } |
290 | ||
bb7ef793 | 291 | if (!session->devs) { |
9f45fb3a | 292 | /* TODO: Actually the case? */ |
bb7ef793 | 293 | sr_err("session: %s: session->devs was NULL; a session " |
9f45fb3a | 294 | "cannot be run without devices.", __func__); |
e0508e67 | 295 | return SR_ERR_BUG; |
9f45fb3a UH |
296 | } |
297 | ||
b08024a8 | 298 | sr_info("session: running"); |
7d658874 | 299 | |
9f45fb3a | 300 | /* Do we have real sources? */ |
b7e94111 | 301 | if (session->num_sources == 1 && session->pollfds[0].fd == -1) { |
9f45fb3a | 302 | /* Dummy source, freewheel over it. */ |
2cbeb2b7 | 303 | while (session->num_sources) |
b7e94111 | 304 | session->sources[0].cb(-1, 0, session->sources[0].cb_data); |
9f45fb3a UH |
305 | } else { |
306 | /* Real sources, use g_poll() main loop. */ | |
8a2efef2 | 307 | sr_session_run_poll(); |
9f45fb3a UH |
308 | } |
309 | ||
e0508e67 | 310 | return SR_OK; |
7d658874 BV |
311 | } |
312 | ||
9f45fb3a UH |
313 | /** |
314 | * Halt the current session. | |
315 | * | |
9ffbde0e LPC |
316 | * This function is deprecated and should not be used in new code, use |
317 | * sr_session_stop() instead. The behaviour of this function is identical to | |
318 | * sr_session_stop(). | |
e0508e67 UH |
319 | * |
320 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. | |
9f45fb3a | 321 | */ |
1a081ca6 | 322 | SR_API int sr_session_halt(void) |
544a4582 | 323 | { |
9ffbde0e | 324 | return sr_session_stop(); |
544a4582 BV |
325 | } |
326 | ||
9f45fb3a UH |
327 | /** |
328 | * Stop the current session. | |
329 | * | |
a1645fcd | 330 | * The current session is stopped immediately, with all acquisition sessions |
c09f0b57 | 331 | * being stopped and hardware drivers cleaned up. |
9f45fb3a | 332 | * |
e0508e67 | 333 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 334 | */ |
1a081ca6 | 335 | SR_API int sr_session_stop(void) |
a1bb33af | 336 | { |
de4d3f99 | 337 | struct sr_dev_inst *sdi; |
a1bb33af UH |
338 | GSList *l; |
339 | ||
9f45fb3a UH |
340 | if (!session) { |
341 | sr_err("session: %s: session was NULL", __func__); | |
e0508e67 | 342 | return SR_ERR_BUG; |
9f45fb3a UH |
343 | } |
344 | ||
b08024a8 | 345 | sr_info("session: stopping"); |
e0508e67 | 346 | |
bb7ef793 | 347 | for (l = session->devs; l; l = l->next) { |
de4d3f99 BV |
348 | sdi = l->data; |
349 | if (sdi->driver) { | |
350 | if (sdi->driver->dev_acquisition_stop) | |
351 | sdi->driver->dev_acquisition_stop(sdi, sdi); | |
8c76be53 | 352 | } |
a1bb33af | 353 | } |
9f45fb3a | 354 | |
e0508e67 | 355 | return SR_OK; |
a1bb33af UH |
356 | } |
357 | ||
9f45fb3a | 358 | /** |
a1645fcd | 359 | * Debug helper. |
9f45fb3a | 360 | * |
996b0c72 | 361 | * @param packet The packet to show debugging information for. |
9f45fb3a | 362 | */ |
18beaeff | 363 | static void datafeed_dump(struct sr_datafeed_packet *packet) |
7d2afd6c BV |
364 | { |
365 | struct sr_datafeed_logic *logic; | |
ee7489d2 | 366 | struct sr_datafeed_analog *analog; |
7d2afd6c BV |
367 | |
368 | switch (packet->type) { | |
369 | case SR_DF_HEADER: | |
370 | sr_dbg("bus: received SR_DF_HEADER"); | |
371 | break; | |
372 | case SR_DF_TRIGGER: | |
01469707 | 373 | sr_dbg("bus: received SR_DF_TRIGGER"); |
7d2afd6c | 374 | break; |
ee7489d2 BV |
375 | case SR_DF_META_LOGIC: |
376 | sr_dbg("bus: received SR_DF_META_LOGIC"); | |
377 | break; | |
7d2afd6c BV |
378 | case SR_DF_LOGIC: |
379 | logic = packet->payload; | |
e0508e67 | 380 | /* TODO: Check for logic != NULL. */ |
01469707 | 381 | sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length); |
7d2afd6c | 382 | break; |
ee7489d2 | 383 | case SR_DF_META_ANALOG: |
dfd8f56e | 384 | sr_dbg("bus: received SR_DF_META_ANALOG"); |
ee7489d2 BV |
385 | break; |
386 | case SR_DF_ANALOG: | |
387 | analog = packet->payload; | |
388 | /* TODO: Check for analog != NULL. */ | |
389 | sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples); | |
390 | break; | |
7d2afd6c BV |
391 | case SR_DF_END: |
392 | sr_dbg("bus: received SR_DF_END"); | |
393 | break; | |
6ea7669c BV |
394 | case SR_DF_FRAME_BEGIN: |
395 | sr_dbg("bus: received SR_DF_FRAME_BEGIN"); | |
396 | break; | |
397 | case SR_DF_FRAME_END: | |
398 | sr_dbg("bus: received SR_DF_FRAME_END"); | |
399 | break; | |
7d2afd6c | 400 | default: |
18beaeff | 401 | sr_dbg("bus: received unknown packet type %d", packet->type); |
9f45fb3a | 402 | break; |
7d2afd6c | 403 | } |
7d2afd6c BV |
404 | } |
405 | ||
9f45fb3a | 406 | /** |
a1645fcd BV |
407 | * Send a packet to whatever is listening on the datafeed bus. |
408 | * | |
409 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 410 | * |
bb7ef793 | 411 | * @param dev TODO. |
31ccebc4 | 412 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 413 | * |
e0508e67 | 414 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
9f45fb3a | 415 | */ |
de4d3f99 | 416 | SR_PRIV int sr_session_send(const struct sr_dev_inst *sdi, |
31ccebc4 | 417 | struct sr_datafeed_packet *packet) |
a1bb33af UH |
418 | { |
419 | GSList *l; | |
d08490aa | 420 | sr_datafeed_callback_t cb; |
a1bb33af | 421 | |
de4d3f99 BV |
422 | if (!sdi) { |
423 | sr_err("session: %s: sdi was NULL", __func__); | |
e0508e67 | 424 | return SR_ERR_ARG; |
9f45fb3a UH |
425 | } |
426 | ||
e0508e67 UH |
427 | if (!packet) { |
428 | sr_err("session: %s: packet was NULL", __func__); | |
429 | return SR_ERR_ARG; | |
9f45fb3a UH |
430 | } |
431 | ||
62c82025 | 432 | for (l = session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
433 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
434 | datafeed_dump(packet); | |
a1bb33af | 435 | cb = l->data; |
de4d3f99 | 436 | cb(sdi, packet); |
a1bb33af | 437 | } |
9f45fb3a | 438 | |
e0508e67 | 439 | return SR_OK; |
a1bb33af UH |
440 | } |
441 | ||
aac0ea25 | 442 | static int _sr_session_source_add(GPollFD *pollfd, int timeout, |
1a895c61 | 443 | sr_receive_data_callback_t cb, void *cb_data, gintptr poll_object) |
544a4582 BV |
444 | { |
445 | struct source *new_sources, *s; | |
aac0ea25 | 446 | GPollFD *new_pollfds; |
544a4582 | 447 | |
d08490aa UH |
448 | if (!cb) { |
449 | sr_err("session: %s: cb was NULL", __func__); | |
e0508e67 | 450 | return SR_ERR_ARG; |
9f45fb3a UH |
451 | } |
452 | ||
1f9813eb | 453 | /* Note: cb_data can be NULL, that's not a bug. */ |
9f45fb3a | 454 | |
2ac2e629 BV |
455 | new_pollfds = g_try_realloc(session->pollfds, |
456 | sizeof(GPollFD) * (session->num_sources + 1)); | |
0687dfcd | 457 | if (!new_pollfds) { |
1a895c61 | 458 | sr_err("session: %s: new_pollfds malloc failed", __func__); |
0687dfcd LPC |
459 | return SR_ERR_MALLOC; |
460 | } | |
461 | ||
b7e94111 | 462 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * |
2ac2e629 | 463 | (session->num_sources + 1)); |
9f45fb3a UH |
464 | if (!new_sources) { |
465 | sr_err("session: %s: new_sources malloc failed", __func__); | |
e0508e67 | 466 | return SR_ERR_MALLOC; |
9f45fb3a | 467 | } |
544a4582 | 468 | |
b7e94111 LPC |
469 | new_pollfds[session->num_sources] = *pollfd; |
470 | s = &new_sources[session->num_sources++]; | |
544a4582 | 471 | s->timeout = timeout; |
d08490aa | 472 | s->cb = cb; |
1f9813eb | 473 | s->cb_data = cb_data; |
aac0ea25 | 474 | s->poll_object = poll_object; |
b7e94111 LPC |
475 | session->pollfds = new_pollfds; |
476 | session->sources = new_sources; | |
544a4582 | 477 | |
b7e94111 LPC |
478 | if (timeout != session->source_timeout && timeout > 0 |
479 | && (session->source_timeout == -1 || timeout < session->source_timeout)) | |
480 | session->source_timeout = timeout; | |
9f45fb3a | 481 | |
e0508e67 | 482 | return SR_OK; |
544a4582 BV |
483 | } |
484 | ||
9f45fb3a | 485 | /** |
aac0ea25 | 486 | * Add a event source for a file descriptor. |
9f45fb3a | 487 | * |
aac0ea25 LPC |
488 | * @param fd The file descriptor. |
489 | * @param events Events to check for. | |
490 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
491 | * @param cb Callback function to add. Must not be NULL. | |
492 | * @param cb_data Data for the callback function. Can be NULL. | |
9f45fb3a | 493 | * |
aac0ea25 LPC |
494 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
495 | * SR_ERR_MALLOC upon memory allocation errors. | |
496 | */ | |
497 | SR_API int sr_session_source_add(int fd, int events, int timeout, | |
498 | sr_receive_data_callback_t cb, void *cb_data) | |
499 | { | |
500 | GPollFD p; | |
501 | ||
aac0ea25 LPC |
502 | p.fd = fd; |
503 | p.events = events; | |
aac0ea25 LPC |
504 | |
505 | return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)fd); | |
506 | } | |
507 | ||
508 | /** | |
1a895c61 | 509 | * Add an event source for a GPollFD. |
aac0ea25 LPC |
510 | * |
511 | * TODO: More error checks etc. | |
512 | * | |
513 | * @param pollfd The GPollFD. | |
514 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
515 | * @param cb Callback function to add. Must not be NULL. | |
516 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 517 | * |
e0508e67 | 518 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
aac0ea25 | 519 | * SR_ERR_MALLOC upon memory allocation errors. |
9f45fb3a | 520 | */ |
aac0ea25 LPC |
521 | SR_API int sr_session_source_add_pollfd(GPollFD *pollfd, int timeout, |
522 | sr_receive_data_callback_t cb, void *cb_data) | |
523 | { | |
1a895c61 UH |
524 | return _sr_session_source_add(pollfd, timeout, cb, |
525 | cb_data, (gintptr)pollfd); | |
aac0ea25 LPC |
526 | } |
527 | ||
528 | /** | |
1a895c61 | 529 | * Add an event source for a GIOChannel. |
aac0ea25 LPC |
530 | * |
531 | * TODO: More error checks etc. | |
532 | * | |
533 | * @param channel The GIOChannel. | |
534 | * @param events Events to poll on. | |
535 | * @param timeout Max time to wait before the callback is called, ignored if 0. | |
536 | * @param cb Callback function to add. Must not be NULL. | |
537 | * @param cb_data Data for the callback function. Can be NULL. | |
538 | * | |
539 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
540 | * SR_ERR_MALLOC upon memory allocation errors. | |
541 | */ | |
1a895c61 UH |
542 | SR_API int sr_session_source_add_channel(GIOChannel *channel, int events, |
543 | int timeout, sr_receive_data_callback_t cb, void *cb_data) | |
aac0ea25 LPC |
544 | { |
545 | GPollFD p; | |
546 | ||
547 | #ifdef _WIN32 | |
548 | g_io_channel_win32_make_pollfd(channel, | |
549 | events, &p); | |
550 | #else | |
551 | p.fd = g_io_channel_unix_get_fd(channel); | |
552 | p.events = events; | |
553 | #endif | |
554 | ||
555 | return _sr_session_source_add(&p, timeout, cb, cb_data, (gintptr)channel); | |
556 | } | |
557 | ||
558 | ||
559 | static int _sr_session_source_remove(gintptr poll_object) | |
544a4582 BV |
560 | { |
561 | struct source *new_sources; | |
0687dfcd | 562 | GPollFD *new_pollfds; |
b7e94111 | 563 | unsigned int old; |
544a4582 | 564 | |
b7e94111 | 565 | if (!session->sources || !session->num_sources) { |
e0508e67 | 566 | sr_err("session: %s: sources was NULL", __func__); |
0abee507 | 567 | return SR_ERR_BUG; |
e0508e67 UH |
568 | } |
569 | ||
b7e94111 LPC |
570 | for (old = 0; old < session->num_sources; old++) { |
571 | if (session->sources[old].poll_object == poll_object) | |
2bccd322 | 572 | break; |
9f45fb3a UH |
573 | } |
574 | ||
2bccd322 | 575 | /* fd not found, nothing to do */ |
b7e94111 | 576 | if (old == session->num_sources) |
2bccd322 LPC |
577 | return SR_OK; |
578 | ||
b7e94111 | 579 | session->num_sources -= 1; |
2bccd322 | 580 | |
b7e94111 LPC |
581 | if (old != session->num_sources) { |
582 | memmove(&session->pollfds[old], &session->pollfds[old+1], | |
583 | (session->num_sources - old) * sizeof(GPollFD)); | |
584 | memmove(&session->sources[old], &session->sources[old+1], | |
585 | (session->num_sources - old) * sizeof(struct source)); | |
9f45fb3a | 586 | } |
544a4582 | 587 | |
b7e94111 LPC |
588 | new_pollfds = g_try_realloc(session->pollfds, sizeof(GPollFD) * session->num_sources); |
589 | if (!new_pollfds && session->num_sources > 0) { | |
1a895c61 | 590 | sr_err("session: %s: new_pollfds malloc failed", __func__); |
0687dfcd LPC |
591 | return SR_ERR_MALLOC; |
592 | } | |
593 | ||
b7e94111 LPC |
594 | new_sources = g_try_realloc(session->sources, sizeof(struct source) * session->num_sources); |
595 | if (!new_sources && session->num_sources > 0) { | |
2bccd322 LPC |
596 | sr_err("session: %s: new_sources malloc failed", __func__); |
597 | return SR_ERR_MALLOC; | |
544a4582 | 598 | } |
e0508e67 | 599 | |
b7e94111 LPC |
600 | session->pollfds = new_pollfds; |
601 | session->sources = new_sources; | |
2bccd322 | 602 | |
e0508e67 | 603 | return SR_OK; |
544a4582 | 604 | } |
aac0ea25 LPC |
605 | |
606 | /* | |
607 | * Remove the source belonging to the specified file descriptor. | |
608 | * | |
609 | * TODO: More error checks. | |
610 | * | |
1a895c61 | 611 | * @param fd The file descriptor for which the source should be removed. |
aac0ea25 LPC |
612 | * |
613 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
614 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
615 | * internal errors. | |
616 | */ | |
617 | SR_API int sr_session_source_remove(int fd) | |
618 | { | |
619 | return _sr_session_source_remove((gintptr)fd); | |
620 | } | |
621 | ||
622 | /** | |
623 | * Remove the source belonging to the specified poll descriptor. | |
624 | * | |
625 | * TODO: More error checks. | |
626 | * | |
627 | * @param pollfd The poll descriptor for which the source should be removed. | |
628 | * | |
629 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
630 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
631 | * internal errors. | |
632 | */ | |
633 | SR_API int sr_session_source_remove_pollfd(GPollFD *pollfd) | |
634 | { | |
635 | return _sr_session_source_remove((gintptr)pollfd); | |
636 | } | |
637 | ||
638 | /* | |
639 | * Remove the source belonging to the specified channel. | |
640 | * | |
641 | * TODO: More error checks. | |
642 | * | |
1a895c61 | 643 | * @param channel The channel for which the source should be removed. |
aac0ea25 LPC |
644 | * |
645 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
646 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
647 | * internal errors. | |
648 | */ | |
649 | SR_API int sr_session_source_remove_channel(GIOChannel *channel) | |
650 | { | |
651 | return _sr_session_source_remove((gintptr)channel); | |
652 | } |