26 #include <glib/gstdio.h> 28 #include "libsigrok-internal.h" 31 #define LOG_PREFIX "session-file" 49 static int session_driver_initialized = 0;
54 SR_PRIV void sr_zip_discard(
struct zip *archive)
56 if (zip_unchange_all(archive) < 0 || zip_close(archive) < 0)
57 sr_err(
"Failed to discard ZIP archive: %s", zip_strerror(archive));
71 SR_PRIV GKeyFile *sr_sessionfile_read_metadata(
struct zip *archive,
72 const struct zip_stat *entry)
80 if (entry->size > G_MAXINT || !(metabuf = g_try_malloc(entry->size))) {
81 sr_err(
"Metadata buffer allocation failed.");
84 zf = zip_fopen_index(archive, entry->index, 0);
86 sr_err(
"Failed to open metadata: %s", zip_strerror(archive));
90 metalen = zip_fread(zf, metabuf, entry->size);
92 sr_err(
"Failed to read metadata: %s", zip_file_strerror(zf));
99 keyfile = g_key_file_new();
101 g_key_file_load_from_data(keyfile, metabuf, metalen,
102 G_KEY_FILE_NONE, &error);
106 sr_err(
"Failed to parse metadata: %s", error->message);
108 g_key_file_free(keyfile);
115 SR_PRIV int sr_sessionfile_check(
const char *filename)
127 if (!g_file_test(filename, G_FILE_TEST_IS_REGULAR)) {
128 sr_err(
"Not a regular file: %s.", filename);
132 if (!(archive = zip_open(filename, 0, NULL)))
138 if (!(zf = zip_fopen(archive,
"version", 0))) {
139 sr_dbg(
"Not a sigrok session file: no version found.");
140 zip_discard(archive);
143 ret = zip_fread(zf, s,
sizeof(s) - 1);
145 sr_err(
"Failed to read version file: %s",
146 zip_file_strerror(zf));
148 zip_discard(archive);
153 version = g_ascii_strtoull(s, NULL, 10);
154 if (version == 0 || version > 2) {
155 sr_dbg(
"Cannot handle sigrok session file version %" PRIu64
".",
157 zip_discard(archive);
160 sr_spew(
"Detected sigrok session file version %" PRIu64
".", version);
163 if (zip_stat(archive,
"metadata", 0, &zs) < 0) {
164 sr_dbg(
"Not a valid sigrok session file.");
165 zip_discard(archive);
168 zip_discard(archive);
174 SR_PRIV struct sr_dev_inst *sr_session_prepare_sdi(
const char *filename,
struct sr_session **session)
176 struct sr_dev_inst *sdi = NULL;
178 sdi = g_malloc0(
sizeof(
struct sr_dev_inst));
179 sdi->driver = &session_driver;
181 if (!session_driver_initialized) {
183 session_driver_initialized = 1;
184 sdi->driver->init(sdi->driver, NULL);
188 (*session)->owned_devs = g_slist_append((*session)->owned_devs, sdi);
190 g_variant_new_string(filename));
214 struct sr_dev_inst *sdi;
218 int total_channels, total_analog, k;
221 char **sections, **keys, *val;
223 gboolean file_has_logic;
225 if ((ret = sr_sessionfile_check(filename)) !=
SR_OK)
228 if (!(archive = zip_open(filename, 0, NULL)))
231 if (zip_stat(archive,
"metadata", 0, &zs) < 0) {
232 zip_discard(archive);
235 kf = sr_sessionfile_read_metadata(archive, &zs);
236 zip_discard(archive);
249 file_has_logic = FALSE;
250 sections = g_key_file_get_groups(kf, NULL);
251 for (i = 0; sections[i] && ret ==
SR_OK; i++) {
252 if (!strcmp(sections[i],
"global"))
255 if (!strncmp(sections[i],
"device ", 7)) {
258 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
261 total_analog = g_key_file_get_integer(kf, sections[i],
262 "total analog", &error);
263 if (total_analog > 0 && !error)
264 sdi = sr_session_prepare_sdi(filename, session);
265 g_clear_error(&error);
268 val = g_key_file_get_string(kf, sections[i],
269 "capturefile", &error);
272 sdi = sr_session_prepare_sdi(filename, session);
274 g_variant_new_string(val));
276 file_has_logic = TRUE;
278 g_clear_error(&error);
280 for (j = 0; keys[j]; j++) {
281 if (!strcmp(keys[j],
"samplerate")) {
282 val = g_key_file_get_string(kf, sections[i],
285 &tmp_u64) !=
SR_OK) {
292 g_variant_new_uint64(tmp_u64));
293 }
else if (!strcmp(keys[j],
"unitsize") && file_has_logic) {
294 unitsize = g_key_file_get_integer(kf, sections[i],
296 if (!sdi || unitsize <= 0 || error) {
301 g_variant_new_uint64(unitsize));
302 }
else if (!strcmp(keys[j],
"total probes")) {
303 total_channels = g_key_file_get_integer(kf,
304 sections[i], keys[j], &error);
305 if (!sdi || total_channels < 0 || error) {
310 g_variant_new_int32(total_channels));
311 for (k = 0; k < total_channels; k++) {
312 g_snprintf(channelname,
sizeof(channelname),
317 }
else if (!strcmp(keys[j],
"total analog")) {
318 total_analog = g_key_file_get_integer(kf,
319 sections[i], keys[j], &error);
320 if (!sdi || total_analog < 0 || error) {
325 g_variant_new_int32(total_analog));
326 for (k = total_channels; k < (total_channels + total_analog); k++) {
327 g_snprintf(channelname,
sizeof(channelname),
332 }
else if (!strncmp(keys[j],
"probe", 5)) {
333 tmp_u64 = g_ascii_strtoull(keys[j] + 5, NULL, 10);
334 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
338 ch = g_slist_nth_data(sdi->channels, tmp_u64 - 1);
343 val = g_key_file_get_string(kf, sections[i],
353 }
else if (!strncmp(keys[j],
"analog", 6)) {
354 tmp_u64 = g_ascii_strtoull(keys[j]+6, NULL, 10);
355 if (!sdi || tmp_u64 == 0 || tmp_u64 > G_MAXINT) {
360 for (l = sdi->channels; l; l = l->next) {
362 if ((guint64)ch->
index == tmp_u64 - 1)
371 val = g_key_file_get_string(kf, sections[i],
386 g_strfreev(sections);
390 sr_err(
"Failed to parse metadata: %s", error->message);
Information on single channel.
int sr_dev_open(struct sr_dev_inst *sdi)
Open the specified device instance.
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.
Channel type is analog channel.
int sr_dev_channel_enable(struct sr_channel *channel, gboolean state)
Enable or disable a channel.
#define SR_MAX_CHANNELNAME_LEN
The device supports specifying a capturefile to inject.
The device instance is live, but not in use.
int index
The index of this channel, starting at 0.
int sr_dev_channel_name_set(struct sr_channel *channel, const char *name)
Set the name of the specified channel.
Channel type is logic channel.
The device supports setting its samplerate, in Hz.
Generic/unspecified error.
The device supports setting the number of logic channels.
The device supports specifying the capturefile unit size.
Opaque structure representing a libsigrok session.
Opaque structure representing a libsigrok context.
int sr_session_dev_add(struct sr_session *session, struct sr_dev_inst *sdi)
Add a device instance to a session.
int sr_session_new(struct sr_context *ctx, struct sr_session **new_session)
Create a new session.
The public libsigrok header file to be used by frontends.
int sr_session_load(struct sr_context *ctx, const char *filename, struct sr_session **session)
Load the session from the specified filename.
The device supports setting the number of analog channels.
int sr_parse_sizestring(const char *sizestring, uint64_t *size)
Convert a "natural" string representation of a size value to uint64_t.