]>
Commit | Line | Data |
---|---|---|
8d3af2e8 BV |
1 | /* |
2 | * This file is part of the libsigrok project. | |
3 | * | |
4 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> | |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
6ec6c43b | 20 | #include <config.h> |
8d3af2e8 BV |
21 | #include <stdlib.h> |
22 | #include <string.h> | |
23 | #include <glib.h> | |
c1aae900 | 24 | #include <libsigrok/libsigrok.h> |
8d3af2e8 BV |
25 | #include "libsigrok-internal.h" |
26 | ||
27 | #define LOG_PREFIX "output/hex" | |
28 | ||
29 | #define DEFAULT_SAMPLES_PER_LINE 192 | |
30 | ||
31 | struct context { | |
32 | unsigned int num_enabled_channels; | |
dcc55fe9 | 33 | int spl; |
8d3af2e8 BV |
34 | int bit_cnt; |
35 | int spl_cnt; | |
36 | int trigger; | |
7bcbbfae | 37 | uint64_t samplerate; |
8d3af2e8 BV |
38 | int *channel_index; |
39 | char **channel_names; | |
40 | char **line_values; | |
41 | uint8_t *sample_buf; | |
7bcbbfae | 42 | gboolean header_done; |
8d3af2e8 | 43 | GString **lines; |
8d3af2e8 BV |
44 | }; |
45 | ||
a755b0e1 | 46 | static int init(struct sr_output *o, GHashTable *options) |
8d3af2e8 BV |
47 | { |
48 | struct context *ctx; | |
49 | struct sr_channel *ch; | |
50 | GSList *l; | |
8d3af2e8 | 51 | unsigned int i, j; |
8d3af2e8 BV |
52 | |
53 | if (!o || !o->sdi) | |
54 | return SR_ERR_ARG; | |
dba3e682 | 55 | |
8d3af2e8 | 56 | ctx = g_malloc0(sizeof(struct context)); |
d686c5ec | 57 | o->priv = ctx; |
8d3af2e8 | 58 | ctx->trigger = -1; |
dcc55fe9 | 59 | ctx->spl = g_variant_get_uint32(g_hash_table_lookup(options, "width")); |
8d3af2e8 BV |
60 | |
61 | for (l = o->sdi->channels; l; l = l->next) { | |
62 | ch = l->data; | |
63 | if (ch->type != SR_CHANNEL_LOGIC) | |
64 | continue; | |
65 | if (!ch->enabled) | |
66 | continue; | |
67 | ctx->num_enabled_channels++; | |
68 | } | |
69 | ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels); | |
70 | ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels); | |
71 | ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels); | |
72 | ctx->sample_buf = g_malloc(ctx->num_enabled_channels); | |
73 | ||
74 | j = 0; | |
75 | for (i = 0, l = o->sdi->channels; l; l = l->next, i++) { | |
76 | ch = l->data; | |
77 | if (ch->type != SR_CHANNEL_LOGIC) | |
78 | continue; | |
79 | if (!ch->enabled) | |
80 | continue; | |
81 | ctx->channel_index[j] = ch->index; | |
82 | ctx->channel_names[j] = ch->name; | |
83 | ctx->lines[j] = g_string_sized_new(80); | |
84 | ctx->sample_buf[j] = 0; | |
85 | g_string_printf(ctx->lines[j], "%s:", ch->name); | |
86 | j++; | |
87 | } | |
88 | ||
7bcbbfae BV |
89 | return SR_OK; |
90 | } | |
91 | ||
a755b0e1 | 92 | static GString *gen_header(const struct sr_output *o) |
7bcbbfae BV |
93 | { |
94 | struct context *ctx; | |
95 | GVariant *gvar; | |
96 | GString *header; | |
97 | int num_channels; | |
98 | char *samplerate_s; | |
99 | ||
d686c5ec | 100 | ctx = o->priv; |
7bcbbfae BV |
101 | if (ctx->samplerate == 0) { |
102 | if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE, | |
103 | &gvar) == SR_OK) { | |
104 | ctx->samplerate = g_variant_get_uint64(gvar); | |
105 | g_variant_unref(gvar); | |
106 | } | |
107 | } | |
108 | ||
109 | header = g_string_sized_new(512); | |
b9eb8e1a | 110 | g_string_printf(header, "%s %s\n", PACKAGE_NAME, SR_PACKAGE_VERSION_STRING); |
8d3af2e8 | 111 | num_channels = g_slist_length(o->sdi->channels); |
7bcbbfae BV |
112 | g_string_append_printf(header, "Acquisition with %d/%d channels", |
113 | ctx->num_enabled_channels, num_channels); | |
114 | if (ctx->samplerate != 0) { | |
115 | samplerate_s = sr_samplerate_string(ctx->samplerate); | |
116 | g_string_append_printf(header, " at %s", samplerate_s); | |
8d3af2e8 | 117 | g_free(samplerate_s); |
8d3af2e8 | 118 | } |
7bcbbfae | 119 | g_string_append_printf(header, "\n"); |
8d3af2e8 | 120 | |
7bcbbfae | 121 | return header; |
8d3af2e8 BV |
122 | } |
123 | ||
a755b0e1 | 124 | static int receive(const struct sr_output *o, const struct sr_datafeed_packet *packet, |
dba3e682 | 125 | GString **out) |
8d3af2e8 | 126 | { |
7bcbbfae | 127 | const struct sr_datafeed_meta *meta; |
8d3af2e8 | 128 | const struct sr_datafeed_logic *logic; |
7bcbbfae BV |
129 | const struct sr_config *src; |
130 | GSList *l; | |
8d3af2e8 BV |
131 | struct context *ctx; |
132 | int idx, pos, offset; | |
133 | uint64_t i, j; | |
134 | gchar *p; | |
135 | ||
8d3af2e8 BV |
136 | *out = NULL; |
137 | if (!o || !o->sdi) | |
138 | return SR_ERR_ARG; | |
d686c5ec | 139 | if (!(ctx = o->priv)) |
8d3af2e8 BV |
140 | return SR_ERR_ARG; |
141 | ||
142 | switch (packet->type) { | |
7bcbbfae BV |
143 | case SR_DF_META: |
144 | meta = packet->payload; | |
145 | for (l = meta->config; l; l = l->next) { | |
146 | src = l->data; | |
147 | if (src->key != SR_CONF_SAMPLERATE) | |
148 | continue; | |
149 | ctx->samplerate = g_variant_get_uint64(src->data); | |
150 | } | |
151 | break; | |
8d3af2e8 BV |
152 | case SR_DF_TRIGGER: |
153 | ctx->trigger = ctx->spl_cnt; | |
154 | break; | |
155 | case SR_DF_LOGIC: | |
7bcbbfae BV |
156 | if (!ctx->header_done) { |
157 | *out = gen_header(o); | |
158 | ctx->header_done = TRUE; | |
8d3af2e8 BV |
159 | } else |
160 | *out = g_string_sized_new(512); | |
161 | ||
162 | logic = packet->payload; | |
163 | for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) { | |
164 | ctx->spl_cnt++; | |
165 | pos = ctx->spl_cnt & 7; | |
166 | for (j = 0; j < ctx->num_enabled_channels; j++) { | |
167 | idx = ctx->channel_index[j]; | |
168 | p = logic->data + i + idx / 8; | |
169 | ctx->sample_buf[j] <<= 1; | |
170 | if (*p & (1 << (idx % 8))) | |
171 | ctx->sample_buf[j] |= 1; | |
172 | if (ctx->spl_cnt && pos == 0) { | |
173 | /* Buffered a byte's worth, output hex. */ | |
174 | g_string_append_printf(ctx->lines[j], "%.2x ", | |
175 | ctx->sample_buf[j]); | |
176 | ctx->sample_buf[j] = 0; | |
177 | } | |
178 | ||
dcc55fe9 | 179 | if (ctx->spl_cnt == ctx->spl) { |
8d3af2e8 BV |
180 | /* Flush line buffers. */ |
181 | g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len); | |
182 | g_string_append_c(*out, '\n'); | |
8cd15dd4 UH |
183 | if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) { |
184 | /* | |
185 | * Sample data lines have one character per nibble, | |
186 | * plus one separator per byte. Align trigger marker | |
187 | * to this layout. | |
188 | */ | |
189 | offset = ctx->trigger / 4 + ctx->trigger / 8; | |
8d3af2e8 BV |
190 | g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger); |
191 | ctx->trigger = -1; | |
192 | } | |
193 | g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]); | |
194 | } | |
195 | } | |
dcc55fe9 | 196 | if (ctx->spl_cnt == ctx->spl) |
8d3af2e8 BV |
197 | /* Line buffers were already flushed. */ |
198 | ctx->spl_cnt = 0; | |
199 | } | |
200 | break; | |
201 | case SR_DF_END: | |
202 | if (ctx->spl_cnt) { | |
203 | /* Line buffers need flushing. */ | |
204 | *out = g_string_sized_new(512); | |
205 | for (i = 0; i < ctx->num_enabled_channels; i++) { | |
206 | if (ctx->spl_cnt & 7) | |
207 | g_string_append_printf(ctx->lines[i], "%.2x ", | |
208 | ctx->sample_buf[i] << (8 - (ctx->spl_cnt & 7))); | |
209 | g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len); | |
210 | g_string_append_c(*out, '\n'); | |
211 | } | |
212 | } | |
213 | break; | |
214 | } | |
215 | ||
216 | return SR_OK; | |
217 | } | |
218 | ||
219 | static int cleanup(struct sr_output *o) | |
220 | { | |
221 | struct context *ctx; | |
222 | unsigned int i; | |
223 | ||
224 | if (!o) | |
225 | return SR_ERR_ARG; | |
226 | ||
d686c5ec | 227 | if (!(ctx = o->priv)) |
8d3af2e8 BV |
228 | return SR_OK; |
229 | ||
8d3af2e8 BV |
230 | g_free(ctx->channel_index); |
231 | g_free(ctx->sample_buf); | |
232 | g_free(ctx->channel_names); | |
233 | for (i = 0; i < ctx->num_enabled_channels; i++) | |
234 | g_string_free(ctx->lines[i], TRUE); | |
235 | g_free(ctx->lines); | |
8d3af2e8 | 236 | g_free(ctx); |
d686c5ec | 237 | o->priv = NULL; |
8d3af2e8 BV |
238 | |
239 | return SR_OK; | |
240 | } | |
241 | ||
a755b0e1 BV |
242 | static struct sr_option options[] = { |
243 | { "width", "Width", "Number of samples per line", NULL, NULL }, | |
1685c276 | 244 | ALL_ZERO |
a755b0e1 BV |
245 | }; |
246 | ||
af7d656d | 247 | static const struct sr_option *get_options(void) |
a755b0e1 | 248 | { |
950043c3 BV |
249 | if (!options[0].def) { |
250 | options[0].def = g_variant_new_uint32(DEFAULT_SAMPLES_PER_LINE); | |
251 | g_variant_ref_sink(options[0].def); | |
252 | } | |
a755b0e1 BV |
253 | |
254 | return options; | |
255 | } | |
256 | ||
257 | SR_PRIV struct sr_output_module output_hex = { | |
8d3af2e8 | 258 | .id = "hex", |
a755b0e1 | 259 | .name = "Hexadecimal", |
8cd15dd4 | 260 | .desc = "Hexadecimal digits logic data", |
8a174d23 | 261 | .exts = (const char*[]){"txt", NULL}, |
3cd4b381 | 262 | .flags = 0, |
a755b0e1 | 263 | .options = get_options, |
8d3af2e8 BV |
264 | .init = init, |
265 | .receive = receive, | |
266 | .cleanup = cleanup, | |
267 | }; |