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