2010-04-02 18:18:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the sigrok project.
|
|
|
|
*
|
2012-02-13 13:31:51 +00:00
|
|
|
* Copyright (C) 2010-2012 Bert Vermeulen <bert@biot.com>
|
2010-04-02 18:18:27 +00:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <string.h>
|
2011-01-30 01:40:55 +00:00
|
|
|
#include <glib.h>
|
2011-12-28 22:07:08 +00:00
|
|
|
#include "sigrok.h"
|
|
|
|
#include "sigrok-internal.h"
|
2011-01-24 06:46:16 +00:00
|
|
|
|
2012-02-05 12:36:03 +00:00
|
|
|
/* demo.c. TODO: Should not be global! */
|
|
|
|
extern SR_PRIV GIOChannel channels[2];
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2011-01-30 01:40:55 +00:00
|
|
|
struct source {
|
|
|
|
int fd;
|
|
|
|
int events;
|
|
|
|
int timeout;
|
2012-02-29 20:56:24 +00:00
|
|
|
sr_receive_data_callback_t cb;
|
2012-02-29 21:32:34 +00:00
|
|
|
void *cb_data;
|
2011-01-30 01:40:55 +00:00
|
|
|
};
|
|
|
|
|
2011-01-31 21:34:14 +00:00
|
|
|
/* There can only be one session at a time. */
|
2011-12-28 21:55:21 +00:00
|
|
|
/* 'session' is not static, it's used elsewhere (via 'extern'). */
|
2011-02-08 16:50:29 +00:00
|
|
|
struct sr_session *session;
|
2011-12-28 21:55:21 +00:00
|
|
|
static int num_sources = 0;
|
2011-01-30 01:40:55 +00:00
|
|
|
|
2011-12-28 21:55:21 +00:00
|
|
|
static struct source *sources = NULL;
|
|
|
|
static int source_timeout = -1;
|
2011-01-30 01:40:55 +00:00
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Create a new session.
|
|
|
|
*
|
|
|
|
* TODO: Should it use the file-global "session" variable or take an argument?
|
|
|
|
* The same question applies to all the other session functions.
|
|
|
|
*
|
|
|
|
* @return A pointer to the newly allocated session, or NULL upon errors.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API struct sr_session *sr_session_new(void)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-02-11 19:06:46 +00:00
|
|
|
if (!(session = g_try_malloc0(sizeof(struct sr_session)))) {
|
2012-01-02 13:15:25 +00:00
|
|
|
sr_err("session: %s: session malloc failed", __func__);
|
|
|
|
return NULL; /* TODO: SR_ERR_MALLOC? */
|
|
|
|
}
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Destroy the current session.
|
|
|
|
*
|
|
|
|
* This frees up all memory used by the session.
|
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_BUG if no session exists.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_session_destroy(void)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!session) {
|
2012-02-11 19:06:46 +00:00
|
|
|
sr_err("session: %s: session was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
g_slist_free(session->devs);
|
|
|
|
session->devs = NULL;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/* TODO: Error checks needed? */
|
|
|
|
|
2011-01-24 06:46:16 +00:00
|
|
|
/* TODO: Loop over protocol decoders and free them. */
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
g_free(session);
|
2012-01-02 13:15:25 +00:00
|
|
|
session = NULL;
|
2012-01-07 16:08:54 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Remove all the devices from the current session. TODO?
|
|
|
|
*
|
|
|
|
* The session itself (i.e., the struct sr_session) is not free'd and still
|
|
|
|
* exists after this function returns.
|
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_BUG if no session exists.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-17 21:25:01 +00:00
|
|
|
SR_API int sr_session_dev_clear(void)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!session) {
|
2012-02-11 19:06:46 +00:00
|
|
|
sr_err("session: %s: session was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
g_slist_free(session->devs);
|
|
|
|
session->devs = NULL;
|
2012-01-07 16:08:54 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Add a device to the current session.
|
|
|
|
*
|
2012-02-17 21:25:01 +00:00
|
|
|
* @param dev The device to add to the current session. Must not be NULL.
|
2012-02-28 22:52:30 +00:00
|
|
|
* Also, dev->driver and dev->driver->dev_open must not be NULL.
|
2012-01-02 13:15:25 +00:00
|
|
|
*
|
|
|
|
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
|
|
|
|
*/
|
2012-02-17 21:25:01 +00:00
|
|
|
SR_API int sr_session_dev_add(struct sr_dev *dev)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
if (!dev) {
|
|
|
|
sr_err("session: %s: dev was NULL", __func__);
|
2012-01-02 13:15:25 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
2012-02-28 22:52:30 +00:00
|
|
|
if (!dev->driver) {
|
|
|
|
sr_err("session: %s: dev->driver was NULL", __func__);
|
2012-01-02 13:15:25 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
2012-02-28 22:52:30 +00:00
|
|
|
if (!dev->driver->dev_open) {
|
|
|
|
sr_err("session: %s: dev->driver->dev_open was NULL",
|
2012-01-02 13:15:25 +00:00
|
|
|
__func__);
|
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!session) {
|
|
|
|
sr_err("session: %s: session was NULL", __func__);
|
2012-03-04 14:08:11 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 22:52:30 +00:00
|
|
|
if ((ret = dev->driver->dev_open(dev->driver_index)) != SR_OK) {
|
2012-02-18 10:57:43 +00:00
|
|
|
sr_err("session: %s: dev_open failed (%d)", __func__, ret);
|
2012-01-02 13:15:25 +00:00
|
|
|
return ret;
|
2011-01-24 06:46:16 +00:00
|
|
|
}
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
session->devs = g_slist_append(session->devs, dev);
|
2011-01-24 06:46:16 +00:00
|
|
|
|
2011-01-29 15:23:12 +00:00
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Clear all datafeed callbacks in the current session.
|
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_BUG if no session exists.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_session_datafeed_callback_clear(void)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!session) {
|
|
|
|
sr_err("session: %s: session was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2010-04-02 18:18:27 +00:00
|
|
|
g_slist_free(session->datafeed_callbacks);
|
|
|
|
session->datafeed_callbacks = NULL;
|
2012-01-07 16:08:54 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Add a datafeed callback to the current session.
|
|
|
|
*
|
2012-02-29 20:56:24 +00:00
|
|
|
* @param cb Function to call when a chunk of data is received.
|
2012-03-04 14:08:11 +00:00
|
|
|
* Must not be NULL.
|
2012-02-13 02:36:32 +00:00
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_BUG if no session exists.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-29 20:56:24 +00:00
|
|
|
SR_API int sr_session_datafeed_callback_add(sr_datafeed_callback_t cb)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!session) {
|
|
|
|
sr_err("session: %s: session was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-03-04 14:08:11 +00:00
|
|
|
if (!cb) {
|
|
|
|
sr_err("session: %s: cb was NULL", __func__);
|
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
2012-01-02 13:15:25 +00:00
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
session->datafeed_callbacks =
|
2012-02-29 20:56:24 +00:00
|
|
|
g_slist_append(session->datafeed_callbacks, cb);
|
2012-01-07 16:08:54 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* TODO.
|
|
|
|
*/
|
2012-01-07 16:08:54 +00:00
|
|
|
static int sr_session_run_poll(void)
|
2011-01-30 01:40:55 +00:00
|
|
|
{
|
|
|
|
GPollFD *fds, my_gpollfd;
|
|
|
|
int ret, i;
|
|
|
|
|
|
|
|
fds = NULL;
|
|
|
|
while (session->running) {
|
2012-02-11 19:06:46 +00:00
|
|
|
/* TODO: Add comment. */
|
|
|
|
g_free(fds);
|
2011-01-30 01:40:55 +00:00
|
|
|
|
|
|
|
/* Construct g_poll()'s array. */
|
2012-02-11 19:06:46 +00:00
|
|
|
if (!(fds = g_try_malloc(sizeof(GPollFD) * num_sources))) {
|
2012-01-07 16:08:54 +00:00
|
|
|
sr_err("session: %s: fds malloc failed", __func__);
|
|
|
|
return SR_ERR_MALLOC;
|
|
|
|
}
|
2011-01-30 01:40:55 +00:00
|
|
|
for (i = 0; i < num_sources; i++) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
g_io_channel_win32_make_pollfd(&channels[0],
|
|
|
|
sources[i].events, &my_gpollfd);
|
|
|
|
#else
|
|
|
|
my_gpollfd.fd = sources[i].fd;
|
|
|
|
my_gpollfd.events = sources[i].events;
|
|
|
|
fds[i] = my_gpollfd;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = g_poll(fds, num_sources, source_timeout);
|
|
|
|
|
|
|
|
for (i = 0; i < num_sources; i++) {
|
|
|
|
if (fds[i].revents > 0 || (ret == 0
|
|
|
|
&& source_timeout == sources[i].timeout)) {
|
|
|
|
/*
|
|
|
|
* Invoke the source's callback on an event,
|
|
|
|
* or if the poll timeout out and this source
|
|
|
|
* asked for that timeout.
|
|
|
|
*/
|
2011-11-05 22:44:25 +00:00
|
|
|
if (!sources[i].cb(fds[i].fd, fds[i].revents,
|
2012-02-29 21:32:34 +00:00
|
|
|
sources[i].cb_data))
|
2011-11-05 22:44:25 +00:00
|
|
|
sr_session_source_remove(sources[i].fd);
|
2011-01-30 01:40:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(fds);
|
2012-01-07 16:08:54 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
2011-01-30 01:40:55 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Start a session.
|
|
|
|
*
|
2012-02-13 02:36:32 +00:00
|
|
|
* There can only be one session at a time.
|
2012-01-02 13:15:25 +00:00
|
|
|
*
|
|
|
|
* @return SR_OK upon success, SR_ERR upon errors.
|
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_session_start(void)
|
2011-01-31 21:34:14 +00:00
|
|
|
{
|
2012-02-17 21:25:01 +00:00
|
|
|
struct sr_dev *dev;
|
2011-01-31 21:34:14 +00:00
|
|
|
GSList *l;
|
|
|
|
int ret;
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!session) {
|
|
|
|
sr_err("session: %s: session was NULL; a session must be "
|
|
|
|
"created first, before starting it.", __func__);
|
2012-03-04 14:08:11 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
if (!session->devs) {
|
2012-01-02 13:15:25 +00:00
|
|
|
/* TODO: Actually the case? */
|
2012-02-17 21:25:01 +00:00
|
|
|
sr_err("session: %s: session->devs was NULL; a session "
|
2012-01-02 13:15:25 +00:00
|
|
|
"cannot be started without devices.", __func__);
|
2012-03-04 14:08:11 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 22:52:30 +00:00
|
|
|
/* TODO: Check driver_index validity? */
|
2012-01-02 13:15:25 +00:00
|
|
|
|
2011-04-14 07:46:53 +00:00
|
|
|
sr_info("session: starting");
|
2012-01-02 13:15:25 +00:00
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
for (l = session->devs; l; l = l->next) {
|
|
|
|
dev = l->data;
|
|
|
|
/* TODO: Check for dev != NULL. */
|
2012-02-28 22:52:30 +00:00
|
|
|
if ((ret = dev->driver->dev_acquisition_start(
|
|
|
|
dev->driver_index, dev)) != SR_OK) {
|
2012-01-06 00:04:29 +00:00
|
|
|
sr_err("session: %s: could not start an acquisition "
|
2012-01-02 13:15:25 +00:00
|
|
|
"(%d)", __func__, ret);
|
2011-01-31 21:34:14 +00:00
|
|
|
break;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
2011-01-31 21:34:14 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/* TODO: What if there are multiple devices? Which return code? */
|
|
|
|
|
2011-01-31 21:34:14 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Run the session.
|
|
|
|
*
|
|
|
|
* TODO: Various error checks etc.
|
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_BUG upon errors.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_session_run(void)
|
2011-01-31 21:34:14 +00:00
|
|
|
{
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!session) {
|
|
|
|
sr_err("session: %s: session was NULL; a session must be "
|
|
|
|
"created first, before running it.", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
if (!session->devs) {
|
2012-01-02 13:15:25 +00:00
|
|
|
/* TODO: Actually the case? */
|
2012-02-17 21:25:01 +00:00
|
|
|
sr_err("session: %s: session->devs was NULL; a session "
|
2012-01-02 13:15:25 +00:00
|
|
|
"cannot be run without devices.", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 07:46:53 +00:00
|
|
|
sr_info("session: running");
|
2011-01-31 21:34:14 +00:00
|
|
|
session->running = TRUE;
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/* Do we have real sources? */
|
|
|
|
if (num_sources == 1 && sources[0].fd == -1) {
|
|
|
|
/* Dummy source, freewheel over it. */
|
2011-01-31 21:34:14 +00:00
|
|
|
while (session->running)
|
2012-02-29 21:32:34 +00:00
|
|
|
sources[0].cb(-1, 0, sources[0].cb_data);
|
2012-01-02 13:15:25 +00:00
|
|
|
} else {
|
|
|
|
/* Real sources, use g_poll() main loop. */
|
2011-02-08 17:00:49 +00:00
|
|
|
sr_session_run_poll();
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_OK;
|
2011-01-31 21:34:14 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Halt the current session.
|
|
|
|
*
|
2012-02-17 19:44:19 +00:00
|
|
|
* This requests the current session be stopped as soon as possible, for
|
|
|
|
* example on receiving an SR_DF_END packet.
|
2012-01-07 16:08:54 +00:00
|
|
|
*
|
|
|
|
* @return SR_OK upon success, SR_ERR_BUG if no session exists.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_session_halt(void)
|
2011-01-30 01:40:55 +00:00
|
|
|
{
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!session) {
|
|
|
|
sr_err("session: %s: session was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 07:46:53 +00:00
|
|
|
sr_info("session: halting");
|
2011-01-30 01:40:55 +00:00
|
|
|
session->running = FALSE;
|
2012-01-02 13:15:25 +00:00
|
|
|
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_OK;
|
2011-01-30 01:40:55 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Stop the current session.
|
|
|
|
*
|
2012-02-13 02:36:32 +00:00
|
|
|
* The current session is stopped immediately, with all acquisition sessions
|
2012-02-28 22:52:30 +00:00
|
|
|
* being stopped and hardware drivers cleaned up.
|
2012-01-02 13:15:25 +00:00
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_BUG if no session exists.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_session_stop(void)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
2012-02-17 21:25:01 +00:00
|
|
|
struct sr_dev *dev;
|
2010-04-02 18:18:27 +00:00
|
|
|
GSList *l;
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!session) {
|
|
|
|
sr_err("session: %s: session was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2011-04-14 07:46:53 +00:00
|
|
|
sr_info("session: stopping");
|
2011-01-30 01:40:55 +00:00
|
|
|
session->running = FALSE;
|
2012-01-07 16:08:54 +00:00
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
for (l = session->devs; l; l = l->next) {
|
|
|
|
dev = l->data;
|
|
|
|
/* Check for dev != NULL. */
|
2012-02-28 22:52:30 +00:00
|
|
|
if (dev->driver) {
|
|
|
|
if (dev->driver->dev_acquisition_stop)
|
|
|
|
dev->driver->dev_acquisition_stop(dev->driver_index, dev);
|
|
|
|
if (dev->driver->cleanup)
|
|
|
|
dev->driver->cleanup();
|
2012-01-08 21:05:00 +00:00
|
|
|
}
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
2012-01-02 13:15:25 +00:00
|
|
|
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
2012-02-13 02:36:32 +00:00
|
|
|
* Debug helper.
|
2012-01-02 13:15:25 +00:00
|
|
|
*
|
2012-02-13 01:13:51 +00:00
|
|
|
* @param packet The packet to show debugging information for.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-12 23:08:23 +00:00
|
|
|
static void datafeed_dump(struct sr_datafeed_packet *packet)
|
2011-06-20 09:42:43 +00:00
|
|
|
{
|
|
|
|
struct sr_datafeed_logic *logic;
|
|
|
|
|
|
|
|
switch (packet->type) {
|
|
|
|
case SR_DF_HEADER:
|
|
|
|
sr_dbg("bus: received SR_DF_HEADER");
|
|
|
|
break;
|
|
|
|
case SR_DF_TRIGGER:
|
2012-02-01 01:59:41 +00:00
|
|
|
sr_dbg("bus: received SR_DF_TRIGGER");
|
2011-06-20 09:42:43 +00:00
|
|
|
break;
|
|
|
|
case SR_DF_LOGIC:
|
|
|
|
logic = packet->payload;
|
2012-01-07 16:08:54 +00:00
|
|
|
/* TODO: Check for logic != NULL. */
|
2012-02-01 01:59:41 +00:00
|
|
|
sr_dbg("bus: received SR_DF_LOGIC %" PRIu64 " bytes", logic->length);
|
2011-06-20 09:42:43 +00:00
|
|
|
break;
|
|
|
|
case SR_DF_END:
|
|
|
|
sr_dbg("bus: received SR_DF_END");
|
|
|
|
break;
|
|
|
|
default:
|
2012-02-12 23:08:23 +00:00
|
|
|
sr_dbg("bus: received unknown packet type %d", packet->type);
|
2012-01-02 13:15:25 +00:00
|
|
|
break;
|
2011-06-20 09:42:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
2012-02-13 02:36:32 +00:00
|
|
|
* Send a packet to whatever is listening on the datafeed bus.
|
|
|
|
*
|
|
|
|
* Hardware drivers use this to send a data packet to the frontend.
|
2012-01-02 13:15:25 +00:00
|
|
|
*
|
2012-02-17 21:25:01 +00:00
|
|
|
* @param dev TODO.
|
2012-02-29 21:08:45 +00:00
|
|
|
* @param packet The datafeed packet to send to the session bus.
|
2012-02-17 19:44:19 +00:00
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-29 21:08:45 +00:00
|
|
|
SR_PRIV int sr_session_send(struct sr_dev *dev,
|
|
|
|
struct sr_datafeed_packet *packet)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
GSList *l;
|
2012-02-29 20:56:24 +00:00
|
|
|
sr_datafeed_callback_t cb;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2012-02-17 21:25:01 +00:00
|
|
|
if (!dev) {
|
|
|
|
sr_err("session: %s: dev was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_ARG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-28 22:52:30 +00:00
|
|
|
if (!dev->driver) {
|
|
|
|
sr_err("session: %s: dev->driver was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!packet) {
|
|
|
|
sr_err("session: %s: packet was NULL", __func__);
|
|
|
|
return SR_ERR_ARG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
for (l = session->datafeed_callbacks; l; l = l->next) {
|
2012-02-12 23:08:23 +00:00
|
|
|
if (sr_log_loglevel_get() >= SR_LOG_DBG)
|
|
|
|
datafeed_dump(packet);
|
2010-04-02 18:18:27 +00:00
|
|
|
cb = l->data;
|
2012-01-02 13:15:25 +00:00
|
|
|
/* TODO: Check for cb != NULL. */
|
2012-02-17 21:25:01 +00:00
|
|
|
cb(dev, packet);
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
2012-01-02 13:15:25 +00:00
|
|
|
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_OK;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* TODO.
|
|
|
|
*
|
|
|
|
* TODO: More error checks etc.
|
|
|
|
*
|
|
|
|
* @param fd TODO.
|
|
|
|
* @param events TODO.
|
|
|
|
* @param timeout TODO.
|
2012-03-04 14:08:11 +00:00
|
|
|
* @param cb Callback function to add. Must not be NULL.
|
|
|
|
* @param cb_data Data for the callback function. Can be NULL.
|
2012-02-17 19:44:19 +00:00
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
|
|
|
|
* SR_ERR_MALLOC upon memory allocation errors.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_session_source_add(int fd, int events, int timeout,
|
2012-02-29 21:32:34 +00:00
|
|
|
sr_receive_data_callback_t cb, void *cb_data)
|
2011-01-30 01:40:55 +00:00
|
|
|
{
|
|
|
|
struct source *new_sources, *s;
|
|
|
|
|
2012-02-29 20:56:24 +00:00
|
|
|
if (!cb) {
|
|
|
|
sr_err("session: %s: cb was NULL", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_ARG;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
2012-02-29 21:32:34 +00:00
|
|
|
/* Note: cb_data can be NULL, that's not a bug. */
|
2012-01-02 13:15:25 +00:00
|
|
|
|
2012-02-11 19:06:46 +00:00
|
|
|
new_sources = g_try_malloc0(sizeof(struct source) * (num_sources + 1));
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!new_sources) {
|
|
|
|
sr_err("session: %s: new_sources malloc failed", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_MALLOC;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
2011-01-30 01:40:55 +00:00
|
|
|
|
|
|
|
if (sources) {
|
|
|
|
memcpy(new_sources, sources,
|
|
|
|
sizeof(struct source) * num_sources);
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(sources);
|
2011-01-30 01:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s = &new_sources[num_sources++];
|
|
|
|
s->fd = fd;
|
|
|
|
s->events = events;
|
|
|
|
s->timeout = timeout;
|
2012-02-29 20:56:24 +00:00
|
|
|
s->cb = cb;
|
2012-02-29 21:32:34 +00:00
|
|
|
s->cb_data = cb_data;
|
2011-01-30 01:40:55 +00:00
|
|
|
sources = new_sources;
|
|
|
|
|
|
|
|
if (timeout != source_timeout && timeout > 0
|
|
|
|
&& (source_timeout == -1 || timeout < source_timeout))
|
|
|
|
source_timeout = timeout;
|
2012-01-02 13:15:25 +00:00
|
|
|
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_OK;
|
2011-01-30 01:40:55 +00:00
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Remove the source belonging to the specified file descriptor.
|
|
|
|
*
|
|
|
|
* TODO: More error checks.
|
|
|
|
*
|
|
|
|
* @param fd TODO.
|
2012-02-17 19:44:19 +00:00
|
|
|
*
|
2012-01-07 16:08:54 +00:00
|
|
|
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
|
|
|
|
* SR_ERR_MALLOC upon memory allocation errors, SR_ERR_BUG upon
|
|
|
|
* internal errors.
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2012-02-01 22:40:35 +00:00
|
|
|
SR_API int sr_session_source_remove(int fd)
|
2011-01-30 01:40:55 +00:00
|
|
|
{
|
|
|
|
struct source *new_sources;
|
|
|
|
int old, new;
|
|
|
|
|
2012-01-07 16:08:54 +00:00
|
|
|
if (!sources) {
|
|
|
|
sr_err("session: %s: sources was NULL", __func__);
|
2012-03-04 14:08:11 +00:00
|
|
|
return SR_ERR_BUG;
|
2012-01-07 16:08:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: Check if 'fd' valid. */
|
2011-01-30 01:40:55 +00:00
|
|
|
|
2012-02-11 19:06:46 +00:00
|
|
|
new_sources = g_try_malloc0(sizeof(struct source) * num_sources);
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!new_sources) {
|
|
|
|
sr_err("session: %s: new_sources malloc failed", __func__);
|
2012-01-07 16:08:54 +00:00
|
|
|
return SR_ERR_MALLOC;
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for (old = 0, new = 0; old < num_sources; old++) {
|
2011-01-30 01:40:55 +00:00
|
|
|
if (sources[old].fd != fd)
|
|
|
|
memcpy(&new_sources[new++], &sources[old],
|
|
|
|
sizeof(struct source));
|
2012-01-02 13:15:25 +00:00
|
|
|
}
|
2011-01-30 01:40:55 +00:00
|
|
|
|
|
|
|
if (old != new) {
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(sources);
|
2011-01-30 01:40:55 +00:00
|
|
|
sources = new_sources;
|
|
|
|
num_sources--;
|
|
|
|
} else {
|
|
|
|
/* Target fd was not found. */
|
2012-02-11 19:06:46 +00:00
|
|
|
g_free(new_sources);
|
2011-01-30 01:40:55 +00:00
|
|
|
}
|
2012-01-07 16:08:54 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
2011-01-30 01:40:55 +00:00
|
|
|
}
|