]> sigrok.org Git - libsigrok.git/blob - output/csv.c
Centralise duplicated logging helper defines.
[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 #define LOG_PREFIX "output/csv"
29
30 struct context {
31         unsigned int num_enabled_probes;
32         unsigned int unitsize;
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
50 static int init(struct sr_output *o)
51 {
52         struct context *ctx;
53         struct sr_probe *probe;
54         GSList *l;
55         GVariant *gvar;
56         int num_probes;
57         time_t t;
58
59         if (!o) {
60                 sr_err("%s: o was NULL", __func__);
61                 return SR_ERR_ARG;
62         }
63
64         if (!o->sdi) {
65                 sr_err("%s: o->sdi was NULL", __func__);
66                 return SR_ERR_ARG;
67         }
68
69         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
70                 sr_err("%s: ctx malloc failed", __func__);
71                 return SR_ERR_MALLOC;
72         }
73
74         o->internal = ctx;
75
76         /* Get the number of probes, and the unitsize. */
77         for (l = o->sdi->probes; l; l = l->next) {
78                 probe = l->data;
79                 if (probe->enabled)
80                         ctx->num_enabled_probes++;
81         }
82
83         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
84
85         num_probes = g_slist_length(o->sdi->probes);
86
87         if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
88                         &gvar) == SR_OK) {
89                 ctx->samplerate = g_variant_get_uint64(gvar);
90                 g_variant_unref(gvar);
91         } else
92                 ctx->samplerate = 0;
93
94         ctx->separator = ',';
95         ctx->header = g_string_sized_new(512);
96
97         t = time(NULL);
98
99         /* Some metadata */
100         g_string_append_printf(ctx->header, "; CSV, generated by %s on %s",
101                                PACKAGE_STRING, ctime(&t));
102         g_string_append_printf(ctx->header, "; Samplerate: %"PRIu64"\n",
103                                ctx->samplerate);
104
105         /* Columns / channels */
106         g_string_append_printf(ctx->header, "; Channels (%d/%d): ",
107                                ctx->num_enabled_probes, num_probes);
108         for (l = o->sdi->probes; l; l = l->next) {
109                 probe = l->data;
110                 if (probe->enabled)
111                         g_string_append_printf(ctx->header, "%s, ", probe->name);
112         }
113         g_string_append_printf(ctx->header, "\n");
114
115         return SR_OK;
116 }
117
118 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
119                  uint64_t *length_out)
120 {
121         struct context *ctx;
122
123         if (!o) {
124                 sr_err("%s: o was NULL", __func__);
125                 return SR_ERR_ARG;
126         }
127
128         if (!(ctx = o->internal)) {
129                 sr_err("%s: o->internal was NULL", __func__);
130                 return SR_ERR_ARG;
131         }
132
133         if (!data_out) {
134                 sr_err("%s: data_out was NULL", __func__);
135                 return SR_ERR_ARG;
136         }
137
138         switch (event_type) {
139         case SR_DF_TRIGGER:
140                 sr_dbg("%s: SR_DF_TRIGGER event", __func__);
141                 /* TODO */
142                 *data_out = NULL;
143                 *length_out = 0;
144                 break;
145         case SR_DF_END:
146                 sr_dbg("%s: SR_DF_END event", __func__);
147                 /* TODO */
148                 *data_out = NULL;
149                 *length_out = 0;
150                 g_free(o->internal);
151                 o->internal = NULL;
152                 break;
153         default:
154                 sr_err("%s: unsupported event type: %d", __func__, event_type);
155                 *data_out = NULL;
156                 *length_out = 0;
157                 break;
158         }
159
160         return SR_OK;
161 }
162
163 static int data(struct sr_output *o, const uint8_t *data_in,
164                 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
165 {
166         struct context *ctx;
167         GString *outstr;
168         uint64_t sample, i, j;
169
170         if (!o) {
171                 sr_err("%s: o was NULL", __func__);
172                 return SR_ERR_ARG;
173         }
174
175         if (!(ctx = o->internal)) {
176                 sr_err("%s: o->internal was NULL", __func__);
177                 return SR_ERR_ARG;
178         }
179
180         if (!data_in) {
181                 sr_err("%s: data_in was NULL", __func__);
182                 return SR_ERR_ARG;
183         }
184
185         if (ctx->header) {
186                 /* First data packet. */
187                 outstr = ctx->header;
188                 ctx->header = NULL;
189         } else {
190                 outstr = g_string_sized_new(512);
191         }
192
193         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
194                 memcpy(&sample, data_in + i, ctx->unitsize);
195                 for (j = 0; j < ctx->num_enabled_probes; j++) {
196                         g_string_append_printf(outstr, "%d%c",
197                                 (int)((sample & (1 << j)) >> j),
198                                 ctx->separator);
199                 }
200                 g_string_append_printf(outstr, "\n");
201         }
202
203         *data_out = (uint8_t *)outstr->str;
204         *length_out = outstr->len;
205         g_string_free(outstr, FALSE);
206
207         return SR_OK;
208 }
209
210 SR_PRIV struct sr_output_format output_csv = {
211         .id = "csv",
212         .description = "Comma-separated values (CSV)",
213         .df_type = SR_DF_LOGIC,
214         .init = init,
215         .data = data,
216         .event = event,
217 };