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