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