]> sigrok.org Git - libsigrok.git/blame - output/csv.c
build: Portability fixes.
[libsigrok.git] / 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>
545f9786 24#include "config.h" /* Needed for PACKAGE_STRING and others. */
45c59c8b
BV
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
02604ed6 27
3544f848 28#define LOG_PREFIX "output/csv"
a944a84b 29
02604ed6 30struct context {
ba7dd8bb 31 unsigned int num_enabled_channels;
02604ed6 32 uint64_t samplerate;
02604ed6 33 char separator;
0f3d8c89 34 gboolean header_done;
2a035e53 35 int *channel_index;
02604ed6
UH
36};
37
38/*
39 * TODO:
40 * - Option to specify delimiter character and/or string.
41 * - Option to (not) print metadata as comments.
42 * - Option to specify the comment character(s), e.g. # or ; or C/C++-style.
43 * - Option to (not) print samplenumber / time as extra column.
44 * - Option to "compress" output (only print changed samples, VCD-like).
45 * - Option to print comma-separated bits, or whole bytes/words (for 8/16
ba7dd8bb 46 * channel LAs) as ASCII/hex etc. etc.
02604ed6
UH
47 * - Trigger support.
48 */
49
50static int init(struct sr_output *o)
51{
52 struct context *ctx;
ba7dd8bb 53 struct sr_channel *ch;
02604ed6 54 GSList *l;
0f3d8c89 55 int i;
02604ed6 56
0f3d8c89 57 if (!o || !o->sdi)
02604ed6 58 return SR_ERR_ARG;
02604ed6 59
2a035e53 60 ctx = g_malloc0(sizeof(struct context));
02604ed6 61 o->internal = ctx;
0f3d8c89 62 ctx->separator = ',';
02604ed6 63
ba7dd8bb
UH
64 /* Get the number of channels, and the unitsize. */
65 for (l = o->sdi->channels; l; l = l->next) {
66 ch = l->data;
3f239f08 67 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 68 continue;
ba7dd8bb 69 if (!ch->enabled)
3699a8a1 70 continue;
ba7dd8bb 71 ctx->num_enabled_channels++;
02604ed6 72 }
2a035e53 73 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
02604ed6 74
0f3d8c89
BV
75 /* Once more to map the enabled channels. */
76 for (i = 0, l = o->sdi->channels; l; l = l->next) {
77 ch = l->data;
78 if (ch->type != SR_CHANNEL_LOGIC)
79 continue;
80 if (!ch->enabled)
81 continue;
82 ctx->channel_index[i++] = ch->index;
83 }
02604ed6 84
0f3d8c89
BV
85 return SR_OK;
86}
02604ed6 87
0f3d8c89
BV
88static GString *gen_header(struct sr_output *o)
89{
90 struct context *ctx;
91 struct sr_channel *ch;
92 GVariant *gvar;
93 GString *header;
94 GSList *l;
95 time_t t;
96 int num_channels, i;
97 char *samplerate_s;
02604ed6 98
0f3d8c89
BV
99 ctx = o->internal;
100 header = g_string_sized_new(512);
02604ed6
UH
101
102 /* Some metadata */
0f3d8c89
BV
103 t = time(NULL);
104 g_string_append_printf(header, "; CSV, generated by %s on %s",
105 PACKAGE_STRING, ctime(&t));
02604ed6
UH
106
107 /* Columns / channels */
0f3d8c89
BV
108 num_channels = g_slist_length(o->sdi->channels);
109 g_string_append_printf(header, "; Channels (%d/%d):",
110 ctx->num_enabled_channels, num_channels);
111 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
ba7dd8bb 112 ch = l->data;
3f239f08 113 if (ch->type != SR_CHANNEL_LOGIC)
3699a8a1 114 continue;
ba7dd8bb 115 if (!ch->enabled)
3699a8a1 116 continue;
0f3d8c89 117 g_string_append_printf(header, " %s,", ch->name);
1c5b099a 118 }
ba7dd8bb 119 if (o->sdi->channels)
e36aae98 120 /* Drop last separator. */
0f3d8c89
BV
121 g_string_truncate(header, header->len - 1);
122 g_string_append_printf(header, "\n");
123
124 if (ctx->samplerate == 0) {
125 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
126 &gvar) == SR_OK) {
127 ctx->samplerate = g_variant_get_uint64(gvar);
128 g_variant_unref(gvar);
129 }
130 }
131 if (ctx->samplerate != 0) {
132 samplerate_s = sr_samplerate_string(ctx->samplerate);
133 g_string_append_printf(header, "; Samplerate: %s\n", samplerate_s);
134 g_free(samplerate_s);
135 }
02604ed6 136
0f3d8c89 137 return header;
02604ed6
UH
138}
139
dba3e682
BV
140static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
141 GString **out)
02604ed6 142{
0f3d8c89 143 const struct sr_datafeed_meta *meta;
4829d37d 144 const struct sr_datafeed_logic *logic;
0f3d8c89
BV
145 const struct sr_config *src;
146 GSList *l;
02604ed6 147 struct context *ctx;
2a035e53 148 int idx;
e96cf218
BV
149 uint64_t i, j;
150 gchar *p, c;
02604ed6 151
4829d37d
BV
152 *out = NULL;
153 if (!o || !o->sdi)
02604ed6 154 return SR_ERR_ARG;
4829d37d 155 if (!(ctx = o->internal))
02604ed6 156 return SR_ERR_ARG;
02604ed6 157
4829d37d 158 switch (packet->type) {
0f3d8c89
BV
159 case SR_DF_META:
160 meta = packet->payload;
161 for (l = meta->config; l; l = l->next) {
162 src = l->data;
163 if (src->key != SR_CONF_SAMPLERATE)
164 continue;
165 ctx->samplerate = g_variant_get_uint64(src->data);
166 }
167 break;
4829d37d
BV
168 case SR_DF_LOGIC:
169 logic = packet->payload;
0f3d8c89
BV
170 if (!ctx->header_done) {
171 *out = gen_header(o);
172 ctx->header_done = TRUE;
4829d37d
BV
173 } else {
174 *out = g_string_sized_new(512);
175 }
176
2a035e53 177 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
ba7dd8bb 178 for (j = 0; j < ctx->num_enabled_channels; j++) {
2a035e53
BV
179 idx = ctx->channel_index[j];
180 p = logic->data + i + idx / 8;
181 c = *p & (1 << (idx % 8));
e96cf218
BV
182 g_string_append_c(*out, c ? '1' : '0');
183 g_string_append_c(*out, ctx->separator);
4829d37d 184 }
e36aae98 185 if (j) {
54da58ca
BV
186 /* Drop last separator. */
187 g_string_truncate(*out, (*out)->len - 1);
188 }
4829d37d
BV
189 g_string_append_printf(*out, "\n");
190 }
02604ed6 191 break;
02604ed6
UH
192 }
193
194 return SR_OK;
195}
196
4829d37d 197static int cleanup(struct sr_output *o)
02604ed6
UH
198{
199 struct context *ctx;
02604ed6 200
4829d37d 201 if (!o || !o->sdi)
02604ed6 202 return SR_ERR_ARG;
02604ed6 203
4829d37d
BV
204 if (o->internal) {
205 ctx = o->internal;
2a035e53 206 g_free(ctx->channel_index);
4829d37d
BV
207 g_free(o->internal);
208 o->internal = NULL;
02604ed6
UH
209 }
210
02604ed6
UH
211 return SR_OK;
212}
213
7c1d391c 214SR_PRIV struct sr_output_format output_csv = {
02604ed6
UH
215 .id = "csv",
216 .description = "Comma-separated values (CSV)",
02604ed6 217 .init = init,
4829d37d
BV
218 .receive = receive,
219 .cleanup = cleanup,
02604ed6 220};