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