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