]> sigrok.org Git - libsigrok.git/blame - output/output_vcd.c
Gnuplot output: More error handling.
[libsigrok.git] / output / output_vcd.c
CommitLineData
4c9ffa83
UH
1/*
2 * This file is part of the sigrok project.
3 *
4 * Copyright (C) 2010 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>
24#include <sigrok.h>
25#include "config.h"
26
27struct context {
28 int num_enabled_probes;
29 int unitsize;
30 char *probelist[65];
31 int *prevbits;
32 char *header;
4c9ffa83
UH
33};
34
35const char *vcd_header = "\
36$date\n %s\n$end\n\
7aae7462 37$version\n %s\n$end\n%s\
4c9ffa83
UH
38$timescale\n %i %s\n$end\n\
39$scope module %s $end\n\
40%s\
41$upscope $end\n\
42$enddefinitions $end\n\
43$dumpvars\n";
44
7aae7462
BV
45const char *vcd_header_comment = "\
46$comment\n Acquisition with %d/%d probes at %s\n$end\n";
47
5a8fda15 48static int init(struct output *o)
4c9ffa83
UH
49{
50/* Maximum header length */
51#define MAX_HEADER_LEN 2048
52
53 struct context *ctx;
54 struct probe *probe;
55 GSList *l;
56 uint64_t samplerate;
57 int i, b, num_probes;
25e7d9b1 58 char *c, *samplerate_s;
7aae7462 59 char wbuf[1000], comment[128];
4c9ffa83 60
6b5e3cee 61 if (!(ctx = calloc(1, sizeof(struct context))))
e31b636d 62 return SIGROK_ERR_MALLOC;
4c9ffa83
UH
63 o->internal = ctx;
64 ctx->num_enabled_probes = 0;
65 for (l = o->device->probes; l; l = l->next) {
66 probe = l->data;
67 if (probe->enabled)
68 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
69 }
70
71 ctx->probelist[ctx->num_enabled_probes] = 0;
72 ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
73
74 /* TODO: Allow for configuration via o->param. */
75
6b5e3cee
UH
76 if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
77 free(ctx);
e31b636d 78 return SIGROK_ERR_MALLOC;
6b5e3cee 79 }
4c9ffa83 80 num_probes = g_slist_length(o->device->probes);
4c9ffa83 81
6b5e3cee 82 comment[0] = '\0';
7aae7462
BV
83 if (o->device->plugin) {
84 /* TODO: Handle num_probes == 0, too many probes, etc. */
6b5e3cee 85 /* TODO: Error handling. */
7aae7462
BV
86 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
87 o->device->plugin_index, DI_CUR_SAMPLERATE));
6b5e3cee
UH
88 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
89 free(ctx->header);
90 free(ctx);
7aae7462 91 return SIGROK_ERR;
6b5e3cee
UH
92 }
93 /* TODO: Handle sprintf() errors. */
94 snprintf(comment, 127, vcd_header_comment,
95 ctx->num_enabled_probes, num_probes, samplerate_s);
7aae7462
BV
96 free(samplerate_s);
97 }
4c9ffa83
UH
98
99 /* Wires / channels */
100 wbuf[0] = '\0';
bc010c05 101 for (i = 0; i < ctx->num_enabled_probes; i++) {
4c9ffa83 102 c = (char *)&wbuf + strlen((char *)&wbuf);
6b5e3cee
UH
103 /* TODO: Needs fixing for very large number of probes. */
104 /* TODO: Handle sprintf() errors. */
bc010c05 105 sprintf(c, "$var wire 1 %c channel%s $end\n",
99c1fc59 106 (char)('!' + i), ctx->probelist[i]);
4c9ffa83
UH
107 }
108
99c1fc59 109 /* TODO: Date: File or signals? Make y/n configurable. */
4c9ffa83 110 b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
6b5e3cee
UH
111 PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf);
112 /* TODO: Handle snprintf() errors. */
4c9ffa83 113
6b5e3cee
UH
114 if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
115 free(ctx->header);
116 free(ctx);
e31b636d 117 return SIGROK_ERR_MALLOC;
6b5e3cee 118 }
5a8fda15 119
6b5e3cee 120 return SIGROK_OK;
4c9ffa83
UH
121}
122
123static int event(struct output *o, int event_type, char **data_out,
124 uint64_t *length_out)
125{
126 struct context *ctx;
127 char *outbuf;
128 int outlen;
129
130 ctx = o->internal;
99c1fc59 131 switch (event_type) {
4c9ffa83 132 case DF_TRIGGER:
6b5e3cee 133 /* TODO */
4c9ffa83
UH
134 break;
135 case DF_END:
136 outlen = strlen("$dumpoff\n$end\n");
6b5e3cee 137 if (!(outbuf = malloc(outlen + 1)))
e31b636d 138 return SIGROK_ERR_MALLOC;
6b5e3cee
UH
139 /* TODO: Bug? Drop the + 1? */
140 /* TODO: Handle snprintf() errors. */
bc010c05 141 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
4c9ffa83
UH
142 *data_out = outbuf;
143 *length_out = outlen;
144 free(o->internal);
145 o->internal = NULL;
146 break;
147 }
148
149 return SIGROK_OK;
150}
151
152static int data(struct output *o, char *data_in, uint64_t length_in,
153 char **data_out, uint64_t *length_out)
154{
155 struct context *ctx;
afc8e4de
UH
156 unsigned int offset, outsize;
157 int p, curbit, prevbit;
4c9ffa83
UH
158 uint64_t sample, prevsample;
159 char *outbuf, *c;
160
161 ctx = o->internal;
e273a904
HE
162 outsize = 0;
163 if (ctx->header)
164 outsize = strlen(ctx->header);
6b5e3cee
UH
165
166 /* FIXME: Use realloc(). */
167 if (!(outbuf = calloc(1, outsize + 1 + 10000)))
168 return SIGROK_ERR_MALLOC; /* TODO: free()? What to free? */
169
170 outbuf[0] = '\0';
4c9ffa83
UH
171 if (ctx->header) {
172 /* The header is still here, this must be the first packet. */
173 strncpy(outbuf, ctx->header, outsize);
174 free(ctx->header);
175 ctx->header = NULL;
4c9ffa83
UH
176 }
177
178 /* TODO: Are disabled probes handled correctly? */
179
180 for (offset = 0; offset <= length_in - ctx->unitsize;
99c1fc59 181 offset += ctx->unitsize) {
4c9ffa83
UH
182 memcpy(&sample, data_in + offset, ctx->unitsize);
183 for (p = 0; p < ctx->num_enabled_probes; p++) {
184 curbit = (sample & ((uint64_t) (1 << p))) != 0;
185 if (offset == 0) {
186 prevbit = ~curbit;
187 } else {
99c1fc59
UH
188 memcpy(&prevsample, data_in + offset - 1,
189 ctx->unitsize);
190 prevbit =
191 (prevsample & ((uint64_t) (1 << p))) != 0;
4c9ffa83
UH
192 }
193
194 if (prevbit != curbit) {
195 /* FIXME: Only once per sample? */
196 c = outbuf + strlen(outbuf);
197 sprintf(c, "#%i\n", offset * 1 /* TODO */);
198
199 c = outbuf + strlen(outbuf);
6b5e3cee 200 sprintf(c, "%i%c\n", curbit, (char)('!' + p));
4c9ffa83
UH
201 }
202 }
203
99c1fc59 204 /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
4c9ffa83
UH
205 }
206
207 *data_out = outbuf;
208 *length_out = strlen(outbuf);
209
210 return SIGROK_OK;
211}
212
213struct output_format output_vcd = {
214 "vcd",
215 "Value Change Dump (VCD)",
216 init,
217 data,
218 event,
219};