26 #include <glib/gstdio.h>
28 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "session-file"
47 static int session_driver_initialized = 0;
54 if (zip_unchange_all(archive) < 0 || zip_close(archive) < 0)
55 sr_err(
"Failed to discard ZIP archive: %s", zip_strerror(archive));
65 const struct zip_stat *entry)
73 if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
74 sr_err(
"Metadata buffer allocation failed.");
77 zf = zip_fopen_index(archive, entry->index, 0);
79 sr_err(
"Failed to open metadata: %s", zip_strerror(archive));
83 metalen = zip_fread(zf, metabuf, entry->size);
85 sr_err(
"Failed to read metadata: %s", zip_file_strerror(zf));
92 keyfile = g_key_file_new();
94 g_key_file_load_from_data(keyfile, metabuf, metalen,
95 G_KEY_FILE_NONE, &error);
99 sr_err(
"Failed to parse metadata: %s", error->message);
101 g_key_file_free(keyfile);
108 SR_PRIV int sr_sessionfile_check(
const char *filename)
120 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
121 sr_err(
"Not a regular file: %s.", filename);
125 if (!(archive = zip_open(filename, 0, NULL)))
131 if (!(zf = zip_fopen(archive,
"version", 0))) {
132 sr_dbg(
"Not a sigrok session file: no version found.");
133 zip_discard(archive);
136 ret = zip_fread(zf, s,
sizeof(s) - 1);
138 sr_err(
"Failed to read version file: %s",
139 zip_file_strerror(zf));
141 zip_discard(archive);
146 version = g_ascii_strtoull(s, NULL, 10);
147 if (version == 0 || version > 2) {
148 sr_dbg(
"Cannot handle sigrok session file version %" PRIu64
".",
150 zip_discard(archive);
153 sr_spew(
"Detected sigrok session file version %" PRIu64
".", version);
156 if (zip_stat(archive,
"metadata", 0, &zs) < 0) {
157 sr_dbg(
"Not a valid sigrok session file.");
158 zip_discard(archive);
161 zip_discard(archive);
185 struct sr_dev_inst *sdi;
189 int total_channels, k;
191 char **sections, **keys, *val;
194 if ((ret = sr_sessionfile_check(filename)) !=
SR_OK)
197 if (!(archive = zip_open(filename, 0, NULL)))
200 if (zip_stat(archive,
"metadata", 0, &zs) < 0) {
201 zip_discard(archive);
205 zip_discard(archive);
216 sections = g_key_file_get_groups(kf, NULL);
217 for (i = 0; sections[i] && ret ==
SR_OK; i++) {
218 if (!strcmp(sections[i],
"global"))
221 if (!strncmp(sections[i],
"device ", 7)) {
224 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
225 for (j = 0; keys[j]; j++) {
226 if (!strcmp(keys[j],
"capturefile")) {
227 val = g_key_file_get_string(kf, sections[i],
233 sdi = g_malloc0(
sizeof(
struct sr_dev_inst));
236 if (!session_driver_initialized) {
238 session_driver_initialized = 1;
239 sdi->driver->init(sdi->driver, NULL);
243 (*session)->owned_devs = g_slist_append(
244 (*session)->owned_devs, sdi);
246 g_variant_new_string(filename));
248 g_variant_new_string(val));
250 }
else if (!strcmp(keys[j],
"samplerate")) {
251 val = g_key_file_get_string(kf, sections[i],
254 &tmp_u64) !=
SR_OK) {
261 g_variant_new_uint64(tmp_u64));
262 }
else if (!strcmp(keys[j],
"unitsize")) {
263 unitsize = g_key_file_get_integer(kf, sections[i],
265 if (!sdi || unitsize <= 0 || error) {
270 g_variant_new_uint64(unitsize));
271 }
else if (!strcmp(keys[j],
"total probes")) {
272 total_channels = g_key_file_get_integer(kf,
273 sections[i], keys[j], &error);
274 if (!sdi || total_channels < 0 || error) {
279 g_variant_new_int32(total_channels));
280 for (k = 0; k < total_channels; k++) {
281 g_snprintf(channelname,
sizeof(channelname),
286 }
else if (!strncmp(keys[j],
"probe", 5)) {
287 tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10);
288 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
292 ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1);
297 val = g_key_file_get_string(kf, sections[i],
312 g_strfreev(sections);
316 sr_err(
"Failed to parse metadata: %s", error->message);
int sr_session_dev_add(struct sr_session *session, struct sr_dev_inst *sdi)
Add a device instance to a session.
SR_PRIV GKeyFile * sr_sessionfile_read_metadata(struct zip *archive, const struct zip_stat *entry)
Read metadata entries from a session archive.
The public libsigrok header file to be used by frontends.
int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
Enable or disable a channel.
SR_PRIV void sr_zip_discard(struct zip *archive)
The device supports specifying a capturefile to inject.
The device supports specifying the capturefile unit size.
Generic/unspecified error.
Opaque structure representing a libsigrok session.
The device supports setting the number of logic channels.
int sr_config_set(const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, uint32_t key, GVariant *data)
Set value of a configuration key in a device instance.
#define SR_MAX_CHANNELNAME_LEN
int sr_session_load(struct sr_context *ctx, const char *filename, struct sr_session **session)
Load the session from the specified filename.
int sr_parse_sizestring(const char *sizestring, uint64_t *size)
Convert a "natural" string representation of a size value to uint64_t.
Information on single channel.
int sr_dev_channel_name_set(struct sr_channel *channel, const char *name)
Set the name of the specified channel.
The device instance is actively in use in a session.
SR_PRIV struct sr_dev_driver session_driver
The device supports setting its samplerate, in Hz.
Opaque structure representing a libsigrok context.
int sr_session_new(struct sr_context *ctx, struct sr_session **new_session)
Create a new session.
Channel type is logic channel.
int sr_dev_open(struct sr_dev_inst *sdi)
Open the specified device.