]> sigrok.org Git - libsigrok.git/blame - session.c
autogen.sh: aclocal support for Windows XP/Vista/7.
[libsigrok.git] / session.c
CommitLineData
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
9f4bc44e
UH
28/* demo.c */
29extern GIOChannel channels[2];
a1bb33af 30
544a4582
BV
31struct 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 41struct sr_session *session;
a0ecd83b 42static int num_sources = 0;
544a4582 43
a0ecd83b
UH
44static struct source *sources = NULL;
45static 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 58SR_API struct sr_session *sr_session_new(void)
a1bb33af 59{
9f45fb3a
UH
60 if (!(session = calloc(1, sizeof(struct sr_session)))) {
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 75SR_API int sr_session_destroy(void)
a1bb33af 76{
9f45fb3a
UH
77 if (!session) {
78 sr_warn("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 103SR_API int sr_session_device_clear(void)
a1bb33af 104{
9f45fb3a
UH
105 if (!session) {
106 sr_warn("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 125SR_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 167SR_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 186SR_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 204static 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) {
211 if (fds)
212 free(fds);
213
214 /* Construct g_poll()'s array. */
9f45fb3a 215 /* TODO: Check malloc return value. */
544a4582 216 fds = malloc(sizeof(GPollFD) * num_sources);
e0508e67
UH
217 if (!fds) {
218 sr_err("session: %s: fds malloc failed", __func__);
219 return SR_ERR_MALLOC;
220 }
544a4582
BV
221 for (i = 0; i < num_sources; i++) {
222#ifdef _WIN32
223 g_io_channel_win32_make_pollfd(&channels[0],
224 sources[i].events, &my_gpollfd);
225#else
226 my_gpollfd.fd = sources[i].fd;
227 my_gpollfd.events = sources[i].events;
228 fds[i] = my_gpollfd;
229#endif
230 }
231
232 ret = g_poll(fds, num_sources, source_timeout);
233
234 for (i = 0; i < num_sources; i++) {
235 if (fds[i].revents > 0 || (ret == 0
236 && source_timeout == sources[i].timeout)) {
237 /*
238 * Invoke the source's callback on an event,
239 * or if the poll timeout out and this source
240 * asked for that timeout.
241 */
5c582d9f
GM
242 if (!sources[i].cb(fds[i].fd, fds[i].revents,
243 sources[i].user_data))
244 sr_session_source_remove(sources[i].fd);
544a4582
BV
245 }
246 }
247 }
248 free(fds);
e0508e67
UH
249
250 return SR_OK;
544a4582
BV
251}
252
9f45fb3a
UH
253/**
254 * Start a session.
255 *
256 * There can only be one session at a time. TODO
257 *
258 * @return SR_OK upon success, SR_ERR upon errors.
259 */
1a081ca6 260SR_API int sr_session_start(void)
7d658874
BV
261{
262 struct sr_device *device;
263 GSList *l;
264 int ret;
265
9f45fb3a
UH
266 if (!session) {
267 sr_err("session: %s: session was NULL; a session must be "
268 "created first, before starting it.", __func__);
269 return SR_ERR; /* TODO: SR_ERR_BUG? */
270 }
271
272 if (!session->devices) {
273 /* TODO: Actually the case? */
274 sr_err("session: %s: session->devices was NULL; a session "
275 "cannot be started without devices.", __func__);
276 return SR_ERR; /* TODO: SR_ERR_BUG? */
277 }
278
279 /* TODO: Check plugin_index validity? */
280
b08024a8 281 sr_info("session: starting");
9f45fb3a 282
7d658874
BV
283 for (l = session->devices; l; l = l->next) {
284 device = l->data;
9f45fb3a 285 /* TODO: Check for device != NULL. */
7d658874 286 if ((ret = device->plugin->start_acquisition(
9f45fb3a 287 device->plugin_index, device)) != SR_OK) {
446a0372 288 sr_err("session: %s: could not start an acquisition "
9f45fb3a 289 "(%d)", __func__, ret);
7d658874 290 break;
9f45fb3a 291 }
7d658874
BV
292 }
293
9f45fb3a
UH
294 /* TODO: What if there are multiple devices? Which return code? */
295
7d658874
BV
296 return ret;
297}
298
9f45fb3a
UH
299/**
300 * Run the session.
301 *
9f45fb3a
UH
302 * TODO: Various error checks etc.
303 *
e0508e67 304 * @return SR_OK upon success, SR_ERR_BUG upon errors.
9f45fb3a 305 */
1a081ca6 306SR_API int sr_session_run(void)
7d658874 307{
9f45fb3a
UH
308 if (!session) {
309 sr_err("session: %s: session was NULL; a session must be "
310 "created first, before running it.", __func__);
e0508e67 311 return SR_ERR_BUG;
9f45fb3a
UH
312 }
313
314 if (!session->devices) {
315 /* TODO: Actually the case? */
316 sr_err("session: %s: session->devices was NULL; a session "
317 "cannot be run without devices.", __func__);
e0508e67 318 return SR_ERR_BUG;
9f45fb3a
UH
319 }
320
b08024a8 321 sr_info("session: running");
7d658874
BV
322 session->running = TRUE;
323
9f45fb3a
UH
324 /* Do we have real sources? */
325 if (num_sources == 1 && sources[0].fd == -1) {
326 /* Dummy source, freewheel over it. */
7d658874
BV
327 while (session->running)
328 sources[0].cb(-1, 0, sources[0].user_data);
9f45fb3a
UH
329 } else {
330 /* Real sources, use g_poll() main loop. */
8a2efef2 331 sr_session_run_poll();
9f45fb3a
UH
332 }
333
e0508e67 334 return SR_OK;
7d658874
BV
335}
336
9f45fb3a
UH
337/**
338 * Halt the current session.
339 *
9f45fb3a 340 * TODO.
e0508e67
UH
341 *
342 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 343 */
1a081ca6 344SR_API int sr_session_halt(void)
544a4582 345{
9f45fb3a
UH
346 if (!session) {
347 sr_err("session: %s: session was NULL", __func__);
e0508e67 348 return SR_ERR_BUG;
9f45fb3a
UH
349 }
350
b08024a8 351 sr_info("session: halting");
544a4582 352 session->running = FALSE;
9f45fb3a 353
e0508e67 354 return SR_OK;
544a4582
BV
355}
356
9f45fb3a
UH
357/**
358 * Stop the current session.
359 *
360 * TODO: Difference to halt?
361 *
e0508e67 362 * @return SR_OK upon success, SR_ERR_BUG if no session exists.
9f45fb3a 363 */
1a081ca6 364SR_API int sr_session_stop(void)
a1bb33af 365{
5c2d46d1 366 struct sr_device *device;
a1bb33af
UH
367 GSList *l;
368
9f45fb3a
UH
369 if (!session) {
370 sr_err("session: %s: session was NULL", __func__);
e0508e67 371 return SR_ERR_BUG;
9f45fb3a
UH
372 }
373
b08024a8 374 sr_info("session: stopping");
544a4582 375 session->running = FALSE;
e0508e67 376
62c82025 377 for (l = session->devices; l; l = l->next) {
a1bb33af 378 device = l->data;
e0508e67 379 /* Check for device != NULL. */
8c76be53
BV
380 if (device->plugin) {
381 if (device->plugin->stop_acquisition)
382 device->plugin->stop_acquisition(device->plugin_index, device);
383 if (device->plugin->cleanup)
384 device->plugin->cleanup();
385 }
a1bb33af 386 }
9f45fb3a 387
e0508e67 388 return SR_OK;
a1bb33af
UH
389}
390
9f45fb3a
UH
391/**
392 * TODO.
393 *
9f45fb3a
UH
394 * TODO: Various error checks.
395 *
396 * @param packet TODO.
e0508e67 397 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
9f45fb3a 398 */
e0508e67 399static int datafeed_dump(struct sr_datafeed_packet *packet)
7d2afd6c
BV
400{
401 struct sr_datafeed_logic *logic;
402
e0508e67
UH
403 if (!packet) {
404 sr_err("session: %s: packet was NULL", __func__);
405 return SR_ERR_ARG;
406 }
407
408 /* TODO: Validity checks for packet contents. */
409
7d2afd6c
BV
410 switch (packet->type) {
411 case SR_DF_HEADER:
412 sr_dbg("bus: received SR_DF_HEADER");
413 break;
414 case SR_DF_TRIGGER:
01469707 415 sr_dbg("bus: received SR_DF_TRIGGER");
7d2afd6c
BV
416 break;
417 case SR_DF_LOGIC:
418 logic = packet->payload;
e0508e67 419 /* TODO: Check for logic != NULL. */
01469707 420 sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
7d2afd6c
BV
421 break;
422 case SR_DF_END:
423 sr_dbg("bus: received SR_DF_END");
424 break;
425 default:
e0508e67
UH
426 /* TODO: Abort? */
427 sr_err("bus: received unknown packet type %d", packet->type);
9f45fb3a 428 break;
7d2afd6c 429 }
e0508e67
UH
430
431 return SR_OK;
7d2afd6c
BV
432}
433
9f45fb3a
UH
434/**
435 * TODO.
436 *
9f45fb3a
UH
437 * @param device TODO.
438 * @param packet TODO.
e0508e67 439 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
9f45fb3a 440 */
1a081ca6
UH
441SR_API int sr_session_bus(struct sr_device *device,
442 struct sr_datafeed_packet *packet)
a1bb33af
UH
443{
444 GSList *l;
13b05733 445 sr_datafeed_callback cb;
a1bb33af 446
9f45fb3a
UH
447 if (!device) {
448 sr_err("session: %s: device was NULL", __func__);
e0508e67 449 return SR_ERR_ARG;
9f45fb3a
UH
450 }
451
452 if (!device->plugin) {
453 sr_err("session: %s: device->plugin was NULL", __func__);
e0508e67
UH
454 return SR_ERR_ARG;
455 }
456
457 if (!packet) {
458 sr_err("session: %s: packet was NULL", __func__);
459 return SR_ERR_ARG;
9f45fb3a
UH
460 }
461
62c82025 462 /*
aa4b1107 463 * TODO: Send packet through PD pipe, and send the output of that to
62c82025 464 * the callbacks as well.
a1bb33af 465 */
62c82025 466 for (l = session->datafeed_callbacks; l; l = l->next) {
a1bb33af 467 cb = l->data;
9f45fb3a 468 /* TODO: Check for cb != NULL. */
7d2afd6c 469 datafeed_dump(packet);
a1bb33af
UH
470 cb(device, packet);
471 }
9f45fb3a 472
e0508e67 473 return SR_OK;
a1bb33af
UH
474}
475
9f45fb3a
UH
476/**
477 * TODO.
478 *
9f45fb3a
UH
479 * TODO: Switch to g_try_malloc0() / g_free().
480 * TODO: More error checks etc.
481 *
482 * @param fd TODO.
483 * @param events TODO.
484 * @param timeout TODO.
485 * @param callback TODO.
486 * @param user_data TODO.
e0508e67
UH
487 * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
488 * SR_ERR_MALLOC upon memory allocation errors.
9f45fb3a 489 */
1a081ca6
UH
490SR_API int sr_session_source_add(int fd, int events, int timeout,
491 sr_receive_data_callback callback, void *user_data)
544a4582
BV
492{
493 struct source *new_sources, *s;
494
9f45fb3a
UH
495 if (!callback) {
496 sr_err("session: %s: callback was NULL", __func__);
e0508e67 497 return SR_ERR_ARG;
9f45fb3a
UH
498 }
499
500 /* Note: user_data can be NULL, that's not a bug. */
501
544a4582 502 new_sources = calloc(1, sizeof(struct source) * (num_sources + 1));
9f45fb3a
UH
503 if (!new_sources) {
504 sr_err("session: %s: new_sources malloc failed", __func__);
e0508e67 505 return SR_ERR_MALLOC;
9f45fb3a 506 }
544a4582
BV
507
508 if (sources) {
509 memcpy(new_sources, sources,
510 sizeof(struct source) * num_sources);
511 free(sources);
512 }
513
514 s = &new_sources[num_sources++];
515 s->fd = fd;
516 s->events = events;
517 s->timeout = timeout;
518 s->cb = callback;
519 s->user_data = user_data;
520 sources = new_sources;
521
522 if (timeout != source_timeout && timeout > 0
523 && (source_timeout == -1 || timeout < source_timeout))
524 source_timeout = timeout;
9f45fb3a 525
e0508e67 526 return SR_OK;
544a4582
BV
527}
528
9f45fb3a
UH
529/**
530 * Remove the source belonging to the specified file descriptor.
531 *
9f45fb3a
UH
532 * TODO: More error checks.
533 * TODO: Switch to g_try_malloc0() / g_free().
534 *
535 * @param fd TODO.
e0508e67
UH
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.
9f45fb3a 539 */
1a081ca6 540SR_API int sr_session_source_remove(int fd)
544a4582
BV
541{
542 struct source *new_sources;
543 int old, new;
544
e0508e67
UH
545 if (!sources) {
546 sr_err("session: %s: sources was NULL", __func__);
547 return SR_ERR_BUG; /* TODO: Other? */
548 }
549
550 /* TODO: Check if 'fd' valid. */
544a4582
BV
551
552 new_sources = calloc(1, sizeof(struct source) * num_sources);
9f45fb3a
UH
553 if (!new_sources) {
554 sr_err("session: %s: new_sources malloc failed", __func__);
e0508e67 555 return SR_ERR_MALLOC;
9f45fb3a
UH
556 }
557
558 for (old = 0, new = 0; old < num_sources; old++) {
544a4582
BV
559 if (sources[old].fd != fd)
560 memcpy(&new_sources[new++], &sources[old],
561 sizeof(struct source));
9f45fb3a 562 }
544a4582
BV
563
564 if (old != new) {
565 free(sources);
566 sources = new_sources;
567 num_sources--;
568 } else {
569 /* Target fd was not found. */
570 free(new_sources);
571 }
e0508e67
UH
572
573 return SR_OK;
544a4582 574}