25 #include <glib/gstdio.h>
28 #include "libsigrok-internal.h"
31 #define LOG_PREFIX "session-file: "
32 #define sr_log(l, s, args...) sr_log(l, LOG_PREFIX s, ## args)
33 #define sr_spew(s, args...) sr_spew(LOG_PREFIX s, ## args)
34 #define sr_dbg(s, args...) sr_dbg(LOG_PREFIX s, ## args)
35 #define sr_info(s, args...) sr_info(LOG_PREFIX s, ## args)
36 #define sr_warn(s, args...) sr_warn(LOG_PREFIX s, ## args)
37 #define sr_err(s, args...) sr_err(LOG_PREFIX s, ## args)
66 GPtrArray *capturefiles;
72 int ret, probenum, devcnt, version, i, j;
73 uint64_t tmp_u64, total_probes, enabled_probes, p;
74 char **sections, **keys, *metafile, *val, s[11];
78 sr_err(
"%s: filename was NULL", __func__);
82 if (!(archive = zip_open(filename, 0, &ret))) {
83 sr_dbg(
"Failed to open session file: zip error %d", ret);
89 if (!(zf = zip_fopen(archive,
"version", 0))) {
90 sr_dbg(
"Not a sigrok session file.");
93 if ((ret = zip_fread(zf, s, 10)) == -1) {
94 sr_dbg(
"Not a valid sigrok session file.");
99 version = strtoull(s, NULL, 10);
101 sr_dbg(
"Not a valid sigrok session file version.");
106 if (zip_stat(archive,
"metadata", 0, &zs) == -1) {
107 sr_dbg(
"Not a valid sigrok session file.");
111 if (!(metafile = g_try_malloc(zs.size))) {
112 sr_err(
"%s: metafile malloc failed", __func__);
116 zf = zip_fopen_index(archive, zs.index, 0);
117 zip_fread(zf, metafile, zs.size);
120 kf = g_key_file_new();
121 if (!g_key_file_load_from_data(kf, metafile, zs.size, 0, NULL)) {
122 sr_dbg(
"Failed to parse metadata.");
129 capturefiles = g_ptr_array_new_with_free_func(g_free);
130 sections = g_key_file_get_groups(kf, NULL);
131 for (i = 0; sections[i]; i++) {
132 if (!strcmp(sections[i],
"global"))
135 if (!strncmp(sections[i],
"device ", 7)) {
138 enabled_probes = total_probes = 0;
139 keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
140 for (j = 0; keys[j]; j++) {
141 val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
142 if (!strcmp(keys[j],
"capturefile")) {
143 sdi = sr_dev_inst_new(devcnt,
SR_ST_ACTIVE, NULL, NULL, NULL);
151 g_variant_new_string(filename), sdi);
153 g_variant_new_string(val), sdi);
154 g_ptr_array_add(capturefiles, val);
155 }
else if (!strcmp(keys[j],
"samplerate")) {
158 g_variant_new_uint64(tmp_u64), sdi);
159 }
else if (!strcmp(keys[j],
"unitsize")) {
160 tmp_u64 = strtoull(val, NULL, 10);
162 g_variant_new_uint64(tmp_u64), sdi);
163 }
else if (!strcmp(keys[j],
"total probes")) {
164 total_probes = strtoull(val, NULL, 10);
166 g_variant_new_uint64(total_probes), sdi);
167 for (p = 0; p < total_probes; p++) {
174 }
else if (!strncmp(keys[j],
"probe", 5)) {
178 tmp_u64 = strtoul(keys[j]+5, NULL, 10);
181 }
else if (!strncmp(keys[j],
"trigger", 7)) {
182 probenum = strtoul(keys[j]+7, NULL, 10);
189 for (p = enabled_probes; p < total_probes; p++)
194 g_strfreev(sections);
214 unsigned char *buf,
int unitsize,
int units)
221 struct zip_source *versrc, *metasrc, *logicsrc;
222 int tmpfile, ret, probecnt;
224 char version[1], rawname[16], metafile[32], *s;
227 sr_err(
"%s: filename was NULL", __func__);
233 if (!(zipfile = zip_open(filename, ZIP_CREATE, &ret)))
238 if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
240 if (zip_add(zipfile,
"version", versrc) == -1) {
241 sr_info(
"error saving version into zipfile: %s",
242 zip_strerror(zipfile));
247 strcpy(metafile,
"sigrok-meta-XXXXXX");
248 if ((tmpfile = g_mkstemp(metafile)) == -1)
251 meta = g_fopen(metafile,
"wb");
252 fprintf(meta,
"[global]\n");
253 fprintf(meta,
"sigrok version = %s\n", PACKAGE_VERSION);
256 fprintf(meta,
"[device 1]\n");
258 fprintf(meta,
"driver = %s\n", sdi->
driver->
name);
261 fprintf(meta,
"capturefile = logic-1\n");
262 fprintf(meta,
"unitsize = %d\n", unitsize);
263 fprintf(meta,
"total probes = %d\n", g_slist_length(sdi->
probes));
266 &gvar, sdi) ==
SR_OK) {
267 samplerate = g_variant_get_uint64(gvar);
269 fprintf(meta,
"samplerate = %s\n", s);
271 g_variant_unref(gvar);
275 for (l = sdi->
probes; l; l = l->next) {
279 fprintf(meta,
"probe%d = %s\n", probecnt, probe->
name);
281 fprintf(meta,
" trigger%d = %s\n", probecnt, probe->
trigger);
286 if (!(logicsrc = zip_source_buffer(zipfile, buf,
287 units * unitsize, FALSE)))
289 snprintf(rawname, 15,
"logic-1");
290 if (zip_add(zipfile, rawname, logicsrc) == -1)
294 if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
296 if (zip_add(zipfile,
"metadata", metasrc) == -1)
299 if ((ret = zip_close(zipfile)) == -1) {
300 sr_info(
"error saving zipfile: %s", zip_strerror(zipfile));
The device instance is actively in use in a session.
int sr_dev_probe_name_set(const struct sr_dev_inst *sdi, int probenum, const char *name)
Set the name of the specified probe in the specified device.
int(* init)(struct sr_context *sr_ctx)
int sr_parse_sizestring(const char *sizestring, uint64_t *size)
Convert a "natural" string representation of a size value to uint64_t.
#define sr_dbg(s, args...)
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.
The device supports setting its samplerate, in Hz.
The public libsigrok header file to be used by frontends.
Malloc/calloc/realloc error.
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)
The device supports specifying a capturefile to inject.
Generic/unspecified error.
int sr_dev_probe_enable(const struct sr_dev_inst *sdi, int probenum, gboolean state)
Enable or disable a probe on the specified device.
#define sr_info(s, args...)
gboolean sr_dev_has_option(const struct sr_dev_inst *sdi, int key)
Determine whether the specified device instance has the specified capability.
int sr_session_load(const char *filename)
Load the session from the specified filename.
struct sr_dev_driver * driver
#define sr_err(s, args...)
struct sr_session * session
int sr_dev_open(struct sr_dev_inst *sdi)
Open the specified device.
struct sr_session * sr_session_new(void)
Create a new session.
The device supports setting the number of probes.
int sr_session_dev_add(const struct sr_dev_inst *sdi)
Add a device instance to the current session.
The device supports specifying the capturefile unit size.
#define SR_MAX_PROBENAME_LEN
int sr_dev_trigger_set(const struct sr_dev_inst *sdi, int probenum, const char *trigger)
Add a trigger to the specified device (and the specified probe).
SR_PRIV struct sr_dev_driver session_driver
int sr_config_get(const struct sr_dev_driver *driver, int key, GVariant **data, const struct sr_dev_inst *sdi)
Returns information about the given driver or device instance.