]> sigrok.org Git - libsigrok.git/blame - output/csv.c
output/csv: Use new output API.
[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
UH
30struct context {
31 unsigned int num_enabled_probes;
32 unsigned int unitsize;
02604ed6
UH
33 uint64_t samplerate;
34 GString *header;
35 char separator;
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
46 * probe LAs) as ASCII/hex etc. etc.
47 * - Trigger support.
48 */
49
50static int init(struct sr_output *o)
51{
52 struct context *ctx;
53 struct sr_probe *probe;
54 GSList *l;
ec4063b8 55 GVariant *gvar;
02604ed6 56 int num_probes;
02604ed6 57 time_t t;
02604ed6 58
4829d37d 59 if (!o)
02604ed6 60 return SR_ERR_ARG;
02604ed6 61
4829d37d 62 if (!o->sdi)
02604ed6 63 return SR_ERR_ARG;
02604ed6 64
4829d37d 65 ctx = g_try_malloc0(sizeof(struct context));
02604ed6
UH
66 o->internal = ctx;
67
1c5b099a 68 /* Get the number of probes, and the unitsize. */
5c3c1241 69 for (l = o->sdi->probes; l; l = l->next) {
02604ed6 70 probe = l->data;
3699a8a1
BV
71 if (probe->type != SR_PROBE_LOGIC)
72 continue;
73 if (!probe->enabled)
74 continue;
75 ctx->num_enabled_probes++;
02604ed6 76 }
1c5b099a 77
02604ed6
UH
78 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
79
5c3c1241 80 num_probes = g_slist_length(o->sdi->probes);
02604ed6 81
d3c74a6f
BV
82 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
83 &gvar) == SR_OK) {
ec4063b8
BV
84 ctx->samplerate = g_variant_get_uint64(gvar);
85 g_variant_unref(gvar);
5c3c1241
BV
86 } else
87 ctx->samplerate = 0;
02604ed6
UH
88
89 ctx->separator = ',';
02604ed6
UH
90 ctx->header = g_string_sized_new(512);
91
92 t = time(NULL);
93
94 /* Some metadata */
95 g_string_append_printf(ctx->header, "; CSV, generated by %s on %s",
96 PACKAGE_STRING, ctime(&t));
97 g_string_append_printf(ctx->header, "; Samplerate: %"PRIu64"\n",
98 ctx->samplerate);
99
100 /* Columns / channels */
101 g_string_append_printf(ctx->header, "; Channels (%d/%d): ",
102 ctx->num_enabled_probes, num_probes);
1c5b099a
ML
103 for (l = o->sdi->probes; l; l = l->next) {
104 probe = l->data;
3699a8a1
BV
105 if (probe->type != SR_PROBE_LOGIC)
106 continue;
107 if (!probe->enabled)
108 continue;
109 g_string_append_printf(ctx->header, "%s, ", probe->name);
1c5b099a 110 }
02604ed6
UH
111 g_string_append_printf(ctx->header, "\n");
112
5c3c1241 113 return SR_OK;
02604ed6
UH
114}
115
4829d37d
BV
116static int receive(struct sr_output *o, const struct sr_dev_inst *sdi,
117 const struct sr_datafeed_packet *packet, GString **out)
02604ed6 118{
4829d37d 119 const struct sr_datafeed_logic *logic;
02604ed6 120 struct context *ctx;
4829d37d 121 uint64_t sample, i, j;
02604ed6 122
4829d37d 123 (void)sdi;
02604ed6 124
4829d37d
BV
125 *out = NULL;
126 if (!o || !o->sdi)
02604ed6 127 return SR_ERR_ARG;
4829d37d 128 if (!(ctx = o->internal))
02604ed6 129 return SR_ERR_ARG;
02604ed6 130
4829d37d
BV
131 switch (packet->type) {
132 case SR_DF_LOGIC:
133 logic = packet->payload;
134 if (ctx->header) {
135 /*
136 * First data packet: prime the output with the
137 * previously prepared header.
138 */
139 *out = ctx->header;
140 ctx->header = NULL;
141 } else {
142 *out = g_string_sized_new(512);
143 }
144
145 for (i = 0; i <= logic->length - ctx->unitsize; i += ctx->unitsize) {
146 memcpy(&sample, logic->data + i, ctx->unitsize);
147 for (j = 0; j < ctx->num_enabled_probes; j++) {
148 g_string_append_printf(*out, "%d%c",
149 (int)((sample & (1 << j)) >> j),
150 ctx->separator);
151 }
152 g_string_append_printf(*out, "\n");
153 }
02604ed6
UH
154 break;
155 case SR_DF_END:
02604ed6
UH
156 g_free(o->internal);
157 o->internal = NULL;
158 break;
02604ed6
UH
159 }
160
161 return SR_OK;
162}
163
4829d37d 164static int cleanup(struct sr_output *o)
02604ed6
UH
165{
166 struct context *ctx;
02604ed6 167
4829d37d 168 if (!o || !o->sdi)
02604ed6 169 return SR_ERR_ARG;
02604ed6 170
4829d37d
BV
171 if (o->internal) {
172 ctx = o->internal;
173 if (ctx->header)
174 g_string_free(ctx->header, TRUE);
175 g_free(o->internal);
176 o->internal = NULL;
02604ed6
UH
177 }
178
02604ed6
UH
179 return SR_OK;
180}
181
7c1d391c 182SR_PRIV struct sr_output_format output_csv = {
02604ed6
UH
183 .id = "csv",
184 .description = "Comma-separated values (CSV)",
185 .df_type = SR_DF_LOGIC,
186 .init = init,
4829d37d
BV
187 .receive = receive,
188 .cleanup = cleanup,
02604ed6 189};