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