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