]>
Commit | Line | Data |
---|---|---|
a1bb33af | 1 | /* |
50985c20 | 2 | * This file is part of the libsigrok project. |
a1bb33af | 3 | * |
17bfaca6 | 4 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> |
a1bb33af UH |
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 3 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, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
19 | ||
a755b0e1 | 20 | #include <string.h> |
c1aae900 | 21 | #include <libsigrok/libsigrok.h> |
45c59c8b | 22 | #include "libsigrok-internal.h" |
a1bb33af | 23 | |
e00b3f58 | 24 | /** @cond PRIVATE */ |
a755b0e1 | 25 | #define LOG_PREFIX "output" |
e00b3f58 | 26 | /** @endcond */ |
a755b0e1 | 27 | |
393fb9cb UH |
28 | /** |
29 | * @file | |
30 | * | |
a755b0e1 | 31 | * Output module handling. |
393fb9cb UH |
32 | */ |
33 | ||
7b870c38 | 34 | /** |
a755b0e1 | 35 | * @defgroup grp_output Output modules |
7b870c38 | 36 | * |
a755b0e1 | 37 | * Output module handling. |
7b870c38 | 38 | * |
a755b0e1 BV |
39 | * libsigrok supports several output modules for file formats such as binary, |
40 | * VCD, gnuplot, and so on. It provides an output API that frontends can use. | |
41 | * New output modules can be added/implemented in libsigrok without having | |
07e1aad5 UH |
42 | * to change the frontends at all. |
43 | * | |
44 | * All output modules are fed data in a stream. Devices that can stream data | |
dba3e682 BV |
45 | * into libsigrok, instead of storing and then transferring the whole buffer, |
46 | * can thus generate output live. | |
07e1aad5 | 47 | * |
dba3e682 BV |
48 | * Output modules generate a newly allocated GString. The caller is then |
49 | * expected to free this with g_string_free() when finished with it. | |
07e1aad5 | 50 | * |
7b870c38 UH |
51 | * @{ |
52 | */ | |
53 | ||
b4bd7088 | 54 | /** @cond PRIVATE */ |
a755b0e1 BV |
55 | extern SR_PRIV struct sr_output_module output_bits; |
56 | extern SR_PRIV struct sr_output_module output_hex; | |
57 | extern SR_PRIV struct sr_output_module output_ascii; | |
58 | extern SR_PRIV struct sr_output_module output_binary; | |
59 | extern SR_PRIV struct sr_output_module output_vcd; | |
60 | extern SR_PRIV struct sr_output_module output_ols; | |
61 | extern SR_PRIV struct sr_output_module output_gnuplot; | |
62 | extern SR_PRIV struct sr_output_module output_chronovu_la8; | |
63 | extern SR_PRIV struct sr_output_module output_csv; | |
64 | extern SR_PRIV struct sr_output_module output_analog; | |
3250d8c7 | 65 | extern SR_PRIV struct sr_output_module output_srzip; |
afaa75b9 | 66 | extern SR_PRIV struct sr_output_module output_wav; |
b4bd7088 | 67 | /* @endcond */ |
a1bb33af | 68 | |
a755b0e1 | 69 | static const struct sr_output_module *output_module_list[] = { |
6f64ebb2 | 70 | &output_ascii, |
1c5b9d30 | 71 | &output_binary, |
dba3e682 BV |
72 | &output_bits, |
73 | &output_csv, | |
e2ad47b5 | 74 | &output_gnuplot, |
dba3e682 BV |
75 | &output_hex, |
76 | &output_ols, | |
77 | &output_vcd, | |
8c48f179 | 78 | &output_chronovu_la8, |
161a8a27 | 79 | &output_analog, |
3250d8c7 | 80 | &output_srzip, |
afaa75b9 | 81 | &output_wav, |
4c9ffa83 | 82 | NULL, |
a1bb33af UH |
83 | }; |
84 | ||
a755b0e1 | 85 | /** |
63f6df68 | 86 | * Returns a NULL-terminated list of all available output modules. |
a755b0e1 BV |
87 | * |
88 | * @since 0.4.0 | |
89 | */ | |
90 | SR_API const struct sr_output_module **sr_output_list(void) | |
a1bb33af | 91 | { |
a1bb33af UH |
92 | return output_module_list; |
93 | } | |
7b870c38 | 94 | |
a755b0e1 BV |
95 | /** |
96 | * Returns the specified output module's ID. | |
97 | * | |
98 | * @since 0.4.0 | |
99 | */ | |
4fb0a5f8 | 100 | SR_API const char *sr_output_id_get(const struct sr_output_module *omod) |
a755b0e1 | 101 | { |
4fb0a5f8 | 102 | if (!omod) { |
a755b0e1 BV |
103 | sr_err("Invalid output module NULL!"); |
104 | return NULL; | |
105 | } | |
106 | ||
4fb0a5f8 | 107 | return omod->id; |
a755b0e1 BV |
108 | } |
109 | ||
110 | /** | |
111 | * Returns the specified output module's name. | |
112 | * | |
113 | * @since 0.4.0 | |
114 | */ | |
4fb0a5f8 | 115 | SR_API const char *sr_output_name_get(const struct sr_output_module *omod) |
a755b0e1 | 116 | { |
4fb0a5f8 | 117 | if (!omod) { |
a755b0e1 BV |
118 | sr_err("Invalid output module NULL!"); |
119 | return NULL; | |
120 | } | |
121 | ||
4fb0a5f8 | 122 | return omod->name; |
a755b0e1 BV |
123 | } |
124 | ||
125 | /** | |
126 | * Returns the specified output module's description. | |
127 | * | |
128 | * @since 0.4.0 | |
129 | */ | |
4fb0a5f8 | 130 | SR_API const char *sr_output_description_get(const struct sr_output_module *omod) |
a755b0e1 | 131 | { |
4fb0a5f8 | 132 | if (!omod) { |
a755b0e1 BV |
133 | sr_err("Invalid output module NULL!"); |
134 | return NULL; | |
135 | } | |
136 | ||
4fb0a5f8 | 137 | return omod->desc; |
a755b0e1 BV |
138 | } |
139 | ||
8a174d23 JH |
140 | /** |
141 | * Returns the specified output module's file extensions typical for the file | |
142 | * format, as a NULL terminated array, or returns a NULL pointer if there is | |
143 | * no preferred extension. | |
144 | * @note these are a suggestions only. | |
145 | * | |
146 | * @since 0.4.0 | |
147 | */ | |
148 | SR_API const char *const *sr_output_extensions_get( | |
4fb0a5f8 | 149 | const struct sr_output_module *omod) |
8a174d23 | 150 | { |
4fb0a5f8 | 151 | if (!omod) { |
8a174d23 JH |
152 | sr_err("Invalid output module NULL!"); |
153 | return NULL; | |
154 | } | |
155 | ||
4fb0a5f8 | 156 | return omod->exts; |
8a174d23 JH |
157 | } |
158 | ||
3cd4b381 SA |
159 | /* |
160 | * Checks whether a given flag is set. | |
161 | * | |
162 | * @see sr_output_flag | |
163 | * @since 0.4.0 | |
164 | */ | |
165 | SR_API gboolean sr_output_test_flag(const struct sr_output_module *omod, | |
166 | uint64_t flag) | |
167 | { | |
168 | return (flag & omod->flags); | |
169 | } | |
170 | ||
a755b0e1 BV |
171 | /** |
172 | * Return the output module with the specified ID, or NULL if no module | |
173 | * with that id is found. | |
174 | * | |
175 | * @since 0.4.0 | |
176 | */ | |
177 | SR_API const struct sr_output_module *sr_output_find(char *id) | |
178 | { | |
179 | int i; | |
180 | ||
181 | for (i = 0; output_module_list[i]; i++) { | |
182 | if (!strcmp(output_module_list[i]->id, id)) | |
183 | return output_module_list[i]; | |
184 | } | |
185 | ||
186 | return NULL; | |
187 | } | |
188 | ||
189 | /** | |
190 | * Returns a NULL-terminated array of struct sr_option, or NULL if the | |
191 | * module takes no options. | |
192 | * | |
193 | * Each call to this function must be followed by a call to | |
194 | * sr_output_options_free(). | |
195 | * | |
196 | * @since 0.4.0 | |
197 | */ | |
4fb0a5f8 | 198 | SR_API const struct sr_option **sr_output_options_get(const struct sr_output_module *omod) |
a755b0e1 | 199 | { |
fc746430 BV |
200 | const struct sr_option *mod_opts, **opts; |
201 | int size, i; | |
a755b0e1 | 202 | |
4fb0a5f8 | 203 | if (!omod || !omod->options) |
a755b0e1 BV |
204 | return NULL; |
205 | ||
4fb0a5f8 | 206 | mod_opts = omod->options(); |
fc746430 | 207 | |
fdefc40a | 208 | for (size = 0; mod_opts[size].id; size++) |
fc746430 | 209 | ; |
879dd50f | 210 | opts = g_malloc((size + 1) * sizeof(struct sr_option *)); |
fc746430 BV |
211 | |
212 | for (i = 0; i < size; i++) | |
213 | opts[i] = &mod_opts[i]; | |
214 | opts[i] = NULL; | |
215 | ||
216 | return opts; | |
a755b0e1 BV |
217 | } |
218 | ||
219 | /** | |
220 | * After a call to sr_output_options_get(), this function cleans up all | |
fc746430 | 221 | * resources returned by that call. |
a755b0e1 BV |
222 | * |
223 | * @since 0.4.0 | |
224 | */ | |
499c85dc | 225 | SR_API void sr_output_options_free(const struct sr_option **options) |
dba3e682 | 226 | { |
499c85dc | 227 | int i; |
a755b0e1 | 228 | |
499c85dc | 229 | if (!options) |
a755b0e1 BV |
230 | return; |
231 | ||
499c85dc BV |
232 | for (i = 0; options[i]; i++) { |
233 | if (options[i]->def) { | |
234 | g_variant_unref(options[i]->def); | |
235 | ((struct sr_option *)options[i])->def = NULL; | |
a755b0e1 BV |
236 | } |
237 | ||
499c85dc BV |
238 | if (options[i]->values) { |
239 | g_slist_free_full(options[i]->values, (GDestroyNotify)g_variant_unref); | |
240 | ((struct sr_option *)options[i])->values = NULL; | |
a755b0e1 | 241 | } |
dba3e682 | 242 | } |
499c85dc | 243 | g_free(options); |
a755b0e1 | 244 | } |
dba3e682 | 245 | |
a755b0e1 BV |
246 | /** |
247 | * Create a new output instance using the specified output module. | |
248 | * | |
249 | * <code>options</code> is a *HashTable with the keys corresponding with | |
250 | * the module options' <code>id</code> field. The values should be GVariant | |
251 | * pointers with sunk * references, of the same GVariantType as the option's | |
252 | * default value. | |
253 | * | |
254 | * The sr_dev_inst passed in can be used by the instance to determine | |
255 | * channel names, samplerate, and so on. | |
256 | * | |
257 | * @since 0.4.0 | |
258 | */ | |
4fb0a5f8 | 259 | SR_API const struct sr_output *sr_output_new(const struct sr_output_module *omod, |
81b3ce37 SA |
260 | GHashTable *options, const struct sr_dev_inst *sdi, |
261 | const char *filename) | |
a755b0e1 BV |
262 | { |
263 | struct sr_output *op; | |
af7d656d | 264 | const struct sr_option *mod_opts; |
dcc55fe9 BV |
265 | const GVariantType *gvt; |
266 | GHashTable *new_opts; | |
267 | GHashTableIter iter; | |
268 | gpointer key, value; | |
269 | int i; | |
a755b0e1 BV |
270 | |
271 | op = g_malloc(sizeof(struct sr_output)); | |
4fb0a5f8 | 272 | op->module = omod; |
a755b0e1 | 273 | op->sdi = sdi; |
81b3ce37 | 274 | op->filename = g_strdup(filename); |
950043c3 | 275 | |
63f6df68 BV |
276 | new_opts = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, |
277 | (GDestroyNotify)g_variant_unref); | |
4fb0a5f8 UH |
278 | if (omod->options) { |
279 | mod_opts = omod->options(); | |
dcc55fe9 | 280 | for (i = 0; mod_opts[i].id; i++) { |
63f6df68 BV |
281 | if (options && g_hash_table_lookup_extended(options, |
282 | mod_opts[i].id, &key, &value)) { | |
283 | /* Pass option along. */ | |
dcc55fe9 BV |
284 | gvt = g_variant_get_type(mod_opts[i].def); |
285 | if (!g_variant_is_of_type(value, gvt)) { | |
286 | sr_err("Invalid type for '%s' option.", key); | |
287 | g_free(op); | |
288 | return NULL; | |
289 | } | |
290 | g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id), | |
291 | g_variant_ref(value)); | |
292 | } else { | |
63f6df68 | 293 | /* Option not given: insert the default value. */ |
dcc55fe9 BV |
294 | g_hash_table_insert(new_opts, g_strdup(mod_opts[i].id), |
295 | g_variant_ref(mod_opts[i].def)); | |
296 | } | |
297 | } | |
298 | ||
299 | /* Make sure no invalid options were given. */ | |
63f6df68 BV |
300 | if (options) { |
301 | g_hash_table_iter_init(&iter, options); | |
302 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
303 | if (!g_hash_table_lookup(new_opts, key)) { | |
4fb0a5f8 | 304 | sr_err("Output module '%s' has no option '%s'", omod->id, key); |
63f6df68 BV |
305 | g_hash_table_destroy(new_opts); |
306 | g_free(op); | |
307 | return NULL; | |
308 | } | |
dcc55fe9 BV |
309 | } |
310 | } | |
63f6df68 | 311 | } |
dcc55fe9 BV |
312 | |
313 | if (op->module->init && op->module->init(op, new_opts) != SR_OK) { | |
a755b0e1 BV |
314 | g_free(op); |
315 | op = NULL; | |
316 | } | |
706f482a BV |
317 | if (new_opts) |
318 | g_hash_table_destroy(new_opts); | |
a755b0e1 BV |
319 | |
320 | return op; | |
dba3e682 BV |
321 | } |
322 | ||
a755b0e1 BV |
323 | /** |
324 | * Send a packet to the specified output instance. | |
325 | * | |
326 | * The instance's output is returned as a newly allocated GString, | |
327 | * which must be freed by the caller. | |
328 | * | |
329 | * @since 0.4.0 | |
330 | */ | |
331 | SR_API int sr_output_send(const struct sr_output *o, | |
dba3e682 BV |
332 | const struct sr_datafeed_packet *packet, GString **out) |
333 | { | |
a755b0e1 | 334 | return o->module->receive(o, packet, out); |
dba3e682 BV |
335 | } |
336 | ||
a755b0e1 BV |
337 | /** |
338 | * Free the specified output instance and all associated resources. | |
339 | * | |
340 | * @since 0.4.0 | |
341 | */ | |
342 | SR_API int sr_output_free(const struct sr_output *o) | |
dba3e682 BV |
343 | { |
344 | int ret; | |
345 | ||
a755b0e1 BV |
346 | if (!o) |
347 | return SR_ERR_ARG; | |
348 | ||
dba3e682 | 349 | ret = SR_OK; |
a755b0e1 BV |
350 | if (o->module->cleanup) |
351 | ret = o->module->cleanup((struct sr_output *)o); | |
81b3ce37 | 352 | g_free((char *)o->filename); |
a755b0e1 | 353 | g_free((gpointer)o); |
dba3e682 BV |
354 | |
355 | return ret; | |
356 | } | |
357 | ||
7b870c38 | 358 | /** @} */ |