]> sigrok.org Git - libsigrok.git/blame - output/hex.c
output/bits: Support getting samplerate from meta packets.
[libsigrok.git] / output / hex.c
CommitLineData
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
20#include <stdlib.h>
21#include <string.h>
22#include <glib.h>
23#include "libsigrok.h"
24#include "libsigrok-internal.h"
25
26#define LOG_PREFIX "output/hex"
27
28#define DEFAULT_SAMPLES_PER_LINE 192
29
30struct context {
31 unsigned int num_enabled_channels;
32 int samples_per_line;
33 int bit_cnt;
34 int spl_cnt;
35 int trigger;
36 int *channel_index;
37 char **channel_names;
38 char **line_values;
39 uint8_t *sample_buf;
40 GString **lines;
41 GString *header;
42};
43
44static int init(struct sr_output *o)
45{
46 struct context *ctx;
47 struct sr_channel *ch;
48 GSList *l;
49 GVariant *gvar;
dba3e682
BV
50 GHashTableIter iter;
51 gpointer key, value;
8d3af2e8
BV
52 uint64_t samplerate;
53 unsigned int i, j;
54 int spl, num_channels;
55 char *samplerate_s;
56
57 if (!o || !o->sdi)
58 return SR_ERR_ARG;
dba3e682
BV
59
60 spl = DEFAULT_SAMPLES_PER_LINE;
61 g_hash_table_iter_init(&iter, o->params);
62 while (g_hash_table_iter_next(&iter, &key, &value)) {
63 if (!strcmp(key, "width")) {
64 if ((spl = strtoul(value, NULL, 10)) < 1) {
65 sr_err("Invalid width.");
66 return SR_ERR_ARG;
67 }
68 } else {
69 sr_err("Unknown parameter '%s'.", key);
70 return SR_ERR_ARG;
71 }
72 }
73
8d3af2e8
BV
74 ctx = g_malloc0(sizeof(struct context));
75 o->internal = ctx;
76 ctx->trigger = -1;
8d3af2e8
BV
77 ctx->samples_per_line = spl;
78
79 for (l = o->sdi->channels; l; l = l->next) {
80 ch = l->data;
81 if (ch->type != SR_CHANNEL_LOGIC)
82 continue;
83 if (!ch->enabled)
84 continue;
85 ctx->num_enabled_channels++;
86 }
87 ctx->channel_index = g_malloc(sizeof(int) * ctx->num_enabled_channels);
88 ctx->channel_names = g_malloc(sizeof(char *) * ctx->num_enabled_channels);
89 ctx->lines = g_malloc(sizeof(GString *) * ctx->num_enabled_channels);
90 ctx->sample_buf = g_malloc(ctx->num_enabled_channels);
91
92 j = 0;
93 for (i = 0, l = o->sdi->channels; l; l = l->next, i++) {
94 ch = l->data;
95 if (ch->type != SR_CHANNEL_LOGIC)
96 continue;
97 if (!ch->enabled)
98 continue;
99 ctx->channel_index[j] = ch->index;
100 ctx->channel_names[j] = ch->name;
101 ctx->lines[j] = g_string_sized_new(80);
102 ctx->sample_buf[j] = 0;
103 g_string_printf(ctx->lines[j], "%s:", ch->name);
104 j++;
105 }
106
107 ctx->header = g_string_sized_new(512);
108 g_string_printf(ctx->header, "%s\n", PACKAGE_STRING);
109 num_channels = g_slist_length(o->sdi->channels);
110 if (sr_config_get(o->sdi->driver, o->sdi, NULL, SR_CONF_SAMPLERATE,
111 &gvar) == SR_OK) {
112 samplerate = g_variant_get_uint64(gvar);
113 samplerate_s = sr_samplerate_string(samplerate);
114 g_string_append_printf(ctx->header, "Acquisition with %d/%d channels at %s\n",
115 ctx->num_enabled_channels, num_channels, samplerate_s);
116 g_free(samplerate_s);
117 g_variant_unref(gvar);
118 }
119
120 return SR_OK;
121}
122
dba3e682
BV
123static int receive(struct sr_output *o, const struct sr_datafeed_packet *packet,
124 GString **out)
8d3af2e8
BV
125{
126 const struct sr_datafeed_logic *logic;
127 struct context *ctx;
128 int idx, pos, offset;
129 uint64_t i, j;
130 gchar *p;
131
8d3af2e8
BV
132 *out = NULL;
133 if (!o || !o->sdi)
134 return SR_ERR_ARG;
135 if (!(ctx = o->internal))
136 return SR_ERR_ARG;
137
138 switch (packet->type) {
139 case SR_DF_TRIGGER:
140 ctx->trigger = ctx->spl_cnt;
141 break;
142 case SR_DF_LOGIC:
143 if (ctx->header) {
144 /* The header is still here, this must be the first packet. */
145 *out = ctx->header;
146 ctx->header = NULL;
147 } else
148 *out = g_string_sized_new(512);
149
150 logic = packet->payload;
151 for (i = 0; i <= logic->length - logic->unitsize; i += logic->unitsize) {
152 ctx->spl_cnt++;
153 pos = ctx->spl_cnt & 7;
154 for (j = 0; j < ctx->num_enabled_channels; j++) {
155 idx = ctx->channel_index[j];
156 p = logic->data + i + idx / 8;
157 ctx->sample_buf[j] <<= 1;
158 if (*p & (1 << (idx % 8)))
159 ctx->sample_buf[j] |= 1;
160 if (ctx->spl_cnt && pos == 0) {
161 /* Buffered a byte's worth, output hex. */
162 g_string_append_printf(ctx->lines[j], "%.2x ",
163 ctx->sample_buf[j]);
164 ctx->sample_buf[j] = 0;
165 }
166
167 if (ctx->spl_cnt == ctx->samples_per_line) {
168 /* Flush line buffers. */
169 g_string_append_len(*out, ctx->lines[j]->str, ctx->lines[j]->len);
170 g_string_append_c(*out, '\n');
171 if (j == ctx->num_enabled_channels - 1 && ctx->trigger > -1) {
172 offset = ctx->trigger + ctx->trigger / 8;
173 g_string_append_printf(*out, "T:%*s^ %d\n", offset, "", ctx->trigger);
174 ctx->trigger = -1;
175 }
176 g_string_printf(ctx->lines[j], "%s:", ctx->channel_names[j]);
177 }
178 }
179 if (ctx->spl_cnt == ctx->samples_per_line)
180 /* Line buffers were already flushed. */
181 ctx->spl_cnt = 0;
182 }
183 break;
184 case SR_DF_END:
185 if (ctx->spl_cnt) {
186 /* Line buffers need flushing. */
187 *out = g_string_sized_new(512);
188 for (i = 0; i < ctx->num_enabled_channels; i++) {
189 if (ctx->spl_cnt & 7)
190 g_string_append_printf(ctx->lines[i], "%.2x ",
191 ctx->sample_buf[i] << (8 - (ctx->spl_cnt & 7)));
192 g_string_append_len(*out, ctx->lines[i]->str, ctx->lines[i]->len);
193 g_string_append_c(*out, '\n');
194 }
195 }
196 break;
197 }
198
199 return SR_OK;
200}
201
202static int cleanup(struct sr_output *o)
203{
204 struct context *ctx;
205 unsigned int i;
206
207 if (!o)
208 return SR_ERR_ARG;
209
210 if (!(ctx = o->internal))
211 return SR_OK;
212
8d3af2e8
BV
213 g_free(ctx->channel_index);
214 g_free(ctx->sample_buf);
215 g_free(ctx->channel_names);
216 for (i = 0; i < ctx->num_enabled_channels; i++)
217 g_string_free(ctx->lines[i], TRUE);
218 g_free(ctx->lines);
219 if (ctx->header)
220 g_string_free(ctx->header, TRUE);
221 g_free(ctx);
222 o->internal = NULL;
223
224 return SR_OK;
225}
226
227SR_PRIV struct sr_output_format output_hex = {
228 .id = "hex",
229 .description = "Hexadecimal",
230 .init = init,
231 .receive = receive,
232 .cleanup = cleanup,
233};
234