]>
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 | 30 | struct context { |
ba7dd8bb | 31 | unsigned int num_enabled_channels; |
02604ed6 | 32 | uint64_t samplerate; |
02604ed6 | 33 | char separator; |
0f3d8c89 | 34 | gboolean header_done; |
2a035e53 | 35 | int *channel_index; |
02604ed6 UH |
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 | |
ba7dd8bb | 46 | * channel LAs) as ASCII/hex etc. etc. |
02604ed6 UH |
47 | * - Trigger support. |
48 | */ | |
49 | ||
a755b0e1 | 50 | static int init(struct sr_output *o, GHashTable *options) |
02604ed6 UH |
51 | { |
52 | struct context *ctx; | |
ba7dd8bb | 53 | struct sr_channel *ch; |
02604ed6 | 54 | GSList *l; |
0f3d8c89 | 55 | int i; |
02604ed6 | 56 | |
a755b0e1 BV |
57 | (void)options; |
58 | ||
0f3d8c89 | 59 | if (!o || !o->sdi) |
02604ed6 | 60 | return SR_ERR_ARG; |
02604ed6 | 61 | |
2a035e53 | 62 | ctx = g_malloc0(sizeof(struct context)); |
d686c5ec | 63 | o->priv = ctx; |
0f3d8c89 | 64 | ctx->separator = ','; |
02604ed6 | 65 | |
ba7dd8bb UH |
66 | /* Get the number of channels, and the unitsize. */ |
67 | for (l = o->sdi->channels; l; l = l->next) { | |
68 | ch = l->data; | |
3f239f08 | 69 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 70 | continue; |
ba7dd8bb | 71 | if (!ch->enabled) |
3699a8a1 | 72 | continue; |
ba7dd8bb | 73 | ctx->num_enabled_channels++; |
02604ed6 | 74 | } |
2a035e53 | 75 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); |
02604ed6 | 76 | |
0f3d8c89 BV |
77 | /* Once more to map the enabled channels. */ |
78 | for (i = 0, l = o->sdi->channels; l; l = l->next) { | |
79 | ch = l->data; | |
80 | if (ch->type != SR_CHANNEL_LOGIC) | |
81 | continue; | |
82 | if (!ch->enabled) | |
83 | continue; | |
84 | ctx->channel_index[i++] = ch->index; | |
85 | } | |
02604ed6 | 86 | |
0f3d8c89 BV |
87 | return SR_OK; |
88 | } | |
02604ed6 | 89 | |
a755b0e1 | 90 | static GString *gen_header(const struct sr_output *o) |
0f3d8c89 BV |
91 | { |
92 | struct context *ctx; | |
93 | struct sr_channel *ch; | |
94 | GVariant *gvar; | |
95 | GString *header; | |
96 | GSList *l; | |
97 | time_t t; | |
98 | int num_channels, i; | |
99 | char *samplerate_s; | |
02604ed6 | 100 | |
d686c5ec | 101 | ctx = o->priv; |
0f3d8c89 | 102 | header = g_string_sized_new(512); |
02604ed6 UH |
103 | |
104 | /* Some metadata */ | |
0f3d8c89 BV |
105 | t = time(NULL); |
106 | g_string_append_printf(header, "; CSV, generated by %s on %s", | |
107 | PACKAGE_STRING, ctime(&t)); | |
02604ed6 UH |
108 | |
109 | /* Columns / channels */ | |
0f3d8c89 BV |
110 | num_channels = g_slist_length(o->sdi->channels); |
111 | g_string_append_printf(header, "; Channels (%d/%d):", | |
112 | ctx->num_enabled_channels, num_channels); | |
113 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { | |
ba7dd8bb | 114 | ch = l->data; |
3f239f08 | 115 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 116 | continue; |
ba7dd8bb | 117 | if (!ch->enabled) |
3699a8a1 | 118 | continue; |
0f3d8c89 | 119 | g_string_append_printf(header, " %s,", ch->name); |
1c5b099a | 120 | } |
ba7dd8bb | 121 | if (o->sdi->channels) |
e36aae98 | 122 | /* Drop last separator. */ |
0f3d8c89 BV |
123 | g_string_truncate(header, header->len - 1); |
124 | g_string_append_printf(header, "\n"); | |
125 | ||
126 | if (ctx->samplerate == 0) { | |
127 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
128 | &gvar) == SR_OK) { | |
129 | ctx->samplerate = g_variant_get_uint64(gvar); | |
130 | g_variant_unref(gvar); | |
131 | } | |
132 | } | |
133 | if (ctx->samplerate != 0) { | |
134 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
135 | g_string_append_printf(header, "; Samplerate: %s\n", samplerate_s); | |
136 | g_free(samplerate_s); | |
137 | } | |
02604ed6 | 138 | |
0f3d8c89 | 139 | return header; |
02604ed6 UH |
140 | } |
141 | ||
a755b0e1 | 142 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 143 | GString **out) |
02604ed6 | 144 | { |
0f3d8c89 | 145 | const struct sr_datafeed_meta *meta; |
4829d37d | 146 | const struct sr_datafeed_logic *logic; |
0f3d8c89 BV |
147 | const struct sr_config *src; |
148 | GSList *l; | |
02604ed6 | 149 | struct context *ctx; |
2a035e53 | 150 | int idx; |
e96cf218 BV |
151 | uint64_t i, j; |
152 | gchar *p, c; | |
02604ed6 | 153 | |
4829d37d BV |
154 | *out = NULL; |
155 | if (!o || !o->sdi) | |
02604ed6 | 156 | return SR_ERR_ARG; |
d686c5ec | 157 | if (!(ctx = o->priv)) |
02604ed6 | 158 | return SR_ERR_ARG; |
02604ed6 | 159 | |
4829d37d | 160 | switch (packet->type) { |
0f3d8c89 BV |
161 | case SR_DF_META: |
162 | meta = packet->payload; | |
163 | for (l = meta->config; l; l = l->next) { | |
164 | src = l->data; | |
165 | if (src->key != SR_CONF_SAMPLERATE) | |
166 | continue; | |
167 | ctx->samplerate = g_variant_get_uint64(src->data); | |
168 | } | |
169 | break; | |
4829d37d BV |
170 | case SR_DF_LOGIC: |
171 | logic = packet->payload; | |
0f3d8c89 BV |
172 | if (!ctx->header_done) { |
173 | *out = gen_header(o); | |
174 | ctx->header_done = TRUE; | |
4829d37d BV |
175 | } else { |
176 | *out = g_string_sized_new(512); | |
177 | } | |
178 | ||
2a035e53 | 179 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { |
ba7dd8bb | 180 | for (j = 0; j < ctx->num_enabled_channels; j++) { |
2a035e53 BV |
181 | idx = ctx->channel_index[j]; |
182 | p = logic->data + i + idx / 8; | |
183 | c = *p & (1 << (idx % 8)); | |
e96cf218 BV |
184 | g_string_append_c(*out, c ? '1' : '0'); |
185 | g_string_append_c(*out, ctx->separator); | |
4829d37d | 186 | } |
e36aae98 | 187 | if (j) { |
54da58ca BV |
188 | /* Drop last separator. */ |
189 | g_string_truncate(*out, (*out)->len - 1); | |
190 | } | |
4829d37d BV |
191 | g_string_append_printf(*out, "\n"); |
192 | } | |
02604ed6 | 193 | break; |
02604ed6 UH |
194 | } |
195 | ||
196 | return SR_OK; | |
197 | } | |
198 | ||
4829d37d | 199 | static int cleanup(struct sr_output *o) |
02604ed6 UH |
200 | { |
201 | struct context *ctx; | |
02604ed6 | 202 | |
4829d37d | 203 | if (!o || !o->sdi) |
02604ed6 | 204 | return SR_ERR_ARG; |
02604ed6 | 205 | |
d686c5ec BV |
206 | if (o->priv) { |
207 | ctx = o->priv; | |
2a035e53 | 208 | g_free(ctx->channel_index); |
d686c5ec BV |
209 | g_free(o->priv); |
210 | o->priv = NULL; | |
02604ed6 UH |
211 | } |
212 | ||
02604ed6 UH |
213 | return SR_OK; |
214 | } | |
215 | ||
a755b0e1 | 216 | SR_PRIV struct sr_output_module output_csv = { |
02604ed6 | 217 | .id = "csv", |
a755b0e1 BV |
218 | .name = "CSV", |
219 | .desc = "Comma-separated values", | |
220 | .options = NULL, | |
02604ed6 | 221 | .init = init, |
4829d37d BV |
222 | .receive = receive, |
223 | .cleanup = cleanup, | |
02604ed6 | 224 | }; |