]> sigrok.org Git - libsigrok.git/blobdiff - session_file.c
Replace 'probe' with 'channel' in most places.
[libsigrok.git] / session_file.c
index 4e3d05bb395331b5936a8f530d92cc8fc198246a..da11321c09ceba009afd9e00cbc87efd2b14c882 100644 (file)
@@ -81,10 +81,11 @@ SR_PRIV int sr_sessionfile_check(const char *filename)
        zip_fclose(zf);
        s[ret] = 0;
        version = strtoull(s, NULL, 10);
-       if (version != 1) {
+       if (version > 2) {
                sr_dbg("Cannot handle sigrok session file version %d.", version);
                return SR_ERR;
        }
+       sr_spew("Detected sigrok session file version %d.", version);
 
        /* read "metadata" */
        if (zip_stat(archive, "metadata", 0, &zs) == -1) {
@@ -112,11 +113,11 @@ SR_API int sr_session_load(const char *filename)
        struct zip_file *zf;
        struct zip_stat zs;
        struct sr_dev_inst *sdi;
-       struct sr_probe *probe;
-       int ret, probenum, devcnt, i, j;
-       uint64_t tmp_u64, total_probes, enabled_probes, p;
+       struct sr_channel *ch;
+       int ret, channelnum, devcnt, i, j;
+       uint64_t tmp_u64, total_channels, enabled_channels, p;
        char **sections, **keys, *metafile, *val;
-       char probename[SR_MAX_PROBENAME_LEN + 1];
+       char channelname[SR_MAX_PROBENAME_LEN + 1];
 
        if ((ret = sr_sessionfile_check(filename)) != SR_OK)
                return ret;
@@ -154,7 +155,7 @@ SR_API int sr_session_load(const char *filename)
                if (!strncmp(sections[i], "device ", 7)) {
                        /* device section */
                        sdi = NULL;
-                       enabled_probes = total_probes = 0;
+                       enabled_channels = total_channels = 0;
                        keys = g_key_file_get_keys(kf, sections[i], NULL, NULL);
                        for (j = 0; keys[j]; j++) {
                                val = g_key_file_get_string(kf, sections[i], keys[j], NULL);
@@ -180,32 +181,32 @@ SR_API int sr_session_load(const char *filename)
                                        sdi->driver->config_set(SR_CONF_CAPTURE_UNITSIZE,
                                                        g_variant_new_uint64(tmp_u64), sdi, NULL);
                                } else if (!strcmp(keys[j], "total probes")) {
-                                       total_probes = strtoull(val, NULL, 10);
+                                       total_channels = strtoull(val, NULL, 10);
                                        sdi->driver->config_set(SR_CONF_NUM_LOGIC_PROBES,
-                                                       g_variant_new_uint64(total_probes), sdi, NULL);
-                                       for (p = 0; p < total_probes; p++) {
-                                               snprintf(probename, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
-                                               if (!(probe = sr_probe_new(p, SR_PROBE_LOGIC, TRUE,
-                                                               probename)))
+                                                       g_variant_new_uint64(total_channels), sdi, NULL);
+                                       for (p = 0; p < total_channels; p++) {
+                                               snprintf(channelname, SR_MAX_PROBENAME_LEN, "%" PRIu64, p);
+                                               if (!(ch = sr_probe_new(p, SR_PROBE_LOGIC, TRUE,
+                                                               channelname)))
                                                        return SR_ERR;
-                                               sdi->probes = g_slist_append(sdi->probes, probe);
+                                               sdi->channels = g_slist_append(sdi->channels, ch);
                                        }
                                } else if (!strncmp(keys[j], "probe", 5)) {
                                        if (!sdi)
                                                continue;
-                                       enabled_probes++;
+                                       enabled_channels++;
                                        tmp_u64 = strtoul(keys[j]+5, NULL, 10);
                                        /* sr_session_save() */
                                        sr_dev_probe_name_set(sdi, tmp_u64 - 1, val);
                                } else if (!strncmp(keys[j], "trigger", 7)) {
-                                       probenum = strtoul(keys[j]+7, NULL, 10);
-                                       sr_dev_trigger_set(sdi, probenum, val);
+                                       channelnum = strtoul(keys[j]+7, NULL, 10);
+                                       sr_dev_trigger_set(sdi, channelnum, val);
                                }
                        }
                        g_strfreev(keys);
-                       /* Disable probes not specifically listed. */
-                       if (total_probes)
-                               for (p = enabled_probes; p < total_probes; p++)
+                       /* Disable channels not specifically listed. */
+                       if (total_channels)
+                               for (p = enabled_channels; p < total_channels; p++)
                                        sr_dev_probe_enable(sdi, p, FALSE);
                }
                devcnt++;
@@ -233,12 +234,12 @@ SR_API int sr_session_load(const char *filename)
 SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
                unsigned char *buf, int unitsize, int units)
 {
-       struct sr_probe *probe;
+       struct sr_channel *ch;
        GSList *l;
        GVariant *gvar;
        uint64_t samplerate;
        int cnt, ret;
-       char **probe_names;
+       char **channel_names;
 
        samplerate = 0;
        if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) {
@@ -249,18 +250,21 @@ SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
                }
        }
 
-       probe_names = g_malloc0(sizeof(char *) * (g_slist_length(sdi->probes) + 1));
+       channel_names = g_malloc0(sizeof(char *) * (g_slist_length(sdi->channels) + 1));
        cnt = 0;
-       for (l = sdi->probes; l; l = l->next) {
-               probe = l->data;
-               if (probe->enabled) {
-                       if (probe->name)
-                               /* Just borrowing the ptr. */
-                               probe_names[cnt++] = probe->name;
-               }
+       for (l = sdi->channels; l; l = l->next) {
+               ch = l->data;
+               if (ch->type != SR_PROBE_LOGIC)
+                       continue;
+               if (ch->enabled != TRUE)
+                       continue;
+               if (!ch->name)
+                       continue;
+               /* Just borrowing the ptr. */
+               channel_names[cnt++] = ch->name;
        }
 
-       if ((ret = sr_session_save_init(filename, samplerate, probe_names)) != SR_OK)
+       if ((ret = sr_session_save_init(filename, samplerate, channel_names)) != SR_OK)
                return ret;
 
        ret = sr_session_append(filename, buf, unitsize, units);
@@ -274,15 +278,15 @@ SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
  * @param filename The name of the filename to save the current session as.
  *                 Must not be NULL.
  * @param samplerate The samplerate to store for this session.
- * @param probes A NULL-terminated array of strings containing the names
- * of all the probes active in this session.
+ * @param channels A NULL-terminated array of strings containing the names
+ * of all the channels active in this session.
  *
  * @retval SR_OK Success
  * @retval SR_ERR_ARG Invalid arguments
  * @retval SR_ERR Other errors
  */
 SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
-               char **probes)
+               char **channels)
 {
        FILE *meta;
        struct zip *zipfile;
@@ -301,7 +305,7 @@ SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
                return SR_ERR;
 
        /* "version" */
-       version[0] = '1';
+       version[0] = '2';
        if (!(versrc = zip_source_buffer(zipfile, version, 1, 0)))
                return SR_ERR;
        if (zip_add(zipfile, "version", versrc) == -1) {
@@ -325,25 +329,30 @@ SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
        /* metadata */
        fprintf(meta, "capturefile = logic-1\n");
        cnt = 0;
-       for (i = 0; probes[i]; i++)
+       for (i = 0; channels[i]; i++)
                cnt++;
        fprintf(meta, "total probes = %d\n", cnt);
        s = sr_samplerate_string(samplerate);
        fprintf(meta, "samplerate = %s\n", s);
        g_free(s);
 
-       for (i = 0; probes[i]; i++)
-               fprintf(meta, "probe%d = %s\n", i + 1, probes[i]);
+       for (i = 0; channels[i]; i++)
+               fprintf(meta, "probe%d = %s\n", i + 1, channels[i]);
 
        fclose(meta);
 
-       if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1)))
+       if (!(metasrc = zip_source_file(zipfile, metafile, 0, -1))) {
+               unlink(metafile);
                return SR_ERR;
-       if (zip_add(zipfile, "metadata", metasrc) == -1)
+       }
+       if (zip_add(zipfile, "metadata", metasrc) == -1) {
+               unlink(metafile);
                return SR_ERR;
+       }
 
        if ((ret = zip_close(zipfile)) == -1) {
                sr_info("error saving zipfile: %s", zip_strerror(zipfile));
+               unlink(metafile);
                return SR_ERR;
        }
 
@@ -408,6 +417,7 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
                return SR_ERR;
        }
        g_free(metafile);
+       tmpname[0] = '\0';
        if (!g_key_file_has_key(kf, "device 1", "unitsize", &error)) {
                if (error && error->code != G_KEY_FILE_ERROR_KEY_NOT_FOUND) {
                        sr_err("Failed to check unitsize key: %s", error ? error->message : "?");
@@ -421,16 +431,21 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
                        return SR_ERR;
                if (write(tmpfile, metafile, len) < 0) {
                        sr_dbg("Failed to create new metadata: %s", strerror(errno));
+                       g_free(metafile);
+                       unlink(tmpname);
                        return SR_ERR;
                }
                close(tmpfile);
                if (!(metasrc = zip_source_file(archive, tmpname, 0, -1))) {
-                       g_free(metafile);
                        sr_err("Failed to create zip source for metadata.");
+                       g_free(metafile);
+                       unlink(tmpname);
                        return SR_ERR;
                }
                if (zip_replace(archive, zs.index, metasrc) == -1) {
                        sr_err("Failed to replace metadata file.");
+                       g_free(metafile);
+                       unlink(tmpname);
                        return SR_ERR;
                }
                g_free(metafile);
@@ -448,6 +463,7 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
                         * Rename it to "logic-1-1" * and continue with chunk 2. */
                        if (zip_rename(archive, i, "logic-1-1") == -1) {
                                sr_err("Failed to rename 'logic-1' to 'logic-1-1'.");
+                               unlink(tmpname);
                                return SR_ERR;
                        }
                        next_chunk_num = 2;
@@ -459,14 +475,20 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
                }
        }
        snprintf(chunkname, 15, "logic-1-%d", next_chunk_num);
-       if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE)))
+       if (!(logicsrc = zip_source_buffer(archive, buf, units * unitsize, FALSE))) {
+               unlink(tmpname);
                return SR_ERR;
-       if (zip_add(archive, chunkname, logicsrc) == -1)
+       }
+       if (zip_add(archive, chunkname, logicsrc) == -1) {
+               unlink(tmpname);
                return SR_ERR;
+       }
        if ((ret = zip_close(archive)) == -1) {
                sr_info("error saving session file: %s", zip_strerror(archive));
+               unlink(tmpname);
                return SR_ERR;
        }
+       unlink(tmpname);
 
        return SR_OK;
 }