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