26 #include <glib/gstdio.h>
28 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "session-file"
47 static int session_driver_initialized = 0;
52 SR_PRIV void sr_zip_discard(
struct zip *archive)
54 if (zip_unchange_all(archive) < 0 || zip_close(archive) < 0)
55 sr_err(
"Failed to discard ZIP archive: %s", zip_strerror(archive));
69 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(
struct zip *archive,
70 const struct zip_stat *entry)
78 if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
79 sr_err(
"Metadata buffer allocation failed.");
82 zf = zip_fopen_index(archive, entry->index, 0);
84 sr_err(
"Failed to open metadata: %s", zip_strerror(archive));
88 metalen = zip_fread(zf, metabuf, entry->size);
90 sr_err(
"Failed to read metadata: %s", zip_file_strerror(zf));
97 keyfile = g_key_file_new();
99 g_key_file_load_from_data(keyfile, metabuf, metalen,
100 G_KEY_FILE_NONE, &error);
104 sr_err(
"Failed to parse metadata: %s", error->message);
106 g_key_file_free(keyfile);
113 SR_PRIV int sr_sessionfile_check(
const char *filename)
125 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
126 sr_err(
"Not a regular file: %s.", filename);
130 if (!(archive = zip_open(filename, 0, NULL)))
136 if (!(zf = zip_fopen(archive,
"version", 0))) {
137 sr_dbg(
"Not a sigrok session file: no version found.");
138 zip_discard(archive);
141 ret = zip_fread(zf, s,
sizeof(s) - 1);
143 sr_err(
"Failed to read version file: %s",
144 zip_file_strerror(zf));
146 zip_discard(archive);
151 version = g_ascii_strtoull(s, NULL, 10);
152 if (version == 0 || version > 2) {
153 sr_dbg(
"Cannot handle sigrok session file version %" PRIu64
".",
155 zip_discard(archive);
158 sr_spew(
"Detected sigrok session file version %" PRIu64
".", version);
161 if (zip_stat(archive,
"metadata", 0, &zs) < 0) {
162 sr_dbg(
"Not a valid sigrok session file.");
163 zip_discard(archive);
166 zip_discard(archive);
172 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(
const char *filename,
struct sr_session **session)
174 struct sr_dev_inst *sdi = NULL;
176 sdi = g_malloc0(
sizeof(
struct sr_dev_inst));
179 if (!session_driver_initialized) {
181 session_driver_initialized = 1;
182 sdi->driver->init(sdi->driver, NULL);
186 (*session)->owned_devs = g_slist_append((*session)->owned_devs, sdi);
188 g_variant_new_string(filename));
212 struct sr_dev_inst *sdi;
216 int total_channels, total_analog, k;
219 char **sections, **keys, *val;
221 gboolean file_has_logic;
223 if ((ret = sr_sessionfile_check(filename)) !=
SR_OK)
226 if (!(archive = zip_open(filename, 0, NULL)))
229 if (zip_stat(archive,
"metadata", 0, &zs) < 0) {
230 zip_discard(archive);
233 kf = sr_sessionfile_read_metadata(archive, &zs);
234 zip_discard(archive);
247 file_has_logic = FALSE;
248 sections = g_key_file_get_groups(kf, NULL);
249 for (i = 0; sections[i] && ret ==
SR_OK; i++) {
250 if (!strcmp(sections[i],
"global"))
253 if (!strncmp(sections[i],
"device ", 7)) {
256 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
259 total_analog = g_key_file_get_integer(kf, sections[i],
260 "total analog", &error);
261 if (total_analog > 0 && !error)
262 sdi = sr_session_prepare_sdi(filename, session);
263 g_clear_error(&error);
266 val = g_key_file_get_string(kf, sections[i],
267 "capturefile", &error);
270 sdi = sr_session_prepare_sdi(filename, session);
272 g_variant_new_string(val));
274 file_has_logic = TRUE;
276 g_clear_error(&error);
278 for (j = 0; keys[j]; j++) {
279 if (!strcmp(keys[j],
"samplerate")) {
280 val = g_key_file_get_string(kf, sections[i],
283 &tmp_u64) !=
SR_OK) {
290 g_variant_new_uint64(tmp_u64));
291 }
else if (!strcmp(keys[j],
"unitsize") && file_has_logic) {
292 unitsize = g_key_file_get_integer(kf, sections[i],
294 if (!sdi || unitsize <= 0 || error) {
299 g_variant_new_uint64(unitsize));
300 }
else if (!strcmp(keys[j],
"total probes")) {
301 total_channels = g_key_file_get_integer(kf,
302 sections[i], keys[j], &error);
303 if (!sdi || total_channels < 0 || error) {
308 g_variant_new_int32(total_channels));
309 for (k = 0; k < total_channels; k++) {
310 g_snprintf(channelname,
sizeof(channelname),
315 }
else if (!strcmp(keys[j],
"total analog")) {
316 total_analog = g_key_file_get_integer(kf,
317 sections[i], keys[j], &error);
318 if (!sdi || total_analog < 0 || error) {
323 g_variant_new_int32(total_analog));
324 for (k = total_channels; k < (total_channels + total_analog); k++) {
325 g_snprintf(channelname,
sizeof(channelname),
330 }
else if (!strncmp(keys[j],
"probe", 5)) {
331 tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10);
332 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
336 ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1);
341 val = g_key_file_get_string(kf, sections[i],
351 }
else if (!strncmp(keys[j],
"analog", 6)) {
352 tmp_u64 = g_ascii_strtoull(keys[j]+6, NULL, 10);
353 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
358 for (l = sdi->channels; l; l = l->next) {
360 if ((guint64)ch->
index == tmp_u64 - 1)
369 val = g_key_file_get_string(kf, sections[i],
384 g_strfreev(sections);
388 sr_err(
"Failed to parse metadata: %s", error->message);
Generic/unspecified error.
SR_PRIV struct sr_dev_driver session_driver
int sr_parse_sizestring(const char *sizestring, uint64_t *size)
Convert a "natural" string representation of a size value to uint64_t.
The device supports setting the number of logic channels.
The device supports setting the number of analog channels.
int sr_dev_open(struct sr_dev_inst *sdi)
Open the specified device.
Channel type is logic channel.
int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
Enable or disable a channel.
The public libsigrok header file to be used by frontends.
The device supports specifying a capturefile to inject.
int sr_session_dev_add(struct sr_session *session, struct sr_dev_inst *sdi)
Add a device instance to a session.
The device supports specifying the capturefile unit size.
Channel type is analog channel.
int sr_session_load(struct sr_context *ctx, const char *filename, struct sr_session **session)
Load the session from the specified filename.
The device instance is actively in use in a session.
int sr_dev_channel_name_set(struct sr_channel *channel, const char *name)
Set the name of the specified channel.
int sr_session_new(struct sr_context *ctx, struct sr_session **new_session)
Create a new session.
The device supports setting its samplerate, in Hz.
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 index
The index of this channel, starting at 0.
Information on single channel.
Opaque structure representing a libsigrok session.
Opaque structure representing a libsigrok context.