]> sigrok.org Git - libsigrok.git/blame - src/output/csv.c
libsigrok-internal.h: Remove unused prototypes
[libsigrok.git] / src / output / csv.c
CommitLineData
02604ed6 1/*
50985c20 2 * This file is part of the libsigrok project.
02604ed6
UH
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>
c1aae900 24#include <libsigrok/libsigrok.h>
45c59c8b 25#include "libsigrok-internal.h"
02604ed6 26
3544f848 27#define LOG_PREFIX "output/csv"
a944a84b 28
02604ed6 29struct context {
ba7dd8bb 30 unsigned int num_enabled_channels;
02604ed6 31 uint64_t samplerate;
02604ed6 32 char separator;
0f3d8c89 33 gboolean header_done;
c04cf9aa
BG
34 struct sr_channel **channels;
35
36 /* For analog measurements split into frames, not packets. */
37 struct sr_channel **analog_channels;
38 float *analog_vals; /* Analog values stored until the end of the frame. */
39 unsigned int num_analog_channels;
40 gboolean inframe;
02604ed6
UH
41};
42
43/*
44 * TODO:
45 * - Option to specify delimiter character and/or string.
46 * - Option to (not) print metadata as comments.
47 * - Option to specify the comment character(s), e.g. # or ; or C/C++-style.
48 * - Option to (not) print samplenumber / time as extra column.
49 * - Option to "compress" output (only print changed samples, VCD-like).
50 * - Option to print comma-separated bits, or whole bytes/words (for 8/16
ba7dd8bb 51 * channel LAs) as ASCII/hex etc. etc.
02604ed6
UH
52 * - Trigger support.
53 */
54
a755b0e1 55static int init(struct sr_output *o, GHashTable *options)
02604ed6
UH
56{
57 struct context *ctx;
ba7dd8bb 58 struct sr_channel *ch;
02604ed6 59 GSList *l;
c04cf9aa 60 int i, j;
02604ed6 61
a755b0e1
BV
62 (void)options;
63
0f3d8c89 64 if (!o || !o->sdi)
02604ed6 65 return SR_ERR_ARG;
02604ed6 66
2a035e53 67 ctx = g_malloc0(sizeof(struct context));
d686c5ec 68 o->priv = ctx;
0f3d8c89 69 ctx->separator = ',';
02604ed6 70
ba7dd8bb
UH
71 /* Get the number of channels, and the unitsize. */
72 for (l = o->sdi->channels; l; l = l->next) {
73 ch = l->data;
c04cf9aa
BG
74 if (ch->enabled) {
75 if (ch->type == SR_CHANNEL_LOGIC ||
76 ch->type == SR_CHANNEL_ANALOG)
77 ctx->num_enabled_channels++;
78 if (ch->type == SR_CHANNEL_ANALOG)
79 ctx->num_analog_channels++;
80 }
02604ed6 81 }
c04cf9aa
BG
82 ctx->channels = g_malloc(sizeof(struct sr_channel *)
83 * ctx->num_enabled_channels);
84 ctx->analog_channels = g_malloc(sizeof(struct sr_channel *)
85 * ctx->num_analog_channels);
86 ctx->analog_vals = g_malloc(sizeof(float) * ctx->num_analog_channels);
02604ed6 87
0f3d8c89 88 /* Once more to map the enabled channels. */
c04cf9aa 89 for (i = 0, l = o->sdi->channels, j = 0; l; l = l->next) {
0f3d8c89 90 ch = l->data;
c04cf9aa
BG
91 if (ch->enabled) {
92 if (ch->type == SR_CHANNEL_LOGIC ||
93 ch->type == SR_CHANNEL_ANALOG)
94 ctx->channels[i++] = ch;
95 if (ch->type == SR_CHANNEL_ANALOG)
96 ctx->analog_channels[j++] = ch;
97 }
98
0f3d8c89 99 }
02604ed6 100
0f3d8c89
BV
101 return SR_OK;
102}
02604ed6 103
a755b0e1 104static GString *gen_header(const struct sr_output *o)
0f3d8c89
BV
105{
106 struct context *ctx;
107 struct sr_channel *ch;
108 GVariant *gvar;
109 GString *header;
110 GSList *l;
111 time_t t;
112 int num_channels, i;
113 char *samplerate_s;
02604ed6 114
d686c5ec 115 ctx = o->priv;
0f3d8c89 116 header = g_string_sized_new(512);
02604ed6
UH
117
118 /* Some metadata */
0f3d8c89 119 t = time(NULL);
b9eb8e1a
DE
120 g_string_append_printf(header, "; CSV, generated by %s %s on %s",
121 PACKAGE_NAME, SR_PACKAGE_VERSION_STRING, ctime(&t));
02604ed6
UH
122
123 /* Columns / channels */
0f3d8c89
BV
124 num_channels = g_slist_length(o->sdi->channels);
125 g_string_append_printf(header, "; Channels (%d/%d):",
126 ctx->num_enabled_channels, num_channels);
127 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
ba7dd8bb 128 ch = l->data;
c04cf9aa
BG
129 if (ch->enabled &&
130 (ch->type == SR_CHANNEL_LOGIC ||
131 ch->type == SR_CHANNEL_ANALOG))
132 g_string_append_printf(header, " %s,", ch->name);
1c5b099a 133 }
ba7dd8bb 134 if (o->sdi->channels)
e36aae98 135 /* Drop last separator. */
0f3d8c89
BV
136 g_string_truncate(header, header->len - 1);
137 g_string_append_printf(header, "\n");
138
139 if (ctx->samplerate == 0) {
140 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
141 &gvar) == SR_OK) {
142 ctx->samplerate = g_variant_get_uint64(gvar);
143 g_variant_unref(gvar);
144 }
145 }
146 if (ctx->samplerate != 0) {
147 samplerate_s = sr_samplerate_string(ctx->samplerate);
148 g_string_append_printf(header, "; Samplerate: %s\n", samplerate_s);
149 g_free(samplerate_s);
150 }
02604ed6 151
0f3d8c89 152 return header;
02604ed6
UH
153}
154
c04cf9aa
BG
155static void init_output(GString **out, struct context *ctx,
156 const struct sr_output *o)
157{
158 if (!ctx->header_done) {
159 *out = gen_header(o);
160 ctx->header_done = TRUE;
161 } else {
162 *out = g_string_sized_new(512);
163 }
164}
165
166static void handle_analog_frame(struct context *ctx,
167 const struct sr_datafeed_analog *analog)
168{
169 unsigned int numch, nums, i, j, s;
170 GSList *l;
171
172 numch = g_slist_length(analog->channels);
173 if ((unsigned int)analog->num_samples > numch)
174 nums = analog->num_samples / numch;
175 else
176 nums = 1;
177
178 s = 0;
179 l = analog->channels;
180 for (i = 0; i < nums; i++) {
181 for (j = 0; j < ctx->num_analog_channels; j++) {
182 if (ctx->analog_channels[j] == l->data)
183 ctx->analog_vals[j] = analog->data[s++];
184 }
185 l = l->next;
186 }
187}
188
a755b0e1 189static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet,
dba3e682 190 GString **out)
02604ed6 191{
0f3d8c89 192 const struct sr_datafeed_meta *meta;
4829d37d 193 const struct sr_datafeed_logic *logic;
c04cf9aa 194 const struct sr_datafeed_analog *analog;
0f3d8c89
BV
195 const struct sr_config *src;
196 GSList *l;
02604ed6 197 struct context *ctx;
2a035e53 198 int idx;
c04cf9aa 199 uint64_t i, j, k, nums, numch;
e96cf218 200 gchar *p, c;
c04cf9aa 201 int ret = SR_OK;
02604ed6 202
4829d37d
BV
203 *out = NULL;
204 if (!o || !o->sdi)
02604ed6 205 return SR_ERR_ARG;
d686c5ec 206 if (!(ctx = o->priv))
02604ed6 207 return SR_ERR_ARG;
02604ed6 208
4829d37d 209 switch (packet->type) {
0f3d8c89
BV
210 case SR_DF_META:
211 meta = packet->payload;
212 for (l = meta->config; l; l = l->next) {
213 src = l->data;
214 if (src->key != SR_CONF_SAMPLERATE)
215 continue;
216 ctx->samplerate = g_variant_get_uint64(src->data);
217 }
218 break;
c04cf9aa
BG
219 case SR_DF_FRAME_BEGIN:
220 /*
221 * Special case - we start gathering data from analog channels
222 * and wait for SR_DF_FRAME_END to dump it.
223 */
224 memset(ctx->analog_vals, 0, sizeof(float) * ctx->num_analog_channels);
225 ctx->inframe = TRUE;
b9300678 226 ret = SR_OK;
c04cf9aa
BG
227 break;
228 case SR_DF_FRAME_END:
229 /*
230 * Dump gathered data.
231 */
232 init_output(out, ctx, o);
233
234 for (i = 0, j = 0; i < ctx->num_enabled_channels; i++) {
235 if (ctx->channels[i]->type == SR_CHANNEL_ANALOG) {
236 g_string_append_printf(*out, "%f",
237 ctx->analog_vals[j++]);
238 }
239 g_string_append_c(*out, ctx->separator);
240 }
241 g_string_truncate(*out, (*out)->len - 1);
242 g_string_append_printf(*out, "\n");
243
244 ctx->inframe = FALSE;
245 break;
4829d37d
BV
246 case SR_DF_LOGIC:
247 logic = packet->payload;
c04cf9aa 248 init_output(out, ctx, o);
4829d37d 249
2a035e53 250 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
ba7dd8bb 251 for (j = 0; j < ctx->num_enabled_channels; j++) {
c04cf9aa
BG
252 if (ctx->channels[j]->type == SR_CHANNEL_LOGIC) {
253 idx = ctx->channels[j]->index;
254 p = logic->data + i + idx / 8;
255 c = *p & (1 << (idx % 8));
256 g_string_append_c(*out, c ? '1' : '0');
257 }
e96cf218 258 g_string_append_c(*out, ctx->separator);
4829d37d 259 }
e36aae98 260 if (j) {
54da58ca
BV
261 /* Drop last separator. */
262 g_string_truncate(*out, (*out)->len - 1);
263 }
4829d37d
BV
264 g_string_append_printf(*out, "\n");
265 }
02604ed6 266 break;
c04cf9aa
BG
267 case SR_DF_ANALOG:
268 analog = packet->payload;
269
270 if (ctx->inframe) {
271 handle_analog_frame(ctx, analog);
c04cf9aa
BG
272 break;
273 }
274
275 init_output(out, ctx, o);
276 k = 0;
277 l = NULL;
278
279 numch = g_slist_length(analog->channels);
280 if ((unsigned int)analog->num_samples > numch)
281 nums = analog->num_samples / numch;
282 else
283 nums = 1;
284
285 for (i = 0; i < nums; i++) {
286 for (j = 0; j < ctx->num_enabled_channels; j++) {
287 if (ctx->channels[j]->type == SR_CHANNEL_ANALOG) {
288 if (!l)
289 l = analog->channels;
290
291 if (ctx->channels[j] == l->data) {
292 g_string_append_printf(*out,
293 "%f", analog->data[k++]);
294 }
295
296 l = l->next;
297 }
298 g_string_append_c(*out, ctx->separator);
299 }
300 g_string_truncate(*out, (*out)->len - 1);
301 g_string_append_printf(*out, "\n");
302 }
303 break;
304 /* TODO case SR_DF_ANALOG2: */
02604ed6
UH
305 }
306
c04cf9aa 307 return ret;
02604ed6
UH
308}
309
4829d37d 310static int cleanup(struct sr_output *o)
02604ed6
UH
311{
312 struct context *ctx;
02604ed6 313
4829d37d 314 if (!o || !o->sdi)
02604ed6 315 return SR_ERR_ARG;
02604ed6 316
d686c5ec
BV
317 if (o->priv) {
318 ctx = o->priv;
c04cf9aa
BG
319 g_free(ctx->channels);
320 g_free(ctx->analog_channels);
321 g_free(ctx->analog_vals);
d686c5ec
BV
322 g_free(o->priv);
323 o->priv = NULL;
02604ed6
UH
324 }
325
02604ed6
UH
326 return SR_OK;
327}
328
a755b0e1 329SR_PRIV struct sr_output_module output_csv = {
02604ed6 330 .id = "csv",
a755b0e1
BV
331 .name = "CSV",
332 .desc = "Comma-separated values",
8a174d23 333 .exts = (const char*[]){"csv", NULL},
3cd4b381 334 .flags = 0,
a755b0e1 335 .options = NULL,
02604ed6 336 .init = init,
4829d37d
BV
337 .receive = receive,
338 .cleanup = cleanup,
02604ed6 339};