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