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