2010-04-02 18:18:27 +00:00
|
|
|
/*
|
|
|
|
* This file is part of the sigrok project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Bert Vermeulen <bert@biot.com>
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
#include <zip.h>
|
2010-04-15 18:16:53 +00:00
|
|
|
#include <sigrok.h>
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* There can only be one session at a time. */
|
2010-04-02 18:18:27 +00:00
|
|
|
struct session *session;
|
|
|
|
|
2010-04-09 20:18:46 +00:00
|
|
|
struct session *session_load(const char *filename)
|
2010-04-02 18:18:27 +00:00
|
|
|
{
|
|
|
|
struct session *session;
|
|
|
|
|
2011-01-13 22:50:34 +00:00
|
|
|
/* Avoid compiler warnings. */
|
|
|
|
filename = filename;
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* TODO: Implement. */
|
2010-04-02 18:18:27 +00:00
|
|
|
session = NULL;
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct session *session_new(void)
|
|
|
|
{
|
|
|
|
session = calloc(1, sizeof(struct session));
|
|
|
|
|
|
|
|
return session;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_destroy(void)
|
|
|
|
{
|
|
|
|
g_slist_free(session->devices);
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* TODO: Loop over protocols and free them. */
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
g_free(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_device_clear(void)
|
|
|
|
{
|
|
|
|
g_slist_free(session->devices);
|
|
|
|
session->devices = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_device_add(struct device *device)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = device->plugin->open(device->plugin_index);
|
2010-04-15 18:16:53 +00:00
|
|
|
if (ret == SIGROK_OK)
|
2010-04-02 18:18:27 +00:00
|
|
|
session->devices = g_slist_append(session->devices, device);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_pa_clear(void)
|
|
|
|
{
|
2010-04-15 18:16:53 +00:00
|
|
|
/*
|
|
|
|
* The protocols are pointers to the global set of PA plugins,
|
|
|
|
* so don't free them.
|
|
|
|
*/
|
2010-04-02 18:18:27 +00:00
|
|
|
g_slist_free(session->analyzers);
|
|
|
|
session->analyzers = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_pa_add(struct analyzer *an)
|
|
|
|
{
|
|
|
|
session->analyzers = g_slist_append(session->analyzers, an);
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_datafeed_callback_clear(void)
|
|
|
|
{
|
|
|
|
g_slist_free(session->datafeed_callbacks);
|
|
|
|
session->datafeed_callbacks = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_datafeed_callback_add(datafeed_callback callback)
|
|
|
|
{
|
2010-04-15 18:16:53 +00:00
|
|
|
session->datafeed_callbacks =
|
|
|
|
g_slist_append(session->datafeed_callbacks, callback);
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int session_start(void)
|
|
|
|
{
|
|
|
|
struct device *device;
|
|
|
|
GSList *l;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
g_message("starting acquisition");
|
2010-04-15 18:16:53 +00:00
|
|
|
for (l = session->devices; l; l = l->next) {
|
2010-04-02 18:18:27 +00:00
|
|
|
device = l->data;
|
2010-04-15 18:16:53 +00:00
|
|
|
if ((ret = device->plugin->start_acquisition(
|
|
|
|
device->plugin_index, device)) != SIGROK_OK)
|
2010-04-02 18:18:27 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_stop(void)
|
|
|
|
{
|
|
|
|
struct device *device;
|
|
|
|
GSList *l;
|
|
|
|
|
|
|
|
g_message("stopping acquisition");
|
2010-04-15 18:16:53 +00:00
|
|
|
for (l = session->devices; l; l = l->next) {
|
2010-04-02 18:18:27 +00:00
|
|
|
device = l->data;
|
|
|
|
device->plugin->stop_acquisition(device->plugin_index, device);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void session_bus(struct device *device, struct datafeed_packet *packet)
|
|
|
|
{
|
|
|
|
GSList *l;
|
|
|
|
datafeed_callback cb;
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/*
|
|
|
|
* TODO: Send packet through PA pipe, and send the output of that to
|
|
|
|
* the callbacks as well.
|
2010-04-02 18:18:27 +00:00
|
|
|
*/
|
2010-04-15 18:16:53 +00:00
|
|
|
for (l = session->datafeed_callbacks; l; l = l->next) {
|
2010-04-02 18:18:27 +00:00
|
|
|
cb = l->data;
|
|
|
|
cb(device, packet);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void make_metadata(char *filename)
|
|
|
|
{
|
|
|
|
GSList *l, *p;
|
|
|
|
struct device *device;
|
|
|
|
struct probe *probe;
|
|
|
|
FILE *f;
|
|
|
|
int devcnt;
|
|
|
|
|
|
|
|
f = fopen(filename, "wb");
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* General */
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* Devices */
|
2010-04-02 18:18:27 +00:00
|
|
|
devcnt = 1;
|
2010-04-15 18:16:53 +00:00
|
|
|
for (l = session->devices; l; l = l->next) {
|
2010-04-02 18:18:27 +00:00
|
|
|
device = l->data;
|
|
|
|
fprintf(f, "[device]\n");
|
|
|
|
fprintf(f, "driver = %s\n", device->plugin->name);
|
2010-04-15 18:16:53 +00:00
|
|
|
|
|
|
|
if (device->datastore)
|
2010-04-02 18:18:27 +00:00
|
|
|
fprintf(f, "capturefile = raw-%d\n", devcnt);
|
2010-04-15 18:16:53 +00:00
|
|
|
|
|
|
|
for (p = device->probes; p; p = p->next) {
|
2010-04-02 18:18:27 +00:00
|
|
|
probe = p->data;
|
2010-04-15 18:16:53 +00:00
|
|
|
if (probe->enabled) {
|
2010-04-02 18:18:27 +00:00
|
|
|
fprintf(f, "probe %d", probe->index);
|
2010-04-15 18:16:53 +00:00
|
|
|
if (probe->name)
|
2010-04-02 18:18:27 +00:00
|
|
|
fprintf(f, " name \"%s\"", probe->name);
|
2010-04-15 18:16:53 +00:00
|
|
|
if (probe->trigger)
|
|
|
|
fprintf(f, " trigger \"%s\"",
|
|
|
|
probe->trigger);
|
2010-04-02 18:18:27 +00:00
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
devcnt++;
|
|
|
|
}
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* TODO: Protocol analyzers */
|
2010-04-02 18:18:27 +00:00
|
|
|
|
|
|
|
fclose(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
int session_save(char *filename)
|
|
|
|
{
|
|
|
|
GSList *l, *d;
|
|
|
|
struct device *device;
|
|
|
|
struct datastore *ds;
|
|
|
|
struct zip *zipfile;
|
|
|
|
struct zip_source *src;
|
|
|
|
int bufcnt, devcnt, tmpfile, ret, error;
|
|
|
|
char version[1], rawname[16], metafile[32], *buf;
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* Quietly delete it first, libzip wants replace ops otherwise. */
|
2010-04-02 18:18:27 +00:00
|
|
|
unlink(filename);
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
if (!(zipfile = zip_open(filename, ZIP_CREATE, &error)))
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-02 18:18:27 +00:00
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* Version */
|
2010-04-02 18:18:27 +00:00
|
|
|
version[0] = '1';
|
2010-04-15 18:16:53 +00:00
|
|
|
if (!(src = zip_source_buffer(zipfile, version, 1, 0)))
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-15 18:16:53 +00:00
|
|
|
if (zip_add(zipfile, "version", src) == -1) {
|
|
|
|
g_message("error saving version into zipfile: %s",
|
|
|
|
zip_strerror(zipfile));
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* Metadata */
|
2010-04-02 18:18:27 +00:00
|
|
|
strcpy(metafile, "sigrok-meta-XXXXXX");
|
2010-04-15 18:16:53 +00:00
|
|
|
if ((tmpfile = g_mkstemp(metafile)) == -1)
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-02 18:18:27 +00:00
|
|
|
close(tmpfile);
|
|
|
|
make_metadata(metafile);
|
2010-04-15 18:16:53 +00:00
|
|
|
if (!(src = zip_source_file(zipfile, metafile, 0, -1)))
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-15 18:16:53 +00:00
|
|
|
if (zip_add(zipfile, "metadata", src) == -1)
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-02 18:18:27 +00:00
|
|
|
unlink(metafile);
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
/* Raw */
|
2010-04-02 18:18:27 +00:00
|
|
|
devcnt = 1;
|
2010-04-15 18:16:53 +00:00
|
|
|
for (l = session->devices; l; l = l->next) {
|
2010-04-02 18:18:27 +00:00
|
|
|
device = l->data;
|
|
|
|
ds = device->datastore;
|
2010-04-15 18:16:53 +00:00
|
|
|
if (ds) {
|
|
|
|
buf = malloc(ds->num_units * ds->ds_unitsize +
|
|
|
|
DATASTORE_CHUNKSIZE);
|
2010-04-02 18:18:27 +00:00
|
|
|
bufcnt = 0;
|
2010-04-15 18:16:53 +00:00
|
|
|
for (d = ds->chunklist; d; d = d->next) {
|
|
|
|
memcpy(buf + bufcnt, d->data,
|
|
|
|
DATASTORE_CHUNKSIZE);
|
2010-04-02 18:18:27 +00:00
|
|
|
bufcnt += DATASTORE_CHUNKSIZE;
|
|
|
|
}
|
2010-04-15 18:16:53 +00:00
|
|
|
if (!(src = zip_source_buffer(zipfile, buf,
|
|
|
|
ds->num_units * ds->ds_unitsize, TRUE)))
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-02 18:18:27 +00:00
|
|
|
snprintf(rawname, 15, "raw-%d", devcnt);
|
2010-04-15 18:16:53 +00:00
|
|
|
if (zip_add(zipfile, rawname, src) == -1)
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
devcnt++;
|
|
|
|
}
|
|
|
|
|
2010-04-15 18:16:53 +00:00
|
|
|
if ((ret = zip_close(zipfile)) == -1) {
|
2010-04-02 18:18:27 +00:00
|
|
|
g_message("error saving zipfile: %s", zip_strerror(zipfile));
|
2010-04-05 14:41:54 +00:00
|
|
|
return SIGROK_ERR;
|
2010-04-02 18:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return SIGROK_OK;
|
|
|
|
}
|