]> sigrok.org Git - libsigrok.git/blob - output/output_vcd.c
Gnuplot output: More error handling.
[libsigrok.git] / output / output_vcd.c
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
27 struct context {
28         int num_enabled_probes;
29         int unitsize;
30         char *probelist[65];
31         int *prevbits;
32         char *header;
33 };
34
35 const char *vcd_header = "\
36 $date\n  %s\n$end\n\
37 $version\n  %s\n$end\n%s\
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
45 const char *vcd_header_comment = "\
46 $comment\n  Acquisition with %d/%d probes at %s\n$end\n";
47
48 static int init(struct output *o)
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;
58         char *c, *samplerate_s;
59         char wbuf[1000], comment[128];
60
61         if (!(ctx = calloc(1, sizeof(struct context))))
62                 return SIGROK_ERR_MALLOC;
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
76         if (!(ctx->header = calloc(1, MAX_HEADER_LEN + 1))) {
77                 free(ctx);
78                 return SIGROK_ERR_MALLOC;
79         }
80         num_probes = g_slist_length(o->device->probes);
81
82         comment[0] = '\0';
83         if (o->device->plugin) {
84                 /* TODO: Handle num_probes == 0, too many probes, etc. */
85                 /* TODO: Error handling. */
86                 samplerate = *((uint64_t *) o->device->plugin->get_device_info(
87                                 o->device->plugin_index, DI_CUR_SAMPLERATE));
88                 if (!((samplerate_s = sigrok_samplerate_string(samplerate)))) {
89                         free(ctx->header);
90                         free(ctx);
91                         return SIGROK_ERR;
92                 }
93                 /* TODO: Handle sprintf() errors. */
94                 snprintf(comment, 127, vcd_header_comment,
95                          ctx->num_enabled_probes, num_probes, samplerate_s);
96                 free(samplerate_s);
97         }
98
99         /* Wires / channels */
100         wbuf[0] = '\0';
101         for (i = 0; i < ctx->num_enabled_probes; i++) {
102                 c = (char *)&wbuf + strlen((char *)&wbuf);
103                 /* TODO: Needs fixing for very large number of probes. */
104                 /* TODO: Handle sprintf() errors. */
105                 sprintf(c, "$var wire 1 %c channel%s $end\n",
106                         (char)('!' + i), ctx->probelist[i]);
107         }
108
109         /* TODO: Date: File or signals? Make y/n configurable. */
110         b = snprintf(ctx->header, MAX_HEADER_LEN, vcd_header, "TODO: Date",
111                      PACKAGE_STRING, comment, 1, "ns", PACKAGE, (char *)&wbuf);
112         /* TODO: Handle snprintf() errors. */
113
114         if (!(ctx->prevbits = calloc(sizeof(int), num_probes))) {
115                 free(ctx->header);
116                 free(ctx);
117                 return SIGROK_ERR_MALLOC;
118         }
119
120         return SIGROK_OK;
121 }
122
123 static 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;
131         switch (event_type) {
132         case DF_TRIGGER:
133                 /* TODO */
134                 break;
135         case DF_END:
136                 outlen = strlen("$dumpoff\n$end\n");
137                 if (!(outbuf = malloc(outlen + 1)))
138                         return SIGROK_ERR_MALLOC;
139                 /* TODO: Bug? Drop the + 1? */
140                 /* TODO: Handle snprintf() errors. */
141                 snprintf(outbuf, outlen + 1, "$dumpoff\n$end\n");
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
152 static 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;
156         unsigned int offset, outsize;
157         int p, curbit, prevbit;
158         uint64_t sample, prevsample;
159         char *outbuf, *c;
160
161         ctx = o->internal;
162         outsize = 0;
163         if (ctx->header)
164                 outsize = strlen(ctx->header);
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';
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;
176         }
177
178         /* TODO: Are disabled probes handled correctly? */
179
180         for (offset = 0; offset <= length_in - ctx->unitsize;
181              offset += ctx->unitsize) {
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 {
188                                 memcpy(&prevsample, data_in + offset - 1,
189                                        ctx->unitsize);
190                                 prevbit =
191                                     (prevsample & ((uint64_t) (1 << p))) != 0;
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);
200                                 sprintf(c, "%i%c\n", curbit, (char)('!' + p));
201                         }
202                 }
203
204                 /* TODO: Use realloc() if strlen(outbuf) is almost "full"... */
205         }
206
207         *data_out = outbuf;
208         *length_out = strlen(outbuf);
209
210         return SIGROK_OK;
211 }
212
213 struct output_format output_vcd = {
214         "vcd",
215         "Value Change Dump (VCD)",
216         init,
217         data,
218         event,
219 };