]> sigrok.org Git - libsigrok.git/blobdiff - session_file.c
build: Portability fixes.
[libsigrok.git] / session_file.c
index 545abcfdbd4fcc198bf933aeb2bf113baee54471..1c1cee5d68c9c9defe1521ef02a2b9a3f5d9260c 100644 (file)
@@ -30,7 +30,9 @@
 #include "libsigrok.h"
 #include "libsigrok-internal.h"
 
+/** @cond PRIVATE */
 #define LOG_PREFIX "session-file"
+/** @endcond */
 
 /**
  * @file
@@ -81,10 +83,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) {
@@ -104,7 +107,7 @@ SR_PRIV int sr_sessionfile_check(const char *filename)
  *         SR_ERR_MALLOC upon memory allocation errors, or SR_ERR upon
  *         other errors.
  */
-SR_API int sr_session_load(const char *filename)
+SR_API int sr_session_load(const char *filename, struct sr_session **session)
 {
        GKeyFile *kf;
        GPtrArray *capturefiles;
@@ -112,11 +115,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 devcnt, ret, 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_CHANNELNAME_LEN + 1];
 
        if ((ret = sr_sessionfile_check(filename)) != SR_OK)
                return ret;
@@ -142,7 +145,8 @@ SR_API int sr_session_load(const char *filename)
                return SR_ERR;
        }
 
-       sr_session_new();
+       if ((ret = sr_session_new(session)) != SR_OK)
+               return ret;
 
        devcnt = 0;
        capturefiles = g_ptr_array_new_with_free_func(g_free);
@@ -154,7 +158,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);
@@ -165,7 +169,7 @@ SR_API int sr_session_load(const char *filename)
                                                /* first device, init the driver */
                                                sdi->driver->init(NULL);
                                        sr_dev_open(sdi);
-                                       sr_session_dev_add(sdi);
+                                       sr_session_dev_add(*session, sdi);
                                        sdi->driver->config_set(SR_CONF_SESSIONFILE,
                                                        g_variant_new_string(filename), sdi, NULL);
                                        sdi->driver->config_set(SR_CONF_CAPTUREFILE,
@@ -180,33 +184,30 @@ 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);
-                                       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)))
+                                       total_channels = strtoull(val, NULL, 10);
+                                       sdi->driver->config_set(SR_CONF_NUM_LOGIC_CHANNELS,
+                                                       g_variant_new_uint64(total_channels), sdi, NULL);
+                                       for (p = 0; p < total_channels; p++) {
+                                               snprintf(channelname, SR_MAX_CHANNELNAME_LEN, "%" PRIu64, p);
+                                               if (!(ch = sr_channel_new(p, SR_CHANNEL_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);
+                                       sr_dev_channel_name_set(sdi, tmp_u64 - 1, val);
                                }
                        }
                        g_strfreev(keys);
-                       /* Disable probes not specifically listed. */
-                       if (total_probes)
-                               for (p = enabled_probes; p < total_probes; p++)
-                                       sr_dev_probe_enable(sdi, p, FALSE);
+                       /* Disable channels not specifically listed. */
+                       if (total_channels)
+                               for (p = enabled_channels; p < total_channels; p++)
+                                       sr_dev_channel_enable(sdi, p, FALSE);
                }
                devcnt++;
        }
@@ -217,9 +218,9 @@ SR_API int sr_session_load(const char *filename)
 }
 
 /**
- * Save the current session to the specified file.
+ * Save a session to the specified file.
  *
- * @param filename The name of the filename to save the current session as.
+ * @param filename The name of the filename to save the session as.
  *                 Must not be NULL.
  * @param sdi The device instance from which the data was captured.
  * @param buf The data to be saved.
@@ -229,16 +230,19 @@ SR_API int sr_session_load(const char *filename)
  * @retval SR_OK Success
  * @retval SR_ERR_ARG Invalid arguments
  * @retval SR_ERR Other errors
+ *
+ * @since 0.2.0
  */
-SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
-               unsigned char *buf, int unitsize, int units)
+SR_API int sr_session_save(struct sr_session *session, 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,24 +253,25 @@ 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->type != SR_PROBE_LOGIC)
+       for (l = sdi->channels; l; l = l->next) {
+               ch = l->data;
+               if (ch->type != SR_CHANNEL_LOGIC)
                        continue;
-               if (probe->enabled != TRUE)
+               if (ch->enabled != TRUE)
                        continue;
-               if (!probe->name)
+               if (!ch->name)
                        continue;
                /* Just borrowing the ptr. */
-               probe_names[cnt++] = probe->name;
+               channel_names[cnt++] = ch->name;
        }
 
-       if ((ret = sr_session_save_init(filename, samplerate, probe_names)) != SR_OK)
+       if ((ret = sr_session_save_init(session, filename, samplerate,
+                       channel_names)) != SR_OK)
                return ret;
 
-       ret = sr_session_append(filename, buf, unitsize, units);
+       ret = sr_session_append(session, filename, buf, unitsize, units);
 
        return ret;
 }
@@ -274,18 +279,20 @@ SR_API int sr_session_save(const char *filename, const struct sr_dev_inst *sdi,
 /**
  * Initialize a saved session file.
  *
- * @param filename The name of the filename to save the current session as.
+ * @param filename The name of the filename to save the 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
+ *
+ * @since 0.3.0
  */
-SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
-               char **probes)
+SR_API int sr_session_save_init(struct sr_session *session,
+               const char *filename, uint64_t samplerate, char **channels)
 {
        FILE *meta;
        struct zip *zipfile;
@@ -293,6 +300,8 @@ SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
        int tmpfile, cnt, ret, i;
        char version[1], metafile[32], *s;
 
+       (void) session;
+
        if (!filename) {
                sr_err("%s: filename was NULL", __func__);
                return SR_ERR_ARG;
@@ -304,7 +313,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) {
@@ -328,15 +337,15 @@ 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);
 
@@ -374,9 +383,11 @@ SR_API int sr_session_save_init(const char *filename, uint64_t samplerate,
  * @retval SR_OK Success
  * @retval SR_ERR_ARG Invalid arguments
  * @retval SR_ERR Other errors
+ *
+ * @since 0.3.0
  */
-SR_API int sr_session_append(const char *filename, unsigned char *buf,
-               int unitsize, int units)
+SR_API int sr_session_append(struct sr_session *session, const char *filename,
+               unsigned char *buf, int unitsize, int units)
 {
        struct zip *archive;
        struct zip_source *logicsrc;
@@ -391,6 +402,8 @@ SR_API int sr_session_append(const char *filename, unsigned char *buf,
        const char *entry_name;
        char *metafile, tmpname[32], chunkname[16];
 
+       (void) session;
+
        if ((ret = sr_sessionfile_check(filename)) != SR_OK)
                return ret;