]>
Commit | Line | Data |
---|---|---|
2be182e6 BV |
1 | /* |
2 | * This file is part of the sigrok-cli project. | |
3 | * | |
4 | * Copyright (C) 2013 Bert Vermeulen <bert@biot.com> | |
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 | ||
20fb52e0 | 20 | #include "sigrok-cli.h" |
2be182e6 BV |
21 | #include <glib.h> |
22 | #include <glib/gstdio.h> | |
55de58a2 UH |
23 | #include <string.h> |
24 | #include <stdlib.h> | |
2be182e6 BV |
25 | |
26 | static struct sr_output_format *output_format = NULL; | |
27 | static int default_output_format = FALSE; | |
2be182e6 BV |
28 | static uint64_t limit_samples = 0; |
29 | static uint64_t limit_frames = 0; | |
30 | ||
31 | extern gchar *opt_output_file; | |
32 | extern gchar *opt_output_format; | |
33 | extern gchar *opt_pds; | |
34 | extern gboolean opt_wait_trigger; | |
35 | extern gchar *opt_time; | |
36 | extern gchar *opt_samples; | |
37 | extern gchar *opt_frames; | |
38 | extern gchar *opt_continuous; | |
39 | extern gchar *opt_config; | |
40 | extern gchar *opt_triggers; | |
41 | #ifdef HAVE_SRD | |
42 | extern struct srd_session *srd_sess; | |
43 | #endif | |
44 | ||
2be182e6 BV |
45 | static int set_limit_time(const struct sr_dev_inst *sdi) |
46 | { | |
47 | GVariant *gvar; | |
48 | uint64_t time_msec; | |
49 | uint64_t samplerate; | |
50 | ||
51 | if (!(time_msec = sr_parse_timestring(opt_time))) { | |
52 | g_critical("Invalid time '%s'", opt_time); | |
53 | return SR_ERR; | |
54 | } | |
55 | ||
56 | if (sr_dev_has_option(sdi, SR_CONF_LIMIT_MSEC)) { | |
57 | gvar = g_variant_new_uint64(time_msec); | |
58 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_MSEC, gvar) != SR_OK) { | |
59 | g_critical("Failed to configure time limit."); | |
60 | return SR_ERR; | |
61 | } | |
62 | } else if (sr_dev_has_option(sdi, SR_CONF_SAMPLERATE)) { | |
63 | /* Convert to samples based on the samplerate. */ | |
64 | sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, &gvar); | |
65 | samplerate = g_variant_get_uint64(gvar); | |
66 | g_variant_unref(gvar); | |
67 | limit_samples = (samplerate) * time_msec / (uint64_t)1000; | |
68 | if (limit_samples == 0) { | |
69 | g_critical("Not enough time at this samplerate."); | |
70 | return SR_ERR; | |
71 | } | |
72 | gvar = g_variant_new_uint64(limit_samples); | |
73 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
74 | g_critical("Failed to configure time-based sample limit."); | |
75 | return SR_ERR; | |
76 | } | |
77 | } else { | |
78 | g_critical("This device does not support time limits."); | |
79 | return SR_ERR; | |
80 | } | |
81 | ||
82 | return SR_OK; | |
83 | } | |
84 | ||
6709a694 | 85 | struct sr_output *setup_output_format(const struct sr_dev_inst *sdi) |
2be182e6 BV |
86 | { |
87 | GHashTable *fmtargs; | |
6709a694 | 88 | struct sr_output *o; |
2be182e6 BV |
89 | struct sr_output_format **outputs; |
90 | int i; | |
91 | char *fmtspec; | |
92 | ||
93 | if (opt_output_format && !strcmp(opt_output_format, "sigrok")) { | |
94 | /* Doesn't really exist as an output module - this is | |
95 | * the session save mode. */ | |
96 | g_free(opt_output_format); | |
97 | opt_output_format = NULL; | |
98 | } | |
99 | ||
100 | if (!opt_output_format) { | |
101 | opt_output_format = DEFAULT_OUTPUT_FORMAT; | |
102 | /* we'll need to remember this so when saving to a file | |
103 | * later, sigrok session format will be used. | |
104 | */ | |
105 | default_output_format = TRUE; | |
106 | } | |
107 | ||
108 | fmtargs = parse_generic_arg(opt_output_format, TRUE); | |
109 | fmtspec = g_hash_table_lookup(fmtargs, "sigrok_key"); | |
6709a694 | 110 | if (!fmtspec) |
2be182e6 | 111 | g_critical("Invalid output format."); |
2be182e6 BV |
112 | outputs = sr_output_list(); |
113 | for (i = 0; outputs[i]; i++) { | |
114 | if (strcmp(outputs[i]->id, fmtspec)) | |
115 | continue; | |
116 | g_hash_table_remove(fmtargs, "sigrok_key"); | |
117 | output_format = outputs[i]; | |
2be182e6 BV |
118 | break; |
119 | } | |
6709a694 BV |
120 | if (!output_format) |
121 | g_critical("Invalid output format '%s'.", opt_output_format); | |
122 | o = sr_output_new(output_format, fmtargs, sdi); | |
2be182e6 BV |
123 | g_hash_table_destroy(fmtargs); |
124 | ||
6709a694 | 125 | return o; |
2be182e6 BV |
126 | } |
127 | ||
128 | void datafeed_in(const struct sr_dev_inst *sdi, | |
129 | const struct sr_datafeed_packet *packet, void *cb_data) | |
130 | { | |
131 | const struct sr_datafeed_meta *meta; | |
132 | const struct sr_datafeed_logic *logic; | |
133 | const struct sr_datafeed_analog *analog; | |
134 | struct sr_config *src; | |
029d73fe | 135 | struct sr_channel *ch; |
2be182e6 | 136 | static struct sr_output *o = NULL; |
6ea663a7 BV |
137 | static uint64_t rcvd_samples_logic = 0; |
138 | static uint64_t rcvd_samples_analog = 0; | |
139 | static uint64_t samplerate = 0; | |
2be182e6 BV |
140 | static int triggered = 0; |
141 | static FILE *outfile = NULL; | |
142 | GSList *l; | |
143 | GString *out; | |
8b65cdcc | 144 | GVariant *gvar; |
55de58a2 | 145 | uint64_t end_sample; |
667d4a18 | 146 | uint64_t input_len; |
6ea663a7 | 147 | int i; |
029d73fe | 148 | char **channels; |
2be182e6 BV |
149 | |
150 | (void) cb_data; | |
151 | ||
152 | /* If the first packet to come in isn't a header, don't even try. */ | |
153 | if (packet->type != SR_DF_HEADER && o == NULL) | |
154 | return; | |
155 | ||
2be182e6 BV |
156 | switch (packet->type) { |
157 | case SR_DF_HEADER: | |
158 | g_debug("cli: Received SR_DF_HEADER"); | |
6709a694 | 159 | o = setup_output_format(sdi); |
2be182e6 BV |
160 | |
161 | /* Prepare non-stdout output. */ | |
162 | outfile = stdout; | |
163 | if (opt_output_file) { | |
164 | if (default_output_format) { | |
2be182e6 | 165 | outfile = NULL; |
2be182e6 BV |
166 | } else { |
167 | /* saving to a file in whatever format was set | |
168 | * with --format, so all we need is a filehandle */ | |
169 | outfile = g_fopen(opt_output_file, "wb"); | |
170 | } | |
171 | } | |
6ea663a7 | 172 | rcvd_samples_logic = rcvd_samples_analog = 0; |
2be182e6 | 173 | |
8b65cdcc BV |
174 | if (sr_config_get(sdi->driver, sdi, NULL, SR_CONF_SAMPLERATE, |
175 | &gvar) == SR_OK) { | |
176 | samplerate = g_variant_get_uint64(gvar); | |
177 | g_variant_unref(gvar); | |
178 | } | |
179 | ||
2be182e6 | 180 | #ifdef HAVE_SRD |
ea6d6dec | 181 | if (opt_pds) { |
8b65cdcc | 182 | if (samplerate) { |
2be182e6 BV |
183 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, |
184 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
185 | g_critical("Failed to configure decode session."); | |
186 | break; | |
187 | } | |
188 | } | |
189 | if (srd_session_start(srd_sess) != SRD_OK) { | |
190 | g_critical("Failed to start decode session."); | |
191 | break; | |
192 | } | |
193 | } | |
194 | #endif | |
195 | break; | |
196 | ||
197 | case SR_DF_META: | |
198 | g_debug("cli: received SR_DF_META"); | |
199 | meta = packet->payload; | |
200 | for (l = meta->config; l; l = l->next) { | |
201 | src = l->data; | |
202 | switch (src->key) { | |
203 | case SR_CONF_SAMPLERATE: | |
204 | samplerate = g_variant_get_uint64(src->data); | |
205 | g_debug("cli: got samplerate %"PRIu64" Hz", samplerate); | |
206 | #ifdef HAVE_SRD | |
207 | if (opt_pds) { | |
208 | if (srd_session_metadata_set(srd_sess, SRD_CONF_SAMPLERATE, | |
209 | g_variant_new_uint64(samplerate)) != SRD_OK) { | |
210 | g_critical("Failed to pass samplerate to decoder."); | |
211 | } | |
212 | } | |
213 | #endif | |
214 | break; | |
215 | case SR_CONF_SAMPLE_INTERVAL: | |
216 | samplerate = g_variant_get_uint64(src->data); | |
217 | g_debug("cli: got sample interval %"PRIu64" ms", samplerate); | |
218 | break; | |
219 | default: | |
220 | /* Unknown metadata is not an error. */ | |
221 | break; | |
222 | } | |
223 | } | |
224 | break; | |
225 | ||
226 | case SR_DF_TRIGGER: | |
227 | g_debug("cli: received SR_DF_TRIGGER"); | |
2be182e6 BV |
228 | triggered = 1; |
229 | break; | |
230 | ||
231 | case SR_DF_LOGIC: | |
232 | logic = packet->payload; | |
233 | g_message("cli: received SR_DF_LOGIC, %"PRIu64" bytes", logic->length); | |
2be182e6 BV |
234 | if (logic->length == 0) |
235 | break; | |
236 | ||
237 | /* Don't store any samples until triggered. */ | |
238 | if (opt_wait_trigger && !triggered) | |
239 | break; | |
240 | ||
6ea663a7 | 241 | if (limit_samples && rcvd_samples_logic >= limit_samples) |
2be182e6 BV |
242 | break; |
243 | ||
6ea663a7 | 244 | end_sample = rcvd_samples_logic + logic->length / logic->unitsize; |
8667d3c2 DE |
245 | /* Cut off last packet according to the sample limit. */ |
246 | if (limit_samples && end_sample > limit_samples) | |
247 | end_sample = limit_samples; | |
6ea663a7 | 248 | input_len = (end_sample - rcvd_samples_logic) * logic->unitsize; |
2be182e6 BV |
249 | |
250 | if (opt_output_file && default_output_format) { | |
251 | /* Saving to a session file. */ | |
6ea663a7 BV |
252 | if (rcvd_samples_logic == 0) { |
253 | /* First packet with logic data, init session file. */ | |
029d73fe UH |
254 | channels = g_malloc(sizeof(char *) * g_slist_length(sdi->channels)); |
255 | for (i = 0, l = sdi->channels; l; l = l->next) { | |
256 | ch = l->data; | |
257 | if (ch->enabled && ch->type == SR_CHANNEL_LOGIC) | |
258 | channels[i++] = ch->name; | |
6ea663a7 | 259 | } |
029d73fe | 260 | channels[i] = NULL; |
6ea663a7 | 261 | sr_session_save_init(opt_output_file, samplerate, |
029d73fe UH |
262 | channels); |
263 | g_free(channels); | |
6ea663a7 BV |
264 | } |
265 | save_chunk_logic(logic->data, input_len, logic->unitsize); | |
2be182e6 BV |
266 | } else { |
267 | if (opt_pds) { | |
268 | #ifdef HAVE_SRD | |
6ea663a7 | 269 | if (srd_session_send(srd_sess, rcvd_samples_logic, end_sample, |
8667d3c2 | 270 | logic->data, input_len) != SRD_OK) |
2be182e6 BV |
271 | sr_session_stop(); |
272 | #endif | |
2be182e6 BV |
273 | } |
274 | } | |
2be182e6 | 275 | |
6ea663a7 | 276 | rcvd_samples_logic = end_sample; |
2be182e6 BV |
277 | break; |
278 | ||
279 | case SR_DF_ANALOG: | |
280 | analog = packet->payload; | |
281 | g_message("cli: received SR_DF_ANALOG, %d samples", analog->num_samples); | |
282 | if (analog->num_samples == 0) | |
283 | break; | |
284 | ||
6ea663a7 | 285 | if (limit_samples && rcvd_samples_analog >= limit_samples) |
2be182e6 BV |
286 | break; |
287 | ||
6ea663a7 | 288 | rcvd_samples_analog += analog->num_samples; |
2be182e6 BV |
289 | break; |
290 | ||
291 | case SR_DF_FRAME_BEGIN: | |
292 | g_debug("cli: received SR_DF_FRAME_BEGIN"); | |
2be182e6 BV |
293 | break; |
294 | ||
295 | case SR_DF_FRAME_END: | |
296 | g_debug("cli: received SR_DF_FRAME_END"); | |
2be182e6 BV |
297 | break; |
298 | ||
299 | default: | |
300 | break; | |
301 | } | |
302 | ||
f4ffd032 | 303 | if (o && outfile && !opt_pds) { |
6709a694 | 304 | if (sr_output_send(o, packet, &out) == SR_OK && out) { |
2be182e6 BV |
305 | fwrite(out->str, 1, out->len, outfile); |
306 | fflush(outfile); | |
307 | g_string_free(out, TRUE); | |
308 | } | |
309 | } | |
310 | ||
311 | /* SR_DF_END needs to be handled after the output module's receive() | |
6ea663a7 | 312 | * is called, so it can properly clean up that module. */ |
2be182e6 BV |
313 | if (packet->type == SR_DF_END) { |
314 | g_debug("cli: Received SR_DF_END"); | |
315 | ||
6709a694 BV |
316 | if (o) |
317 | sr_output_free(o); | |
2be182e6 BV |
318 | o = NULL; |
319 | ||
320 | if (outfile && outfile != stdout) | |
321 | fclose(outfile); | |
322 | ||
6ea663a7 BV |
323 | if (opt_output_file && default_output_format) |
324 | /* Flush whatever is left out to the session file. */ | |
325 | save_chunk_logic(NULL, 0, 0); | |
326 | ||
327 | if (limit_samples) { | |
328 | if (rcvd_samples_logic > 0 && rcvd_samples_logic < limit_samples) | |
329 | g_warning("Device only sent %" PRIu64 " samples.", | |
330 | rcvd_samples_logic); | |
331 | else if (rcvd_samples_analog > 0 && rcvd_samples_analog < limit_samples) | |
332 | g_warning("Device only sent %" PRIu64 " samples.", | |
333 | rcvd_samples_analog); | |
2be182e6 BV |
334 | } |
335 | } | |
336 | ||
337 | } | |
338 | ||
ad54a1a6 | 339 | int opt_to_gvar(char *key, char *value, struct sr_config *src) |
2be182e6 BV |
340 | { |
341 | const struct sr_config_info *srci; | |
426d0cda | 342 | double tmp_double, dlow, dhigh; |
2be182e6 | 343 | uint64_t tmp_u64, p, q, low, high; |
ad54a1a6 | 344 | GVariant *rational[2], *range[2]; |
2be182e6 | 345 | gboolean tmp_bool; |
ad54a1a6 | 346 | int ret; |
2be182e6 | 347 | |
ad54a1a6 BV |
348 | if (!(srci = sr_config_info_name_get(key))) { |
349 | g_critical("Unknown device option '%s'.", (char *) key); | |
350 | return -1; | |
351 | } | |
352 | src->key = srci->key; | |
2be182e6 | 353 | |
ad54a1a6 BV |
354 | if ((value == NULL) && |
355 | (srci->datatype != SR_T_BOOL)) { | |
356 | g_critical("Option '%s' needs a value.", (char *)key); | |
357 | return -1; | |
358 | } | |
359 | ||
360 | ret = 0; | |
361 | switch (srci->datatype) { | |
362 | case SR_T_UINT64: | |
363 | ret = sr_parse_sizestring(value, &tmp_u64); | |
364 | if (ret != 0) | |
2be182e6 | 365 | break; |
ad54a1a6 BV |
366 | src->data = g_variant_new_uint64(tmp_u64); |
367 | break; | |
368 | case SR_T_INT32: | |
369 | ret = sr_parse_sizestring(value, &tmp_u64); | |
370 | if (ret != 0) | |
2be182e6 | 371 | break; |
ad54a1a6 BV |
372 | src->data = g_variant_new_int32(tmp_u64); |
373 | break; | |
374 | case SR_T_CHAR: | |
375 | src->data = g_variant_new_string(value); | |
376 | break; | |
377 | case SR_T_BOOL: | |
378 | if (!value) | |
379 | tmp_bool = TRUE; | |
380 | else | |
381 | tmp_bool = sr_parse_boolstring(value); | |
382 | src->data = g_variant_new_boolean(tmp_bool); | |
383 | break; | |
384 | case SR_T_FLOAT: | |
385 | tmp_double = strtof(value, NULL); | |
386 | src->data = g_variant_new_double(tmp_double); | |
387 | break; | |
388 | case SR_T_RATIONAL_PERIOD: | |
389 | if ((ret = sr_parse_period(value, &p, &q)) != SR_OK) | |
2be182e6 | 390 | break; |
ad54a1a6 BV |
391 | rational[0] = g_variant_new_uint64(p); |
392 | rational[1] = g_variant_new_uint64(q); | |
393 | src->data = g_variant_new_tuple(rational, 2); | |
394 | break; | |
395 | case SR_T_RATIONAL_VOLT: | |
396 | if ((ret = sr_parse_voltage(value, &p, &q)) != SR_OK) | |
2be182e6 | 397 | break; |
ad54a1a6 BV |
398 | rational[0] = g_variant_new_uint64(p); |
399 | rational[1] = g_variant_new_uint64(q); | |
400 | src->data = g_variant_new_tuple(rational, 2); | |
401 | break; | |
402 | case SR_T_UINT64_RANGE: | |
403 | if (sscanf(value, "%"PRIu64"-%"PRIu64, &low, &high) != 2) { | |
404 | ret = -1; | |
2be182e6 | 405 | break; |
ad54a1a6 BV |
406 | } else { |
407 | range[0] = g_variant_new_uint64(low); | |
408 | range[1] = g_variant_new_uint64(high); | |
409 | src->data = g_variant_new_tuple(range, 2); | |
2be182e6 | 410 | } |
ad54a1a6 | 411 | break; |
426d0cda BV |
412 | case SR_T_DOUBLE_RANGE: |
413 | if (sscanf(value, "%lf-%lf", &dlow, &dhigh) != 2) { | |
414 | ret = -1; | |
415 | break; | |
416 | } else { | |
417 | range[0] = g_variant_new_double(dlow); | |
418 | range[1] = g_variant_new_double(dhigh); | |
419 | src->data = g_variant_new_tuple(range, 2); | |
420 | } | |
421 | break; | |
ad54a1a6 BV |
422 | default: |
423 | ret = -1; | |
424 | } | |
425 | ||
426 | return ret; | |
427 | } | |
428 | ||
429 | int set_dev_options(struct sr_dev_inst *sdi, GHashTable *args) | |
430 | { | |
431 | struct sr_config src; | |
ca50f4b3 | 432 | struct sr_channel_group *cg; |
ad54a1a6 BV |
433 | GHashTableIter iter; |
434 | gpointer key, value; | |
435 | int ret; | |
436 | ||
437 | g_hash_table_iter_init(&iter, args); | |
438 | while (g_hash_table_iter_next(&iter, &key, &value)) { | |
439 | if ((ret = opt_to_gvar(key, value, &src)) != 0) | |
440 | return ret; | |
ca50f4b3 UH |
441 | cg = select_channel_group(sdi); |
442 | ret = sr_config_set(sdi, cg, src.key, src.data); | |
2be182e6 BV |
443 | if (ret != SR_OK) { |
444 | g_critical("Failed to set device option '%s'.", (char *)key); | |
445 | return ret; | |
446 | } | |
447 | } | |
448 | ||
449 | return SR_OK; | |
450 | } | |
451 | ||
452 | void run_session(void) | |
453 | { | |
454 | GSList *devices; | |
455 | GHashTable *devargs; | |
456 | GVariant *gvar; | |
457 | struct sr_dev_inst *sdi; | |
02c65935 | 458 | uint64_t min_samples, max_samples; |
029d73fe | 459 | int max_channels, i; |
2be182e6 BV |
460 | char **triggerlist; |
461 | ||
462 | devices = device_scan(); | |
463 | if (!devices) { | |
464 | g_critical("No devices found."); | |
465 | return; | |
466 | } | |
467 | if (g_slist_length(devices) > 1) { | |
468 | g_critical("sigrok-cli only supports one device for capturing."); | |
469 | return; | |
470 | } | |
471 | sdi = devices->data; | |
472 | ||
473 | sr_session_new(); | |
474 | sr_session_datafeed_callback_add(datafeed_in, NULL); | |
475 | ||
476 | if (sr_dev_open(sdi) != SR_OK) { | |
477 | g_critical("Failed to open device."); | |
478 | return; | |
479 | } | |
480 | ||
481 | if (sr_session_dev_add(sdi) != SR_OK) { | |
482 | g_critical("Failed to add device to session."); | |
483 | sr_session_destroy(); | |
484 | return; | |
485 | } | |
486 | ||
487 | if (opt_config) { | |
488 | if ((devargs = parse_generic_arg(opt_config, FALSE))) { | |
489 | if (set_dev_options(sdi, devargs) != SR_OK) | |
490 | return; | |
491 | g_hash_table_destroy(devargs); | |
492 | } | |
493 | } | |
494 | ||
029d73fe UH |
495 | if (select_channels(sdi) != SR_OK) { |
496 | g_critical("Failed to set channels."); | |
2be182e6 BV |
497 | sr_session_destroy(); |
498 | return; | |
499 | } | |
500 | ||
501 | if (opt_triggers) { | |
502 | if (!(triggerlist = sr_parse_triggerstring(sdi, opt_triggers))) { | |
503 | sr_session_destroy(); | |
504 | return; | |
505 | } | |
029d73fe UH |
506 | max_channels = g_slist_length(sdi->channels); |
507 | for (i = 0; i < max_channels; i++) { | |
2be182e6 BV |
508 | if (triggerlist[i]) { |
509 | sr_dev_trigger_set(sdi, i, triggerlist[i]); | |
510 | g_free(triggerlist[i]); | |
511 | } | |
512 | } | |
513 | g_free(triggerlist); | |
514 | } | |
515 | ||
516 | if (opt_continuous) { | |
517 | if (!sr_dev_has_option(sdi, SR_CONF_CONTINUOUS)) { | |
518 | g_critical("This device does not support continuous sampling."); | |
519 | sr_session_destroy(); | |
520 | return; | |
521 | } | |
522 | } | |
523 | ||
524 | if (opt_time) { | |
525 | if (set_limit_time(sdi) != SR_OK) { | |
526 | sr_session_destroy(); | |
527 | return; | |
528 | } | |
529 | } | |
530 | ||
531 | if (opt_samples) { | |
532 | if ((sr_parse_sizestring(opt_samples, &limit_samples) != SR_OK)) { | |
533 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
534 | sr_session_destroy(); | |
535 | return; | |
536 | } | |
02c65935 BV |
537 | if (sr_config_list(sdi->driver, sdi, NULL, |
538 | SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) { | |
c7a5cb12 BV |
539 | /* The device has no compression, or compression is turned |
540 | * off, and publishes its sample memory size. */ | |
02c65935 | 541 | g_variant_get(gvar, "(tt)", &min_samples, &max_samples); |
be781321 | 542 | g_variant_unref(gvar); |
02c65935 BV |
543 | if (limit_samples < min_samples) { |
544 | g_critical("The device stores at least %"PRIu64 | |
545 | " samples with the current settings.", min_samples); | |
546 | } | |
c7a5cb12 BV |
547 | if (limit_samples > max_samples) { |
548 | g_critical("The device can store only %"PRIu64 | |
549 | " samples with the current settings.", max_samples); | |
550 | } | |
551 | } | |
2be182e6 BV |
552 | gvar = g_variant_new_uint64(limit_samples); |
553 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, gvar) != SR_OK) { | |
554 | g_critical("Failed to configure sample limit."); | |
555 | sr_session_destroy(); | |
556 | return; | |
557 | } | |
558 | } | |
559 | ||
560 | if (opt_frames) { | |
561 | if ((sr_parse_sizestring(opt_frames, &limit_frames) != SR_OK)) { | |
562 | g_critical("Invalid sample limit '%s'.", opt_samples); | |
563 | sr_session_destroy(); | |
564 | return; | |
565 | } | |
566 | gvar = g_variant_new_uint64(limit_frames); | |
567 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, gvar) != SR_OK) { | |
568 | g_critical("Failed to configure frame limit."); | |
569 | sr_session_destroy(); | |
570 | return; | |
571 | } | |
572 | } | |
573 | ||
574 | if (sr_session_start() != SR_OK) { | |
575 | g_critical("Failed to start session."); | |
576 | sr_session_destroy(); | |
577 | return; | |
578 | } | |
579 | ||
580 | if (opt_continuous) | |
581 | add_anykey(); | |
582 | ||
583 | sr_session_run(); | |
584 | ||
585 | if (opt_continuous) | |
586 | clear_anykey(); | |
587 | ||
588 | sr_session_datafeed_callback_remove_all(); | |
589 | sr_session_destroy(); | |
590 | g_slist_free(devices); | |
591 | ||
592 | } | |
593 | ||
6ea663a7 BV |
594 | void save_chunk_logic(uint8_t *data, uint64_t data_len, int unitsize) |
595 | { | |
596 | static uint8_t *buf = NULL; | |
597 | static int buf_len = 0; | |
598 | static int last_unitsize = 0; | |
599 | int max; | |
600 | ||
601 | if (!buf) | |
602 | buf = g_malloc(SAVE_CHUNK_SIZE); | |
603 | ||
604 | if (buf_len + data_len > SAVE_CHUNK_SIZE) { | |
605 | max = (SAVE_CHUNK_SIZE - buf_len) / unitsize * unitsize; | |
606 | memcpy(buf + buf_len, data, max); | |
607 | sr_session_append(opt_output_file, buf, unitsize, | |
608 | (buf_len + max) / unitsize); | |
609 | memcpy(buf, data + max, data_len - max); | |
610 | buf_len = data_len - max; | |
efe1b57d | 611 | } else if (data_len == 0 && last_unitsize != 0) { |
6ea663a7 BV |
612 | /* End of data, flush the buffer out. */ |
613 | sr_session_append(opt_output_file, buf, last_unitsize, | |
614 | buf_len / last_unitsize); | |
615 | } else { | |
616 | /* Buffer chunk. */ | |
617 | memcpy(buf + buf_len, data, data_len); | |
618 | buf_len += data_len; | |
619 | } | |
620 | last_unitsize = unitsize; | |
621 | ||
622 | } |