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