]> sigrok.org Git - libsigrok.git/blob - src/output/csv.c
Build: Set local include directories in Makefile.am
[libsigrok.git] / src / output / csv.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2011 Uwe Hermann <uwe@hermann-uwe.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include "config.h" /* Needed for PACKAGE_STRING and others. */
25 #include <libsigrok/libsigrok.h>
26 #include "libsigrok-internal.h"
27
28 #define LOG_PREFIX "output/csv"
29
30 struct context {
31         unsigned int num_enabled_channels;
32         uint64_t samplerate;
33         char separator;
34         gboolean header_done;
35         struct sr_channel **channels;
36
37         /* For analog measurements split into frames, not packets. */
38         struct sr_channel **analog_channels;
39         float *analog_vals; /* Analog values stored until the end of the frame. */
40         unsigned int num_analog_channels;
41         gboolean inframe;
42 };
43
44 /*
45  * TODO:
46  *  - Option to specify delimiter character and/or string.
47  *  - Option to (not) print metadata as comments.
48  *  - Option to specify the comment character(s), e.g. # or ; or C/C++-style.
49  *  - Option to (not) print samplenumber / time as extra column.
50  *  - Option to "compress" output (only print changed samples, VCD-like).
51  *  - Option to print comma-separated bits, or whole bytes/words (for 8/16
52  *    channel LAs) as ASCII/hex etc. etc.
53  *  - Trigger support.
54  */
55
56 static int init(struct sr_output *o, GHashTable *options)
57 {
58         struct context *ctx;
59         struct sr_channel *ch;
60         GSList *l;
61         int i, j;
62
63         (void)options;
64
65         if (!o || !o->sdi)
66                 return SR_ERR_ARG;
67
68         ctx = g_malloc0(sizeof(struct context));
69         o->priv = ctx;
70         ctx->separator = ',';
71
72         /* Get the number of channels, and the unitsize. */
73         for (l = o->sdi->channels; l; l = l->next) {
74                 ch = l->data;
75                 if (ch->enabled) {
76                         if (ch->type == SR_CHANNEL_LOGIC ||
77                             ch->type == SR_CHANNEL_ANALOG)
78                                 ctx->num_enabled_channels++;
79                         if (ch->type == SR_CHANNEL_ANALOG)
80                                 ctx->num_analog_channels++;
81                 }
82         }
83         ctx->channels = g_malloc(sizeof(struct sr_channel *)
84                                         * ctx->num_enabled_channels);
85         ctx->analog_channels = g_malloc(sizeof(struct sr_channel *)
86                                         * ctx->num_analog_channels);
87         ctx->analog_vals = g_malloc(sizeof(float) * ctx->num_analog_channels);
88
89         /* Once more to map the enabled channels. */
90         for (i = 0, l = o->sdi->channels, j = 0; l; l = l->next) {
91                 ch = l->data;
92                 if (ch->enabled) {
93                         if (ch->type == SR_CHANNEL_LOGIC ||
94                             ch->type == SR_CHANNEL_ANALOG)
95                                 ctx->channels[i++] = ch;
96                         if (ch->type == SR_CHANNEL_ANALOG)
97                                 ctx->analog_channels[j++] = ch;
98                 }
99
100         }
101
102         return SR_OK;
103 }
104
105 static GString *gen_header(const struct sr_output *o)
106 {
107         struct context *ctx;
108         struct sr_channel *ch;
109         GVariant *gvar;
110         GString *header;
111         GSList *l;
112         time_t t;
113         int num_channels, i;
114         char *samplerate_s;
115
116         ctx = o->priv;
117         header = g_string_sized_new(512);
118
119         /* Some metadata */
120         t = time(NULL);
121         g_string_append_printf(header, "; CSV, generated by %s on %s",
122                         PACKAGE_STRING, ctime(&t));
123
124         /* Columns / channels */
125         num_channels = g_slist_length(o->sdi->channels);
126         g_string_append_printf(header, "; Channels (%d/%d):",
127                         ctx->num_enabled_channels, num_channels);
128         for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
129                 ch = l->data;
130                 if (ch->enabled &&
131                     (ch->type == SR_CHANNEL_LOGIC ||
132                      ch->type == SR_CHANNEL_ANALOG))
133                         g_string_append_printf(header, " %s,", ch->name);
134         }
135         if (o->sdi->channels)
136                 /* Drop last separator. */
137                 g_string_truncate(header, header->len - 1);
138         g_string_append_printf(header, "\n");
139
140         if (ctx->samplerate == 0) {
141                 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
142                                 &gvar) == SR_OK) {
143                         ctx->samplerate = g_variant_get_uint64(gvar);
144                         g_variant_unref(gvar);
145                 }
146         }
147         if (ctx->samplerate != 0) {
148                 samplerate_s = sr_samplerate_string(ctx->samplerate);
149                 g_string_append_printf(header, "; Samplerate: %s\n", samplerate_s);
150                 g_free(samplerate_s);
151         }
152
153         return header;
154 }
155
156 static void init_output(GString **out, struct context *ctx,
157                         const struct sr_output *o)
158 {
159         if (!ctx->header_done) {
160                 *out = gen_header(o);
161                 ctx->header_done = TRUE;
162         } else {
163                 *out = g_string_sized_new(512);
164         }
165 }
166
167 static void handle_analog_frame(struct context *ctx,
168                                 const struct sr_datafeed_analog *analog)
169 {
170         unsigned int numch, nums, i, j, s;
171         GSList *l;
172
173         numch = g_slist_length(analog->channels);
174         if ((unsigned int)analog->num_samples > numch)
175                 nums = analog->num_samples / numch;
176         else
177                 nums = 1;
178
179         s = 0;
180         l = analog->channels;
181         for (i = 0; i < nums; i++) {
182                 for (j = 0; j < ctx->num_analog_channels; j++) {
183                         if (ctx->analog_channels[j] == l->data)
184                                 ctx->analog_vals[j] = analog->data[s++];
185                 }
186                 l = l->next;
187         }
188 }
189
190 static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
191                 GString **out)
192 {
193         const struct sr_datafeed_meta *meta;
194         const struct sr_datafeed_logic *logic;
195         const struct sr_datafeed_analog *analog;
196         const struct sr_config *src;
197         GSList *l;
198         struct context *ctx;
199         int idx;
200         uint64_t i, j, k, nums, numch;
201         gchar *p, c;
202         int ret = SR_OK;
203
204         *out = NULL;
205         if (!o || !o->sdi)
206                 return SR_ERR_ARG;
207         if (!(ctx = o->priv))
208                 return SR_ERR_ARG;
209
210         switch (packet->type) {
211         case SR_DF_META:
212                 meta = packet->payload;
213                 for (l = meta->config; l; l = l->next) {
214                         src = l->data;
215                         if (src->key != SR_CONF_SAMPLERATE)
216                                 continue;
217                         ctx->samplerate = g_variant_get_uint64(src->data);
218                 }
219                 break;
220         case SR_DF_FRAME_BEGIN:
221                 /*
222                  * Special case - we start gathering data from analog channels
223                  * and wait for SR_DF_FRAME_END to dump it.
224                  */
225                 memset(ctx->analog_vals, 0, sizeof(float) * ctx->num_analog_channels);
226                 ctx->inframe = TRUE;
227                 ret = SR_OK_CONTINUE;
228                 break;
229         case SR_DF_FRAME_END:
230                 /*
231                  * Dump gathered data.
232                  */
233                 init_output(out, ctx, o);
234
235                 for (i = 0, j = 0; i < ctx->num_enabled_channels; i++) {
236                         if (ctx->channels[i]->type == SR_CHANNEL_ANALOG) {
237                                 g_string_append_printf(*out, "%f",
238                                                         ctx->analog_vals[j++]);
239                         }
240                         g_string_append_c(*out, ctx->separator);
241                 }
242                 g_string_truncate(*out, (*out)->len - 1);
243                 g_string_append_printf(*out, "\n");
244
245                 ctx->inframe = FALSE;
246                 break;
247         case SR_DF_LOGIC:
248                 logic = packet->payload;
249                 init_output(out, ctx, o);
250
251                 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
252                         for (j = 0; j < ctx->num_enabled_channels; j++) {
253                                 if (ctx->channels[j]->type == SR_CHANNEL_LOGIC) {
254                                         idx = ctx->channels[j]->index;
255                                         p = logic->data + i + idx / 8;
256                                         c = *p & (1 << (idx % 8));
257                                         g_string_append_c(*out, c ? '1' : '0');
258                                 }
259                                 g_string_append_c(*out, ctx->separator);
260                         }
261                         if (j) {
262                                 /* Drop last separator. */
263                                 g_string_truncate(*out, (*out)->len - 1);
264                         }
265                         g_string_append_printf(*out, "\n");
266                 }
267                 break;
268         case SR_DF_ANALOG:
269                 analog = packet->payload;
270
271                 if (ctx->inframe) {
272                         handle_analog_frame(ctx, analog);
273                         ret = SR_OK_CONTINUE;
274                         break;
275                 }
276
277                 init_output(out, ctx, o);
278                 k = 0;
279                 l = NULL;
280
281                 numch = g_slist_length(analog->channels);
282                 if ((unsigned int)analog->num_samples > numch)
283                         nums = analog->num_samples / numch;
284                 else
285                         nums = 1;
286
287                 for (i = 0; i < nums; i++) {
288                         for (j = 0; j < ctx->num_enabled_channels; j++) {
289                                 if (ctx->channels[j]->type == SR_CHANNEL_ANALOG) {
290                                         if (!l)
291                                                 l = analog->channels;
292
293                                         if (ctx->channels[j] == l->data) {
294                                                 g_string_append_printf(*out,
295                                                         "%f", analog->data[k++]);
296                                         }
297
298                                         l = l->next;
299                                 }
300                                 g_string_append_c(*out, ctx->separator);
301                         }
302                         g_string_truncate(*out, (*out)->len - 1);
303                         g_string_append_printf(*out, "\n");
304                 }
305                 break;
306         /* TODO case SR_DF_ANALOG2: */
307         }
308
309         return ret;
310 }
311
312 static int cleanup(struct sr_output *o)
313 {
314         struct context *ctx;
315
316         if (!o || !o->sdi)
317                 return SR_ERR_ARG;
318
319         if (o->priv) {
320                 ctx = o->priv;
321                 g_free(ctx->channels);
322                 g_free(ctx->analog_channels);
323                 g_free(ctx->analog_vals);
324                 g_free(o->priv);
325                 o->priv = NULL;
326         }
327
328         return SR_OK;
329 }
330
331 SR_PRIV struct sr_output_module output_csv = {
332         .id = "csv",
333         .name = "CSV",
334         .desc = "Comma-separated values",
335         .exts = (const char*[]){"csv", NULL},
336         .flags = 0,
337         .options = NULL,
338         .init = init,
339         .receive = receive,
340         .cleanup = cleanup,
341 };