]> sigrok.org Git - libsigrok.git/blame - output/csv.c
Add sr_config_free()
[libsigrok.git] / output / csv.c
CommitLineData
02604ed6
UH
1/*
2 * This file is part of the sigrok 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>
545f9786 24#include "config.h" /* Needed for PACKAGE_STRING and others. */
45c59c8b
BV
25#include "libsigrok.h"
26#include "libsigrok-internal.h"
02604ed6 27
a944a84b
UH
28/* Message logging helpers with driver-specific prefix string. */
29#define DRIVER_LOG_DOMAIN "output/csv: "
30#define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31#define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32#define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33#define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34#define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35#define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
36
02604ed6
UH
37struct context {
38 unsigned int num_enabled_probes;
39 unsigned int unitsize;
40 char *probelist[SR_MAX_NUM_PROBES + 1];
41 uint64_t samplerate;
42 GString *header;
43 char separator;
44};
45
46/*
47 * TODO:
48 * - Option to specify delimiter character and/or string.
49 * - Option to (not) print metadata as comments.
50 * - Option to specify the comment character(s), e.g. # or ; or C/C++-style.
51 * - Option to (not) print samplenumber / time as extra column.
52 * - Option to "compress" output (only print changed samples, VCD-like).
53 * - Option to print comma-separated bits, or whole bytes/words (for 8/16
54 * probe LAs) as ASCII/hex etc. etc.
55 * - Trigger support.
56 */
57
58static int init(struct sr_output *o)
59{
60 struct context *ctx;
61 struct sr_probe *probe;
62 GSList *l;
63 int num_probes;
5c3c1241 64 uint64_t *samplerate;
02604ed6
UH
65 time_t t;
66 unsigned int i;
67
68 if (!o) {
a944a84b 69 sr_err("%s: o was NULL", __func__);
02604ed6
UH
70 return SR_ERR_ARG;
71 }
72
5c3c1241 73 if (!o->sdi) {
a944a84b 74 sr_err("%s: o->sdi was NULL", __func__);
02604ed6
UH
75 return SR_ERR_ARG;
76 }
77
5c3c1241 78 if (!o->sdi->driver) {
a944a84b 79 sr_err("%s: o->sdi->driver was NULL", __func__);
02604ed6
UH
80 return SR_ERR_ARG;
81 }
82
83 if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
a944a84b 84 sr_err("%s: ctx malloc failed", __func__);
02604ed6
UH
85 return SR_ERR_MALLOC;
86 }
87
88 o->internal = ctx;
89
90 /* Get the number of probes, their names, and the unitsize. */
5c3c1241 91 for (l = o->sdi->probes; l; l = l->next) {
02604ed6
UH
92 probe = l->data;
93 if (!probe->enabled)
94 continue;
95 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
96 }
97 ctx->probelist[ctx->num_enabled_probes] = 0;
98 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
99
5c3c1241 100 num_probes = g_slist_length(o->sdi->probes);
02604ed6 101
4d15e5c9 102 if (sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
123e1313 103 o->sdi->driver->config_get(SR_CONF_SAMPLERATE,
5c3c1241
BV
104 (const void **)&samplerate, o->sdi);
105 ctx->samplerate = *samplerate;
106 } else
107 ctx->samplerate = 0;
02604ed6
UH
108
109 ctx->separator = ',';
02604ed6
UH
110 ctx->header = g_string_sized_new(512);
111
112 t = time(NULL);
113
114 /* Some metadata */
115 g_string_append_printf(ctx->header, "; CSV, generated by %s on %s",
116 PACKAGE_STRING, ctime(&t));
117 g_string_append_printf(ctx->header, "; Samplerate: %"PRIu64"\n",
118 ctx->samplerate);
119
120 /* Columns / channels */
121 g_string_append_printf(ctx->header, "; Channels (%d/%d): ",
122 ctx->num_enabled_probes, num_probes);
123 for (i = 0; i < ctx->num_enabled_probes; i++)
124 g_string_append_printf(ctx->header, "%s, ", ctx->probelist[i]);
125 g_string_append_printf(ctx->header, "\n");
126
5c3c1241 127 return SR_OK;
02604ed6
UH
128}
129
054e6709 130static int event(struct sr_output *o, int event_type, uint8_t **data_out,
02604ed6
UH
131 uint64_t *length_out)
132{
133 struct context *ctx;
02604ed6
UH
134
135 if (!o) {
a944a84b 136 sr_err("%s: o was NULL", __func__);
02604ed6
UH
137 return SR_ERR_ARG;
138 }
139
140 if (!(ctx = o->internal)) {
a944a84b 141 sr_err("%s: o->internal was NULL", __func__);
02604ed6
UH
142 return SR_ERR_ARG;
143 }
144
145 if (!data_out) {
a944a84b 146 sr_err("%s: data_out was NULL", __func__);
02604ed6
UH
147 return SR_ERR_ARG;
148 }
149
150 switch (event_type) {
151 case SR_DF_TRIGGER:
a944a84b 152 sr_dbg("%s: SR_DF_TRIGGER event", __func__);
02604ed6
UH
153 /* TODO */
154 *data_out = NULL;
155 *length_out = 0;
156 break;
157 case SR_DF_END:
a944a84b 158 sr_dbg("%s: SR_DF_END event", __func__);
02604ed6
UH
159 /* TODO */
160 *data_out = NULL;
161 *length_out = 0;
162 g_free(o->internal);
163 o->internal = NULL;
164 break;
165 default:
a944a84b 166 sr_err("%s: unsupported event type: %d", __func__, event_type);
02604ed6
UH
167 *data_out = NULL;
168 *length_out = 0;
169 break;
170 }
171
172 return SR_OK;
173}
174
054e6709
UH
175static int data(struct sr_output *o, const uint8_t *data_in,
176 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
02604ed6
UH
177{
178 struct context *ctx;
179 GString *outstr;
180 uint64_t sample, i;
181 int j;
182
183 if (!o) {
a944a84b 184 sr_err("%s: o was NULL", __func__);
02604ed6
UH
185 return SR_ERR_ARG;
186 }
187
188 if (!(ctx = o->internal)) {
a944a84b 189 sr_err("%s: o->internal was NULL", __func__);
02604ed6
UH
190 return SR_ERR_ARG;
191 }
192
193 if (!data_in) {
a944a84b 194 sr_err("%s: data_in was NULL", __func__);
02604ed6
UH
195 return SR_ERR_ARG;
196 }
197
198 if (ctx->header) {
199 /* First data packet. */
200 outstr = ctx->header;
201 ctx->header = NULL;
202 } else {
203 outstr = g_string_sized_new(512);
204 }
205
206 for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
207 memcpy(&sample, data_in + i, ctx->unitsize);
208 for (j = ctx->num_enabled_probes - 1; j >= 0; j--) {
209 g_string_append_printf(outstr, "%d%c",
210 (int)((sample & (1 << j)) >> j),
211 ctx->separator);
212 }
213 g_string_append_printf(outstr, "\n");
214 }
215
054e6709 216 *data_out = (uint8_t *)outstr->str;
02604ed6
UH
217 *length_out = outstr->len;
218 g_string_free(outstr, FALSE);
219
220 return SR_OK;
221}
222
7c1d391c 223SR_PRIV struct sr_output_format output_csv = {
02604ed6
UH
224 .id = "csv",
225 .description = "Comma-separated values (CSV)",
226 .df_type = SR_DF_LOGIC,
227 .init = init,
228 .data = data,
229 .event = event,
230};