]>
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 UH |
32 | uint64_t samplerate; |
33 | GString *header; | |
34 | char separator; | |
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 | ||
50 | static int init(struct sr_output *o) | |
51 | { | |
52 | struct context *ctx; | |
ba7dd8bb | 53 | struct sr_channel *ch; |
02604ed6 | 54 | GSList *l; |
ec4063b8 | 55 | GVariant *gvar; |
2a035e53 | 56 | int num_channels, i, j; |
02604ed6 | 57 | time_t t; |
02604ed6 | 58 | |
4829d37d | 59 | if (!o) |
02604ed6 | 60 | return SR_ERR_ARG; |
02604ed6 | 61 | |
4829d37d | 62 | if (!o->sdi) |
02604ed6 | 63 | return SR_ERR_ARG; |
02604ed6 | 64 | |
2a035e53 | 65 | ctx = g_malloc0(sizeof(struct context)); |
02604ed6 UH |
66 | o->internal = ctx; |
67 | ||
ba7dd8bb UH |
68 | /* Get the number of channels, and the unitsize. */ |
69 | for (l = o->sdi->channels; l; l = l->next) { | |
70 | ch = l->data; | |
3f239f08 | 71 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 72 | continue; |
ba7dd8bb | 73 | if (!ch->enabled) |
3699a8a1 | 74 | continue; |
ba7dd8bb | 75 | ctx->num_enabled_channels++; |
02604ed6 | 76 | } |
2a035e53 | 77 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); |
02604ed6 | 78 | |
ba7dd8bb | 79 | num_channels = g_slist_length(o->sdi->channels); |
02604ed6 | 80 | |
d3c74a6f BV |
81 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, |
82 | &gvar) == SR_OK) { | |
ec4063b8 BV |
83 | ctx->samplerate = g_variant_get_uint64(gvar); |
84 | g_variant_unref(gvar); | |
5c3c1241 BV |
85 | } else |
86 | ctx->samplerate = 0; | |
02604ed6 UH |
87 | |
88 | ctx->separator = ','; | |
02604ed6 UH |
89 | ctx->header = g_string_sized_new(512); |
90 | ||
91 | t = time(NULL); | |
92 | ||
93 | /* Some metadata */ | |
94 | g_string_append_printf(ctx->header, "; CSV, generated by %s on %s", | |
95 | PACKAGE_STRING, ctime(&t)); | |
96 | g_string_append_printf(ctx->header, "; Samplerate: %"PRIu64"\n", | |
97 | ctx->samplerate); | |
98 | ||
99 | /* Columns / channels */ | |
e36aae98 | 100 | g_string_append_printf(ctx->header, "; Channels (%d/%d):", |
ba7dd8bb | 101 | ctx->num_enabled_channels, num_channels); |
2a035e53 | 102 | for (i = 0, j = 0, l = o->sdi->channels; l; l = l->next, i++) { |
ba7dd8bb | 103 | ch = l->data; |
3f239f08 | 104 | if (ch->type != SR_CHANNEL_LOGIC) |
3699a8a1 | 105 | continue; |
ba7dd8bb | 106 | if (!ch->enabled) |
3699a8a1 | 107 | continue; |
ba7dd8bb | 108 | g_string_append_printf(ctx->header, " %s,", ch->name); |
2a035e53 BV |
109 | /* Remember the enabled channel's index while we're at it. */ |
110 | ctx->channel_index[j++] = i; | |
1c5b099a | 111 | } |
ba7dd8bb | 112 | if (o->sdi->channels) |
e36aae98 BV |
113 | /* Drop last separator. */ |
114 | g_string_truncate(ctx->header, ctx->header->len - 1); | |
02604ed6 UH |
115 | g_string_append_printf(ctx->header, "\n"); |
116 | ||
5c3c1241 | 117 | return SR_OK; |
02604ed6 UH |
118 | } |
119 | ||
dba3e682 BV |
120 | static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet, |
121 | GString **out) | |
02604ed6 | 122 | { |
4829d37d | 123 | const struct sr_datafeed_logic *logic; |
02604ed6 | 124 | struct context *ctx; |
2a035e53 | 125 | int idx; |
e96cf218 BV |
126 | uint64_t i, j; |
127 | gchar *p, c; | |
02604ed6 | 128 | |
4829d37d BV |
129 | *out = NULL; |
130 | if (!o || !o->sdi) | |
02604ed6 | 131 | return SR_ERR_ARG; |
4829d37d | 132 | if (!(ctx = o->internal)) |
02604ed6 | 133 | return SR_ERR_ARG; |
02604ed6 | 134 | |
4829d37d BV |
135 | switch (packet->type) { |
136 | case SR_DF_LOGIC: | |
137 | logic = packet->payload; | |
138 | if (ctx->header) { | |
139 | /* | |
140 | * First data packet: prime the output with the | |
141 | * previously prepared header. | |
142 | */ | |
143 | *out = ctx->header; | |
144 | ctx->header = NULL; | |
145 | } else { | |
146 | *out = g_string_sized_new(512); | |
147 | } | |
148 | ||
2a035e53 | 149 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { |
ba7dd8bb | 150 | for (j = 0; j < ctx->num_enabled_channels; j++) { |
2a035e53 BV |
151 | idx = ctx->channel_index[j]; |
152 | p = logic->data + i + idx / 8; | |
153 | c = *p & (1 << (idx % 8)); | |
e96cf218 BV |
154 | g_string_append_c(*out, c ? '1' : '0'); |
155 | g_string_append_c(*out, ctx->separator); | |
4829d37d | 156 | } |
e36aae98 | 157 | if (j) { |
54da58ca BV |
158 | /* Drop last separator. */ |
159 | g_string_truncate(*out, (*out)->len - 1); | |
160 | } | |
4829d37d BV |
161 | g_string_append_printf(*out, "\n"); |
162 | } | |
02604ed6 | 163 | break; |
02604ed6 UH |
164 | } |
165 | ||
166 | return SR_OK; | |
167 | } | |
168 | ||
4829d37d | 169 | static int cleanup(struct sr_output *o) |
02604ed6 UH |
170 | { |
171 | struct context *ctx; | |
02604ed6 | 172 | |
4829d37d | 173 | if (!o || !o->sdi) |
02604ed6 | 174 | return SR_ERR_ARG; |
02604ed6 | 175 | |
4829d37d BV |
176 | if (o->internal) { |
177 | ctx = o->internal; | |
178 | if (ctx->header) | |
179 | g_string_free(ctx->header, TRUE); | |
2a035e53 | 180 | g_free(ctx->channel_index); |
4829d37d BV |
181 | g_free(o->internal); |
182 | o->internal = NULL; | |
02604ed6 UH |
183 | } |
184 | ||
02604ed6 UH |
185 | return SR_OK; |
186 | } | |
187 | ||
7c1d391c | 188 | SR_PRIV struct sr_output_format output_csv = { |
02604ed6 UH |
189 | .id = "csv", |
190 | .description = "Comma-separated values (CSV)", | |
02604ed6 | 191 | .init = init, |
4829d37d BV |
192 | .receive = receive, |
193 | .cleanup = cleanup, | |
02604ed6 | 194 | }; |