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