2011-01-31 21:34:14 +00:00
|
|
|
/*
|
2013-04-23 20:24:30 +00:00
|
|
|
* This file is part of the libsigrok project.
|
2011-01-31 21:34:14 +00:00
|
|
|
*
|
2013-03-24 10:21:00 +00:00
|
|
|
* Copyright (C) 2013 Bert Vermeulen <bert@biot.com>
|
2011-01-31 21:34:14 +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 <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <zip.h>
|
2013-12-11 09:49:01 +00:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <errno.h>
|
2011-01-31 21:34:14 +00:00
|
|
|
#include <glib.h>
|
2011-02-06 01:14:57 +00:00
|
|
|
#include <glib/gstdio.h>
|
2012-10-23 22:41:21 +00:00
|
|
|
#include "config.h" /* Needed for PACKAGE_VERSION and others. */
|
2012-07-04 22:55:07 +00:00
|
|
|
#include "libsigrok.h"
|
|
|
|
#include "libsigrok-internal.h"
|
2011-01-31 21:34:14 +00:00
|
|
|
|
2014-04-25 16:40:59 +00:00
|
|
|
/** @cond PRIVATE */
|
2013-12-23 03:38:35 +00:00
|
|
|
#define LOG_PREFIX "session-file"
|
2014-04-25 16:40:59 +00:00
|
|
|
/** @endcond */
|
2012-11-11 11:44:16 +00:00
|
|
|
|
2012-10-21 22:30:12 +00:00
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* Loading and saving libsigrok session files.
|
|
|
|
*/
|
|
|
|
|
2012-10-21 14:13:36 +00:00
|
|
|
/**
|
2012-10-21 21:43:41 +00:00
|
|
|
* @addtogroup grp_session
|
2012-10-21 14:13:36 +00:00
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
2011-02-08 16:50:29 +00:00
|
|
|
extern struct sr_session *session;
|
2012-02-28 22:52:30 +00:00
|
|
|
extern SR_PRIV struct sr_dev_driver session_driver;
|
2011-01-31 21:34:14 +00:00
|
|
|
|
2013-09-25 09:51:38 +00:00
|
|
|
/** @private */
|
2013-09-18 11:28:07 +00:00
|
|
|
SR_PRIV int sr_sessionfile_check(const char *filename)
|
2011-01-31 21:34:14 +00:00
|
|
|
{
|
2013-12-11 09:49:01 +00:00
|
|
|
struct stat st;
|
2011-01-31 21:34:14 +00:00
|
|
|
struct zip *archive;
|
|
|
|
struct zip_file *zf;
|
|
|
|
struct zip_stat zs;
|
2013-09-18 11:28:07 +00:00
|
|
|
int version, ret;
|
|
|
|
char s[11];
|
2011-01-31 21:34:14 +00:00
|
|
|
|
2013-09-18 11:28:07 +00:00
|
|
|
if (!filename)
|
2012-01-02 13:15:25 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
|
2013-12-11 09:49:01 +00:00
|
|
|
if (stat(filename, &st) == -1) {
|
|
|
|
sr_err("Couldn't stat %s: %s", filename, strerror(errno));
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
|
2013-09-18 11:28:07 +00:00
|
|
|
if (!(archive = zip_open(filename, 0, &ret)))
|
|
|
|
/* No logging: this can be used just to check if it's
|
|
|
|
* a sigrok session file or not. */
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
/* check "version" */
|
2012-07-23 13:08:44 +00:00
|
|
|
version = 0;
|
2011-01-31 21:34:14 +00:00
|
|
|
if (!(zf = zip_fopen(archive, "version", 0))) {
|
2013-09-18 11:28:07 +00:00
|
|
|
sr_dbg("Not a sigrok session file: no version found.");
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
2013-09-18 11:28:07 +00:00
|
|
|
if ((ret = zip_fread(zf, s, 10)) == -1)
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
|
|
|
zip_fclose(zf);
|
2012-07-23 13:08:44 +00:00
|
|
|
s[ret] = 0;
|
|
|
|
version = strtoull(s, NULL, 10);
|
2014-01-23 21:45:08 +00:00
|
|
|
if (version > 2) {
|
2013-09-18 11:28:07 +00:00
|
|
|
sr_dbg("Cannot handle sigrok session file version %d.", version);
|
2012-07-23 13:08:44 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
2014-01-23 21:45:08 +00:00
|
|
|
sr_spew("Detected sigrok session file version %d.", version);
|
2011-01-31 21:34:14 +00:00
|
|
|
|
|
|
|
/* read "metadata" */
|
|
|
|
if (zip_stat(archive, "metadata", 0, &zs) == -1) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_dbg("Not a valid sigrok session file.");
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
2011-04-16 12:17:51 +00:00
|
|
|
|
2013-09-18 11:28:07 +00:00
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Load the session from the specified filename.
|
|
|
|
*
|
|
|
|
* @param filename The name of the session file to load. Must not be NULL.
|
|
|
|
*
|
|
|
|
* @return SR_OK upon success, SR_ERR_ARG upon invalid arguments,
|
|
|
|
* SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon
|
|
|
|
* other errors.
|
|
|
|
*/
|
|
|
|
SR_API int sr_session_load(const char *filename)
|
|
|
|
{
|
|
|
|
GKeyFile *kf;
|
|
|
|
GPtrArray *capturefiles;
|
|
|
|
struct zip *archive;
|
|
|
|
struct zip_file *zf;
|
|
|
|
struct zip_stat zs;
|
|
|
|
struct sr_dev_inst *sdi;
|
2014-03-24 20:34:20 +00:00
|
|
|
struct sr_channel *ch;
|
2014-05-27 22:05:51 +00:00
|
|
|
int devcnt, ret, i, j;
|
2014-03-24 20:34:20 +00:00
|
|
|
uint64_t tmp_u64, total_channels, enabled_channels, p;
|
2013-09-18 11:28:07 +00:00
|
|
|
char **sections, **keys, *metafile, *val;
|
2014-03-24 21:39:42 +00:00
|
|
|
char channelname[SR_MAX_CHANNELNAME_LEN + 1];
|
2013-09-18 11:28:07 +00:00
|
|
|
|
|
|
|
if ((ret = sr_sessionfile_check(filename)) != SR_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!(archive = zip_open(filename, 0, &ret)))
|
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
if (zip_stat(archive, "metadata", 0, &zs) == -1)
|
|
|
|
return SR_ERR;
|
|
|
|
|
2011-04-16 12:17:51 +00:00
|
|
|
if (!(metafile = g_try_malloc(zs.size))) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: metafile malloc failed", __func__);
|
2011-04-16 12:17:51 +00:00
|
|
|
return SR_ERR_MALLOC;
|
|
|
|
}
|
|
|
|
|
2011-01-31 21:34:14 +00:00
|
|
|
zf = zip_fopen_index(archive, zs.index, 0);
|
|
|
|
zip_fread(zf, metafile, zs.size);
|
|
|
|
zip_fclose(zf);
|
|
|
|
|
|
|
|
kf = g_key_file_new();
|
|
|
|
if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_dbg("Failed to parse metadata.");
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
|
2012-02-28 16:47:02 +00:00
|
|
|
sr_session_new();
|
2011-01-31 21:34:14 +00:00
|
|
|
|
|
|
|
devcnt = 0;
|
|
|
|
capturefiles = g_ptr_array_new_with_free_func(g_free);
|
|
|
|
sections = g_key_file_get_groups(kf, NULL);
|
|
|
|
for (i = 0; sections[i]; i++) {
|
|
|
|
if (!strcmp(sections[i], "global"))
|
|
|
|
/* nothing really interesting in here yet */
|
|
|
|
continue;
|
|
|
|
if (!strncmp(sections[i], "device ", 7)) {
|
|
|
|
/* device section */
|
2012-07-22 10:18:34 +00:00
|
|
|
sdi = NULL;
|
2014-03-24 20:34:20 +00:00
|
|
|
enabled_channels = total_channels = 0;
|
2011-01-31 21:34:14 +00:00
|
|
|
keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
|
|
|
|
for (j = 0; keys[j]; j++) {
|
|
|
|
val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
|
|
|
|
if (!strcmp(keys[j], "capturefile")) {
|
2012-07-22 10:18:34 +00:00
|
|
|
sdi = sr_dev_inst_new(devcnt, SR_ST_ACTIVE, NULL, NULL, NULL);
|
|
|
|
sdi->driver = &session_driver;
|
2011-01-31 21:34:14 +00:00
|
|
|
if (devcnt == 0)
|
2012-02-28 22:52:30 +00:00
|
|
|
/* first device, init the driver */
|
2012-12-03 01:42:57 +00:00
|
|
|
sdi->driver->init(NULL);
|
2013-04-24 21:06:50 +00:00
|
|
|
sr_dev_open(sdi);
|
2012-07-22 10:18:34 +00:00
|
|
|
sr_session_dev_add(sdi);
|
2013-03-25 19:21:10 +00:00
|
|
|
sdi->driver->config_set(SR_CONF_SESSIONFILE,
|
2013-04-20 00:00:49 +00:00
|
|
|
g_variant_new_string(filename), sdi, NULL);
|
2013-03-25 19:21:10 +00:00
|
|
|
sdi->driver->config_set(SR_CONF_CAPTUREFILE,
|
2013-04-20 00:00:49 +00:00
|
|
|
g_variant_new_string(val), sdi, NULL);
|
2011-01-31 21:34:14 +00:00
|
|
|
g_ptr_array_add(capturefiles, val);
|
|
|
|
} else if (!strcmp(keys[j], "samplerate")) {
|
2011-11-27 18:31:25 +00:00
|
|
|
sr_parse_sizestring(val, &tmp_u64);
|
2013-03-25 19:21:10 +00:00
|
|
|
sdi->driver->config_set(SR_CONF_SAMPLERATE,
|
2013-04-20 00:00:49 +00:00
|
|
|
g_variant_new_uint64(tmp_u64), sdi, NULL);
|
2011-01-31 21:34:14 +00:00
|
|
|
} else if (!strcmp(keys[j], "unitsize")) {
|
|
|
|
tmp_u64 = strtoull(val, NULL, 10);
|
2013-03-25 19:21:10 +00:00
|
|
|
sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE,
|
2013-04-20 00:00:49 +00:00
|
|
|
g_variant_new_uint64(tmp_u64), sdi, NULL);
|
2011-01-31 21:34:14 +00:00
|
|
|
} else if (!strcmp(keys[j], "total probes")) {
|
2014-03-24 20:34:20 +00:00
|
|
|
total_channels = strtoull(val, NULL, 10);
|
2014-03-24 21:39:42 +00:00
|
|
|
sdi->driver->config_set(SR_CONF_NUM_LOGIC_CHANNELS,
|
2014-03-24 20:34:20 +00:00
|
|
|
g_variant_new_uint64(total_channels), sdi, NULL);
|
|
|
|
for (p = 0; p < total_channels; p++) {
|
2014-03-24 21:39:42 +00:00
|
|
|
snprintf(channelname, SR_MAX_CHANNELNAME_LEN, "%" PRIu64, p);
|
|
|
|
if (!(ch = sr_channel_new(p, SR_CHANNEL_LOGIC, TRUE,
|
2014-03-24 20:34:20 +00:00
|
|
|
channelname)))
|
2012-07-22 10:18:34 +00:00
|
|
|
return SR_ERR;
|
2014-03-24 20:34:20 +00:00
|
|
|
sdi->channels = g_slist_append(sdi->channels, ch);
|
2011-12-29 16:04:31 +00:00
|
|
|
}
|
2011-01-31 21:34:14 +00:00
|
|
|
} else if (!strncmp(keys[j], "probe", 5)) {
|
2012-07-22 10:18:34 +00:00
|
|
|
if (!sdi)
|
2011-01-31 21:34:14 +00:00
|
|
|
continue;
|
2014-03-24 20:34:20 +00:00
|
|
|
enabled_channels++;
|
2011-01-31 21:34:14 +00:00
|
|
|
tmp_u64 = strtoul(keys[j]+5, NULL, 10);
|
2012-07-23 12:55:43 +00:00
|
|
|
/* sr_session_save() */
|
2014-03-24 15:05:42 +00:00
|
|
|
sr_dev_channel_name_set(sdi, tmp_u64 - 1, val);
|
2011-01-31 21:34:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev(keys);
|
2014-03-24 20:34:20 +00:00
|
|
|
/* Disable channels not specifically listed. */
|
|
|
|
if (total_channels)
|
|
|
|
for (p = enabled_channels; p < total_channels; p++)
|
2014-03-24 15:05:42 +00:00
|
|
|
sr_dev_channel_enable(sdi, p, FALSE);
|
2011-01-31 21:34:14 +00:00
|
|
|
}
|
2012-07-22 10:18:34 +00:00
|
|
|
devcnt++;
|
2011-01-31 21:34:14 +00:00
|
|
|
}
|
|
|
|
g_strfreev(sections);
|
|
|
|
g_key_file_free(kf);
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
/**
|
|
|
|
* Save the current session to the specified file.
|
|
|
|
*
|
2013-01-08 01:51:03 +00:00
|
|
|
* @param filename The name of the filename to save the current session as.
|
2012-01-02 13:15:25 +00:00
|
|
|
* Must not be NULL.
|
2012-07-23 13:06:49 +00:00
|
|
|
* @param sdi The device instance from which the data was captured.
|
2013-01-08 01:51:03 +00:00
|
|
|
* @param buf The data to be saved.
|
|
|
|
* @param unitsize The number of bytes per sample.
|
|
|
|
* @param units The number of samples.
|
2012-01-02 13:15:25 +00:00
|
|
|
*
|
2014-01-07 11:52:23 +00:00
|
|
|
* @retval SR_OK Success
|
|
|
|
* @retval SR_ERR_ARG Invalid arguments
|
|
|
|
* @retval SR_ERR Other errors
|
2014-05-04 18:51:05 +00:00
|
|
|
*
|
|
|
|
* @since 0.2.0
|
2012-01-02 13:15:25 +00:00
|
|
|
*/
|
2013-01-08 01:51:03 +00:00
|
|
|
SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
|
|
|
|
unsigned char *buf, int unitsize, int units)
|
2011-01-31 21:34:14 +00:00
|
|
|
{
|
2014-03-24 20:34:20 +00:00
|
|
|
struct sr_channel *ch;
|
2013-01-08 01:51:03 +00:00
|
|
|
GSList *l;
|
2013-03-25 19:21:10 +00:00
|
|
|
GVariant *gvar;
|
2014-01-07 11:52:23 +00:00
|
|
|
uint64_t samplerate;
|
|
|
|
int cnt, ret;
|
2014-03-24 20:34:20 +00:00
|
|
|
char **channel_names;
|
2014-01-07 11:52:23 +00:00
|
|
|
|
|
|
|
samplerate = 0;
|
|
|
|
if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
|
|
|
|
if (sr_config_get(sdi->driver, sdi, NULL,
|
|
|
|
SR_CONF_SAMPLERATE, &gvar) == SR_OK) {
|
|
|
|
samplerate = g_variant_get_uint64(gvar);
|
|
|
|
g_variant_unref(gvar);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
channel_names = g_malloc0(sizeof(char *) * (g_slist_length(sdi->channels) + 1));
|
2014-01-07 11:52:23 +00:00
|
|
|
cnt = 0;
|
2014-03-24 20:34:20 +00:00
|
|
|
for (l = sdi->channels; l; l = l->next) {
|
|
|
|
ch = l->data;
|
2014-03-24 21:39:42 +00:00
|
|
|
if (ch->type != SR_CHANNEL_LOGIC)
|
2014-01-21 17:16:52 +00:00
|
|
|
continue;
|
2014-03-24 20:34:20 +00:00
|
|
|
if (ch->enabled != TRUE)
|
2014-01-21 17:16:52 +00:00
|
|
|
continue;
|
2014-03-24 20:34:20 +00:00
|
|
|
if (!ch->name)
|
2014-01-21 17:16:52 +00:00
|
|
|
continue;
|
|
|
|
/* Just borrowing the ptr. */
|
2014-03-24 20:34:20 +00:00
|
|
|
channel_names[cnt++] = ch->name;
|
2014-01-07 11:52:23 +00:00
|
|
|
}
|
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
if ((ret = sr_session_save_init(filename, samplerate, channel_names)) != SR_OK)
|
2014-01-07 11:52:23 +00:00
|
|
|
return ret;
|
|
|
|
|
|
|
|
ret = sr_session_append(filename, buf, unitsize, units);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize a saved session file.
|
|
|
|
*
|
|
|
|
* @param filename The name of the filename to save the current session as.
|
|
|
|
* Must not be NULL.
|
|
|
|
* @param samplerate The samplerate to store for this session.
|
2014-03-24 20:34:20 +00:00
|
|
|
* @param channels A NULL-terminated array of strings containing the names
|
|
|
|
* of all the channels active in this session.
|
2014-01-07 11:52:23 +00:00
|
|
|
*
|
|
|
|
* @retval SR_OK Success
|
|
|
|
* @retval SR_ERR_ARG Invalid arguments
|
|
|
|
* @retval SR_ERR Other errors
|
2014-05-04 18:51:05 +00:00
|
|
|
*
|
|
|
|
* @since 0.3.0
|
2014-01-07 11:52:23 +00:00
|
|
|
*/
|
|
|
|
SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
|
2014-03-24 20:34:20 +00:00
|
|
|
char **channels)
|
2014-01-07 11:52:23 +00:00
|
|
|
{
|
2011-01-31 21:34:14 +00:00
|
|
|
FILE *meta;
|
|
|
|
struct zip *zipfile;
|
2014-01-07 11:52:23 +00:00
|
|
|
struct zip_source *versrc, *metasrc;
|
|
|
|
int tmpfile, cnt, ret, i;
|
|
|
|
char version[1], metafile[32], *s;
|
2011-01-31 21:34:14 +00:00
|
|
|
|
2012-01-02 13:15:25 +00:00
|
|
|
if (!filename) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_err("%s: filename was NULL", __func__);
|
2012-01-02 13:15:25 +00:00
|
|
|
return SR_ERR_ARG;
|
|
|
|
}
|
|
|
|
|
2011-01-31 21:34:14 +00:00
|
|
|
/* Quietly delete it first, libzip wants replace ops otherwise. */
|
2011-02-01 00:41:33 +00:00
|
|
|
unlink(filename);
|
2012-03-20 16:51:18 +00:00
|
|
|
if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
/* "version" */
|
2014-01-23 21:45:08 +00:00
|
|
|
version[0] = '2';
|
2011-01-31 21:34:14 +00:00
|
|
|
if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
|
|
|
|
return SR_ERR;
|
|
|
|
if (zip_add(zipfile, "version", versrc) == -1) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_info("error saving version into zipfile: %s",
|
2011-04-14 07:46:53 +00:00
|
|
|
zip_strerror(zipfile));
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* init "metadata" */
|
|
|
|
strcpy(metafile, "sigrok-meta-XXXXXX");
|
|
|
|
if ((tmpfile = g_mkstemp(metafile)) == -1)
|
|
|
|
return SR_ERR;
|
|
|
|
close(tmpfile);
|
2011-02-05 19:03:17 +00:00
|
|
|
meta = g_fopen(metafile, "wb");
|
2011-01-31 21:34:14 +00:00
|
|
|
fprintf(meta, "[global]\n");
|
|
|
|
fprintf(meta, "sigrok version = %s\n", PACKAGE_VERSION);
|
|
|
|
|
2012-07-23 13:06:49 +00:00
|
|
|
/* metadata */
|
|
|
|
fprintf(meta, "[device 1]\n");
|
2011-01-31 21:34:14 +00:00
|
|
|
|
2012-07-23 13:06:49 +00:00
|
|
|
/* metadata */
|
|
|
|
fprintf(meta, "capturefile = logic-1\n");
|
2014-01-07 11:52:23 +00:00
|
|
|
cnt = 0;
|
2014-03-24 20:34:20 +00:00
|
|
|
for (i = 0; channels[i]; i++)
|
2014-01-07 11:52:23 +00:00
|
|
|
cnt++;
|
|
|
|
fprintf(meta, "total probes = %d\n", cnt);
|
|
|
|
s = sr_samplerate_string(samplerate);
|
|
|
|
fprintf(meta, "samplerate = %s\n", s);
|
|
|
|
g_free(s);
|
|
|
|
|
2014-03-24 20:34:20 +00:00
|
|
|
for (i = 0; channels[i]; i++)
|
|
|
|
fprintf(meta, "probe%d = %s\n", i + 1, channels[i]);
|
2011-01-31 21:34:14 +00:00
|
|
|
|
|
|
|
fclose(meta);
|
|
|
|
|
2014-01-21 13:47:05 +00:00
|
|
|
if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) {
|
|
|
|
unlink(metafile);
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
2014-01-21 13:47:05 +00:00
|
|
|
}
|
|
|
|
if (zip_add(zipfile, "metadata", metasrc) == -1) {
|
|
|
|
unlink(metafile);
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
2014-01-21 13:47:05 +00:00
|
|
|
}
|
2011-01-31 21:34:14 +00:00
|
|
|
|
|
|
|
if ((ret = zip_close(zipfile)) == -1) {
|
2012-11-11 11:44:16 +00:00
|
|
|
sr_info("error saving zipfile: %s", zip_strerror(zipfile));
|
2014-01-21 13:47:05 +00:00
|
|
|
unlink(metafile);
|
2011-01-31 21:34:14 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
unlink(metafile);
|
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
2012-10-21 14:13:36 +00:00
|
|
|
|
2013-09-18 11:28:07 +00:00
|
|
|
/**
|
|
|
|
* Append data to an existing session file.
|
|
|
|
*
|
2014-01-07 11:52:23 +00:00
|
|
|
* The session file must have been created with sr_session_save_init()
|
|
|
|
* or sr_session_save() beforehand.
|
|
|
|
*
|
2013-09-18 11:28:07 +00:00
|
|
|
* @param filename The name of the filename to append to. Must not be NULL.
|
|
|
|
* @param buf The data to be appended.
|
|
|
|
* @param unitsize The number of bytes per sample.
|
|
|
|
* @param units The number of samples.
|
|
|
|
*
|
2014-01-07 11:52:23 +00:00
|
|
|
* @retval SR_OK Success
|
|
|
|
* @retval SR_ERR_ARG Invalid arguments
|
|
|
|
* @retval SR_ERR Other errors
|
2014-05-04 18:51:05 +00:00
|
|
|
*
|
|
|
|
* @since 0.3.0
|
2013-09-18 11:28:07 +00:00
|
|
|
*/
|
|
|
|
SR_API int sr_session_append(const char *filename, unsigned char *buf,
|
|
|
|
int unitsize, int units)
|
|
|
|
{
|
|
|
|
struct zip *archive;
|
|
|
|
struct zip_source *logicsrc;
|
|
|
|
zip_int64_t num_files;
|
2014-01-07 11:52:23 +00:00
|
|
|
struct zip_file *zf;
|
|
|
|
struct zip_stat zs;
|
|
|
|
struct zip_source *metasrc;
|
|
|
|
GKeyFile *kf;
|
|
|
|
GError *error;
|
|
|
|
gsize len;
|
|
|
|
int chunk_num, next_chunk_num, tmpfile, ret, i;
|
2013-09-18 11:28:07 +00:00
|
|
|
const char *entry_name;
|
2014-01-07 11:52:23 +00:00
|
|
|
char *metafile, tmpname[32], chunkname[16];
|
2013-09-18 11:28:07 +00:00
|
|
|
|
|
|
|
if ((ret = sr_sessionfile_check(filename)) != SR_OK)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
if (!(archive = zip_open(filename, 0, &ret)))
|
|
|
|
return SR_ERR;
|
|
|
|
|
2014-01-07 11:52:23 +00:00
|
|
|
if (zip_stat(archive, "metadata", 0, &zs) == -1)
|
|
|
|
return SR_ERR;
|
|
|
|
|
|
|
|
metafile = g_malloc(zs.size);
|
|
|
|
zf = zip_fopen_index(archive, zs.index, 0);
|
|
|
|
zip_fread(zf, metafile, zs.size);
|
|
|
|
zip_fclose(zf);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* If the file was only initialized but doesn't yet have any
|
|
|
|
* data it in, it won't have a unitsize field in metadata yet.
|
|
|
|
*/
|
|
|
|
error = NULL;
|
|
|
|
kf = g_key_file_new();
|
|
|
|
if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, &error)) {
|
|
|
|
sr_err("Failed to parse metadata: %s.", error->message);
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
g_free(metafile);
|
2014-01-21 13:47:05 +00:00
|
|
|
tmpname[0] = '\0';
|
2014-01-07 11:52:23 +00:00
|
|
|
if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) {
|
|
|
|
if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
|
|
|
|
sr_err("Failed to check unitsize key: %s", error ? error->message : "?");
|
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
/* Add unitsize field. */
|
|
|
|
g_key_file_set_integer(kf, "device 1", "unitsize", unitsize);
|
|
|
|
metafile = g_key_file_to_data(kf, &len, &error);
|
|
|
|
strcpy(tmpname, "sigrok-meta-XXXXXX");
|
|
|
|
if ((tmpfile = g_mkstemp(tmpname)) == -1)
|
|
|
|
return SR_ERR;
|
|
|
|
if (write(tmpfile, metafile, len) < 0) {
|
|
|
|
sr_dbg("Failed to create new metadata: %s", strerror(errno));
|
2014-01-21 13:47:05 +00:00
|
|
|
g_free(metafile);
|
|
|
|
unlink(tmpname);
|
2014-01-07 11:52:23 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
close(tmpfile);
|
|
|
|
if (!(metasrc = zip_source_file(archive, tmpname, 0, -1))) {
|
|
|
|
sr_err("Failed to create zip source for metadata.");
|
2014-01-21 13:47:05 +00:00
|
|
|
g_free(metafile);
|
|
|
|
unlink(tmpname);
|
2014-01-07 11:52:23 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
if (zip_replace(archive, zs.index, metasrc) == -1) {
|
|
|
|
sr_err("Failed to replace metadata file.");
|
2014-01-21 13:47:05 +00:00
|
|
|
g_free(metafile);
|
|
|
|
unlink(tmpname);
|
2014-01-07 11:52:23 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
g_free(metafile);
|
|
|
|
}
|
|
|
|
g_key_file_free(kf);
|
|
|
|
|
2013-09-18 11:28:07 +00:00
|
|
|
next_chunk_num = 1;
|
|
|
|
num_files = zip_get_num_entries(archive, 0);
|
|
|
|
for (i = 0; i < num_files; i++) {
|
|
|
|
entry_name = zip_get_name(archive, i, 0);
|
|
|
|
if (strncmp(entry_name, "logic-1", 7))
|
|
|
|
continue;
|
|
|
|
if (strlen(entry_name) == 7) {
|
|
|
|
/* This file has no extra chunks, just a single "logic-1".
|
|
|
|
* Rename it to "logic-1-1" * and continue with chunk 2. */
|
|
|
|
if (zip_rename(archive, i, "logic-1-1") == -1) {
|
|
|
|
sr_err("Failed to rename 'logic-1' to 'logic-1-1'.");
|
2014-01-21 13:47:05 +00:00
|
|
|
unlink(tmpname);
|
2013-09-18 11:28:07 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
|
|
|
next_chunk_num = 2;
|
|
|
|
break;
|
|
|
|
} else if (strlen(entry_name) > 8 && entry_name[7] == '-') {
|
|
|
|
chunk_num = strtoull(entry_name + 8, NULL, 10);
|
|
|
|
if (chunk_num >= next_chunk_num)
|
|
|
|
next_chunk_num = chunk_num + 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
snprintf(chunkname, 15, "logic-1-%d", next_chunk_num);
|
2014-01-21 13:47:05 +00:00
|
|
|
if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE))) {
|
|
|
|
unlink(tmpname);
|
2013-09-18 11:28:07 +00:00
|
|
|
return SR_ERR;
|
2014-01-21 13:47:05 +00:00
|
|
|
}
|
|
|
|
if (zip_add(archive, chunkname, logicsrc) == -1) {
|
|
|
|
unlink(tmpname);
|
2013-09-18 11:28:07 +00:00
|
|
|
return SR_ERR;
|
2014-01-21 13:47:05 +00:00
|
|
|
}
|
2013-09-18 11:28:07 +00:00
|
|
|
if ((ret = zip_close(archive)) == -1) {
|
|
|
|
sr_info("error saving session file: %s", zip_strerror(archive));
|
2014-01-21 13:47:05 +00:00
|
|
|
unlink(tmpname);
|
2013-09-18 11:28:07 +00:00
|
|
|
return SR_ERR;
|
|
|
|
}
|
2014-01-21 13:47:05 +00:00
|
|
|
unlink(tmpname);
|
2013-09-18 11:28:07 +00:00
|
|
|
|
|
|
|
return SR_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-21 14:13:36 +00:00
|
|
|
/** @} */
|