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