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