24 #include <sys/types.h>
28 #include <glib/gstdio.h>
31 #include "libsigrok-internal.h"
34 #define LOG_PREFIX "session-file"
53 SR_PRIV int sr_sessionfile_check(
const char *filename)
65 if (stat(filename, &st) == -1) {
66 sr_err(
"Couldn't stat %s: %s", filename, strerror(errno));
70 if (!(archive = zip_open(filename, 0, &ret)))
77 if (!(zf = zip_fopen(archive,
"version", 0))) {
78 sr_dbg(
"Not a sigrok session file: no version found.");
81 if ((ret = zip_fread(zf, s, 10)) == -1)
85 version = strtoull(s, NULL, 10);
87 sr_dbg(
"Cannot handle sigrok session file version %d.", version);
90 sr_spew(
"Detected sigrok session file version %d.", version);
93 if (zip_stat(archive,
"metadata", 0, &zs) == -1) {
94 sr_dbg(
"Not a valid sigrok session file.");
113 GPtrArray *capturefiles;
119 int ret, channelnum, devcnt, i, j;
120 uint64_t tmp_u64, total_channels, enabled_channels, p;
121 char **sections, **keys, *metafile, *val;
124 if ((ret = sr_sessionfile_check(filename)) !=
SR_OK)
127 if (!(archive = zip_open(filename, 0, &ret)))
130 if (zip_stat(archive,
"metadata", 0, &zs) == -1)
133 if (!(metafile = g_try_malloc(zs.size))) {
134 sr_err(
"%s: metafile malloc failed", __func__);
138 zf = zip_fopen_index(archive, zs.index, 0);
139 zip_fread(zf, metafile, zs.size);
142 kf = g_key_file_new();
143 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
144 sr_dbg(
"Failed to parse metadata.");
151 capturefiles = g_ptr_array_new_with_free_func(g_free);
152 sections = g_key_file_get_groups(kf, NULL);
153 for (i = 0; sections[i]; i++) {
154 if (!strcmp(sections[i],
"global"))
157 if (!strncmp(sections[i],
"device ", 7)) {
160 enabled_channels = total_channels = 0;
161 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
162 for (j = 0; keys[j]; j++) {
163 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
164 if (!strcmp(keys[j],
"capturefile")) {
165 sdi = sr_dev_inst_new(devcnt,
SR_ST_ACTIVE, NULL, NULL, NULL);
173 g_variant_new_string(filename), sdi, NULL);
175 g_variant_new_string(val), sdi, NULL);
176 g_ptr_array_add(capturefiles, val);
177 }
else if (!strcmp(keys[j],
"samplerate")) {
180 g_variant_new_uint64(tmp_u64), sdi, NULL);
181 }
else if (!strcmp(keys[j],
"unitsize")) {
182 tmp_u64 = strtoull(val, NULL, 10);
184 g_variant_new_uint64(tmp_u64), sdi, NULL);
185 }
else if (!strcmp(keys[j],
"total probes")) {
186 total_channels = strtoull(val, NULL, 10);
188 g_variant_new_uint64(total_channels), sdi, NULL);
189 for (p = 0; p < total_channels; p++) {
196 }
else if (!strncmp(keys[j],
"probe", 5)) {
200 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
203 }
else if (!strncmp(keys[j],
"trigger", 7)) {
204 channelnum = strtoul(keys[j]+7, NULL, 10);
211 for (p = enabled_channels; p < total_channels; p++)
216 g_strfreev(sections);
239 unsigned char *buf,
int unitsize,
int units)
246 char **channel_names;
252 samplerate = g_variant_get_uint64(gvar);
253 g_variant_unref(gvar);
257 channel_names = g_malloc0(
sizeof(
char *) * (g_slist_length(sdi->
channels) + 1));
259 for (l = sdi->
channels; l; l = l->next) {
268 channel_names[cnt++] = ch->
name;
299 struct zip_source *versrc, *metasrc;
300 int tmpfile, cnt, ret, i;
301 char version[1], metafile[32], *s;
304 sr_err(
"%s: filename was NULL", __func__);
310 if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
315 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
317 if (zip_add(zipfile,
"version", versrc) == -1) {
318 sr_info(
"error saving version into zipfile: %s",
319 zip_strerror(zipfile));
324 strcpy(metafile,
"sigrok-meta-XXXXXX");
325 if ((tmpfile = g_mkstemp(metafile)) == -1)
328 meta = g_fopen(metafile,
"wb");
329 fprintf(meta,
"[global]\n");
330 fprintf(meta,
"sigrok version = %s\n", PACKAGE_VERSION);
333 fprintf(meta,
"[device 1]\n");
336 fprintf(meta,
"capturefile = logic-1\n");
338 for (i = 0; channels[i]; i++)
340 fprintf(meta,
"total probes = %d\n", cnt);
342 fprintf(meta,
"samplerate = %s\n", s);
345 for (i = 0; channels[i]; i++)
346 fprintf(meta,
"probe%d = %s\n", i + 1, channels[i]);
350 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) {
354 if (zip_add(zipfile,
"metadata", metasrc) == -1) {
359 if ((ret = zip_close(zipfile)) == -1) {
360 sr_info(
"error saving zipfile: %s", zip_strerror(zipfile));
388 int unitsize,
int units)
391 struct zip_source *logicsrc;
392 zip_int64_t num_files;
395 struct zip_source *metasrc;
399 int chunk_num, next_chunk_num, tmpfile, ret, i;
400 const char *entry_name;
401 char *metafile, tmpname[32], chunkname[16];
403 if ((ret = sr_sessionfile_check(filename)) !=
SR_OK)
406 if (!(archive = zip_open(filename, 0, &ret)))
409 if (zip_stat(archive,
"metadata", 0, &zs) == -1)
412 metafile = g_malloc(zs.size);
413 zf = zip_fopen_index(archive, zs.index, 0);
414 zip_fread(zf, metafile, zs.size);
422 kf = g_key_file_new();
423 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, &error)) {
424 sr_err(
"Failed to parse metadata: %s.", error->message);
429 if (!g_key_file_has_key(kf,
"device 1",
"unitsize", &error)) {
430 if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
431 sr_err(
"Failed to check unitsize key: %s", error ? error->message :
"?");
435 g_key_file_set_integer(kf,
"device 1",
"unitsize", unitsize);
436 metafile = g_key_file_to_data(kf, &len, &error);
437 strcpy(tmpname,
"sigrok-meta-XXXXXX");
438 if ((tmpfile = g_mkstemp(tmpname)) == -1)
440 if (write(tmpfile, metafile, len) < 0) {
441 sr_dbg(
"Failed to create new metadata: %s", strerror(errno));
447 if (!(metasrc = zip_source_file(archive, tmpname, 0, -1))) {
448 sr_err(
"Failed to create zip source for metadata.");
453 if (zip_replace(archive, zs.index, metasrc) == -1) {
454 sr_err(
"Failed to replace metadata file.");
464 num_files = zip_get_num_entries(archive, 0);
465 for (i = 0; i < num_files; i++) {
466 entry_name = zip_get_name(archive, i, 0);
467 if (strncmp(entry_name,
"logic-1", 7))
469 if (strlen(entry_name) == 7) {
472 if (zip_rename(archive, i,
"logic-1-1") == -1) {
473 sr_err(
"Failed to rename 'logic-1' to 'logic-1-1'.");
479 }
else if (strlen(entry_name) > 8 && entry_name[7] ==
'-') {
480 chunk_num = strtoull(entry_name + 8, NULL, 10);
481 if (chunk_num >= next_chunk_num)
482 next_chunk_num = chunk_num + 1;
485 snprintf(chunkname, 15,
"logic-1-%d", next_chunk_num);
486 if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE))) {
490 if (zip_add(archive, chunkname, logicsrc) == -1) {
494 if ((ret = zip_close(archive)) == -1) {
495 sr_info(
"error saving session file: %s", zip_strerror(archive));
Generic/unspecified error.
struct sr_session * session
int sr_dev_channel_name_set(const struct sr_dev_inst *sdi, int channelnum, const char *name)
Set the name of the specified channel in the specified device.
The device supports setting the number of logic channels.
int sr_session_dev_add(const struct sr_dev_inst *sdi)
Add a device instance to the current session.
SR_PRIV struct sr_dev_driver session_driver
Information on single channel.
Channel type is logic channel.
gboolean enabled
Is this channel enabled?
The public libsigrok header file to be used by frontends.
GSList * channels
List of channels.
The device supports specifying a capturefile to inject.
The device supports specifying the capturefile unit size.
int sr_session_append(const char *filename, unsigned char *buf, int unitsize, int units)
Append data to an existing session file.
gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
Determine whether the specified device instance has the specified capability.
The device instance is actively in use in a session.
Opaque data structure representing a libsigrok session.
int sr_dev_channel_enable(const struct sr_dev_inst *sdi, int channelnum, gboolean state)
Enable or disable a channel on the specified device.
The device supports setting its samplerate, in Hz.
int sr_config_get(const struct sr_dev_driver *driver, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg, int key, GVariant **data)
Query value of a configuration key at the given driver or device instance.
Malloc/calloc/realloc error.
int sr_dev_open(struct sr_dev_inst *sdi)
Open the specified device.
int sr_session_save_init(const char *filename, uint64_t samplerate, char **channels)
Initialize a saved session file.
int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int channelnum, const char *trigger)
Add a trigger to the specified device (and the specified channel).
int type
Channel type (SR_CHANNEL_LOGIC, ...)
int(* init)(struct sr_context *sr_ctx)
Called when driver is loaded, e.g.
#define SR_MAX_CHANNELNAME_LEN
char * name
Name of channel.
struct sr_dev_driver * driver
Device driver.
struct sr_session * sr_session_new(void)
Create a new session.
int sr_parse_sizestring(const char *sizestring, uint64_t *size)
Convert a "natural" string representation of a size value to uint64_t.
char * sr_samplerate_string(uint64_t samplerate)
Convert a numeric samplerate value to its "natural" string representation.
int(* config_set)(int id, GVariant *data, const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
Set value of a configuration key in driver or a given device instance.
int sr_session_save(const char *filename, const struct sr_dev_inst *sdi, unsigned char *buf, int unitsize, int units)
Save the current session to the specified file.
int sr_session_load(const char *filename)
Load the session from the specified filename.