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