]> sigrok.org Git - libsigrok.git/blobdiff - src/output/srzip.c
output/srzip: Minor whitespace fixes.
[libsigrok.git] / src / output / srzip.c
index c562e60401654fe2bd18bd66e678c1e20e529839..aa3c37577d33fbe17dbf576f79f8da9986017993 100644 (file)
@@ -33,6 +33,7 @@ struct out_context {
        gboolean zip_created;
        uint64_t samplerate;
        char *filename;
+       gint min_analog_index;
 };
 
 static int init(struct sr_output *o, GHashTable *options)
@@ -65,6 +66,7 @@ static int zip_create(const struct sr_output *o)
        const char *devgroup;
        char *s, *metabuf;
        gsize metalen;
+       guint logic_channels = 0, analog_channels = 0;
 
        outc = o->priv;
 
@@ -82,7 +84,7 @@ static int zip_create(const struct sr_output *o)
 
        /* "version" */
        versrc = zip_source_buffer(zipfile, "2", 1, FALSE);
-       if (zip_file_add(zipfile, "version", versrc, 0) < 0) {
+       if (zip_add(zipfile, "version", versrc) < 0) {
                sr_err("Error saving version into zipfile: %s",
                        zip_strerror(zipfile));
                zip_source_free(versrc);
@@ -99,26 +101,51 @@ static int zip_create(const struct sr_output *o)
        devgroup = "device 1";
        g_key_file_set_string(meta, devgroup, "capturefile", "logic-1");
 
-       g_key_file_set_integer(meta, devgroup, "total probes",
-                       g_slist_length(o->sdi->channels));
-
        s = sr_samplerate_string(outc->samplerate);
        g_key_file_set_string(meta, devgroup, "samplerate", s);
        g_free(s);
 
+       outc->min_analog_index = -1;
+
        for (l = o->sdi->channels; l; l = l->next) {
                ch = l->data;
-               if (ch->enabled && ch->type == SR_CHANNEL_LOGIC) {
+               switch (ch->type) {
+               case SR_CHANNEL_LOGIC:
+                       logic_channels++;
+                       break;
+               case SR_CHANNEL_ANALOG:
+                       if (outc->min_analog_index == -1 ||
+                                       ch->index < outc->min_analog_index)
+                               outc->min_analog_index = ch->index;
+                       analog_channels++;
+                       break;
+               }
+       }
+
+       g_key_file_set_integer(meta, devgroup, "total probes", logic_channels);
+       g_key_file_set_integer(meta, devgroup, "total analog", analog_channels);
+
+       for (l = o->sdi->channels; l; l = l->next) {
+               ch = l->data;
+               switch (ch->type) {
+               case SR_CHANNEL_LOGIC:
                        s = g_strdup_printf("probe%d", ch->index + 1);
-                       g_key_file_set_string(meta, devgroup, s, ch->name);
-                       g_free(s);
+                       break;
+               case SR_CHANNEL_ANALOG:
+                       s = g_strdup_printf("analog%d",
+                                       ch->index - outc->min_analog_index + 1);
+                       break;
                }
+               if (ch->enabled)
+                       g_key_file_set_string(meta, devgroup, s, ch->name);
+               g_free(s);
        }
+
        metabuf = g_key_file_to_data(meta, &metalen, NULL);
        g_key_file_free(meta);
 
        metasrc = zip_source_buffer(zipfile, metabuf, metalen, FALSE);
-       if (zip_file_add(zipfile, "metadata", metasrc, 0) < 0) {
+       if (zip_add(zipfile, "metadata", metasrc) < 0) {
                sr_err("Error saving metadata into zipfile: %s",
                        zip_strerror(zipfile));
                zip_source_free(metasrc);
@@ -255,12 +282,89 @@ static int zip_append(const struct sr_output *o, unsigned char *buf,
        return SR_OK;
 }
 
+static int zip_append_analog(const struct sr_output *o,
+               const struct sr_datafeed_analog *analog)
+{
+       struct out_context *outc;
+       struct zip *archive;
+       struct zip_source *analogsrc;
+       int64_t i, num_files;
+       struct zip_stat zs;
+       uint64_t chunk_num;
+       const char *entry_name;
+       char *basename;
+       gsize baselen;
+       struct sr_channel *channel;
+       float *chunkbuf;
+       gsize chunksize;
+       char *chunkname;
+       unsigned int next_chunk_num;
+
+       outc = o->priv;
+       if (!(archive = zip_open(outc->filename, 0, NULL)))
+               return SR_ERR;
+
+       if (zip_stat(archive, "metadata", 0, &zs) < 0) {
+               sr_err("Failed to open metadata: %s", zip_strerror(archive));
+               zip_discard(archive);
+               return SR_ERR;
+       }
+
+       /* TODO: support packets covering multiple channels */
+       if (g_slist_length(analog->meaning->channels) != 1) {
+               sr_err("Analog packets covering multiple channels not supported yet");
+               return SR_ERR;
+       }
+       channel = analog->meaning->channels->data;
+
+       basename = g_strdup_printf("analog-1-%u",
+                       channel->index - outc->min_analog_index + 1);
+       baselen = strlen(basename);
+       next_chunk_num = 1;
+       num_files = zip_get_num_entries(archive, 0);
+       for (i = 0; i < num_files; i++) {
+               entry_name = zip_get_name(archive, i, 0);
+               if (!entry_name || strncmp(entry_name, basename, baselen) != 0) {
+                       continue;
+               } else if (entry_name[baselen] == '-') {
+                       chunk_num = g_ascii_strtoull(entry_name + baselen + 1, NULL, 10);
+                       if (chunk_num < G_MAXINT && chunk_num >= next_chunk_num)
+                               next_chunk_num = chunk_num + 1;
+               }
+       }
+
+       chunksize = sizeof(float) * analog->num_samples;
+       if (!(chunkbuf = g_try_malloc(chunksize)))
+               return SR_ERR;
+       if (sr_analog_to_float(analog, chunkbuf) != SR_OK)
+               return SR_ERR;
+
+       analogsrc = zip_source_buffer(archive, chunkbuf, chunksize, FALSE);
+       chunkname = g_strdup_printf("%s-%u", basename, next_chunk_num);
+       i = zip_add(archive, chunkname, analogsrc);
+       g_free(chunkname);
+       if (i < 0) {
+               sr_err("Failed to add chunk '%s': %s", chunkname, zip_strerror(archive));
+               zip_source_free(analogsrc);
+               zip_discard(archive);
+               return SR_ERR;
+       }
+       if (zip_close(archive) < 0) {
+               sr_err("Error saving session file: %s", zip_strerror(archive));
+               zip_discard(archive);
+               return SR_ERR;
+       }
+
+       return SR_OK;
+}
+
 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
                GString **out)
 {
        struct out_context *outc;
        const struct sr_datafeed_meta *meta;
        const struct sr_datafeed_logic *logic;
+       const struct sr_datafeed_analog *analog;
        const struct sr_config *src;
        GSList *l;
 
@@ -291,23 +395,22 @@ static int receive(const struct sr_output *o, const struct sr_datafeed_packet *p
                if (ret != SR_OK)
                        return ret;
                break;
+       case SR_DF_ANALOG:
+               if (!outc->zip_created) {
+                       if ((ret = zip_create(o)) != SR_OK)
+                               return ret;
+                       outc->zip_created = TRUE;
+               }
+               analog = packet->payload;
+               ret = zip_append_analog(o, analog);
+               if (ret != SR_OK)
+                       return ret;
+               break;
        }
 
        return SR_OK;
 }
 
-static int cleanup(struct sr_output *o)
-{
-       struct out_context *outc;
-
-       outc = o->priv;
-       g_free(outc->filename);
-       g_free(outc);
-       o->priv = NULL;
-
-       return SR_OK;
-}
-
 static struct sr_option options[] = {
        ALL_ZERO
 };
@@ -320,6 +423,19 @@ static const struct sr_option *get_options(void)
        return options;
 }
 
+static int cleanup(struct sr_output *o)
+{
+       struct out_context *outc;
+
+       outc = o->priv;
+       g_variant_unref(options[0].def);
+       g_free(outc->filename);
+       g_free(outc);
+       o->priv = NULL;
+
+       return SR_OK;
+}
+
 SR_PRIV struct sr_output_module output_srzip = {
        .id = "srzip",
        .name = "srzip",