]> sigrok.org Git - libsigrok.git/blob - output/gnuplot.c
gnuplot: fix for big-endian architectures.
[libsigrok.git] / output / gnuplot.c
1 /*
2  * This file is part of the libsigrok 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 "config.h" /* Needed for PACKAGE_STRING and others. */
25 #include "libsigrok.h"
26 #include "libsigrok-internal.h"
27
28 /* Message logging helpers with driver-specific prefix string. */
29 #define DRIVER_LOG_DOMAIN "output/gnuplot: "
30 #define sr_log(l, s, args...) sr_log(l, DRIVER_LOG_DOMAIN s, ## args)
31 #define sr_spew(s, args...) sr_spew(DRIVER_LOG_DOMAIN s, ## args)
32 #define sr_dbg(s, args...) sr_dbg(DRIVER_LOG_DOMAIN s, ## args)
33 #define sr_info(s, args...) sr_info(DRIVER_LOG_DOMAIN s, ## args)
34 #define sr_warn(s, args...) sr_warn(DRIVER_LOG_DOMAIN s, ## args)
35 #define sr_err(s, args...) sr_err(DRIVER_LOG_DOMAIN s, ## args)
36
37 struct context {
38         unsigned int num_enabled_probes;
39         unsigned int unitsize;
40         char *probelist[SR_MAX_NUM_PROBES + 1];
41         char *header;
42         uint8_t *old_sample;
43 };
44
45 #define MAX_HEADER_LEN \
46         (1024 + (SR_MAX_NUM_PROBES * (SR_MAX_PROBENAME_LEN + 10)))
47
48 static const char *gnuplot_header = "\
49 # Sample data in space-separated columns format usable by gnuplot\n\
50 #\n\
51 # Generated by: %s on %s%s\
52 # Period: %s\n\
53 #\n\
54 # Column\tProbe\n\
55 # -------------------------------------\
56 ----------------------------------------\n\
57 # 0\t\tSample counter (for internal gnuplot purposes)\n%s\n";
58
59 static const char *gnuplot_header_comment = "\
60 # Comment: Acquisition with %d/%d probes at %s\n";
61
62 static int init(struct sr_output *o)
63 {
64         struct context *ctx;
65         struct sr_probe *probe;
66         GSList *l;
67         GVariant *gvar;
68         uint64_t samplerate;
69         unsigned int i;
70         int b, num_probes;
71         char *c, *frequency_s;
72         char wbuf[1000], comment[128];
73         time_t t;
74
75         if (!o) {
76                 sr_err("%s: o was NULL", __func__);
77                 return SR_ERR_ARG;
78         }
79
80         if (!o->sdi) {
81                 sr_err("%s: o->sdi was NULL", __func__);
82                 return SR_ERR_ARG;
83         }
84
85         if (!o->sdi->driver) {
86                 sr_err("%s: o->sdi->driver was NULL", __func__);
87                 return SR_ERR_ARG;
88         }
89
90         if (!(ctx = g_try_malloc0(sizeof(struct context)))) {
91                 sr_err("%s: ctx malloc failed", __func__);
92                 return SR_ERR_MALLOC;
93         }
94
95         if (!(ctx->header = g_try_malloc0(MAX_HEADER_LEN + 1))) {
96                 sr_err("%s: ctx->header malloc failed", __func__);
97                 g_free(ctx);
98                 return SR_ERR_MALLOC;
99         }
100
101         o->internal = ctx;
102         ctx->num_enabled_probes = 0;
103         for (l = o->sdi->probes; l; l = l->next) {
104                 probe = l->data;
105                 if (!probe->enabled)
106                         continue;
107                 ctx->probelist[ctx->num_enabled_probes++] = probe->name;
108         }
109         ctx->probelist[ctx->num_enabled_probes] = 0;
110         ctx->unitsize = (ctx->num_enabled_probes + 7) / 8;
111
112         num_probes = g_slist_length(o->sdi->probes);
113         comment[0] = '\0';
114         samplerate = 0;
115         if (sr_dev_has_option(o->sdi, SR_CONF_SAMPLERATE)) {
116                 o->sdi->driver->config_get(SR_CONF_SAMPLERATE, &gvar, o->sdi);
117                 samplerate = g_variant_get_uint64(gvar);
118                 if (!(frequency_s = sr_samplerate_string(samplerate))) {
119                         sr_err("%s: sr_samplerate_string failed", __func__);
120                         g_free(ctx->header);
121                         g_free(ctx);
122                         g_variant_unref(gvar);
123                         return SR_ERR;
124                 }
125                 snprintf(comment, 127, gnuplot_header_comment,
126                         ctx->num_enabled_probes, num_probes, frequency_s);
127                 g_free(frequency_s);
128                 g_variant_unref(gvar);
129         }
130
131         /* Columns / channels */
132         wbuf[0] = '\0';
133         for (i = 0; i < ctx->num_enabled_probes; i++) {
134                 c = (char *)&wbuf + strlen((const char *)&wbuf);
135                 sprintf(c, "# %d\t\t%s\n", i + 1, ctx->probelist[i]);
136         }
137
138         if (!(frequency_s = sr_period_string(samplerate))) {
139                 sr_err("%s: sr_period_string failed", __func__);
140                 g_free(ctx->header);
141                 g_free(ctx);
142                 return SR_ERR;
143         }
144
145         t = time(NULL);
146         b = snprintf(ctx->header, MAX_HEADER_LEN, gnuplot_header,
147                      PACKAGE_STRING, ctime(&t), comment, frequency_s,
148                      (char *)&wbuf);
149         g_free(frequency_s);
150
151         if (b < 0) {
152                 sr_err("%s: sprintf failed", __func__);
153                 g_free(ctx->header);
154                 g_free(ctx);
155                 return SR_ERR;
156         }
157
158         if (!(ctx->old_sample = g_try_malloc0(ctx->unitsize))) {
159                 sr_err("%s: ctx->old_sample malloc failed", __func__);
160                 g_free(ctx->header);
161                 g_free(ctx);
162                 return SR_ERR_MALLOC;
163         }
164
165         return 0;
166 }
167
168 static int event(struct sr_output *o, int event_type, uint8_t **data_out,
169                  uint64_t *length_out)
170 {
171         if (!o) {
172                 sr_err("%s: o was NULL", __func__);
173                 return SR_ERR_ARG;
174         }
175
176         if (!data_out) {
177                 sr_err("%s: data_out was NULL", __func__);
178                 return SR_ERR_ARG;
179         }
180
181         if (!length_out) {
182                 sr_err("%s: length_out was NULL", __func__);
183                 return SR_ERR_ARG;
184         }
185
186         switch (event_type) {
187         case SR_DF_TRIGGER:
188                 /* TODO: Can a trigger mark be in a gnuplot data file? */
189                 break;
190         case SR_DF_END:
191                 g_free(o->internal);
192                 o->internal = NULL;
193                 break;
194         default:
195                 sr_err("%s: unsupported event type: %d", __func__, event_type);
196                 break;
197         }
198
199         *data_out = NULL;
200         *length_out = 0;
201
202         return SR_OK;
203 }
204
205 static int data(struct sr_output *o, const uint8_t *data_in,
206                 uint64_t length_in, uint8_t **data_out, uint64_t *length_out)
207 {
208         struct context *ctx;
209         unsigned int max_linelen, outsize, p, curbit, i;
210         const uint8_t *sample;
211         static uint64_t samplecount = 0;
212         uint8_t *outbuf, *c;
213
214         if (!o) {
215                 sr_err("%s: o was NULL", __func__);
216                 return SR_ERR_ARG;
217         }
218
219         if (!o->internal) {
220                 sr_err("%s: o->internal was NULL", __func__);
221                 return SR_ERR_ARG;
222         }
223
224         if (!data_in) {
225                 sr_err("%s: data_in was NULL", __func__);
226                 return SR_ERR_ARG;
227         }
228
229         if (!data_out) {
230                 sr_err("%s: data_out was NULL", __func__);
231                 return SR_ERR_ARG;
232         }
233
234         if (!length_out) {
235                 sr_err("%s: length_out was NULL", __func__);
236                 return SR_ERR_ARG;
237         }
238
239         ctx = o->internal;
240         max_linelen = 16 + ctx->num_enabled_probes * 2;
241         outsize = length_in / ctx->unitsize * max_linelen;
242         if (ctx->header)
243                 outsize += strlen(ctx->header);
244
245         if (!(outbuf = g_try_malloc0(outsize))) {
246                 sr_err("%s: outbuf malloc failed", __func__);
247                 return SR_ERR_MALLOC;
248         }
249
250         outbuf[0] = '\0';
251         if (ctx->header) {
252                 /* The header is still here, this must be the first packet. */
253                 strncpy((char *)outbuf, ctx->header, outsize);
254                 g_free(ctx->header);
255                 ctx->header = NULL;
256         }
257
258         for (i = 0; i <= length_in - ctx->unitsize; i += ctx->unitsize) {
259
260                 sample = data_in + i;
261
262                 /*
263                  * Don't output the same samples multiple times. However, make
264                  * sure to output at least the first and last sample.
265                  */
266                 if (samplecount++ != 0 &&
267                                 !memcmp(sample, ctx->old_sample, ctx->unitsize)) {
268                         if (i != (length_in - ctx->unitsize))
269                                 continue;
270                 }
271                 memcpy(ctx->old_sample, sample, ctx->unitsize);
272
273                 /* The first column is a counter (needed for gnuplot). */
274                 c = outbuf + strlen((const char *)outbuf);
275                 sprintf((char *)c, "%" PRIu64 "\t", samplecount++);
276
277                 /* The next columns are the values of all channels. */
278                 for (p = 0; p < ctx->num_enabled_probes; p++) {
279                         curbit = (sample[p / 8] & ((uint8_t) (1 << (p % 8)))) >> (p % 8);
280                         c = outbuf + strlen((const char *)outbuf);
281                         sprintf((char *)c, "%d ", curbit);
282                 }
283
284                 c = outbuf + strlen((const char *)outbuf);
285                 sprintf((char *)c, "\n");
286         }
287
288         *data_out = outbuf;
289         *length_out = strlen((const char *)outbuf);
290
291         return SR_OK;
292 }
293
294 SR_PRIV struct sr_output_format output_gnuplot = {
295         .id = "gnuplot",
296         .description = "Gnuplot",
297         .df_type = SR_DF_LOGIC,
298         .init = init,
299         .data = data,
300         .event = event,
301 };