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