]>
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> |
b7f09cf8 UH |
25 | #include "sigrok.h" |
26 | #include "sigrok-internal.h" | |
aa4b1107 | 27 | |
3d2efd7d UH |
28 | /* demo.c. TODO: Should not be global! */ |
29 | extern SR_PRIV GIOChannel channels[2]; | |
a1bb33af | 30 | |
544a4582 BV |
31 | struct source { |
32 | int fd; | |
33 | int events; | |
34 | int timeout; | |
d08490aa | 35 | sr_receive_data_callback_t cb; |
1f9813eb | 36 | void *cb_data; |
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; |
a0ecd83b | 42 | static int num_sources = 0; |
544a4582 | 43 | |
a0ecd83b UH |
44 | static struct source *sources = NULL; |
45 | static int source_timeout = -1; | |
544a4582 | 46 | |
9f45fb3a UH |
47 | /** |
48 | * Create a new session. | |
49 | * | |
9f45fb3a UH |
50 | * TODO: Should it use the file-global "session" variable or take an argument? |
51 | * The same question applies to all the other session functions. | |
52 | * | |
53 | * @return A pointer to the newly allocated session, or NULL upon errors. | |
54 | */ | |
1a081ca6 | 55 | SR_API struct sr_session *sr_session_new(void) |
a1bb33af | 56 | { |
133a37bf | 57 | if (!(session = g_try_malloc0(sizeof(struct sr_session)))) { |
9f45fb3a UH |
58 | sr_err("session: %s: session malloc failed", __func__); |
59 | return NULL; /* TODO: SR_ERR_MALLOC? */ | |
60 | } | |
a1bb33af UH |
61 | |
62 | return session; | |
63 | } | |
64 | ||
9f45fb3a UH |
65 | /** |
66 | * Destroy the current session. | |
67 | * | |
68 | * This frees up all memory used by the session. | |
69 | * | |
e0508e67 | 70 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 71 | */ |
1a081ca6 | 72 | SR_API int sr_session_destroy(void) |
a1bb33af | 73 | { |
9f45fb3a | 74 | if (!session) { |
133a37bf | 75 | sr_err("session: %s: session was NULL", __func__); |
e0508e67 | 76 | return SR_ERR_BUG; |
9f45fb3a UH |
77 | } |
78 | ||
bb7ef793 UH |
79 | g_slist_free(session->devs); |
80 | session->devs = NULL; | |
a1bb33af | 81 | |
9f45fb3a UH |
82 | /* TODO: Error checks needed? */ |
83 | ||
aa4b1107 | 84 | /* TODO: Loop over protocol decoders and free them. */ |
a1bb33af UH |
85 | |
86 | g_free(session); | |
9f45fb3a | 87 | session = NULL; |
e0508e67 UH |
88 | |
89 | return SR_OK; | |
a1bb33af UH |
90 | } |
91 | ||
9f45fb3a UH |
92 | /** |
93 | * Remove all the devices from the current session. TODO? | |
94 | * | |
95 | * The session itself (i.e., the struct sr_session) is not free'd and still | |
96 | * exists after this function returns. | |
97 | * | |
e0508e67 | 98 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 99 | */ |
01c3e9db | 100 | SR_API int sr_session_dev_remove_all(void) |
a1bb33af | 101 | { |
9f45fb3a | 102 | if (!session) { |
133a37bf | 103 | sr_err("session: %s: session was NULL", __func__); |
e0508e67 | 104 | return SR_ERR_BUG; |
9f45fb3a UH |
105 | } |
106 | ||
bb7ef793 UH |
107 | g_slist_free(session->devs); |
108 | session->devs = NULL; | |
e0508e67 UH |
109 | |
110 | return SR_OK; | |
a1bb33af UH |
111 | } |
112 | ||
9f45fb3a UH |
113 | /** |
114 | * Add a device to the current session. | |
115 | * | |
bb7ef793 | 116 | * @param dev The device to add to the current session. Must not be NULL. |
c09f0b57 | 117 | * Also, dev->driver and dev->driver->dev_open must not be NULL. |
9f45fb3a UH |
118 | * |
119 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. | |
120 | */ | |
bb7ef793 | 121 | SR_API int sr_session_dev_add(struct sr_dev *dev) |
a1bb33af UH |
122 | { |
123 | int ret; | |
124 | ||
bb7ef793 UH |
125 | if (!dev) { |
126 | sr_err("session: %s: dev was NULL", __func__); | |
9f45fb3a UH |
127 | return SR_ERR_ARG; |
128 | } | |
129 | ||
d6eb0c33 UH |
130 | if (!session) { |
131 | sr_err("session: %s: session was NULL", __func__); | |
132 | return SR_ERR_BUG; | |
133 | } | |
134 | ||
135 | /* If dev->driver is NULL, this is a virtual device. */ | |
c09f0b57 | 136 | if (!dev->driver) { |
d6eb0c33 UH |
137 | sr_dbg("session: %s: dev->driver was NULL, this seems to be " |
138 | "a virtual device; continuing", __func__); | |
139 | /* Just add the device, don't run dev_open(). */ | |
140 | session->devs = g_slist_append(session->devs, dev); | |
141 | return SR_OK; | |
9f45fb3a UH |
142 | } |
143 | ||
d6eb0c33 | 144 | /* dev->driver is non-NULL (i.e. we have a real device). */ |
c09f0b57 UH |
145 | if (!dev->driver->dev_open) { |
146 | sr_err("session: %s: dev->driver->dev_open was NULL", | |
9f45fb3a | 147 | __func__); |
8ec95d22 | 148 | return SR_ERR_BUG; |
9f45fb3a UH |
149 | } |
150 | ||
c09f0b57 | 151 | if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) { |
e7eb703f | 152 | sr_err("session: %s: dev_open failed (%d)", __func__, ret); |
9f45fb3a | 153 | return ret; |
aa4b1107 | 154 | } |
a1bb33af | 155 | |
bb7ef793 | 156 | session->devs = g_slist_append(session->devs, dev); |
aa4b1107 | 157 | |
e46b8fb1 | 158 | return SR_OK; |
a1bb33af UH |
159 | } |
160 | ||
9f45fb3a | 161 | /** |
01c3e9db | 162 | * Remove all datafeed callbacks in the current session. |
9f45fb3a | 163 | * |
e0508e67 | 164 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 165 | */ |
01c3e9db | 166 | SR_API int sr_session_datafeed_callback_remove_all(void) |
a1bb33af | 167 | { |
9f45fb3a UH |
168 | if (!session) { |
169 | sr_err("session: %s: session was NULL", __func__); | |
e0508e67 | 170 | return SR_ERR_BUG; |
9f45fb3a UH |
171 | } |
172 | ||
a1bb33af UH |
173 | g_slist_free(session->datafeed_callbacks); |
174 | session->datafeed_callbacks = NULL; | |
e0508e67 UH |
175 | |
176 | return SR_OK; | |
a1bb33af UH |
177 | } |
178 | ||
9f45fb3a UH |
179 | /** |
180 | * Add a datafeed callback to the current session. | |
181 | * | |
d08490aa | 182 | * @param cb Function to call when a chunk of data is received. |
0abee507 | 183 | * Must not be NULL. |
a1645fcd | 184 | * |
e0508e67 | 185 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 186 | */ |
d08490aa | 187 | SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb) |
a1bb33af | 188 | { |
9f45fb3a UH |
189 | if (!session) { |
190 | sr_err("session: %s: session was NULL", __func__); | |
e0508e67 | 191 | return SR_ERR_BUG; |
9f45fb3a UH |
192 | } |
193 | ||
0abee507 UH |
194 | if (!cb) { |
195 | sr_err("session: %s: cb was NULL", __func__); | |
196 | return SR_ERR_ARG; | |
197 | } | |
9f45fb3a | 198 | |
62c82025 | 199 | session->datafeed_callbacks = |
d08490aa | 200 | g_slist_append(session->datafeed_callbacks, cb); |
e0508e67 UH |
201 | |
202 | return SR_OK; | |
a1bb33af UH |
203 | } |
204 | ||
9f45fb3a UH |
205 | /** |
206 | * TODO. | |
207 | */ | |
e0508e67 | 208 | static int sr_session_run_poll(void) |
544a4582 BV |
209 | { |
210 | GPollFD *fds, my_gpollfd; | |
211 | int ret, i; | |
212 | ||
544a4582 BV |
213 | fds = NULL; |
214 | while (session->running) { | |
133a37bf UH |
215 | /* TODO: Add comment. */ |
216 | g_free(fds); | |
544a4582 BV |
217 | |
218 | /* Construct g_poll()'s array. */ | |
133a37bf | 219 | if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) { |
e0508e67 UH |
220 | sr_err("session: %s: fds malloc failed", __func__); |
221 | return SR_ERR_MALLOC; | |
222 | } | |
544a4582 BV |
223 | for (i = 0; i < num_sources; i++) { |
224 | #ifdef _WIN32 | |
225 | g_io_channel_win32_make_pollfd(&channels[0], | |
226 | sources[i].events, &my_gpollfd); | |
227 | #else | |
228 | my_gpollfd.fd = sources[i].fd; | |
229 | my_gpollfd.events = sources[i].events; | |
230 | fds[i] = my_gpollfd; | |
231 | #endif | |
232 | } | |
233 | ||
234 | ret = g_poll(fds, num_sources, source_timeout); | |
235 | ||
236 | for (i = 0; i < num_sources; i++) { | |
237 | if (fds[i].revents > 0 || (ret == 0 | |
238 | && source_timeout == sources[i].timeout)) { | |
239 | /* | |
240 | * Invoke the source's callback on an event, | |
241 | * or if the poll timeout out and this source | |
242 | * asked for that timeout. | |
243 | */ | |
5c582d9f | 244 | if (!sources[i].cb(fds[i].fd, fds[i].revents, |
1f9813eb | 245 | sources[i].cb_data)) |
5c582d9f | 246 | sr_session_source_remove(sources[i].fd); |
544a4582 BV |
247 | } |
248 | } | |
249 | } | |
133a37bf | 250 | g_free(fds); |
e0508e67 UH |
251 | |
252 | return SR_OK; | |
544a4582 BV |
253 | } |
254 | ||
9f45fb3a UH |
255 | /** |
256 | * Start a session. | |
257 | * | |
a1645fcd | 258 | * There can only be one session at a time. |
9f45fb3a UH |
259 | * |
260 | * @return SR_OK upon success, SR_ERR upon errors. | |
261 | */ | |
1a081ca6 | 262 | SR_API int sr_session_start(void) |
7d658874 | 263 | { |
bb7ef793 | 264 | struct sr_dev *dev; |
7d658874 BV |
265 | GSList *l; |
266 | int ret; | |
267 | ||
9f45fb3a UH |
268 | if (!session) { |
269 | sr_err("session: %s: session was NULL; a session must be " | |
270 | "created first, before starting it.", __func__); | |
0abee507 | 271 | return SR_ERR_BUG; |
9f45fb3a UH |
272 | } |
273 | ||
bb7ef793 | 274 | if (!session->devs) { |
9f45fb3a | 275 | /* TODO: Actually the case? */ |
bb7ef793 | 276 | sr_err("session: %s: session->devs was NULL; a session " |
9f45fb3a | 277 | "cannot be started without devices.", __func__); |
0abee507 | 278 | return SR_ERR_BUG; |
9f45fb3a UH |
279 | } |
280 | ||
c09f0b57 | 281 | /* TODO: Check driver_index validity? */ |
9f45fb3a | 282 | |
b08024a8 | 283 | sr_info("session: starting"); |
9f45fb3a | 284 | |
bb7ef793 UH |
285 | for (l = session->devs; l; l = l->next) { |
286 | dev = l->data; | |
287 | /* TODO: Check for dev != NULL. */ | |
c09f0b57 UH |
288 | if ((ret = dev->driver->dev_acquisition_start( |
289 | dev->driver_index, dev)) != SR_OK) { | |
446a0372 | 290 | sr_err("session: %s: could not start an acquisition " |
9f45fb3a | 291 | "(%d)", __func__, ret); |
7d658874 | 292 | break; |
9f45fb3a | 293 | } |
7d658874 BV |
294 | } |
295 | ||
9f45fb3a UH |
296 | /* TODO: What if there are multiple devices? Which return code? */ |
297 | ||
7d658874 BV |
298 | return ret; |
299 | } | |
300 | ||
9f45fb3a UH |
301 | /** |
302 | * Run the session. | |
303 | * | |
9f45fb3a UH |
304 | * TODO: Various error checks etc. |
305 | * | |
e0508e67 | 306 | * @return SR_OK upon success, SR_ERR_BUG upon errors. |
9f45fb3a | 307 | */ |
1a081ca6 | 308 | SR_API int sr_session_run(void) |
7d658874 | 309 | { |
9f45fb3a UH |
310 | if (!session) { |
311 | sr_err("session: %s: session was NULL; a session must be " | |
312 | "created first, before running it.", __func__); | |
e0508e67 | 313 | return SR_ERR_BUG; |
9f45fb3a UH |
314 | } |
315 | ||
bb7ef793 | 316 | if (!session->devs) { |
9f45fb3a | 317 | /* TODO: Actually the case? */ |
bb7ef793 | 318 | sr_err("session: %s: session->devs was NULL; a session " |
9f45fb3a | 319 | "cannot be run without devices.", __func__); |
e0508e67 | 320 | return SR_ERR_BUG; |
9f45fb3a UH |
321 | } |
322 | ||
b08024a8 | 323 | sr_info("session: running"); |
7d658874 BV |
324 | session->running = TRUE; |
325 | ||
9f45fb3a UH |
326 | /* Do we have real sources? */ |
327 | if (num_sources == 1 && sources[0].fd == -1) { | |
328 | /* Dummy source, freewheel over it. */ | |
7d658874 | 329 | while (session->running) |
1f9813eb | 330 | sources[0].cb(-1, 0, sources[0].cb_data); |
9f45fb3a UH |
331 | } else { |
332 | /* Real sources, use g_poll() main loop. */ | |
8a2efef2 | 333 | sr_session_run_poll(); |
9f45fb3a UH |
334 | } |
335 | ||
e0508e67 | 336 | return SR_OK; |
7d658874 BV |
337 | } |
338 | ||
9f45fb3a UH |
339 | /** |
340 | * Halt the current session. | |
341 | * | |
44dae539 UH |
342 | * This requests the current session be stopped as soon as possible, for |
343 | * example on receiving an SR_DF_END packet. | |
e0508e67 UH |
344 | * |
345 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. | |
9f45fb3a | 346 | */ |
1a081ca6 | 347 | SR_API int sr_session_halt(void) |
544a4582 | 348 | { |
9f45fb3a UH |
349 | if (!session) { |
350 | sr_err("session: %s: session was NULL", __func__); | |
e0508e67 | 351 | return SR_ERR_BUG; |
9f45fb3a UH |
352 | } |
353 | ||
b08024a8 | 354 | sr_info("session: halting"); |
544a4582 | 355 | session->running = FALSE; |
9f45fb3a | 356 | |
e0508e67 | 357 | return SR_OK; |
544a4582 BV |
358 | } |
359 | ||
9f45fb3a UH |
360 | /** |
361 | * Stop the current session. | |
362 | * | |
a1645fcd | 363 | * The current session is stopped immediately, with all acquisition sessions |
c09f0b57 | 364 | * being stopped and hardware drivers cleaned up. |
9f45fb3a | 365 | * |
e0508e67 | 366 | * @return SR_OK upon success, SR_ERR_BUG if no session exists. |
9f45fb3a | 367 | */ |
1a081ca6 | 368 | SR_API int sr_session_stop(void) |
a1bb33af | 369 | { |
bb7ef793 | 370 | struct sr_dev *dev; |
a1bb33af UH |
371 | GSList *l; |
372 | ||
9f45fb3a UH |
373 | if (!session) { |
374 | sr_err("session: %s: session was NULL", __func__); | |
e0508e67 | 375 | return SR_ERR_BUG; |
9f45fb3a UH |
376 | } |
377 | ||
b08024a8 | 378 | sr_info("session: stopping"); |
544a4582 | 379 | session->running = FALSE; |
e0508e67 | 380 | |
bb7ef793 UH |
381 | for (l = session->devs; l; l = l->next) { |
382 | dev = l->data; | |
383 | /* Check for dev != NULL. */ | |
c09f0b57 UH |
384 | if (dev->driver) { |
385 | if (dev->driver->dev_acquisition_stop) | |
386 | dev->driver->dev_acquisition_stop(dev->driver_index, dev); | |
387 | if (dev->driver->cleanup) | |
388 | dev->driver->cleanup(); | |
8c76be53 | 389 | } |
a1bb33af | 390 | } |
9f45fb3a | 391 | |
e0508e67 | 392 | return SR_OK; |
a1bb33af UH |
393 | } |
394 | ||
9f45fb3a | 395 | /** |
a1645fcd | 396 | * Debug helper. |
9f45fb3a | 397 | * |
996b0c72 | 398 | * @param packet The packet to show debugging information for. |
9f45fb3a | 399 | */ |
18beaeff | 400 | static void datafeed_dump(struct sr_datafeed_packet *packet) |
7d2afd6c BV |
401 | { |
402 | struct sr_datafeed_logic *logic; | |
ee7489d2 | 403 | struct sr_datafeed_analog *analog; |
7d2afd6c BV |
404 | |
405 | switch (packet->type) { | |
406 | case SR_DF_HEADER: | |
407 | sr_dbg("bus: received SR_DF_HEADER"); | |
408 | break; | |
409 | case SR_DF_TRIGGER: | |
01469707 | 410 | sr_dbg("bus: received SR_DF_TRIGGER"); |
7d2afd6c | 411 | break; |
ee7489d2 BV |
412 | case SR_DF_META_LOGIC: |
413 | sr_dbg("bus: received SR_DF_META_LOGIC"); | |
414 | break; | |
7d2afd6c BV |
415 | case SR_DF_LOGIC: |
416 | logic = packet->payload; | |
e0508e67 | 417 | /* TODO: Check for logic != NULL. */ |
01469707 | 418 | sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length); |
7d2afd6c | 419 | break; |
ee7489d2 BV |
420 | case SR_DF_META_ANALOG: |
421 | sr_dbg("bus: received SR_DF_META_LOGIC"); | |
422 | break; | |
423 | case SR_DF_ANALOG: | |
424 | analog = packet->payload; | |
425 | /* TODO: Check for analog != NULL. */ | |
426 | sr_dbg("bus: received SR_DF_ANALOG %d samples", analog->num_samples); | |
427 | break; | |
7d2afd6c BV |
428 | case SR_DF_END: |
429 | sr_dbg("bus: received SR_DF_END"); | |
430 | break; | |
6ea7669c BV |
431 | case SR_DF_FRAME_BEGIN: |
432 | sr_dbg("bus: received SR_DF_FRAME_BEGIN"); | |
433 | break; | |
434 | case SR_DF_FRAME_END: | |
435 | sr_dbg("bus: received SR_DF_FRAME_END"); | |
436 | break; | |
7d2afd6c | 437 | default: |
18beaeff | 438 | sr_dbg("bus: received unknown packet type %d", packet->type); |
9f45fb3a | 439 | break; |
7d2afd6c | 440 | } |
7d2afd6c BV |
441 | } |
442 | ||
9f45fb3a | 443 | /** |
a1645fcd BV |
444 | * Send a packet to whatever is listening on the datafeed bus. |
445 | * | |
446 | * Hardware drivers use this to send a data packet to the frontend. | |
9f45fb3a | 447 | * |
bb7ef793 | 448 | * @param dev TODO. |
31ccebc4 | 449 | * @param packet The datafeed packet to send to the session bus. |
44dae539 | 450 | * |
e0508e67 | 451 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
9f45fb3a | 452 | */ |
31ccebc4 UH |
453 | SR_PRIV int sr_session_send(struct sr_dev *dev, |
454 | struct sr_datafeed_packet *packet) | |
a1bb33af UH |
455 | { |
456 | GSList *l; | |
d08490aa | 457 | sr_datafeed_callback_t cb; |
a1bb33af | 458 | |
bb7ef793 UH |
459 | if (!dev) { |
460 | sr_err("session: %s: dev was NULL", __func__); | |
e0508e67 | 461 | return SR_ERR_ARG; |
9f45fb3a UH |
462 | } |
463 | ||
e0508e67 UH |
464 | if (!packet) { |
465 | sr_err("session: %s: packet was NULL", __func__); | |
466 | return SR_ERR_ARG; | |
9f45fb3a UH |
467 | } |
468 | ||
62c82025 | 469 | for (l = session->datafeed_callbacks; l; l = l->next) { |
18beaeff BV |
470 | if (sr_log_loglevel_get() >= SR_LOG_DBG) |
471 | datafeed_dump(packet); | |
a1bb33af | 472 | cb = l->data; |
9f45fb3a | 473 | /* TODO: Check for cb != NULL. */ |
bb7ef793 | 474 | cb(dev, packet); |
a1bb33af | 475 | } |
9f45fb3a | 476 | |
e0508e67 | 477 | return SR_OK; |
a1bb33af UH |
478 | } |
479 | ||
9f45fb3a UH |
480 | /** |
481 | * TODO. | |
482 | * | |
9f45fb3a UH |
483 | * TODO: More error checks etc. |
484 | * | |
485 | * @param fd TODO. | |
486 | * @param events TODO. | |
487 | * @param timeout TODO. | |
0abee507 UH |
488 | * @param cb Callback function to add. Must not be NULL. |
489 | * @param cb_data Data for the callback function. Can be NULL. | |
44dae539 | 490 | * |
e0508e67 UH |
491 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
492 | * SR_ERR_MALLOC upon memory allocation errors. | |
9f45fb3a | 493 | */ |
1a081ca6 | 494 | SR_API int sr_session_source_add(int fd, int events, int timeout, |
1f9813eb | 495 | sr_receive_data_callback_t cb, void *cb_data) |
544a4582 BV |
496 | { |
497 | struct source *new_sources, *s; | |
498 | ||
d08490aa UH |
499 | if (!cb) { |
500 | sr_err("session: %s: cb was NULL", __func__); | |
e0508e67 | 501 | return SR_ERR_ARG; |
9f45fb3a UH |
502 | } |
503 | ||
1f9813eb | 504 | /* Note: cb_data can be NULL, that's not a bug. */ |
9f45fb3a | 505 | |
133a37bf | 506 | new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1)); |
9f45fb3a UH |
507 | if (!new_sources) { |
508 | sr_err("session: %s: new_sources malloc failed", __func__); | |
e0508e67 | 509 | return SR_ERR_MALLOC; |
9f45fb3a | 510 | } |
544a4582 BV |
511 | |
512 | if (sources) { | |
513 | memcpy(new_sources, sources, | |
514 | sizeof(struct source) * num_sources); | |
133a37bf | 515 | g_free(sources); |
544a4582 BV |
516 | } |
517 | ||
518 | s = &new_sources[num_sources++]; | |
519 | s->fd = fd; | |
520 | s->events = events; | |
521 | s->timeout = timeout; | |
d08490aa | 522 | s->cb = cb; |
1f9813eb | 523 | s->cb_data = cb_data; |
544a4582 BV |
524 | sources = new_sources; |
525 | ||
526 | if (timeout != source_timeout && timeout > 0 | |
527 | && (source_timeout == -1 || timeout < source_timeout)) | |
528 | source_timeout = timeout; | |
9f45fb3a | 529 | |
e0508e67 | 530 | return SR_OK; |
544a4582 BV |
531 | } |
532 | ||
9f45fb3a UH |
533 | /** |
534 | * Remove the source belonging to the specified file descriptor. | |
535 | * | |
9f45fb3a | 536 | * TODO: More error checks. |
9f45fb3a UH |
537 | * |
538 | * @param fd TODO. | |
44dae539 | 539 | * |
e0508e67 UH |
540 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
541 | * SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon | |
542 | * internal errors. | |
9f45fb3a | 543 | */ |
1a081ca6 | 544 | SR_API int sr_session_source_remove(int fd) |
544a4582 BV |
545 | { |
546 | struct source *new_sources; | |
547 | int old, new; | |
548 | ||
e0508e67 UH |
549 | if (!sources) { |
550 | sr_err("session: %s: sources was NULL", __func__); | |
0abee507 | 551 | return SR_ERR_BUG; |
e0508e67 UH |
552 | } |
553 | ||
554 | /* TODO: Check if 'fd' valid. */ | |
544a4582 | 555 | |
133a37bf | 556 | new_sources = g_try_malloc0(sizeof(struct source) * num_sources); |
9f45fb3a UH |
557 | if (!new_sources) { |
558 | sr_err("session: %s: new_sources malloc failed", __func__); | |
e0508e67 | 559 | return SR_ERR_MALLOC; |
9f45fb3a UH |
560 | } |
561 | ||
562 | for (old = 0, new = 0; old < num_sources; old++) { | |
544a4582 BV |
563 | if (sources[old].fd != fd) |
564 | memcpy(&new_sources[new++], &sources[old], | |
565 | sizeof(struct source)); | |
9f45fb3a | 566 | } |
544a4582 BV |
567 | |
568 | if (old != new) { | |
133a37bf | 569 | g_free(sources); |
544a4582 BV |
570 | sources = new_sources; |
571 | num_sources--; | |
572 | } else { | |
573 | /* Target fd was not found. */ | |
133a37bf | 574 | g_free(new_sources); |
544a4582 | 575 | } |
e0508e67 UH |
576 | |
577 | return SR_OK; | |
544a4582 | 578 | } |