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